From: Charles Plessy Date: Mon, 14 Feb 2011 09:52:55 +0000 (+0900) Subject: Imported Upstream version 0.4 X-Git-Tag: archive/raspbian/0.22.0+ds-1+rpi1~1^2^2~434^2~5 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=26f0309da73972c169b6e5be6645ba0f1fb072fe;p=python-pysam.git Imported Upstream version 0.4 --- diff --git a/MANIFEST.in b/MANIFEST.in index ac9e5c8..7e07b23 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -8,10 +8,14 @@ include COPYING include INSTALL include KNOWN_BUGS include THANKS +include ez_setup.py include pysam/csamtools.pxd +include pysam/csamtools.pyx +include pysam/csamtools.c include pysam/ctabix.pxd include pysam/pysam_util.h include samtools/*.h +include samtools/*/*.h include tabix/*.h # pysam tests @@ -25,6 +29,9 @@ include tests/ex5.sam include tests/ex6.sam include tests/ex7.sam include tests/ex8.sam +include tests/ex9_fail.bam +include tests/ex9_nofail.bam +include tests/ex10.sam include tests/example.py include tests/pysam_test.py include tests/segfault_tests.py diff --git a/PKG-INFO b/PKG-INFO index 8c788c7..94b8b6e 100644 --- a/PKG-INFO +++ b/PKG-INFO @@ -1,6 +1,6 @@ -Metadata-Version: 1.0 +Metadata-Version: 1.1 Name: pysam -Version: 0.3.1 +Version: 0.4 Summary: pysam Home-page: http://code.google.com/p/pysam/ Author: Andreas Heger @@ -13,3 +13,4 @@ Description: Platform: ALL +Requires: cython (>=0.12) diff --git a/ez_setup.py b/ez_setup.py new file mode 100644 index 0000000..1ff1d3e --- /dev/null +++ b/ez_setup.py @@ -0,0 +1,284 @@ +#!python +"""Bootstrap setuptools installation + +If you want to use setuptools in your package's setup.py, just include this +file in the same directory with it, and add this to the top of your setup.py:: + + from ez_setup import use_setuptools + use_setuptools() + +If you want to require a specific version of setuptools, set a download +mirror, or use an alternate download directory, you can do so by supplying +the appropriate options to ``use_setuptools()``. + +This file can also be run as a script to install or upgrade setuptools. +""" +import sys +DEFAULT_VERSION = "0.6c11" +DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3] + +md5_data = { + 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca', + 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb', + 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b', + 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a', + 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618', + 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac', + 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5', + 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4', + 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c', + 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b', + 'setuptools-0.6c10-py2.3.egg': 'ce1e2ab5d3a0256456d9fc13800a7090', + 'setuptools-0.6c10-py2.4.egg': '57d6d9d6e9b80772c59a53a8433a5dd4', + 'setuptools-0.6c10-py2.5.egg': 'de46ac8b1c97c895572e5e8596aeb8c7', + 'setuptools-0.6c10-py2.6.egg': '58ea40aef06da02ce641495523a0b7f5', + 'setuptools-0.6c11-py2.3.egg': '2baeac6e13d414a9d28e7ba5b5a596de', + 'setuptools-0.6c11-py2.4.egg': 'bd639f9b0eac4c42497034dec2ec0c2b', + 'setuptools-0.6c11-py2.5.egg': '64c94f3bf7a72a13ec83e0b24f2749b2', + 'setuptools-0.6c11-py2.6.egg': 'bfa92100bd772d5a213eedd356d64086', + 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27', + 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277', + 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa', + 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e', + 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e', + 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f', + 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2', + 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc', + 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167', + 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64', + 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d', + 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20', + 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab', + 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53', + 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2', + 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e', + 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372', + 'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902', + 'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de', + 'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b', + 'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03', + 'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a', + 'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6', + 'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a', +} + +import sys, os +try: from hashlib import md5 +except ImportError: from md5 import md5 + +def _validate_md5(egg_name, data): + if egg_name in md5_data: + digest = md5(data).hexdigest() + if digest != md5_data[egg_name]: + print >>sys.stderr, ( + "md5 validation of %s failed! (Possible download problem?)" + % egg_name + ) + sys.exit(2) + return data + +def use_setuptools( + version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, + download_delay=15 +): + """Automatically find/download setuptools and make it available on sys.path + + `version` should be a valid setuptools version number that is available + as an egg for download under the `download_base` URL (which should end with + a '/'). `to_dir` is the directory where setuptools will be downloaded, if + it is not already available. If `download_delay` is specified, it should + be the number of seconds that will be paused before initiating a download, + should one be required. If an older version of setuptools is installed, + this routine will print a message to ``sys.stderr`` and raise SystemExit in + an attempt to abort the calling script. + """ + was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules + def do_download(): + egg = download_setuptools(version, download_base, to_dir, download_delay) + sys.path.insert(0, egg) + import setuptools; setuptools.bootstrap_install_from = egg + try: + import pkg_resources + except ImportError: + return do_download() + try: + pkg_resources.require("setuptools>="+version); return + except pkg_resources.VersionConflict, e: + if was_imported: + print >>sys.stderr, ( + "The required version of setuptools (>=%s) is not available, and\n" + "can't be installed while this script is running. Please install\n" + " a more recent version first, using 'easy_install -U setuptools'." + "\n\n(Currently using %r)" + ) % (version, e.args[0]) + sys.exit(2) + else: + del pkg_resources, sys.modules['pkg_resources'] # reload ok + return do_download() + except pkg_resources.DistributionNotFound: + return do_download() + +def download_setuptools( + version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, + delay = 15 +): + """Download setuptools from a specified location and return its filename + + `version` should be a valid setuptools version number that is available + as an egg for download under the `download_base` URL (which should end + with a '/'). `to_dir` is the directory where the egg will be downloaded. + `delay` is the number of seconds to pause before an actual download attempt. + """ + import urllib2, shutil + egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3]) + url = download_base + egg_name + saveto = os.path.join(to_dir, egg_name) + src = dst = None + if not os.path.exists(saveto): # Avoid repeated downloads + try: + from distutils import log + if delay: + log.warn(""" +--------------------------------------------------------------------------- +This script requires setuptools version %s to run (even to display +help). I will attempt to download it for you (from +%s), but +you may need to enable firewall access for this script first. +I will start the download in %d seconds. + +(Note: if this machine does not have network access, please obtain the file + + %s + +and place it in this directory before rerunning this script.) +---------------------------------------------------------------------------""", + version, download_base, delay, url + ); from time import sleep; sleep(delay) + log.warn("Downloading %s", url) + src = urllib2.urlopen(url) + # Read/write all in one block, so we don't create a corrupt file + # if the download is interrupted. + data = _validate_md5(egg_name, src.read()) + dst = open(saveto,"wb"); dst.write(data) + finally: + if src: src.close() + if dst: dst.close() + return os.path.realpath(saveto) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +def main(argv, version=DEFAULT_VERSION): + """Install or upgrade setuptools and EasyInstall""" + try: + import setuptools + except ImportError: + egg = None + try: + egg = download_setuptools(version, delay=0) + sys.path.insert(0,egg) + from setuptools.command.easy_install import main + return main(list(argv)+[egg]) # we're done here + finally: + if egg and os.path.exists(egg): + os.unlink(egg) + else: + if setuptools.__version__ == '0.0.1': + print >>sys.stderr, ( + "You have an obsolete version of setuptools installed. Please\n" + "remove it from your system entirely before rerunning this script." + ) + sys.exit(2) + + req = "setuptools>="+version + import pkg_resources + try: + pkg_resources.require(req) + except pkg_resources.VersionConflict: + try: + from setuptools.command.easy_install import main + except ImportError: + from easy_install import main + main(list(argv)+[download_setuptools(delay=0)]) + sys.exit(0) # try to force an exit + else: + if argv: + from setuptools.command.easy_install import main + main(argv) + else: + print "Setuptools version",version,"or greater has been installed." + print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' + +def update_md5(filenames): + """Update our built-in md5 registry""" + + import re + + for name in filenames: + base = os.path.basename(name) + f = open(name,'rb') + md5_data[base] = md5(f.read()).hexdigest() + f.close() + + data = [" %r: %r,\n" % it for it in md5_data.items()] + data.sort() + repl = "".join(data) + + import inspect + srcfile = inspect.getsourcefile(sys.modules[__name__]) + f = open(srcfile, 'rb'); src = f.read(); f.close() + + match = re.search("\nmd5_data = {\n([^}]+)}", src) + if not match: + print >>sys.stderr, "Internal error!" + sys.exit(2) + + src = src[:match.start(1)] + repl + src[match.end(1):] + f = open(srcfile,'w') + f.write(src) + f.close() + + +if __name__=='__main__': + if len(sys.argv)>2 and sys.argv[1]=='--md5update': + update_md5(sys.argv[2:]) + else: + main(sys.argv[1:]) + + + + + + diff --git a/pysam.egg-info/PKG-INFO b/pysam.egg-info/PKG-INFO new file mode 100644 index 0000000..94b8b6e --- /dev/null +++ b/pysam.egg-info/PKG-INFO @@ -0,0 +1,16 @@ +Metadata-Version: 1.1 +Name: pysam +Version: 0.4 +Summary: pysam +Home-page: http://code.google.com/p/pysam/ +Author: Andreas Heger +Author-email: andreas.heger@gmail.com +License: MIT +Description: + + pysam + ***** + + +Platform: ALL +Requires: cython (>=0.12) diff --git a/pysam.egg-info/SOURCES.txt b/pysam.egg-info/SOURCES.txt new file mode 100644 index 0000000..37688d0 --- /dev/null +++ b/pysam.egg-info/SOURCES.txt @@ -0,0 +1,124 @@ +COPYING +INSTALL +KNOWN_BUGS +MANIFEST.in +THANKS +ez_setup.py +setup.cfg +setup.py +pysam/Pileup.py +pysam/__init__.py +pysam/csamtools.c +pysam/csamtools.pxd +pysam/csamtools.pyx +pysam/ctabix.pxd +pysam/ctabix.pyx +pysam/namedtuple.py +pysam/pysam_util.c +pysam/pysam_util.h +pysam/tabix_util.c +pysam/version.py +pysam.egg-info/PKG-INFO +pysam.egg-info/SOURCES.txt +pysam.egg-info/dependency_links.txt +pysam.egg-info/requires.txt +pysam.egg-info/top_level.txt +samtools/bam.c.pysam.c +samtools/bam.h +samtools/bam2bcf.c.pysam.c +samtools/bam2bcf.h +samtools/bam2bcf_indel.c.pysam.c +samtools/bam_aux.c.pysam.c +samtools/bam_color.c.pysam.c +samtools/bam_endian.h +samtools/bam_import.c.pysam.c +samtools/bam_index.c.pysam.c +samtools/bam_lpileup.c.pysam.c +samtools/bam_maqcns.c.pysam.c +samtools/bam_maqcns.h +samtools/bam_mate.c.pysam.c +samtools/bam_md.c.pysam.c +samtools/bam_pileup.c.pysam.c +samtools/bam_plcmd.c.pysam.c +samtools/bam_rmdup.c.pysam.c +samtools/bam_rmdupse.c.pysam.c +samtools/bam_sort.c.pysam.c +samtools/bam_stat.c.pysam.c +samtools/bam_tview.c.pysam.c +samtools/bgzf.c.pysam.c +samtools/bgzf.h +samtools/errmod.c.pysam.c +samtools/errmod.h +samtools/faidx.c.pysam.c +samtools/faidx.h +samtools/glf.c.pysam.c +samtools/glf.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.pysam.c +samtools/kprobaln.h +samtools/kseq.h +samtools/ksort.h +samtools/kstring.c.pysam.c +samtools/kstring.h +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 +samtools/sam_header.h +samtools/sam_view.c.pysam.c +samtools/sample.c.pysam.c +samtools/sample.h +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/fet.c.pysam.c +samtools/bcftools/index.c.pysam.c +samtools/bcftools/kfunc.c.pysam.c +samtools/bcftools/ld.c.pysam.c +samtools/bcftools/prob1.c.pysam.c +samtools/bcftools/prob1.h +samtools/bcftools/vcf.c.pysam.c +samtools/win32/xcurses.h +samtools/win32/zconf.h +samtools/win32/zlib.h +tabix/bam_endian.h +tabix/bgzf.c.pysam.c +tabix/bgzf.h +tabix/bgzip.c.pysam.c +tabix/index.c.pysam.c +tabix/khash.h +tabix/knetfile.c.pysam.c +tabix/knetfile.h +tabix/ksort.h +tabix/kstring.c.pysam.c +tabix/kstring.h +tabix/pysam.h +tabix/tabix.h +tests/00README.txt +tests/Makefile +tests/ex1.fa +tests/ex1.sam.gz +tests/ex10.sam +tests/ex3.sam +tests/ex4.sam +tests/ex5.sam +tests/ex6.sam +tests/ex7.sam +tests/ex8.sam +tests/ex9_fail.bam +tests/ex9_nofail.bam +tests/example.gtf.gz +tests/example.gtf.gz.tbi +tests/example.py +tests/pysam_test.py +tests/segfault_tests.py +tests/tabix_test.py \ No newline at end of file diff --git a/pysam.egg-info/dependency_links.txt b/pysam.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/pysam.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/pysam.egg-info/requires.txt b/pysam.egg-info/requires.txt new file mode 100644 index 0000000..98f0172 --- /dev/null +++ b/pysam.egg-info/requires.txt @@ -0,0 +1 @@ +cython>=0.12.1 \ No newline at end of file diff --git a/pysam.egg-info/top_level.txt b/pysam.egg-info/top_level.txt new file mode 100644 index 0000000..acdcd2c --- /dev/null +++ b/pysam.egg-info/top_level.txt @@ -0,0 +1,6 @@ +pysam/namedtuple +ctabix +pysam/Pileup +csamtools +pysam/version +pysam/__init__ diff --git a/pysam/Pileup.py b/pysam/Pileup.py index e182d12..e182689 100644 --- a/pysam/Pileup.py +++ b/pysam/Pileup.py @@ -5,12 +5,12 @@ import pysam PileupSubstitution = collections.namedtuple( "PileupSubstitution", " ".join( (\ "chromosome", - "position", + "pos", "reference_base", - "consensus_base", + "genotype", "consensus_quality", "snp_quality", - "rms_mapping_quality", + "mapping_quality", "coverage", "read_bases", "base_qualities" ) ) ) @@ -18,14 +18,14 @@ PileupSubstitution = collections.namedtuple( "PileupSubstitution", PileupIndel = collections.namedtuple( "PileupIndel", " ".join( (\ "chromosome", - "position", + "pos", "reference_base", "genotype", "consensus_quality", "snp_quality", - "rms_mapping_quality", + "mapping_quality", "coverage", - "first_allelle", + "first_allele", "second_allele", "reads_first", "reads_second", diff --git a/pysam/__init__.py b/pysam/__init__.py index 282ef29..28cd233 100644 --- a/pysam/__init__.py +++ b/pysam/__init__.py @@ -42,8 +42,8 @@ class SamtoolsDispatcher(object): self.parsers = parsers self.stderr = [] - def __call__(self,*args, **kwargs): - '''execute the samtools command + def __call__(self, *args, **kwargs): + '''execute a samtools command ''' retval, stderr, stdout = csamtools._samtools_dispatch( self.dispatch, args ) if retval: raise SamtoolsError( "\n".join( stderr ) ) @@ -54,10 +54,10 @@ class SamtoolsDispatcher(object): # for example: [sam_header_read2] 2 sequences loaded. # Ignore messages like these stderr = [ x for x in stderr \ - if not x.startswith( "[sam_header_read2]" ) or \ - x.startswith("[bam_index_load]") or \ - x.startswith("[bam_sort_core]") or \ - x.startswith("[samopen] SAM header is present") + if not (x.startswith( "[sam_header_read2]" ) or \ + 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 ) ) diff --git a/pysam/csamtools.c b/pysam/csamtools.c new file mode 100644 index 0000000..3175a72 --- /dev/null +++ b/pysam/csamtools.c @@ -0,0 +1,36165 @@ +/* Generated by Cython 0.14 on Thu Feb 10 23:20:05 2011 */ + +#define PY_SSIZE_T_CLEAN +#include "Python.h" +#ifndef Py_PYTHON_H + #error Python headers needed to compile C extensions, please install development version of Python. +#else + +#include /* For offsetof */ +#ifndef offsetof +#define offsetof(type, member) ( (size_t) & ((type*)0) -> member ) +#endif + +#if !defined(WIN32) && !defined(MS_WINDOWS) + #ifndef __stdcall + #define __stdcall + #endif + #ifndef __cdecl + #define __cdecl + #endif + #ifndef __fastcall + #define __fastcall + #endif +#endif + +#ifndef DL_IMPORT + #define DL_IMPORT(t) t +#endif +#ifndef DL_EXPORT + #define DL_EXPORT(t) t +#endif + +#ifndef PY_LONG_LONG + #define PY_LONG_LONG LONG_LONG +#endif + +#if PY_VERSION_HEX < 0x02040000 + #define METH_COEXIST 0 + #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type) + #define PyDict_Contains(d,o) PySequence_Contains(d,o) +#endif + +#if PY_VERSION_HEX < 0x02050000 + typedef int Py_ssize_t; + #define PY_SSIZE_T_MAX INT_MAX + #define PY_SSIZE_T_MIN INT_MIN + #define PY_FORMAT_SIZE_T "" + #define PyInt_FromSsize_t(z) PyInt_FromLong(z) + #define PyInt_AsSsize_t(o) PyInt_AsLong(o) + #define PyNumber_Index(o) PyNumber_Int(o) + #define PyIndex_Check(o) PyNumber_Check(o) + #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message) +#endif + +#if PY_VERSION_HEX < 0x02060000 + #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) + #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) + #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size) + #define PyVarObject_HEAD_INIT(type, size) \ + PyObject_HEAD_INIT(type) size, + #define PyType_Modified(t) + + typedef struct { + void *buf; + PyObject *obj; + Py_ssize_t len; + Py_ssize_t itemsize; + int readonly; + int ndim; + char *format; + Py_ssize_t *shape; + Py_ssize_t *strides; + Py_ssize_t *suboffsets; + void *internal; + } Py_buffer; + + #define PyBUF_SIMPLE 0 + #define PyBUF_WRITABLE 0x0001 + #define PyBUF_FORMAT 0x0004 + #define PyBUF_ND 0x0008 + #define PyBUF_STRIDES (0x0010 | PyBUF_ND) + #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES) + #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES) + #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES) + #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES) + +#endif + +#if PY_MAJOR_VERSION < 3 + #define __Pyx_BUILTIN_MODULE_NAME "__builtin__" +#else + #define __Pyx_BUILTIN_MODULE_NAME "builtins" +#endif + +#if PY_MAJOR_VERSION >= 3 + #define Py_TPFLAGS_CHECKTYPES 0 + #define Py_TPFLAGS_HAVE_INDEX 0 +#endif + +#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3) + #define Py_TPFLAGS_HAVE_NEWBUFFER 0 +#endif + +#if PY_MAJOR_VERSION >= 3 + #define PyBaseString_Type PyUnicode_Type + #define PyStringObject PyUnicodeObject + #define PyString_Type PyUnicode_Type + #define PyString_Check PyUnicode_Check + #define PyString_CheckExact PyUnicode_CheckExact +#endif + +#if PY_VERSION_HEX < 0x02060000 + #define PyBytesObject PyStringObject + #define PyBytes_Type PyString_Type + #define PyBytes_Check PyString_Check + #define PyBytes_CheckExact PyString_CheckExact + #define PyBytes_FromString PyString_FromString + #define PyBytes_FromStringAndSize PyString_FromStringAndSize + #define PyBytes_FromFormat PyString_FromFormat + #define PyBytes_DecodeEscape PyString_DecodeEscape + #define PyBytes_AsString PyString_AsString + #define PyBytes_AsStringAndSize PyString_AsStringAndSize + #define PyBytes_Size PyString_Size + #define PyBytes_AS_STRING PyString_AS_STRING + #define PyBytes_GET_SIZE PyString_GET_SIZE + #define PyBytes_Repr PyString_Repr + #define PyBytes_Concat PyString_Concat + #define PyBytes_ConcatAndDel PyString_ConcatAndDel + #define PySet_Check(obj) PyObject_TypeCheck(obj, &PySet_Type) + #define PyFrozenSet_Check(obj) PyObject_TypeCheck(obj, &PyFrozenSet_Type) +#endif + +#ifndef PySet_CheckExact +# define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type) +#endif + +#if PY_MAJOR_VERSION >= 3 + #define PyIntObject PyLongObject + #define PyInt_Type PyLong_Type + #define PyInt_Check(op) PyLong_Check(op) + #define PyInt_CheckExact(op) PyLong_CheckExact(op) + #define PyInt_FromString PyLong_FromString + #define PyInt_FromUnicode PyLong_FromUnicode + #define PyInt_FromLong PyLong_FromLong + #define PyInt_FromSize_t PyLong_FromSize_t + #define PyInt_FromSsize_t PyLong_FromSsize_t + #define PyInt_AsLong PyLong_AsLong + #define PyInt_AS_LONG PyLong_AS_LONG + #define PyInt_AsSsize_t PyLong_AsSsize_t + #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask + #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask +#endif + +#if PY_MAJOR_VERSION >= 3 + #define PyBoolObject PyLongObject +#endif + + +#if PY_MAJOR_VERSION >= 3 + #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y) + #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y) +#else + #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y) + #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y) +#endif + +#if (PY_MAJOR_VERSION < 3) || (PY_VERSION_HEX >= 0x03010300) + #define __Pyx_PySequence_GetSlice(obj, a, b) PySequence_GetSlice(obj, a, b) + #define __Pyx_PySequence_SetSlice(obj, a, b, value) PySequence_SetSlice(obj, a, b, value) + #define __Pyx_PySequence_DelSlice(obj, a, b) PySequence_DelSlice(obj, a, b) +#else + #define __Pyx_PySequence_GetSlice(obj, a, b) (unlikely(!(obj)) ? \ + (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), (PyObject*)0) : \ + (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_GetSlice(obj, a, b)) : \ + (PyErr_Format(PyExc_TypeError, "'%.200s' object is unsliceable", (obj)->ob_type->tp_name), (PyObject*)0))) + #define __Pyx_PySequence_SetSlice(obj, a, b, value) (unlikely(!(obj)) ? \ + (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \ + (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_SetSlice(obj, a, b, value)) : \ + (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice assignment", (obj)->ob_type->tp_name), -1))) + #define __Pyx_PySequence_DelSlice(obj, a, b) (unlikely(!(obj)) ? \ + (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \ + (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_DelSlice(obj, a, b)) : \ + (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice deletion", (obj)->ob_type->tp_name), -1))) +#endif + +#if PY_MAJOR_VERSION >= 3 + #define PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func)) +#endif + +#if PY_VERSION_HEX < 0x02050000 + #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n))) + #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a)) + #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n))) +#else + #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n)) + #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a)) + #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n)) +#endif + +#if PY_VERSION_HEX < 0x02050000 + #define __Pyx_NAMESTR(n) ((char *)(n)) + #define __Pyx_DOCSTR(n) ((char *)(n)) +#else + #define __Pyx_NAMESTR(n) (n) + #define __Pyx_DOCSTR(n) (n) +#endif + +#ifdef __cplusplus +#define __PYX_EXTERN_C extern "C" +#else +#define __PYX_EXTERN_C extern +#endif + +#if defined(WIN32) || defined(MS_WINDOWS) +#define _USE_MATH_DEFINES +#endif +#include +#define __PYX_HAVE_API__csamtools +#include "string.h" +#include "stdlib.h" +#include "math.h" +#include "stdio.h" +#include "ctype.h" +#include "unistd.h" +#include "fileobject.h" +#include "razf.h" +#include "stdint.h" +#include "bam.h" +#include "sam.h" +#include "glf.h" +#include "bam_maqcns.h" +#include "faidx.h" +#include "pysam_util.h" +#include "pythread.h" + +#ifdef PYREX_WITHOUT_ASSERTIONS +#define CYTHON_WITHOUT_ASSERTIONS +#endif + + +/* inline attribute */ +#ifndef CYTHON_INLINE + #if defined(__GNUC__) + #define CYTHON_INLINE __inline__ + #elif defined(_MSC_VER) + #define CYTHON_INLINE __inline + #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + #define CYTHON_INLINE inline + #else + #define CYTHON_INLINE + #endif +#endif + +/* unused attribute */ +#ifndef CYTHON_UNUSED +# if defined(__GNUC__) +# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) +# define CYTHON_UNUSED __attribute__ ((__unused__)) +# else +# define CYTHON_UNUSED +# endif +# elif defined(__ICC) || defined(__INTEL_COMPILER) +# define CYTHON_UNUSED __attribute__ ((__unused__)) +# else +# define CYTHON_UNUSED +# endif +#endif + +typedef struct {PyObject **p; char *s; const long n; const char* encoding; const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; /*proto*/ + + +/* Type Conversion Predeclarations */ + +#define __Pyx_PyBytes_FromUString(s) PyBytes_FromString((char*)s) +#define __Pyx_PyBytes_AsUString(s) ((unsigned char*) PyBytes_AsString(s)) + +#define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False)) +static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*); +static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x); + +static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*); +static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t); +static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject*); + +#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x)) + + +#ifdef __GNUC__ +/* Test for GCC > 2.95 */ +#if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)) +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) +#else /* __GNUC__ > 2 ... */ +#define likely(x) (x) +#define unlikely(x) (x) +#endif /* __GNUC__ > 2 ... */ +#else /* __GNUC__ */ +#define likely(x) (x) +#define unlikely(x) (x) +#endif /* __GNUC__ */ + +static PyObject *__pyx_m; +static PyObject *__pyx_b; +static PyObject *__pyx_empty_tuple; +static PyObject *__pyx_empty_bytes; +static int __pyx_lineno; +static int __pyx_clineno = 0; +static const char * __pyx_cfilenm= __FILE__; +static const char *__pyx_filename; + + +static const char *__pyx_f[] = { + "csamtools.pyx", + "csamtools.pxd", + "bool.pxd", + "complex.pxd", +}; + +/* Type declarations */ + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":366 + * counter[0] += 1; + * + * ctypedef struct MateData: # <<<<<<<<<<<<<< + * char * name + * bam1_t * mate + */ + +typedef struct { + char *name; + bam1_t *mate; + uint32_t flag; +} __pyx_t_9csamtools_MateData; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1332 + * ##------------------------------------------------------------------- + * ##------------------------------------------------------------------- + * ctypedef struct __iterdata: # <<<<<<<<<<<<<< + * samfile_t * samfile + * bam_iter_t iter + */ + +typedef struct { + samfile_t *samfile; + bam_iter_t iter; + faidx_t *fastafile; + int tid; + char *seq; + int seq_len; +} __pyx_t_9csamtools___iterdata; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1510 + * bam_plp_set_mask( self.pileup_iter, self.mask ) + * + * cdef setupIteratorData( self, # <<<<<<<<<<<<<< + * int tid, + * int start, + */ + +struct __pyx_opt_args_9csamtools_14IteratorColumn_setupIteratorData { + int __pyx_n; + int reopen; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":80 + * return dest + * + * cdef class PileupRead # <<<<<<<<<<<<<< + * cdef makePileupRead( bam_pileup1_t * src ): + * '''fill a PileupRead object from a bam_pileup1_t * object.''' + */ + +struct __pyx_obj_9csamtools_PileupRead { + PyObject_HEAD + struct __pyx_obj_9csamtools_AlignedRead *_alignment; + int32_t _qpos; + int _indel; + int _level; + uint32_t _is_del; + uint32_t _is_head; + uint32_t _is_tail; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1125 + * ##------------------------------------------------------------------- + * ##------------------------------------------------------------------- + * cdef class IteratorRow: # <<<<<<<<<<<<<< + * '''abstract base class for iterators over mapped reads. + * + */ + +struct __pyx_obj_9csamtools_IteratorRow { + PyObject_HEAD +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1283 + * + * + * cdef class IteratorRowAllRefs(IteratorRow): # <<<<<<<<<<<<<< + * """iterates over all mapped reads by chaining iterators over each reference + * """ + */ + +struct __pyx_obj_9csamtools_IteratorRowAllRefs { + struct __pyx_obj_9csamtools_IteratorRow __pyx_base; + struct __pyx_obj_9csamtools_Samfile *samfile; + int tid; + struct __pyx_obj_9csamtools_IteratorRowRegion *rowiter; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":71 + * return dest + * + * cdef class PileupProxy # <<<<<<<<<<<<<< + * cdef makePileupProxy( bam_pileup1_t * plp, int tid, int pos, int n ): + * cdef PileupProxy dest = PileupProxy.__new__(PileupProxy) + */ + +struct __pyx_obj_9csamtools_PileupProxy { + PyObject_HEAD + bam_pileup1_t *plp; + int tid; + int pos; + int n_pu; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1144 + * pass + * + * cdef class IteratorRowRegion(IteratorRow): # <<<<<<<<<<<<<< + * """*(Samfile samfile, int tid, int beg, int end, int reopen = True )* + * + */ + +struct __pyx_obj_9csamtools_IteratorRowRegion { + struct __pyx_obj_9csamtools_IteratorRow __pyx_base; + struct __pyx_vtabstruct_9csamtools_IteratorRowRegion *__pyx_vtab; + bam_iter_t iter; + bam1_t *b; + int retval; + struct __pyx_obj_9csamtools_Samfile *samfile; + samfile_t *fp; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":222 + * ## Public methods + * ###################################################################### + * cdef class Fastafile: # <<<<<<<<<<<<<< + * '''*(filename)* + * + */ + +struct __pyx_obj_9csamtools_Fastafile { + PyObject_HEAD + struct __pyx_vtabstruct_9csamtools_Fastafile *__pyx_vtab; + char *_filename; + faidx_t *fastafile; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2892 + * return call + * + * cdef class IndelCall: # <<<<<<<<<<<<<< + * '''the results of an indel call.''' + * cdef int _tid + */ + +struct __pyx_obj_9csamtools_IndelCall { + PyObject_HEAD + int _tid; + int _pos; + int _coverage; + int _rms_mapping_quality; + bam_maqindel_ret_t *_r; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2981 + * bam_maqindel_ret_destroy(self._r) + * + * cdef class IndelCallerBase: # <<<<<<<<<<<<<< + * '''Base class for SNP callers. + * + */ + +struct __pyx_obj_9csamtools_IndelCallerBase { + PyObject_HEAD + bam_maqindel_opt_t *options; + struct __pyx_obj_9csamtools_IteratorColumn *iter; + int cap_mapQ; + int max_depth; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3119 + * return self._call() + * + * cdef class IteratorIndelCalls( IndelCallerBase ): # <<<<<<<<<<<<<< + * """*(IteratorColumn iterator)* + * + */ + +struct __pyx_obj_9csamtools_IteratorIndelCalls { + struct __pyx_obj_9csamtools_IndelCallerBase __pyx_base; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2662 + * + * + * cdef class SNPCallerBase: # <<<<<<<<<<<<<< + * '''Base class for SNP callers. + * + */ + +struct __pyx_obj_9csamtools_SNPCallerBase { + PyObject_HEAD + struct __pyx_vtabstruct_9csamtools_SNPCallerBase *__pyx_vtab; + bam_maqcns_t *c; + struct __pyx_obj_9csamtools_IteratorColumn *iter; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2735 + * sys.stdout.flush() + * + * cdef class IteratorSNPCalls( SNPCallerBase ): # <<<<<<<<<<<<<< + * """*(IteratorColumn iterator)* + * + */ + +struct __pyx_obj_9csamtools_IteratorSNPCalls { + struct __pyx_obj_9csamtools_SNPCallerBase __pyx_base; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1397 + * return ret + * + * cdef class IteratorColumn: # <<<<<<<<<<<<<< + * '''abstract base class for iterators over columns. + * + */ + +struct __pyx_obj_9csamtools_IteratorColumn { + PyObject_HEAD + struct __pyx_vtabstruct_9csamtools_IteratorColumn *__pyx_vtab; + struct __pyx_obj_9csamtools_IteratorRowRegion *iter; + int tid; + int pos; + int n_plp; + int mask; + const bam_pileup1_t * plp; + bam_plp_t pileup_iter; + __pyx_t_9csamtools___iterdata iterdata; + struct __pyx_obj_9csamtools_Samfile *samfile; + struct __pyx_obj_9csamtools_Fastafile *fastafile; + PyObject *stepper; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1567 + * self.iterdata.seq = NULL + * + * cdef class IteratorColumnRegion(IteratorColumn): # <<<<<<<<<<<<<< + * '''iterates over a region only. + * ''' + */ + +struct __pyx_obj_9csamtools_IteratorColumnRegion { + struct __pyx_obj_9csamtools_IteratorColumn __pyx_base; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2810 + * return call + * + * cdef class SNPCaller( SNPCallerBase ): # <<<<<<<<<<<<<< + * '''*(IteratorColumn iterator_column )* + * + */ + +struct __pyx_obj_9csamtools_SNPCaller { + struct __pyx_obj_9csamtools_SNPCallerBase __pyx_base; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1596 + * self.n_plp ) + * + * cdef class IteratorColumnAllRefs(IteratorColumn): # <<<<<<<<<<<<<< + * """iterates over all columns by chaining iterators over each reference + * """ + */ + +struct __pyx_obj_9csamtools_IteratorColumnAllRefs { + struct __pyx_obj_9csamtools_IteratorColumn __pyx_base; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3076 + * return call + * + * cdef class IndelCaller( IndelCallerBase ): # <<<<<<<<<<<<<< + * '''*(IteratorColumn iterator_column )* + * + */ + +struct __pyx_obj_9csamtools_IndelCaller { + struct __pyx_obj_9csamtools_IndelCallerBase __pyx_base; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":393 + * + * + * cdef class Samfile: # <<<<<<<<<<<<<< + * '''*(filename, mode='r', template = None, referencenames = None, referencelengths = None, text = NULL, header = None)* + * + */ + +struct __pyx_obj_9csamtools_Samfile { + PyObject_HEAD + struct __pyx_vtabstruct_9csamtools_Samfile *__pyx_vtab; + char *_filename; + samfile_t *samfile; + bam_index_t *index; + int isbam; + int isremote; + bam1_t *b; + char *mode; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2605 + * return retval, out_stderr, out_stdout + * + * cdef class SNPCall: # <<<<<<<<<<<<<< + * '''the results of a SNP call.''' + * cdef int _tid + */ + +struct __pyx_obj_9csamtools_SNPCall { + PyObject_HEAD + int _tid; + int _pos; + char _reference_base; + char _genotype; + int _consensus_quality; + int _snp_quality; + int _rms_mapping_quality; + int _coverage; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1224 + * samclose( self.fp ) + * + * cdef class IteratorRowAll(IteratorRow): # <<<<<<<<<<<<<< + * """*(Samfile samfile, int reopen = True)* + * + */ + +struct __pyx_obj_9csamtools_IteratorRowAll { + struct __pyx_obj_9csamtools_IteratorRow __pyx_base; + struct __pyx_vtabstruct_9csamtools_IteratorRowAll *__pyx_vtab; + bam1_t *b; + samfile_t *fp; +}; + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":64 + * ## private factory methods + * ##################################################################### + * cdef class AlignedRead # <<<<<<<<<<<<<< + * cdef makeAlignedRead(bam1_t * src): + * '''enter src into AlignedRead.''' + */ + +struct __pyx_obj_9csamtools_AlignedRead { + PyObject_HEAD + bam1_t *_delegate; +}; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1397 + * return ret + * + * cdef class IteratorColumn: # <<<<<<<<<<<<<< + * '''abstract base class for iterators over columns. + * + */ + +struct __pyx_vtabstruct_9csamtools_IteratorColumn { + int (*cnext)(struct __pyx_obj_9csamtools_IteratorColumn *); + char *(*getSequence)(struct __pyx_obj_9csamtools_IteratorColumn *); + PyObject *(*setMask)(struct __pyx_obj_9csamtools_IteratorColumn *, PyObject *); + PyObject *(*setupIteratorData)(struct __pyx_obj_9csamtools_IteratorColumn *, int, int, int, struct __pyx_opt_args_9csamtools_14IteratorColumn_setupIteratorData *__pyx_optional_args); + PyObject *(*reset)(struct __pyx_obj_9csamtools_IteratorColumn *, PyObject *, PyObject *, PyObject *); +}; +static struct __pyx_vtabstruct_9csamtools_IteratorColumn *__pyx_vtabptr_9csamtools_IteratorColumn; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1567 + * self.iterdata.seq = NULL + * + * cdef class IteratorColumnRegion(IteratorColumn): # <<<<<<<<<<<<<< + * '''iterates over a region only. + * ''' + */ + +struct __pyx_vtabstruct_9csamtools_IteratorColumnRegion { + struct __pyx_vtabstruct_9csamtools_IteratorColumn __pyx_base; +}; +static struct __pyx_vtabstruct_9csamtools_IteratorColumnRegion *__pyx_vtabptr_9csamtools_IteratorColumnRegion; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2662 + * + * + * cdef class SNPCallerBase: # <<<<<<<<<<<<<< + * '''Base class for SNP callers. + * + */ + +struct __pyx_vtabstruct_9csamtools_SNPCallerBase { + PyObject *(*__dump)(struct __pyx_obj_9csamtools_SNPCallerBase *, glf1_t *, uint32_t, int); +}; +static struct __pyx_vtabstruct_9csamtools_SNPCallerBase *__pyx_vtabptr_9csamtools_SNPCallerBase; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2810 + * return call + * + * cdef class SNPCaller( SNPCallerBase ): # <<<<<<<<<<<<<< + * '''*(IteratorColumn iterator_column )* + * + */ + +struct __pyx_vtabstruct_9csamtools_SNPCaller { + struct __pyx_vtabstruct_9csamtools_SNPCallerBase __pyx_base; +}; +static struct __pyx_vtabstruct_9csamtools_SNPCaller *__pyx_vtabptr_9csamtools_SNPCaller; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1144 + * pass + * + * cdef class IteratorRowRegion(IteratorRow): # <<<<<<<<<<<<<< + * """*(Samfile samfile, int tid, int beg, int end, int reopen = True )* + * + */ + +struct __pyx_vtabstruct_9csamtools_IteratorRowRegion { + bam1_t *(*getCurrent)(struct __pyx_obj_9csamtools_IteratorRowRegion *); + int (*cnext)(struct __pyx_obj_9csamtools_IteratorRowRegion *); +}; +static struct __pyx_vtabstruct_9csamtools_IteratorRowRegion *__pyx_vtabptr_9csamtools_IteratorRowRegion; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1596 + * self.n_plp ) + * + * cdef class IteratorColumnAllRefs(IteratorColumn): # <<<<<<<<<<<<<< + * """iterates over all columns by chaining iterators over each reference + * """ + */ + +struct __pyx_vtabstruct_9csamtools_IteratorColumnAllRefs { + struct __pyx_vtabstruct_9csamtools_IteratorColumn __pyx_base; +}; +static struct __pyx_vtabstruct_9csamtools_IteratorColumnAllRefs *__pyx_vtabptr_9csamtools_IteratorColumnAllRefs; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":393 + * + * + * cdef class Samfile: # <<<<<<<<<<<<<< + * '''*(filename, mode='r', template = None, referencenames = None, referencelengths = None, text = NULL, header = None)* + * + */ + +struct __pyx_vtabstruct_9csamtools_Samfile { + bam_header_t *(*_buildHeader)(struct __pyx_obj_9csamtools_Samfile *, PyObject *); + bam1_t *(*getCurrent)(struct __pyx_obj_9csamtools_Samfile *); + int (*cnext)(struct __pyx_obj_9csamtools_Samfile *); +}; +static struct __pyx_vtabstruct_9csamtools_Samfile *__pyx_vtabptr_9csamtools_Samfile; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":222 + * ## Public methods + * ###################################################################### + * cdef class Fastafile: # <<<<<<<<<<<<<< + * '''*(filename)* + * + */ + +struct __pyx_vtabstruct_9csamtools_Fastafile { + char *(*_fetch)(struct __pyx_obj_9csamtools_Fastafile *, char *, int, int, int *); +}; +static struct __pyx_vtabstruct_9csamtools_Fastafile *__pyx_vtabptr_9csamtools_Fastafile; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1224 + * samclose( self.fp ) + * + * cdef class IteratorRowAll(IteratorRow): # <<<<<<<<<<<<<< + * """*(Samfile samfile, int reopen = True)* + * + */ + +struct __pyx_vtabstruct_9csamtools_IteratorRowAll { + bam1_t *(*getCurrent)(struct __pyx_obj_9csamtools_IteratorRowAll *); + int (*cnext)(struct __pyx_obj_9csamtools_IteratorRowAll *); +}; +static struct __pyx_vtabstruct_9csamtools_IteratorRowAll *__pyx_vtabptr_9csamtools_IteratorRowAll; + + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2735 + * sys.stdout.flush() + * + * cdef class IteratorSNPCalls( SNPCallerBase ): # <<<<<<<<<<<<<< + * """*(IteratorColumn iterator)* + * + */ + +struct __pyx_vtabstruct_9csamtools_IteratorSNPCalls { + struct __pyx_vtabstruct_9csamtools_SNPCallerBase __pyx_base; +}; +static struct __pyx_vtabstruct_9csamtools_IteratorSNPCalls *__pyx_vtabptr_9csamtools_IteratorSNPCalls; + +#ifndef CYTHON_REFNANNY + #define CYTHON_REFNANNY 0 +#endif + +#if CYTHON_REFNANNY + typedef struct { + void (*INCREF)(void*, PyObject*, int); + void (*DECREF)(void*, PyObject*, int); + void (*GOTREF)(void*, PyObject*, int); + void (*GIVEREF)(void*, PyObject*, int); + void* (*SetupContext)(const char*, int, const char*); + void (*FinishContext)(void**); + } __Pyx_RefNannyAPIStruct; + static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL; + 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; + } + #define __Pyx_RefNannySetupContext(name) void *__pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__) + #define __Pyx_RefNannyFinishContext() __Pyx_RefNanny->FinishContext(&__pyx_refnanny) + #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__) + #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__) + #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__) + #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__) + #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r);} } while(0) +#else + #define __Pyx_RefNannySetupContext(name) + #define __Pyx_RefNannyFinishContext() + #define __Pyx_INCREF(r) Py_INCREF(r) + #define __Pyx_DECREF(r) Py_DECREF(r) + #define __Pyx_GOTREF(r) + #define __Pyx_GIVEREF(r) + #define __Pyx_XDECREF(r) Py_XDECREF(r) +#endif /* CYTHON_REFNANNY */ +#define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);} } while(0) +#define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r);} } while(0) + +static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/ + +#ifndef CYTHON_PROFILE + #define CYTHON_PROFILE 1 +#endif + +#ifndef CYTHON_PROFILE_REUSE_FRAME + #define CYTHON_PROFILE_REUSE_FRAME 0 +#endif + +#if CYTHON_PROFILE + + #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_DECREF(__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)) { \ + __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) { \ + PyObject *exc_info = __Pyx_GetExceptionTuple(); \ + if (exc_info) { \ + PyThreadState_GET()->c_profilefunc( \ + PyThreadState_GET()->c_profileobj, __pyx_frame, PyTrace_EXCEPTION, exc_info); \ + Py_DECREF(exc_info); \ + } \ + } + + #define __Pyx_TraceReturn(result) \ + if (unlikely(__Pyx_use_tracing) && PyThreadState_GET()->use_tracing && PyThreadState_GET()->c_profilefunc) { \ + PyThreadState_GET()->c_profilefunc( \ + PyThreadState_GET()->c_profileobj, __pyx_frame, PyTrace_RETURN, (PyObject*)result); \ + CYTHON_FRAME_DEL; \ + } + + 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 */ + +static CYTHON_INLINE PyObject* __Pyx_tp_new(PyObject* type_obj) { + return (PyObject*) (((PyTypeObject*)(type_obj))->tp_new( + (PyTypeObject*)(type_obj), __pyx_empty_tuple, NULL)); +} + +static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/ + +static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index); + +static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected); + +static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/ +static int __Pyx_EndUnpack(PyObject *, Py_ssize_t expected); /*proto*/ + +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*/ + +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_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, const char* function_name); /*proto*/ + +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 int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, + const char *name, int exact); /*proto*/ + + +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; +} + + +#define __Pyx_GetItemInt_List(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \ + __Pyx_GetItemInt_List_Fast(o, i) : \ + __Pyx_GetItemInt_Generic(o, to_py_func(i))) + +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i) { + if (likely(o != Py_None)) { + if (likely((0 <= i) & (i < PyList_GET_SIZE(o)))) { + PyObject *r = PyList_GET_ITEM(o, i); + Py_INCREF(r); + return r; + } + else if ((-PyList_GET_SIZE(o) <= i) & (i < 0)) { + PyObject *r = PyList_GET_ITEM(o, PyList_GET_SIZE(o) + i); + Py_INCREF(r); + return r; + } + } + return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); +} + +#define __Pyx_GetItemInt_Tuple(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \ + __Pyx_GetItemInt_Tuple_Fast(o, i) : \ + __Pyx_GetItemInt_Generic(o, to_py_func(i))) + +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i) { + if (likely(o != Py_None)) { + if (likely((0 <= i) & (i < PyTuple_GET_SIZE(o)))) { + PyObject *r = PyTuple_GET_ITEM(o, i); + Py_INCREF(r); + return r; + } + else if ((-PyTuple_GET_SIZE(o) <= i) & (i < 0)) { + PyObject *r = PyTuple_GET_ITEM(o, PyTuple_GET_SIZE(o) + i); + Py_INCREF(r); + return r; + } + } + return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); +} + + +#define __Pyx_GetItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \ + __Pyx_GetItemInt_Fast(o, i) : \ + __Pyx_GetItemInt_Generic(o, to_py_func(i))) + +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i) { + PyObject *r; + if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) { + r = PyList_GET_ITEM(o, i); + Py_INCREF(r); + } + else if (PyTuple_CheckExact(o) && ((0 <= i) & (i < PyTuple_GET_SIZE(o)))) { + r = PyTuple_GET_ITEM(o, i); + Py_INCREF(r); + } + else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_item && (likely(i >= 0))) { + r = PySequence_GetItem(o, i); + } + else { + r = __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); + } + return r; +} + +static CYTHON_INLINE long __Pyx_NegateNonNeg(long b) { return unlikely(b < 0) ? b : !b; } +static CYTHON_INLINE PyObject* __Pyx_PyBoolOrNull_FromLong(long b) { + return unlikely(b < 0) ? NULL : __Pyx_PyBool_FromLong(b); +} + +static CYTHON_INLINE PyObject* __Pyx_PyObject_Append(PyObject* L, PyObject* x) { + if (likely(PyList_CheckExact(L))) { + if (PyList_Append(L, x) < 0) return NULL; + Py_INCREF(Py_None); + return Py_None; /* this is just to have an accurate signature */ + } + else { + PyObject *r, *m; + m = __Pyx_GetAttrString(L, "append"); + if (!m) return NULL; + r = PyObject_CallFunctionObjArgs(m, x, NULL); + Py_DECREF(m); + return r; + } +} + +static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void); + +#if PY_MAJOR_VERSION >= 3 +static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) { + PyObject *value; + if (unlikely(d == Py_None)) { + __Pyx_RaiseNoneIndexingError(); + return NULL; + } + value = PyDict_GetItemWithError(d, key); + if (unlikely(!value)) { + if (!PyErr_Occurred()) + PyErr_SetObject(PyExc_KeyError, key); + return NULL; + } + Py_INCREF(value); + return value; +} +#else + #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key) +#endif + +static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ + +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)) { + /* these presumably have safe hash functions */ + value = PyDict_GetItem(d, key); + if (unlikely(!value)) { + value = default_value; + } + Py_INCREF(value); + } else { + PyObject *m; + m = __Pyx_GetAttrString(d, "get"); + if (!m) return NULL; + value = PyObject_CallFunctionObjArgs(m, key, + (default_value == Py_None) ? NULL : default_value, NULL); + Py_DECREF(m); + } +#endif + return value; +} + +static CYTHON_INLINE long __Pyx_div_long(long, long); /* proto */ + +static CYTHON_INLINE long __Pyx_mod_long(long, long); /* proto */ + +#define __Pyx_DelItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \ + __Pyx_DelItemInt_Fast(o, i) : \ + __Pyx_DelItem_Generic(o, to_py_func(i))) + +static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) { + int r; + if (!j) return -1; + r = PyObject_DelItem(o, j); + Py_DECREF(j); + return r; +} + +static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i) { + if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && likely(i >= 0)) + return PySequence_DelItem(o, i); + else { + PyObject *j = PyInt_FromSsize_t(i); + return __Pyx_DelItem_Generic(o, j); + } +} + +static CYTHON_INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ +static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ + +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/ + +static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases); /*proto*/ + +static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, + PyObject *modname); /*proto*/ + +#define __pyx_binding_PyCFunctionType_USED 1 + +typedef struct { + PyCFunctionObject func; +} __pyx_binding_PyCFunctionType_object; + +PyTypeObject __pyx_binding_PyCFunctionType_type; +PyTypeObject *__pyx_binding_PyCFunctionType = NULL; + +PyObject *__pyx_binding_PyCFunctionType_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module); /* proto */ +#define __pyx_binding_PyCFunctionType_New(ml, self) __pyx_binding_PyCFunctionType_NewEx(ml, self, NULL) + +int __pyx_binding_PyCFunctionType_init(void); /* proto */ + +static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_uint32_t(uint32_t); + +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ + +static CYTHON_INLINE uint32_t __Pyx_PyInt_from_py_uint32_t(PyObject *); + +static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_int32_t(int32_t); + +static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_int64_t(int64_t); + +static CYTHON_INLINE int32_t __Pyx_PyInt_from_py_int32_t(PyObject *); + +static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_uint8_t(uint8_t); + +static int __Pyx_Print(PyObject*, PyObject *, int); /*proto*/ +#if PY_MAJOR_VERSION >= 3 +static PyObject* __pyx_print = 0; +static PyObject* __pyx_print_kwargs = 0; +#endif + +static int __Pyx_PrintOne(PyObject* stream, PyObject *o); /*proto*/ + +static CYTHON_INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject *); + +static CYTHON_INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject *); + +static CYTHON_INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject *); + +static CYTHON_INLINE char __Pyx_PyInt_AsChar(PyObject *); + +static CYTHON_INLINE short __Pyx_PyInt_AsShort(PyObject *); + +static CYTHON_INLINE int __Pyx_PyInt_AsInt(PyObject *); + +static CYTHON_INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject *); + +static CYTHON_INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject *); + +static CYTHON_INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject *); + +static CYTHON_INLINE int __Pyx_PyInt_AsLongDouble(PyObject *); + +static CYTHON_INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject *); + +static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject *); + +static CYTHON_INLINE long __Pyx_PyInt_AsLong(PyObject *); + +static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(PyObject *); + +static CYTHON_INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject *); + +static CYTHON_INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject *); + +static void __Pyx_WriteUnraisable(const char *name); /*proto*/ + +static CYTHON_INLINE uint64_t __Pyx_PyInt_from_py_uint64_t(PyObject *); + +static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, long size, int strict); /*proto*/ + +static PyObject *__Pyx_ImportModule(const char *name); /*proto*/ + +static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/ + +static void __Pyx_AddTraceback(const char *funcname); /*proto*/ + +static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/ +/* Module declarations from __builtin__ */ + +/* 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 libc.stdio */ + +/* Module declarations from cpython.object */ + +/* Module declarations from cpython.sequence */ + +/* Module declarations from cpython.mapping */ + +/* Module declarations from cpython.iterator */ + +/* Module declarations from cpython.type */ + +/* 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.cobject */ + +/* Module declarations from cpython.oldbuffer */ + +/* Module declarations from cpython.set */ + +/* Module declarations from cpython.buffer */ + +/* Module declarations from cpython.bytes */ + +/* Module declarations from cpython.pycapsule */ + +/* Module declarations from cpython */ + +/* Module declarations from csamtools */ + +static PyTypeObject *__pyx_ptype_9csamtools_file = 0; +static PyTypeObject *__pyx_ptype_9csamtools_AlignedRead = 0; +static PyTypeObject *__pyx_ptype_9csamtools_PileupProxy = 0; +static PyTypeObject *__pyx_ptype_9csamtools_PileupRead = 0; +static PyTypeObject *__pyx_ptype_9csamtools_Fastafile = 0; +static PyTypeObject *__pyx_ptype_9csamtools_Samfile = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorRow = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorRowRegion = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorRowAll = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorRowAllRefs = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorColumn = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorColumnRegion = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorColumnAllRefs = 0; +static PyTypeObject *__pyx_ptype_9csamtools_SNPCall = 0; +static PyTypeObject *__pyx_ptype_9csamtools_SNPCallerBase = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorSNPCalls = 0; +static PyTypeObject *__pyx_ptype_9csamtools_SNPCaller = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IndelCall = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IndelCallerBase = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IndelCaller = 0; +static PyTypeObject *__pyx_ptype_9csamtools_IteratorIndelCalls = 0; +static char *__pyx_v_9csamtools_bam_nt16_rev_table; +static int __pyx_v_9csamtools_max_pos; +static PyObject *__pyx_f_9csamtools_makeAlignedRead(bam1_t *); /*proto*/ +static PyObject *__pyx_f_9csamtools_makePileupProxy(bam_pileup1_t *, int, int, int); /*proto*/ +static PyObject *__pyx_f_9csamtools_makePileupRead(bam_pileup1_t *); /*proto*/ +static int __pyx_f_9csamtools_fetch_callback(bam1_t *, void *); /*proto*/ +static int __pyx_f_9csamtools_pileup_callback(uint32_t, uint32_t, int, bam_pileup1_t *, void *); /*proto*/ +static int __pyx_f_9csamtools_pileup_fetch_callback(bam1_t *, void *); /*proto*/ +static int __pyx_f_9csamtools_count_callback(bam1_t *, void *); /*proto*/ +static int __pyx_f_9csamtools_mate_callback(bam1_t *, void *); /*proto*/ +static int __pyx_f_9csamtools___advance_all(void *, bam1_t *); /*proto*/ +static int __pyx_f_9csamtools___advance_snpcalls(void *, bam1_t *); /*proto*/ +static CYTHON_INLINE int32_t __pyx_f_9csamtools_query_start(bam1_t *); /*proto*/ +static CYTHON_INLINE int32_t __pyx_f_9csamtools_query_end(bam1_t *); /*proto*/ +static CYTHON_INLINE PyObject *__pyx_f_9csamtools_get_seq_range(bam1_t *, uint32_t, uint32_t); /*proto*/ +static CYTHON_INLINE PyObject *__pyx_f_9csamtools_get_qual_range(bam1_t *, uint32_t, uint32_t); /*proto*/ +#define __Pyx_MODULE_NAME "csamtools" +int __pyx_module_is_main_csamtools = 0; + +/* Implementation of csamtools */ +static PyObject *__pyx_builtin_open; +static PyObject *__pyx_builtin_object; +static PyObject *__pyx_builtin_map; +static PyObject *__pyx_builtin_ValueError; +static PyObject *__pyx_builtin_IOError; +static PyObject *__pyx_builtin_NotImplementedError; +static PyObject *__pyx_builtin_KeyError; +static PyObject *__pyx_builtin_StopIteration; +static PyObject *__pyx_builtin_cmp; +static PyObject *__pyx_builtin_chr; +static PyObject *__pyx_builtin_TypeError; +static PyObject *__pyx_builtin_range; +static char __pyx_k_1[] = "\t"; +static char __pyx_k_2[] = "\n"; +static char __pyx_k_3[] = "calling len() on closed file"; +static char __pyx_k_5[] = "could not open file `%s`"; +static char __pyx_k_6[] = "I/O operation on closed file"; +static char __pyx_k_9[] = "no sequence/region supplied."; +static char __pyx_k_11[] = "invalid region: start (%i) > end (%i)"; +static char __pyx_k_12[] = ""; +static char __pyx_k_13[] = "start out of range (%i)"; +static char __pyx_k_14[] = "end out of range (%i)"; +static char __pyx_k_15[] = "%s:%i-%i"; +static char __pyx_k_16[] = "invalid file opening mode `%s`"; +static char __pyx_k_17[] = "http:"; +static char __pyx_k_18[] = "ftp:"; +static char __pyx_k_19[] = "either supply options `template`, `header` or both `referencenames` and `referencelengths` for writing"; +static char __pyx_k_20[] = "unequal names and lengths of reference sequences"; +static char __pyx_k_21[] = "-"; +static char __pyx_k_22[] = "file `%s` not found"; +static char __pyx_k_23[] = "could not open file - is it SAM/BAM format?"; +static char __pyx_k_27[] = ".bai"; +static char __pyx_k_28[] = "error while opening index `%s` "; +static char __pyx_k_30[] = "tid out of range 0<=tid<%i"; +static char __pyx_k_32[] = "[:-]"; +static char __pyx_k_34[] = "invalid reference `%s`"; +static char __pyx_k_35[] = "invalid coordinates: start (%i) > end (%i)"; +static char __pyx_k_37[] = "seek only available in bam files"; +static char __pyx_k_43[] = "fetch called on bamfile without index"; +static char __pyx_k_45[] = "callback functionality requires a region/reference"; +static char __pyx_k_47[] = "no index available for fetch"; +static char __pyx_k_49[] = "fetch called for samfile without header"; +static char __pyx_k_51[] = "fetch for a region is not available for sam files"; +static char __pyx_k_53[] = "callback not implemented yet"; +static char __pyx_k_55[] = "read is unpaired"; +static char __pyx_k_57[] = "mate is unmapped"; +static char __pyx_k_59[] = "mate not found"; +static char __pyx_k_64[] = "counting functionality requires a region/reference"; +static char __pyx_k_67[] = "count for a region is not available for sam files"; +static char __pyx_k_70[] = "no index available for pileup"; +static char __pyx_k_73[] = "pileup of samfiles not implemented yet"; +static char __pyx_k_83[] = "@"; +static char __pyx_k_85[] = "header line without '@': '%s'"; +static char __pyx_k_87[] = "header line with invalid type '%s': '%s'"; +static char __pyx_k_88[] = ":"; +static char __pyx_k_90[] = "unknown field code '%s' in record '%s'"; +static char __pyx_k_91[] = "multiple '%s' lines are not permitted"; +static char __pyx_k_92[] = "@%s"; +static char __pyx_k_93[] = "%s:%s"; +static char __pyx_k_94[] = "invalid type for record %s: %s, expected %s"; +static char __pyx_k_95[] = "incomplete sequence information in '%s'"; +static char __pyx_k_98[] = "no index available for iteration"; +static char __pyx_k__A[] = "A"; +static char __pyx_k__C[] = "C"; +static char __pyx_k__D[] = "D"; +static char __pyx_k__F[] = "F"; +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__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__x[] = "x"; +static char __pyx_k_102[] = "reference sequence for '%s' (tid=%i) not found"; +static char __pyx_k_103[] = "unknown stepper option `%s` in IteratorColumn"; +static char __pyx_k_105[] = "error during iteration"; +static char __pyx_k_108[] = "Invalid clipping in CIGAR string"; +static char __pyx_k_109[] = "quality and sequence mismatch: %i != %i"; +static char __pyx_k_110[] = "%c"; +static char __pyx_k_111[] = "create_string_buffer"; +static char __pyx_k_112[] = "_delegate = bam_dup1(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":69 + * 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; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.makeAlignedRead"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_dest); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":72 + * + * cdef class PileupProxy + * cdef makePileupProxy( bam_pileup1_t * plp, int tid, int pos, int n ): # <<<<<<<<<<<<<< + * cdef PileupProxy dest = PileupProxy.__new__(PileupProxy) + * dest.plp = plp + */ + +static PyObject *__pyx_f_9csamtools_makePileupProxy(bam_pileup1_t *__pyx_v_plp, int __pyx_v_tid, int __pyx_v_pos, int __pyx_v_n) { + struct __pyx_obj_9csamtools_PileupProxy *__pyx_v_dest = 0; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("makePileupProxy"); + __Pyx_TraceCall("makePileupProxy", __pyx_f[0], 72); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":73 + * cdef class PileupProxy + * cdef makePileupProxy( bam_pileup1_t * plp, int tid, int pos, int n ): + * cdef PileupProxy dest = PileupProxy.__new__(PileupProxy) # <<<<<<<<<<<<<< + * dest.plp = plp + * dest.tid = tid + */ + __pyx_t_1 = __Pyx_tp_new(((PyObject*)__pyx_ptype_9csamtools_PileupProxy)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_9csamtools_PileupProxy)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_dest = ((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":74 + * cdef makePileupProxy( bam_pileup1_t * plp, int tid, int pos, int n ): + * cdef PileupProxy dest = PileupProxy.__new__(PileupProxy) + * dest.plp = plp # <<<<<<<<<<<<<< + * dest.tid = tid + * dest.pos = pos + */ + __pyx_v_dest->plp = __pyx_v_plp; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":75 + * 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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":76 + * dest.plp = plp + * dest.tid = tid + * dest.pos = pos # <<<<<<<<<<<<<< + * dest.n = n + * return dest + */ + __pyx_v_dest->pos = __pyx_v_pos; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":77 + * dest.tid = tid + * dest.pos = pos + * dest.n = n # <<<<<<<<<<<<<< + * return dest + * + */ + __pyx_t_1 = PyInt_FromLong(__pyx_v_n); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + if (PyObject_SetAttr(((PyObject *)__pyx_v_dest), __pyx_n_s__n, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":78 + * 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; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.makePileupProxy"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_dest); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":81 + * + * 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_9csamtools_makePileupRead(bam_pileup1_t *__pyx_v_src) { + struct __pyx_obj_9csamtools_PileupRead *__pyx_v_dest = 0; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("makePileupRead"); + __Pyx_TraceCall("makePileupRead", __pyx_f[0], 81); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":83 + * 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(((PyObject*)__pyx_ptype_9csamtools_PileupRead)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_9csamtools_PileupRead)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_dest = ((struct __pyx_obj_9csamtools_PileupRead *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":84 + * '''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_9csamtools_makeAlignedRead(__pyx_v_src->b); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 84; __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_9csamtools_AlignedRead))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 84; __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_9csamtools_AlignedRead *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":85 + * cdef PileupRead dest = PileupRead.__new__(PileupRead) + * dest._alignment = makeAlignedRead( src.b ) + * dest._qpos = src.qpos # <<<<<<<<<<<<<< + * dest._indel = src.indel + * dest._level = src.level + */ + __pyx_v_dest->_qpos = __pyx_v_src->qpos; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":86 + * dest._alignment = makeAlignedRead( src.b ) + * dest._qpos = src.qpos + * dest._indel = src.indel # <<<<<<<<<<<<<< + * dest._level = src.level + * dest._is_del = src.is_del + */ + __pyx_v_dest->_indel = __pyx_v_src->indel; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":87 + * 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_v_dest->_level = __pyx_v_src->level; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":88 + * 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_v_dest->_is_del = __pyx_v_src->is_del; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":89 + * 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_v_dest->_is_head = __pyx_v_src->is_head; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":90 + * dest._is_del = src.is_del + * dest._is_head = src.is_head + * dest._is_tail = src.is_tail # <<<<<<<<<<<<<< + * return dest + * + */ + __pyx_v_dest->_is_tail = __pyx_v_src->is_tail; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":91 + * dest._is_head = src.is_head + * dest._is_tail = src.is_tail + * return dest # <<<<<<<<<<<<<< + * + * ##################################################################### + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_dest)); + __pyx_r = ((PyObject *)__pyx_v_dest); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.makePileupRead"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_dest); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":98 + * ## Generic callbacks for inserting python callbacks. + * ##################################################################### + * cdef int fetch_callback( bam1_t *alignment, void *f): # <<<<<<<<<<<<<< + * '''callback for bam_fetch. + * + */ + +static int __pyx_f_9csamtools_fetch_callback(bam1_t *__pyx_v_alignment, void *__pyx_v_f) { + PyObject *__pyx_v_a; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("fetch_callback"); + __Pyx_TraceCall("fetch_callback", __pyx_f[0], 98); + __pyx_v_a = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":103 + * calls function in *f* with a new :class:`AlignedRead` object as parameter. + * ''' + * a = makeAlignedRead( alignment ) # <<<<<<<<<<<<<< + * (f)(a) + * + */ + __pyx_t_1 = __pyx_f_9csamtools_makeAlignedRead(__pyx_v_alignment); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 103; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_v_a); + __pyx_v_a = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":104 + * ''' + * a = makeAlignedRead( alignment ) + * (f)(a) # <<<<<<<<<<<<<< + * + * class PileupColumn(object): + */ + __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(__pyx_v_a); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_a); + __Pyx_GIVEREF(__pyx_v_a); + __pyx_t_2 = PyObject_Call(((PyObject *)__pyx_v_f), ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_WriteUnraisable("csamtools.fetch_callback"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_a); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":119 + * 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) ) + */ + +static PyObject *__pyx_pf_9csamtools_12PileupColumn_0__str__(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/ +static PyMethodDef __pyx_mdef_9csamtools_12PileupColumn_0__str__ = {__Pyx_NAMESTR("__str__"), (PyCFunction)__pyx_pf_9csamtools_12PileupColumn_0__str__, METH_O, __Pyx_DOCSTR(0)}; +static PyObject *__pyx_pf_9csamtools_12PileupColumn_0__str__(PyObject *__pyx_self, PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + 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; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__str__"); + __Pyx_TraceCall("__str__", __pyx_f[0], 119); + __pyx_self = __pyx_self; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":120 + * ''' + * 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); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":121 + * def __str__(self): + * return "\t".join( map(str, (self.tid, self.pos, self.n))) +\ + * "\n" + "\n".join( map(str, self.pileups) ) # <<<<<<<<<<<<<< + * + * cdef int pileup_callback( uint32_t tid, uint32_t pos, int n, bam_pileup1_t *pl, void *f): + */ + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_1), __pyx_n_s__join); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":120 + * ''' + * def __str__(self): + * return "\t".join( map(str, (self.tid, self.pos, self.n))) +\ # <<<<<<<<<<<<<< + * "\n" + "\n".join( map(str, self.pileups) ) + * + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_4 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__n); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __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 = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_3); + __Pyx_GIVEREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_5, 2, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + __pyx_t_2 = 0; + __pyx_t_3 = 0; + __pyx_t_4 = 0; + __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)((PyObject*)(&PyString_Type)))); + __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_4, 1, ((PyObject *)__pyx_t_5)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_5)); + __pyx_t_5 = 0; + __pyx_t_5 = PyObject_Call(__pyx_builtin_map, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(((PyObject *)__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 = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __pyx_t_4 = PyNumber_Add(__pyx_t_5, ((PyObject *)__pyx_kp_s_2)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":121 + * def __str__(self): + * return "\t".join( map(str, (self.tid, self.pos, self.n))) +\ + * "\n" + "\n".join( map(str, self.pileups) ) # <<<<<<<<<<<<<< + * + * cdef int pileup_callback( uint32_t tid, uint32_t pos, int n, bam_pileup1_t *pl, void *f): + */ + __pyx_t_5 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_2), __pyx_n_s__join); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 121; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__pileups); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 121; __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 = 121; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_1); + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_map, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 121; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__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 = 121; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 121; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_t_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 121; __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; + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + + __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("csamtools.PileupColumn.__str__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":123 + * "\n" + "\n".join( map(str, self.pileups) ) + * + * cdef int pileup_callback( uint32_t tid, uint32_t pos, int n, bam_pileup1_t *pl, void *f): # <<<<<<<<<<<<<< + * '''callback for pileup. + * + */ + +static int __pyx_f_9csamtools_pileup_callback(uint32_t __pyx_v_tid, uint32_t __pyx_v_pos, int __pyx_v_n, bam_pileup1_t *__pyx_v_pl, void *__pyx_v_f) { + PyObject *__pyx_v_p; + PyObject *__pyx_v_pileups; + int __pyx_v_x; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("pileup_callback"); + __Pyx_TraceCall("pileup_callback", __pyx_f[0], 123); + __pyx_v_p = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_pileups = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":140 + * ''' + * + * p = PileupColumn() # <<<<<<<<<<<<<< + * p.tid = tid + * p.pos = pos + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__PileupColumn); 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); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 140; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_p); + __pyx_v_p = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":141 + * + * p = PileupColumn() + * p.tid = tid # <<<<<<<<<<<<<< + * p.pos = pos + * p.n = n + */ + __pyx_t_2 = __Pyx_PyInt_to_py_uint32_t(__pyx_v_tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + if (PyObject_SetAttr(__pyx_v_p, __pyx_n_s__tid, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":142 + * p = PileupColumn() + * p.tid = tid + * p.pos = pos # <<<<<<<<<<<<<< + * p.n = n + * pileups = [] + */ + __pyx_t_2 = __Pyx_PyInt_to_py_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 142; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + if (PyObject_SetAttr(__pyx_v_p, __pyx_n_s__pos, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 142; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":143 + * p.tid = tid + * p.pos = pos + * p.n = n # <<<<<<<<<<<<<< + * pileups = [] + * + */ + __pyx_t_2 = PyInt_FromLong(__pyx_v_n); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 143; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + if (PyObject_SetAttr(__pyx_v_p, __pyx_n_s__n, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 143; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":144 + * p.pos = pos + * p.n = n + * pileups = [] # <<<<<<<<<<<<<< + * + * cdef int x + */ + __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__pyx_v_pileups)); + __pyx_v_pileups = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":147 + * + * cdef int x + * for x from 0 <= x < n: # <<<<<<<<<<<<<< + * pileups.append( makePileupRead( &(pl[x]) ) ) + * p.pileups = pileups + */ + __pyx_t_3 = __pyx_v_n; + for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_3; __pyx_v_x++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":148 + * cdef int x + * for x from 0 <= x < n: + * pileups.append( makePileupRead( &(pl[x]) ) ) # <<<<<<<<<<<<<< + * p.pileups = pileups + * + */ + if (unlikely(__pyx_v_pileups == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 148; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = __pyx_f_9csamtools_makePileupRead((&(__pyx_v_pl[__pyx_v_x]))); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 148; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_4 = PyList_Append(__pyx_v_pileups, __pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 148; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":149 + * for x from 0 <= x < n: + * pileups.append( makePileupRead( &(pl[x]) ) ) + * p.pileups = pileups # <<<<<<<<<<<<<< + * + * (f)(p) + */ + if (PyObject_SetAttr(__pyx_v_p, __pyx_n_s__pileups, ((PyObject *)__pyx_v_pileups)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 149; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":151 + * p.pileups = pileups + * + * (f)(p) # <<<<<<<<<<<<<< + * + * cdef int pileup_fetch_callback( bam1_t *b, void *data): + */ + __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_v_p); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_p); + __Pyx_GIVEREF(__pyx_v_p); + __pyx_t_1 = PyObject_Call(((PyObject *)__pyx_v_f), ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_WriteUnraisable("csamtools.pileup_callback"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_p); + __Pyx_DECREF(__pyx_v_pileups); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":153 + * (f)(p) + * + * cdef int pileup_fetch_callback( bam1_t *b, void *data): # <<<<<<<<<<<<<< + * '''callback for bam_fetch. + * + */ + +static int __pyx_f_9csamtools_pileup_fetch_callback(bam1_t *__pyx_v_b, void *__pyx_v_data) { + bam_plbuf_t *__pyx_v_buf; + int __pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("pileup_fetch_callback"); + __Pyx_TraceCall("pileup_fetch_callback", __pyx_f[0], 153); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":159 + * ''' + * cdef bam_plbuf_t * buf + * buf = data # <<<<<<<<<<<<<< + * bam_plbuf_push(b, buf) + * return 0 + */ + __pyx_v_buf = ((bam_plbuf_t *)__pyx_v_data); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":160 + * cdef bam_plbuf_t * buf + * buf = data + * bam_plbuf_push(b, buf) # <<<<<<<<<<<<<< + * return 0 + * + */ + bam_plbuf_push(__pyx_v_b, __pyx_v_buf); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":161 + * buf = data + * bam_plbuf_push(b, buf) + * return 0 # <<<<<<<<<<<<<< + * + * class StderrStore(): + */ + __pyx_r = 0; + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":167 + * stderr is captured. + * ''' + * def __init__(self): # <<<<<<<<<<<<<< + * return + * self.stderr_h, self.stderr_f = tempfile.mkstemp() + */ + +static PyObject *__pyx_pf_9csamtools_11StderrStore_0__init__(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/ +static PyMethodDef __pyx_mdef_9csamtools_11StderrStore_0__init__ = {__Pyx_NAMESTR("__init__"), (PyCFunction)__pyx_pf_9csamtools_11StderrStore_0__init__, METH_O, __Pyx_DOCSTR(0)}; +static PyObject *__pyx_pf_9csamtools_11StderrStore_0__init__(PyObject *__pyx_self, PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__init__"); + __Pyx_TraceCall("__init__", __pyx_f[0], 167); + __pyx_self = __pyx_self; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":168 + * ''' + * def __init__(self): + * return # <<<<<<<<<<<<<< + * self.stderr_h, self.stderr_f = tempfile.mkstemp() + * self.stderr_save = Outs( sys.stderr.fileno() ) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":169 + * def __init__(self): + * return + * self.stderr_h, self.stderr_f = tempfile.mkstemp() # <<<<<<<<<<<<<< + * self.stderr_save = Outs( sys.stderr.fileno() ) + * self.stderr_save.setfd( self.stderr_h ) + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__tempfile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__mkstemp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (PyTuple_CheckExact(__pyx_t_1) && likely(PyTuple_GET_SIZE(__pyx_t_1) == 2)) { + PyObject* tuple = __pyx_t_1; + __pyx_t_2 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_2); + __pyx_t_3 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (PyObject_SetAttr(__pyx_v_self, __pyx_n_s__stderr_h, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (PyObject_SetAttr(__pyx_v_self, __pyx_n_s__stderr_f, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + } else { + __pyx_t_4 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_2 = __Pyx_UnpackItem(__pyx_t_4, 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = __Pyx_UnpackItem(__pyx_t_4, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (__Pyx_EndUnpack(__pyx_t_4, 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + if (PyObject_SetAttr(__pyx_v_self, __pyx_n_s__stderr_h, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (PyObject_SetAttr(__pyx_v_self, __pyx_n_s__stderr_f, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":170 + * return + * self.stderr_h, self.stderr_f = tempfile.mkstemp() + * self.stderr_save = Outs( sys.stderr.fileno() ) # <<<<<<<<<<<<<< + * self.stderr_save.setfd( self.stderr_h ) + * + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__Outs); 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_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__sys); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 170; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_2 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__stderr); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 170; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__fileno); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 170; __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_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 170; __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 = 170; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 170; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + if (PyObject_SetAttr(__pyx_v_self, __pyx_n_s__stderr_save, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 170; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":171 + * self.stderr_h, self.stderr_f = tempfile.mkstemp() + * self.stderr_save = Outs( sys.stderr.fileno() ) + * self.stderr_save.setfd( self.stderr_h ) # <<<<<<<<<<<<<< + * + * def readAndRelease( self ): + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__stderr_save); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 171; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__setfd); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 171; __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_GetAttr(__pyx_v_self, __pyx_n_s__stderr_h); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 171; __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 = 171; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 171; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + __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_AddTraceback("csamtools.StderrStore.__init__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":173 + * self.stderr_save.setfd( self.stderr_h ) + * + * def readAndRelease( self ): # <<<<<<<<<<<<<< + * return [] + * self.stderr_save.restore() + */ + +static PyObject *__pyx_pf_9csamtools_11StderrStore_1readAndRelease(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/ +static PyMethodDef __pyx_mdef_9csamtools_11StderrStore_1readAndRelease = {__Pyx_NAMESTR("readAndRelease"), (PyCFunction)__pyx_pf_9csamtools_11StderrStore_1readAndRelease, METH_O, __Pyx_DOCSTR(0)}; +static PyObject *__pyx_pf_9csamtools_11StderrStore_1readAndRelease(PyObject *__pyx_self, PyObject *__pyx_v_self) { + PyObject *__pyx_v_lines; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("readAndRelease"); + __Pyx_TraceCall("readAndRelease", __pyx_f[0], 173); + __pyx_self = __pyx_self; + __pyx_v_lines = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":174 + * + * def readAndRelease( self ): + * return [] # <<<<<<<<<<<<<< + * self.stderr_save.restore() + * lines = [] + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_r = ((PyObject *)__pyx_t_1); + __pyx_t_1 = 0; + goto __pyx_L0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":175 + * def readAndRelease( self ): + * return [] + * self.stderr_save.restore() # <<<<<<<<<<<<<< + * lines = [] + * if os.path.exists(self.stderr_f): + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__stderr_save); 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_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__restore); 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_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); 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_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":176 + * return [] + * self.stderr_save.restore() + * lines = [] # <<<<<<<<<<<<<< + * if os.path.exists(self.stderr_f): + * lines = open( self.stderr_f, "r" ).readlines() + */ + __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(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(__pyx_v_lines); + __pyx_v_lines = ((PyObject *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":177 + * self.stderr_save.restore() + * lines = [] + * if os.path.exists(self.stderr_f): # <<<<<<<<<<<<<< + * lines = open( self.stderr_f, "r" ).readlines() + * os.remove( self.stderr_f ) + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); 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_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__path); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__exists); 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_2); __pyx_t_2 = 0; + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__stderr_f); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __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 = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__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 = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":178 + * lines = [] + * if os.path.exists(self.stderr_f): + * lines = open( self.stderr_f, "r" ).readlines() # <<<<<<<<<<<<<< + * os.remove( self.stderr_f ) + * return lines + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__stderr_f); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 178; __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 = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2); + __Pyx_GIVEREF(__pyx_t_2); + __Pyx_INCREF(((PyObject *)__pyx_n_s__r)); + PyTuple_SET_ITEM(__pyx_t_3, 1, ((PyObject *)__pyx_n_s__r)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__r)); + __pyx_t_2 = 0; + __pyx_t_2 = PyObject_Call(__pyx_builtin_open, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__readlines); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 178; __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_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_v_lines); + __pyx_v_lines = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":179 + * if os.path.exists(self.stderr_f): + * lines = open( self.stderr_f, "r" ).readlines() + * os.remove( self.stderr_f ) # <<<<<<<<<<<<<< + * return lines + * + */ + __pyx_t_2 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); 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_t_3 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__remove); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __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_GetAttr(__pyx_v_self, __pyx_n_s__stderr_f); 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_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_1), NULL); 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(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":180 + * lines = open( self.stderr_f, "r" ).readlines() + * os.remove( self.stderr_f ) + * return lines # <<<<<<<<<<<<<< + * + * def release(self): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_lines); + __pyx_r = __pyx_v_lines; + goto __pyx_L0; + + __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("csamtools.StderrStore.readAndRelease"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_lines); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":182 + * return lines + * + * def release(self): # <<<<<<<<<<<<<< + * return + * self.stderr_save.restore() + */ + +static PyObject *__pyx_pf_9csamtools_11StderrStore_2release(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/ +static PyMethodDef __pyx_mdef_9csamtools_11StderrStore_2release = {__Pyx_NAMESTR("release"), (PyCFunction)__pyx_pf_9csamtools_11StderrStore_2release, METH_O, __Pyx_DOCSTR(0)}; +static PyObject *__pyx_pf_9csamtools_11StderrStore_2release(PyObject *__pyx_self, PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("release"); + __Pyx_TraceCall("release", __pyx_f[0], 182); + __pyx_self = __pyx_self; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":183 + * + * def release(self): + * return # <<<<<<<<<<<<<< + * self.stderr_save.restore() + * if os.path.exists(self.stderr_f): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":184 + * def release(self): + * return + * self.stderr_save.restore() # <<<<<<<<<<<<<< + * if os.path.exists(self.stderr_f): + * os.remove( self.stderr_f ) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__stderr_save); 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 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__restore); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); 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_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":185 + * return + * self.stderr_save.restore() + * if os.path.exists(self.stderr_f): # <<<<<<<<<<<<<< + * os.remove( self.stderr_f ) + * + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); 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_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__path); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__exists); 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_2); __pyx_t_2 = 0; + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__stderr_f); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __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 = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__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 = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":186 + * self.stderr_save.restore() + * if os.path.exists(self.stderr_f): + * os.remove( self.stderr_f ) # <<<<<<<<<<<<<< + * + * def __del__(self): + */ + __pyx_t_2 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); 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 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__remove); 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 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__stderr_f); 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_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_1), NULL); 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_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + goto __pyx_L5; + } + __pyx_L5:; + + __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("csamtools.StderrStore.release"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":188 + * os.remove( self.stderr_f ) + * + * def __del__(self): # <<<<<<<<<<<<<< + * self.release() + * + */ + +static PyObject *__pyx_pf_9csamtools_11StderrStore_3__del__(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/ +static PyMethodDef __pyx_mdef_9csamtools_11StderrStore_3__del__ = {__Pyx_NAMESTR("__del__"), (PyCFunction)__pyx_pf_9csamtools_11StderrStore_3__del__, METH_O, __Pyx_DOCSTR(0)}; +static PyObject *__pyx_pf_9csamtools_11StderrStore_3__del__(PyObject *__pyx_self, PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__del__"); + __Pyx_TraceCall("__del__", __pyx_f[0], 188); + __pyx_self = __pyx_self; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":189 + * + * def __del__(self): + * self.release() # <<<<<<<<<<<<<< + * + * ###################################################################### + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__release); 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_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); 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_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + __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("csamtools.StderrStore.__del__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":238 + * cdef faidx_t * fastafile + * + * def __cinit__(self, *args, **kwargs ): # <<<<<<<<<<<<<< + * self.fastafile = NULL + * self._filename = NULL + */ + +static int __pyx_pf_9csamtools_9Fastafile_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_9Fastafile_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_args = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 238); + 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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":239 + * + * def __cinit__(self, *args, **kwargs ): + * self.fastafile = NULL # <<<<<<<<<<<<<< + * self._filename = NULL + * self._open( *args, **kwargs ) + */ + ((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":240 + * def __cinit__(self, *args, **kwargs ): + * self.fastafile = NULL + * self._filename = NULL # <<<<<<<<<<<<<< + * self._open( *args, **kwargs ) + * + */ + ((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->_filename = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":241 + * self.fastafile = NULL + * self._filename = NULL + * self._open( *args, **kwargs ) # <<<<<<<<<<<<<< + * + * def _isOpen( self ): + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___open); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PySequence_Tuple(((PyObject *)__pyx_v_args)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_t_3 = PyEval_CallObjectWithKeywords(__pyx_t_1, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_v_kwargs)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + __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_AddTraceback("csamtools.Fastafile.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_args); + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":243 + * self._open( *args, **kwargs ) + * + * def _isOpen( self ): # <<<<<<<<<<<<<< + * '''return true if samfile has been opened.''' + * return self.fastafile != NULL + */ + +static PyObject *__pyx_pf_9csamtools_9Fastafile_1_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static char __pyx_doc_9csamtools_9Fastafile_1_isOpen[] = "return true if samfile has been opened."; +static PyObject *__pyx_pf_9csamtools_9Fastafile_1_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_isOpen"); + __Pyx_TraceCall("_isOpen", __pyx_f[0], 243); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":245 + * def _isOpen( self ): + * '''return true if samfile has been opened.''' + * return self.fastafile != NULL # <<<<<<<<<<<<<< + * + * def __len__(self): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyBool_FromLong((((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.Fastafile._isOpen"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":247 + * return self.fastafile != NULL + * + * def __len__(self): # <<<<<<<<<<<<<< + * if self.fastafile == NULL: + * raise ValueError( "calling len() on closed file" ) + */ + +static Py_ssize_t __pyx_pf_9csamtools_9Fastafile_2__len__(PyObject *__pyx_v_self); /*proto*/ +static Py_ssize_t __pyx_pf_9csamtools_9Fastafile_2__len__(PyObject *__pyx_v_self) { + Py_ssize_t __pyx_r; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__len__"); + __Pyx_TraceCall("__len__", __pyx_f[0], 247); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":248 + * + * def __len__(self): + * if self.fastafile == NULL: # <<<<<<<<<<<<<< + * raise ValueError( "calling len() on closed file" ) + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile == NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":249 + * def __len__(self): + * if self.fastafile == NULL: + * raise ValueError( "calling len() on closed file" ) # <<<<<<<<<<<<<< + * + * return faidx_fetch_nseq(self.fastafile) + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_4), 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_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":251 + * raise ValueError( "calling len() on closed file" ) + * + * return faidx_fetch_nseq(self.fastafile) # <<<<<<<<<<<<<< + * + * def _open( self, + */ + __pyx_r = faidx_fetch_nseq(((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile); + goto __pyx_L0; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.Fastafile.__len__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":253 + * return faidx_fetch_nseq(self.fastafile) + * + * def _open( self, # <<<<<<<<<<<<<< + * char * filename ): + * '''open an indexed fasta file. + */ + +static PyObject *__pyx_pf_9csamtools_9Fastafile_3_open(PyObject *__pyx_v_self, PyObject *__pyx_arg_filename); /*proto*/ +static char __pyx_doc_9csamtools_9Fastafile_3_open[] = "open an indexed fasta file.\n\n This method expects an indexed fasta file.\n "; +static PyObject *__pyx_pf_9csamtools_9Fastafile_3_open(PyObject *__pyx_v_self, PyObject *__pyx_arg_filename) { + char *__pyx_v_filename; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_open"); + __Pyx_TraceCall("_open", __pyx_f[0], 253); + assert(__pyx_arg_filename); { + __pyx_v_filename = PyBytes_AsString(__pyx_arg_filename); if (unlikely((!__pyx_v_filename) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 254; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Fastafile._open"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":261 + * + * # close a previously opened file + * if self.fastafile != NULL: self.close() # <<<<<<<<<<<<<< + * if self._filename != NULL: free(self._filename) + * self._filename = strdup(filename) + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile != NULL); + if (__pyx_t_1) { + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__close); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 261; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 261; __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_3); __pyx_t_3 = 0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":262 + * # close a previously opened file + * if self.fastafile != NULL: self.close() + * if self._filename != NULL: free(self._filename) # <<<<<<<<<<<<<< + * self._filename = strdup(filename) + * self.fastafile = fai_load( filename ) + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->_filename != NULL); + if (__pyx_t_1) { + free(((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->_filename); + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":263 + * if self.fastafile != NULL: self.close() + * if self._filename != NULL: free(self._filename) + * self._filename = strdup(filename) # <<<<<<<<<<<<<< + * self.fastafile = fai_load( filename ) + * + */ + ((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->_filename = strdup(__pyx_v_filename); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":264 + * if self._filename != NULL: free(self._filename) + * self._filename = strdup(filename) + * self.fastafile = fai_load( filename ) # <<<<<<<<<<<<<< + * + * if self.fastafile == NULL: + */ + ((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile = fai_load(__pyx_v_filename); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":266 + * self.fastafile = fai_load( filename ) + * + * if self.fastafile == NULL: # <<<<<<<<<<<<<< + * raise IOError("could not open file `%s`" % filename ) + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile == NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":267 + * + * if self.fastafile == NULL: + * raise IOError("could not open file `%s`" % filename ) # <<<<<<<<<<<<<< + * + * def close( self ): + */ + __pyx_t_3 = PyBytes_FromString(__pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + __pyx_t_2 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_5), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__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 = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_t_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_2)); + __pyx_t_2 = 0; + __pyx_t_2 = PyObject_Call(__pyx_builtin_IOError, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + + __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_AddTraceback("csamtools.Fastafile._open"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":269 + * raise IOError("could not open file `%s`" % filename ) + * + * def close( self ): # <<<<<<<<<<<<<< + * if self.fastafile != NULL: + * fai_destroy( self.fastafile ) + */ + +static PyObject *__pyx_pf_9csamtools_9Fastafile_4close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9Fastafile_4close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("close"); + __Pyx_TraceCall("close", __pyx_f[0], 269); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":270 + * + * def close( self ): + * if self.fastafile != NULL: # <<<<<<<<<<<<<< + * fai_destroy( self.fastafile ) + * self.fastafile = NULL + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile != NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":271 + * def close( self ): + * if self.fastafile != NULL: + * fai_destroy( self.fastafile ) # <<<<<<<<<<<<<< + * self.fastafile = NULL + * + */ + fai_destroy(((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":272 + * if self.fastafile != NULL: + * fai_destroy( self.fastafile ) + * self.fastafile = NULL # <<<<<<<<<<<<<< + * + * def __dealloc__(self): + */ + ((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile = NULL; + goto __pyx_L5; + } + __pyx_L5:; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":274 + * self.fastafile = NULL + * + * def __dealloc__(self): # <<<<<<<<<<<<<< + * self.close() + * if self._filename != NULL: free(self._filename) + */ + +static void __pyx_pf_9csamtools_9Fastafile_5__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_9Fastafile_5__dealloc__(PyObject *__pyx_v_self) { + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 274); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":275 + * + * def __dealloc__(self): + * self.close() # <<<<<<<<<<<<<< + * if self._filename != NULL: free(self._filename) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__close); 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_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":276 + * def __dealloc__(self): + * self.close() + * if self._filename != NULL: free(self._filename) # <<<<<<<<<<<<<< + * + * property filename: + */ + __pyx_t_3 = (((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->_filename != NULL); + if (__pyx_t_3) { + free(((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->_filename); + goto __pyx_L5; + } + __pyx_L5:; + + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.Fastafile.__dealloc__"); + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":280 + * property filename: + * '''number of :term:`filename` associated with this object.''' + * def __get__(self): # <<<<<<<<<<<<<< + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return self._filename + */ + +static PyObject *__pyx_pf_9csamtools_9Fastafile_8filename_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9Fastafile_8filename_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 280); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":281 + * '''number of :term:`filename` associated with this object.''' + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * return self._filename + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 281; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 281; __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 = 281; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_7), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 281; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 281; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":282 + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return self._filename # <<<<<<<<<<<<<< + * + * def fetch( self, + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyBytes_FromString(((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->_filename); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_r = ((PyObject *)__pyx_t_2); + __pyx_t_2 = 0; + goto __pyx_L0; + + __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("csamtools.Fastafile.filename.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":284 + * return self._filename + * + * def fetch( self, # <<<<<<<<<<<<<< + * reference = None, + * start = None, + */ + +static PyObject *__pyx_pf_9csamtools_9Fastafile_6fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_9Fastafile_6fetch[] = "*(reference = None, start = None, end = None, region = None)*\n \n fetch :meth:`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 *reference* is given and *start* is None, the sequence from the \n first base is returned. Similarly, if *end* is None, the sequence \n until the last base is returned.\n \n Alternatively, a samtools :term:`region` string can be supplied.\n "; +static PyObject *__pyx_pf_9csamtools_9Fastafile_6fetch(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; + int __pyx_v_length; + char *__pyx_v_seq; + PyObject *__pyx_v_py_seq; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + char *__pyx_t_5; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__reference,&__pyx_n_s__start,&__pyx_n_s__end,&__pyx_n_s__region,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("fetch"); + __Pyx_TraceCall("fetch", __pyx_f[0], 284); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[4] = {0,0,0,0}; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":285 + * + * def fetch( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + values[0] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":286 + * def fetch( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None): + */ + values[1] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":287 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None): + * + */ + values[2] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":288 + * start = None, + * end = None, + * region = None): # <<<<<<<<<<<<<< + * + * '''*(reference = None, start = None, end = None, region = None)* + */ + values[3] = ((PyObject *)Py_None); + 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; + } + switch (PyTuple_GET_SIZE(__pyx_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, 0, values, PyTuple_GET_SIZE(__pyx_args), "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 284; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_reference = values[0]; + __pyx_v_start = values[1]; + __pyx_v_end = values[2]; + __pyx_v_region = values[3]; + } else { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":285 + * + * def fetch( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + __pyx_v_reference = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":286 + * def fetch( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None): + */ + __pyx_v_start = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":287 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None): + * + */ + __pyx_v_end = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":288 + * start = None, + * end = None, + * region = None): # <<<<<<<<<<<<<< + * + * '''*(reference = None, start = None, end = None, region = None)* + */ + __pyx_v_region = ((PyObject *)Py_None); + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 4: __pyx_v_region = PyTuple_GET_ITEM(__pyx_args, 3); + case 3: __pyx_v_end = PyTuple_GET_ITEM(__pyx_args, 2); + case 2: __pyx_v_start = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_reference = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("fetch", 0, 0, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 284; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Fastafile.fetch"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __Pyx_INCREF(__pyx_v_start); + __Pyx_INCREF(__pyx_v_end); + __Pyx_INCREF(__pyx_v_region); + __pyx_v_py_seq = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":305 + * ''' + * + * if not self._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 305; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 305; __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 = 305; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":306 + * + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * cdef int length + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_8), NULL); 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_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 306; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":311 + * cdef char * seq + * + * if not region: # <<<<<<<<<<<<<< + * if reference is None: raise ValueError( 'no sequence/region supplied.' ) + * if start is None: start = 0 + */ + __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_region); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 311; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = (!__pyx_t_4); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":312 + * + * if not region: + * if reference is None: raise ValueError( 'no sequence/region supplied.' ) # <<<<<<<<<<<<<< + * if start is None: start = 0 + * if end is None: end = max_pos -1 + */ + __pyx_t_3 = (__pyx_v_reference == Py_None); + if (__pyx_t_3) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_10), 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_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 312; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":313 + * if not region: + * if reference is None: raise ValueError( 'no sequence/region supplied.' ) + * if start is None: start = 0 # <<<<<<<<<<<<<< + * if end is None: end = max_pos -1 + * + */ + __pyx_t_3 = (__pyx_v_start == Py_None); + if (__pyx_t_3) { + __Pyx_INCREF(__pyx_int_0); + __Pyx_DECREF(__pyx_v_start); + __pyx_v_start = __pyx_int_0; + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":314 + * if reference is None: raise ValueError( 'no sequence/region supplied.' ) + * if start is None: start = 0 + * if end is None: end = max_pos -1 # <<<<<<<<<<<<<< + * + * if start > end: raise ValueError( 'invalid region: start (%i) > end (%i)' % (start, end) ) + */ + __pyx_t_3 = (__pyx_v_end == Py_None); + if (__pyx_t_3) { + __pyx_t_2 = PyInt_FromLong((__pyx_v_9csamtools_max_pos - 1)); 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_DECREF(__pyx_v_end); + __pyx_v_end = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L10; + } + __pyx_L10:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":316 + * if end is None: end = max_pos -1 + * + * if start > end: raise ValueError( 'invalid region: start (%i) > end (%i)' % (start, end) ) # <<<<<<<<<<<<<< + * if start == end: return "" + * # valid ranges are from 0 to 2^29-1 + */ + __pyx_t_2 = PyObject_RichCompare(__pyx_v_start, __pyx_v_end, Py_GT); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __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_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (__pyx_t_3) { + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_v_start); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_start); + __Pyx_GIVEREF(__pyx_v_start); + __Pyx_INCREF(__pyx_v_end); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_end); + __Pyx_GIVEREF(__pyx_v_end); + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_11), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__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 = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_2), 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(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_Raise(__pyx_t_1, 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;} + goto __pyx_L11; + } + __pyx_L11:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":317 + * + * if start > end: raise ValueError( 'invalid region: start (%i) > end (%i)' % (start, end) ) + * if start == end: return "" # <<<<<<<<<<<<<< + * # valid ranges are from 0 to 2^29-1 + * if not 0 <= start < max_pos: raise ValueError( 'start out of range (%i)' % start ) + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, __pyx_v_end, Py_EQ); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 317; __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 = 317; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_3) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_12)); + __pyx_r = ((PyObject *)__pyx_kp_s_12); + goto __pyx_L0; + goto __pyx_L12; + } + __pyx_L12:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":319 + * if start == end: return "" + * # valid ranges are from 0 to 2^29-1 + * if not 0 <= start < max_pos: raise ValueError( 'start out of range (%i)' % start ) # <<<<<<<<<<<<<< + * if not 0 <= end < max_pos: raise ValueError( '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_start, Py_LE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + if (__Pyx_PyObject_IsTrue(__pyx_t_1)) { + __Pyx_DECREF(__pyx_t_1); + __pyx_t_2 = PyInt_FromLong(__pyx_v_9csamtools_max_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __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); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __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 = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_13), __pyx_v_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L13; + } + __pyx_L13:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":320 + * # valid ranges are from 0 to 2^29-1 + * if not 0 <= start < max_pos: raise ValueError( 'start out of range (%i)' % start ) + * if not 0 <= end < max_pos: raise ValueError( '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 = PyObject_RichCompare(__pyx_int_0, __pyx_v_end, Py_LE); 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); + if (__Pyx_PyObject_IsTrue(__pyx_t_1)) { + __Pyx_DECREF(__pyx_t_1); + __pyx_t_2 = PyInt_FromLong(__pyx_v_9csamtools_max_pos); 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_t_1 = PyObject_RichCompare(__pyx_v_end, __pyx_t_2, Py_LT); 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_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 = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_3 = (!__pyx_t_4); + if (__pyx_t_3) { + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_14), __pyx_v_end); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_2), 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(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_Raise(__pyx_t_1, 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;} + goto __pyx_L14; + } + __pyx_L14:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":329 + * # end-1, + * # &length) + * region = "%s:%i-%i" % (reference, start+1, end) # <<<<<<<<<<<<<< + * seq = fai_fetch( self.fastafile, + * region, + */ + __pyx_t_1 = PyNumber_Add(__pyx_v_start, __pyx_int_1); 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_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_v_reference); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_reference); + __Pyx_GIVEREF(__pyx_v_reference); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + __Pyx_INCREF(__pyx_v_end); + PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_end); + __Pyx_GIVEREF(__pyx_v_end); + __pyx_t_1 = 0; + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_15), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_v_region); + __pyx_v_region = ((PyObject *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":331 + * region = "%s:%i-%i" % (reference, start+1, end) + * seq = fai_fetch( self.fastafile, + * region, # <<<<<<<<<<<<<< + * &length ) + * else: + */ + __pyx_t_5 = PyBytes_AsString(__pyx_v_region); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":332 + * seq = fai_fetch( self.fastafile, + * region, + * &length ) # <<<<<<<<<<<<<< + * else: + * # samtools adds a '\0' at the end + */ + __pyx_v_seq = fai_fetch(((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile, __pyx_t_5, (&__pyx_v_length)); + goto __pyx_L7; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":335 + * else: + * # samtools adds a '\0' at the end + * seq = fai_fetch( self.fastafile, region, &length ) # <<<<<<<<<<<<<< + * + * # copy to python + */ + __pyx_t_5 = PyBytes_AsString(__pyx_v_region); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 335; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_seq = fai_fetch(((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_self)->fastafile, __pyx_t_5, (&__pyx_v_length)); + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":338 + * + * # copy to python + * if seq == NULL: # <<<<<<<<<<<<<< + * return "" + * else: + */ + __pyx_t_3 = (__pyx_v_seq == NULL); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":339 + * # copy to python + * if seq == NULL: + * return "" # <<<<<<<<<<<<<< + * else: + * try: + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_12)); + __pyx_r = ((PyObject *)__pyx_kp_s_12); + goto __pyx_L0; + goto __pyx_L15; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":341 + * return "" + * else: + * try: # <<<<<<<<<<<<<< + * py_seq = PyString_FromStringAndSize(seq, length) + * finally: + */ + /*try:*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":342 + * else: + * try: + * py_seq = PyString_FromStringAndSize(seq, length) # <<<<<<<<<<<<<< + * finally: + * free(seq) + */ + __pyx_t_1 = PyString_FromStringAndSize(__pyx_v_seq, __pyx_v_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 342; __pyx_clineno = __LINE__; goto __pyx_L17;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_v_py_seq); + __pyx_v_py_seq = __pyx_t_1; + __pyx_t_1 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":344 + * py_seq = PyString_FromStringAndSize(seq, length) + * finally: + * free(seq) # <<<<<<<<<<<<<< + * + * return py_seq + */ + /*finally:*/ { + int __pyx_why; + PyObject *__pyx_exc_type, *__pyx_exc_value, *__pyx_exc_tb; + int __pyx_exc_lineno; + __pyx_exc_type = 0; __pyx_exc_value = 0; __pyx_exc_tb = 0; __pyx_exc_lineno = 0; + __pyx_why = 0; goto __pyx_L18; + __pyx_L17: { + __pyx_why = 4; + __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_ErrFetch(&__pyx_exc_type, &__pyx_exc_value, &__pyx_exc_tb); + __pyx_exc_lineno = __pyx_lineno; + goto __pyx_L18; + } + __pyx_L18:; + free(__pyx_v_seq); + switch (__pyx_why) { + case 4: { + __Pyx_ErrRestore(__pyx_exc_type, __pyx_exc_value, __pyx_exc_tb); + __pyx_lineno = __pyx_exc_lineno; + __pyx_exc_type = 0; + __pyx_exc_value = 0; + __pyx_exc_tb = 0; + goto __pyx_L1_error; + } + } + } + } + __pyx_L15:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":346 + * free(seq) + * + * return py_seq # <<<<<<<<<<<<<< + * + * cdef char * _fetch( self, char * reference, int start, int end, int * length ): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_py_seq); + __pyx_r = __pyx_v_py_seq; + goto __pyx_L0; + + __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("csamtools.Fastafile.fetch"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_py_seq); + __Pyx_DECREF(__pyx_v_start); + __Pyx_DECREF(__pyx_v_end); + __Pyx_DECREF(__pyx_v_region); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":348 + * return py_seq + * + * cdef char * _fetch( self, char * reference, int start, int end, int * length ): # <<<<<<<<<<<<<< + * '''fetch sequence for reference, start and end''' + * + */ + +static char *__pyx_f_9csamtools_9Fastafile__fetch(struct __pyx_obj_9csamtools_Fastafile *__pyx_v_self, char *__pyx_v_reference, int __pyx_v_start, int __pyx_v_end, int *__pyx_v_length) { + char *__pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_fetch"); + __Pyx_TraceCall("_fetch", __pyx_f[0], 348); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":355 + * start, + * end-1, + * length ) # <<<<<<<<<<<<<< + * + * #------------------------------------------------------------------------ + */ + __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; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":360 + * #------------------------------------------------------------------------ + * #------------------------------------------------------------------------ + * cdef int count_callback( bam1_t *alignment, void *f): # <<<<<<<<<<<<<< + * '''callback for bam_fetch - count number of reads. + * ''' + */ + +static int __pyx_f_9csamtools_count_callback(bam1_t *__pyx_v_alignment, void *__pyx_v_f) { + int *__pyx_v_counter; + int __pyx_r; + long __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("count_callback"); + __Pyx_TraceCall("count_callback", __pyx_f[0], 360); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":363 + * '''callback for bam_fetch - count number of reads. + * ''' + * cdef int* counter = (f) # <<<<<<<<<<<<<< + * counter[0] += 1; + * + */ + __pyx_v_counter = ((int *)__pyx_v_f); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":364 + * ''' + * cdef int* counter = (f) + * counter[0] += 1; # <<<<<<<<<<<<<< + * + * ctypedef struct MateData: + */ + __pyx_t_1 = 0; + (__pyx_v_counter[__pyx_t_1]) = ((__pyx_v_counter[__pyx_t_1]) + 1); + + __pyx_r = 0; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":374 + * #------------------------------------------------------------------------ + * #------------------------------------------------------------------------ + * cdef int mate_callback( bam1_t *alignment, void *f): # <<<<<<<<<<<<<< + * '''callback for bam_fetch = filter mate + * ''' + */ + +static int __pyx_f_9csamtools_mate_callback(bam1_t *__pyx_v_alignment, void *__pyx_v_f) { + __pyx_t_9csamtools_MateData *__pyx_v_d; + int __pyx_r; + int __pyx_t_1; + int __pyx_t_2; + int __pyx_t_3; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("mate_callback"); + __Pyx_TraceCall("mate_callback", __pyx_f[0], 374); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":377 + * '''callback for bam_fetch = filter mate + * ''' + * cdef MateData * d = (f) # <<<<<<<<<<<<<< + * # printf("mate = %p, name1 = %s, name2=%s\t%i\t%i\t%i\n", + * # d.mate, d.name, bam1_qname(alignment), + */ + __pyx_v_d = ((__pyx_t_9csamtools_MateData *)__pyx_v_f); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":382 + * # d.flag, alignment.core.flag, alignment.core.flag & d.flag) + * + * if d.mate == NULL: # <<<<<<<<<<<<<< + * # could be sped up by comparing the lengths of query strings first + * # using l_qname + */ + __pyx_t_1 = (__pyx_v_d->mate == NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":388 + * # also, make sure that we get the other read by comparing + * # the flags + * if alignment.core.flag & d.flag != 0 and \ # <<<<<<<<<<<<<< + * strcmp( bam1_qname( alignment ), d.name ) == 0: + * d.mate = bam_dup1( alignment ) + */ + __pyx_t_1 = ((__pyx_v_alignment->core.flag & __pyx_v_d->flag) != 0); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":389 + * # the flags + * if alignment.core.flag & d.flag != 0 and \ + * strcmp( bam1_qname( alignment ), d.name ) == 0: # <<<<<<<<<<<<<< + * d.mate = bam_dup1( alignment ) + * + */ + __pyx_t_2 = (strcmp(bam1_qname(__pyx_v_alignment), __pyx_v_d->name) == 0); + __pyx_t_3 = __pyx_t_2; + } else { + __pyx_t_3 = __pyx_t_1; + } + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":390 + * if alignment.core.flag & d.flag != 0 and \ + * strcmp( bam1_qname( alignment ), d.name ) == 0: + * d.mate = bam_dup1( alignment ) # <<<<<<<<<<<<<< + * + * + */ + __pyx_v_d->mate = bam_dup1(__pyx_v_alignment); + goto __pyx_L4; + } + __pyx_L4:; + goto __pyx_L3; + } + __pyx_L3:; + + __pyx_r = 0; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":442 + * cdef char * mode + * + * def __cinit__(self, *args, **kwargs ): # <<<<<<<<<<<<<< + * self.samfile = NULL + * self._filename = NULL + */ + +static int __pyx_pf_9csamtools_7Samfile_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_7Samfile_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_args = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 442); + 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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":443 + * + * def __cinit__(self, *args, **kwargs ): + * self.samfile = NULL # <<<<<<<<<<<<<< + * self._filename = NULL + * self.isbam = False + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":444 + * def __cinit__(self, *args, **kwargs ): + * self.samfile = NULL + * self._filename = NULL # <<<<<<<<<<<<<< + * self.isbam = False + * self._open( *args, **kwargs ) + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->_filename = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":445 + * self.samfile = NULL + * self._filename = NULL + * self.isbam = False # <<<<<<<<<<<<<< + * self._open( *args, **kwargs ) + * + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isbam = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":446 + * self._filename = NULL + * self.isbam = False + * self._open( *args, **kwargs ) # <<<<<<<<<<<<<< + * + * # allocate memory for iterator + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___open); 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 = PySequence_Tuple(((PyObject *)__pyx_v_args)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_t_3 = PyEval_CallObjectWithKeywords(__pyx_t_1, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_v_kwargs)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":449 + * + * # allocate memory for iterator + * self.b = calloc(1, sizeof(bam1_t)) # <<<<<<<<<<<<<< + * + * def _isOpen( self ): + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->b = ((bam1_t *)calloc(1, (sizeof(bam1_t)))); + + __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_AddTraceback("csamtools.Samfile.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_args); + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":451 + * self.b = calloc(1, sizeof(bam1_t)) + * + * def _isOpen( self ): # <<<<<<<<<<<<<< + * '''return true if samfile has been opened.''' + * return self.samfile != NULL + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_1_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_1_isOpen[] = "return true if samfile has been opened."; +static PyObject *__pyx_pf_9csamtools_7Samfile_1_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_isOpen"); + __Pyx_TraceCall("_isOpen", __pyx_f[0], 451); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":453 + * def _isOpen( self ): + * '''return true if samfile has been opened.''' + * return self.samfile != NULL # <<<<<<<<<<<<<< + * + * def _hasIndex( self ): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyBool_FromLong((((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 453; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.Samfile._isOpen"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":455 + * return self.samfile != NULL + * + * def _hasIndex( self ): # <<<<<<<<<<<<<< + * '''return true if samfile has an existing (and opened) index.''' + * return self.index != NULL + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_2_hasIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_2_hasIndex[] = "return true if samfile has an existing (and opened) index."; +static PyObject *__pyx_pf_9csamtools_7Samfile_2_hasIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_hasIndex"); + __Pyx_TraceCall("_hasIndex", __pyx_f[0], 455); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":457 + * def _hasIndex( self ): + * '''return true if samfile has an existing (and opened) index.''' + * return self.index != NULL # <<<<<<<<<<<<<< + * + * def _open( self, + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyBool_FromLong((((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.Samfile._hasIndex"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":459 + * return self.index != NULL + * + * def _open( self, # <<<<<<<<<<<<<< + * char * filename, + * mode = 'r', + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_3_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_3_open[] = "open 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_pf_9csamtools_7Samfile_3_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + char *__pyx_v_filename; + PyObject *__pyx_v_mode = 0; + struct __pyx_obj_9csamtools_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; + bam_header_t *__pyx_v_header_to_write; + char *__pyx_v_ctext; + PyObject *__pyx_v_n; + PyObject *__pyx_v_x; + PyObject *__pyx_v_name; + PyObject *__pyx_v_store; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + int __pyx_t_5; + Py_ssize_t __pyx_t_6; + PyObject *__pyx_t_7 = NULL; + int __pyx_t_8; + Py_ssize_t __pyx_t_9; + size_t __pyx_t_10; + int32_t __pyx_t_11; + long __pyx_t_12; + uint32_t __pyx_t_13; + char *__pyx_t_14; + int __pyx_t_15; + 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,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_open"); + __Pyx_TraceCall("_open", __pyx_f[0], 459); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[8] = {0,0,0,0,0,0,0,0}; + values[1] = ((PyObject *)__pyx_n_s__r); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":462 + * char * filename, + * mode = 'r', + * Samfile template = None, # <<<<<<<<<<<<<< + * referencenames = None, + * referencelengths = None, + */ + values[2] = (PyObject *)((struct __pyx_obj_9csamtools_Samfile *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":463 + * mode = 'r', + * Samfile template = None, + * referencenames = None, # <<<<<<<<<<<<<< + * referencelengths = None, + * text = None, + */ + values[3] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":464 + * Samfile template = None, + * referencenames = None, + * referencelengths = None, # <<<<<<<<<<<<<< + * text = None, + * header = None, + */ + values[4] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":465 + * referencenames = None, + * referencelengths = None, + * text = None, # <<<<<<<<<<<<<< + * header = None, + * port = None, + */ + values[5] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":466 + * referencelengths = None, + * text = None, + * header = None, # <<<<<<<<<<<<<< + * port = None, + * ): + */ + values[6] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":467 + * text = None, + * header = None, + * port = None, # <<<<<<<<<<<<<< + * ): + * '''open a sam/bam file. + */ + values[7] = ((PyObject *)Py_None); + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__filename); + if (likely(values[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--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "_open") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_filename = PyBytes_AsString(values[0]); if (unlikely((!__pyx_v_filename) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 460; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_mode = values[1]; + __pyx_v_template = ((struct __pyx_obj_9csamtools_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]; + } else { + __pyx_v_mode = ((PyObject *)__pyx_n_s__r); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":462 + * char * filename, + * mode = 'r', + * Samfile template = None, # <<<<<<<<<<<<<< + * referencenames = None, + * referencelengths = None, + */ + __pyx_v_template = ((struct __pyx_obj_9csamtools_Samfile *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":463 + * mode = 'r', + * Samfile template = None, + * referencenames = None, # <<<<<<<<<<<<<< + * referencelengths = None, + * text = None, + */ + __pyx_v_referencenames = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":464 + * Samfile template = None, + * referencenames = None, + * referencelengths = None, # <<<<<<<<<<<<<< + * text = None, + * header = None, + */ + __pyx_v_referencelengths = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":465 + * referencenames = None, + * referencelengths = None, + * text = None, # <<<<<<<<<<<<<< + * header = None, + * port = None, + */ + __pyx_v_text = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":466 + * referencelengths = None, + * text = None, + * header = None, # <<<<<<<<<<<<<< + * port = None, + * ): + */ + __pyx_v_header = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":467 + * text = None, + * header = None, + * port = None, # <<<<<<<<<<<<<< + * ): + * '''open a sam/bam file. + */ + __pyx_v_port = ((PyObject *)Py_None); + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 8: __pyx_v_port = PyTuple_GET_ITEM(__pyx_args, 7); + case 7: __pyx_v_header = PyTuple_GET_ITEM(__pyx_args, 6); + case 6: __pyx_v_text = PyTuple_GET_ITEM(__pyx_args, 5); + case 5: __pyx_v_referencelengths = PyTuple_GET_ITEM(__pyx_args, 4); + case 4: __pyx_v_referencenames = PyTuple_GET_ITEM(__pyx_args, 3); + case 3: __pyx_v_template = ((struct __pyx_obj_9csamtools_Samfile *)PyTuple_GET_ITEM(__pyx_args, 2)); + case 2: __pyx_v_mode = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_filename = PyBytes_AsString(PyTuple_GET_ITEM(__pyx_args, 0)); if (unlikely((!__pyx_v_filename) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 460; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + break; + default: goto __pyx_L5_argtuple_error; + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("_open", 0, 1, 8, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Samfile._open"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_v_n = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_x = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_name = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_store = Py_None; __Pyx_INCREF(Py_None); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_template), __pyx_ptype_9csamtools_Samfile, 1, "template", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":475 + * ''' + * + * assert mode in ( "r","w","rb","wb", "wh", "wbu" ), "invalid file opening mode `%s`" % mode # <<<<<<<<<<<<<< + * assert filename != NULL + * + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + __Pyx_INCREF(__pyx_v_mode); + __pyx_t_1 = __pyx_v_mode; + __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)__pyx_n_s__r), Py_EQ); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = __pyx_t_3; + if (!__pyx_t_4) { + __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)__pyx_n_s__w), Py_EQ); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_5 = __pyx_t_3; + __pyx_t_3 = __pyx_t_5; + } else { + __pyx_t_3 = __pyx_t_4; + } + if (!__pyx_t_3) { + __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)__pyx_n_s__rb), Py_EQ); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely((__pyx_t_4 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_5 = __pyx_t_4; + __pyx_t_4 = __pyx_t_5; + } else { + __pyx_t_4 = __pyx_t_3; + } + if (!__pyx_t_4) { + __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)__pyx_n_s__wb), Py_EQ); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_5 = __pyx_t_3; + __pyx_t_3 = __pyx_t_5; + } else { + __pyx_t_3 = __pyx_t_4; + } + if (!__pyx_t_3) { + __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)__pyx_n_s__wh), Py_EQ); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely((__pyx_t_4 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_5 = __pyx_t_4; + __pyx_t_4 = __pyx_t_5; + } else { + __pyx_t_4 = __pyx_t_3; + } + if (!__pyx_t_4) { + __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)__pyx_n_s__wbu), Py_EQ); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_5 = __pyx_t_3; + __pyx_t_3 = __pyx_t_5; + } else { + __pyx_t_3 = __pyx_t_4; + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (unlikely(!__pyx_t_3)) { + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_16), __pyx_v_mode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyErr_SetObject(PyExc_AssertionError, ((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":476 + * + * assert mode in ( "r","w","rb","wb", "wh", "wbu" ), "invalid file opening mode `%s`" % mode + * assert filename != NULL # <<<<<<<<<<<<<< + * + * # close a previously opened file + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + if (unlikely(!(__pyx_v_filename != NULL))) { + PyErr_SetNone(PyExc_AssertionError); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":479 + * + * # close a previously opened file + * if self.samfile != NULL: self.close() # <<<<<<<<<<<<<< + * self.samfile = NULL + * + */ + __pyx_t_3 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile != NULL); + if (__pyx_t_3) { + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__close); 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_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __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; + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":480 + * # close a previously opened file + * if self.samfile != NULL: self.close() + * self.samfile = NULL # <<<<<<<<<<<<<< + * + * cdef bam_header_t * header_to_write + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":483 + * + * cdef bam_header_t * header_to_write + * header_to_write = NULL # <<<<<<<<<<<<<< + * + * if self._filename != NULL: free(self._filename ) + */ + __pyx_v_header_to_write = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":485 + * header_to_write = NULL + * + * if self._filename != NULL: free(self._filename ) # <<<<<<<<<<<<<< + * self._filename = strdup( filename ) + * + */ + __pyx_t_3 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->_filename != NULL); + if (__pyx_t_3) { + free(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->_filename); + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":486 + * + * if self._filename != NULL: free(self._filename ) + * self._filename = strdup( filename ) # <<<<<<<<<<<<<< + * + * self.isbam = len(mode) > 1 and mode[1] == 'b' + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->_filename = strdup(__pyx_v_filename); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":488 + * self._filename = strdup( filename ) + * + * self.isbam = len(mode) > 1 and mode[1] == 'b' # <<<<<<<<<<<<<< + * + * self.isremote = strncmp(filename,"http:",5) == 0 or \ + */ + __pyx_t_6 = PyObject_Length(__pyx_v_mode); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = __Pyx_PyBool_FromLong((__pyx_t_6 > 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); + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_3) { + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_mode, 1, sizeof(long), PyInt_FromLong); if (!__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_7 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)__pyx_n_s__b), Py_EQ); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __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_t_7; + __pyx_t_7 = 0; + } else { + __pyx_t_1 = __pyx_t_2; + __pyx_t_2 = 0; + } + __pyx_t_8 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isbam = __pyx_t_8; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":490 + * self.isbam = len(mode) > 1 and mode[1] == 'b' + * + * self.isremote = strncmp(filename,"http:",5) == 0 or \ # <<<<<<<<<<<<<< + * strncmp(filename,"ftp:",4) == 0 + * + */ + __pyx_t_3 = (strncmp(__pyx_v_filename, __pyx_k_17, 5) == 0); + if (!__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":491 + * + * self.isremote = strncmp(filename,"http:",5) == 0 or \ + * strncmp(filename,"ftp:",4) == 0 # <<<<<<<<<<<<<< + * + * cdef char * ctext + */ + __pyx_t_4 = (strncmp(__pyx_v_filename, __pyx_k_18, 4) == 0); + __pyx_t_5 = __pyx_t_4; + } else { + __pyx_t_5 = __pyx_t_3; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":490 + * self.isbam = len(mode) > 1 and mode[1] == 'b' + * + * self.isremote = strncmp(filename,"http:",5) == 0 or \ # <<<<<<<<<<<<<< + * strncmp(filename,"ftp:",4) == 0 + * + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isremote = __pyx_t_5; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":494 + * + * cdef char * ctext + * ctext = NULL # <<<<<<<<<<<<<< + * + * if mode[0] == 'w': + */ + __pyx_v_ctext = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":496 + * ctext = NULL + * + * if mode[0] == 'w': # <<<<<<<<<<<<<< + * # open file for writing + * + */ + __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_mode, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)__pyx_n_s__w), Py_EQ); 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_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":500 + * + * # header structure (used for writing) + * if template: # <<<<<<<<<<<<<< + * # copy header from another file + * header_to_write = template.samfile.header + */ + __pyx_t_5 = __Pyx_PyObject_IsTrue(((PyObject *)__pyx_v_template)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":502 + * if template: + * # copy header from another file + * header_to_write = template.samfile.header # <<<<<<<<<<<<<< + * + * elif header: + */ + __pyx_v_header_to_write = __pyx_v_template->samfile->header; + goto __pyx_L9; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":504 + * header_to_write = template.samfile.header + * + * elif header: # <<<<<<<<<<<<<< + * header_to_write = self._buildHeader( header ) + * + */ + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_v_header); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":505 + * + * elif header: + * header_to_write = self._buildHeader( header ) # <<<<<<<<<<<<<< + * + * else: + */ + __pyx_v_header_to_write = ((struct __pyx_vtabstruct_9csamtools_Samfile *)((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->__pyx_vtab)->_buildHeader(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self), __pyx_v_header); + goto __pyx_L9; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":509 + * 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" + * + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_v_referencenames); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_5) { + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_referencelengths); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = __pyx_t_3; + } else { + __pyx_t_4 = __pyx_t_5; + } + if (unlikely(!__pyx_t_4)) { + PyErr_SetObject(PyExc_AssertionError, ((PyObject *)__pyx_kp_s_19)); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":510 + * # 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 + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + __pyx_t_6 = PyObject_Length(__pyx_v_referencenames); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_9 = PyObject_Length(__pyx_v_referencelengths); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!(__pyx_t_6 == __pyx_t_9))) { + PyErr_SetObject(PyExc_AssertionError, ((PyObject *)__pyx_kp_s_20)); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":513 + * + * # allocate and fill header + * header_to_write = bam_header_init() # <<<<<<<<<<<<<< + * header_to_write.n_targets = len(referencenames) + * n = 0 + */ + __pyx_v_header_to_write = bam_header_init(); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":514 + * # allocate and fill header + * header_to_write = bam_header_init() + * header_to_write.n_targets = len(referencenames) # <<<<<<<<<<<<<< + * n = 0 + * for x in referencenames: n += len(x) + 1 + */ + __pyx_t_9 = PyObject_Length(__pyx_v_referencenames); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_header_to_write->n_targets = __pyx_t_9; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":515 + * header_to_write = bam_header_init() + * header_to_write.n_targets = len(referencenames) + * n = 0 # <<<<<<<<<<<<<< + * for x in referencenames: n += len(x) + 1 + * header_to_write.target_name = calloc(n, sizeof(char*)) + */ + __Pyx_INCREF(__pyx_int_0); + __Pyx_DECREF(__pyx_v_n); + __pyx_v_n = __pyx_int_0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":516 + * header_to_write.n_targets = len(referencenames) + * n = 0 + * for x in referencenames: n += len(x) + 1 # <<<<<<<<<<<<<< + * header_to_write.target_name = calloc(n, sizeof(char*)) + * header_to_write.target_len = calloc(n, sizeof(uint32_t)) + */ + if (PyList_CheckExact(__pyx_v_referencenames) || PyTuple_CheckExact(__pyx_v_referencenames)) { + __pyx_t_9 = 0; __pyx_t_2 = __pyx_v_referencenames; __Pyx_INCREF(__pyx_t_2); + } else { + __pyx_t_9 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_v_referencenames); 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); + } + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_2))) { + if (__pyx_t_9 >= PyList_GET_SIZE(__pyx_t_2)) break; + __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_9); __Pyx_INCREF(__pyx_t_1); __pyx_t_9++; + } else if (likely(PyTuple_CheckExact(__pyx_t_2))) { + if (__pyx_t_9 >= PyTuple_GET_SIZE(__pyx_t_2)) break; + __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_9); __Pyx_INCREF(__pyx_t_1); __pyx_t_9++; + } else { + __pyx_t_1 = PyIter_Next(__pyx_t_2); + if (!__pyx_t_1) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_1); + } + __Pyx_DECREF(__pyx_v_x); + __pyx_v_x = __pyx_t_1; + __pyx_t_1 = 0; + __pyx_t_6 = PyObject_Length(__pyx_v_x); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = PyInt_FromSsize_t((__pyx_t_6 + 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_7 = PyNumber_InPlaceAdd(__pyx_v_n, __pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_n); + __pyx_v_n = __pyx_t_7; + __pyx_t_7 = 0; + } + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":517 + * n = 0 + * for x in referencenames: n += len(x) + 1 + * header_to_write.target_name = calloc(n, sizeof(char*)) # <<<<<<<<<<<<<< + * header_to_write.target_len = calloc(n, sizeof(uint32_t)) + * for x from 0 <= x < header_to_write.n_targets: + */ + __pyx_t_10 = __Pyx_PyInt_AsSize_t(__pyx_v_n); if (unlikely((__pyx_t_10 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_header_to_write->target_name = ((char **)calloc(__pyx_t_10, (sizeof(char *)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":518 + * for x in referencenames: n += len(x) + 1 + * header_to_write.target_name = calloc(n, sizeof(char*)) + * header_to_write.target_len = calloc(n, sizeof(uint32_t)) # <<<<<<<<<<<<<< + * for x from 0 <= x < header_to_write.n_targets: + * header_to_write.target_len[x] = referencelengths[x] + */ + __pyx_t_10 = __Pyx_PyInt_AsSize_t(__pyx_v_n); if (unlikely((__pyx_t_10 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_header_to_write->target_len = ((uint32_t *)calloc(__pyx_t_10, (sizeof(uint32_t)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":519 + * header_to_write.target_name = calloc(n, sizeof(char*)) + * header_to_write.target_len = calloc(n, sizeof(uint32_t)) + * for x from 0 <= x < header_to_write.n_targets: # <<<<<<<<<<<<<< + * header_to_write.target_len[x] = referencelengths[x] + * name = referencenames[x] + */ + __pyx_t_11 = __pyx_v_header_to_write->n_targets; + for (__pyx_t_12 = 0; __pyx_t_12 < __pyx_t_11; __pyx_t_12++) { + __pyx_t_2 = PyInt_FromLong(__pyx_t_12); 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_DECREF(__pyx_v_x); + __pyx_v_x = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":520 + * header_to_write.target_len = calloc(n, sizeof(uint32_t)) + * for x from 0 <= x < header_to_write.n_targets: + * header_to_write.target_len[x] = referencelengths[x] # <<<<<<<<<<<<<< + * name = referencenames[x] + * header_to_write.target_name[x] = calloc(len(name)+1, sizeof(char)) + */ + __pyx_t_2 = PyObject_GetItem(__pyx_v_referencelengths, __pyx_v_x); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_13 = __Pyx_PyInt_from_py_uint32_t(__pyx_t_2); if (unlikely((__pyx_t_13 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_9 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_9 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + (__pyx_v_header_to_write->target_len[__pyx_t_9]) = __pyx_t_13; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":521 + * for x from 0 <= x < header_to_write.n_targets: + * header_to_write.target_len[x] = referencelengths[x] + * name = referencenames[x] # <<<<<<<<<<<<<< + * header_to_write.target_name[x] = calloc(len(name)+1, sizeof(char)) + * strncpy( header_to_write.target_name[x], name, len(name) ) + */ + __pyx_t_2 = PyObject_GetItem(__pyx_v_referencenames, __pyx_v_x); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_name); + __pyx_v_name = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":522 + * header_to_write.target_len[x] = referencelengths[x] + * name = referencenames[x] + * header_to_write.target_name[x] = calloc(len(name)+1, sizeof(char)) # <<<<<<<<<<<<<< + * strncpy( header_to_write.target_name[x], name, len(name) ) + * + */ + __pyx_t_9 = PyObject_Length(__pyx_v_name); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_6 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_6 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + (__pyx_v_header_to_write->target_name[__pyx_t_6]) = ((char *)calloc((__pyx_t_9 + 1), (sizeof(char)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":523 + * name = referencenames[x] + * header_to_write.target_name[x] = calloc(len(name)+1, sizeof(char)) + * strncpy( header_to_write.target_name[x], name, len(name) ) # <<<<<<<<<<<<<< + * + * if text != None: + */ + __pyx_t_9 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_9 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_14 = PyBytes_AsString(__pyx_v_name); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_6 = PyObject_Length(__pyx_v_name); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + strncpy((__pyx_v_header_to_write->target_name[__pyx_t_9]), __pyx_t_14, __pyx_t_6); + __pyx_t_12 = __Pyx_PyInt_AsLong(__pyx_v_x); if (unlikely((__pyx_t_12 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":519 + * header_to_write.target_name = calloc(n, sizeof(char*)) + * header_to_write.target_len = calloc(n, sizeof(uint32_t)) + * for x from 0 <= x < header_to_write.n_targets: # <<<<<<<<<<<<<< + * header_to_write.target_len[x] = referencelengths[x] + * name = referencenames[x] + */ + __pyx_t_2 = PyInt_FromLong(__pyx_t_12); 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_DECREF(__pyx_v_x); + __pyx_v_x = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":525 + * strncpy( header_to_write.target_name[x], name, len(name) ) + * + * if text != None: # <<<<<<<<<<<<<< + * # copy without \0 + * ctext = text + */ + __pyx_t_2 = PyObject_RichCompare(__pyx_v_text, Py_None, Py_NE); 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_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":527 + * if text != None: + * # copy without \0 + * ctext = text # <<<<<<<<<<<<<< + * header_to_write.l_text = strlen(ctext) + * header_to_write.text = calloc( strlen(ctext), sizeof(char) ) + */ + __pyx_t_14 = PyBytes_AsString(__pyx_v_text); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_ctext = __pyx_t_14; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":528 + * # copy without \0 + * ctext = text + * header_to_write.l_text = strlen(ctext) # <<<<<<<<<<<<<< + * header_to_write.text = calloc( strlen(ctext), sizeof(char) ) + * memcpy( header_to_write.text, ctext, strlen(ctext) ) + */ + __pyx_v_header_to_write->l_text = strlen(__pyx_v_ctext); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":529 + * ctext = text + * header_to_write.l_text = strlen(ctext) + * header_to_write.text = calloc( strlen(ctext), sizeof(char) ) # <<<<<<<<<<<<<< + * memcpy( header_to_write.text, ctext, strlen(ctext) ) + * + */ + __pyx_v_header_to_write->text = ((char *)calloc(strlen(__pyx_v_ctext), (sizeof(char)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":530 + * header_to_write.l_text = strlen(ctext) + * header_to_write.text = calloc( strlen(ctext), sizeof(char) ) + * memcpy( header_to_write.text, ctext, strlen(ctext) ) # <<<<<<<<<<<<<< + * + * header_to_write.hash = NULL + */ + memcpy(__pyx_v_header_to_write->text, __pyx_v_ctext, strlen(__pyx_v_ctext)); + goto __pyx_L14; + } + __pyx_L14:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":532 + * memcpy( header_to_write.text, ctext, strlen(ctext) ) + * + * header_to_write.hash = NULL # <<<<<<<<<<<<<< + * header_to_write.rg2lib = NULL + * + */ + __pyx_v_header_to_write->hash = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":533 + * + * header_to_write.hash = NULL + * header_to_write.rg2lib = NULL # <<<<<<<<<<<<<< + * + * # open file. Header gets written to file at the same time for bam files + */ + __pyx_v_header_to_write->rg2lib = NULL; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":537 + * # 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) + * store = StderrStore() # <<<<<<<<<<<<<< + * self.samfile = samopen( filename, mode, header_to_write ) + * store.release() + */ + __pyx_t_2 = __Pyx_GetName(__pyx_m, __pyx_n_s__StderrStore); 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_t_7 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_v_store); + __pyx_v_store = __pyx_t_7; + __pyx_t_7 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":538 + * # and sam files (in the latter case, the mode needs to be wh) + * store = StderrStore() + * self.samfile = samopen( filename, mode, header_to_write ) # <<<<<<<<<<<<<< + * store.release() + * + */ + __pyx_t_14 = PyBytes_AsString(__pyx_v_mode); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 538; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile = samopen(__pyx_v_filename, __pyx_t_14, __pyx_v_header_to_write); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":539 + * store = StderrStore() + * self.samfile = samopen( filename, mode, header_to_write ) + * store.release() # <<<<<<<<<<<<<< + * + * # bam_header_destroy takes care of cleaning up of all the members + */ + __pyx_t_7 = PyObject_GetAttr(__pyx_v_store, __pyx_n_s__release); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 539; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_2 = PyObject_Call(__pyx_t_7, ((PyObject *)__pyx_empty_tuple), 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_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":542 + * + * # bam_header_destroy takes care of cleaning up of all the members + * if not template and header_to_write != NULL: # <<<<<<<<<<<<<< + * bam_header_destroy( header_to_write ) + * + */ + __pyx_t_4 = __Pyx_PyObject_IsTrue(((PyObject *)__pyx_v_template)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_5 = (!__pyx_t_4); + if (__pyx_t_5) { + __pyx_t_4 = (__pyx_v_header_to_write != NULL); + __pyx_t_3 = __pyx_t_4; + } else { + __pyx_t_3 = __pyx_t_5; + } + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":543 + * # bam_header_destroy takes care of cleaning up of all the members + * if not template and header_to_write != NULL: + * bam_header_destroy( header_to_write ) # <<<<<<<<<<<<<< + * + * elif mode[0] == "r": + */ + bam_header_destroy(__pyx_v_header_to_write); + goto __pyx_L15; + } + __pyx_L15:; + goto __pyx_L8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":545 + * bam_header_destroy( header_to_write ) + * + * elif mode[0] == "r": # <<<<<<<<<<<<<< + * # open file for reading + * if strncmp( filename, "-", 1) != 0 and \ + */ + __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_mode, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_7 = PyObject_RichCompare(__pyx_t_2, ((PyObject *)__pyx_n_s__r), Py_EQ); 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_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":547 + * elif mode[0] == "r": + * # open file for reading + * if strncmp( filename, "-", 1) != 0 and \ # <<<<<<<<<<<<<< + * not self.isremote and \ + * not os.path.exists( filename ): + */ + __pyx_t_3 = (strncmp(__pyx_v_filename, __pyx_k_21, 1) != 0); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":548 + * # open file for reading + * if strncmp( filename, "-", 1) != 0 and \ + * not self.isremote and \ # <<<<<<<<<<<<<< + * not os.path.exists( filename ): + * raise IOError( "file `%s` not found" % filename) + */ + __pyx_t_5 = (!((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isremote); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":549 + * if strncmp( filename, "-", 1) != 0 and \ + * not self.isremote and \ + * not os.path.exists( filename ): # <<<<<<<<<<<<<< + * raise IOError( "file `%s` not found" % filename) + * + */ + __pyx_t_7 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_2 = PyObject_GetAttr(__pyx_t_7, __pyx_n_s__path); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __pyx_t_7 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__exists); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_2 = PyBytes_FromString(__pyx_v_filename); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_2)); + __pyx_t_2 = 0; + __pyx_t_2 = PyObject_Call(__pyx_t_7, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __Pyx_DECREF(((PyObject *)__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 = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_15 = (!__pyx_t_4); + __pyx_t_4 = __pyx_t_15; + } else { + __pyx_t_4 = __pyx_t_5; + } + __pyx_t_5 = __pyx_t_4; + } else { + __pyx_t_5 = __pyx_t_3; + } + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":550 + * not self.isremote and \ + * not os.path.exists( filename ): + * raise IOError( "file `%s` not found" % filename) # <<<<<<<<<<<<<< + * + * # try to detect errors + */ + __pyx_t_2 = PyBytes_FromString(__pyx_v_filename); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_22), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__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 = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_IOError, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L16; + } + __pyx_L16:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":553 + * + * # try to detect errors + * self.samfile = samopen( filename, mode, NULL ) # <<<<<<<<<<<<<< + * if self.samfile == NULL: + * raise ValueError( "could not open file - is it SAM/BAM format?") + */ + __pyx_t_14 = PyBytes_AsString(__pyx_v_mode); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile = samopen(__pyx_v_filename, __pyx_t_14, NULL); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":554 + * # try to detect errors + * self.samfile = samopen( filename, mode, NULL ) + * if self.samfile == NULL: # <<<<<<<<<<<<<< + * raise ValueError( "could not open file - is it SAM/BAM format?") + * + */ + __pyx_t_5 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile == NULL); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":555 + * self.samfile = samopen( filename, mode, NULL ) + * if self.samfile == NULL: + * raise ValueError( "could not open file - is it SAM/BAM format?") # <<<<<<<<<<<<<< + * + * if self.samfile.header == NULL: + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_24), NULL); 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); + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L17; + } + __pyx_L17:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":557 + * raise ValueError( "could not open file - is it SAM/BAM format?") + * + * if self.samfile.header == NULL: # <<<<<<<<<<<<<< + * raise ValueError( "could not open file - is it SAM/BAM format?") + * + */ + __pyx_t_5 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header == NULL); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":558 + * + * if self.samfile.header == NULL: + * raise ValueError( "could not open file - is it SAM/BAM format?") # <<<<<<<<<<<<<< + * + * if self.samfile.header.n_targets == 0: + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_25), NULL); 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_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L18; + } + __pyx_L18:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":560 + * raise ValueError( "could not open file - is it SAM/BAM format?") + * + * if self.samfile.header.n_targets == 0: # <<<<<<<<<<<<<< + * raise ValueError( "could not open file - is it SAM/BAM format?") + * + */ + __pyx_t_5 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->n_targets == 0); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":561 + * + * if self.samfile.header.n_targets == 0: + * raise ValueError( "could not open file - is it SAM/BAM format?") # <<<<<<<<<<<<<< + * + * if self.samfile == NULL: + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_26), NULL); 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_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L19; + } + __pyx_L19:; + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":563 + * raise ValueError( "could not open file - is it SAM/BAM format?") + * + * if self.samfile == NULL: # <<<<<<<<<<<<<< + * raise IOError("could not open file `%s`" % filename ) + * + */ + __pyx_t_5 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile == NULL); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":564 + * + * if self.samfile == NULL: + * raise IOError("could not open file `%s`" % filename ) # <<<<<<<<<<<<<< + * + * # check for index and open if present + */ + __pyx_t_1 = PyBytes_FromString(__pyx_v_filename); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_t_2 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_5), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__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 = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_2)); + __pyx_t_2 = 0; + __pyx_t_2 = PyObject_Call(__pyx_builtin_IOError, ((PyObject *)__pyx_t_1), NULL); 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_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L20; + } + __pyx_L20:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":567 + * + * # check for index and open if present + * if mode[0] == "r" and self.isbam: # <<<<<<<<<<<<<< + * + * if not self.isremote: + */ + __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_mode, 0, sizeof(long), PyInt_FromLong); if (!__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_1 = PyObject_RichCompare(__pyx_t_2, ((PyObject *)__pyx_n_s__r), Py_EQ); 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_5 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_5) { + __pyx_t_3 = ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isbam; + } else { + __pyx_t_3 = __pyx_t_5; + } + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":569 + * if mode[0] == "r" and self.isbam: + * + * if not self.isremote: # <<<<<<<<<<<<<< + * if not os.path.exists(filename +".bai"): + * self.index = NULL + */ + __pyx_t_3 = (!((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isremote); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":570 + * + * if not self.isremote: + * if not os.path.exists(filename +".bai"): # <<<<<<<<<<<<<< + * self.index = NULL + * else: + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); 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 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__path); 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_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__exists); 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_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_2 = PyBytes_FromString(__pyx_v_filename); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_t_7 = PyNumber_Add(((PyObject *)__pyx_t_2), ((PyObject *)__pyx_kp_s_27)); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(((PyObject *)__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 = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __pyx_t_5 = (!__pyx_t_3); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":571 + * if not self.isremote: + * if not os.path.exists(filename +".bai"): + * self.index = NULL # <<<<<<<<<<<<<< + * else: + * # returns NULL if there is no index or index could not be opened + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index = NULL; + goto __pyx_L23; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":574 + * else: + * # returns NULL if there is no index or index could not be opened + * self.index = bam_index_load(filename) # <<<<<<<<<<<<<< + * if self.index == NULL: + * raise IOError("error while opening index `%s` " % filename ) + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index = bam_index_load(__pyx_v_filename); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":575 + * # returns NULL if there is no index or index could not be opened + * self.index = bam_index_load(filename) + * if self.index == NULL: # <<<<<<<<<<<<<< + * raise IOError("error while opening index `%s` " % filename ) + * else: + */ + __pyx_t_5 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index == NULL); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":576 + * self.index = bam_index_load(filename) + * if self.index == NULL: + * raise IOError("error while opening index `%s` " % filename ) # <<<<<<<<<<<<<< + * else: + * self.index = bam_index_load(filename) + */ + __pyx_t_7 = PyBytes_FromString(__pyx_v_filename); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_7)); + __pyx_t_2 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_28), ((PyObject *)__pyx_t_7)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__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 = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_7)); + PyTuple_SET_ITEM(__pyx_t_7, 0, ((PyObject *)__pyx_t_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_2)); + __pyx_t_2 = 0; + __pyx_t_2 = PyObject_Call(__pyx_builtin_IOError, ((PyObject *)__pyx_t_7), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(((PyObject *)__pyx_t_7)); __pyx_t_7 = 0; + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L24; + } + __pyx_L24:; + } + __pyx_L23:; + goto __pyx_L22; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":578 + * raise IOError("error while opening index `%s` " % filename ) + * else: + * self.index = bam_index_load(filename) # <<<<<<<<<<<<<< + * if self.index == NULL: + * raise IOError("error while opening index `%s` " % filename ) + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index = bam_index_load(__pyx_v_filename); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":579 + * else: + * self.index = bam_index_load(filename) + * if self.index == NULL: # <<<<<<<<<<<<<< + * raise IOError("error while opening index `%s` " % filename ) + * + */ + __pyx_t_5 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index == NULL); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":580 + * self.index = bam_index_load(filename) + * if self.index == NULL: + * raise IOError("error while opening index `%s` " % filename ) # <<<<<<<<<<<<<< + * + * def getrname( self, tid ): + */ + __pyx_t_2 = PyBytes_FromString(__pyx_v_filename); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_t_7 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_28), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_7)); + __Pyx_DECREF(((PyObject *)__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 = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_7)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_7)); + __pyx_t_7 = 0; + __pyx_t_7 = PyObject_Call(__pyx_builtin_IOError, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L25; + } + __pyx_L25:; + } + __pyx_L22:; + goto __pyx_L21; + } + __pyx_L21:; + + __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_7); + __Pyx_AddTraceback("csamtools.Samfile._open"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_n); + __Pyx_DECREF(__pyx_v_x); + __Pyx_DECREF(__pyx_v_name); + __Pyx_DECREF(__pyx_v_store); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":582 + * raise IOError("error while opening index `%s` " % filename ) + * + * def getrname( self, tid ): # <<<<<<<<<<<<<< + * ''' + * convert numerical :term:`tid` into :term:`reference` name.''' + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_4getrname(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_4getrname[] = "\n convert numerical :term:`tid` into :term:`reference` name."; +static PyObject *__pyx_pf_9csamtools_7Samfile_4getrname(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + Py_ssize_t __pyx_t_5; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("getrname"); + __Pyx_TraceCall("getrname", __pyx_f[0], 582); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":585 + * ''' + * 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.samfile.header.n_targets: + * raise ValueError( "tid out of range 0<=tid<%i" % self.samfile.header.n_targets ) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); 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_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __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 = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_29), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":586 + * 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.samfile.header.n_targets: # <<<<<<<<<<<<<< + * raise ValueError( "tid out of range 0<=tid<%i" % self.samfile.header.n_targets ) + * return self.samfile.header.target_name[tid] + */ + __pyx_t_2 = PyObject_RichCompare(__pyx_int_0, __pyx_v_tid, Py_LE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + if (__Pyx_PyObject_IsTrue(__pyx_t_2)) { + __Pyx_DECREF(__pyx_t_2); + __pyx_t_1 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->n_targets); 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_t_2 = PyObject_RichCompare(__pyx_v_tid, __pyx_t_1, Py_LT); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __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 = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_3 = (!__pyx_t_4); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":587 + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * if not 0 <= tid < self.samfile.header.n_targets: + * raise ValueError( "tid out of range 0<=tid<%i" % self.samfile.header.n_targets ) # <<<<<<<<<<<<<< + * return self.samfile.header.target_name[tid] + * + */ + __pyx_t_2 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->n_targets); 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); + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_30), __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_2), NULL); 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(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":588 + * if not 0 <= tid < self.samfile.header.n_targets: + * raise ValueError( "tid out of range 0<=tid<%i" % self.samfile.header.n_targets ) + * return self.samfile.header.target_name[tid] # <<<<<<<<<<<<<< + * + * def gettid( self, reference ): + */ + __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 = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = PyBytes_FromString((((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->target_name[__pyx_t_5])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_r = ((PyObject *)__pyx_t_1); + __pyx_t_1 = 0; + goto __pyx_L0; + + __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("csamtools.Samfile.getrname"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":590 + * return self.samfile.header.target_name[tid] + * + * def gettid( self, reference ): # <<<<<<<<<<<<<< + * ''' + * convert :term:`reference` name into numerical :term:`tid` + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_5gettid(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_5gettid[] = "\n convert :term:`reference` name into numerical :term:`tid`\n\n returns -1 if reference is not known.\n "; +static PyObject *__pyx_pf_9csamtools_7Samfile_5gettid(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + char *__pyx_t_5; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("gettid"); + __Pyx_TraceCall("gettid", __pyx_f[0], 590); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":596 + * returns -1 if reference is not known. + * ''' + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * return pysam_reference2tid( self.samfile.header, reference ) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __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 = 596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_31), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 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;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":597 + * ''' + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return pysam_reference2tid( self.samfile.header, reference ) # <<<<<<<<<<<<<< + * + * def _parseRegion( self, + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_5 = PyBytes_AsString(__pyx_v_reference); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = PyInt_FromLong(pysam_reference2tid(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header, __pyx_t_5)); 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_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + + __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("csamtools.Samfile.gettid"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":599 + * return pysam_reference2tid( self.samfile.header, reference ) + * + * def _parseRegion( self, # <<<<<<<<<<<<<< + * reference = None, + * start = None, + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_6_parseRegion(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_6_parseRegion[] = "\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_pf_9csamtools_7Samfile_6_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; + int __pyx_v_rtid; + int __pyx_v_rstart; + int __pyx_v_rend; + PyObject *__pyx_v_parts; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + int __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_5 = NULL; + Py_ssize_t __pyx_t_6; + int __pyx_t_7; + PyObject *__pyx_t_8 = NULL; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__reference,&__pyx_n_s__start,&__pyx_n_s__end,&__pyx_n_s__region,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_parseRegion"); + __Pyx_TraceCall("_parseRegion", __pyx_f[0], 599); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[4] = {0,0,0,0}; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":600 + * + * def _parseRegion( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + values[0] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":601 + * def _parseRegion( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None ): + */ + values[1] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":602 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None ): + * ''' + */ + values[2] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":603 + * start = None, + * end = None, + * region = None ): # <<<<<<<<<<<<<< + * ''' + * parse region information. + */ + values[3] = ((PyObject *)Py_None); + 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; + } + switch (PyTuple_GET_SIZE(__pyx_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, 0, values, PyTuple_GET_SIZE(__pyx_args), "_parseRegion") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_reference = values[0]; + __pyx_v_start = values[1]; + __pyx_v_end = values[2]; + __pyx_v_region = values[3]; + } else { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":600 + * + * def _parseRegion( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + __pyx_v_reference = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":601 + * def _parseRegion( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None ): + */ + __pyx_v_start = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":602 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None ): + * ''' + */ + __pyx_v_end = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":603 + * start = None, + * end = None, + * region = None ): # <<<<<<<<<<<<<< + * ''' + * parse region information. + */ + __pyx_v_region = ((PyObject *)Py_None); + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 4: __pyx_v_region = PyTuple_GET_ITEM(__pyx_args, 3); + case 3: __pyx_v_end = PyTuple_GET_ITEM(__pyx_args, 2); + case 2: __pyx_v_start = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_reference = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("_parseRegion", 0, 0, 4, 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("csamtools.Samfile._parseRegion"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __Pyx_INCREF(__pyx_v_reference); + __pyx_v_parts = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":622 + * cdef int rend + * + * rtid = -1 # <<<<<<<<<<<<<< + * rstart = 0 + * rend = max_pos + */ + __pyx_v_rtid = -1; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":623 + * + * rtid = -1 + * rstart = 0 # <<<<<<<<<<<<<< + * rend = max_pos + * if start != None: rstart = start + */ + __pyx_v_rstart = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":624 + * rtid = -1 + * rstart = 0 + * rend = max_pos # <<<<<<<<<<<<<< + * if start != None: rstart = start + * if end != None: rend = end + */ + __pyx_v_rend = __pyx_v_9csamtools_max_pos; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":625 + * rstart = 0 + * rend = max_pos + * if start != None: rstart = start # <<<<<<<<<<<<<< + * if end != None: rend = end + * + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, Py_None, Py_NE); 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_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_v_start); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_rstart = __pyx_t_3; + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":626 + * rend = max_pos + * if start != None: rstart = start + * if end != None: rend = end # <<<<<<<<<<<<<< + * + * if region: + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_end, Py_None, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __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 = 626; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_v_end); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_rend = __pyx_t_3; + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":628 + * if end != None: rend = end + * + * if region: # <<<<<<<<<<<<<< + * parts = re.split( "[:-]", region ) + * reference = parts[0] + */ + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_region); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_2) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":629 + * + * if region: + * parts = re.split( "[:-]", region ) # <<<<<<<<<<<<<< + * reference = parts[0] + * if len(parts) >= 2: rstart = int(parts[1]) - 1 + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__re); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_4 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__split); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __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(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_32)); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_kp_s_32)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_32)); + __Pyx_INCREF(__pyx_v_region); + PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_region); + __Pyx_GIVEREF(__pyx_v_region); + __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_parts); + __pyx_v_parts = __pyx_t_5; + __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":630 + * if 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]) + */ + __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_parts, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 630; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_v_reference); + __pyx_v_reference = __pyx_t_5; + __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":631 + * parts = re.split( "[:-]", region ) + * reference = parts[0] + * if len(parts) >= 2: rstart = int(parts[1]) - 1 # <<<<<<<<<<<<<< + * if len(parts) >= 3: rend = int(parts[2]) + * + */ + __pyx_t_6 = PyObject_Length(__pyx_v_parts); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = (__pyx_t_6 >= 2); + if (__pyx_t_2) { + __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_parts, 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __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 = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(((PyObject *)((PyObject*)(&PyInt_Type))), ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __pyx_t_1 = PyNumber_Subtract(__pyx_t_5, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_v_rstart = __pyx_t_3; + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":632 + * 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 + */ + __pyx_t_6 = PyObject_Length(__pyx_v_parts); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = (__pyx_t_6 >= 3); + if (__pyx_t_2) { + __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_parts, 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __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 = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(((PyObject *)((PyObject*)(&PyInt_Type))), ((PyObject *)__pyx_t_5), NULL); 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); + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_v_rend = __pyx_t_3; + goto __pyx_L10; + } + __pyx_L10:; + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":634 + * if len(parts) >= 3: rend = int(parts[2]) + * + * if not reference: return 0, 0, 0, 0 # <<<<<<<<<<<<<< + * + * rtid = self.gettid( reference ) + */ + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_reference); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 634; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_7 = (!__pyx_t_2); + if (__pyx_t_7) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_k_tuple_33)); + __pyx_r = ((PyObject *)__pyx_k_tuple_33); + goto __pyx_L0; + goto __pyx_L11; + } + __pyx_L11:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":636 + * if not reference: return 0, 0, 0, 0 + * + * 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) ) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__gettid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __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 = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __Pyx_INCREF(__pyx_v_reference); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_reference); + __Pyx_GIVEREF(__pyx_v_reference); + __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_t_4); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_v_rtid = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":637 + * + * 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 ) + */ + __pyx_t_7 = (__pyx_v_rtid < 0); + if (__pyx_t_7) { + __pyx_t_4 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_34), __pyx_v_reference); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_4)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); + __pyx_t_4 = 0; + __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_5), NULL); 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(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __Pyx_Raise(__pyx_t_4, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L12; + } + __pyx_L12:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":638 + * 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 ) + */ + __pyx_t_7 = (__pyx_v_rstart > __pyx_v_rend); + if (__pyx_t_7) { + __pyx_t_4 = PyInt_FromLong(__pyx_v_rstart); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_5 = PyInt_FromLong(__pyx_v_rend); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __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 = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + __pyx_t_4 = 0; + __pyx_t_5 = 0; + __pyx_t_5 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_35), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __Pyx_DECREF(((PyObject *)__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 = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_5)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_5)); + __pyx_t_5 = 0; + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L13; + } + __pyx_L13:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":639 + * 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 ) + * + */ + __pyx_t_7 = (0 <= __pyx_v_rstart); + if (__pyx_t_7) { + __pyx_t_7 = (__pyx_v_rstart < __pyx_v_9csamtools_max_pos); + } + __pyx_t_2 = (!__pyx_t_7); + if (__pyx_t_2) { + __pyx_t_5 = PyInt_FromLong(__pyx_v_rstart); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_13), __pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L14; + } + __pyx_L14:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":640 + * 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 + */ + __pyx_t_2 = (0 <= __pyx_v_rend); + if (__pyx_t_2) { + __pyx_t_2 = (__pyx_v_rend <= __pyx_v_9csamtools_max_pos); + } + __pyx_t_7 = (!__pyx_t_2); + if (__pyx_t_7) { + __pyx_t_1 = PyInt_FromLong(__pyx_v_rend); 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_t_5 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_14), __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __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 = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_5)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_5)); + __pyx_t_5 = 0; + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L15; + } + __pyx_L15:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":642 + * if not 0 <= rend <= max_pos: raise ValueError( 'end out of range (%i)' % rend ) + * + * return 1, rtid, rstart, rend # <<<<<<<<<<<<<< + * + * def seek( self, uint64_t offset, int where = 0): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_5 = PyInt_FromLong(__pyx_v_rtid); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_1 = PyInt_FromLong(__pyx_v_rstart); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_4 = PyInt_FromLong(__pyx_v_rend); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_8 = PyTuple_New(4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_8)); + __Pyx_INCREF(__pyx_int_1); + PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_int_1); + __Pyx_GIVEREF(__pyx_int_1); + PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_8, 2, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + PyTuple_SET_ITEM(__pyx_t_8, 3, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + __pyx_t_5 = 0; + __pyx_t_1 = 0; + __pyx_t_4 = 0; + __pyx_r = ((PyObject *)__pyx_t_8); + __pyx_t_8 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_AddTraceback("csamtools.Samfile._parseRegion"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_parts); + __Pyx_DECREF(__pyx_v_reference); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":644 + * return 1, rtid, rstart, rend + * + * def seek( self, uint64_t offset, int where = 0): # <<<<<<<<<<<<<< + * ''' + * move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`. + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_7seek(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_7seek[] = "\n move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.\n "; +static PyObject *__pyx_pf_9csamtools_7Samfile_7seek(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + uint64_t __pyx_v_offset; + int __pyx_v_where; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__offset,&__pyx_n_s__where,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("seek"); + __Pyx_TraceCall("seek", __pyx_f[0], 644); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__offset); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "seek") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_offset = __Pyx_PyInt_from_py_uint64_t(values[0]); if (unlikely((__pyx_v_offset == (uint64_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + if (values[1]) { + __pyx_v_where = __Pyx_PyInt_AsInt(values[1]); if (unlikely((__pyx_v_where == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } else { + __pyx_v_where = ((int)0); + } + } else { + __pyx_v_where = ((int)0); + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 2: __pyx_v_where = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 1)); if (unlikely((__pyx_v_where == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + case 1: __pyx_v_offset = __Pyx_PyInt_from_py_uint64_t(PyTuple_GET_ITEM(__pyx_args, 0)); if (unlikely((__pyx_v_offset == (uint64_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + break; + default: goto __pyx_L5_argtuple_error; + } + } + 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 = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Samfile.seek"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":649 + * ''' + * + * if not self._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * if not self.isbam: + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 649; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 649; __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 = 649; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":650 + * + * 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 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_36), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":651 + * 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 bam_seek( self.samfile.x.bam, offset, where ) + */ + __pyx_t_4 = (!((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isbam); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":652 + * raise ValueError( "I/O operation on closed file" ) + * if not self.isbam: + * raise NotImplementedError("seek only available in bam files") # <<<<<<<<<<<<<< + * return bam_seek( self.samfile.x.bam, offset, where ) + * + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_NotImplementedError, ((PyObject *)__pyx_k_tuple_38), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":653 + * if not self.isbam: + * raise NotImplementedError("seek only available in bam files") + * return bam_seek( self.samfile.x.bam, offset, where ) # <<<<<<<<<<<<<< + * + * def tell( self ): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = __Pyx_PyInt_to_py_int64_t(bam_seek(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->x.bam, __pyx_v_offset, __pyx_v_where)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __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_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("csamtools.Samfile.seek"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":655 + * return bam_seek( self.samfile.x.bam, offset, where ) + * + * def tell( self ): # <<<<<<<<<<<<<< + * ''' + * return current file position + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_8tell(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_8tell[] = "\n return current file position\n "; +static PyObject *__pyx_pf_9csamtools_7Samfile_8tell(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("tell"); + __Pyx_TraceCall("tell", __pyx_f[0], 655); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":659 + * return current file position + * ''' + * if not self._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * if not self.isbam: + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); 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_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 = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":660 + * ''' + * 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 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_39), NULL); 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_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 660; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":661 + * 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 = (!((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isbam); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":662 + * raise ValueError( "I/O operation on closed file" ) + * if not self.isbam: + * raise NotImplementedError("seek only available in bam files") # <<<<<<<<<<<<<< + * + * return bam_tell( self.samfile.x.bam ) + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_NotImplementedError, ((PyObject *)__pyx_k_tuple_40), NULL); 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_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 662; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":664 + * raise NotImplementedError("seek only available in bam files") + * + * return bam_tell( self.samfile.x.bam ) # <<<<<<<<<<<<<< + * + * def fetch( self, + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = __Pyx_PyInt_to_py_int64_t(bam_tell(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->x.bam)); 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_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + + __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("csamtools.Samfile.tell"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":666 + * return bam_tell( self.samfile.x.bam ) + * + * def fetch( self, # <<<<<<<<<<<<<< + * reference = None, + * start = None, + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_9fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_9fetch[] = "\n fetch aligned reads in a :term:`region` using 0-based indexing. The region is specified by\n :term:`reference`, *start* and *end*. Alternatively, a samtools :term:`region` string can \n be supplied.\n\n Without *reference* or *region* all reads will be fetched. The reads will be returned\n ordered by reference sequence, which will not necessarily be the order within the file.\n If *until_eof* is given, all reads from the current file position will be returned\n *in order as they are within the file*. \n \n If only *reference* is set, all reads aligned to *reference* will be fetched.\n\n The method returns an iterator of type :class:`pysam.IteratorRow` unless\n a *callback is provided. If *callback* is given, the callback will be executed \n for each position within the :term:`region`. Note that callbacks currently work\n only, if *region* or *reference* is given.\n\n Note that a :term:`SAM` file does not allow random access. If *region* or *reference* are given,\n an exception is raised.\n "; +static PyObject *__pyx_pf_9csamtools_7Samfile_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_callback = 0; + PyObject *__pyx_v_until_eof = 0; + int __pyx_v_rtid; + int __pyx_v_rstart; + int __pyx_v_rend; + int __pyx_v_has_coord; + PyObject *__pyx_r = NULL; + 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; + int __pyx_t_8; + int __pyx_t_9; + int __pyx_t_10; + int __pyx_t_11; + PyObject *__pyx_t_12 = NULL; + int __pyx_t_13; + int __pyx_t_14; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__reference,&__pyx_n_s__start,&__pyx_n_s__end,&__pyx_n_s__region,&__pyx_n_s__callback,&__pyx_n_s__until_eof,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("fetch"); + __Pyx_TraceCall("fetch", __pyx_f[0], 666); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[6] = {0,0,0,0,0,0}; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":667 + * + * def fetch( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + values[0] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":668 + * def fetch( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None, + */ + values[1] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":669 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None, + * callback = None, + */ + values[2] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":670 + * start = None, + * end = None, + * region = None, # <<<<<<<<<<<<<< + * callback = None, + * until_eof = False ): + */ + values[3] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":671 + * end = None, + * region = None, + * callback = None, # <<<<<<<<<<<<<< + * until_eof = False ): + * ''' + */ + values[4] = ((PyObject *)Py_None); + values[5] = __pyx_k_41; + 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); + 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; + } + switch (PyTuple_GET_SIZE(__pyx_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__callback); + if (value) { values[4] = value; kw_args--; } + } + case 5: + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__until_eof); + if (value) { values[5] = value; kw_args--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_reference = values[0]; + __pyx_v_start = values[1]; + __pyx_v_end = values[2]; + __pyx_v_region = values[3]; + __pyx_v_callback = values[4]; + __pyx_v_until_eof = values[5]; + } else { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":667 + * + * def fetch( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + __pyx_v_reference = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":668 + * def fetch( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None, + */ + __pyx_v_start = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":669 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None, + * callback = None, + */ + __pyx_v_end = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":670 + * start = None, + * end = None, + * region = None, # <<<<<<<<<<<<<< + * callback = None, + * until_eof = False ): + */ + __pyx_v_region = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":671 + * end = None, + * region = None, + * callback = None, # <<<<<<<<<<<<<< + * until_eof = False ): + * ''' + */ + __pyx_v_callback = ((PyObject *)Py_None); + __pyx_v_until_eof = __pyx_k_41; + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 6: __pyx_v_until_eof = PyTuple_GET_ITEM(__pyx_args, 5); + case 5: __pyx_v_callback = PyTuple_GET_ITEM(__pyx_args, 4); + case 4: __pyx_v_region = PyTuple_GET_ITEM(__pyx_args, 3); + case 3: __pyx_v_end = PyTuple_GET_ITEM(__pyx_args, 2); + case 2: __pyx_v_start = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_reference = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("fetch", 0, 0, 6, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Samfile.fetch"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":695 + * cdef int rtid, rstart, rend, has_coord + * + * if not self._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); 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_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 = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":696 + * + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * has_coord, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_42), NULL); 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_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":698 + * raise ValueError( "I/O operation on closed file" ) + * + * has_coord, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) # <<<<<<<<<<<<<< + * + * if self.isbam: + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___parseRegion); 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_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_t_5 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + if (PyTuple_CheckExact(__pyx_t_5) && likely(PyTuple_GET_SIZE(__pyx_t_5) == 4)) { + PyObject* tuple = __pyx_t_5; + __pyx_t_1 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_1); + __pyx_t_8 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_2 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_2); + __pyx_t_9 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = PyTuple_GET_ITEM(tuple, 2); __Pyx_INCREF(__pyx_t_6); + __pyx_t_10 = __Pyx_PyInt_AsInt(__pyx_t_6); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_7 = PyTuple_GET_ITEM(tuple, 3); __Pyx_INCREF(__pyx_t_7); + __pyx_t_11 = __Pyx_PyInt_AsInt(__pyx_t_7); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_has_coord = __pyx_t_8; + __pyx_v_rtid = __pyx_t_9; + __pyx_v_rstart = __pyx_t_10; + __pyx_v_rend = __pyx_t_11; + } else { + __pyx_t_12 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_12); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_1 = __Pyx_UnpackItem(__pyx_t_12, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_11 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_2 = __Pyx_UnpackItem(__pyx_t_12, 1); 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_10 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __Pyx_UnpackItem(__pyx_t_12, 2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_9 = __Pyx_PyInt_AsInt(__pyx_t_6); if (unlikely((__pyx_t_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_7 = __Pyx_UnpackItem(__pyx_t_12, 3); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_8 = __Pyx_PyInt_AsInt(__pyx_t_7); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + if (__Pyx_EndUnpack(__pyx_t_12, 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0; + __pyx_v_has_coord = __pyx_t_11; + __pyx_v_rtid = __pyx_t_10; + __pyx_v_rstart = __pyx_t_9; + __pyx_v_rend = __pyx_t_8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":700 + * has_coord, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + * + * if self.isbam: # <<<<<<<<<<<<<< + * if not until_eof and not self._hasIndex() and not self.isremote: + * raise ValueError( "fetch called on bamfile without index" ) + */ + if (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isbam) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":701 + * + * 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 = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = (!__pyx_t_4); + if (__pyx_t_3) { + __pyx_t_5 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___hasIndex); 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_t_7 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __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 = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __pyx_t_13 = (!__pyx_t_4); + if (__pyx_t_13) { + __pyx_t_4 = (!((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isremote); + __pyx_t_14 = __pyx_t_4; + } else { + __pyx_t_14 = __pyx_t_13; + } + __pyx_t_13 = __pyx_t_14; + } else { + __pyx_t_13 = __pyx_t_3; + } + if (__pyx_t_13) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":702 + * if self.isbam: + * if not until_eof and not self._hasIndex() and not self.isremote: + * raise ValueError( "fetch called on bamfile without index" ) # <<<<<<<<<<<<<< + * + * if callback: + */ + __pyx_t_7 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_44), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":704 + * raise ValueError( "fetch called on bamfile without index" ) + * + * if callback: # <<<<<<<<<<<<<< + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) + */ + __pyx_t_13 = __Pyx_PyObject_IsTrue(__pyx_v_callback); if (unlikely(__pyx_t_13 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 704; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_13) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":705 + * + * if callback: + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) # <<<<<<<<<<<<<< + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) + * return bam_fetch(self.samfile.x.bam, + */ + __pyx_t_13 = (!__pyx_v_has_coord); + if (__pyx_t_13) { + __pyx_t_7 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_46), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L10; + } + __pyx_L10:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":706 + * if callback: + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) # <<<<<<<<<<<<<< + * return bam_fetch(self.samfile.x.bam, + * self.index, + */ + __pyx_t_7 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___hasIndex); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_5 = PyObject_Call(__pyx_t_7, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __pyx_t_13 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_13 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_3 = (!__pyx_t_13); + if (__pyx_t_3) { + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_48), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L11; + } + __pyx_L11:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":707 + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) + * return bam_fetch(self.samfile.x.bam, # <<<<<<<<<<<<<< + * self.index, + * rtid, + */ + __Pyx_XDECREF(__pyx_r); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":713 + * rend, + * callback, + * fetch_callback ) # <<<<<<<<<<<<<< + * else: + * if has_coord: + */ + __pyx_t_5 = PyInt_FromLong(bam_fetch(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->x.bam, ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index, __pyx_v_rtid, __pyx_v_rstart, __pyx_v_rend, ((void *)__pyx_v_callback), __pyx_f_9csamtools_fetch_callback)); 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_r = __pyx_t_5; + __pyx_t_5 = 0; + goto __pyx_L0; + goto __pyx_L9; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":715 + * fetch_callback ) + * else: + * if has_coord: # <<<<<<<<<<<<<< + * return IteratorRowRegion( self, rtid, rstart, rend ) + * else: + */ + if (__pyx_v_has_coord) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":716 + * else: + * if has_coord: + * return IteratorRowRegion( self, rtid, rstart, rend ) # <<<<<<<<<<<<<< + * else: + * if until_eof: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_5 = PyInt_FromLong(__pyx_v_rtid); 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_t_7 = PyInt_FromLong(__pyx_v_rstart); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_6 = PyInt_FromLong(__pyx_v_rend); 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_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_v_self); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_self); + __Pyx_GIVEREF(__pyx_v_self); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + PyTuple_SET_ITEM(__pyx_t_2, 3, __pyx_t_6); + __Pyx_GIVEREF(__pyx_t_6); + __pyx_t_5 = 0; + __pyx_t_7 = 0; + __pyx_t_6 = 0; + __pyx_t_6 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorRowRegion)), ((PyObject *)__pyx_t_2), 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_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __pyx_r = __pyx_t_6; + __pyx_t_6 = 0; + goto __pyx_L0; + goto __pyx_L12; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":718 + * return IteratorRowRegion( self, rtid, rstart, rend ) + * else: + * if until_eof: # <<<<<<<<<<<<<< + * return IteratorRowAll( self ) + * else: + */ + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_until_eof); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 718; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":719 + * else: + * if until_eof: + * return IteratorRowAll( self ) # <<<<<<<<<<<<<< + * else: + * return IteratorRowAllRefs(self) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_INCREF(__pyx_v_self); + PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_self); + __Pyx_GIVEREF(__pyx_v_self); + __pyx_t_2 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorRowAll)), ((PyObject *)__pyx_t_6), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + __pyx_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + goto __pyx_L13; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":721 + * return IteratorRowAll( self ) + * else: + * return IteratorRowAllRefs(self) # <<<<<<<<<<<<<< + * else: + * # check if header is present - otherwise sam_read1 aborts + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_v_self); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_self); + __Pyx_GIVEREF(__pyx_v_self); + __pyx_t_6 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorRowAllRefs)), ((PyObject *)__pyx_t_2), NULL); 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(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __pyx_r = __pyx_t_6; + __pyx_t_6 = 0; + goto __pyx_L0; + } + __pyx_L13:; + } + __pyx_L12:; + } + __pyx_L9:; + goto __pyx_L7; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":725 + * # check if header is present - otherwise sam_read1 aborts + * # this happens if a bamfile is opened with mode 'r' + * if self.samfile.header.n_targets == 0: # <<<<<<<<<<<<<< + * raise ValueError( "fetch called for samfile without header") + * + */ + __pyx_t_3 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->n_targets == 0); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":726 + * # this happens if a bamfile is opened with mode 'r' + * if self.samfile.header.n_targets == 0: + * raise ValueError( "fetch called for samfile without header") # <<<<<<<<<<<<<< + * + * if region != None: + */ + __pyx_t_6 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_50), NULL); 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_Raise(__pyx_t_6, 0, 0); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L14; + } + __pyx_L14:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":728 + * raise ValueError( "fetch called for samfile without header") + * + * if region != None: # <<<<<<<<<<<<<< + * raise ValueError ("fetch for a region is not available for sam files" ) + * if callback: + */ + __pyx_t_6 = PyObject_RichCompare(__pyx_v_region, Py_None, Py_NE); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":729 + * + * if region != None: + * raise ValueError ("fetch for a region is not available for sam files" ) # <<<<<<<<<<<<<< + * if callback: + * raise NotImplementedError( "callback not implemented yet" ) + */ + __pyx_t_6 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_52), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_Raise(__pyx_t_6, 0, 0); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L15; + } + __pyx_L15:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":730 + * if region != None: + * raise ValueError ("fetch for a region is not available for sam files" ) + * if callback: # <<<<<<<<<<<<<< + * raise NotImplementedError( "callback not implemented yet" ) + * else: + */ + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_callback); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":731 + * raise ValueError ("fetch for a region is not available for sam files" ) + * if callback: + * raise NotImplementedError( "callback not implemented yet" ) # <<<<<<<<<<<<<< + * else: + * return IteratorRowAll( self ) + */ + __pyx_t_6 = PyObject_Call(__pyx_builtin_NotImplementedError, ((PyObject *)__pyx_k_tuple_54), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 731; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_Raise(__pyx_t_6, 0, 0); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 731; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L16; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":733 + * raise NotImplementedError( "callback not implemented yet" ) + * else: + * return IteratorRowAll( self ) # <<<<<<<<<<<<<< + * + * def mate( self, + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_INCREF(__pyx_v_self); + PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_self); + __Pyx_GIVEREF(__pyx_v_self); + __pyx_t_2 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorRowAll)), ((PyObject *)__pyx_t_6), NULL); 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_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + __pyx_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + } + __pyx_L16:; + } + __pyx_L7:; + + __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_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_12); + __Pyx_AddTraceback("csamtools.Samfile.fetch"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":735 + * return IteratorRowAll( self ) + * + * def mate( self, # <<<<<<<<<<<<<< + * AlignedRead read ): + * '''return the mate of :class:`AlignedRead` *read*. + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_10mate(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_10mate[] = "return 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 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 "; +static PyObject *__pyx_pf_9csamtools_7Samfile_10mate(PyObject *__pyx_v_self, PyObject *__pyx_v_read) { + __pyx_t_9csamtools_MateData __pyx_v_mate_data; + int __pyx_v_x; + struct __pyx_obj_9csamtools_AlignedRead *__pyx_v_dest = 0; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + int __pyx_t_3; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("mate"); + __Pyx_TraceCall("mate", __pyx_f[0], 735); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_read), __pyx_ptype_9csamtools_AlignedRead, 1, "read", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":748 + * + * ''' + * if not read.is_paired: # <<<<<<<<<<<<<< + * raise ValueError( "read is unpaired" ) + * if read.mate_is_unmapped: + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_read, __pyx_n_s__is_paired); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __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 = 748; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_3 = (!__pyx_t_2); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":749 + * ''' + * if not read.is_paired: + * raise ValueError( "read is unpaired" ) # <<<<<<<<<<<<<< + * if read.mate_is_unmapped: + * raise ValueError( "mate is unmapped" ) + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_56), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 749; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 749; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":750 + * if not read.is_paired: + * raise ValueError( "read is unpaired" ) + * if read.mate_is_unmapped: # <<<<<<<<<<<<<< + * raise ValueError( "mate is unmapped" ) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_read, __pyx_n_s__mate_is_unmapped); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __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 = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":751 + * raise ValueError( "read is unpaired" ) + * if read.mate_is_unmapped: + * raise ValueError( "mate is unmapped" ) # <<<<<<<<<<<<<< + * + * cdef MateData mate_data + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_58), NULL); 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_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 751; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":755 + * cdef MateData mate_data + * + * mate_data.name = bam1_qname(read._delegate) # <<<<<<<<<<<<<< + * mate_data.mate = NULL + * # xor flags to get the other mate + */ + __pyx_v_mate_data.name = bam1_qname(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_read)->_delegate); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":756 + * + * mate_data.name = bam1_qname(read._delegate) + * mate_data.mate = NULL # <<<<<<<<<<<<<< + * # xor flags to get the other mate + * cdef int x = BAM_FREAD1 + BAM_FREAD2 + */ + __pyx_v_mate_data.mate = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":758 + * mate_data.mate = NULL + * # xor flags to get the other mate + * cdef int x = BAM_FREAD1 + BAM_FREAD2 # <<<<<<<<<<<<<< + * mate_data.flag = ( read._delegate.core.flag ^ x) & x + * + */ + __pyx_v_x = 192; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":759 + * # xor flags to get the other mate + * cdef int x = BAM_FREAD1 + BAM_FREAD2 + * mate_data.flag = ( read._delegate.core.flag ^ x) & x # <<<<<<<<<<<<<< + * + * bam_fetch(self.samfile.x.bam, + */ + __pyx_v_mate_data.flag = ((((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_read)->_delegate->core.flag ^ __pyx_v_x) & __pyx_v_x); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":767 + * read._delegate.core.mpos + 1, + * &mate_data, + * mate_callback ) # <<<<<<<<<<<<<< + * + * if mate_data.mate == NULL: + */ + bam_fetch(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->x.bam, ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index, ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_read)->_delegate->core.mtid, ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_read)->_delegate->core.mpos, (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_read)->_delegate->core.mpos + 1), ((void *)(&__pyx_v_mate_data)), __pyx_f_9csamtools_mate_callback); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":769 + * mate_callback ) + * + * if mate_data.mate == NULL: # <<<<<<<<<<<<<< + * raise ValueError( "mate not found" ) + * + */ + __pyx_t_3 = (__pyx_v_mate_data.mate == NULL); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":770 + * + * if mate_data.mate == NULL: + * raise ValueError( "mate not found" ) # <<<<<<<<<<<<<< + * + * cdef AlignedRead dest = AlignedRead.__new__(AlignedRead) + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_60), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":772 + * raise ValueError( "mate not found" ) + * + * cdef AlignedRead dest = AlignedRead.__new__(AlignedRead) # <<<<<<<<<<<<<< + * dest._delegate = mate_data.mate + * return dest + */ + __pyx_t_1 = __Pyx_tp_new(((PyObject*)__pyx_ptype_9csamtools_AlignedRead)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_9csamtools_AlignedRead)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_dest = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":773 + * + * cdef AlignedRead dest = AlignedRead.__new__(AlignedRead) + * dest._delegate = mate_data.mate # <<<<<<<<<<<<<< + * return dest + * + */ + __pyx_v_dest->_delegate = __pyx_v_mate_data.mate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":774 + * cdef AlignedRead dest = AlignedRead.__new__(AlignedRead) + * dest._delegate = mate_data.mate + * return dest # <<<<<<<<<<<<<< + * + * def count( self, + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_dest)); + __pyx_r = ((PyObject *)__pyx_v_dest); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.Samfile.mate"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_dest); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":776 + * return dest + * + * def count( self, # <<<<<<<<<<<<<< + * reference = None, + * start = None, + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_11count(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_11count[] = "*(reference = None, start = None, end = None, region = None, callback = None, until_eof = False)*\n \n count reads :term:`region` using 0-based indexing. The region is specified by\n :term:`reference`, *start* and *end*. Alternatively, a samtools :term:`region` string can be supplied.\n\n Note that a :term:`TAM` file does not allow random access. If *region* or *reference* are given,\n an exception is raised.\n "; +static PyObject *__pyx_pf_9csamtools_7Samfile_11count(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_v_rtid; + int __pyx_v_rstart; + int __pyx_v_rend; + int __pyx_v_counter; + PyObject *__pyx_r = NULL; + 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; + int __pyx_t_8; + int __pyx_t_9; + int __pyx_t_10; + PyObject *__pyx_t_11 = NULL; + int __pyx_t_12; + int __pyx_t_13; + 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}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("count"); + __Pyx_TraceCall("count", __pyx_f[0], 776); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[5] = {0,0,0,0,0}; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":777 + * + * def count( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + values[0] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":778 + * def count( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None, + */ + values[1] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":779 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None, + * until_eof = False ): + */ + values[2] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":780 + * start = None, + * end = None, + * region = None, # <<<<<<<<<<<<<< + * until_eof = False ): + * '''*(reference = None, start = None, end = None, region = None, callback = None, until_eof = False)* + */ + values[3] = ((PyObject *)Py_None); + values[4] = __pyx_k_61; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_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, PyTuple_GET_SIZE(__pyx_args), "count") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L3_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]; + } else { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":777 + * + * def count( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + __pyx_v_reference = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":778 + * def count( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None, + */ + __pyx_v_start = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":779 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None, + * until_eof = False ): + */ + __pyx_v_end = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":780 + * start = None, + * end = None, + * region = None, # <<<<<<<<<<<<<< + * until_eof = False ): + * '''*(reference = None, start = None, end = None, region = None, callback = None, until_eof = False)* + */ + __pyx_v_region = ((PyObject *)Py_None); + __pyx_v_until_eof = __pyx_k_61; + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 5: __pyx_v_until_eof = PyTuple_GET_ITEM(__pyx_args, 4); + case 4: __pyx_v_region = PyTuple_GET_ITEM(__pyx_args, 3); + case 3: __pyx_v_end = PyTuple_GET_ITEM(__pyx_args, 2); + case 2: __pyx_v_start = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_reference = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + } + 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 = 776; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Samfile.count"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __Pyx_INCREF(__pyx_v_region); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":794 + * cdef int rend + * + * if not self._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); 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_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); 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_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 = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":795 + * + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * region, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_62), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":797 + * raise ValueError( "I/O operation on closed file" ) + * + * region, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) # <<<<<<<<<<<<<< + * + * cdef int counter + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___parseRegion); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_t_5 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + if (PyTuple_CheckExact(__pyx_t_5) && likely(PyTuple_GET_SIZE(__pyx_t_5) == 4)) { + PyObject* tuple = __pyx_t_5; + __pyx_t_1 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_2); + __pyx_t_8 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = PyTuple_GET_ITEM(tuple, 2); __Pyx_INCREF(__pyx_t_6); + __pyx_t_9 = __Pyx_PyInt_AsInt(__pyx_t_6); if (unlikely((__pyx_t_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_7 = PyTuple_GET_ITEM(tuple, 3); __Pyx_INCREF(__pyx_t_7); + __pyx_t_10 = __Pyx_PyInt_AsInt(__pyx_t_7); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_v_region); + __pyx_v_region = __pyx_t_1; + __pyx_t_1 = 0; + __pyx_v_rtid = __pyx_t_8; + __pyx_v_rstart = __pyx_t_9; + __pyx_v_rend = __pyx_t_10; + } else { + __pyx_t_11 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_11); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_1 = __Pyx_UnpackItem(__pyx_t_11, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = __Pyx_UnpackItem(__pyx_t_11, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_10 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __Pyx_UnpackItem(__pyx_t_11, 2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_9 = __Pyx_PyInt_AsInt(__pyx_t_6); if (unlikely((__pyx_t_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_7 = __Pyx_UnpackItem(__pyx_t_11, 3); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_8 = __Pyx_PyInt_AsInt(__pyx_t_7); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + if (__Pyx_EndUnpack(__pyx_t_11, 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; + __Pyx_DECREF(__pyx_v_region); + __pyx_v_region = __pyx_t_1; + __pyx_t_1 = 0; + __pyx_v_rtid = __pyx_t_10; + __pyx_v_rstart = __pyx_t_9; + __pyx_v_rend = __pyx_t_8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":800 + * + * cdef int counter + * counter = 0; # <<<<<<<<<<<<<< + * + * if self.isbam: + */ + __pyx_v_counter = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":802 + * counter = 0; + * + * if self.isbam: # <<<<<<<<<<<<<< + * if not until_eof and not self._hasIndex() and not self.isremote: + * raise ValueError( "fetch called on bamfile without index" ) + */ + if (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isbam) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":803 + * + * 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 = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = (!__pyx_t_4); + if (__pyx_t_3) { + __pyx_t_5 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___hasIndex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_7 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 803; __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 = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __pyx_t_12 = (!__pyx_t_4); + if (__pyx_t_12) { + __pyx_t_4 = (!((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isremote); + __pyx_t_13 = __pyx_t_4; + } else { + __pyx_t_13 = __pyx_t_12; + } + __pyx_t_12 = __pyx_t_13; + } else { + __pyx_t_12 = __pyx_t_3; + } + if (__pyx_t_12) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":804 + * if self.isbam: + * if not until_eof and not self._hasIndex() and not self.isremote: + * raise ValueError( "fetch called on bamfile without index" ) # <<<<<<<<<<<<<< + * + * if not region: + */ + __pyx_t_7 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_63), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":806 + * raise ValueError( "fetch called on bamfile without index" ) + * + * if not region: # <<<<<<<<<<<<<< + * raise ValueError( "counting functionality requires a region/reference" ) + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) + */ + __pyx_t_12 = __Pyx_PyObject_IsTrue(__pyx_v_region); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = (!__pyx_t_12); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":807 + * + * if not region: + * raise ValueError( "counting functionality requires a region/reference" ) # <<<<<<<<<<<<<< + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) + * bam_fetch(self.samfile.x.bam, + */ + __pyx_t_7 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_65), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":808 + * if not region: + * raise ValueError( "counting functionality requires a region/reference" ) + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) # <<<<<<<<<<<<<< + * bam_fetch(self.samfile.x.bam, + * self.index, + */ + __pyx_t_7 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___hasIndex); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 808; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_5 = PyObject_Call(__pyx_t_7, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 808; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 808; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_12 = (!__pyx_t_3); + if (__pyx_t_12) { + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_66), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 808; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 808; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L10; + } + __pyx_L10:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":815 + * rend, + * &counter, + * count_callback ) # <<<<<<<<<<<<<< + * return counter + * else: + */ + bam_fetch(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->x.bam, ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index, __pyx_v_rtid, __pyx_v_rstart, __pyx_v_rend, ((void *)(&__pyx_v_counter)), __pyx_f_9csamtools_count_callback); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":816 + * &counter, + * count_callback ) + * return counter # <<<<<<<<<<<<<< + * else: + * raise ValueError ("count for a region is not available for sam files" ) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_5 = PyInt_FromLong(__pyx_v_counter); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_r = __pyx_t_5; + __pyx_t_5 = 0; + goto __pyx_L0; + goto __pyx_L7; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":818 + * return counter + * else: + * raise ValueError ("count for a region is not available for sam files" ) # <<<<<<<<<<<<<< + * + * + */ + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_68), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_L7:; + + __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_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_11); + __Pyx_AddTraceback("csamtools.Samfile.count"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_region); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":821 + * + * + * def pileup( self, # <<<<<<<<<<<<<< + * reference = None, + * start = None, + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_12pileup(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_12pileup[] = "\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 ordered by :term:`reference` sequence, which will not necessarily be the order within the file.\n\n The method returns an iterator of type :class:`pysam.IteratorColumn` unless\n a *callback is provided. If a *callback* is given, the callback will be executed \n for each column within the :term:`region`. \n\n Note that :term:`SAM` formatted files do not allow random access. \n In these files, if a *region* or *reference* are given an exception is raised.\n \n Optional *kwargs* to the iterator:\n\n stepper\n The stepper controlls how the iterator advances. \n Possible options for the stepper are \n \n ``all``\n use all reads for pileup.\n ``samtools``\n same filter and read processing as in :term:`csamtools` pileup\n\n fastafile\n A :class:`FastaFile` object\n\n mask\n Skip all reads with bits set in mask.\n\n\n .. note::\n\n *all* reads which overlap the region are returned. The first base returned will be the \n first base of the first read *not* necessarily the first base of the region used in the query.\n\n "; +static PyObject *__pyx_pf_9csamtools_7Samfile_12pileup(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_callback = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_v_rtid; + int __pyx_v_rstart; + int __pyx_v_rend; + int __pyx_v_has_coord; + bam_plbuf_t *__pyx_v_buf; + PyObject *__pyx_r = NULL; + 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; + int __pyx_t_8; + int __pyx_t_9; + int __pyx_t_10; + int __pyx_t_11; + PyObject *__pyx_t_12 = NULL; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__reference,&__pyx_n_s__start,&__pyx_n_s__end,&__pyx_n_s__region,&__pyx_n_s__callback,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("pileup"); + __Pyx_TraceCall("pileup", __pyx_f[0], 821); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return NULL; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[5] = {0,0,0,0,0}; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":822 + * + * def pileup( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + values[0] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":823 + * def pileup( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None, + */ + values[1] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":824 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None, + * callback = None, + */ + values[2] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":825 + * start = None, + * end = None, + * region = None, # <<<<<<<<<<<<<< + * callback = None, + * **kwargs ): + */ + values[3] = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":826 + * end = None, + * region = None, + * callback = None, # <<<<<<<<<<<<<< + * **kwargs ): + * ''' + */ + values[4] = ((PyObject *)Py_None); + 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; + } + switch (PyTuple_GET_SIZE(__pyx_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__callback); + if (value) { values[4] = value; kw_args--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, PyTuple_GET_SIZE(__pyx_args), "pileup") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_reference = values[0]; + __pyx_v_start = values[1]; + __pyx_v_end = values[2]; + __pyx_v_region = values[3]; + __pyx_v_callback = values[4]; + } else { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":822 + * + * def pileup( self, + * reference = None, # <<<<<<<<<<<<<< + * start = None, + * end = None, + */ + __pyx_v_reference = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":823 + * def pileup( self, + * reference = None, + * start = None, # <<<<<<<<<<<<<< + * end = None, + * region = None, + */ + __pyx_v_start = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":824 + * reference = None, + * start = None, + * end = None, # <<<<<<<<<<<<<< + * region = None, + * callback = None, + */ + __pyx_v_end = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":825 + * start = None, + * end = None, + * region = None, # <<<<<<<<<<<<<< + * callback = None, + * **kwargs ): + */ + __pyx_v_region = ((PyObject *)Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":826 + * end = None, + * region = None, + * callback = None, # <<<<<<<<<<<<<< + * **kwargs ): + * ''' + */ + __pyx_v_callback = ((PyObject *)Py_None); + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 5: __pyx_v_callback = PyTuple_GET_ITEM(__pyx_args, 4); + case 4: __pyx_v_region = PyTuple_GET_ITEM(__pyx_args, 3); + case 3: __pyx_v_end = PyTuple_GET_ITEM(__pyx_args, 2); + case 2: __pyx_v_start = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_reference = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("pileup", 0, 0, 5, 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_AddTraceback("csamtools.Samfile.pileup"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":870 + * cdef bam_plbuf_t *buf + * + * if not self._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); 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_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); 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); + __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 = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":871 + * + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * has_coord, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_69), 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_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":873 + * raise ValueError( "I/O operation on closed file" ) + * + * has_coord, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) # <<<<<<<<<<<<<< + * + * if self.isbam: + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___parseRegion); 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_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_t_5 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + if (PyTuple_CheckExact(__pyx_t_5) && likely(PyTuple_GET_SIZE(__pyx_t_5) == 4)) { + PyObject* tuple = __pyx_t_5; + __pyx_t_1 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_1); + __pyx_t_8 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_2 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_2); + __pyx_t_9 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = PyTuple_GET_ITEM(tuple, 2); __Pyx_INCREF(__pyx_t_6); + __pyx_t_10 = __Pyx_PyInt_AsInt(__pyx_t_6); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_7 = PyTuple_GET_ITEM(tuple, 3); __Pyx_INCREF(__pyx_t_7); + __pyx_t_11 = __Pyx_PyInt_AsInt(__pyx_t_7); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_has_coord = __pyx_t_8; + __pyx_v_rtid = __pyx_t_9; + __pyx_v_rstart = __pyx_t_10; + __pyx_v_rend = __pyx_t_11; + } else { + __pyx_t_12 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_12); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_1 = __Pyx_UnpackItem(__pyx_t_12, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_11 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_2 = __Pyx_UnpackItem(__pyx_t_12, 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_t_10 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __Pyx_UnpackItem(__pyx_t_12, 2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_9 = __Pyx_PyInt_AsInt(__pyx_t_6); if (unlikely((__pyx_t_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_7 = __Pyx_UnpackItem(__pyx_t_12, 3); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_8 = __Pyx_PyInt_AsInt(__pyx_t_7); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + if (__Pyx_EndUnpack(__pyx_t_12, 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0; + __pyx_v_has_coord = __pyx_t_11; + __pyx_v_rtid = __pyx_t_10; + __pyx_v_rstart = __pyx_t_9; + __pyx_v_rend = __pyx_t_8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":875 + * 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 (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->isbam) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":876 + * + * if self.isbam: + * if not self._hasIndex(): raise ValueError( "no index available for pileup" ) # <<<<<<<<<<<<<< + * + * if callback: + */ + __pyx_t_5 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___hasIndex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 876; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_7 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 876; __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 = 876; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __pyx_t_3 = (!__pyx_t_4); + if (__pyx_t_3) { + __pyx_t_7 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_71), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 876; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 876; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":878 + * if not self._hasIndex(): raise ValueError( "no index available for pileup" ) + * + * if callback: # <<<<<<<<<<<<<< + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) + * + */ + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_callback); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":879 + * + * if callback: + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) # <<<<<<<<<<<<<< + * + * buf = bam_plbuf_init( pileup_callback, callback ) + */ + __pyx_t_3 = (!__pyx_v_has_coord); + if (__pyx_t_3) { + __pyx_t_7 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_72), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L10; + } + __pyx_L10:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":881 + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) + * + * buf = bam_plbuf_init( pileup_callback, callback ) # <<<<<<<<<<<<<< + * bam_fetch(self.samfile.x.bam, + * self.index, rtid, rstart, rend, + */ + __pyx_v_buf = bam_plbuf_init(((bam_pileup_f)__pyx_f_9csamtools_pileup_callback), ((void *)__pyx_v_callback)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":884 + * bam_fetch(self.samfile.x.bam, + * self.index, rtid, rstart, rend, + * buf, pileup_fetch_callback ) # <<<<<<<<<<<<<< + * + * # finalize pileup + */ + bam_fetch(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->x.bam, ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index, __pyx_v_rtid, __pyx_v_rstart, __pyx_v_rend, __pyx_v_buf, __pyx_f_9csamtools_pileup_fetch_callback); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":887 + * + * # finalize pileup + * bam_plbuf_push( NULL, buf) # <<<<<<<<<<<<<< + * bam_plbuf_destroy(buf) + * else: + */ + bam_plbuf_push(NULL, __pyx_v_buf); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":888 + * # finalize pileup + * bam_plbuf_push( NULL, buf) + * bam_plbuf_destroy(buf) # <<<<<<<<<<<<<< + * else: + * if has_coord: + */ + bam_plbuf_destroy(__pyx_v_buf); + goto __pyx_L9; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":890 + * bam_plbuf_destroy(buf) + * else: + * if has_coord: # <<<<<<<<<<<<<< + * return IteratorColumnRegion( self, + * tid = rtid, + */ + if (__pyx_v_has_coord) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":891 + * else: + * 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 = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_7)); + __Pyx_INCREF(__pyx_v_self); + PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_self); + __Pyx_GIVEREF(__pyx_v_self); + __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":892 + * if has_coord: + * return IteratorColumnRegion( self, + * tid = rtid, # <<<<<<<<<<<<<< + * start = rstart, + * end = rend, + */ + __pyx_t_6 = PyInt_FromLong(__pyx_v_rtid); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__tid), __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":893 + * return IteratorColumnRegion( self, + * tid = rtid, + * start = rstart, # <<<<<<<<<<<<<< + * end = rend, + * **kwargs ) + */ + __pyx_t_6 = PyInt_FromLong(__pyx_v_rstart); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 893; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__start), __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":894 + * tid = rtid, + * start = rstart, + * end = rend, # <<<<<<<<<<<<<< + * **kwargs ) + * else: + */ + __pyx_t_6 = PyInt_FromLong(__pyx_v_rend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__end), __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":895 + * start = rstart, + * end = rend, + * **kwargs ) # <<<<<<<<<<<<<< + * else: + * return IteratorColumnAllRefs(self, **kwargs ) + */ + if (PyDict_Update(((PyObject *)__pyx_t_5), ((PyObject *)__pyx_v_kwargs)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_6 = PyEval_CallObjectWithKeywords(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorColumnRegion)), ((PyObject *)__pyx_t_7), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(((PyObject *)__pyx_t_7)); __pyx_t_7 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __pyx_r = __pyx_t_6; + __pyx_t_6 = 0; + goto __pyx_L0; + goto __pyx_L11; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":897 + * **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 = 897; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_INCREF(__pyx_v_self); + PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_self); + __Pyx_GIVEREF(__pyx_v_self); + __pyx_t_5 = PyEval_CallObjectWithKeywords(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorColumnAllRefs)), ((PyObject *)__pyx_t_6), ((PyObject *)__pyx_v_kwargs)); 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_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + __pyx_r = __pyx_t_5; + __pyx_t_5 = 0; + goto __pyx_L0; + } + __pyx_L11:; + } + __pyx_L9:; + goto __pyx_L7; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":900 + * + * else: + * raise NotImplementedError( "pileup of samfiles not implemented yet" ) # <<<<<<<<<<<<<< + * + * def close( self ): + */ + __pyx_t_5 = PyObject_Call(__pyx_builtin_NotImplementedError, ((PyObject *)__pyx_k_tuple_74), NULL); 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_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_L7:; + + __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_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_12); + __Pyx_AddTraceback("csamtools.Samfile.pileup"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":902 + * raise NotImplementedError( "pileup of samfiles not implemented yet" ) + * + * def close( self ): # <<<<<<<<<<<<<< + * ''' + * closes the :class:`pysam.Samfile`.''' + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_13close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_13close[] = "\n closes the :class:`pysam.Samfile`."; +static PyObject *__pyx_pf_9csamtools_7Samfile_13close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("close"); + __Pyx_TraceCall("close", __pyx_f[0], 902); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":905 + * ''' + * closes the :class:`pysam.Samfile`.''' + * if self.samfile != NULL: # <<<<<<<<<<<<<< + * samclose( self.samfile ) + * bam_index_destroy(self.index); + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile != NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":906 + * closes the :class:`pysam.Samfile`.''' + * if self.samfile != NULL: + * samclose( self.samfile ) # <<<<<<<<<<<<<< + * bam_index_destroy(self.index); + * self.samfile = NULL + */ + samclose(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":907 + * if self.samfile != NULL: + * samclose( self.samfile ) + * bam_index_destroy(self.index); # <<<<<<<<<<<<<< + * self.samfile = NULL + * + */ + bam_index_destroy(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->index); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":908 + * samclose( self.samfile ) + * bam_index_destroy(self.index); + * self.samfile = NULL # <<<<<<<<<<<<<< + * + * def __dealloc__( self ): + */ + ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile = NULL; + goto __pyx_L5; + } + __pyx_L5:; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":910 + * self.samfile = NULL + * + * def __dealloc__( self ): # <<<<<<<<<<<<<< + * # remember: dealloc cannot call other methods + * # note: no doc string + */ + +static void __pyx_pf_9csamtools_7Samfile_14__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_7Samfile_14__dealloc__(PyObject *__pyx_v_self) { + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 910); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":914 + * # note: no doc string + * # note: __del__ is not called. + * self.close() # <<<<<<<<<<<<<< + * bam_destroy1(self.b) + * if self._filename != NULL: free( self._filename ) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":915 + * # note: __del__ is not called. + * self.close() + * bam_destroy1(self.b) # <<<<<<<<<<<<<< + * if self._filename != NULL: free( self._filename ) + * + */ + bam_destroy1(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->b); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":916 + * self.close() + * bam_destroy1(self.b) + * if self._filename != NULL: free( self._filename ) # <<<<<<<<<<<<<< + * + * def write( self, AlignedRead read ): + */ + __pyx_t_3 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->_filename != NULL); + if (__pyx_t_3) { + free(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->_filename); + goto __pyx_L5; + } + __pyx_L5:; + + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.Samfile.__dealloc__"); + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":918 + * if self._filename != NULL: free( self._filename ) + * + * def write( self, AlignedRead read ): # <<<<<<<<<<<<<< + * ''' + * write a single :class:`pysam.AlignedRead` to disk. + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_15write(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_15write[] = "\n write a single :class:`pysam.AlignedRead` to disk.\n\n returns the number of bytes written.\n "; +static PyObject *__pyx_pf_9csamtools_7Samfile_15write(PyObject *__pyx_v_self, PyObject *__pyx_v_read) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("write"); + __Pyx_TraceCall("write", __pyx_f[0], 918); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_read), __pyx_ptype_9csamtools_AlignedRead, 1, "read", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":924 + * returns the number of bytes written. + * ''' + * if not self._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __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 = 924; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":925 + * ''' + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * return samwrite( self.samfile, read._delegate ) + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_75), 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_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 925; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":927 + * raise ValueError( "I/O operation on closed file" ) + * + * return samwrite( self.samfile, read._delegate ) # <<<<<<<<<<<<<< + * + * def __enter__(self): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyInt_FromLong(samwrite(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile, ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_read)->_delegate)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 927; __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_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("csamtools.Samfile.write"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":929 + * return samwrite( self.samfile, read._delegate ) + * + * def __enter__(self): # <<<<<<<<<<<<<< + * return self + * + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_16__enter__(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_16__enter__(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__enter__"); + __Pyx_TraceCall("__enter__", __pyx_f[0], 929); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":930 + * + * def __enter__(self): + * return self # <<<<<<<<<<<<<< + * + * def __exit__(self, exc_type, exc_value, traceback): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_self); + __pyx_r = __pyx_v_self; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":932 + * return self + * + * def __exit__(self, exc_type, exc_value, traceback): # <<<<<<<<<<<<<< + * self.close() + * return False + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_17__exit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_17__exit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_exc_type = 0; + PyObject *__pyx_v_exc_value = 0; + PyObject *__pyx_v_traceback = 0; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__exc_type,&__pyx_n_s__exc_value,&__pyx_n_s__traceback,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__exit__"); + __Pyx_TraceCall("__exit__", __pyx_f[0], 932); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[3] = {0,0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__exc_type); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__exc_value); + if (likely(values[1])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + case 2: + values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__traceback); + if (likely(values[2])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "__exit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_exc_type = values[0]; + __pyx_v_exc_value = values[1]; + __pyx_v_traceback = values[2]; + } else if (PyTuple_GET_SIZE(__pyx_args) != 3) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_exc_type = PyTuple_GET_ITEM(__pyx_args, 0); + __pyx_v_exc_value = PyTuple_GET_ITEM(__pyx_args, 1); + __pyx_v_traceback = PyTuple_GET_ITEM(__pyx_args, 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 = 932; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Samfile.__exit__"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":933 + * + * def __exit__(self, exc_type, exc_value, traceback): + * self.close() # <<<<<<<<<<<<<< + * return False + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__close); 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_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 933; __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":934 + * def __exit__(self, exc_type, exc_value, traceback): + * self.close() + * return False # <<<<<<<<<<<<<< + * + * ############################################################### + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = __Pyx_PyBool_FromLong(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 934; __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_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("csamtools.Samfile.__exit__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":943 + * property filename: + * '''number of :term:`filename` associated with this object.''' + * def __get__(self): # <<<<<<<<<<<<<< + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return self._filename + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_8filename_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_8filename_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 943); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":944 + * '''number of :term:`filename` associated with this object.''' + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * return self._filename + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __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 = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_76), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":945 + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return self._filename # <<<<<<<<<<<<<< + * + * property nreferences: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyBytes_FromString(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->_filename); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 945; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_r = ((PyObject *)__pyx_t_2); + __pyx_t_2 = 0; + goto __pyx_L0; + + __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("csamtools.Samfile.filename.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":949 + * 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.samfile.header.n_targets + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_11nreferences_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_11nreferences_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 949); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":950 + * '''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.samfile.header.n_targets + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); 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_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); 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_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 = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_77), NULL); 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_Raise(__pyx_t_2, 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;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":951 + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return self.samfile.header.n_targets # <<<<<<<<<<<<<< + * + * property references: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->n_targets); 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; + + __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("csamtools.Samfile.nreferences.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":955 + * 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 = [] + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_10references_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_10references_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_v_t; + long __pyx_v_x; + PyObject *__pyx_r = NULL; + 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; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 955); + __pyx_v_t = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":956 + * """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.samfile.header.n_targets: + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __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 = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_78), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":957 + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * t = [] # <<<<<<<<<<<<<< + * for x from 0 <= x < self.samfile.header.n_targets: + * t.append( self.samfile.header.target_name[x] ) + */ + __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 957; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__pyx_v_t)); + __pyx_v_t = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":958 + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * t = [] + * for x from 0 <= x < self.samfile.header.n_targets: # <<<<<<<<<<<<<< + * t.append( self.samfile.header.target_name[x] ) + * return tuple(t) + */ + __pyx_t_5 = ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->n_targets; + for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_5; __pyx_v_x++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":959 + * t = [] + * for x from 0 <= x < self.samfile.header.n_targets: + * t.append( self.samfile.header.target_name[x] ) # <<<<<<<<<<<<<< + * return tuple(t) + * + */ + if (unlikely(__pyx_v_t == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 959; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = PyBytes_FromString((((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->target_name[__pyx_v_x])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 959; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_t_6 = PyList_Append(__pyx_v_t, ((PyObject *)__pyx_t_2)); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 959; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":960 + * for x from 0 <= x < self.samfile.header.n_targets: + * t.append( self.samfile.header.target_name[x] ) + * return tuple(t) # <<<<<<<<<<<<<< + * + * property lengths: + */ + __Pyx_XDECREF(__pyx_r); + if (unlikely(__pyx_v_t == Py_None)) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = ((PyObject *)PyList_AsTuple(__pyx_v_t)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_r = ((PyObject *)__pyx_t_2); + __pyx_t_2 = 0; + goto __pyx_L0; + + __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("csamtools.Samfile.references.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_t); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":966 + * :attr:`pysam.Samfile.references` + * """ + * def __get__(self): # <<<<<<<<<<<<<< + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * t = [] + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_7lengths_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_7lengths_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_v_t; + long __pyx_v_x; + PyObject *__pyx_r = NULL; + 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; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 966); + __pyx_v_t = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":967 + * """ + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * t = [] + * for x from 0 <= x < self.samfile.header.n_targets: + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 967; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 967; __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 = 967; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_79), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 967; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 967; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":968 + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * t = [] # <<<<<<<<<<<<<< + * for x from 0 <= x < self.samfile.header.n_targets: + * t.append( self.samfile.header.target_len[x] ) + */ + __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__pyx_v_t)); + __pyx_v_t = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":969 + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * t = [] + * for x from 0 <= x < self.samfile.header.n_targets: # <<<<<<<<<<<<<< + * t.append( self.samfile.header.target_len[x] ) + * return tuple(t) + */ + __pyx_t_5 = ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->n_targets; + for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_5; __pyx_v_x++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":970 + * t = [] + * for x from 0 <= x < self.samfile.header.n_targets: + * t.append( self.samfile.header.target_len[x] ) # <<<<<<<<<<<<<< + * return tuple(t) + * + */ + if (unlikely(__pyx_v_t == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 970; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = __Pyx_PyInt_to_py_uint32_t((((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->target_len[__pyx_v_x])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 970; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_6 = PyList_Append(__pyx_v_t, __pyx_t_2); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 970; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":971 + * for x from 0 <= x < self.samfile.header.n_targets: + * t.append( self.samfile.header.target_len[x] ) + * return tuple(t) # <<<<<<<<<<<<<< + * + * property text: + */ + __Pyx_XDECREF(__pyx_r); + if (unlikely(__pyx_v_t == Py_None)) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = ((PyObject *)PyList_AsTuple(__pyx_v_t)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_r = ((PyObject *)__pyx_t_2); + __pyx_t_2 = 0; + goto __pyx_L0; + + __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("csamtools.Samfile.lengths.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_t); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":975 + * 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 PyString_FromStringAndSize(self.samfile.header.text, self.samfile.header.l_text) + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_4text_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_4text_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 975); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":976 + * '''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 PyString_FromStringAndSize(self.samfile.header.text, self.samfile.header.l_text) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __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 = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_80), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":977 + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return PyString_FromStringAndSize(self.samfile.header.text, self.samfile.header.l_text) # <<<<<<<<<<<<<< + * + * property header: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyString_FromStringAndSize(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->text, ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->l_text); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __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_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("csamtools.Samfile.text.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":983 + * a two-level dictionary. + * ''' + * def __get__(self): # <<<<<<<<<<<<<< + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_6header_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_6header_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_v_result; + PyObject *__pyx_v_t; + PyObject *__pyx_v_line; + PyObject *__pyx_v_fields; + PyObject *__pyx_v_record; + PyObject *__pyx_v_x; + PyObject *__pyx_v_field; + PyObject *__pyx_v_key; + PyObject *__pyx_v_value; + PyObject *__pyx_r = NULL; + 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 = NULL; + PyObject *__pyx_t_7 = NULL; + PyObject *__pyx_t_8 = NULL; + Py_ssize_t __pyx_t_9; + PyObject *__pyx_t_10 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 983); + __pyx_v_result = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_t = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_line = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_fields = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_record = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_x = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_field = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_key = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_value = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":984 + * ''' + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * result = {} + */ + __pyx_t_1 = PyObject_GetAttr(__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__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 = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_81), 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); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":986 + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * + * result = {} # <<<<<<<<<<<<<< + * + * if self.samfile.header.text != NULL: + */ + __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 986; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__pyx_v_result)); + __pyx_v_result = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":988 + * result = {} + * + * if self.samfile.header.text != NULL: # <<<<<<<<<<<<<< + * # convert to python string (note: call self.text to create 0-terminated string) + * t = self.text + */ + __pyx_t_4 = (((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile->header->text != NULL); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":990 + * if self.samfile.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 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__text); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_t); + __pyx_v_t = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":991 + * # 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 = PyObject_GetAttr(__pyx_v_t, __pyx_n_s__split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_k_tuple_82), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __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_5 = 0; __pyx_t_2 = __pyx_t_1; __Pyx_INCREF(__pyx_t_2); + } 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 = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_2))) { + if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_2)) break; + __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; + } else if (likely(PyTuple_CheckExact(__pyx_t_2))) { + if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_2)) break; + __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; + } else { + __pyx_t_1 = PyIter_Next(__pyx_t_2); + if (!__pyx_t_1) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_1); + } + __Pyx_DECREF(__pyx_v_line); + __pyx_v_line = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":992 + * 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 = PyObject_GetAttr(__pyx_v_line, __pyx_n_s__strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 992; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_6 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 992; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __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 = 992; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_3 = (!__pyx_t_4); + if (__pyx_t_3) { + goto __pyx_L7_continue; + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":993 + * 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 + __pyx_t_6 = PyObject_GetAttr(__pyx_v_line, __pyx_n_s__startswith); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_1 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_k_tuple_84), NULL); 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_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (unlikely(!__pyx_t_3)) { + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_85), __pyx_v_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyErr_SetObject(PyExc_AssertionError, ((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":994 + * 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_PySequence_GetSlice(__pyx_v_line, 1, PY_SSIZE_T_MAX); 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_6 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__split); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_k_tuple_86), NULL); 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_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(__pyx_v_fields); + __pyx_v_fields = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":995 + * 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, sizeof(long), PyInt_FromLong); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 995; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_v_record); + __pyx_v_record = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":996 + * 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 + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__VALID_HEADER_TYPES); 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_t_3 = ((PySequence_Contains(__pyx_t_1, __pyx_v_record))); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (unlikely(!__pyx_t_3)) { + __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_6 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_87), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + PyErr_SetObject(PyExc_AssertionError, ((PyObject *)__pyx_t_6)); + __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":999 + * + * # treat comments + * if record == "CO": # <<<<<<<<<<<<<< + * if record not in result: result[record] = [] + * result[record].append( "\t".join( fields[1:] ) ) + */ + __pyx_t_6 = PyObject_RichCompare(__pyx_v_record, ((PyObject *)__pyx_n_s__CO), Py_EQ); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 999; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 999; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1000 + * # treat comments + * if record == "CO": + * if record not in result: result[record] = [] # <<<<<<<<<<<<<< + * result[record].append( "\t".join( fields[1:] ) ) + * continue + */ + if (unlikely(__pyx_v_result == Py_None)) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_3 = (__Pyx_NegateNonNeg(PyDict_Contains(((PyObject *)__pyx_v_result), __pyx_v_record))); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_3) { + __pyx_t_6 = PyList_New(0); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + if (PyDict_SetItem(((PyObject *)__pyx_v_result), __pyx_v_record, ((PyObject *)__pyx_t_6)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + goto __pyx_L11; + } + __pyx_L11:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1001 + * if record == "CO": + * if record not in result: result[record] = [] + * result[record].append( "\t".join( fields[1:] ) ) # <<<<<<<<<<<<<< + * continue + * + */ + __pyx_t_6 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_result), __pyx_v_record); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_1), __pyx_n_s__join); 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_t_7 = __Pyx_PySequence_GetSlice(__pyx_v_fields, 1, PY_SSIZE_T_MAX); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __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 = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_8), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_8)); __pyx_t_8 = 0; + __pyx_t_8 = __Pyx_PyObject_Append(__pyx_t_6, __pyx_t_7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __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_7); __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1002 + * if record not in result: result[record] = [] + * result[record].append( "\t".join( fields[1:] ) ) + * continue # <<<<<<<<<<<<<< + * + * # the following is clumsy as generators do not work? + */ + goto __pyx_L7_continue; + goto __pyx_L10; + } + __pyx_L10:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1005 + * + * # the following is clumsy as generators do not work? + * x = {} # <<<<<<<<<<<<<< + * for field in fields[1:]: + * key, value = field.split(":",1) + */ + __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1005; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_8)); + __Pyx_DECREF(((PyObject *)__pyx_v_x)); + __pyx_v_x = __pyx_t_8; + __pyx_t_8 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1006 + * # the following is clumsy as generators do not work? + * x = {} + * for field in fields[1:]: # <<<<<<<<<<<<<< + * key, value = field.split(":",1) + * if key not in VALID_HEADER_FIELDS[record]: + */ + __pyx_t_8 = __Pyx_PySequence_GetSlice(__pyx_v_fields, 1, PY_SSIZE_T_MAX); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __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_9 = 0; __pyx_t_7 = __pyx_t_8; __Pyx_INCREF(__pyx_t_7); + } 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 = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + } + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_7))) { + if (__pyx_t_9 >= PyList_GET_SIZE(__pyx_t_7)) break; + __pyx_t_8 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_9); __Pyx_INCREF(__pyx_t_8); __pyx_t_9++; + } else if (likely(PyTuple_CheckExact(__pyx_t_7))) { + if (__pyx_t_9 >= PyTuple_GET_SIZE(__pyx_t_7)) break; + __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_9); __Pyx_INCREF(__pyx_t_8); __pyx_t_9++; + } else { + __pyx_t_8 = PyIter_Next(__pyx_t_7); + if (!__pyx_t_8) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_8); + } + __Pyx_DECREF(__pyx_v_field); + __pyx_v_field = __pyx_t_8; + __pyx_t_8 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1007 + * x = {} + * for field in fields[1:]: + * key, value = field.split(":",1) # <<<<<<<<<<<<<< + * if key not in VALID_HEADER_FIELDS[record]: + * raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) ) + */ + __pyx_t_8 = PyObject_GetAttr(__pyx_v_field, __pyx_n_s__split); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + __pyx_t_6 = PyObject_Call(__pyx_t_8, ((PyObject *)__pyx_k_tuple_89), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + if (PyTuple_CheckExact(__pyx_t_6) && likely(PyTuple_GET_SIZE(__pyx_t_6) == 2)) { + PyObject* tuple = __pyx_t_6; + __pyx_t_8 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_8); + __pyx_t_1 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(__pyx_v_key); + __pyx_v_key = __pyx_t_8; + __pyx_t_8 = 0; + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_1; + __pyx_t_1 = 0; + } else { + __pyx_t_10 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_10); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_8 = __Pyx_UnpackItem(__pyx_t_10, 0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + __pyx_t_1 = __Pyx_UnpackItem(__pyx_t_10, 1); 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); + if (__Pyx_EndUnpack(__pyx_t_10, 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; + __Pyx_DECREF(__pyx_v_key); + __pyx_v_key = __pyx_t_8; + __pyx_t_8 = 0; + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_1; + __pyx_t_1 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1008 + * for field in fields[1:]: + * key, value = field.split(":",1) + * if key not in VALID_HEADER_FIELDS[record]: # <<<<<<<<<<<<<< + * raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) ) + * x[key] = VALID_HEADER_FIELDS[record][key](value) + */ + __pyx_t_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1008; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_1 = PyObject_GetItem(__pyx_t_6, __pyx_v_record); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1008; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_3 = (__Pyx_NegateNonNeg(PySequence_Contains(__pyx_t_1, __pyx_v_key))); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1008; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1009 + * key, value = field.split(":",1) + * if key not in VALID_HEADER_FIELDS[record]: + * raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) ) # <<<<<<<<<<<<<< + * x[key] = VALID_HEADER_FIELDS[record][key](value) + * + */ + __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_6 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_90), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_DECREF(((PyObject *)__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 = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_6)); + __pyx_t_6 = 0; + __pyx_t_6 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_Raise(__pyx_t_6, 0, 0); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L14; + } + __pyx_L14:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1010 + * if key not in VALID_HEADER_FIELDS[record]: + * raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) ) + * x[key] = VALID_HEADER_FIELDS[record][key](value) # <<<<<<<<<<<<<< + * + * if VALID_HEADER_TYPES[record] == dict: + */ + __pyx_t_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1010; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_1 = PyObject_GetItem(__pyx_t_6, __pyx_v_record); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1010; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_6 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1010; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __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 = 1010; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1010; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + if (PyDict_SetItem(((PyObject *)__pyx_v_x), __pyx_v_key, __pyx_t_8) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1010; __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1012 + * x[key] = VALID_HEADER_FIELDS[record][key](value) + * + * if VALID_HEADER_TYPES[record] == dict: # <<<<<<<<<<<<<< + * if record in result: + * raise ValueError( "multiple '%s' lines are not permitted" % record ) + */ + __pyx_t_7 = __Pyx_GetName(__pyx_m, __pyx_n_s__VALID_HEADER_TYPES); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_8 = PyObject_GetItem(__pyx_t_7, __pyx_v_record); if (!__pyx_t_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __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); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __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 = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1013 + * + * if VALID_HEADER_TYPES[record] == dict: + * if record in result: # <<<<<<<<<<<<<< + * raise ValueError( "multiple '%s' lines are not permitted" % record ) + * result[record] = x + */ + if (unlikely(__pyx_v_result == Py_None)) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1013; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_3 = ((PyDict_Contains(((PyObject *)__pyx_v_result), __pyx_v_record))); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1013; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1014 + * 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 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_91), __pyx_v_record); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_7)); + __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_8)); + PyTuple_SET_ITEM(__pyx_t_8, 0, ((PyObject *)__pyx_t_7)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_7)); + __pyx_t_7 = 0; + __pyx_t_7 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_8), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(((PyObject *)__pyx_t_8)); __pyx_t_8 = 0; + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L16; + } + __pyx_L16:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1015 + * 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 (PyDict_SetItem(((PyObject *)__pyx_v_result), __pyx_v_record, ((PyObject *)__pyx_v_x)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1015; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L15; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1016 + * 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_GetName(__pyx_m, __pyx_n_s__VALID_HEADER_TYPES); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1016; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_8 = PyObject_GetItem(__pyx_t_7, __pyx_v_record); if (!__pyx_t_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1016; __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); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1016; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __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 = 1016; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1017 + * result[record] = x + * elif VALID_HEADER_TYPES[record] == list: + * if record not in result: result[record] = [] # <<<<<<<<<<<<<< + * result[record].append( x ) + * + */ + if (unlikely(__pyx_v_result == Py_None)) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_3 = (__Pyx_NegateNonNeg(PyDict_Contains(((PyObject *)__pyx_v_result), __pyx_v_record))); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_3) { + __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_7)); + if (PyDict_SetItem(((PyObject *)__pyx_v_result), __pyx_v_record, ((PyObject *)__pyx_t_7)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_7)); __pyx_t_7 = 0; + goto __pyx_L17; + } + __pyx_L17:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1018 + * elif VALID_HEADER_TYPES[record] == list: + * if record not in result: result[record] = [] + * result[record].append( x ) # <<<<<<<<<<<<<< + * + * return result + */ + __pyx_t_7 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_result), __pyx_v_record); if (!__pyx_t_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_8 = __Pyx_PyObject_Append(__pyx_t_7, ((PyObject *)__pyx_v_x)); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __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_8); __pyx_t_8 = 0; + goto __pyx_L15; + } + __pyx_L15:; + __pyx_L7_continue:; + } + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1020 + * result[record].append( x ) + * + * return result # <<<<<<<<<<<<<< + * + * def _buildLine( self, fields, record ): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_result)); + __pyx_r = ((PyObject *)__pyx_v_result); + goto __pyx_L0; + + __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_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_XDECREF(__pyx_t_10); + __Pyx_AddTraceback("csamtools.Samfile.header.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_result); + __Pyx_DECREF(__pyx_v_t); + __Pyx_DECREF(__pyx_v_line); + __Pyx_DECREF(__pyx_v_fields); + __Pyx_DECREF(__pyx_v_record); + __Pyx_DECREF(__pyx_v_x); + __Pyx_DECREF(__pyx_v_field); + __Pyx_DECREF(__pyx_v_key); + __Pyx_DECREF(__pyx_v_value); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1022 + * return result + * + * def _buildLine( self, fields, record ): # <<<<<<<<<<<<<< + * '''build a header line from *fields* dictionary for *record*''' + * + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_18_buildLine(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_18_buildLine[] = "build a header line from *fields* dictionary for *record*"; +static PyObject *__pyx_pf_9csamtools_7Samfile_18_buildLine(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_fields = 0; + PyObject *__pyx_v_record = 0; + PyObject *__pyx_v_line; + PyObject *__pyx_v_key; + PyObject *__pyx_r = NULL; + 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 = NULL; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__fields,&__pyx_n_s__record,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_buildLine"); + __Pyx_TraceCall("_buildLine", __pyx_f[0], 1022); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__fields); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__record); + if (likely(values[1])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("_buildLine", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "_buildLine") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_fields = values[0]; + __pyx_v_record = values[1]; + } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_fields = PyTuple_GET_ITEM(__pyx_args, 0); + __pyx_v_record = PyTuple_GET_ITEM(__pyx_args, 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 = 1022; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Samfile._buildLine"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_v_line = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_key = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1026 + * + * # TODO: add checking for field and sort order + * line = ["@%s" % record ] # <<<<<<<<<<<<<< + * if record == "CO": + * line.append( fields ) + */ + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_92), __pyx_v_record); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyList_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_v_line)); + __pyx_v_line = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1027 + * # TODO: add checking for field and sort order + * line = ["@%s" % record ] + * if record == "CO": # <<<<<<<<<<<<<< + * line.append( fields ) + * else: + */ + __pyx_t_2 = PyObject_RichCompare(__pyx_v_record, ((PyObject *)__pyx_n_s__CO), Py_EQ); 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_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1028 + * line = ["@%s" % record ] + * if record == "CO": + * line.append( fields ) # <<<<<<<<<<<<<< + * else: + * for key in VALID_HEADER_ORDER[record]: + */ + if (unlikely(__pyx_v_line == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_4 = PyList_Append(__pyx_v_line, __pyx_v_fields); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1030 + * line.append( fields ) + * else: + * for key in VALID_HEADER_ORDER[record]: # <<<<<<<<<<<<<< + * if key in fields: + * line.append( "%s:%s" % (key, str(fields[key]))) + */ + __pyx_t_2 = __Pyx_GetName(__pyx_m, __pyx_n_s__VALID_HEADER_ORDER); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyObject_GetItem(__pyx_t_2, __pyx_v_record); if (!__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_2); __pyx_t_2 = 0; + if (PyList_CheckExact(__pyx_t_1) || PyTuple_CheckExact(__pyx_t_1)) { + __pyx_t_5 = 0; __pyx_t_2 = __pyx_t_1; __Pyx_INCREF(__pyx_t_2); + } 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 = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_2))) { + if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_2)) break; + __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; + } else if (likely(PyTuple_CheckExact(__pyx_t_2))) { + if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_2)) break; + __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; + } else { + __pyx_t_1 = PyIter_Next(__pyx_t_2); + if (!__pyx_t_1) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_1); + } + __Pyx_DECREF(__pyx_v_key); + __pyx_v_key = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1031 + * else: + * for key in VALID_HEADER_ORDER[record]: + * if key in fields: # <<<<<<<<<<<<<< + * line.append( "%s:%s" % (key, str(fields[key]))) + * return "\t".join( line ) + */ + __pyx_t_3 = ((PySequence_Contains(__pyx_v_fields, __pyx_v_key))); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1032 + * for key in VALID_HEADER_ORDER[record]: + * if key in fields: + * line.append( "%s:%s" % (key, str(fields[key]))) # <<<<<<<<<<<<<< + * return "\t".join( line ) + * + */ + if (unlikely(__pyx_v_line == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (!__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_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), ((PyObject *)__pyx_t_6), NULL); 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_DECREF(((PyObject *)__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 = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_INCREF(__pyx_v_key); + PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_key); + __Pyx_GIVEREF(__pyx_v_key); + PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_1 = 0; + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_93), ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + __pyx_t_4 = PyList_Append(__pyx_v_line, ((PyObject *)__pyx_t_1)); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + goto __pyx_L9; + } + __pyx_L9:; + } + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1033 + * if key in fields: + * line.append( "%s:%s" % (key, str(fields[key]))) + * return "\t".join( line ) # <<<<<<<<<<<<<< + * + * cdef bam_header_t * _buildHeader( self, new_header ): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_1), __pyx_n_s__join); 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_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1033; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(((PyObject *)__pyx_v_line)); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_line)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_line)); + __pyx_t_6 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1033; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __pyx_r = __pyx_t_6; + __pyx_t_6 = 0; + goto __pyx_L0; + + __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_6); + __Pyx_AddTraceback("csamtools.Samfile._buildLine"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_line); + __Pyx_DECREF(__pyx_v_key); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1035 + * return "\t".join( line ) + * + * cdef bam_header_t * _buildHeader( self, new_header ): # <<<<<<<<<<<<<< + * '''return a new header built from a dictionary in *new_header*. + * + */ + +static bam_header_t *__pyx_f_9csamtools_7Samfile__buildHeader(struct __pyx_obj_9csamtools_Samfile *__pyx_v_self, PyObject *__pyx_v_new_header) { + PyObject *__pyx_v_lines; + bam_header_t *__pyx_v_dest; + PyObject *__pyx_v_record; + PyObject *__pyx_v_ttype; + PyObject *__pyx_v_data; + PyObject *__pyx_v_fields; + PyObject *__pyx_v_text; + PyObject *__pyx_v_seqs; + long __pyx_v_x; + PyObject *__pyx_v_seqname; + PyObject *__pyx_v_seqlen; + bam_header_t *__pyx_r; + PyObject *__pyx_t_1 = NULL; + Py_ssize_t __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + PyObject *__pyx_t_5 = NULL; + PyObject *__pyx_t_6 = NULL; + int __pyx_t_7; + Py_ssize_t __pyx_t_8; + PyObject *__pyx_t_9 = NULL; + char *__pyx_t_10; + PyObject *__pyx_t_11 = NULL; + int32_t __pyx_t_12; + uint32_t __pyx_t_13; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_buildHeader"); + __Pyx_TraceCall("_buildHeader", __pyx_f[0], 1035); + __pyx_v_lines = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_record = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_ttype = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_data = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_fields = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_text = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_seqs = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_seqname = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_seqlen = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1041 + * ''' + * + * lines = [] # <<<<<<<<<<<<<< + * + * # check if hash exists + */ + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__pyx_v_lines)); + __pyx_v_lines = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1048 + * cdef bam_header_t * dest + * + * dest = bam_header_init() # <<<<<<<<<<<<<< + * + * for record in VALID_HEADERS: + */ + __pyx_v_dest = bam_header_init(); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1050 + * dest = bam_header_init() + * + * for record in VALID_HEADERS: # <<<<<<<<<<<<<< + * if record in new_header: + * ttype = VALID_HEADER_TYPES[record] + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__VALID_HEADERS); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1050; __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 = 0; __pyx_t_3 = __pyx_t_1; __Pyx_INCREF(__pyx_t_3); + } else { + __pyx_t_2 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1050; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_3))) { + if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_3)) break; + __pyx_t_1 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_2); __Pyx_INCREF(__pyx_t_1); __pyx_t_2++; + } else if (likely(PyTuple_CheckExact(__pyx_t_3))) { + if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_3)) break; + __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_2); __Pyx_INCREF(__pyx_t_1); __pyx_t_2++; + } else { + __pyx_t_1 = PyIter_Next(__pyx_t_3); + if (!__pyx_t_1) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1050; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_1); + } + __Pyx_DECREF(__pyx_v_record); + __pyx_v_record = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1051 + * + * for record in VALID_HEADERS: + * if record in new_header: # <<<<<<<<<<<<<< + * ttype = VALID_HEADER_TYPES[record] + * data = new_header[record] + */ + __pyx_t_4 = ((PySequence_Contains(__pyx_v_new_header, __pyx_v_record))); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1051; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1052 + * 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_GetName(__pyx_m, __pyx_n_s__VALID_HEADER_TYPES); 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_5 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_ttype); + __pyx_v_ttype = __pyx_t_5; + __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1053 + * 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()) ) ) + */ + __pyx_t_5 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1053; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_v_data); + __pyx_v_data = __pyx_t_5; + __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1054 + * 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 ) == types.DictType: + */ + __pyx_t_5 = PyObject_Call(__pyx_v_ttype, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_data)), ((PyObject *)Py_TYPE(__pyx_t_5)), Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __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 = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1055 + * 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 ) == types.DictType: + * lines.append( self._buildLine( data, record ) ) + */ + __pyx_t_1 = PyObject_Call(__pyx_v_ttype, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1055; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_5 = PyTuple_New(3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1055; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __Pyx_INCREF(__pyx_v_record); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_record); + __Pyx_GIVEREF(__pyx_v_record); + __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_data))); + PyTuple_SET_ITEM(__pyx_t_5, 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_5, 2, ((PyObject *)Py_TYPE(__pyx_t_1))); + __Pyx_GIVEREF(((PyObject *)Py_TYPE(__pyx_t_1))); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_94), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1055; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__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 = 1055; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1055; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1055; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1056 + * if type( data ) != type( ttype() ): + * raise ValueError( "invalid type for record %s: %s, expected %s" % (record, type(data), type(ttype()) ) ) + * if type( data ) == types.DictType: # <<<<<<<<<<<<<< + * lines.append( self._buildLine( data, record ) ) + * else: + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__types); 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); + __pyx_t_5 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__DictType); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_data)), __pyx_t_5, Py_EQ); 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); + __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 = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1057 + * raise ValueError( "invalid type for record %s: %s, expected %s" % (record, type(data), type(ttype()) ) ) + * if type( data ) == types.DictType: + * lines.append( self._buildLine( data, record ) ) # <<<<<<<<<<<<<< + * else: + * for fields in new_header[record]: + */ + if (unlikely(__pyx_v_lines == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_self), __pyx_n_s___buildLine); 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_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __Pyx_INCREF(__pyx_v_data); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_data); + __Pyx_GIVEREF(__pyx_v_data); + __Pyx_INCREF(__pyx_v_record); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_v_record); + __Pyx_GIVEREF(__pyx_v_record); + __pyx_t_6 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __pyx_t_7 = PyList_Append(__pyx_v_lines, __pyx_t_6); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + goto __pyx_L7; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1059 + * lines.append( self._buildLine( data, record ) ) + * else: + * for fields in new_header[record]: # <<<<<<<<<<<<<< + * lines.append( self._buildLine( fields, record ) ) + * + */ + __pyx_t_6 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + if (PyList_CheckExact(__pyx_t_6) || PyTuple_CheckExact(__pyx_t_6)) { + __pyx_t_8 = 0; __pyx_t_5 = __pyx_t_6; __Pyx_INCREF(__pyx_t_5); + } else { + __pyx_t_8 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + } + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_5))) { + if (__pyx_t_8 >= PyList_GET_SIZE(__pyx_t_5)) break; + __pyx_t_6 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_8); __Pyx_INCREF(__pyx_t_6); __pyx_t_8++; + } else if (likely(PyTuple_CheckExact(__pyx_t_5))) { + if (__pyx_t_8 >= PyTuple_GET_SIZE(__pyx_t_5)) break; + __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_8); __Pyx_INCREF(__pyx_t_6); __pyx_t_8++; + } else { + __pyx_t_6 = PyIter_Next(__pyx_t_5); + if (!__pyx_t_6) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_6); + } + __Pyx_DECREF(__pyx_v_fields); + __pyx_v_fields = __pyx_t_6; + __pyx_t_6 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1060 + * else: + * for fields in new_header[record]: + * lines.append( self._buildLine( fields, record ) ) # <<<<<<<<<<<<<< + * + * text = "\n".join(lines) + "\n" + */ + if (unlikely(__pyx_v_lines == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_6 = PyObject_GetAttr(((PyObject *)__pyx_v_self), __pyx_n_s___buildLine); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_9 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __pyx_t_7 = PyList_Append(__pyx_v_lines, __pyx_t_9); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; + } + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + } + __pyx_L7:; + goto __pyx_L5; + } + __pyx_L5:; + } + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1062 + * lines.append( self._buildLine( fields, record ) ) + * + * text = "\n".join(lines) + "\n" # <<<<<<<<<<<<<< + * if dest.text != NULL: free( dest.text ) + * dest.text = calloc( len(text), sizeof(char)) + */ + __pyx_t_3 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_2), __pyx_n_s__join); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __Pyx_INCREF(((PyObject *)__pyx_v_lines)); + PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_lines)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_lines)); + __pyx_t_9 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __pyx_t_5 = PyNumber_Add(__pyx_t_9, ((PyObject *)__pyx_kp_s_2)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; + __Pyx_DECREF(__pyx_v_text); + __pyx_v_text = __pyx_t_5; + __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1063 + * + * text = "\n".join(lines) + "\n" + * if dest.text != NULL: free( dest.text ) # <<<<<<<<<<<<<< + * dest.text = calloc( len(text), sizeof(char)) + * dest.l_text = len(text) + */ + __pyx_t_4 = (__pyx_v_dest->text != NULL); + if (__pyx_t_4) { + free(__pyx_v_dest->text); + goto __pyx_L10; + } + __pyx_L10:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1064 + * text = "\n".join(lines) + "\n" + * if dest.text != NULL: free( dest.text ) + * dest.text = calloc( len(text), sizeof(char)) # <<<<<<<<<<<<<< + * dest.l_text = len(text) + * strncpy( dest.text, text, dest.l_text ) + */ + __pyx_t_2 = PyObject_Length(__pyx_v_text); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1064; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_dest->text = ((char *)calloc(__pyx_t_2, (sizeof(char)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1065 + * if dest.text != NULL: free( dest.text ) + * dest.text = calloc( len(text), sizeof(char)) + * dest.l_text = len(text) # <<<<<<<<<<<<<< + * strncpy( dest.text, text, dest.l_text ) + * + */ + __pyx_t_2 = PyObject_Length(__pyx_v_text); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1065; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_dest->l_text = __pyx_t_2; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1066 + * dest.text = calloc( len(text), sizeof(char)) + * dest.l_text = len(text) + * strncpy( dest.text, text, dest.l_text ) # <<<<<<<<<<<<<< + * + * # collect targets + */ + __pyx_t_10 = PyBytes_AsString(__pyx_v_text); if (unlikely((!__pyx_t_10) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1066; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + strncpy(__pyx_v_dest->text, __pyx_t_10, __pyx_v_dest->l_text); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1069 + * + * # collect targets + * if "SQ" in new_header: # <<<<<<<<<<<<<< + * seqs = [] + * for fields in new_header["SQ"]: + */ + __pyx_t_4 = ((PySequence_Contains(__pyx_v_new_header, ((PyObject *)__pyx_n_s__SQ)))); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1070 + * # collect targets + * if "SQ" in new_header: + * seqs = [] # <<<<<<<<<<<<<< + * for fields in new_header["SQ"]: + * try: + */ + __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __Pyx_DECREF(((PyObject *)__pyx_v_seqs)); + __pyx_v_seqs = __pyx_t_5; + __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1071 + * if "SQ" in new_header: + * seqs = [] + * for fields in new_header["SQ"]: # <<<<<<<<<<<<<< + * try: + * seqs.append( (fields["SN"], fields["LN"] ) ) + */ + __pyx_t_5 = PyObject_GetItem(__pyx_v_new_header, ((PyObject *)__pyx_n_s__SQ)); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + if (PyList_CheckExact(__pyx_t_5) || PyTuple_CheckExact(__pyx_t_5)) { + __pyx_t_2 = 0; __pyx_t_9 = __pyx_t_5; __Pyx_INCREF(__pyx_t_9); + } else { + __pyx_t_2 = -1; __pyx_t_9 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + } + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_9))) { + if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_9)) break; + __pyx_t_5 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_2); __Pyx_INCREF(__pyx_t_5); __pyx_t_2++; + } else if (likely(PyTuple_CheckExact(__pyx_t_9))) { + if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_9)) break; + __pyx_t_5 = PyTuple_GET_ITEM(__pyx_t_9, __pyx_t_2); __Pyx_INCREF(__pyx_t_5); __pyx_t_2++; + } else { + __pyx_t_5 = PyIter_Next(__pyx_t_9); + if (!__pyx_t_5) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_5); + } + __Pyx_DECREF(__pyx_v_fields); + __pyx_v_fields = __pyx_t_5; + __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1072 + * seqs = [] + * for fields in new_header["SQ"]: + * try: # <<<<<<<<<<<<<< + * seqs.append( (fields["SN"], fields["LN"] ) ) + * except KeyError: + */ + { + PyObject *__pyx_save_exc_type, *__pyx_save_exc_value, *__pyx_save_exc_tb; + __Pyx_ExceptionSave(&__pyx_save_exc_type, &__pyx_save_exc_value, &__pyx_save_exc_tb); + __Pyx_XGOTREF(__pyx_save_exc_type); + __Pyx_XGOTREF(__pyx_save_exc_value); + __Pyx_XGOTREF(__pyx_save_exc_tb); + /*try:*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1073 + * for fields in new_header["SQ"]: + * try: + * seqs.append( (fields["SN"], fields["LN"] ) ) # <<<<<<<<<<<<<< + * except KeyError: + * raise KeyError( "incomplete sequence information in '%s'" % str(fields)) + */ + if (unlikely(__pyx_v_seqs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1073; __pyx_clineno = __LINE__; goto __pyx_L14_error;} + } + __pyx_t_5 = PyObject_GetItem(__pyx_v_fields, ((PyObject *)__pyx_n_s__SN)); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1073; __pyx_clineno = __LINE__; goto __pyx_L14_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_3 = PyObject_GetItem(__pyx_v_fields, ((PyObject *)__pyx_n_s__LN)); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1073; __pyx_clineno = __LINE__; goto __pyx_L14_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1073; __pyx_clineno = __LINE__; goto __pyx_L14_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3); + __Pyx_GIVEREF(__pyx_t_3); + __pyx_t_5 = 0; + __pyx_t_3 = 0; + __pyx_t_7 = PyList_Append(__pyx_v_seqs, ((PyObject *)__pyx_t_1)); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1073; __pyx_clineno = __LINE__; goto __pyx_L14_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + } + __Pyx_XDECREF(__pyx_save_exc_type); __pyx_save_exc_type = 0; + __Pyx_XDECREF(__pyx_save_exc_value); __pyx_save_exc_value = 0; + __Pyx_XDECREF(__pyx_save_exc_tb); __pyx_save_exc_tb = 0; + goto __pyx_L21_try_end; + __pyx_L14_error:; + __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1074 + * try: + * seqs.append( (fields["SN"], fields["LN"] ) ) + * except KeyError: # <<<<<<<<<<<<<< + * raise KeyError( "incomplete sequence information in '%s'" % str(fields)) + * + */ + __pyx_t_7 = PyErr_ExceptionMatches(__pyx_builtin_KeyError); + if (__pyx_t_7) { + __Pyx_AddTraceback("csamtools.Samfile._buildHeader"); + if (__Pyx_GetException(&__pyx_t_1, &__pyx_t_3, &__pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1074; __pyx_clineno = __LINE__; goto __pyx_L16_except_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_GOTREF(__pyx_t_3); + __Pyx_GOTREF(__pyx_t_5); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1075 + * seqs.append( (fields["SN"], fields["LN"] ) ) + * except KeyError: + * raise KeyError( "incomplete sequence information in '%s'" % str(fields)) # <<<<<<<<<<<<<< + * + * dest.n_targets = len(seqs) + */ + __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1075; __pyx_clineno = __LINE__; goto __pyx_L16_except_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_INCREF(__pyx_v_fields); + PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_fields); + __Pyx_GIVEREF(__pyx_v_fields); + __pyx_t_11 = PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), ((PyObject *)__pyx_t_6), NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1075; __pyx_clineno = __LINE__; goto __pyx_L16_except_error;} + __Pyx_GOTREF(__pyx_t_11); + __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + __pyx_t_6 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_95), __pyx_t_11); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1075; __pyx_clineno = __LINE__; goto __pyx_L16_except_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; + __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1075; __pyx_clineno = __LINE__; goto __pyx_L16_except_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_11)); + PyTuple_SET_ITEM(__pyx_t_11, 0, ((PyObject *)__pyx_t_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_6)); + __pyx_t_6 = 0; + __pyx_t_6 = PyObject_Call(__pyx_builtin_KeyError, ((PyObject *)__pyx_t_11), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1075; __pyx_clineno = __LINE__; goto __pyx_L16_except_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(((PyObject *)__pyx_t_11)); __pyx_t_11 = 0; + __Pyx_Raise(__pyx_t_6, 0, 0); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1075; __pyx_clineno = __LINE__; goto __pyx_L16_except_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + goto __pyx_L15_exception_handled; + } + __pyx_L16_except_error:; + __Pyx_XGIVEREF(__pyx_save_exc_type); + __Pyx_XGIVEREF(__pyx_save_exc_value); + __Pyx_XGIVEREF(__pyx_save_exc_tb); + __Pyx_ExceptionReset(__pyx_save_exc_type, __pyx_save_exc_value, __pyx_save_exc_tb); + goto __pyx_L1_error; + __pyx_L15_exception_handled:; + __Pyx_XGIVEREF(__pyx_save_exc_type); + __Pyx_XGIVEREF(__pyx_save_exc_value); + __Pyx_XGIVEREF(__pyx_save_exc_tb); + __Pyx_ExceptionReset(__pyx_save_exc_type, __pyx_save_exc_value, __pyx_save_exc_tb); + __pyx_L21_try_end:; + } + } + __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1077 + * raise KeyError( "incomplete sequence information in '%s'" % str(fields)) + * + * dest.n_targets = len(seqs) # <<<<<<<<<<<<<< + * dest.target_name = calloc( dest.n_targets, sizeof(char*) ) + * dest.target_len = calloc( dest.n_targets, sizeof(uint32_t) ) + */ + if (unlikely(__pyx_v_seqs == Py_None)) { + PyErr_SetString(PyExc_TypeError, "object of type 'NoneType' has no len()"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1077; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = PyList_GET_SIZE(((PyObject *)__pyx_v_seqs)); + __pyx_v_dest->n_targets = __pyx_t_2; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1078 + * + * dest.n_targets = len(seqs) + * dest.target_name = calloc( dest.n_targets, sizeof(char*) ) # <<<<<<<<<<<<<< + * dest.target_len = calloc( dest.n_targets, sizeof(uint32_t) ) + * + */ + __pyx_v_dest->target_name = ((char **)calloc(__pyx_v_dest->n_targets, (sizeof(char *)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1079 + * dest.n_targets = len(seqs) + * dest.target_name = calloc( dest.n_targets, sizeof(char*) ) + * dest.target_len = 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)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1081 + * dest.target_len = calloc( dest.n_targets, sizeof(uint32_t) ) + * + * for x from 0 <= x < dest.n_targets: # <<<<<<<<<<<<<< + * seqname, seqlen = seqs[x] + * dest.target_name[x] = calloc( len( seqname ) + 1, sizeof(char) ) + */ + __pyx_t_12 = __pyx_v_dest->n_targets; + for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_12; __pyx_v_x++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1082 + * + * for x from 0 <= x < dest.n_targets: + * seqname, seqlen = seqs[x] # <<<<<<<<<<<<<< + * dest.target_name[x] = calloc( len( seqname ) + 1, sizeof(char) ) + * strncpy( dest.target_name[x], seqname, len(seqname) + 1 ) + */ + __pyx_t_9 = __Pyx_GetItemInt_List(((PyObject *)__pyx_v_seqs), __pyx_v_x, sizeof(long), PyInt_FromLong); if (!__pyx_t_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + if (PyTuple_CheckExact(__pyx_t_9) && likely(PyTuple_GET_SIZE(__pyx_t_9) == 2)) { + PyObject* tuple = __pyx_t_9; + __pyx_t_5 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_5); + __pyx_t_3 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; + __Pyx_DECREF(__pyx_v_seqname); + __pyx_v_seqname = __pyx_t_5; + __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_v_seqlen); + __pyx_v_seqlen = __pyx_t_3; + __pyx_t_3 = 0; + } else { + __pyx_t_1 = PyObject_GetIter(__pyx_t_9); 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_9); __pyx_t_9 = 0; + __pyx_t_5 = __Pyx_UnpackItem(__pyx_t_1, 0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_3 = __Pyx_UnpackItem(__pyx_t_1, 1); 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); + if (__Pyx_EndUnpack(__pyx_t_1, 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_seqname); + __pyx_v_seqname = __pyx_t_5; + __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_v_seqlen); + __pyx_v_seqlen = __pyx_t_3; + __pyx_t_3 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1083 + * for x from 0 <= x < dest.n_targets: + * seqname, seqlen = seqs[x] + * dest.target_name[x] = calloc( len( seqname ) + 1, sizeof(char) ) # <<<<<<<<<<<<<< + * strncpy( dest.target_name[x], seqname, len(seqname) + 1 ) + * dest.target_len[x] = seqlen + */ + __pyx_t_2 = PyObject_Length(__pyx_v_seqname); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1083; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + (__pyx_v_dest->target_name[__pyx_v_x]) = ((char *)calloc((__pyx_t_2 + 1), (sizeof(char)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1084 + * seqname, seqlen = seqs[x] + * dest.target_name[x] = calloc( len( seqname ) + 1, sizeof(char) ) + * strncpy( dest.target_name[x], seqname, len(seqname) + 1 ) # <<<<<<<<<<<<<< + * dest.target_len[x] = seqlen + * + */ + __pyx_t_10 = PyBytes_AsString(__pyx_v_seqname); if (unlikely((!__pyx_t_10) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1084; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = PyObject_Length(__pyx_v_seqname); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1084; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + strncpy((__pyx_v_dest->target_name[__pyx_v_x]), __pyx_t_10, (__pyx_t_2 + 1)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1085 + * dest.target_name[x] = calloc( len( seqname ) + 1, sizeof(char) ) + * strncpy( dest.target_name[x], seqname, len(seqname) + 1 ) + * dest.target_len[x] = seqlen # <<<<<<<<<<<<<< + * + * return dest + */ + __pyx_t_13 = __Pyx_PyInt_from_py_uint32_t(__pyx_v_seqlen); if (unlikely((__pyx_t_13 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + (__pyx_v_dest->target_len[__pyx_v_x]) = __pyx_t_13; + } + goto __pyx_L11; + } + __pyx_L11:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1087 + * dest.target_len[x] = seqlen + * + * return dest # <<<<<<<<<<<<<< + * + * ############################################################### + */ + __pyx_r = __pyx_v_dest; + goto __pyx_L0; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_9); + __Pyx_XDECREF(__pyx_t_11); + __Pyx_WriteUnraisable("csamtools.Samfile._buildHeader"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_lines); + __Pyx_DECREF(__pyx_v_record); + __Pyx_DECREF(__pyx_v_ttype); + __Pyx_DECREF(__pyx_v_data); + __Pyx_DECREF(__pyx_v_fields); + __Pyx_DECREF(__pyx_v_text); + __Pyx_DECREF(__pyx_v_seqs); + __Pyx_DECREF(__pyx_v_seqname); + __Pyx_DECREF(__pyx_v_seqlen); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1097 + * ## Possible solutions: deprecate or open new file handle + * ############################################################### + * def __iter__(self): # <<<<<<<<<<<<<< + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return self + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_19__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7Samfile_19__iter__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__iter__"); + __Pyx_TraceCall("__iter__", __pyx_f[0], 1097); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1098 + * ############################################################### + * def __iter__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * return self + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1098; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1098; __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 = 1098; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_96), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1098; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1098; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1099 + * def __iter__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + * return self # <<<<<<<<<<<<<< + * + * cdef bam1_t * getCurrent( self ): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_self); + __pyx_r = __pyx_v_self; + goto __pyx_L0; + + __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("csamtools.Samfile.__iter__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1101 + * return self + * + * cdef bam1_t * getCurrent( self ): # <<<<<<<<<<<<<< + * return self.b + * + */ + +static bam1_t *__pyx_f_9csamtools_7Samfile_getCurrent(struct __pyx_obj_9csamtools_Samfile *__pyx_v_self) { + bam1_t *__pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("getCurrent"); + __Pyx_TraceCall("getCurrent", __pyx_f[0], 1101); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1102 + * + * cdef bam1_t * getCurrent( self ): + * return self.b # <<<<<<<<<<<<<< + * + * cdef int cnext(self): + */ + __pyx_r = __pyx_v_self->b; + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1104 + * return self.b + * + * cdef int cnext(self): # <<<<<<<<<<<<<< + * ''' + * cversion of iterator. Used by :class:`pysam.Samfile.IteratorColumn`. + */ + +static int __pyx_f_9csamtools_7Samfile_cnext(struct __pyx_obj_9csamtools_Samfile *__pyx_v_self) { + int __pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("cnext"); + __Pyx_TraceCall("cnext", __pyx_f[0], 1104); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1109 + * ''' + * cdef int ret + * return samread(self.samfile, self.b) # <<<<<<<<<<<<<< + * + * def __next__(self): + */ + __pyx_r = samread(__pyx_v_self->samfile, __pyx_v_self->b); + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1111 + * return samread(self.samfile, self.b) + * + * def __next__(self): # <<<<<<<<<<<<<< + * """ + * python version of next(). + */ + +static PyObject *__pyx_pf_9csamtools_7Samfile_20__next__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_7Samfile_20__next__[] = "\n python version of next().\n "; +struct wrapperbase __pyx_wrapperbase_9csamtools_7Samfile_20__next__; +static PyObject *__pyx_pf_9csamtools_7Samfile_20__next__(PyObject *__pyx_v_self) { + int __pyx_v_ret; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__next__"); + __Pyx_TraceCall("__next__", __pyx_f[0], 1111); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1116 + * """ + * cdef int ret + * ret = samread(self.samfile, self.b) # <<<<<<<<<<<<<< + * if (ret > 0): + * return makeAlignedRead( self.b ) + */ + __pyx_v_ret = samread(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->samfile, ((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->b); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1117 + * cdef int ret + * ret = samread(self.samfile, self.b) + * if (ret > 0): # <<<<<<<<<<<<<< + * return makeAlignedRead( self.b ) + * else: + */ + __pyx_t_1 = (__pyx_v_ret > 0); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1118 + * ret = samread(self.samfile, self.b) + * if (ret > 0): + * return makeAlignedRead( self.b ) # <<<<<<<<<<<<<< + * else: + * raise StopIteration + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = __pyx_f_9csamtools_makeAlignedRead(((struct __pyx_obj_9csamtools_Samfile *)__pyx_v_self)->b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1120 + * return makeAlignedRead( self.b ) + * else: + * raise StopIteration # <<<<<<<<<<<<<< + * + * ##------------------------------------------------------------------- + */ + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_L5:; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.Samfile.__next__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1170 + * cdef samfile_t * fp + * + * def __cinit__(self, Samfile samfile, int tid, int beg, int end, int reopen = True ): # <<<<<<<<<<<<<< + * + * if not samfile._isOpen(): + */ + +static int __pyx_pf_9csamtools_17IteratorRowRegion_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_17IteratorRowRegion_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_Samfile *__pyx_v_samfile = 0; + int __pyx_v_tid; + int __pyx_v_beg; + int __pyx_v_end; + int __pyx_v_reopen; + PyObject *__pyx_v_mode; + PyObject *__pyx_v_store; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + char *__pyx_t_5; + 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}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 1170); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[5] = {0,0,0,0,0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__samfile); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tid); + if (likely(values[1])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 4, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + case 2: + values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__beg); + if (likely(values[2])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 4, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + case 3: + values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end); + if (likely(values[3])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 4, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)values[0]); + __pyx_v_tid = __Pyx_PyInt_AsInt(values[1]); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_beg = __Pyx_PyInt_AsInt(values[2]); if (unlikely((__pyx_v_beg == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_end = __Pyx_PyInt_AsInt(values[3]); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + if (values[4]) { + __pyx_v_reopen = __Pyx_PyInt_AsInt(values[4]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } else { + __pyx_v_reopen = ((int)1); + } + } else { + __pyx_v_reopen = ((int)1); + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 5: + __pyx_v_reopen = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 4)); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + case 4: + __pyx_v_end = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 3)); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_beg = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 2)); if (unlikely((__pyx_v_beg == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_tid = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 1)); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)PyTuple_GET_ITEM(__pyx_args, 0)); + break; + default: goto __pyx_L5_argtuple_error; + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 4, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.IteratorRowRegion.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + __pyx_v_mode = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_store = Py_None; __Pyx_INCREF(Py_None); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_9csamtools_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1170; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1172 + * def __cinit__(self, Samfile samfile, int tid, int beg, int end, int reopen = True ): + * + * if not samfile._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * + */ + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_samfile), __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1172; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1172; __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 = 1172; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1173 + * + * if not samfile._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * if not samfile._hasIndex(): + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_97), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1173; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1173; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1175 + * raise ValueError( "I/O operation on closed file" ) + * + * if not samfile._hasIndex(): # <<<<<<<<<<<<<< + * raise ValueError( "no index available for iteration" ) + * + */ + __pyx_t_2 = PyObject_GetAttr(((PyObject *)__pyx_v_samfile), __pyx_n_s___hasIndex); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1175; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1175; __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 = 1175; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_3 = (!__pyx_t_4); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1176 + * + * if not samfile._hasIndex(): + * raise ValueError( "no index available for iteration" ) # <<<<<<<<<<<<<< + * + * # makes sure that samfile stays alive as long as the + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_99), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1176; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1176; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1180 + * # makes sure that samfile stays alive as long as the + * # iterator is alive + * self.samfile = samfile # <<<<<<<<<<<<<< + * + * if samfile.isbam: mode = "rb" + */ + __Pyx_INCREF(((PyObject *)__pyx_v_samfile)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile)); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->samfile); + __Pyx_DECREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->samfile)); + ((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->samfile = __pyx_v_samfile; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1182 + * self.samfile = samfile + * + * if samfile.isbam: mode = "rb" # <<<<<<<<<<<<<< + * else: mode = "r" + * + */ + if (__pyx_v_samfile->isbam) { + __Pyx_INCREF(((PyObject *)__pyx_n_s__rb)); + __Pyx_DECREF(__pyx_v_mode); + __pyx_v_mode = ((PyObject *)__pyx_n_s__rb); + goto __pyx_L8; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1183 + * + * if samfile.isbam: mode = "rb" + * else: mode = "r" # <<<<<<<<<<<<<< + * + * # reopen the file - note that this makes the iterator + */ + __Pyx_INCREF(((PyObject *)__pyx_n_s__r)); + __Pyx_DECREF(__pyx_v_mode); + __pyx_v_mode = ((PyObject *)__pyx_n_s__r); + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1187 + * # reopen the file - note that this makes the iterator + * # slow and causes pileup to slow down significantly. + * if reopen: # <<<<<<<<<<<<<< + * store = StderrStore() + * self.fp = samopen( samfile._filename, mode, NULL ) + */ + if (__pyx_v_reopen) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1188 + * # slow and causes pileup to slow down significantly. + * if reopen: + * store = StderrStore() # <<<<<<<<<<<<<< + * self.fp = samopen( samfile._filename, mode, NULL ) + * store.release() + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__StderrStore); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1188; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1188; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_store); + __pyx_v_store = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1189 + * if reopen: + * store = StderrStore() + * self.fp = samopen( samfile._filename, mode, NULL ) # <<<<<<<<<<<<<< + * store.release() + * assert self.fp != NULL + */ + __pyx_t_5 = PyBytes_AsString(__pyx_v_mode); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->fp = samopen(__pyx_v_samfile->_filename, __pyx_t_5, NULL); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1190 + * store = StderrStore() + * self.fp = samopen( samfile._filename, mode, NULL ) + * store.release() # <<<<<<<<<<<<<< + * assert self.fp != NULL + * + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_store, __pyx_n_s__release); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1190; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1190; __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_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1191 + * self.fp = samopen( samfile._filename, mode, NULL ) + * store.release() + * assert self.fp != NULL # <<<<<<<<<<<<<< + * + * self.retval = 0 + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + if (unlikely(!(((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->fp != NULL))) { + PyErr_SetNone(PyExc_AssertionError); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1191; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1193 + * assert self.fp != NULL + * + * self.retval = 0 # <<<<<<<<<<<<<< + * + * self.iter = bam_iter_query(self.samfile.index, + */ + ((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->retval = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1195 + * self.retval = 0 + * + * self.iter = bam_iter_query(self.samfile.index, # <<<<<<<<<<<<<< + * tid, + * beg, + */ + ((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->iter = bam_iter_query(((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->samfile->index, __pyx_v_tid, __pyx_v_beg, __pyx_v_end); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1199 + * beg, + * end) + * self.b = bam_init1() # <<<<<<<<<<<<<< + * + * def __iter__(self): + */ + ((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->b = bam_init1(); + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IteratorRowRegion.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_mode); + __Pyx_DECREF(__pyx_v_store); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1201 + * self.b = bam_init1() + * + * def __iter__(self): # <<<<<<<<<<<<<< + * return self + * + */ + +static PyObject *__pyx_pf_9csamtools_17IteratorRowRegion_1__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_17IteratorRowRegion_1__iter__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__iter__"); + __Pyx_TraceCall("__iter__", __pyx_f[0], 1201); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1202 + * + * def __iter__(self): + * return self # <<<<<<<<<<<<<< + * + * cdef bam1_t * getCurrent( self ): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_self); + __pyx_r = __pyx_v_self; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1204 + * return self + * + * cdef bam1_t * getCurrent( self ): # <<<<<<<<<<<<<< + * return self.b + * + */ + +static bam1_t *__pyx_f_9csamtools_17IteratorRowRegion_getCurrent(struct __pyx_obj_9csamtools_IteratorRowRegion *__pyx_v_self) { + bam1_t *__pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("getCurrent"); + __Pyx_TraceCall("getCurrent", __pyx_f[0], 1204); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1205 + * + * cdef bam1_t * getCurrent( self ): + * return self.b # <<<<<<<<<<<<<< + * + * cdef int cnext(self): + */ + __pyx_r = __pyx_v_self->b; + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1207 + * return self.b + * + * cdef int cnext(self): # <<<<<<<<<<<<<< + * '''cversion of iterator. Used by IteratorColumn''' + * self.retval = bam_iter_read( self.fp.x.bam, + */ + +static int __pyx_f_9csamtools_17IteratorRowRegion_cnext(struct __pyx_obj_9csamtools_IteratorRowRegion *__pyx_v_self) { + int __pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("cnext"); + __Pyx_TraceCall("cnext", __pyx_f[0], 1207); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1209 + * cdef int cnext(self): + * '''cversion of iterator. Used by IteratorColumn''' + * self.retval = bam_iter_read( self.fp.x.bam, # <<<<<<<<<<<<<< + * self.iter, + * self.b) + */ + __pyx_v_self->retval = bam_iter_read(__pyx_v_self->fp->x.bam, __pyx_v_self->iter, __pyx_v_self->b); + + __pyx_r = 0; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1213 + * self.b) + * + * def __next__(self): # <<<<<<<<<<<<<< + * """python version of next(). + * """ + */ + +static PyObject *__pyx_pf_9csamtools_17IteratorRowRegion_2__next__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_17IteratorRowRegion_2__next__[] = "python version of next().\n "; +struct wrapperbase __pyx_wrapperbase_9csamtools_17IteratorRowRegion_2__next__; +static PyObject *__pyx_pf_9csamtools_17IteratorRowRegion_2__next__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__next__"); + __Pyx_TraceCall("__next__", __pyx_f[0], 1213); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1216 + * """python version of next(). + * """ + * self.cnext() # <<<<<<<<<<<<<< + * if self.retval < 0: raise StopIteration + * return makeAlignedRead( self.b ) + */ + ((struct __pyx_vtabstruct_9csamtools_IteratorRowRegion *)((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->__pyx_vtab)->cnext(((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1217 + * """ + * self.cnext() + * if self.retval < 0: raise StopIteration # <<<<<<<<<<<<<< + * return makeAlignedRead( self.b ) + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->retval < 0); + if (__pyx_t_1) { + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1217; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1218 + * self.cnext() + * if self.retval < 0: raise StopIteration + * return makeAlignedRead( self.b ) # <<<<<<<<<<<<<< + * + * def __dealloc__(self): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = __pyx_f_9csamtools_makeAlignedRead(((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1218; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IteratorRowRegion.__next__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1220 + * return makeAlignedRead( self.b ) + * + * def __dealloc__(self): # <<<<<<<<<<<<<< + * bam_destroy1(self.b) + * samclose( self.fp ) + */ + +static void __pyx_pf_9csamtools_17IteratorRowRegion_3__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_17IteratorRowRegion_3__dealloc__(PyObject *__pyx_v_self) { + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1220); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1221 + * + * def __dealloc__(self): + * bam_destroy1(self.b) # <<<<<<<<<<<<<< + * samclose( self.fp ) + * + */ + bam_destroy1(((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->b); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1222 + * def __dealloc__(self): + * bam_destroy1(self.b) + * samclose( self.fp ) # <<<<<<<<<<<<<< + * + * cdef class IteratorRowAll(IteratorRow): + */ + samclose(((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_v_self)->fp); + + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1237 + * cdef samfile_t * fp + * + * def __cinit__(self, Samfile samfile, int reopen = True ): # <<<<<<<<<<<<<< + * + * if not samfile._isOpen(): + */ + +static int __pyx_pf_9csamtools_14IteratorRowAll_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_14IteratorRowAll_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_Samfile *__pyx_v_samfile = 0; + int __pyx_v_reopen; + PyObject *__pyx_v_mode; + PyObject *__pyx_v_store; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + char *__pyx_t_5; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__samfile,&__pyx_n_s__reopen,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 1237); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__samfile); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)values[0]); + if (values[1]) { + __pyx_v_reopen = __Pyx_PyInt_AsInt(values[1]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } else { + __pyx_v_reopen = ((int)1); + } + } else { + __pyx_v_reopen = ((int)1); + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 2: __pyx_v_reopen = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 1)); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + case 1: __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)PyTuple_GET_ITEM(__pyx_args, 0)); + break; + default: goto __pyx_L5_argtuple_error; + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.IteratorRowAll.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + __pyx_v_mode = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_store = Py_None; __Pyx_INCREF(Py_None); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_9csamtools_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1239 + * def __cinit__(self, Samfile samfile, int reopen = True ): + * + * if not samfile._isOpen(): # <<<<<<<<<<<<<< + * raise ValueError( "I/O operation on closed file" ) + * + */ + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_samfile), __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), 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_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 = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1240 + * + * if not samfile._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * if samfile.isbam: mode = "rb" + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_100), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1240; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1240; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1242 + * raise ValueError( "I/O operation on closed file" ) + * + * if samfile.isbam: mode = "rb" # <<<<<<<<<<<<<< + * else: mode = "r" + * + */ + if (__pyx_v_samfile->isbam) { + __Pyx_INCREF(((PyObject *)__pyx_n_s__rb)); + __Pyx_DECREF(__pyx_v_mode); + __pyx_v_mode = ((PyObject *)__pyx_n_s__rb); + goto __pyx_L7; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1243 + * + * if samfile.isbam: mode = "rb" + * else: mode = "r" # <<<<<<<<<<<<<< + * + * # reopen the file to avoid iterator conflict + */ + __Pyx_INCREF(((PyObject *)__pyx_n_s__r)); + __Pyx_DECREF(__pyx_v_mode); + __pyx_v_mode = ((PyObject *)__pyx_n_s__r); + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1246 + * + * # reopen the file to avoid iterator conflict + * if reopen: # <<<<<<<<<<<<<< + * store = StderrStore() + * self.fp = samopen( samfile._filename, mode, NULL ) + */ + if (__pyx_v_reopen) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1247 + * # reopen the file to avoid iterator conflict + * if reopen: + * store = StderrStore() # <<<<<<<<<<<<<< + * self.fp = samopen( samfile._filename, mode, NULL ) + * store.release() + */ + __pyx_t_2 = __Pyx_GetName(__pyx_m, __pyx_n_s__StderrStore); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1247; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1247; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_v_store); + __pyx_v_store = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1248 + * if reopen: + * store = StderrStore() + * self.fp = samopen( samfile._filename, mode, NULL ) # <<<<<<<<<<<<<< + * store.release() + * assert self.fp != NULL + */ + __pyx_t_5 = PyBytes_AsString(__pyx_v_mode); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1248; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_IteratorRowAll *)__pyx_v_self)->fp = samopen(__pyx_v_samfile->_filename, __pyx_t_5, NULL); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1249 + * store = StderrStore() + * self.fp = samopen( samfile._filename, mode, NULL ) + * store.release() # <<<<<<<<<<<<<< + * assert self.fp != NULL + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_store, __pyx_n_s__release); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1249; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1249; __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1250 + * self.fp = samopen( samfile._filename, mode, NULL ) + * store.release() + * assert self.fp != NULL # <<<<<<<<<<<<<< + * + * # allocate memory for alignment + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + if (unlikely(!(((struct __pyx_obj_9csamtools_IteratorRowAll *)__pyx_v_self)->fp != NULL))) { + PyErr_SetNone(PyExc_AssertionError); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1250; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1253 + * + * # allocate memory for alignment + * self.b = calloc(1, sizeof(bam1_t)) # <<<<<<<<<<<<<< + * + * def __iter__(self): + */ + ((struct __pyx_obj_9csamtools_IteratorRowAll *)__pyx_v_self)->b = ((bam1_t *)calloc(1, (sizeof(bam1_t)))); + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IteratorRowAll.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_mode); + __Pyx_DECREF(__pyx_v_store); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1255 + * self.b = calloc(1, sizeof(bam1_t)) + * + * def __iter__(self): # <<<<<<<<<<<<<< + * return self + * + */ + +static PyObject *__pyx_pf_9csamtools_14IteratorRowAll_1__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_14IteratorRowAll_1__iter__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__iter__"); + __Pyx_TraceCall("__iter__", __pyx_f[0], 1255); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1256 + * + * def __iter__(self): + * return self # <<<<<<<<<<<<<< + * + * cdef bam1_t * getCurrent( self ): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_self); + __pyx_r = __pyx_v_self; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1258 + * return self + * + * cdef bam1_t * getCurrent( self ): # <<<<<<<<<<<<<< + * return self.b + * + */ + +static bam1_t *__pyx_f_9csamtools_14IteratorRowAll_getCurrent(struct __pyx_obj_9csamtools_IteratorRowAll *__pyx_v_self) { + bam1_t *__pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("getCurrent"); + __Pyx_TraceCall("getCurrent", __pyx_f[0], 1258); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1259 + * + * cdef bam1_t * getCurrent( self ): + * return self.b # <<<<<<<<<<<<<< + * + * cdef int cnext(self): + */ + __pyx_r = __pyx_v_self->b; + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1261 + * return self.b + * + * cdef int cnext(self): # <<<<<<<<<<<<<< + * '''cversion of iterator. Used by IteratorColumn''' + * cdef int ret + */ + +static int __pyx_f_9csamtools_14IteratorRowAll_cnext(struct __pyx_obj_9csamtools_IteratorRowAll *__pyx_v_self) { + int __pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("cnext"); + __Pyx_TraceCall("cnext", __pyx_f[0], 1261); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1264 + * '''cversion of iterator. Used by IteratorColumn''' + * cdef int ret + * return samread(self.fp, self.b) # <<<<<<<<<<<<<< + * + * def __next__(self): + */ + __pyx_r = samread(__pyx_v_self->fp, __pyx_v_self->b); + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1266 + * return samread(self.fp, self.b) + * + * def __next__(self): # <<<<<<<<<<<<<< + * """python version of next(). + * + */ + +static PyObject *__pyx_pf_9csamtools_14IteratorRowAll_2__next__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_14IteratorRowAll_2__next__[] = "python version of next().\n\n pyrex uses this non-standard name instead of next()\n "; +struct wrapperbase __pyx_wrapperbase_9csamtools_14IteratorRowAll_2__next__; +static PyObject *__pyx_pf_9csamtools_14IteratorRowAll_2__next__(PyObject *__pyx_v_self) { + int __pyx_v_ret; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__next__"); + __Pyx_TraceCall("__next__", __pyx_f[0], 1266); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1272 + * """ + * cdef int ret + * ret = samread(self.fp, self.b) # <<<<<<<<<<<<<< + * if (ret > 0): + * return makeAlignedRead( self.b ) + */ + __pyx_v_ret = samread(((struct __pyx_obj_9csamtools_IteratorRowAll *)__pyx_v_self)->fp, ((struct __pyx_obj_9csamtools_IteratorRowAll *)__pyx_v_self)->b); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1273 + * cdef int ret + * ret = samread(self.fp, self.b) + * if (ret > 0): # <<<<<<<<<<<<<< + * return makeAlignedRead( self.b ) + * else: + */ + __pyx_t_1 = (__pyx_v_ret > 0); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1274 + * ret = samread(self.fp, self.b) + * if (ret > 0): + * return makeAlignedRead( self.b ) # <<<<<<<<<<<<<< + * else: + * raise StopIteration + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = __pyx_f_9csamtools_makeAlignedRead(((struct __pyx_obj_9csamtools_IteratorRowAll *)__pyx_v_self)->b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1274; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1276 + * return makeAlignedRead( self.b ) + * else: + * raise StopIteration # <<<<<<<<<<<<<< + * + * def __dealloc__(self): + */ + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1276; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_L5:; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IteratorRowAll.__next__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1278 + * raise StopIteration + * + * def __dealloc__(self): # <<<<<<<<<<<<<< + * bam_destroy1(self.b) + * samclose( self.fp ) + */ + +static void __pyx_pf_9csamtools_14IteratorRowAll_3__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_14IteratorRowAll_3__dealloc__(PyObject *__pyx_v_self) { + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1278); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1279 + * + * def __dealloc__(self): + * bam_destroy1(self.b) # <<<<<<<<<<<<<< + * samclose( self.fp ) + * + */ + bam_destroy1(((struct __pyx_obj_9csamtools_IteratorRowAll *)__pyx_v_self)->b); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1280 + * def __dealloc__(self): + * bam_destroy1(self.b) + * samclose( self.fp ) # <<<<<<<<<<<<<< + * + * + */ + samclose(((struct __pyx_obj_9csamtools_IteratorRowAll *)__pyx_v_self)->fp); + + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1290 + * cdef IteratorRowRegion rowiter + * + * def __cinit__(self, Samfile samfile): # <<<<<<<<<<<<<< + * assert samfile._isOpen() + * if not samfile._hasIndex(): raise ValueError("no index available for fetch") + */ + +static int __pyx_pf_9csamtools_18IteratorRowAllRefs_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_18IteratorRowAllRefs_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_Samfile *__pyx_v_samfile = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__samfile,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 1290); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__samfile); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1290; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)PyTuple_GET_ITEM(__pyx_args, 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 = 1290; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.IteratorRowAllRefs.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_9csamtools_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1290; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1291 + * + * def __cinit__(self, Samfile samfile): + * assert samfile._isOpen() # <<<<<<<<<<<<<< + * if not samfile._hasIndex(): raise ValueError("no index available for fetch") + * self.samfile = samfile + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_samfile), __pyx_n_s___isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __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 = 1291; __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 = 1291; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1292 + * def __cinit__(self, Samfile samfile): + * assert samfile._isOpen() + * if not samfile._hasIndex(): raise ValueError("no index available for fetch") # <<<<<<<<<<<<<< + * self.samfile = samfile + * self.tid = -1 + */ + __pyx_t_2 = PyObject_GetAttr(((PyObject *)__pyx_v_samfile), __pyx_n_s___hasIndex); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __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 = 1292; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_4 = (!__pyx_t_3); + if (__pyx_t_4) { + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_101), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1293 + * assert samfile._isOpen() + * if not samfile._hasIndex(): raise ValueError("no index available for fetch") + * self.samfile = samfile # <<<<<<<<<<<<<< + * self.tid = -1 + * + */ + __Pyx_INCREF(((PyObject *)__pyx_v_samfile)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile)); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->samfile); + __Pyx_DECREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->samfile)); + ((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->samfile = __pyx_v_samfile; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1294 + * if not samfile._hasIndex(): raise ValueError("no index available for fetch") + * self.samfile = samfile + * self.tid = -1 # <<<<<<<<<<<<<< + * + * def nextiter(self): + */ + ((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->tid = -1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IteratorRowAllRefs.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1296 + * self.tid = -1 + * + * def nextiter(self): # <<<<<<<<<<<<<< + * self.rowiter = IteratorRowRegion(self.samfile, self.tid, 0, 1<<29) + * + */ + +static PyObject *__pyx_pf_9csamtools_18IteratorRowAllRefs_1nextiter(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pf_9csamtools_18IteratorRowAllRefs_1nextiter(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("nextiter"); + __Pyx_TraceCall("nextiter", __pyx_f[0], 1296); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1297 + * + * def nextiter(self): + * self.rowiter = IteratorRowRegion(self.samfile, self.tid, 0, 1<<29) # <<<<<<<<<<<<<< + * + * def __iter__(self): + */ + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->samfile)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->samfile)); + __Pyx_GIVEREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->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 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorRowRegion)), ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_GIVEREF(__pyx_t_1); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->rowiter); + __Pyx_DECREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->rowiter)); + ((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->rowiter = ((struct __pyx_obj_9csamtools_IteratorRowRegion *)__pyx_t_1); + __pyx_t_1 = 0; + + __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("csamtools.IteratorRowAllRefs.nextiter"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1299 + * self.rowiter = IteratorRowRegion(self.samfile, self.tid, 0, 1<<29) + * + * def __iter__(self): # <<<<<<<<<<<<<< + * return self + * + */ + +static PyObject *__pyx_pf_9csamtools_18IteratorRowAllRefs_2__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_18IteratorRowAllRefs_2__iter__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__iter__"); + __Pyx_TraceCall("__iter__", __pyx_f[0], 1299); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1300 + * + * 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; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1302 + * return self + * + * def __next__(self): # <<<<<<<<<<<<<< + * """python version of next(). + * + */ + +static PyObject *__pyx_pf_9csamtools_18IteratorRowAllRefs_3__next__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_18IteratorRowAllRefs_3__next__[] = "python version of next().\n\n pyrex uses this non-standard name instead of next()\n "; +struct wrapperbase __pyx_wrapperbase_9csamtools_18IteratorRowAllRefs_3__next__; +static PyObject *__pyx_pf_9csamtools_18IteratorRowAllRefs_3__next__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_5 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__next__"); + __Pyx_TraceCall("__next__", __pyx_f[0], 1302); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1308 + * """ + * # Create an initial iterator + * if self.tid==-1: # <<<<<<<<<<<<<< + * if not self.samfile.nreferences: + * raise StopIteration + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->tid == -1); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1309 + * # Create an initial iterator + * if self.tid==-1: + * if not self.samfile.nreferences: # <<<<<<<<<<<<<< + * raise StopIteration + * self.tid = 0 + */ + __pyx_t_2 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->samfile), __pyx_n_s__nreferences); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1309; __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 = 1309; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_3 = (!__pyx_t_1); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1310 + * if self.tid==-1: + * if not self.samfile.nreferences: + * raise StopIteration # <<<<<<<<<<<<<< + * self.tid = 0 + * self.nextiter() + */ + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1310; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1311 + * if not self.samfile.nreferences: + * raise StopIteration + * self.tid = 0 # <<<<<<<<<<<<<< + * self.nextiter() + * + */ + ((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->tid = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1312 + * raise StopIteration + * self.tid = 0 + * self.nextiter() # <<<<<<<<<<<<<< + * + * while 1: + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__nextiter); 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_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1312; __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_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1314 + * self.nextiter() + * + * while 1: # <<<<<<<<<<<<<< + * self.rowiter.cnext() + * + */ + while (1) { + if (!1) break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1315 + * + * while 1: + * self.rowiter.cnext() # <<<<<<<<<<<<<< + * + * # If current iterator is not exhausted, return aligned read + */ + ((struct __pyx_vtabstruct_9csamtools_IteratorRowRegion *)((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->rowiter->__pyx_vtab)->cnext(((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->rowiter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1318 + * + * # If current iterator is not exhausted, return aligned read + * if self.rowiter.retval>0: # <<<<<<<<<<<<<< + * return makeAlignedRead(self.rowiter.b) + * + */ + __pyx_t_3 = (((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->rowiter->retval > 0); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1319 + * # 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_9csamtools_makeAlignedRead(((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->rowiter->b); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1319; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_r = __pyx_t_4; + __pyx_t_4 = 0; + goto __pyx_L0; + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1321 + * return makeAlignedRead(self.rowiter.b) + * + * self.tid += 1 # <<<<<<<<<<<<<< + * + * # Otherwise, proceed to next reference or stop + */ + ((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->tid = (((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->tid + 1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1324 + * + * # Otherwise, proceed to next reference or stop + * if self.tidtid); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_2 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)__pyx_v_self)->samfile), __pyx_n_s__nreferences); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __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); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __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_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 = 1324; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1325 + * # Otherwise, proceed to next reference or stop + * if self.tiddata # <<<<<<<<<<<<<< + * return bam_iter_read( d.samfile.x.bam, d.iter, b ) + * + */ + __pyx_v_d = ((__pyx_t_9csamtools___iterdata *)__pyx_v_data); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1345 + * cdef __iterdata * d + * d = <__iterdata*>data + * return bam_iter_read( d.samfile.x.bam, d.iter, b ) # <<<<<<<<<<<<<< + * + * cdef int __advance_snpcalls( void * data, bam1_t * b ): + */ + __pyx_r = bam_iter_read(__pyx_v_d->samfile->x.bam, __pyx_v_d->iter, __pyx_v_b); + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1347 + * return bam_iter_read( d.samfile.x.bam, d.iter, b ) + * + * 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_9csamtools___advance_snpcalls(void *__pyx_v_data, bam1_t *__pyx_v_b) { + __pyx_t_9csamtools___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; + int __pyx_t_1; + int __pyx_t_2; + int __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_5 = NULL; + PyObject *__pyx_t_6 = NULL; + long __pyx_t_7; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__advance_snpcalls"); + __Pyx_TraceCall("__advance_snpcalls", __pyx_f[0], 1347); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1352 + * ''' + * cdef __iterdata * d + * d = <__iterdata*>data # <<<<<<<<<<<<<< + * + * cdef int ret = bam_iter_read( d.samfile.x.bam, d.iter, b ) + */ + __pyx_v_d = ((__pyx_t_9csamtools___iterdata *)__pyx_v_data); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1354 + * d = <__iterdata*>data + * + * cdef int ret = bam_iter_read( d.samfile.x.bam, d.iter, b ) # <<<<<<<<<<<<<< + * cdef int skip = 0 + * cdef int q + */ + __pyx_v_ret = bam_iter_read(__pyx_v_d->samfile->x.bam, __pyx_v_d->iter, __pyx_v_b); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1355 + * + * cdef int ret = bam_iter_read( d.samfile.x.bam, d.iter, b ) + * cdef int skip = 0 # <<<<<<<<<<<<<< + * cdef int q + * cdef int is_cns = 1 + */ + __pyx_v_skip = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1357 + * 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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1358 + * cdef int q + * cdef int is_cns = 1 + * cdef int is_nobaq = 0 # <<<<<<<<<<<<<< + * cdef int capQ_thres = 0 + * + */ + __pyx_v_is_nobaq = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1359 + * cdef int is_cns = 1 + * cdef int is_nobaq = 0 + * cdef int capQ_thres = 0 # <<<<<<<<<<<<<< + * + * # reload sequence + */ + __pyx_v_capQ_thres = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1362 + * + * # 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_1 = (__pyx_v_d->fastafile != NULL); + if (__pyx_t_1) { + __pyx_t_2 = (__pyx_v_b->core.tid != __pyx_v_d->tid); + __pyx_t_3 = __pyx_t_2; + } else { + __pyx_t_3 = __pyx_t_1; + } + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1363 + * # 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, + */ + __pyx_t_3 = (__pyx_v_d->seq != NULL); + if (__pyx_t_3) { + free(__pyx_v_d->seq); + goto __pyx_L4; + } + __pyx_L4:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1364 + * 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.samfile.header.target_name[d.tid], + */ + __pyx_v_d->tid = __pyx_v_b->core.tid; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1365 + * if d.seq != NULL: free(d.seq) + * d.tid = b.core.tid; + * d.seq = faidx_fetch_seq(d.fastafile, # <<<<<<<<<<<<<< + * d.samfile.header.target_name[d.tid], + * 0, max_pos, + */ + __pyx_v_d->seq = faidx_fetch_seq(__pyx_v_d->fastafile, (__pyx_v_d->samfile->header->target_name[__pyx_v_d->tid]), 0, __pyx_v_9csamtools_max_pos, (&__pyx_v_d->seq_len)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1370 + * &d.seq_len) + * + * if d.seq == NULL: # <<<<<<<<<<<<<< + * raise ValueError( "reference sequence for '%s' (tid=%i) not found" % \ + * (d.samfile.header.target_name[d.tid], + */ + __pyx_t_3 = (__pyx_v_d->seq == NULL); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1372 + * if d.seq == NULL: + * raise ValueError( "reference sequence for '%s' (tid=%i) not found" % \ + * (d.samfile.header.target_name[d.tid], # <<<<<<<<<<<<<< + * d.tid)) + * + */ + __pyx_t_4 = PyBytes_FromString((__pyx_v_d->samfile->header->target_name[__pyx_v_d->tid])); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1372; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1373 + * raise ValueError( "reference sequence for '%s' (tid=%i) not found" % \ + * (d.samfile.header.target_name[d.tid], + * d.tid)) # <<<<<<<<<<<<<< + * + * + */ + __pyx_t_5 = PyInt_FromLong(__pyx_v_d->tid); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1373; __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 = 1372; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_t_4)); + __Pyx_GIVEREF(((PyObject *)__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 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_102), ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1371; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __Pyx_DECREF(((PyObject *)__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 = 1371; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_t_5)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_5)); + __pyx_t_5 = 0; + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_6), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1371; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1371; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + goto __pyx_L3; + } + __pyx_L3:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1376 + * + * + * while ret >= 0: # <<<<<<<<<<<<<< + * + * skip = 0 + */ + while (1) { + __pyx_t_3 = (__pyx_v_ret >= 0); + if (!__pyx_t_3) break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1378 + * while ret >= 0: + * + * skip = 0 # <<<<<<<<<<<<<< + * + * # realign read - changes base qualities + */ + __pyx_v_skip = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1381 + * + * # 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: + */ + __pyx_t_3 = (__pyx_v_d->seq != NULL); + if (__pyx_t_3) { + if (__pyx_v_is_cns) { + __pyx_t_1 = (!__pyx_v_is_nobaq); + __pyx_t_2 = __pyx_t_1; + } else { + __pyx_t_2 = __pyx_v_is_cns; + } + __pyx_t_1 = __pyx_t_2; + } else { + __pyx_t_1 = __pyx_t_3; + } + if (__pyx_t_1) { + bam_prob_realn(__pyx_v_b, __pyx_v_d->seq); + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1383 + * 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 + */ + __pyx_t_1 = (__pyx_v_d->seq != NULL); + if (__pyx_t_1) { + __pyx_t_3 = (__pyx_v_capQ_thres > 10); + __pyx_t_2 = __pyx_t_3; + } else { + __pyx_t_2 = __pyx_t_1; + } + if (__pyx_t_2) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1384 + * + * 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 + */ + __pyx_v_q = bam_cap_mapQ(__pyx_v_b, __pyx_v_d->seq, __pyx_v_capQ_thres); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1385 + * 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 + */ + __pyx_t_2 = (__pyx_v_q < 0); + if (__pyx_t_2) { + __pyx_v_skip = 1; + goto __pyx_L10; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1386 + * 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 + */ + __pyx_t_2 = (__pyx_v_b->core.qual > __pyx_v_q); + if (__pyx_t_2) { + __pyx_v_b->core.qual = __pyx_v_q; + goto __pyx_L10; + } + __pyx_L10:; + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1387 + * 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 + * + */ + __pyx_t_7 = (__pyx_v_b->core.flag & 4); + if (__pyx_t_7) { + __pyx_v_skip = 1; + goto __pyx_L11; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1388 + * 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 + */ + if ((__pyx_v_b->core.flag & 1)) { + __pyx_t_2 = (!(__pyx_v_b->core.flag & 2)); + __pyx_t_1 = __pyx_t_2; + } else { + __pyx_t_1 = (__pyx_v_b->core.flag & 1); + } + if (__pyx_t_1) { + __pyx_v_skip = 1; + goto __pyx_L11; + } + __pyx_L11:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1390 + * elif b.core.flag & 1 and not b.core.flag & 2: skip = 1 + * + * if not skip: break # <<<<<<<<<<<<<< + * # additional filters + * + */ + __pyx_t_1 = (!__pyx_v_skip); + if (__pyx_t_1) { + goto __pyx_L7_break; + goto __pyx_L12; + } + __pyx_L12:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1393 + * # additional filters + * + * ret = bam_iter_read( d.samfile.x.bam, d.iter, b ) # <<<<<<<<<<<<<< + * + * return ret + */ + __pyx_v_ret = bam_iter_read(__pyx_v_d->samfile->x.bam, __pyx_v_d->iter, __pyx_v_b); + } + __pyx_L7_break:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1395 + * ret = bam_iter_read( d.samfile.x.bam, d.iter, b ) + * + * return ret # <<<<<<<<<<<<<< + * + * cdef class IteratorColumn: + */ + __pyx_r = __pyx_v_ret; + goto __pyx_L0; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_WriteUnraisable("csamtools.__advance_snpcalls"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1454 + * cdef stepper + * + * def __cinit__( self, Samfile samfile, **kwargs ): # <<<<<<<<<<<<<< + * self.samfile = samfile + * self.mask = kwargs.get("mask", BAM_DEF_MASK ) + */ + +static int __pyx_pf_9csamtools_14IteratorColumn_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_14IteratorColumn_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_Samfile *__pyx_v_samfile = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__samfile,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 1454); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__samfile); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1454; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)PyTuple_GET_ITEM(__pyx_args, 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 = 1454; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.IteratorColumn.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_9csamtools_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1454; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1455 + * + * def __cinit__( self, Samfile samfile, **kwargs ): + * self.samfile = samfile # <<<<<<<<<<<<<< + * self.mask = kwargs.get("mask", BAM_DEF_MASK ) + * self.fastafile = kwargs.get( "fastafile", None ) + */ + __Pyx_INCREF(((PyObject *)__pyx_v_samfile)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile)); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->samfile); + __Pyx_DECREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->samfile)); + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->samfile = __pyx_v_samfile; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1456 + * def __cinit__( self, Samfile samfile, **kwargs ): + * self.samfile = samfile + * self.mask = kwargs.get("mask", BAM_DEF_MASK ) # <<<<<<<<<<<<<< + * self.fastafile = kwargs.get( "fastafile", None ) + * self.stepper = kwargs.get( "stepper", None ) + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1456; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = PyInt_FromLong(BAM_DEF_MASK); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1456; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__mask), __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1456; __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_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1456; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->mask = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1457 + * self.samfile = samfile + * self.mask = kwargs.get("mask", BAM_DEF_MASK ) + * self.fastafile = kwargs.get( "fastafile", None ) # <<<<<<<<<<<<<< + * self.stepper = kwargs.get( "stepper", None ) + * self.iterdata.seq = NULL + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1457; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__fastafile), Py_None); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1457; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_9csamtools_Fastafile))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1457; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GIVEREF(__pyx_t_2); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile); + __Pyx_DECREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile)); + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile = ((struct __pyx_obj_9csamtools_Fastafile *)__pyx_t_2); + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1458 + * self.mask = kwargs.get("mask", BAM_DEF_MASK ) + * self.fastafile = kwargs.get( "fastafile", None ) + * self.stepper = kwargs.get( "stepper", None ) # <<<<<<<<<<<<<< + * self.iterdata.seq = NULL + * self.tid = 0 + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1458; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__stepper), Py_None); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1458; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_GIVEREF(__pyx_t_2); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->stepper); + __Pyx_DECREF(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->stepper); + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->stepper = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1459 + * self.fastafile = kwargs.get( "fastafile", None ) + * self.stepper = kwargs.get( "stepper", None ) + * self.iterdata.seq = NULL # <<<<<<<<<<<<<< + * self.tid = 0 + * self.pos = 0 + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.seq = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1460 + * self.stepper = kwargs.get( "stepper", None ) + * self.iterdata.seq = NULL + * self.tid = 0 # <<<<<<<<<<<<<< + * self.pos = 0 + * self.n_plp = 0 + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->tid = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1461 + * self.iterdata.seq = NULL + * self.tid = 0 + * self.pos = 0 # <<<<<<<<<<<<<< + * self.n_plp = 0 + * self.plp = NULL + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->pos = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1462 + * self.tid = 0 + * self.pos = 0 + * self.n_plp = 0 # <<<<<<<<<<<<<< + * self.plp = NULL + * self.pileup_iter = NULL + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->n_plp = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1463 + * self.pos = 0 + * self.n_plp = 0 + * self.plp = NULL # <<<<<<<<<<<<<< + * self.pileup_iter = NULL + * + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->plp = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1464 + * self.n_plp = 0 + * self.plp = NULL + * self.pileup_iter = NULL # <<<<<<<<<<<<<< + * + * def __iter__(self): + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->pileup_iter = ((bam_plp_t)NULL); + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IteratorColumn.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1466 + * self.pileup_iter = NULL + * + * def __iter__(self): # <<<<<<<<<<<<<< + * return self + * + */ + +static PyObject *__pyx_pf_9csamtools_14IteratorColumn_1__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_14IteratorColumn_1__iter__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__iter__"); + __Pyx_TraceCall("__iter__", __pyx_f[0], 1466); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1467 + * + * def __iter__(self): + * return self # <<<<<<<<<<<<<< + * + * cdef int cnext(self): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_self); + __pyx_r = __pyx_v_self; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1469 + * return self + * + * cdef int cnext(self): # <<<<<<<<<<<<<< + * '''perform next iteration. + * + */ + +static int __pyx_f_9csamtools_14IteratorColumn_cnext(struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_self) { + int __pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("cnext"); + __Pyx_TraceCall("cnext", __pyx_f[0], 1469); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1475 + * It has been re-implemented to permit for filtering. + * ''' + * 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)); + + __pyx_r = 0; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1480 + * &self.n_plp ) + * + * cdef char * getSequence( self ): # <<<<<<<<<<<<<< + * '''return current reference sequence underlying the iterator. + * ''' + */ + +static char *__pyx_f_9csamtools_14IteratorColumn_getSequence(struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_self) { + char *__pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("getSequence"); + __Pyx_TraceCall("getSequence", __pyx_f[0], 1480); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1483 + * '''return current reference sequence underlying the iterator. + * ''' + * return self.iterdata.seq # <<<<<<<<<<<<<< + * + * property seq_len: + */ + __pyx_r = __pyx_v_self->iterdata.seq; + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1487 + * property seq_len: + * '''current sequence length.''' + * def __get__(self): return self.iterdata.seq_len # <<<<<<<<<<<<<< + * + * def addReference( self, Fastafile fastafile ): + */ + +static PyObject *__pyx_pf_9csamtools_14IteratorColumn_7seq_len_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_14IteratorColumn_7seq_len_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 1487); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.seq_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1487; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IteratorColumn.seq_len.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1489 + * def __get__(self): return self.iterdata.seq_len + * + * def addReference( self, Fastafile fastafile ): # <<<<<<<<<<<<<< + * ''' + * add reference sequences in *fastafile* to iterator.''' + */ + +static PyObject *__pyx_pf_9csamtools_14IteratorColumn_2addReference(PyObject *__pyx_v_self, PyObject *__pyx_v_fastafile); /*proto*/ +static char __pyx_doc_9csamtools_14IteratorColumn_2addReference[] = "\n add reference sequences in *fastafile* to iterator."; +static PyObject *__pyx_pf_9csamtools_14IteratorColumn_2addReference(PyObject *__pyx_v_self, PyObject *__pyx_v_fastafile) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("addReference"); + __Pyx_TraceCall("addReference", __pyx_f[0], 1489); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_fastafile), __pyx_ptype_9csamtools_Fastafile, 1, "fastafile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1489; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1492 + * ''' + * 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(__pyx_v_fastafile); + __Pyx_GIVEREF(__pyx_v_fastafile); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile); + __Pyx_DECREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile)); + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile = ((struct __pyx_obj_9csamtools_Fastafile *)__pyx_v_fastafile); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1493 + * 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 = (((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.seq != NULL); + if (__pyx_t_1) { + free(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.seq); + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1494 + * self.fastafile = fastafile + * if self.iterdata.seq != NULL: free(self.iterdata.seq) + * self.iterdata.tid = -1 # <<<<<<<<<<<<<< + * self.iterdata.fastafile = self.fastafile.fastafile + * + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.tid = -1; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1495 + * if self.iterdata.seq != NULL: free(self.iterdata.seq) + * self.iterdata.tid = -1 + * self.iterdata.fastafile = self.fastafile.fastafile # <<<<<<<<<<<<<< + * + * def hasReference( self ): + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.fastafile = ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile->fastafile; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.IteratorColumn.addReference"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1497 + * self.iterdata.fastafile = self.fastafile.fastafile + * + * def hasReference( self ): # <<<<<<<<<<<<<< + * ''' + * return true if iterator is associated with a reference''' + */ + +static PyObject *__pyx_pf_9csamtools_14IteratorColumn_3hasReference(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static char __pyx_doc_9csamtools_14IteratorColumn_3hasReference[] = "\n return true if iterator is associated with a reference"; +static PyObject *__pyx_pf_9csamtools_14IteratorColumn_3hasReference(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("hasReference"); + __Pyx_TraceCall("hasReference", __pyx_f[0], 1497); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1500 + * ''' + * return true if iterator is associated with a reference''' + * return self.fastafile # <<<<<<<<<<<<<< + * + * cdef setMask( self, mask ): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile)); + __pyx_r = ((PyObject *)((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->fastafile); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1502 + * return self.fastafile + * + * cdef setMask( self, mask ): # <<<<<<<<<<<<<< + * '''set masking flag in iterator. + * + */ + +static PyObject *__pyx_f_9csamtools_14IteratorColumn_setMask(struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_self, PyObject *__pyx_v_mask) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("setMask"); + __Pyx_TraceCall("setMask", __pyx_f[0], 1502); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1507 + * reads with bits set in *mask* will be skipped. + * ''' + * self.mask = mask # <<<<<<<<<<<<<< + * bam_plp_set_mask( self.pileup_iter, self.mask ) + * + */ + __pyx_t_1 = __Pyx_PyInt_AsInt(__pyx_v_mask); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1507; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_self->mask = __pyx_t_1; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1508 + * ''' + * self.mask = mask + * bam_plp_set_mask( self.pileup_iter, self.mask ) # <<<<<<<<<<<<<< + * + * cdef setupIteratorData( self, + */ + bam_plp_set_mask(__pyx_v_self->pileup_iter, __pyx_v_self->mask); + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.IteratorColumn.setMask"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1510 + * bam_plp_set_mask( self.pileup_iter, self.mask ) + * + * cdef setupIteratorData( self, # <<<<<<<<<<<<<< + * int tid, + * int start, + */ + +static PyObject *__pyx_f_9csamtools_14IteratorColumn_setupIteratorData(struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_self, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, struct __pyx_opt_args_9csamtools_14IteratorColumn_setupIteratorData *__pyx_optional_args) { + int __pyx_v_reopen = ((int)0); + PyObject *__pyx_r = NULL; + 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; + int __pyx_t_6; + int __pyx_t_7; + int __pyx_t_8; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("setupIteratorData"); + __Pyx_TraceCall("setupIteratorData", __pyx_f[0], 1510); + if (__pyx_optional_args) { + if (__pyx_optional_args->__pyx_n > 0) { + __pyx_v_reopen = __pyx_optional_args->reopen; + } + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1517 + * '''setup the iterator structure''' + * + * self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen ) # <<<<<<<<<<<<<< + * self.iterdata.samfile = self.samfile.samfile + * self.iterdata.iter = self.iter.iter + */ + __pyx_t_1 = PyInt_FromLong(__pyx_v_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyInt_FromLong(__pyx_v_start); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyInt_FromLong(__pyx_v_end); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_4 = PyInt_FromLong(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1517; __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 = 1517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorRowRegion)), ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__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_9csamtools_IteratorRowRegion *)__pyx_t_4); + __pyx_t_4 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1518 + * + * self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen ) + * self.iterdata.samfile = self.samfile.samfile # <<<<<<<<<<<<<< + * self.iterdata.iter = self.iter.iter + * self.iterdata.seq = NULL + */ + __pyx_v_self->iterdata.samfile = __pyx_v_self->samfile->samfile; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1519 + * self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen ) + * self.iterdata.samfile = self.samfile.samfile + * self.iterdata.iter = self.iter.iter # <<<<<<<<<<<<<< + * self.iterdata.seq = NULL + * self.iterdata.tid = -1 + */ + __pyx_v_self->iterdata.iter = __pyx_v_self->iter->iter; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1520 + * self.iterdata.samfile = self.samfile.samfile + * self.iterdata.iter = self.iter.iter + * self.iterdata.seq = NULL # <<<<<<<<<<<<<< + * self.iterdata.tid = -1 + * + */ + __pyx_v_self->iterdata.seq = NULL; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1521 + * self.iterdata.iter = self.iter.iter + * self.iterdata.seq = NULL + * self.iterdata.tid = -1 # <<<<<<<<<<<<<< + * + * if self.fastafile != None: + */ + __pyx_v_self->iterdata.tid = -1; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1523 + * self.iterdata.tid = -1 + * + * 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); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1523; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1523; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + if (__pyx_t_6) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1524 + * + * if self.fastafile != None: + * self.iterdata.fastafile = self.fastafile.fastafile # <<<<<<<<<<<<<< + * else: + * self.iterdata.fastafile = NULL + */ + __pyx_v_self->iterdata.fastafile = __pyx_v_self->fastafile->fastafile; + goto __pyx_L3; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1526 + * 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:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1528 + * self.iterdata.fastafile = NULL + * + * if self.stepper == None or self.stepper == "all": # <<<<<<<<<<<<<< + * self.pileup_iter = bam_plp_init( &__advance_all, &self.iterdata ) + * elif self.stepper == "samtools": + */ + __pyx_t_4 = PyObject_RichCompare(__pyx_v_self->stepper, Py_None, Py_EQ); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1528; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1528; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + if (!__pyx_t_6) { + __pyx_t_4 = PyObject_RichCompare(__pyx_v_self->stepper, ((PyObject *)__pyx_n_s__all), Py_EQ); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1528; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1528; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_t_8 = __pyx_t_7; + } else { + __pyx_t_8 = __pyx_t_6; + } + if (__pyx_t_8) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1529 + * + * if self.stepper == None or self.stepper == "all": + * self.pileup_iter = bam_plp_init( &__advance_all, &self.iterdata ) # <<<<<<<<<<<<<< + * elif self.stepper == "samtools": + * self.pileup_iter = bam_plp_init( &__advance_snpcalls, &self.iterdata ) + */ + __pyx_v_self->pileup_iter = bam_plp_init((&__pyx_f_9csamtools___advance_all), (&__pyx_v_self->iterdata)); + goto __pyx_L4; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1530 + * if self.stepper == None or self.stepper == "all": + * self.pileup_iter = bam_plp_init( &__advance_all, &self.iterdata ) + * elif self.stepper == "samtools": # <<<<<<<<<<<<<< + * self.pileup_iter = bam_plp_init( &__advance_snpcalls, &self.iterdata ) + * else: + */ + __pyx_t_4 = PyObject_RichCompare(__pyx_v_self->stepper, ((PyObject *)__pyx_n_s__samtools), Py_EQ); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1530; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1530; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + if (__pyx_t_8) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1531 + * self.pileup_iter = bam_plp_init( &__advance_all, &self.iterdata ) + * elif self.stepper == "samtools": + * self.pileup_iter = bam_plp_init( &__advance_snpcalls, &self.iterdata ) # <<<<<<<<<<<<<< + * else: + * raise ValueError( "unknown stepper option `%s` in IteratorColumn" % self.stepper) + */ + __pyx_v_self->pileup_iter = bam_plp_init((&__pyx_f_9csamtools___advance_snpcalls), (&__pyx_v_self->iterdata)); + goto __pyx_L4; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1533 + * self.pileup_iter = bam_plp_init( &__advance_snpcalls, &self.iterdata ) + * else: + * raise ValueError( "unknown stepper option `%s` in IteratorColumn" % self.stepper) # <<<<<<<<<<<<<< + * + * bam_plp_set_mask( self.pileup_iter, self.mask ) + */ + __pyx_t_4 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_103), __pyx_v_self->stepper); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1533; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1533; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_4)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); + __pyx_t_4 = 0; + __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1533; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __Pyx_Raise(__pyx_t_4, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1533; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_L4:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1535 + * raise ValueError( "unknown stepper option `%s` in IteratorColumn" % self.stepper) + * + * bam_plp_set_mask( self.pileup_iter, self.mask ) # <<<<<<<<<<<<<< + * + * cdef reset( self, tid, start, end ): + */ + bam_plp_set_mask(__pyx_v_self->pileup_iter, __pyx_v_self->mask); + + __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("csamtools.IteratorColumn.setupIteratorData"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1537 + * bam_plp_set_mask( self.pileup_iter, self.mask ) + * + * cdef reset( self, tid, start, end ): # <<<<<<<<<<<<<< + * '''reset iterator position. + * + */ + +static PyObject *__pyx_f_9csamtools_14IteratorColumn_reset(struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_self, PyObject *__pyx_v_tid, PyObject *__pyx_v_start, PyObject *__pyx_v_end) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("reset"); + __Pyx_TraceCall("reset", __pyx_f[0], 1537); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1543 + * 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 = 1543; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = 1543; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__reopen), __pyx_int_0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1543; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = PyEval_CallObjectWithKeywords(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IteratorRowRegion)), ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1543; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__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_9csamtools_IteratorRowRegion *)__pyx_t_3); + __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1544 + * ''' + * self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen = 0 ) + * self.iterdata.iter = self.iter.iter # <<<<<<<<<<<<<< + * + * # invalidate sequence if different tid + */ + __pyx_v_self->iterdata.iter = __pyx_v_self->iter->iter; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1547 + * + * # 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 = PyInt_FromLong(__pyx_v_self->tid); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1547; __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); 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_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 = 1547; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1548 + * # 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_4 = (__pyx_v_self->iterdata.seq != NULL); + if (__pyx_t_4) { + free(__pyx_v_self->iterdata.seq); + goto __pyx_L4; + } + __pyx_L4:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1549 + * 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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1550 + * 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:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1553 + * + * # 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); + + __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("csamtools.IteratorColumn.reset"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1555 + * 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 + */ + +static void __pyx_pf_9csamtools_14IteratorColumn_4__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_14IteratorColumn_4__dealloc__(PyObject *__pyx_v_self) { + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1555); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1558 + * # reset in order to avoid memory leak messages for iterators that have + * # not been fully consumed + * if self.pileup_iter != NULL: # <<<<<<<<<<<<<< + * bam_plp_reset(self.pileup_iter) + * bam_plp_destroy(self.pileup_iter) + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->pileup_iter != ((bam_plp_t)NULL)); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1559 + * # not been fully consumed + * if self.pileup_iter != NULL: + * bam_plp_reset(self.pileup_iter) # <<<<<<<<<<<<<< + * bam_plp_destroy(self.pileup_iter) + * self.pileup_iter = NULL + */ + bam_plp_reset(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->pileup_iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1560 + * if self.pileup_iter != NULL: + * bam_plp_reset(self.pileup_iter) + * bam_plp_destroy(self.pileup_iter) # <<<<<<<<<<<<<< + * self.pileup_iter = NULL + * + */ + bam_plp_destroy(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->pileup_iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1561 + * bam_plp_reset(self.pileup_iter) + * bam_plp_destroy(self.pileup_iter) + * self.pileup_iter = NULL # <<<<<<<<<<<<<< + * + * if self.iterdata.seq != NULL: + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->pileup_iter = ((bam_plp_t)NULL); + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1563 + * self.pileup_iter = NULL + * + * if self.iterdata.seq != NULL: # <<<<<<<<<<<<<< + * free(self.iterdata.seq) + * self.iterdata.seq = NULL + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.seq != NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1564 + * + * if self.iterdata.seq != NULL: + * free(self.iterdata.seq) # <<<<<<<<<<<<<< + * self.iterdata.seq = NULL + * + */ + free(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.seq); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1565 + * if self.iterdata.seq != NULL: + * free(self.iterdata.seq) + * self.iterdata.seq = NULL # <<<<<<<<<<<<<< + * + * cdef class IteratorColumnRegion(IteratorColumn): + */ + ((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)->iterdata.seq = NULL; + goto __pyx_L6; + } + __pyx_L6:; + + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1570 + * '''iterates over a region only. + * ''' + * def __cinit__(self, Samfile samfile, # <<<<<<<<<<<<<< + * int tid = 0, + * int start = 0, + */ + +static int __pyx_pf_9csamtools_20IteratorColumnRegion_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_20IteratorColumnRegion_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_Samfile *__pyx_v_samfile = 0; + int __pyx_v_tid; + int __pyx_v_start; + int __pyx_v_end; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + struct __pyx_opt_args_9csamtools_14IteratorColumn_setupIteratorData __pyx_t_2; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__samfile,&__pyx_n_s__tid,&__pyx_n_s__start,&__pyx_n_s__end,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 1570); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[4] = {0,0,0,0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__samfile); + if (likely(values[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--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1570; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)values[0]); + if (values[1]) { + __pyx_v_tid = __Pyx_PyInt_AsInt(values[1]); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } else { + __pyx_v_tid = ((int)0); + } + if (values[2]) { + __pyx_v_start = __Pyx_PyInt_AsInt(values[2]); if (unlikely((__pyx_v_start == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1572; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } else { + __pyx_v_start = ((int)0); + } + if (values[3]) { + __pyx_v_end = __Pyx_PyInt_AsInt(values[3]); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1573; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } else { + __pyx_v_end = __pyx_k_104; + } + } else { + __pyx_v_tid = ((int)0); + __pyx_v_start = ((int)0); + __pyx_v_end = __pyx_k_104; + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 4: __pyx_v_end = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 3)); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1573; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + case 3: __pyx_v_start = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 2)); if (unlikely((__pyx_v_start == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1572; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + case 2: __pyx_v_tid = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 1)); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + case 1: __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)PyTuple_GET_ITEM(__pyx_args, 0)); + break; + default: goto __pyx_L5_argtuple_error; + } + } + 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 = 1570; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.IteratorColumnRegion.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_9csamtools_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1577 + * + * # initialize iterator + * self.setupIteratorData( tid, start, end, 1 ) # <<<<<<<<<<<<<< + * + * def __next__(self): + */ + __pyx_t_2.__pyx_n = 1; + __pyx_t_2.reopen = 1; + __pyx_t_1 = ((struct __pyx_vtabstruct_9csamtools_IteratorColumnRegion *)((struct __pyx_obj_9csamtools_IteratorColumnRegion *)__pyx_v_self)->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_9csamtools_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 = 1577; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IteratorColumnRegion.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1579 + * self.setupIteratorData( tid, start, end, 1 ) + * + * def __next__(self): # <<<<<<<<<<<<<< + * """python version of next(). + * """ + */ + +static PyObject *__pyx_pf_9csamtools_20IteratorColumnRegion_1__next__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_20IteratorColumnRegion_1__next__[] = "python version of next().\n "; +struct wrapperbase __pyx_wrapperbase_9csamtools_20IteratorColumnRegion_1__next__; +static PyObject *__pyx_pf_9csamtools_20IteratorColumnRegion_1__next__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__next__"); + __Pyx_TraceCall("__next__", __pyx_f[0], 1579); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1583 + * """ + * + * while 1: # <<<<<<<<<<<<<< + * self.cnext() + * if self.n_plp < 0: + */ + while (1) { + if (!1) break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1584 + * + * while 1: + * self.cnext() # <<<<<<<<<<<<<< + * if self.n_plp < 0: + * raise ValueError("error during iteration" ) + */ + ((struct __pyx_vtabstruct_9csamtools_IteratorColumnRegion *)((struct __pyx_obj_9csamtools_IteratorColumnRegion *)__pyx_v_self)->__pyx_base.__pyx_vtab)->__pyx_base.cnext(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1585 + * while 1: + * self.cnext() + * if self.n_plp < 0: # <<<<<<<<<<<<<< + * raise ValueError("error during iteration" ) + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorColumnRegion *)__pyx_v_self)->__pyx_base.n_plp < 0); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1586 + * self.cnext() + * if self.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.plp == NULL: + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_106), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1586; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1586; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1588 + * raise ValueError("error during iteration" ) + * + * if self.plp == NULL: # <<<<<<<<<<<<<< + * raise StopIteration + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorColumnRegion *)__pyx_v_self)->__pyx_base.plp == NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1589 + * + * if self.plp == NULL: + * raise StopIteration # <<<<<<<<<<<<<< + * + * return makePileupProxy( self.plp, + */ + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1589; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1591 + * raise StopIteration + * + * return makePileupProxy( self.plp, # <<<<<<<<<<<<<< + * self.tid, + * self.pos, + */ + __Pyx_XDECREF(__pyx_r); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1594 + * self.tid, + * self.pos, + * self.n_plp ) # <<<<<<<<<<<<<< + * + * cdef class IteratorColumnAllRefs(IteratorColumn): + */ + __pyx_t_2 = __pyx_f_9csamtools_makePileupProxy(((bam_pileup1_t *)((struct __pyx_obj_9csamtools_IteratorColumnRegion *)__pyx_v_self)->__pyx_base.plp), ((struct __pyx_obj_9csamtools_IteratorColumnRegion *)__pyx_v_self)->__pyx_base.tid, ((struct __pyx_obj_9csamtools_IteratorColumnRegion *)__pyx_v_self)->__pyx_base.pos, ((struct __pyx_obj_9csamtools_IteratorColumnRegion *)__pyx_v_self)->__pyx_base.n_plp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1591; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IteratorColumnRegion.__next__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1600 + * """ + * + * def __cinit__(self, # <<<<<<<<<<<<<< + * Samfile samfile, + * **kwargs ): + */ + +static int __pyx_pf_9csamtools_21IteratorColumnAllRefs_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_21IteratorColumnAllRefs_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_Samfile *__pyx_v_samfile = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + int __pyx_t_3; + struct __pyx_opt_args_9csamtools_14IteratorColumn_setupIteratorData __pyx_t_4; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__samfile,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 1600); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__samfile); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1600; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_samfile = ((struct __pyx_obj_9csamtools_Samfile *)PyTuple_GET_ITEM(__pyx_args, 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 = 1600; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.IteratorColumnAllRefs.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_9csamtools_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1601; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1605 + * + * # no iteration over empty files + * if not samfile.nreferences: raise StopIteration # <<<<<<<<<<<<<< + * + * # initialize iterator + */ + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_samfile), __pyx_n_s__nreferences); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1605; __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 = 1605; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_3 = (!__pyx_t_2); + if (__pyx_t_3) { + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1605; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1608 + * + * # 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_9csamtools_IteratorColumnAllRefs *)((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self), ((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.tid, 0, __pyx_v_9csamtools_max_pos, &__pyx_t_4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1608; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IteratorColumnAllRefs.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1610 + * self.setupIteratorData( self.tid, 0, max_pos, 1 ) + * + * def __next__(self): # <<<<<<<<<<<<<< + * """python version of next(). + * """ + */ + +static PyObject *__pyx_pf_9csamtools_21IteratorColumnAllRefs_1__next__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_21IteratorColumnAllRefs_1__next__[] = "python version of next().\n "; +struct wrapperbase __pyx_wrapperbase_9csamtools_21IteratorColumnAllRefs_1__next__; +static PyObject *__pyx_pf_9csamtools_21IteratorColumnAllRefs_1__next__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + struct __pyx_opt_args_9csamtools_14IteratorColumn_setupIteratorData __pyx_t_5; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__next__"); + __Pyx_TraceCall("__next__", __pyx_f[0], 1610); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1614 + * """ + * + * while 1: # <<<<<<<<<<<<<< + * self.cnext() + * + */ + while (1) { + if (!1) break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1615 + * + * while 1: + * self.cnext() # <<<<<<<<<<<<<< + * + * if self.n_plp < 0: + */ + ((struct __pyx_vtabstruct_9csamtools_IteratorColumnAllRefs *)((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.__pyx_vtab)->__pyx_base.cnext(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1617 + * self.cnext() + * + * if self.n_plp < 0: # <<<<<<<<<<<<<< + * raise ValueError("error during iteration" ) + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.n_plp < 0); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1618 + * + * if self.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * # return result, if within same reference + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_107), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1618; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1618; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1621 + * + * # return result, if within same reference + * if self.plp != NULL: # <<<<<<<<<<<<<< + * return makePileupProxy( self.plp, + * self.tid, + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.plp != NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1622 + * # return result, if within same reference + * if self.plp != NULL: + * return makePileupProxy( self.plp, # <<<<<<<<<<<<<< + * self.tid, + * self.pos, + */ + __Pyx_XDECREF(__pyx_r); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1625 + * self.tid, + * self.pos, + * self.n_plp ) # <<<<<<<<<<<<<< + * + * # otherwise, proceed to next reference or stop + */ + __pyx_t_2 = __pyx_f_9csamtools_makePileupProxy(((bam_pileup1_t *)((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.plp), ((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.tid, ((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.pos, ((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.n_plp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1622; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1628 + * + * # otherwise, proceed to next reference or stop + * self.tid += 1 # <<<<<<<<<<<<<< + * if self.tid < self.samfile.nreferences: + * self.setupIteratorData( self.tid, 0, max_pos, 0 ) + */ + ((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.tid = (((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.tid + 1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1629 + * # 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 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1629; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.samfile), __pyx_n_s__nreferences); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1629; __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); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1629; __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_1 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1629; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1630 + * 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_9csamtools_IteratorColumnAllRefs *)((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_9csamtools_IteratorColumn *)__pyx_v_self), ((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)__pyx_v_self)->__pyx_base.tid, 0, __pyx_v_9csamtools_max_pos, &__pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1630; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + goto __pyx_L9; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1632 + * self.setupIteratorData( self.tid, 0, max_pos, 0 ) + * else: + * raise StopIteration # <<<<<<<<<<<<<< + * + * ##------------------------------------------------------------------- + */ + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_L9:; + } + + __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("csamtools.IteratorColumnAllRefs.__next__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1637 + * ##------------------------------------------------------------------- + * ##------------------------------------------------------------------- + * cdef inline int32_t query_start(bam1_t *src) except -1: # <<<<<<<<<<<<<< + * cdef uint32_t * cigar_p, op + * cdef uint32_t k + */ + +static CYTHON_INLINE int32_t __pyx_f_9csamtools_query_start(bam1_t *__pyx_v_src) { + uint32_t *__pyx_v_cigar_p; + uint32_t __pyx_v_op; + uint32_t __pyx_v_k; + uint32_t __pyx_v_start_offset; + int32_t __pyx_r; + uint32_t __pyx_t_1; + int __pyx_t_2; + int __pyx_t_3; + int __pyx_t_4; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("query_start"); + __Pyx_TraceCall("query_start", __pyx_f[0], 1637); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1640 + * cdef uint32_t * cigar_p, op + * cdef uint32_t k + * cdef uint32_t start_offset = 0 # <<<<<<<<<<<<<< + * + * if src.core.n_cigar: + */ + __pyx_v_start_offset = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1642 + * cdef uint32_t start_offset = 0 + * + * if src.core.n_cigar: # <<<<<<<<<<<<<< + * cigar_p = bam1_cigar(src); + * for k from 0 <= k < src.core.n_cigar: + */ + if (__pyx_v_src->core.n_cigar) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1643 + * + * if src.core.n_cigar: + * cigar_p = bam1_cigar(src); # <<<<<<<<<<<<<< + * for k from 0 <= k < src.core.n_cigar: + * op = cigar_p[k] & BAM_CIGAR_MASK + */ + __pyx_v_cigar_p = bam1_cigar(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1644 + * if src.core.n_cigar: + * cigar_p = bam1_cigar(src); + * for k from 0 <= k < src.core.n_cigar: # <<<<<<<<<<<<<< + * op = cigar_p[k] & BAM_CIGAR_MASK + * if op==BAM_CHARD_CLIP: + */ + __pyx_t_1 = __pyx_v_src->core.n_cigar; + for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_1; __pyx_v_k++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1645 + * cigar_p = bam1_cigar(src); + * for k from 0 <= k < src.core.n_cigar: + * 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]) & 15); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1650 + * PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string') + * return -1 + * elif op==BAM_CSOFT_CLIP: # <<<<<<<<<<<<<< + * start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT + * else: + */ + switch (__pyx_v_op) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1646 + * for k from 0 <= k < src.core.n_cigar: + * 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') + */ + case 5: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1647 + * 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_2 = (__pyx_v_start_offset != 0); + if (__pyx_t_2) { + __pyx_t_3 = (__pyx_v_start_offset != __pyx_v_src->core.l_qseq); + __pyx_t_4 = __pyx_t_3; + } else { + __pyx_t_4 = __pyx_t_2; + } + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1648 + * 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_108); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1649 + * 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; + } + __pyx_L6:; + break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1650 + * PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string') + * return -1 + * elif op==BAM_CSOFT_CLIP: # <<<<<<<<<<<<<< + * start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT + * else: + */ + case 4: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1651 + * 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]) >> 4)); + break; + default: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1653 + * start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT + * else: + * break # <<<<<<<<<<<<<< + * + * return start_offset + */ + goto __pyx_L5_break; + break; + } + } + __pyx_L5_break:; + goto __pyx_L3; + } + __pyx_L3:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1655 + * break + * + * return start_offset # <<<<<<<<<<<<<< + * + * ##------------------------------------------------------------------- + */ + __pyx_r = __pyx_v_start_offset; + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1660 + * ##------------------------------------------------------------------- + * ##------------------------------------------------------------------- + * cdef inline int32_t query_end(bam1_t *src) except -1: # <<<<<<<<<<<<<< + * cdef uint32_t * cigar_p, op + * cdef uint32_t k + */ + +static CYTHON_INLINE int32_t __pyx_f_9csamtools_query_end(bam1_t *__pyx_v_src) { + uint32_t *__pyx_v_cigar_p; + uint32_t __pyx_v_op; + uint32_t __pyx_v_k; + uint32_t __pyx_v_end_offset; + int32_t __pyx_r; + int __pyx_t_1; + int __pyx_t_2; + int __pyx_t_3; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("query_end"); + __Pyx_TraceCall("query_end", __pyx_f[0], 1660); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1663 + * cdef uint32_t * cigar_p, op + * cdef uint32_t k + * cdef uint32_t end_offset = src.core.l_qseq # <<<<<<<<<<<<<< + * + * if src.core.n_cigar>1: + */ + __pyx_v_end_offset = __pyx_v_src->core.l_qseq; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1665 + * cdef uint32_t end_offset = src.core.l_qseq + * + * if src.core.n_cigar>1: # <<<<<<<<<<<<<< + * cigar_p = bam1_cigar(src); + * for k from src.core.n_cigar > k >= 1: + */ + __pyx_t_1 = (__pyx_v_src->core.n_cigar > 1); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1666 + * + * if src.core.n_cigar>1: + * cigar_p = bam1_cigar(src); # <<<<<<<<<<<<<< + * for k from src.core.n_cigar > k >= 1: + * op = cigar_p[k] & BAM_CIGAR_MASK + */ + __pyx_v_cigar_p = bam1_cigar(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1667 + * if src.core.n_cigar>1: + * cigar_p = bam1_cigar(src); + * for k from src.core.n_cigar > k >= 1: # <<<<<<<<<<<<<< + * op = cigar_p[k] & BAM_CIGAR_MASK + * if op==BAM_CHARD_CLIP: + */ + for (__pyx_v_k = __pyx_v_src->core.n_cigar-1; __pyx_v_k >= 1; __pyx_v_k--) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1668 + * cigar_p = bam1_cigar(src); + * for k from src.core.n_cigar > 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]) & 15); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1673 + * PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string') + * return -1 + * elif op==BAM_CSOFT_CLIP: # <<<<<<<<<<<<<< + * end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT + * else: + */ + switch (__pyx_v_op) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1669 + * for k from src.core.n_cigar > 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') + */ + case 5: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1670 + * 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 + */ + __pyx_t_1 = (__pyx_v_end_offset != 0); + if (__pyx_t_1) { + __pyx_t_2 = (__pyx_v_end_offset != __pyx_v_src->core.l_qseq); + __pyx_t_3 = __pyx_t_2; + } else { + __pyx_t_3 = __pyx_t_1; + } + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1671 + * 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: + */ + PyErr_SetString(__pyx_builtin_ValueError, __pyx_k_108); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1672 + * 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 + */ + __pyx_r = -1; + goto __pyx_L0; + goto __pyx_L6; + } + __pyx_L6:; + break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1673 + * PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string') + * return -1 + * elif op==BAM_CSOFT_CLIP: # <<<<<<<<<<<<<< + * end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT + * else: + */ + case 4: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1674 + * 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]) >> 4)); + break; + default: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1676 + * end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT + * else: + * break # <<<<<<<<<<<<<< + * + * if end_offset==0: + */ + goto __pyx_L5_break; + break; + } + } + __pyx_L5_break:; + goto __pyx_L3; + } + __pyx_L3:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1678 + * break + * + * if end_offset==0: # <<<<<<<<<<<<<< + * end_offset = src.core.l_qseq + * + */ + __pyx_t_3 = (__pyx_v_end_offset == 0); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1679 + * + * if end_offset==0: + * end_offset = src.core.l_qseq # <<<<<<<<<<<<<< + * + * return end_offset + */ + __pyx_v_end_offset = __pyx_v_src->core.l_qseq; + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1681 + * end_offset = src.core.l_qseq + * + * return end_offset # <<<<<<<<<<<<<< + * + * + */ + __pyx_r = __pyx_v_end_offset; + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1684 + * + * + * 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_9csamtools_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; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + uint32_t __pyx_t_3; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("get_seq_range"); + __Pyx_TraceCall("get_seq_range", __pyx_f[0], 1684); + __pyx_v_seq = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1689 + * cdef char * s + * + * if not src.core.l_qseq: # <<<<<<<<<<<<<< + * return None + * + */ + __pyx_t_1 = (!__pyx_v_src->core.l_qseq); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1690 + * + * if not src.core.l_qseq: + * return None # <<<<<<<<<<<<<< + * + * seq = PyString_FromStringAndSize(NULL, end-start) + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L3; + } + __pyx_L3:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1692 + * return None + * + * seq = PyString_FromStringAndSize(NULL, end-start) # <<<<<<<<<<<<<< + * s = PyString_AS_STRING(seq) + * p = bam1_seq(src) + */ + __pyx_t_2 = PyString_FromStringAndSize(NULL, (__pyx_v_end - __pyx_v_start)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1692; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_seq); + __pyx_v_seq = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1693 + * + * seq = PyString_FromStringAndSize(NULL, end-start) + * s = PyString_AS_STRING(seq) # <<<<<<<<<<<<<< + * p = bam1_seq(src) + * + */ + __pyx_v_s = PyString_AS_STRING(__pyx_v_seq); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1694 + * seq = PyString_FromStringAndSize(NULL, end-start) + * s = PyString_AS_STRING(seq) + * p = bam1_seq(src) # <<<<<<<<<<<<<< + * + * for k from start <= k < end: + */ + __pyx_v_p = bam1_seq(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1696 + * p = bam1_seq(src) + * + * for k from start <= k < end: # <<<<<<<<<<<<<< + * # equivalent to bam_nt16_rev_table[bam1_seqi(s, i)] (see bam.c) + * # note: do not use string literal as it will be a python string + */ + __pyx_t_3 = __pyx_v_end; + for (__pyx_v_k = __pyx_v_start; __pyx_v_k < __pyx_t_3; __pyx_v_k++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1699 + * # equivalent to bam_nt16_rev_table[bam1_seqi(s, i)] (see bam.c) + * # note: do not use string literal as it will be a python string + * s[k-start] = bam_nt16_rev_table[p[k/2] >> 4 * (1 - k%2) & 0xf] # <<<<<<<<<<<<<< + * + * return seq + */ + (__pyx_v_s[(__pyx_v_k - __pyx_v_start)]) = (__pyx_v_9csamtools_bam_nt16_rev_table[(((__pyx_v_p[__Pyx_div_long(__pyx_v_k, 2)]) >> (4 * (1 - __Pyx_mod_long(__pyx_v_k, 2)))) & 0xf)]); + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1701 + * s[k-start] = bam_nt16_rev_table[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; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.get_seq_range"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_seq); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1704 + * + * + * 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_9csamtools_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; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + uint32_t __pyx_t_3; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("get_qual_range"); + __Pyx_TraceCall("get_qual_range", __pyx_f[0], 1704); + __pyx_v_qual = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1709 + * cdef char * q + * + * p = bam1_qual(src) # <<<<<<<<<<<<<< + * if p[0] == 0xff: + * return None + */ + __pyx_v_p = bam1_qual(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1710 + * + * p = bam1_qual(src) + * if p[0] == 0xff: # <<<<<<<<<<<<<< + * return None + * + */ + __pyx_t_1 = ((__pyx_v_p[0]) == 0xff); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1711 + * p = bam1_qual(src) + * if p[0] == 0xff: + * return None # <<<<<<<<<<<<<< + * + * qual = PyString_FromStringAndSize(NULL, end-start) + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L3; + } + __pyx_L3:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1713 + * return None + * + * qual = PyString_FromStringAndSize(NULL, end-start) # <<<<<<<<<<<<<< + * q = PyString_AS_STRING(qual) + * + */ + __pyx_t_2 = PyString_FromStringAndSize(NULL, (__pyx_v_end - __pyx_v_start)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_qual); + __pyx_v_qual = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1714 + * + * qual = PyString_FromStringAndSize(NULL, end-start) + * q = PyString_AS_STRING(qual) # <<<<<<<<<<<<<< + * + * for k from start <= k < end: + */ + __pyx_v_q = PyString_AS_STRING(__pyx_v_qual); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1716 + * q = PyString_AS_STRING(qual) + * + * for k from start <= k < end: # <<<<<<<<<<<<<< + * ## equivalent to t[i] + 33 (see bam.c) + * q[k-start] = p[k] + 33 + */ + __pyx_t_3 = __pyx_v_end; + for (__pyx_v_k = __pyx_v_start; __pyx_v_k < __pyx_t_3; __pyx_v_k++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1718 + * 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); + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1720 + * q[k-start] = p[k] + 33 + * + * return qual # <<<<<<<<<<<<<< + * + * cdef class AlignedRead: + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_qual); + __pyx_r = __pyx_v_qual; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.get_qual_range"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_qual); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1746 + * + * # Now only called when instances are created from Python + * def __init__(self): # <<<<<<<<<<<<<< + * # see bam_init1 + * self._delegate = calloc( 1, sizeof( bam1_t) ) + */ + +static int __pyx_pf_9csamtools_11AlignedRead_0__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_0__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + int __pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__init__"); + __Pyx_TraceCall("__init__", __pyx_f[0], 1746); + 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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1748 + * def __init__(self): + * # see bam_init1 + * self._delegate = calloc( 1, sizeof( bam1_t) ) # <<<<<<<<<<<<<< + * # allocate some memory + * # If size is 0, calloc does not return a pointer that can be passed to free() + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate = ((bam1_t *)calloc(1, (sizeof(bam1_t)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1752 + * # 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 = calloc( self._delegate.m_data, 1 ) + * self._delegate.data_len = 0 + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->m_data = 40; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1753 + * # so allocate 40 bytes for a new read + * self._delegate.m_data = 40 + * self._delegate.data = calloc( self._delegate.m_data, 1 ) # <<<<<<<<<<<<<< + * self._delegate.data_len = 0 + * + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->data = ((uint8_t *)calloc(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->m_data, 1)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1754 + * self._delegate.m_data = 40 + * self._delegate.data = calloc( self._delegate.m_data, 1 ) + * self._delegate.data_len = 0 # <<<<<<<<<<<<<< + * + * def __dealloc__(self): + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->data_len = 0; + + __pyx_r = 0; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1756 + * self._delegate.data_len = 0 + * + * def __dealloc__(self): # <<<<<<<<<<<<<< + * bam_destroy1(self._delegate) + * + */ + +static void __pyx_pf_9csamtools_11AlignedRead_1__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_11AlignedRead_1__dealloc__(PyObject *__pyx_v_self) { + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1756); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1757 + * + * def __dealloc__(self): + * bam_destroy1(self._delegate) # <<<<<<<<<<<<<< + * + * def __str__(self): + */ + bam_destroy1(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate); + + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1759 + * bam_destroy1(self._delegate) + * + * def __str__(self): # <<<<<<<<<<<<<< + * """todo""" + * return "\t".join(map(str, (self.qname, + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_2__str__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_11AlignedRead_2__str__[] = "todo"; +struct wrapperbase __pyx_wrapperbase_9csamtools_11AlignedRead_2__str__; +static PyObject *__pyx_pf_9csamtools_11AlignedRead_2__str__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + 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; + PyObject *__pyx_t_10 = NULL; + PyObject *__pyx_t_11 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__str__"); + __Pyx_TraceCall("__str__", __pyx_f[0], 1759); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1761 + * def __str__(self): + * """todo""" + * return "\t".join(map(str, (self.qname, # <<<<<<<<<<<<<< + * self.rname, + * self.pos, + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_1), __pyx_n_s__join); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1761; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__qname); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1761; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1762 + * """todo""" + * return "\t".join(map(str, (self.qname, + * self.rname, # <<<<<<<<<<<<<< + * self.pos, + * self.cigar, + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__rname); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1762; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1763 + * return "\t".join(map(str, (self.qname, + * self.rname, + * self.pos, # <<<<<<<<<<<<<< + * self.cigar, + * self.qual, + */ + __pyx_t_4 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__pos); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1763; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1764 + * self.rname, + * self.pos, + * self.cigar, # <<<<<<<<<<<<<< + * self.qual, + * self.flag, + */ + __pyx_t_5 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__cigar); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1764; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1765 + * self.pos, + * self.cigar, + * self.qual, # <<<<<<<<<<<<<< + * self.flag, + * self.seq, + */ + __pyx_t_6 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__qual); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1765; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1766 + * self.cigar, + * self.qual, + * self.flag, # <<<<<<<<<<<<<< + * self.seq, + * self.mapq, + */ + __pyx_t_7 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1766; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1767 + * self.qual, + * self.flag, + * self.seq, # <<<<<<<<<<<<<< + * self.mapq, + * self.tags))) + */ + __pyx_t_8 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__seq); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1767; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1768 + * self.flag, + * self.seq, + * self.mapq, # <<<<<<<<<<<<<< + * self.tags))) + * + */ + __pyx_t_9 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__mapq); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1768; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1769 + * self.seq, + * self.mapq, + * self.tags))) # <<<<<<<<<<<<<< + * + * + */ + __pyx_t_10 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__tags); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1769; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_10); + __pyx_t_11 = PyTuple_New(9); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1761; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_3); + __Pyx_GIVEREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_11, 2, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_11, 3, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_11, 4, __pyx_t_6); + __Pyx_GIVEREF(__pyx_t_6); + PyTuple_SET_ITEM(__pyx_t_11, 5, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + PyTuple_SET_ITEM(__pyx_t_11, 6, __pyx_t_8); + __Pyx_GIVEREF(__pyx_t_8); + PyTuple_SET_ITEM(__pyx_t_11, 7, __pyx_t_9); + __Pyx_GIVEREF(__pyx_t_9); + PyTuple_SET_ITEM(__pyx_t_11, 8, __pyx_t_10); + __Pyx_GIVEREF(__pyx_t_10); + __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; + __pyx_t_9 = 0; + __pyx_t_10 = 0; + __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1761; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_10)); + __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_10, 0, ((PyObject *)((PyObject*)(&PyString_Type)))); + __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_10, 1, ((PyObject *)__pyx_t_11)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_11)); + __pyx_t_11 = 0; + __pyx_t_11 = PyObject_Call(__pyx_builtin_map, ((PyObject *)__pyx_t_10), NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1761; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_11); + __Pyx_DECREF(((PyObject *)__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 = 1761; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_10)); + PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_11); + __Pyx_GIVEREF(__pyx_t_11); + __pyx_t_11 = 0; + __pyx_t_11 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_10), NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1761; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_11); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_10)); __pyx_t_10 = 0; + __pyx_r = __pyx_t_11; + __pyx_t_11 = 0; + goto __pyx_L0; + + __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_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_AddTraceback("csamtools.AlignedRead.__str__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1772 + * + * + * def compare(self, AlignedRead other): # <<<<<<<<<<<<<< + * '''return -1,0,1, if contents in this are binary <,=,> to *other*''' + * + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3compare(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/ +static char __pyx_doc_9csamtools_11AlignedRead_3compare[] = "return -1,0,1, if contents in this are binary <,=,> to *other*"; +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3compare(PyObject *__pyx_v_self, PyObject *__pyx_v_other) { + int __pyx_v_retval; + bam1_t *__pyx_v_t; + bam1_t *__pyx_v_o; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + int __pyx_t_5; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("compare"); + __Pyx_TraceCall("compare", __pyx_f[0], 1772); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_9csamtools_AlignedRead, 1, "other", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1772; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1778 + * cdef bam1_t *t, *o + * + * t = self._delegate # <<<<<<<<<<<<<< + * o = other._delegate + * + */ + __pyx_v_t = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1779 + * + * t = self._delegate + * o = other._delegate # <<<<<<<<<<<<<< + * + * # uncomment for debugging purposes + */ + __pyx_v_o = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_other)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1791 + * + * # Fast-path test for object identity + * if t==o: # <<<<<<<<<<<<<< + * return 0 + * + */ + __pyx_t_1 = (__pyx_v_t == __pyx_v_o); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1792 + * # 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; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1794 + * 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))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1796 + * retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t)) + * + * if retval: return retval # <<<<<<<<<<<<<< + * retval = cmp(t.data_len, o.data_len) + * if retval: return retval + */ + if (__pyx_v_retval) { + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyInt_FromLong(__pyx_v_retval); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1796; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1797 + * + * if retval: return retval + * retval = cmp(t.data_len, o.data_len) # <<<<<<<<<<<<<< + * if retval: return retval + * return memcmp(t.data, o.data, t.data_len) + */ + __pyx_t_2 = PyInt_FromLong(__pyx_v_t->data_len); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyInt_FromLong(__pyx_v_o->data_len); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __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 = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2); + __Pyx_GIVEREF(__pyx_t_2); + PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_3); + __Pyx_GIVEREF(__pyx_t_3); + __pyx_t_2 = 0; + __pyx_t_3 = 0; + __pyx_t_3 = PyObject_Call(__pyx_builtin_cmp, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __pyx_t_5 = __Pyx_PyInt_AsInt(__pyx_t_3); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_v_retval = __pyx_t_5; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1798 + * if retval: return retval + * retval = cmp(t.data_len, o.data_len) + * if retval: return retval # <<<<<<<<<<<<<< + * return memcmp(t.data, o.data, t.data_len) + * + */ + if (__pyx_v_retval) { + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = PyInt_FromLong(__pyx_v_retval); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1798; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1799 + * retval = cmp(t.data_len, o.data_len) + * if retval: return retval + * return memcmp(t.data, o.data, t.data_len) # <<<<<<<<<<<<<< + * + * # Disabled so long as __cmp__ is a special method + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = PyInt_FromLong(memcmp(__pyx_v_t->data, __pyx_v_o->data, __pyx_v_t->data_len)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1799; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + + __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("csamtools.AlignedRead.compare"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1802 + * + * # Disabled so long as __cmp__ is a special method + * def __hash__(self): # <<<<<<<<<<<<<< + * return _Py_HashPointer(self) + * + */ + +static long __pyx_pf_9csamtools_11AlignedRead_4__hash__(PyObject *__pyx_v_self); /*proto*/ +static long __pyx_pf_9csamtools_11AlignedRead_4__hash__(PyObject *__pyx_v_self) { + long __pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__hash__"); + __Pyx_TraceCall("__hash__", __pyx_f[0], 1802); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1803 + * # Disabled so long as __cmp__ is a special method + * def __hash__(self): + * return _Py_HashPointer(self) # <<<<<<<<<<<<<< + * + * property qname: + */ + __pyx_r = _Py_HashPointer(((void *)__pyx_v_self)); + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + if (unlikely(__pyx_r == -1) && !PyErr_Occurred()) __pyx_r = -2; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1807 + * property qname: + * """the query name (None if not present)""" + * def __get__(self): # <<<<<<<<<<<<<< + * cdef bam1_t * src + * src = self._delegate + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5qname_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5qname_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 1807); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1809 + * def __get__(self): + * cdef bam1_t * src + * src = self._delegate # <<<<<<<<<<<<<< + * if src.core.l_qname == 0: return None + * return bam1_qname( src ) + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1810 + * cdef bam1_t * src + * src = self._delegate + * if src.core.l_qname == 0: return None # <<<<<<<<<<<<<< + * return bam1_qname( src ) + * + */ + __pyx_t_1 = (__pyx_v_src->core.l_qname == 0); + if (__pyx_t_1) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1811 + * src = self._delegate + * if src.core.l_qname == 0: return None + * return bam1_qname( src ) # <<<<<<<<<<<<<< + * + * def __set__(self, qname ): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyBytes_FromString(bam1_qname(__pyx_v_src)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1811; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __pyx_r = ((PyObject *)__pyx_t_2); + __pyx_t_2 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.AlignedRead.qname.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1813 + * return bam1_qname( src ) + * + * def __set__(self, qname ): # <<<<<<<<<<<<<< + * if qname == None or len(qname) == 0: return + * cdef bam1_t * src + */ + +static int __pyx_pf_9csamtools_11AlignedRead_5qname_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qname); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_5qname_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qname) { + bam1_t *__pyx_v_src; + int __pyx_v_l; + char *__pyx_v_p; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + Py_ssize_t __pyx_t_3; + int __pyx_t_4; + int __pyx_t_5; + char *__pyx_t_6; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 1813); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1814 + * + * def __set__(self, qname ): + * if qname == None or len(qname) == 0: return # <<<<<<<<<<<<<< + * cdef bam1_t * src + * cdef int l + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_qname, Py_None, Py_EQ); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1814; __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 = 1814; __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 = 1814; __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; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1819 + * cdef char * p + * + * src = self._delegate # <<<<<<<<<<<<<< + * p = bam1_qname( src ) + * + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1820 + * + * src = self._delegate + * p = bam1_qname( src ) # <<<<<<<<<<<<<< + * + * # the qname is \0 terminated + */ + __pyx_v_p = bam1_qname(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1823 + * + * # the qname is \0 terminated + * l = len(qname) + 1 # <<<<<<<<<<<<<< + * pysam_bam_update( src, + * src.core.l_qname, + */ + __pyx_t_3 = PyObject_Length(__pyx_v_qname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1823; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_l = (__pyx_t_3 + 1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1827 + * src.core.l_qname, + * l, + * p ) # <<<<<<<<<<<<<< + * + * src.core.l_qname = l + */ + pysam_bam_update(__pyx_v_src, __pyx_v_src->core.l_qname, __pyx_v_l, ((uint8_t *)__pyx_v_p)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1829 + * p ) + * + * src.core.l_qname = l # <<<<<<<<<<<<<< + * + * # re-acquire pointer to location in memory + */ + __pyx_v_src->core.l_qname = __pyx_v_l; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1833 + * # re-acquire pointer to location in memory + * # as it might have moved + * p = bam1_qname(src) # <<<<<<<<<<<<<< + * + * strncpy( p, qname, l ) + */ + __pyx_v_p = bam1_qname(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1835 + * p = bam1_qname(src) + * + * strncpy( p, qname, l ) # <<<<<<<<<<<<<< + * + * property cigar: + */ + __pyx_t_6 = PyBytes_AsString(__pyx_v_qname); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1835; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + strncpy(__pyx_v_p, __pyx_t_6, __pyx_v_l); + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.qname.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1840 + * """the :term:`cigar` alignment (None if not present). + * """ + * def __get__(self): # <<<<<<<<<<<<<< + * cdef uint32_t * cigar_p + * cdef bam1_t * src + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5cigar_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5cigar_0__get__(PyObject *__pyx_v_self) { + uint32_t *__pyx_v_cigar_p; + bam1_t *__pyx_v_src; + PyObject *__pyx_v_op; + PyObject *__pyx_v_l; + PyObject *__pyx_v_cigar; + int __pyx_v_k; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + uint32_t __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 1840); + __pyx_v_op = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_l = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_cigar = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1846 + * cdef int k + * + * src = self._delegate # <<<<<<<<<<<<<< + * if src.core.n_cigar == 0: return None + * + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1847 + * + * src = self._delegate + * if src.core.n_cigar == 0: return None # <<<<<<<<<<<<<< + * + * cigar = [] + */ + __pyx_t_1 = (__pyx_v_src->core.n_cigar == 0); + if (__pyx_t_1) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1849 + * if src.core.n_cigar == 0: return None + * + * cigar = [] # <<<<<<<<<<<<<< + * cigar_p = bam1_cigar(src); + * for k from 0 <= k < src.core.n_cigar: + */ + __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1849; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(__pyx_v_cigar); + __pyx_v_cigar = ((PyObject *)__pyx_t_2); + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1850 + * + * cigar = [] + * cigar_p = bam1_cigar(src); # <<<<<<<<<<<<<< + * for k from 0 <= k < src.core.n_cigar: + * op = cigar_p[k] & BAM_CIGAR_MASK + */ + __pyx_v_cigar_p = bam1_cigar(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1851 + * cigar = [] + * cigar_p = bam1_cigar(src); + * for k from 0 <= k < src.core.n_cigar: # <<<<<<<<<<<<<< + * op = cigar_p[k] & BAM_CIGAR_MASK + * l = cigar_p[k] >> BAM_CIGAR_SHIFT + */ + __pyx_t_3 = __pyx_v_src->core.n_cigar; + for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_3; __pyx_v_k++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1852 + * cigar_p = bam1_cigar(src); + * for k from 0 <= k < src.core.n_cigar: + * op = cigar_p[k] & BAM_CIGAR_MASK # <<<<<<<<<<<<<< + * l = cigar_p[k] >> BAM_CIGAR_SHIFT + * cigar.append((op, l)) + */ + __pyx_t_2 = PyInt_FromLong(((__pyx_v_cigar_p[__pyx_v_k]) & 15)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1852; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_op); + __pyx_v_op = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1853 + * for k from 0 <= k < src.core.n_cigar: + * op = cigar_p[k] & BAM_CIGAR_MASK + * l = cigar_p[k] >> BAM_CIGAR_SHIFT # <<<<<<<<<<<<<< + * cigar.append((op, l)) + * return cigar + */ + __pyx_t_2 = PyInt_FromLong(((__pyx_v_cigar_p[__pyx_v_k]) >> 4)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1853; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_l); + __pyx_v_l = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1854 + * op = cigar_p[k] & BAM_CIGAR_MASK + * l = cigar_p[k] >> BAM_CIGAR_SHIFT + * cigar.append((op, l)) # <<<<<<<<<<<<<< + * return cigar + * + */ + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_v_op); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_op); + __Pyx_GIVEREF(__pyx_v_op); + __Pyx_INCREF(__pyx_v_l); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_l); + __Pyx_GIVEREF(__pyx_v_l); + __pyx_t_4 = __Pyx_PyObject_Append(__pyx_v_cigar, ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1855 + * 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; + + __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_AddTraceback("csamtools.AlignedRead.cigar.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_op); + __Pyx_DECREF(__pyx_v_l); + __Pyx_DECREF(__pyx_v_cigar); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1857 + * return cigar + * + * def __set__(self, values ): # <<<<<<<<<<<<<< + * if values == None or len(values) == 0: return + * cdef uint32_t * p + */ + +static int __pyx_pf_9csamtools_11AlignedRead_5cigar_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_values); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_5cigar_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_values) { + uint32_t *__pyx_v_p; + bam1_t *__pyx_v_src; + PyObject *__pyx_v_op; + PyObject *__pyx_v_l; + int __pyx_v_k; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + Py_ssize_t __pyx_t_3; + 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; + uint32_t __pyx_t_10; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 1857); + __pyx_v_op = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_l = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1858 + * + * def __set__(self, values ): + * if values == None or len(values) == 0: return # <<<<<<<<<<<<<< + * cdef uint32_t * p + * cdef bam1_t * src + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_values, Py_None, Py_EQ); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1858; __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 = 1858; __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_values); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1858; __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; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1864 + * cdef int k + * + * k = 0 # <<<<<<<<<<<<<< + * + * src = self._delegate + */ + __pyx_v_k = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1866 + * k = 0 + * + * src = self._delegate # <<<<<<<<<<<<<< + * + * # get location of cigar string + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1869 + * + * # get location of cigar string + * p = bam1_cigar(src) # <<<<<<<<<<<<<< + * + * # create space for cigar data within src.data + */ + __pyx_v_p = bam1_cigar(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1874 + * pysam_bam_update( src, + * src.core.n_cigar * 4, + * len(values) * 4, # <<<<<<<<<<<<<< + * p ) + * + */ + __pyx_t_3 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1874; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1875 + * src.core.n_cigar * 4, + * len(values) * 4, + * p ) # <<<<<<<<<<<<<< + * + * # length is number of cigar operations, not bytes + */ + pysam_bam_update(__pyx_v_src, (__pyx_v_src->core.n_cigar * 4), (__pyx_t_3 * 4), ((uint8_t *)__pyx_v_p)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1878 + * + * # length is number of cigar operations, not bytes + * src.core.n_cigar = len(values) # <<<<<<<<<<<<<< + * + * # re-acquire pointer to location in memory + */ + __pyx_t_3 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1878; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_src->core.n_cigar = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1882 + * # re-acquire pointer to location in memory + * # as it might have moved + * p = bam1_cigar(src) # <<<<<<<<<<<<<< + * + * # insert cigar operations + */ + __pyx_v_p = bam1_cigar(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1885 + * + * # 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_3 = 0; __pyx_t_1 = __pyx_v_values; __Pyx_INCREF(__pyx_t_1); + } else { + __pyx_t_3 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + } + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_1))) { + if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_1)) break; + __pyx_t_6 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_6); __pyx_t_3++; + } else if (likely(PyTuple_CheckExact(__pyx_t_1))) { + if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_1)) break; + __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_6); __pyx_t_3++; + } else { + __pyx_t_6 = PyIter_Next(__pyx_t_1); + if (!__pyx_t_6) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_6); + } + if (PyTuple_CheckExact(__pyx_t_6) && likely(PyTuple_GET_SIZE(__pyx_t_6) == 2)) { + PyObject* tuple = __pyx_t_6; + __pyx_t_7 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_7); + __pyx_t_8 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_8); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(__pyx_v_op); + __pyx_v_op = __pyx_t_7; + __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_v_l); + __pyx_v_l = __pyx_t_8; + __pyx_t_8 = 0; + } else { + __pyx_t_9 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_7 = __Pyx_UnpackItem(__pyx_t_9, 0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_8 = __Pyx_UnpackItem(__pyx_t_9, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + if (__Pyx_EndUnpack(__pyx_t_9, 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; + __Pyx_DECREF(__pyx_v_op); + __pyx_v_op = __pyx_t_7; + __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_v_l); + __pyx_v_l = __pyx_t_8; + __pyx_t_8 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1886 + * # insert cigar operations + * for op, l in values: + * p[k] = l << BAM_CIGAR_SHIFT | op # <<<<<<<<<<<<<< + * k += 1 + * + */ + __pyx_t_6 = PyNumber_Lshift(__pyx_v_l, __pyx_int_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1886; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_8 = PyNumber_Or(__pyx_t_6, __pyx_v_op); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1886; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_10 = __Pyx_PyInt_from_py_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_10 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1886; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + (__pyx_v_p[__pyx_v_k]) = __pyx_t_10; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1887 + * for op, l in values: + * p[k] = l << BAM_CIGAR_SHIFT | op + * k += 1 # <<<<<<<<<<<<<< + * + * ## setting the cigar string also updates the "bin" attribute + */ + __pyx_v_k = (__pyx_v_k + 1); + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1890 + * + * ## setting the cigar string also updates the "bin" attribute + * src.core.bin = bam_reg2bin( src.core.pos, bam_calend( &src.core, p)) # <<<<<<<<<<<<<< + * + * property seq: + */ + __pyx_v_src->core.bin = bam_reg2bin(__pyx_v_src->core.pos, bam_calend((&__pyx_v_src->core), __pyx_v_p)); + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_XDECREF(__pyx_t_9); + __Pyx_AddTraceback("csamtools.AlignedRead.cigar.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_op); + __Pyx_DECREF(__pyx_v_l); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1894 + * property seq: + * """read sequence bases, including :term:`soft clipped` bases (None if not present)""" + * def __get__(self): # <<<<<<<<<<<<<< + * cdef bam1_t * src + * cdef char * s + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3seq_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3seq_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 1894); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1897 + * cdef bam1_t * src + * cdef char * s + * src = self._delegate # <<<<<<<<<<<<<< + * + * if src.core.l_qseq == 0: return None + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1899 + * src = self._delegate + * + * if src.core.l_qseq == 0: return None # <<<<<<<<<<<<<< + * + * return get_seq_range(src, 0, src.core.l_qseq) + */ + __pyx_t_1 = (__pyx_v_src->core.l_qseq == 0); + if (__pyx_t_1) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1901 + * 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_2 = __pyx_f_9csamtools_get_seq_range(__pyx_v_src, 0, __pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1901; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.AlignedRead.seq.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1903 + * 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. + */ + +static int __pyx_pf_9csamtools_11AlignedRead_3seq_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_seq); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_3seq_1__set__(PyObject *__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; + PyObject *__pyx_t_1 = NULL; + 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; + long __pyx_t_8; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 1903); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1907 + * # if no quality information is present, the first byte says 0xff. + * + * if seq == None or len(seq) == 0: return # <<<<<<<<<<<<<< + * cdef bam1_t * src + * cdef uint8_t * p + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_seq, Py_None, Py_EQ); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1907; __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 = 1907; __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_seq); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1907; __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; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1913 + * cdef int l, k, nbytes_new, nbytes_old + * + * src = self._delegate # <<<<<<<<<<<<<< + * + * l = len(seq) + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1915 + * src = self._delegate + * + * l = len(seq) # <<<<<<<<<<<<<< + * + * # as the sequence is stored in half-bytes, the total length (sequence + */ + __pyx_t_3 = PyObject_Length(__pyx_v_seq); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_l = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1919 + * # 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 + */ + __pyx_v_nbytes_new = (__Pyx_div_long((__pyx_v_l + 1), 2) + __pyx_v_l); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1920 + * # 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 = bam1_seq( src ) + */ + __pyx_v_nbytes_old = (__Pyx_div_long((__pyx_v_src->core.l_qseq + 1), 2) + __pyx_v_src->core.l_qseq); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1922 + * nbytes_old = (src.core.l_qseq+1)/2 + src.core.l_qseq + * # acquire pointer to location in memory + * p = bam1_seq( src ) # <<<<<<<<<<<<<< + * src.core.l_qseq = l + * + */ + __pyx_v_p = bam1_seq(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1923 + * # acquire pointer to location in memory + * p = bam1_seq( src ) + * src.core.l_qseq = l # <<<<<<<<<<<<<< + * + * pysam_bam_update( src, + */ + __pyx_v_src->core.l_qseq = __pyx_v_l; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1928 + * nbytes_old, + * nbytes_new, + * p) # <<<<<<<<<<<<<< + * # re-acquire pointer to location in memory + * # as it might have moved + */ + pysam_bam_update(__pyx_v_src, __pyx_v_nbytes_old, __pyx_v_nbytes_new, __pyx_v_p); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1931 + * # re-acquire pointer to location in memory + * # as it might have moved + * p = bam1_seq( src ) # <<<<<<<<<<<<<< + * for k from 0 <= k < nbytes_new: p[k] = 0 + * # convert to C string + */ + __pyx_v_p = bam1_seq(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1932 + * # as it might have moved + * p = bam1_seq( src ) + * for k from 0 <= k < nbytes_new: p[k] = 0 # <<<<<<<<<<<<<< + * # convert to C string + * s = seq + */ + __pyx_t_6 = __pyx_v_nbytes_new; + for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_6; __pyx_v_k++) { + (__pyx_v_p[__pyx_v_k]) = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1934 + * 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] |= pysam_translate_sequence(s[k]) << 4 * (1 - k % 2) + */ + __pyx_t_7 = PyBytes_AsString(__pyx_v_seq); if (unlikely((!__pyx_t_7) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1934; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_s = __pyx_t_7; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1935 + * # convert to C string + * s = seq + * for k from 0 <= k < l: # <<<<<<<<<<<<<< + * p[k/2] |= pysam_translate_sequence(s[k]) << 4 * (1 - k % 2) + * + */ + __pyx_t_6 = __pyx_v_l; + for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_6; __pyx_v_k++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1936 + * s = seq + * for k from 0 <= k < l: + * p[k/2] |= pysam_translate_sequence(s[k]) << 4 * (1 - k % 2) # <<<<<<<<<<<<<< + * + * # erase qualities + */ + __pyx_t_8 = __Pyx_div_long(__pyx_v_k, 2); + (__pyx_v_p[__pyx_t_8]) = ((__pyx_v_p[__pyx_t_8]) | (pysam_translate_sequence((__pyx_v_s[__pyx_v_k])) << (4 * (1 - __Pyx_mod_long(__pyx_v_k, 2))))); + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1939 + * + * # erase qualities + * p = bam1_qual( src ) # <<<<<<<<<<<<<< + * p[0] = 0xff + * + */ + __pyx_v_p = bam1_qual(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1940 + * # erase qualities + * p = bam1_qual( src ) + * p[0] = 0xff # <<<<<<<<<<<<<< + * + * + */ + (__pyx_v_p[0]) = 0xff; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.seq.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1945 + * property qual: + * """read sequence base qualities, including :term:`soft clipped` bases (None if not present)""" + * def __get__(self): # <<<<<<<<<<<<<< + * + * cdef bam1_t * src + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4qual_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4qual_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 1945); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1950 + * cdef char * q + * + * src = self._delegate # <<<<<<<<<<<<<< + * + * if src.core.l_qseq == 0: return None + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1952 + * src = self._delegate + * + * if src.core.l_qseq == 0: return None # <<<<<<<<<<<<<< + * + * return get_qual_range(src, 0, src.core.l_qseq) + */ + __pyx_t_1 = (__pyx_v_src->core.l_qseq == 0); + if (__pyx_t_1) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1954 + * 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_2 = __pyx_f_9csamtools_get_qual_range(__pyx_v_src, 0, __pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1954; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.AlignedRead.qual.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1956 + * 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 + */ + +static int __pyx_pf_9csamtools_11AlignedRead_4qual_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_4qual_1__set__(PyObject *__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; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + Py_ssize_t __pyx_t_3; + int __pyx_t_4; + int __pyx_t_5; + char *__pyx_t_6; + PyObject *__pyx_t_7 = NULL; + PyObject *__pyx_t_8 = NULL; + int __pyx_t_9; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 1956); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1963 + * cdef int k + * + * src = self._delegate # <<<<<<<<<<<<<< + * p = bam1_qual( src ) + * if qual == None or len(qual) == 0: + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1964 + * + * src = self._delegate + * p = bam1_qual( src ) # <<<<<<<<<<<<<< + * if qual == None or len(qual) == 0: + * # if absent - set to 0xff + */ + __pyx_v_p = bam1_qual(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1965 + * src = self._delegate + * p = bam1_qual( src ) + * if qual == None or len(qual) == 0: # <<<<<<<<<<<<<< + * # if absent - set to 0xff + * p[0] = 0xff + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_qual, Py_None, Py_EQ); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1965; __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 = 1965; __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_qual); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1965; __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) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1967 + * if qual == None or len(qual) == 0: + * # if absent - set to 0xff + * p[0] = 0xff # <<<<<<<<<<<<<< + * return + * cdef int l + */ + (__pyx_v_p[0]) = 0xff; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1968 + * # if absent - set to 0xff + * p[0] = 0xff + * return # <<<<<<<<<<<<<< + * cdef int l + * # convert to C string + */ + __pyx_r = 0; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1971 + * cdef int l + * # convert to C string + * q = qual # <<<<<<<<<<<<<< + * l = len(qual) + * if src.core.l_qseq != l: + */ + __pyx_t_6 = PyBytes_AsString(__pyx_v_qual); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1971; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_q = __pyx_t_6; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1972 + * # 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_3 = PyObject_Length(__pyx_v_qual); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1972; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_l = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1973 + * 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_5 = (__pyx_v_src->core.l_qseq != __pyx_v_l); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1974 + * 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_1 = PyInt_FromLong(__pyx_v_l); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1974; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_7 = __Pyx_PyInt_to_py_int32_t(__pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1974; __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 = 1974; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_7); + __Pyx_GIVEREF(__pyx_t_7); + __pyx_t_1 = 0; + __pyx_t_7 = 0; + __pyx_t_7 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_109), ((PyObject *)__pyx_t_8)); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1974; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_7)); + __Pyx_DECREF(((PyObject *)__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 = 1974; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_8)); + PyTuple_SET_ITEM(__pyx_t_8, 0, ((PyObject *)__pyx_t_7)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_7)); + __pyx_t_7 = 0; + __pyx_t_7 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_8), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1974; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(((PyObject *)__pyx_t_8)); __pyx_t_8 = 0; + __Pyx_Raise(__pyx_t_7, 0, 0); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1974; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1975 + * 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] = q[k] - 33 + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + if (unlikely(!(__pyx_v_src->core.l_qseq == __pyx_v_l))) { + PyErr_SetNone(PyExc_AssertionError); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1975; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1976 + * 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] = q[k] - 33 + * + */ + __pyx_t_9 = __pyx_v_l; + for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_9; __pyx_v_k++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1977 + * assert src.core.l_qseq == l + * for k from 0 <= k < l: + * p[k] = q[k] - 33 # <<<<<<<<<<<<<< + * + * property query: + */ + (__pyx_v_p[__pyx_v_k]) = (((uint8_t)(__pyx_v_q[__pyx_v_k])) - 33); + } + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_AddTraceback("csamtools.AlignedRead.qual.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1989 + * were not considered for alignment may have been retained.""" + * + * def __get__(self): # <<<<<<<<<<<<<< + * cdef bam1_t * src + * cdef uint32_t start, end + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5query_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5query_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + uint32_t __pyx_v_start; + uint32_t __pyx_v_end; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + int32_t __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 1989); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1994 + * cdef char * s + * + * src = self._delegate # <<<<<<<<<<<<<< + * + * if src.core.l_qseq == 0: return None + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1996 + * src = self._delegate + * + * if src.core.l_qseq == 0: return None # <<<<<<<<<<<<<< + * + * start = query_start(src) + */ + __pyx_t_1 = (__pyx_v_src->core.l_qseq == 0); + if (__pyx_t_1) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1998 + * if src.core.l_qseq == 0: return None + * + * start = query_start(src) # <<<<<<<<<<<<<< + * end = query_end(src) + * + */ + __pyx_t_2 = __pyx_f_9csamtools_query_start(__pyx_v_src); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1998; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_start = __pyx_t_2; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1999 + * + * start = query_start(src) + * end = query_end(src) # <<<<<<<<<<<<<< + * + * return get_seq_range(src, start, end) + */ + __pyx_t_2 = __pyx_f_9csamtools_query_end(__pyx_v_src); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1999; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_end = __pyx_t_2; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2001 + * end = query_end(src) + * + * return get_seq_range(src, start, end) # <<<<<<<<<<<<<< + * + * property qqual: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = __pyx_f_9csamtools_get_seq_range(__pyx_v_src, __pyx_v_start, __pyx_v_end); 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_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("csamtools.AlignedRead.query.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2005 + * property qqual: + * """aligned query sequence quality values (None if not present)""" + * def __get__(self): # <<<<<<<<<<<<<< + * cdef bam1_t * src + * cdef uint32_t start, end + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5qqual_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5qqual_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + uint32_t __pyx_v_start; + uint32_t __pyx_v_end; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + int32_t __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2005); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2010 + * cdef char * q + * + * src = self._delegate # <<<<<<<<<<<<<< + * + * if src.core.l_qseq == 0: return None + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2012 + * src = self._delegate + * + * if src.core.l_qseq == 0: return None # <<<<<<<<<<<<<< + * + * start = query_start(src) + */ + __pyx_t_1 = (__pyx_v_src->core.l_qseq == 0); + if (__pyx_t_1) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2014 + * if src.core.l_qseq == 0: return None + * + * start = query_start(src) # <<<<<<<<<<<<<< + * end = query_end(src) + * + */ + __pyx_t_2 = __pyx_f_9csamtools_query_start(__pyx_v_src); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2014; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_start = __pyx_t_2; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2015 + * + * start = query_start(src) + * end = query_end(src) # <<<<<<<<<<<<<< + * + * return get_qual_range(src, start, end) + */ + __pyx_t_2 = __pyx_f_9csamtools_query_end(__pyx_v_src); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2015; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_end = __pyx_t_2; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2017 + * end = query_end(src) + * + * return get_qual_range(src, start, end) # <<<<<<<<<<<<<< + * + * property qstart: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = __pyx_f_9csamtools_get_qual_range(__pyx_v_src, __pyx_v_start, __pyx_v_end); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2017; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("csamtools.AlignedRead.qqual.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2021 + * property qstart: + * """start index of the aligned query portion of the sequence (0-based, inclusive)""" + * def __get__(self): # <<<<<<<<<<<<<< + * return query_start(self._delegate) + * + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_6qstart_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_6qstart_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + int32_t __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2021); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2022 + * """start index of the aligned query portion of the sequence (0-based, inclusive)""" + * def __get__(self): + * return query_start(self._delegate) # <<<<<<<<<<<<<< + * + * property qend: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __pyx_f_9csamtools_query_start(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2022; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = __Pyx_PyInt_to_py_int32_t(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2022; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.AlignedRead.qstart.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2026 + * property qend: + * """end index of the aligned query portion of the sequence (0-based, exclusive)""" + * def __get__(self): # <<<<<<<<<<<<<< + * return query_end(self._delegate) + * + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4qend_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4qend_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + int32_t __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2026); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2027 + * """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_9csamtools_query_end(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2027; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = __Pyx_PyInt_to_py_int32_t(__pyx_t_1); 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; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.AlignedRead.qend.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2031 + * property qlen: + * """Length of the aligned query sequence""" + * def __get__(self): # <<<<<<<<<<<<<< + * cdef bam1_t * src + * src = self._delegate + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4qlen_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4qlen_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + PyObject *__pyx_r = NULL; + int32_t __pyx_t_1; + int32_t __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2031); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2033 + * def __get__(self): + * cdef bam1_t * src + * src = self._delegate # <<<<<<<<<<<<<< + * return query_end(src)-query_start(src) + * + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2034 + * cdef bam1_t * src + * src = self._delegate + * return query_end(src)-query_start(src) # <<<<<<<<<<<<<< + * + * property tags: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __pyx_f_9csamtools_query_end(__pyx_v_src); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = __pyx_f_9csamtools_query_start(__pyx_v_src); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = __Pyx_PyInt_to_py_int32_t((__pyx_t_1 - __pyx_t_2)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("csamtools.AlignedRead.qlen.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2048 + * + * """ + * def __get__(self): # <<<<<<<<<<<<<< + * cdef char * ctag + * cdef bam1_t * src + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4tags_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4tags_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + uint8_t *__pyx_v_s; + char __pyx_v_auxtag[3]; + char __pyx_v_auxtype; + PyObject *__pyx_v_result; + PyObject *__pyx_v_value; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + char __pyx_t_3; + int __pyx_t_4; + int __pyx_t_5; + PyObject *__pyx_t_6 = NULL; + Py_ssize_t __pyx_t_7; + int __pyx_t_8; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2048); + __pyx_v_result = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_value = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2055 + * cdef char auxtype + * + * src = self._delegate # <<<<<<<<<<<<<< + * if src.l_aux == 0: return None + * + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2056 + * + * src = self._delegate + * if src.l_aux == 0: return None # <<<<<<<<<<<<<< + * + * s = bam1_aux( src ) + */ + __pyx_t_1 = (__pyx_v_src->l_aux == 0); + if (__pyx_t_1) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2058 + * if src.l_aux == 0: return None + * + * s = bam1_aux( src ) # <<<<<<<<<<<<<< + * result = [] + * auxtag[2] = 0 + */ + __pyx_v_s = bam1_aux(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2059 + * + * s = bam1_aux( src ) + * result = [] # <<<<<<<<<<<<<< + * auxtag[2] = 0 + * while s < (src.data + src.data_len): + */ + __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2059; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__pyx_v_result)); + __pyx_v_result = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2060 + * s = bam1_aux( src ) + * result = [] + * auxtag[2] = 0 # <<<<<<<<<<<<<< + * while s < (src.data + src.data_len): + * # get tag + */ + (__pyx_v_auxtag[2]) = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2061 + * result = [] + * auxtag[2] = 0 + * while s < (src.data + src.data_len): # <<<<<<<<<<<<<< + * # get tag + * auxtag[0] = s[0] + */ + while (1) { + __pyx_t_1 = (__pyx_v_s < (__pyx_v_src->data + __pyx_v_src->data_len)); + if (!__pyx_t_1) break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2063 + * while s < (src.data + src.data_len): + * # get tag + * auxtag[0] = s[0] # <<<<<<<<<<<<<< + * auxtag[1] = s[1] + * s += 2 + */ + (__pyx_v_auxtag[0]) = (__pyx_v_s[0]); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2064 + * # get tag + * auxtag[0] = s[0] + * auxtag[1] = s[1] # <<<<<<<<<<<<<< + * s += 2 + * auxtype = s[0] + */ + (__pyx_v_auxtag[1]) = (__pyx_v_s[1]); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2065 + * auxtag[0] = s[0] + * auxtag[1] = s[1] + * s += 2 # <<<<<<<<<<<<<< + * auxtype = s[0] + * + */ + __pyx_v_s = (__pyx_v_s + 2); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2066 + * auxtag[1] = s[1] + * s += 2 + * auxtype = s[0] # <<<<<<<<<<<<<< + * + * if auxtype in ('c', 'C'): + */ + __pyx_v_auxtype = (__pyx_v_s[0]); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2068 + * auxtype = s[0] + * + * if auxtype in ('c', 'C'): # <<<<<<<<<<<<<< + * value = bam_aux2i(s) + * s += 1 + */ + __pyx_t_3 = __pyx_v_auxtype; + __pyx_t_1 = (__pyx_t_3 == 'c'); + if (!__pyx_t_1) { + __pyx_t_4 = (__pyx_t_3 == 'C'); + __pyx_t_5 = __pyx_t_4; + } else { + __pyx_t_5 = __pyx_t_1; + } + __pyx_t_1 = __pyx_t_5; + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2069 + * + * if auxtype in ('c', 'C'): + * value = bam_aux2i(s) # <<<<<<<<<<<<<< + * s += 1 + * elif auxtype in ('s', 'S'): + */ + __pyx_t_2 = PyInt_FromLong(((int)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2069; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2070 + * if auxtype in ('c', 'C'): + * value = bam_aux2i(s) + * s += 1 # <<<<<<<<<<<<<< + * elif auxtype in ('s', 'S'): + * value = bam_aux2i(s) + */ + __pyx_v_s = (__pyx_v_s + 1); + goto __pyx_L8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2071 + * value = bam_aux2i(s) + * s += 1 + * elif auxtype in ('s', 'S'): # <<<<<<<<<<<<<< + * value = bam_aux2i(s) + * s += 2 + */ + __pyx_t_3 = __pyx_v_auxtype; + __pyx_t_1 = (__pyx_t_3 == 's'); + if (!__pyx_t_1) { + __pyx_t_5 = (__pyx_t_3 == 'S'); + __pyx_t_4 = __pyx_t_5; + } else { + __pyx_t_4 = __pyx_t_1; + } + __pyx_t_1 = __pyx_t_4; + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2072 + * s += 1 + * elif auxtype in ('s', 'S'): + * value = bam_aux2i(s) # <<<<<<<<<<<<<< + * s += 2 + * elif auxtype in ('i', 'I'): + */ + __pyx_t_2 = PyInt_FromLong(((int)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2072; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2073 + * elif auxtype in ('s', 'S'): + * value = bam_aux2i(s) + * s += 2 # <<<<<<<<<<<<<< + * elif auxtype in ('i', 'I'): + * value = bam_aux2i(s) + */ + __pyx_v_s = (__pyx_v_s + 2); + goto __pyx_L8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2074 + * value = bam_aux2i(s) + * s += 2 + * elif auxtype in ('i', 'I'): # <<<<<<<<<<<<<< + * value = bam_aux2i(s) + * s += 4 + */ + __pyx_t_3 = __pyx_v_auxtype; + __pyx_t_1 = (__pyx_t_3 == 'i'); + if (!__pyx_t_1) { + __pyx_t_4 = (__pyx_t_3 == 'I'); + __pyx_t_5 = __pyx_t_4; + } else { + __pyx_t_5 = __pyx_t_1; + } + __pyx_t_1 = __pyx_t_5; + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2075 + * s += 2 + * elif auxtype in ('i', 'I'): + * value = bam_aux2i(s) # <<<<<<<<<<<<<< + * s += 4 + * elif auxtype == 'f': + */ + __pyx_t_2 = PyFloat_FromDouble(((float)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2075; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2076 + * elif auxtype in ('i', 'I'): + * value = bam_aux2i(s) + * s += 4 # <<<<<<<<<<<<<< + * elif auxtype == 'f': + * value = bam_aux2f(s) + */ + __pyx_v_s = (__pyx_v_s + 4); + goto __pyx_L8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2077 + * value = bam_aux2i(s) + * s += 4 + * elif auxtype == 'f': # <<<<<<<<<<<<<< + * value = bam_aux2f(s) + * s += 4 + */ + __pyx_t_1 = (__pyx_v_auxtype == 'f'); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2078 + * s += 4 + * elif auxtype == 'f': + * value = bam_aux2f(s) # <<<<<<<<<<<<<< + * s += 4 + * elif auxtype == 'd': + */ + __pyx_t_2 = PyFloat_FromDouble(bam_aux2f(__pyx_v_s)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2078; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2079 + * elif auxtype == 'f': + * value = bam_aux2f(s) + * s += 4 # <<<<<<<<<<<<<< + * elif auxtype == 'd': + * value = bam_aux2d(s) + */ + __pyx_v_s = (__pyx_v_s + 4); + goto __pyx_L8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2080 + * value = bam_aux2f(s) + * s += 4 + * elif auxtype == 'd': # <<<<<<<<<<<<<< + * value = bam_aux2d(s) + * s += 8 + */ + __pyx_t_1 = (__pyx_v_auxtype == 'd'); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2081 + * s += 4 + * elif auxtype == 'd': + * value = bam_aux2d(s) # <<<<<<<<<<<<<< + * s += 8 + * elif auxtype == 'A': + */ + __pyx_t_2 = PyFloat_FromDouble(bam_aux2d(__pyx_v_s)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2081; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2082 + * elif auxtype == 'd': + * value = bam_aux2d(s) + * s += 8 # <<<<<<<<<<<<<< + * elif auxtype == 'A': + * value = "%c" % bam_aux2A(s) + */ + __pyx_v_s = (__pyx_v_s + 8); + goto __pyx_L8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2083 + * value = bam_aux2d(s) + * s += 8 + * elif auxtype == 'A': # <<<<<<<<<<<<<< + * value = "%c" % bam_aux2A(s) + * s += 1 + */ + __pyx_t_1 = (__pyx_v_auxtype == 'A'); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2084 + * s += 8 + * elif auxtype == 'A': + * value = "%c" % bam_aux2A(s) # <<<<<<<<<<<<<< + * s += 1 + * elif auxtype in ('Z', 'H'): + */ + __pyx_t_2 = PyInt_FromLong(bam_aux2A(__pyx_v_s)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2084; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_6 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_110), __pyx_t_2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2084; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = ((PyObject *)__pyx_t_6); + __pyx_t_6 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2085 + * elif auxtype == 'A': + * value = "%c" % bam_aux2A(s) + * s += 1 # <<<<<<<<<<<<<< + * elif auxtype in ('Z', 'H'): + * value = bam_aux2Z(s) + */ + __pyx_v_s = (__pyx_v_s + 1); + goto __pyx_L8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2086 + * value = "%c" % bam_aux2A(s) + * s += 1 + * elif auxtype in ('Z', 'H'): # <<<<<<<<<<<<<< + * value = bam_aux2Z(s) + * # +1 for NULL terminated string + */ + __pyx_t_3 = __pyx_v_auxtype; + __pyx_t_1 = (__pyx_t_3 == 'Z'); + if (!__pyx_t_1) { + __pyx_t_5 = (__pyx_t_3 == 'H'); + __pyx_t_4 = __pyx_t_5; + } else { + __pyx_t_4 = __pyx_t_1; + } + __pyx_t_1 = __pyx_t_4; + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2087 + * s += 1 + * elif auxtype in ('Z', 'H'): + * value = bam_aux2Z(s) # <<<<<<<<<<<<<< + * # +1 for NULL terminated string + * s += len(value) + 1 + */ + __pyx_t_6 = PyBytes_FromString(bam_aux2Z(__pyx_v_s)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2087; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = ((PyObject *)__pyx_t_6); + __pyx_t_6 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2089 + * value = bam_aux2Z(s) + * # +1 for NULL terminated string + * s += len(value) + 1 # <<<<<<<<<<<<<< + * # + * s += 1 + */ + __pyx_t_7 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2089; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_s = (__pyx_v_s + (__pyx_t_7 + 1)); + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2091 + * s += len(value) + 1 + * # + * s += 1 # <<<<<<<<<<<<<< + * + * result.append( (auxtag, value) ) + */ + __pyx_v_s = (__pyx_v_s + 1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2093 + * s += 1 + * + * result.append( (auxtag, value) ) # <<<<<<<<<<<<<< + * + * return result + */ + if (unlikely(__pyx_v_result == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2093; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_6 = PyBytes_FromString(__pyx_v_auxtag); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2093; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_6)); + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2093; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_6)); + __Pyx_INCREF(__pyx_v_value); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value); + __Pyx_GIVEREF(__pyx_v_value); + __pyx_t_6 = 0; + __pyx_t_8 = PyList_Append(__pyx_v_result, ((PyObject *)__pyx_t_2)); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2093; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2095 + * result.append( (auxtag, value) ) + * + * return result # <<<<<<<<<<<<<< + * + * def __set__(self, tags): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_result)); + __pyx_r = ((PyObject *)__pyx_v_result); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_AddTraceback("csamtools.AlignedRead.tags.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_result); + __Pyx_DECREF(__pyx_v_value); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2097 + * return result + * + * def __set__(self, tags): # <<<<<<<<<<<<<< + * cdef char * ctag + * cdef bam1_t * src + */ + +static int __pyx_pf_9csamtools_11AlignedRead_4tags_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tags); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_4tags_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tags) { + bam1_t *__pyx_v_src; + uint8_t *__pyx_v_s; + char *__pyx_v_temp; + int __pyx_v_max_size; + int __pyx_v_size; + int __pyx_v_offset; + PyObject *__pyx_v_buffer; + PyObject *__pyx_v_pytag; + PyObject *__pyx_v_value; + PyObject *__pyx_v_t; + PyObject *__pyx_v_fmt; + PyObject *__pyx_v_pytype; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + 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; + Py_ssize_t __pyx_t_8; + int __pyx_t_9; + PyObject *__pyx_t_10 = NULL; + char *__pyx_t_11; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2097); + __pyx_v_buffer = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_pytag = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_value = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_t = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_fmt = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_pytype = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2106 + * cdef int max_size, size, offset + * + * src = self._delegate # <<<<<<<<<<<<<< + * max_size = 4000 + * offset = 0 + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2107 + * + * src = self._delegate + * max_size = 4000 # <<<<<<<<<<<<<< + * offset = 0 + * + */ + __pyx_v_max_size = 4000; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2108 + * src = self._delegate + * max_size = 4000 + * offset = 0 # <<<<<<<<<<<<<< + * + * if tags != None: + */ + __pyx_v_offset = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2110 + * offset = 0 + * + * if tags != None: # <<<<<<<<<<<<<< + * + * # map samtools code to python.struct code and byte size + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_tags, Py_None, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __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 = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2113 + * + * # map samtools code to python.struct code and byte size + * buffer = ctypes.create_string_buffer(max_size) # <<<<<<<<<<<<<< + * + * for pytag, value in tags: + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__ctypes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s_111); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyInt_FromLong(__pyx_v_max_size); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __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 = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_buffer); + __pyx_v_buffer = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2115 + * buffer = ctypes.create_string_buffer(max_size) + * + * for pytag, value in tags: # <<<<<<<<<<<<<< + * t = type(value) + * if t == types.FloatType: + */ + if (PyList_CheckExact(__pyx_v_tags) || PyTuple_CheckExact(__pyx_v_tags)) { + __pyx_t_5 = 0; __pyx_t_1 = __pyx_v_tags; __Pyx_INCREF(__pyx_t_1); + } else { + __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_tags); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + } + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_1))) { + if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break; + __pyx_t_4 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_4); __pyx_t_5++; + } else if (likely(PyTuple_CheckExact(__pyx_t_1))) { + if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break; + __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_4); __pyx_t_5++; + } else { + __pyx_t_4 = PyIter_Next(__pyx_t_1); + if (!__pyx_t_4) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_4); + } + if (PyTuple_CheckExact(__pyx_t_4) && likely(PyTuple_GET_SIZE(__pyx_t_4) == 2)) { + PyObject* tuple = __pyx_t_4; + __pyx_t_3 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_3); + __pyx_t_6 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_6); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_pytag); + __pyx_v_pytag = __pyx_t_3; + __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_6; + __pyx_t_6 = 0; + } else { + __pyx_t_7 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __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_UnpackItem(__pyx_t_7, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_6 = __Pyx_UnpackItem(__pyx_t_7, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + if (__Pyx_EndUnpack(__pyx_t_7, 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_v_pytag); + __pyx_v_pytag = __pyx_t_3; + __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_6; + __pyx_t_6 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2116 + * + * for pytag, value in tags: + * t = type(value) # <<<<<<<<<<<<<< + * if t == types.FloatType: + * fmt, pytype = "= -127: fmt, pytype = "= -127: fmt, pytype = "= -32767: fmt, pytype = "= -127: fmt, pytype = "= -32767: fmt, pytype = "= -127: fmt, pytype = "= -32767: fmt, pytype = "= -127: fmt, pytype = "= -32767: fmt, pytype = "= -32767: fmt, pytype = " 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); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2126; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2126; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + if (__pyx_t_2) { + __pyx_t_4 = ((PyObject *)__pyx_kp_s_117); + __Pyx_INCREF(__pyx_t_4); + __pyx_t_6 = ((PyObject *)__pyx_n_s__C); + __Pyx_INCREF(__pyx_t_6); + __Pyx_DECREF(__pyx_v_fmt); + __pyx_v_fmt = __pyx_t_4; + __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_pytype); + __pyx_v_pytype = __pyx_t_6; + __pyx_t_6 = 0; + goto __pyx_L11; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2127 + * else: + * if value <= 255: fmt, pytype = " 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value ) + * else: fmt, pytype = " 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value ) # <<<<<<<<<<<<<< + * else: fmt, pytype = " 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value ) + * else: fmt, pytype = " max_size: + * raise NotImplementedError("tags field too large") + */ + __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__struct); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2137; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_6 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__calcsize); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2137; __pyx_clineno = __LINE__; goto __pyx_L1_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 = 2137; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_INCREF(__pyx_v_fmt); + PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_fmt); + __Pyx_GIVEREF(__pyx_v_fmt); + __pyx_t_3 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_t_4), NULL); 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_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __pyx_t_9 = __Pyx_PyInt_AsInt(__pyx_t_3); if (unlikely((__pyx_t_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2137; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_v_size = __pyx_t_9; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2138 + * + * size = struct.calcsize(fmt) + * if offset + size > max_size: # <<<<<<<<<<<<<< + * raise NotImplementedError("tags field too large") + * + */ + __pyx_t_2 = ((__pyx_v_offset + __pyx_v_size) > __pyx_v_max_size); + if (__pyx_t_2) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2139 + * size = struct.calcsize(fmt) + * if offset + size > max_size: + * raise NotImplementedError("tags field too large") # <<<<<<<<<<<<<< + * + * struct.pack_into( fmt, + */ + __pyx_t_3 = PyObject_Call(__pyx_builtin_NotImplementedError, ((PyObject *)__pyx_k_tuple_123), NULL); 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_Raise(__pyx_t_3, 0, 0); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2139; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L13; + } + __pyx_L13:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2141 + * raise NotImplementedError("tags field too large") + * + * struct.pack_into( fmt, # <<<<<<<<<<<<<< + * buffer, + * offset, + */ + __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__struct); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2141; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__pack_into); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2141; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2143 + * struct.pack_into( fmt, + * buffer, + * offset, # <<<<<<<<<<<<<< + * pytag[0], + * pytag[1], + */ + __pyx_t_3 = PyInt_FromLong(__pyx_v_offset); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2143; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2144 + * buffer, + * offset, + * pytag[0], # <<<<<<<<<<<<<< + * pytag[1], + * pytype, + */ + __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_pytag, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2145 + * offset, + * pytag[0], + * pytag[1], # <<<<<<<<<<<<<< + * pytype, + * value ) + */ + __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_pytag, 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2145; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2147 + * pytag[1], + * pytype, + * value ) # <<<<<<<<<<<<<< + * offset += size + * + */ + __pyx_t_10 = PyTuple_New(7); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2141; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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); + PyTuple_SET_ITEM(__pyx_t_10, 2, __pyx_t_3); + __Pyx_GIVEREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_10, 3, __pyx_t_6); + __Pyx_GIVEREF(__pyx_t_6); + PyTuple_SET_ITEM(__pyx_t_10, 4, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + __Pyx_INCREF(__pyx_v_pytype); + PyTuple_SET_ITEM(__pyx_t_10, 5, __pyx_v_pytype); + __Pyx_GIVEREF(__pyx_v_pytype); + __Pyx_INCREF(__pyx_v_value); + PyTuple_SET_ITEM(__pyx_t_10, 6, __pyx_v_value); + __Pyx_GIVEREF(__pyx_v_value); + __pyx_t_3 = 0; + __pyx_t_6 = 0; + __pyx_t_7 = 0; + __pyx_t_7 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_10), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2141; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_10)); __pyx_t_10 = 0; + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2148 + * pytype, + * value ) + * offset += size # <<<<<<<<<<<<<< + * + * # delete the old data and allocate new + */ + __pyx_v_offset = (__pyx_v_offset + __pyx_v_size); + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2156 + * src.l_aux, + * offset, + * bam1_aux( src ) ) # <<<<<<<<<<<<<< + * + * src.l_aux = offset + */ + pysam_bam_update(__pyx_v_src, __pyx_v_src->l_aux, __pyx_v_offset, bam1_aux(__pyx_v_src)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2158 + * bam1_aux( src ) ) + * + * src.l_aux = offset # <<<<<<<<<<<<<< + * + * # copy data only if there is any + */ + __pyx_v_src->l_aux = __pyx_v_offset; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2161 + * + * # copy data only if there is any + * if offset != 0: # <<<<<<<<<<<<<< + * + * # get location of new data + */ + __pyx_t_2 = (__pyx_v_offset != 0); + if (__pyx_t_2) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2164 + * + * # get location of new data + * s = bam1_aux( src ) # <<<<<<<<<<<<<< + * + * # check if there is direct path from buffer.raw to tmp + */ + __pyx_v_s = bam1_aux(__pyx_v_src); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2167 + * + * # check if there is direct path from buffer.raw to tmp + * temp = buffer.raw # <<<<<<<<<<<<<< + * memcpy( s, temp, offset ) + * + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_buffer, __pyx_n_s__raw); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_11 = PyBytes_AsString(__pyx_t_1); if (unlikely((!__pyx_t_11) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_v_temp = __pyx_t_11; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2168 + * # check if there is direct path from buffer.raw to tmp + * temp = buffer.raw + * memcpy( s, temp, offset ) # <<<<<<<<<<<<<< + * + * property flag: + */ + memcpy(__pyx_v_s, __pyx_v_temp, __pyx_v_offset); + goto __pyx_L14; + } + __pyx_L14:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_10); + __Pyx_AddTraceback("csamtools.AlignedRead.tags.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_buffer); + __Pyx_DECREF(__pyx_v_pytag); + __Pyx_DECREF(__pyx_v_value); + __Pyx_DECREF(__pyx_v_t); + __Pyx_DECREF(__pyx_v_fmt); + __Pyx_DECREF(__pyx_v_pytype); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2172 + * property flag: + * """properties flag""" + * def __get__(self): return self._delegate.core.flag # <<<<<<<<<<<<<< + * def __set__(self, flag): self._delegate.core.flag = flag + * + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4flag_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4flag_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2172); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_uint32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.flag.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2173 + * """properties flag""" + * def __get__(self): return self._delegate.core.flag + * def __set__(self, flag): self._delegate.core.flag = flag # <<<<<<<<<<<<<< + * + * property rname: + */ + +static int __pyx_pf_9csamtools_11AlignedRead_4flag_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_flag); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_4flag_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_flag) { + int __pyx_r; + uint32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2173); + __pyx_t_1 = __Pyx_PyInt_from_py_uint32_t(__pyx_v_flag); if (unlikely((__pyx_t_1 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2173; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.flag.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2191 + * + * """ + * def __get__(self): return self._delegate.core.tid # <<<<<<<<<<<<<< + * def __set__(self, tid): self._delegate.core.tid = tid + * + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5rname_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5rname_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2191); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2191; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.rname.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2192 + * """ + * def __get__(self): return self._delegate.core.tid + * def __set__(self, tid): self._delegate.core.tid = tid # <<<<<<<<<<<<<< + * + * property tid: + */ + +static int __pyx_pf_9csamtools_11AlignedRead_5rname_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_5rname_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) { + int __pyx_r; + int32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2192); + __pyx_t_1 = __Pyx_PyInt_from_py_int32_t(__pyx_v_tid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2192; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.tid = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.rname.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2205 + * + * """ + * def __get__(self): return self._delegate.core.tid # <<<<<<<<<<<<<< + * def __set__(self, tid): self._delegate.core.tid = tid + * + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3tid_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3tid_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2205); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2205; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.tid.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2206 + * """ + * def __get__(self): return self._delegate.core.tid + * def __set__(self, tid): self._delegate.core.tid = tid # <<<<<<<<<<<<<< + * + * property pos: + */ + +static int __pyx_pf_9csamtools_11AlignedRead_3tid_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_3tid_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) { + int __pyx_r; + int32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2206); + __pyx_t_1 = __Pyx_PyInt_from_py_int32_t(__pyx_v_tid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2206; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.tid = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.tid.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2210 + * property pos: + * """0-based leftmost coordinate""" + * def __get__(self): return self._delegate.core.pos # <<<<<<<<<<<<<< + * def __set__(self, pos): + * ## setting the cigar string also updates the "bin" attribute + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3pos_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3pos_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2210); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2210; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.pos.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2211 + * """0-based leftmost coordinate""" + * def __get__(self): return self._delegate.core.pos + * def __set__(self, pos): # <<<<<<<<<<<<<< + * ## setting the cigar string also updates the "bin" attribute + * cdef bam1_t * src + */ + +static int __pyx_pf_9csamtools_11AlignedRead_3pos_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_pos); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_3pos_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_pos) { + bam1_t *__pyx_v_src; + int __pyx_r; + int32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2211); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2214 + * ## setting the cigar string also updates the "bin" attribute + * cdef bam1_t * src + * src = self._delegate # <<<<<<<<<<<<<< + * if src.core.n_cigar: + * src.core.bin = bam_reg2bin( src.core.pos, bam_calend( &src.core, bam1_cigar(src)) ) + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2215 + * cdef bam1_t * src + * src = self._delegate + * if src.core.n_cigar: # <<<<<<<<<<<<<< + * src.core.bin = bam_reg2bin( src.core.pos, bam_calend( &src.core, bam1_cigar(src)) ) + * else: + */ + if (__pyx_v_src->core.n_cigar) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2216 + * src = self._delegate + * if src.core.n_cigar: + * src.core.bin = bam_reg2bin( src.core.pos, bam_calend( &src.core, bam1_cigar(src)) ) # <<<<<<<<<<<<<< + * else: + * src.core.bin = bam_reg2bin( src.core.pos, src.core.pos + 1) + */ + __pyx_v_src->core.bin = bam_reg2bin(__pyx_v_src->core.pos, bam_calend((&__pyx_v_src->core), bam1_cigar(__pyx_v_src))); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2218 + * src.core.bin = bam_reg2bin( src.core.pos, bam_calend( &src.core, bam1_cigar(src)) ) + * else: + * src.core.bin = bam_reg2bin( src.core.pos, src.core.pos + 1) # <<<<<<<<<<<<<< + * self._delegate.core.pos = pos + * property bin: + */ + __pyx_v_src->core.bin = bam_reg2bin(__pyx_v_src->core.pos, (__pyx_v_src->core.pos + 1)); + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2219 + * else: + * src.core.bin = bam_reg2bin( src.core.pos, src.core.pos + 1) + * self._delegate.core.pos = pos # <<<<<<<<<<<<<< + * property bin: + * """properties bin""" + */ + __pyx_t_1 = __Pyx_PyInt_from_py_int32_t(__pyx_v_pos); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2219; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.pos = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.pos.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2222 + * property bin: + * """properties bin""" + * def __get__(self): return self._delegate.core.bin # <<<<<<<<<<<<<< + * def __set__(self, bin): self._delegate.core.bin = bin + * property rlen: + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3bin_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_3bin_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2222); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_uint32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.bin); 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_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.bin.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2223 + * """properties bin""" + * def __get__(self): return self._delegate.core.bin + * def __set__(self, bin): self._delegate.core.bin = bin # <<<<<<<<<<<<<< + * property rlen: + * '''length of the read (read only). Returns 0 if not given.''' + */ + +static int __pyx_pf_9csamtools_11AlignedRead_3bin_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_bin); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_3bin_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_bin) { + int __pyx_r; + uint32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2223); + __pyx_t_1 = __Pyx_PyInt_from_py_uint32_t(__pyx_v_bin); if (unlikely((__pyx_t_1 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2223; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.bin = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.bin.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2226 + * property rlen: + * '''length of the read (read only). Returns 0 if not given.''' + * def __get__(self): return self._delegate.core.l_qseq # <<<<<<<<<<<<<< + * property aend: + * '''aligned end position of the read (read only). Returns + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4rlen_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4rlen_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2226); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.l_qseq); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2226; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.rlen.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2230 + * '''aligned end position of the read (read only). Returns + * None if not available.''' + * def __get__(self): # <<<<<<<<<<<<<< + * cdef bam1_t * src + * src = self._delegate + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4aend_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4aend_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + int __pyx_t_5; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2230); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2232 + * def __get__(self): + * cdef bam1_t * src + * src = self._delegate # <<<<<<<<<<<<<< + * if (self.flag & BAM_FUNMAP) or src.core.n_cigar == 0: + * return None + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2233 + * cdef bam1_t * src + * src = self._delegate + * if (self.flag & BAM_FUNMAP) or src.core.n_cigar == 0: # <<<<<<<<<<<<<< + * return None + * return bam_calend(&src.core, bam1_cigar(src)) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2233; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2233; __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 = 2233; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (!__pyx_t_3) { + __pyx_t_4 = (__pyx_v_src->core.n_cigar == 0); + __pyx_t_5 = __pyx_t_4; + } else { + __pyx_t_5 = __pyx_t_3; + } + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2234 + * src = self._delegate + * if (self.flag & BAM_FUNMAP) or src.core.n_cigar == 0: + * return None # <<<<<<<<<<<<<< + * return bam_calend(&src.core, bam1_cigar(src)) + * property alen: + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2235 + * if (self.flag & BAM_FUNMAP) or src.core.n_cigar == 0: + * return None + * return bam_calend(&src.core, bam1_cigar(src)) # <<<<<<<<<<<<<< + * property alen: + * '''aligned length of the read (read only). Returns None if + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = __Pyx_PyInt_to_py_uint32_t(bam_calend((&__pyx_v_src->core), bam1_cigar(__pyx_v_src))); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2235; __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_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("csamtools.AlignedRead.aend.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2239 + * '''aligned length of the read (read only). Returns None if + * not available.''' + * def __get__(self): # <<<<<<<<<<<<<< + * cdef bam1_t * src + * src = self._delegate + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4alen_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4alen_0__get__(PyObject *__pyx_v_self) { + bam1_t *__pyx_v_src; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + int __pyx_t_4; + int __pyx_t_5; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2239); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2241 + * def __get__(self): + * cdef bam1_t * src + * src = self._delegate # <<<<<<<<<<<<<< + * if (self.flag & BAM_FUNMAP) or src.core.n_cigar == 0: + * return None + */ + __pyx_v_src = ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2242 + * cdef bam1_t * src + * src = self._delegate + * if (self.flag & BAM_FUNMAP) or src.core.n_cigar == 0: # <<<<<<<<<<<<<< + * return None + * return bam_calend(&src.core, + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2242; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2242; __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 = 2242; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (!__pyx_t_3) { + __pyx_t_4 = (__pyx_v_src->core.n_cigar == 0); + __pyx_t_5 = __pyx_t_4; + } else { + __pyx_t_5 = __pyx_t_3; + } + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2243 + * src = self._delegate + * if (self.flag & BAM_FUNMAP) or src.core.n_cigar == 0: + * return None # <<<<<<<<<<<<<< + * return bam_calend(&src.core, + * bam1_cigar(src)) - \ + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2244 + * if (self.flag & BAM_FUNMAP) or src.core.n_cigar == 0: + * return None + * return bam_calend(&src.core, # <<<<<<<<<<<<<< + * bam1_cigar(src)) - \ + * self._delegate.core.pos + */ + __Pyx_XDECREF(__pyx_r); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2246 + * return bam_calend(&src.core, + * bam1_cigar(src)) - \ + * self._delegate.core.pos # <<<<<<<<<<<<<< + * + * property mapq: + */ + __pyx_t_2 = __Pyx_PyInt_to_py_int32_t((bam_calend((&__pyx_v_src->core), bam1_cigar(__pyx_v_src)) - ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.pos)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2245; __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_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("csamtools.AlignedRead.alen.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2250 + * property mapq: + * """mapping quality""" + * def __get__(self): return self._delegate.core.qual # <<<<<<<<<<<<<< + * def __set__(self, qual): self._delegate.core.qual = qual + * property mrnm: + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4mapq_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4mapq_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2250); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_uint32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.qual); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2250; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.mapq.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2251 + * """mapping quality""" + * def __get__(self): return self._delegate.core.qual + * def __set__(self, qual): self._delegate.core.qual = qual # <<<<<<<<<<<<<< + * property mrnm: + * """the :term:`reference` id of the mate """ + */ + +static int __pyx_pf_9csamtools_11AlignedRead_4mapq_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_4mapq_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual) { + int __pyx_r; + uint32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2251); + __pyx_t_1 = __Pyx_PyInt_from_py_uint32_t(__pyx_v_qual); if (unlikely((__pyx_t_1 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2251; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.qual = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.mapq.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2254 + * property mrnm: + * """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: + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4mrnm_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4mrnm_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2254); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.mtid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2254; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.mrnm.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2255 + * """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""" + */ + +static int __pyx_pf_9csamtools_11AlignedRead_4mrnm_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_4mrnm_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid) { + int __pyx_r; + int32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2255); + __pyx_t_1 = __Pyx_PyInt_from_py_int32_t(__pyx_v_mtid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2255; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.mtid = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.mrnm.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2258 + * property mpos: + * """the position of the mate""" + * def __get__(self): return self._delegate.core.mpos # <<<<<<<<<<<<<< + * def __set__(self, mpos): self._delegate.core.mpos = mpos + * property isize: + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4mpos_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_4mpos_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2258); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.mpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2258; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.mpos.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2259 + * """the position of the mate""" + * def __get__(self): return self._delegate.core.mpos + * def __set__(self, mpos): self._delegate.core.mpos = mpos # <<<<<<<<<<<<<< + * property isize: + * """the insert size""" + */ + +static int __pyx_pf_9csamtools_11AlignedRead_4mpos_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_4mpos_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos) { + int __pyx_r; + int32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2259); + __pyx_t_1 = __Pyx_PyInt_from_py_int32_t(__pyx_v_mpos); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2259; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.mpos = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.mpos.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2262 + * property isize: + * """the insert size""" + * def __get__(self): return self._delegate.core.isize # <<<<<<<<<<<<<< + * def __set__(self, isize): self._delegate.core.isize = isize + * property is_paired: + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5isize_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5isize_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2262); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_int32_t(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.isize); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2262; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.isize.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2263 + * """the insert size""" + * 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""" + */ + +static int __pyx_pf_9csamtools_11AlignedRead_5isize_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_5isize_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize) { + int __pyx_r; + int32_t __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2263); + __pyx_t_1 = __Pyx_PyInt_from_py_int32_t(__pyx_v_isize); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2263; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.isize = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.isize.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2266 + * property is_paired: + * """true if read is paired in sequencing""" + * def __get__(self): return (self._delegate.core.flag & BAM_FPAIRED) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FPAIRED + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_9is_paired_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_9is_paired_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2266); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyBool_FromLong(((((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & 1) != 0)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2266; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.AlignedRead.is_paired.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2267 + * """true if read is paired in sequencing""" + * def __get__(self): return (self._delegate.core.flag & BAM_FPAIRED) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FPAIRED + * else: self._delegate.core.flag &= ~BAM_FPAIRED + */ + +static int __pyx_pf_9csamtools_11AlignedRead_9is_paired_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_9is_paired_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2267); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2268 + * def __get__(self): return (self._delegate.core.flag & BAM_FPAIRED) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FPAIRED # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FPAIRED + * property is_proper_pair: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2268; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 1); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2269 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FPAIRED + * else: self._delegate.core.flag &= ~BAM_FPAIRED # <<<<<<<<<<<<<< + * property is_proper_pair: + * """true if read is mapped in a proper pair""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~1)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_paired.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2272 + * 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): + * if val: self._delegate.core.flag |= BAM_FPROPER_PAIR + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_14is_proper_pair_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_14is_proper_pair_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2272); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2272; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2272; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2272; __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; + + __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("csamtools.AlignedRead.is_proper_pair.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2273 + * """true if read is mapped in a proper pair""" + * def __get__(self): return (self.flag & BAM_FPROPER_PAIR) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FPROPER_PAIR + * else: self._delegate.core.flag &= ~BAM_FPROPER_PAIR + */ + +static int __pyx_pf_9csamtools_11AlignedRead_14is_proper_pair_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_14is_proper_pair_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2273); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2274 + * def __get__(self): return (self.flag & BAM_FPROPER_PAIR) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FPROPER_PAIR # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FPROPER_PAIR + * property is_unmapped: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2274; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 2); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2275 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FPROPER_PAIR + * else: self._delegate.core.flag &= ~BAM_FPROPER_PAIR # <<<<<<<<<<<<<< + * property is_unmapped: + * """true if read itself is unmapped""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~2)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_proper_pair.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2278 + * property is_unmapped: + * """true if read itself is unmapped""" + * def __get__(self): return (self.flag & BAM_FUNMAP) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FUNMAP + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_11is_unmapped_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_11is_unmapped_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2278); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2278; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2278; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2278; __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; + + __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("csamtools.AlignedRead.is_unmapped.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2279 + * """true if read itself is unmapped""" + * def __get__(self): return (self.flag & BAM_FUNMAP) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FUNMAP + * else: self._delegate.core.flag &= ~BAM_FUNMAP + */ + +static int __pyx_pf_9csamtools_11AlignedRead_11is_unmapped_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_11is_unmapped_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2279); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2280 + * def __get__(self): return (self.flag & BAM_FUNMAP) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FUNMAP # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FUNMAP + * property mate_is_unmapped: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2280; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 4); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2281 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FUNMAP + * else: self._delegate.core.flag &= ~BAM_FUNMAP # <<<<<<<<<<<<<< + * property mate_is_unmapped: + * """true if the mate is unmapped""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~4)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_unmapped.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2284 + * property mate_is_unmapped: + * """true if the mate is unmapped""" + * def __get__(self): return (self.flag & BAM_FMUNMAP) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FMUNMAP + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_16mate_is_unmapped_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_16mate_is_unmapped_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2284); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2284; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_8); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2284; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2284; __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; + + __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("csamtools.AlignedRead.mate_is_unmapped.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2285 + * """true if the mate is unmapped""" + * def __get__(self): return (self.flag & BAM_FMUNMAP) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FMUNMAP + * else: self._delegate.core.flag &= ~BAM_FMUNMAP + */ + +static int __pyx_pf_9csamtools_11AlignedRead_16mate_is_unmapped_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_16mate_is_unmapped_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2285); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2286 + * def __get__(self): return (self.flag & BAM_FMUNMAP) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FMUNMAP # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FMUNMAP + * property is_reverse: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2286; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 8); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2287 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FMUNMAP + * else: self._delegate.core.flag &= ~BAM_FMUNMAP # <<<<<<<<<<<<<< + * property is_reverse: + * """true if read is mapped to reverse strand""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~8)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.mate_is_unmapped.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2290 + * property is_reverse: + * """true if read is mapped to reverse strand""" + * def __get__(self): return (self.flag & BAM_FREVERSE) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREVERSE + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_10is_reverse_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_10is_reverse_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2290); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2290; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_16); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2290; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2290; __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; + + __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("csamtools.AlignedRead.is_reverse.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2291 + * """true if read is mapped to reverse strand""" + * def __get__(self): return (self.flag & BAM_FREVERSE) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FREVERSE + * else: self._delegate.core.flag &= ~BAM_FREVERSE + */ + +static int __pyx_pf_9csamtools_11AlignedRead_10is_reverse_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_10is_reverse_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2291); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2292 + * def __get__(self): return (self.flag & BAM_FREVERSE) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREVERSE # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FREVERSE + * property mate_is_reverse: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2292; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 16); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2293 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREVERSE + * else: self._delegate.core.flag &= ~BAM_FREVERSE # <<<<<<<<<<<<<< + * property mate_is_reverse: + * """true is read is mapped to reverse strand""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~16)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_reverse.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2296 + * 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): + * if val: self._delegate.core.flag |= BAM_FMREVERSE + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_15mate_is_reverse_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_15mate_is_reverse_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2296); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2296; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_32); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2296; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2296; __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; + + __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("csamtools.AlignedRead.mate_is_reverse.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2297 + * """true is read is mapped to reverse strand""" + * def __get__(self): return (self.flag & BAM_FMREVERSE) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FMREVERSE + * else: self._delegate.core.flag &= ~BAM_FMREVERSE + */ + +static int __pyx_pf_9csamtools_11AlignedRead_15mate_is_reverse_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_15mate_is_reverse_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2297); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2298 + * def __get__(self): return (self.flag & BAM_FMREVERSE) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FMREVERSE # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FMREVERSE + * property is_read1: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2298; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 32); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2299 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FMREVERSE + * else: self._delegate.core.flag &= ~BAM_FMREVERSE # <<<<<<<<<<<<<< + * property is_read1: + * """true if this is read1""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~32)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.mate_is_reverse.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2302 + * property is_read1: + * """true if this is read1""" + * def __get__(self): return (self.flag & BAM_FREAD1) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREAD1 + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_8is_read1_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_8is_read1_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2302); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2302; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_64); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2302; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2302; __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; + + __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("csamtools.AlignedRead.is_read1.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2303 + * """true if this is read1""" + * def __get__(self): return (self.flag & BAM_FREAD1) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FREAD1 + * else: self._delegate.core.flag &= ~BAM_FREAD1 + */ + +static int __pyx_pf_9csamtools_11AlignedRead_8is_read1_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_8is_read1_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2303); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2304 + * def __get__(self): return (self.flag & BAM_FREAD1) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREAD1 # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FREAD1 + * property is_read2: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2304; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 64); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2305 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREAD1 + * else: self._delegate.core.flag &= ~BAM_FREAD1 # <<<<<<<<<<<<<< + * property is_read2: + * """true if this is read2""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~64)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_read1.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2308 + * property is_read2: + * """true if this is read2""" + * def __get__(self): return (self.flag & BAM_FREAD2) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREAD2 + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_8is_read2_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_8is_read2_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2308); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2308; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_128); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2308; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2308; __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; + + __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("csamtools.AlignedRead.is_read2.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2309 + * """true if this is read2""" + * def __get__(self): return (self.flag & BAM_FREAD2) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FREAD2 + * else: self._delegate.core.flag &= ~BAM_FREAD2 + */ + +static int __pyx_pf_9csamtools_11AlignedRead_8is_read2_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_8is_read2_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2309); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2310 + * def __get__(self): return (self.flag & BAM_FREAD2) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREAD2 # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FREAD2 + * property is_secondary: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2310; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 128); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2311 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FREAD2 + * else: self._delegate.core.flag &= ~BAM_FREAD2 # <<<<<<<<<<<<<< + * property is_secondary: + * """true if not primary alignment""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~128)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_read2.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2314 + * property is_secondary: + * """true if not primary alignment""" + * def __get__(self): return (self.flag & BAM_FSECONDARY) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FSECONDARY + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_12is_secondary_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_12is_secondary_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2314); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); 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_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_256); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2314; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); 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_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + __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("csamtools.AlignedRead.is_secondary.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2315 + * """true if not primary alignment""" + * def __get__(self): return (self.flag & BAM_FSECONDARY) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FSECONDARY + * else: self._delegate.core.flag &= ~BAM_FSECONDARY + */ + +static int __pyx_pf_9csamtools_11AlignedRead_12is_secondary_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_12is_secondary_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2315); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2316 + * def __get__(self): return (self.flag & BAM_FSECONDARY) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FSECONDARY # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FSECONDARY + * property is_qcfail: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2316; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 256); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2317 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FSECONDARY + * else: self._delegate.core.flag &= ~BAM_FSECONDARY # <<<<<<<<<<<<<< + * property is_qcfail: + * """true if QC failure""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~256)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_secondary.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2320 + * property is_qcfail: + * """true if QC failure""" + * def __get__(self): return (self.flag & BAM_FQCFAIL) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FQCFAIL + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_9is_qcfail_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_9is_qcfail_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2320); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2320; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_512); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2320; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2320; __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; + + __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("csamtools.AlignedRead.is_qcfail.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2321 + * """true if QC failure""" + * def __get__(self): return (self.flag & BAM_FQCFAIL) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FQCFAIL + * else: self._delegate.core.flag &= ~BAM_FQCFAIL + */ + +static int __pyx_pf_9csamtools_11AlignedRead_9is_qcfail_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_9is_qcfail_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2321); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2322 + * def __get__(self): return (self.flag & BAM_FQCFAIL) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FQCFAIL # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FQCFAIL + * property is_duplicate: + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2322; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 512); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2323 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FQCFAIL + * else: self._delegate.core.flag &= ~BAM_FQCFAIL # <<<<<<<<<<<<<< + * property is_duplicate: + * """ true if optical or PCR duplicate""" + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~512)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_qcfail.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2326 + * property is_duplicate: + * """ true if optical or PCR duplicate""" + * def __get__(self): return (self.flag & BAM_FDUP) != 0 # <<<<<<<<<<<<<< + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FDUP + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_12is_duplicate_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11AlignedRead_12is_duplicate_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2326); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2326; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyNumber_And(__pyx_t_1, __pyx_int_1024); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2326; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2326; __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; + + __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("csamtools.AlignedRead.is_duplicate.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2327 + * """ true if optical or PCR duplicate""" + * def __get__(self): return (self.flag & BAM_FDUP) != 0 + * def __set__(self,val): # <<<<<<<<<<<<<< + * if val: self._delegate.core.flag |= BAM_FDUP + * else: self._delegate.core.flag &= ~BAM_FDUP + */ + +static int __pyx_pf_9csamtools_11AlignedRead_12is_duplicate_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/ +static int __pyx_pf_9csamtools_11AlignedRead_12is_duplicate_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2327); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2328 + * def __get__(self): return (self.flag & BAM_FDUP) != 0 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FDUP # <<<<<<<<<<<<<< + * else: self._delegate.core.flag &= ~BAM_FDUP + * + */ + __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_val); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2328; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag | 1024); + goto __pyx_L5; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2329 + * def __set__(self,val): + * if val: self._delegate.core.flag |= BAM_FDUP + * else: self._delegate.core.flag &= ~BAM_FDUP # <<<<<<<<<<<<<< + * + * def opt(self, tag): + */ + ((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag = (((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate->core.flag & (~1024)); + } + __pyx_L5:; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.AlignedRead.is_duplicate.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2331 + * else: self._delegate.core.flag &= ~BAM_FDUP + * + * def opt(self, tag): # <<<<<<<<<<<<<< + * """retrieves optional data given a two-letter *tag*""" + * #see bam_aux.c: bam_aux_get() and bam_aux2i() etc + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5opt(PyObject *__pyx_v_self, PyObject *__pyx_v_tag); /*proto*/ +static char __pyx_doc_9csamtools_11AlignedRead_5opt[] = "retrieves optional data given a two-letter *tag*"; +static PyObject *__pyx_pf_9csamtools_11AlignedRead_5opt(PyObject *__pyx_v_self, PyObject *__pyx_v_tag) { + uint8_t *__pyx_v_v; + PyObject *__pyx_v_type; + PyObject *__pyx_r = NULL; + char *__pyx_t_1; + int __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + int __pyx_t_5; + int __pyx_t_6; + int __pyx_t_7; + int __pyx_t_8; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("opt"); + __Pyx_TraceCall("opt", __pyx_f[0], 2331); + __pyx_v_type = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2335 + * #see bam_aux.c: bam_aux_get() and bam_aux2i() etc + * cdef uint8_t * v + * v = bam_aux_get(self._delegate, tag) # <<<<<<<<<<<<<< + * if v == NULL: raise KeyError( "tag '%s' not present" % tag ) + * type = chr(v[0]) + */ + __pyx_t_1 = PyBytes_AsString(__pyx_v_tag); if (unlikely((!__pyx_t_1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2335; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_v = bam_aux_get(((struct __pyx_obj_9csamtools_AlignedRead *)__pyx_v_self)->_delegate, __pyx_t_1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2336 + * cdef uint8_t * v + * v = bam_aux_get(self._delegate, tag) + * if v == NULL: raise KeyError( "tag '%s' not present" % tag ) # <<<<<<<<<<<<<< + * type = chr(v[0]) + * if type == 'c' or type == 'C' or type == 's' or type == 'S': + */ + __pyx_t_2 = (__pyx_v_v == NULL); + if (__pyx_t_2) { + __pyx_t_3 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_124), __pyx_v_tag); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2336; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2336; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_t_3)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_3)); + __pyx_t_3 = 0; + __pyx_t_3 = PyObject_Call(__pyx_builtin_KeyError, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2336; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __Pyx_Raise(__pyx_t_3, 0, 0); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2336; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2337 + * v = bam_aux_get(self._delegate, tag) + * if v == NULL: raise KeyError( "tag '%s' not present" % tag ) + * type = chr(v[0]) # <<<<<<<<<<<<<< + * if type == 'c' or type == 'C' or type == 's' or type == 'S': + * return bam_aux2i(v) + */ + __pyx_t_3 = __Pyx_PyInt_to_py_uint8_t((__pyx_v_v[0])); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2337; __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 = 2337; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_builtin_chr, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2337; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_type); + __pyx_v_type = __pyx_t_3; + __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2338 + * if v == NULL: raise KeyError( "tag '%s' not present" % tag ) + * type = chr(v[0]) + * if type == 'c' or type == 'C' or type == 's' or type == 'S': # <<<<<<<<<<<<<< + * return bam_aux2i(v) + * elif type == 'i' or type == 'I': + */ + __pyx_t_3 = PyObject_RichCompare(__pyx_v_type, ((PyObject *)__pyx_n_s__c), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2338; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2338; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + if (!__pyx_t_2) { + __pyx_t_3 = PyObject_RichCompare(__pyx_v_type, ((PyObject *)__pyx_n_s__C), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2338; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2338; __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_type, ((PyObject *)__pyx_n_s__s), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2338; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2338; __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_type, ((PyObject *)__pyx_n_s__S), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2338; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2338; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 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_2; + } + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2339 + * type = chr(v[0]) + * if type == 'c' or type == 'C' or type == 's' or type == 'S': + * return bam_aux2i(v) # <<<<<<<<<<<<<< + * elif type == 'i' or type == 'I': + * return bam_aux2i(v) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = PyInt_FromLong(((int)bam_aux2i(__pyx_v_v))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2339; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + goto __pyx_L6; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2340 + * if type == 'c' or type == 'C' or type == 's' or type == 'S': + * return bam_aux2i(v) + * elif type == 'i' or type == 'I': # <<<<<<<<<<<<<< + * return bam_aux2i(v) + * elif type == 'f' or type == 'F': + */ + __pyx_t_3 = PyObject_RichCompare(__pyx_v_type, ((PyObject *)__pyx_n_s__i), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2340; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2340; __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_type, ((PyObject *)__pyx_n_s__I), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2340; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2340; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_6 = __pyx_t_2; + } else { + __pyx_t_6 = __pyx_t_5; + } + if (__pyx_t_6) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2341 + * return bam_aux2i(v) + * elif type == 'i' or type == 'I': + * return bam_aux2i(v) # <<<<<<<<<<<<<< + * elif type == 'f' or type == 'F': + * return bam_aux2f(v) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = __Pyx_PyInt_to_py_int32_t(bam_aux2i(__pyx_v_v)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2341; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + goto __pyx_L6; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2342 + * elif type == 'i' or type == 'I': + * return bam_aux2i(v) + * elif type == 'f' or type == 'F': # <<<<<<<<<<<<<< + * return bam_aux2f(v) + * elif type == 'd' or type == 'D': + */ + __pyx_t_3 = PyObject_RichCompare(__pyx_v_type, ((PyObject *)__pyx_n_s__f), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2342; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2342; __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_type, ((PyObject *)__pyx_n_s__F), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2342; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2342; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_2 = __pyx_t_5; + } else { + __pyx_t_2 = __pyx_t_6; + } + if (__pyx_t_2) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2343 + * return bam_aux2i(v) + * elif type == 'f' or type == 'F': + * return bam_aux2f(v) # <<<<<<<<<<<<<< + * elif type == 'd' or type == 'D': + * return bam_aux2d(v) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = PyFloat_FromDouble(bam_aux2f(__pyx_v_v)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2343; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + goto __pyx_L6; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2344 + * elif type == 'f' or type == 'F': + * return bam_aux2f(v) + * elif type == 'd' or type == 'D': # <<<<<<<<<<<<<< + * return bam_aux2d(v) + * elif type == 'A': + */ + __pyx_t_3 = PyObject_RichCompare(__pyx_v_type, ((PyObject *)__pyx_n_s__d), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2344; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2344; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + if (!__pyx_t_2) { + __pyx_t_3 = PyObject_RichCompare(__pyx_v_type, ((PyObject *)__pyx_n_s__D), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2344; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2344; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_5 = __pyx_t_6; + } else { + __pyx_t_5 = __pyx_t_2; + } + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2345 + * return bam_aux2f(v) + * elif type == 'd' or type == 'D': + * return bam_aux2d(v) # <<<<<<<<<<<<<< + * elif type == 'A': + * # there might a more efficient way + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = PyFloat_FromDouble(bam_aux2d(__pyx_v_v)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2345; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + goto __pyx_L6; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2346 + * elif type == 'd' or type == 'D': + * return bam_aux2d(v) + * elif type == 'A': # <<<<<<<<<<<<<< + * # there might a more efficient way + * # to convert a char into a string + */ + __pyx_t_3 = PyObject_RichCompare(__pyx_v_type, ((PyObject *)__pyx_n_s__A), Py_EQ); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2346; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2346; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2349 + * # there might a more efficient way + * # to convert a char into a string + * return '%c' % bam_aux2A(v) # <<<<<<<<<<<<<< + * elif type == 'Z': + * return bam_aux2Z(v) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_3 = PyInt_FromLong(bam_aux2A(__pyx_v_v)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_4 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_110), __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_r = ((PyObject *)__pyx_t_4); + __pyx_t_4 = 0; + goto __pyx_L0; + goto __pyx_L6; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2350 + * # to convert a char into a string + * return '%c' % bam_aux2A(v) + * elif type == 'Z': # <<<<<<<<<<<<<< + * return bam_aux2Z(v) + * + */ + __pyx_t_4 = PyObject_RichCompare(__pyx_v_type, ((PyObject *)__pyx_n_s__Z), Py_EQ); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2351 + * return '%c' % bam_aux2A(v) + * elif type == 'Z': + * return bam_aux2Z(v) # <<<<<<<<<<<<<< + * + * def fancy_str (self): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_4 = PyBytes_FromString(bam_aux2Z(__pyx_v_v)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2351; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __pyx_r = ((PyObject *)__pyx_t_4); + __pyx_t_4 = 0; + goto __pyx_L0; + goto __pyx_L6; + } + __pyx_L6:; + + __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_AddTraceback("csamtools.AlignedRead.opt"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_type); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2353 + * return bam_aux2Z(v) + * + * def fancy_str (self): # <<<<<<<<<<<<<< + * """returns list of fieldnames/values in pretty format for debugging + * """ + */ + +static PyObject *__pyx_pf_9csamtools_11AlignedRead_6fancy_str(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static char __pyx_doc_9csamtools_11AlignedRead_6fancy_str[] = "returns list of fieldnames/values in pretty format for debugging\n "; +static PyObject *__pyx_pf_9csamtools_11AlignedRead_6fancy_str(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_v_ret_string; + PyObject *__pyx_v_field_names; + PyObject *__pyx_v_fields_names_in_order; + PyObject *__pyx_v_f; + PyObject *__pyx_r = NULL; + 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; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("fancy_str"); + __Pyx_TraceCall("fancy_str", __pyx_f[0], 2353); + __pyx_v_ret_string = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_field_names = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_fields_names_in_order = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + __pyx_v_f = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2356 + * """returns list of fieldnames/values in pretty format for debugging + * """ + * ret_string = [] # <<<<<<<<<<<<<< + * field_names = { + * "tid": "Contig index", + */ + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2356; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__pyx_v_ret_string)); + __pyx_v_ret_string = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2357 + * """ + * ret_string = [] + * 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 = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__tid), ((PyObject *)__pyx_kp_s_125)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__pos), ((PyObject *)__pyx_kp_s_126)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__mtid), ((PyObject *)__pyx_kp_s_127)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__mpos), ((PyObject *)__pyx_kp_s_128)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__isize), ((PyObject *)__pyx_kp_s_129)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__flag), ((PyObject *)__pyx_kp_s_130)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__n_cigar), ((PyObject *)__pyx_kp_s_131)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__cigar), ((PyObject *)__pyx_kp_s_132)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__qual), ((PyObject *)__pyx_kp_s_133)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__bin), ((PyObject *)__pyx_kp_s_134)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__l_qname), ((PyObject *)__pyx_kp_s_135)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__qname), ((PyObject *)__pyx_kp_s_136)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__l_qseq), ((PyObject *)__pyx_kp_s_137)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__qseq), ((PyObject *)__pyx_kp_s_138)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__bqual), ((PyObject *)__pyx_kp_s_139)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__l_aux), ((PyObject *)__pyx_kp_s_140)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__m_data), ((PyObject *)__pyx_kp_s_141)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__data_len), ((PyObject *)__pyx_kp_s_142)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2357; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_v_field_names)); + __pyx_v_field_names = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2377 + * "data_len": "Current 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_aux", "m_data", "data_len"] + */ + __pyx_t_1 = PyList_New(18); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2377; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__tid)); + PyList_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_n_s__tid)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__tid)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__pos)); + PyList_SET_ITEM(__pyx_t_1, 1, ((PyObject *)__pyx_n_s__pos)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__pos)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__mtid)); + PyList_SET_ITEM(__pyx_t_1, 2, ((PyObject *)__pyx_n_s__mtid)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__mtid)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__mpos)); + PyList_SET_ITEM(__pyx_t_1, 3, ((PyObject *)__pyx_n_s__mpos)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__mpos)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__isize)); + PyList_SET_ITEM(__pyx_t_1, 4, ((PyObject *)__pyx_n_s__isize)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__isize)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__flag)); + PyList_SET_ITEM(__pyx_t_1, 5, ((PyObject *)__pyx_n_s__flag)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__flag)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__n_cigar)); + PyList_SET_ITEM(__pyx_t_1, 6, ((PyObject *)__pyx_n_s__n_cigar)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__n_cigar)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__cigar)); + PyList_SET_ITEM(__pyx_t_1, 7, ((PyObject *)__pyx_n_s__cigar)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__cigar)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__qual)); + PyList_SET_ITEM(__pyx_t_1, 8, ((PyObject *)__pyx_n_s__qual)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__qual)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__bin)); + PyList_SET_ITEM(__pyx_t_1, 9, ((PyObject *)__pyx_n_s__bin)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__bin)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__l_qname)); + PyList_SET_ITEM(__pyx_t_1, 10, ((PyObject *)__pyx_n_s__l_qname)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__l_qname)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__qname)); + PyList_SET_ITEM(__pyx_t_1, 11, ((PyObject *)__pyx_n_s__qname)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__qname)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__l_qseq)); + PyList_SET_ITEM(__pyx_t_1, 12, ((PyObject *)__pyx_n_s__l_qseq)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__l_qseq)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__qseq)); + PyList_SET_ITEM(__pyx_t_1, 13, ((PyObject *)__pyx_n_s__qseq)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__qseq)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__bqual)); + PyList_SET_ITEM(__pyx_t_1, 14, ((PyObject *)__pyx_n_s__bqual)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__bqual)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__l_aux)); + PyList_SET_ITEM(__pyx_t_1, 15, ((PyObject *)__pyx_n_s__l_aux)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__l_aux)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__m_data)); + PyList_SET_ITEM(__pyx_t_1, 16, ((PyObject *)__pyx_n_s__m_data)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__m_data)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__data_len)); + PyList_SET_ITEM(__pyx_t_1, 17, ((PyObject *)__pyx_n_s__data_len)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__data_len)); + __Pyx_DECREF(((PyObject *)__pyx_v_fields_names_in_order)); + __pyx_v_fields_names_in_order = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2381 + * "l_qseq", "qseq", "bqual", "l_aux", "m_data", "data_len"] + * + * for f in fields_names_in_order: # <<<<<<<<<<<<<< + * if not f in self.__dict__: + * continue + */ + if (unlikely(__pyx_v_fields_names_in_order == Py_None)) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2381; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = 0; __pyx_t_1 = ((PyObject *)__pyx_v_fields_names_in_order); __Pyx_INCREF(__pyx_t_1); + for (;;) { + if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_1)) break; + __pyx_t_3 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_3); __pyx_t_2++; + __Pyx_DECREF(__pyx_v_f); + __pyx_v_f = __pyx_t_3; + __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2382 + * + * for f in fields_names_in_order: + * if not f in self.__dict__: # <<<<<<<<<<<<<< + * continue + * ret_string.append("%-30s %-10s= %s" % (field_names[f], "(" + f + ")", self.__getattribute__(f))) + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s____dict__); 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_4 = ((PySequence_Contains(__pyx_t_3, __pyx_v_f))); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_5 = (!__pyx_t_4); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2383 + * for f in fields_names_in_order: + * if not f in self.__dict__: + * continue # <<<<<<<<<<<<<< + * ret_string.append("%-30s %-10s= %s" % (field_names[f], "(" + f + ")", self.__getattribute__(f))) + * + */ + goto __pyx_L5_continue; + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2384 + * if not f in self.__dict__: + * continue + * ret_string.append("%-30s %-10s= %s" % (field_names[f], "(" + f + ")", self.__getattribute__(f))) # <<<<<<<<<<<<<< + * + * for f in self.__dict__: + */ + if (unlikely(__pyx_v_ret_string == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_3 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_field_names), __pyx_v_f); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_6 = PyNumber_Add(((PyObject *)__pyx_kp_s_144), __pyx_v_f); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_7 = PyNumber_Add(__pyx_t_6, ((PyObject *)__pyx_kp_s_145)); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __pyx_t_6 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s____getattribute__); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __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 = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_t_8), NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_8)); __pyx_t_8 = 0; + __pyx_t_8 = PyTuple_New(3); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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; + __pyx_t_9 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_143), ((PyObject *)__pyx_t_8)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_9)); + __Pyx_DECREF(((PyObject *)__pyx_t_8)); __pyx_t_8 = 0; + __pyx_t_10 = PyList_Append(__pyx_v_ret_string, ((PyObject *)__pyx_t_9)); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2384; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_9)); __pyx_t_9 = 0; + __pyx_L5_continue:; + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2386 + * ret_string.append("%-30s %-10s= %s" % (field_names[f], "(" + f + ")", self.__getattribute__(f))) + * + * for f in self.__dict__: # <<<<<<<<<<<<<< + * if not f in field_names: + * ret_string.append("%-30s %-10s= %s" % (f, "", self.__getattribute__(f))) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s____dict__); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2386; __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 = 0; __pyx_t_9 = __pyx_t_1; __Pyx_INCREF(__pyx_t_9); + } else { + __pyx_t_2 = -1; __pyx_t_9 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2386; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + for (;;) { + if (likely(PyList_CheckExact(__pyx_t_9))) { + if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_9)) break; + __pyx_t_1 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_2); __Pyx_INCREF(__pyx_t_1); __pyx_t_2++; + } else if (likely(PyTuple_CheckExact(__pyx_t_9))) { + if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_9)) break; + __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_9, __pyx_t_2); __Pyx_INCREF(__pyx_t_1); __pyx_t_2++; + } else { + __pyx_t_1 = PyIter_Next(__pyx_t_9); + if (!__pyx_t_1) { + if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2386; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + break; + } + __Pyx_GOTREF(__pyx_t_1); + } + __Pyx_DECREF(__pyx_v_f); + __pyx_v_f = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2387 + * + * for f in self.__dict__: + * if not f in field_names: # <<<<<<<<<<<<<< + * ret_string.append("%-30s %-10s= %s" % (f, "", self.__getattribute__(f))) + * return ret_string + */ + if (unlikely(__pyx_v_field_names == Py_None)) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_5 = ((PyDict_Contains(((PyObject *)__pyx_v_field_names), __pyx_v_f))); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = (!__pyx_t_5); + if (__pyx_t_4) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2388 + * for f in self.__dict__: + * if not f in field_names: + * ret_string.append("%-30s %-10s= %s" % (f, "", self.__getattribute__(f))) # <<<<<<<<<<<<<< + * return ret_string + * + */ + if (unlikely(__pyx_v_ret_string == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s____getattribute__); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_7 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_8), 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_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_8)); __pyx_t_8 = 0; + __pyx_t_8 = PyTuple_New(3); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_8)); + __Pyx_INCREF(__pyx_v_f); + PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_f); + __Pyx_GIVEREF(__pyx_v_f); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_12)); + PyTuple_SET_ITEM(__pyx_t_8, 1, ((PyObject *)__pyx_kp_s_12)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_12)); + PyTuple_SET_ITEM(__pyx_t_8, 2, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + __pyx_t_7 = 0; + __pyx_t_7 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_143), ((PyObject *)__pyx_t_8)); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_7)); + __Pyx_DECREF(((PyObject *)__pyx_t_8)); __pyx_t_8 = 0; + __pyx_t_10 = PyList_Append(__pyx_v_ret_string, ((PyObject *)__pyx_t_7)); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_7)); __pyx_t_7 = 0; + goto __pyx_L10; + } + __pyx_L10:; + } + __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2389 + * if not f in field_names: + * ret_string.append("%-30s %-10s= %s" % (f, "", self.__getattribute__(f))) + * return ret_string # <<<<<<<<<<<<<< + * + * cdef class PileupProxy: + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_ret_string)); + __pyx_r = ((PyObject *)__pyx_v_ret_string); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __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("csamtools.AlignedRead.fancy_str"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_ret_string); + __Pyx_DECREF(__pyx_v_field_names); + __Pyx_DECREF(__pyx_v_fields_names_in_order); + __Pyx_DECREF(__pyx_v_f); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2413 + * cdef int n_pu + * + * def __init__(self): # <<<<<<<<<<<<<< + * raise TypeError("This class cannot be instantiated from Python") + * + */ + +static int __pyx_pf_9csamtools_11PileupProxy_0__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_11PileupProxy_0__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__init__"); + __Pyx_TraceCall("__init__", __pyx_f[0], 2413); + 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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2414 + * + * def __init__(self): + * raise TypeError("This class cannot be instantiated from Python") # <<<<<<<<<<<<<< + * + * def __str__(self): + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_TypeError, ((PyObject *)__pyx_k_tuple_147), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2414; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2414; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupProxy.__init__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2416 + * raise TypeError("This class cannot be instantiated from Python") + * + * def __str__(self): # <<<<<<<<<<<<<< + * return "\t".join( map(str, (self.tid, self.pos, self.n))) +\ + * "\n" +\ + */ + +static PyObject *__pyx_pf_9csamtools_11PileupProxy_1__str__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11PileupProxy_1__str__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + 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; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__str__"); + __Pyx_TraceCall("__str__", __pyx_f[0], 2416); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2417 + * + * 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); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2418 + * def __str__(self): + * return "\t".join( map(str, (self.tid, self.pos, self.n))) +\ + * "\n" +\ # <<<<<<<<<<<<<< + * "\n".join( map(str, self.pileups) ) + * + */ + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_1), __pyx_n_s__join); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2417 + * + * def __str__(self): + * return "\t".join( map(str, (self.tid, self.pos, self.n))) +\ # <<<<<<<<<<<<<< + * "\n" +\ + * "\n".join( map(str, self.pileups) ) + */ + __pyx_t_2 = PyInt_FromLong(((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_v_self)->tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyInt_FromLong(((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_v_self)->pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_4 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__n); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2417; __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 = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_3); + __Pyx_GIVEREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_5, 2, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + __pyx_t_2 = 0; + __pyx_t_3 = 0; + __pyx_t_4 = 0; + __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)((PyObject*)(&PyString_Type)))); + __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_4, 1, ((PyObject *)__pyx_t_5)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_5)); + __pyx_t_5 = 0; + __pyx_t_5 = PyObject_Call(__pyx_builtin_map, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(((PyObject *)__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 = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __pyx_t_4 = PyNumber_Add(__pyx_t_5, ((PyObject *)__pyx_kp_s_2)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2417; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2419 + * return "\t".join( map(str, (self.tid, self.pos, self.n))) +\ + * "\n" +\ + * "\n".join( map(str, self.pileups) ) # <<<<<<<<<<<<<< + * + * property tid: + */ + __pyx_t_5 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_2), __pyx_n_s__join); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2419; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__pileups); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2419; __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 = 2419; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_1); + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_map, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2419; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__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 = 2419; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2419; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_t_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2418; __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; + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; + + __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("csamtools.PileupProxy.__str__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2423 + * property tid: + * '''the chromosome ID as is defined in the header''' + * def __get__(self): return self.tid # <<<<<<<<<<<<<< + * + * property n: + */ + +static PyObject *__pyx_pf_9csamtools_11PileupProxy_3tid_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11PileupProxy_3tid_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2423); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_v_self)->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2423; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupProxy.tid.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2427 + * property n: + * '''number of reads mapping to this column.''' + * def __get__(self): return self.n_pu # <<<<<<<<<<<<<< + * def __set__(self, n): self.n_pu = n + * + */ + +static PyObject *__pyx_pf_9csamtools_11PileupProxy_1n_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11PileupProxy_1n_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2427); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_v_self)->n_pu); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2427; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupProxy.n.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2428 + * '''number of reads mapping to this column.''' + * def __get__(self): return self.n_pu + * def __set__(self, n): self.n_pu = n # <<<<<<<<<<<<<< + * + * property pos: + */ + +static int __pyx_pf_9csamtools_11PileupProxy_1n_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_n); /*proto*/ +static int __pyx_pf_9csamtools_11PileupProxy_1n_1__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_n) { + int __pyx_r; + int __pyx_t_1; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__set__"); + __Pyx_TraceCall("__set__", __pyx_f[0], 2428); + __pyx_t_1 = __Pyx_PyInt_AsInt(__pyx_v_n); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2428; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + ((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_v_self)->n_pu = __pyx_t_1; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.PileupProxy.n.__set__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2431 + * + * property pos: + * def __get__(self): return self.pos # <<<<<<<<<<<<<< + * + * property pileups: + */ + +static PyObject *__pyx_pf_9csamtools_11PileupProxy_3pos_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11PileupProxy_3pos_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2431); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_v_self)->pos); 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; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupProxy.pos.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2435 + * property pileups: + * '''list of reads (:class:`pysam.PileupRead`) aligned to this column''' + * def __get__(self): # <<<<<<<<<<<<<< + * cdef int x + * pileups = [] + */ + +static PyObject *__pyx_pf_9csamtools_11PileupProxy_7pileups_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_11PileupProxy_7pileups_0__get__(PyObject *__pyx_v_self) { + int __pyx_v_x; + PyObject *__pyx_v_pileups; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + int __pyx_t_3; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2435); + __pyx_v_pileups = ((PyObject*)Py_None); __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2437 + * def __get__(self): + * cdef int x + * pileups = [] # <<<<<<<<<<<<<< + * # warning: there could be problems if self.n and self.buf are + * # out of sync. + */ + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2437; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__pyx_v_pileups)); + __pyx_v_pileups = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2440 + * # 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[x])) ) + * return pileups + */ + __pyx_t_2 = ((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_v_self)->n_pu; + for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2441 + * # out of sync. + * for x from 0 <= x < self.n_pu: + * pileups.append( makePileupRead( &(self.plp[x])) ) # <<<<<<<<<<<<<< + * return pileups + * + */ + if (unlikely(__pyx_v_pileups == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'append'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2441; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = __pyx_f_9csamtools_makePileupRead((&(((struct __pyx_obj_9csamtools_PileupProxy *)__pyx_v_self)->plp[__pyx_v_x]))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2441; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyList_Append(__pyx_v_pileups, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2441; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2442 + * for x from 0 <= x < self.n_pu: + * pileups.append( makePileupRead( &(self.plp[x])) ) + * return pileups # <<<<<<<<<<<<<< + * + * cdef class PileupRead: + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_pileups)); + __pyx_r = ((PyObject *)__pyx_v_pileups); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupProxy.pileups.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_pileups); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2457 + * uint32_t _is_tail + * + * def __init__(self): # <<<<<<<<<<<<<< + * raise TypeError("This class cannot be instantiated from Python") + * + */ + +static int __pyx_pf_9csamtools_10PileupRead_0__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_10PileupRead_0__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__init__"); + __Pyx_TraceCall("__init__", __pyx_f[0], 2457); + 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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2458 + * + * def __init__(self): + * raise TypeError("This class cannot be instantiated from Python") # <<<<<<<<<<<<<< + * + * def __str__(self): + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_TypeError, ((PyObject *)__pyx_k_tuple_148), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2458; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2458; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupRead.__init__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2460 + * 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 ) ) ) + * + */ + +static PyObject *__pyx_pf_9csamtools_10PileupRead_1__str__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_10PileupRead_1__str__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + 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; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__str__"); + __Pyx_TraceCall("__str__", __pyx_f[0], 2460); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2461 + * + * 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 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_1), __pyx_n_s__join); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__alignment); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__qpos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_4 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__indel); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_5 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__level); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_6 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__is_del); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_7 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__is_head); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_8 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__is_tail); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + __pyx_t_9 = PyTuple_New(7); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_3); + __Pyx_GIVEREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_9, 2, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_9, 3, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_9, 4, __pyx_t_6); + __Pyx_GIVEREF(__pyx_t_6); + PyTuple_SET_ITEM(__pyx_t_9, 5, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + PyTuple_SET_ITEM(__pyx_t_9, 6, __pyx_t_8); + __Pyx_GIVEREF(__pyx_t_8); + __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; + __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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, ((PyObject *)__pyx_t_9)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_9)); + __pyx_t_9 = 0; + __pyx_t_9 = PyObject_Call(__pyx_builtin_map, ((PyObject *)__pyx_t_8), NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + __Pyx_DECREF(((PyObject *)__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 = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_8)); + PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_9); + __Pyx_GIVEREF(__pyx_t_9); + __pyx_t_9 = 0; + __pyx_t_9 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_8), NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2461; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_8)); __pyx_t_8 = 0; + __pyx_r = __pyx_t_9; + __pyx_t_9 = 0; + goto __pyx_L0; + + __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_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_XDECREF(__pyx_t_9); + __Pyx_AddTraceback("csamtools.PileupRead.__str__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2465 + * property alignment: + * """a :class:`pysam.AlignedRead` object of the aligned read""" + * def __get__(self): # <<<<<<<<<<<<<< + * return self._alignment + * property qpos: + */ + +static PyObject *__pyx_pf_9csamtools_10PileupRead_9alignment_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_10PileupRead_9alignment_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2465); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2466 + * """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 *)((struct __pyx_obj_9csamtools_PileupRead *)__pyx_v_self)->_alignment)); + __pyx_r = ((PyObject *)((struct __pyx_obj_9csamtools_PileupRead *)__pyx_v_self)->_alignment); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2469 + * property qpos: + * """position of the read base at the pileup site, 0-based""" + * def __get__(self): # <<<<<<<<<<<<<< + * return self._qpos + * property indel: + */ + +static PyObject *__pyx_pf_9csamtools_10PileupRead_4qpos_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_10PileupRead_4qpos_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2469); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2470 + * """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_to_py_int32_t(((struct __pyx_obj_9csamtools_PileupRead *)__pyx_v_self)->_qpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2470; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupRead.qpos.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2473 + * property indel: + * """indel length; 0 for no indel, positive for ins and negative for del""" + * def __get__(self): # <<<<<<<<<<<<<< + * return self._indel + * property is_del: + */ + +static PyObject *__pyx_pf_9csamtools_10PileupRead_5indel_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_10PileupRead_5indel_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2473); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2474 + * """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 = PyInt_FromLong(((struct __pyx_obj_9csamtools_PileupRead *)__pyx_v_self)->_indel); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2474; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupRead.indel.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2477 + * property is_del: + * """1 iff the base on the padded read is a deletion""" + * def __get__(self): # <<<<<<<<<<<<<< + * return self._is_del + * property is_head: + */ + +static PyObject *__pyx_pf_9csamtools_10PileupRead_6is_del_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_10PileupRead_6is_del_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2477); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2478 + * """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_to_py_uint32_t(((struct __pyx_obj_9csamtools_PileupRead *)__pyx_v_self)->_is_del); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2478; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupRead.is_del.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2480 + * return self._is_del + * property is_head: + * def __get__(self): # <<<<<<<<<<<<<< + * return self._is_head + * property is_tail: + */ + +static PyObject *__pyx_pf_9csamtools_10PileupRead_7is_head_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_10PileupRead_7is_head_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2480); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2481 + * 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_to_py_uint32_t(((struct __pyx_obj_9csamtools_PileupRead *)__pyx_v_self)->_is_head); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2481; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupRead.is_head.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2483 + * return self._is_head + * property is_tail: + * def __get__(self): # <<<<<<<<<<<<<< + * return self._is_tail + * property level: + */ + +static PyObject *__pyx_pf_9csamtools_10PileupRead_7is_tail_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_10PileupRead_7is_tail_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2483); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2484 + * property is_tail: + * def __get__(self): + * return self._is_tail # <<<<<<<<<<<<<< + * property level: + * def __get__(self): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyInt_to_py_uint32_t(((struct __pyx_obj_9csamtools_PileupRead *)__pyx_v_self)->_is_tail); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2484; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupRead.is_tail.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2486 + * return self._is_tail + * property level: + * def __get__(self): # <<<<<<<<<<<<<< + * return self._level + * + */ + +static PyObject *__pyx_pf_9csamtools_10PileupRead_5level_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_10PileupRead_5level_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2486); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2487 + * property level: + * def __get__(self): + * return self._level # <<<<<<<<<<<<<< + * + * class Outs: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_PileupRead *)__pyx_v_self)->_level); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2487; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.PileupRead.level.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2491 + * class Outs: + * '''http://mail.python.org/pipermail/python-list/2000-June/038406.html''' + * def __init__(self, id = 1): # <<<<<<<<<<<<<< + * self.streams = [] + * self.id = id + */ + +static PyObject *__pyx_pf_9csamtools_4Outs_0__init__(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyMethodDef __pyx_mdef_9csamtools_4Outs_0__init__ = {__Pyx_NAMESTR("__init__"), (PyCFunction)__pyx_pf_9csamtools_4Outs_0__init__, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)}; +static PyObject *__pyx_pf_9csamtools_4Outs_0__init__(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_self = 0; + PyObject *__pyx_v_id = 0; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__self,&__pyx_n_s__id,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__init__"); + __Pyx_TraceCall("__init__", __pyx_f[0], 2491); + __pyx_self = __pyx_self; + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + values[1] = ((PyObject *)__pyx_int_1); + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__self); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__id); + if (value) { values[1] = value; kw_args--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2491; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_self = values[0]; + __pyx_v_id = values[1]; + } else { + __pyx_v_id = ((PyObject *)__pyx_int_1); + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 2: __pyx_v_id = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_self = PyTuple_GET_ITEM(__pyx_args, 0); + break; + default: goto __pyx_L5_argtuple_error; + } + } + 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 = 2491; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Outs.__init__"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2492 + * '''http://mail.python.org/pipermail/python-list/2000-June/038406.html''' + * def __init__(self, id = 1): + * self.streams = [] # <<<<<<<<<<<<<< + * self.id = id + * + */ + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2492; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + if (PyObject_SetAttr(__pyx_v_self, __pyx_n_s__streams, ((PyObject *)__pyx_t_1)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2492; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2493 + * def __init__(self, id = 1): + * self.streams = [] + * self.id = id # <<<<<<<<<<<<<< + * + * def setdevice(self, filename): + */ + if (PyObject_SetAttr(__pyx_v_self, __pyx_n_s__id, __pyx_v_id) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2493; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.Outs.__init__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2495 + * self.id = id + * + * def setdevice(self, filename): # <<<<<<<<<<<<<< + * '''open an existing file, like "/dev/null"''' + * fd = os.open(filename, os.O_WRONLY) + */ + +static PyObject *__pyx_pf_9csamtools_4Outs_1setdevice(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_4Outs_1setdevice[] = "open an existing file, like \"/dev/null\""; +static PyMethodDef __pyx_mdef_9csamtools_4Outs_1setdevice = {__Pyx_NAMESTR("setdevice"), (PyCFunction)__pyx_pf_9csamtools_4Outs_1setdevice, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_4Outs_1setdevice)}; +static PyObject *__pyx_pf_9csamtools_4Outs_1setdevice(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_self = 0; + PyObject *__pyx_v_filename = 0; + PyObject *__pyx_v_fd; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__self,&__pyx_n_s__filename,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("setdevice"); + __Pyx_TraceCall("setdevice", __pyx_f[0], 2495); + __pyx_self = __pyx_self; + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__self); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__filename); + if (likely(values[1])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("setdevice", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2495; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "setdevice") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2495; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_self = values[0]; + __pyx_v_filename = values[1]; + } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_self = PyTuple_GET_ITEM(__pyx_args, 0); + __pyx_v_filename = PyTuple_GET_ITEM(__pyx_args, 1); + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("setdevice", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2495; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Outs.setdevice"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_v_fd = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2497 + * def setdevice(self, filename): + * '''open an existing file, like "/dev/null"''' + * fd = os.open(filename, os.O_WRONLY) # <<<<<<<<<<<<<< + * self.setfd(fd) + * + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2497; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__open); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2497; __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_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2497; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__O_WRONLY); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2497; __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(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2497; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(__pyx_v_filename); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_filename); + __Pyx_GIVEREF(__pyx_v_filename); + PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3); + __Pyx_GIVEREF(__pyx_t_3); + __pyx_t_3 = 0; + __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2497; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_fd); + __pyx_v_fd = __pyx_t_3; + __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2498 + * '''open an existing file, like "/dev/null"''' + * fd = os.open(filename, os.O_WRONLY) + * self.setfd(fd) # <<<<<<<<<<<<<< + * + * def setfile(self, filename): + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__setfd); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2498; __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 = 2498; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(__pyx_v_fd); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_fd); + __Pyx_GIVEREF(__pyx_v_fd); + __pyx_t_2 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2498; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + __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("csamtools.Outs.setdevice"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_fd); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2500 + * self.setfd(fd) + * + * def setfile(self, filename): # <<<<<<<<<<<<<< + * '''open a new file.''' + * fd = os.open(filename, os.O_WRONLY|os.O_CREAT, 0660); + */ + +static PyObject *__pyx_pf_9csamtools_4Outs_2setfile(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_4Outs_2setfile[] = "open a new file."; +static PyMethodDef __pyx_mdef_9csamtools_4Outs_2setfile = {__Pyx_NAMESTR("setfile"), (PyCFunction)__pyx_pf_9csamtools_4Outs_2setfile, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_4Outs_2setfile)}; +static PyObject *__pyx_pf_9csamtools_4Outs_2setfile(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_self = 0; + PyObject *__pyx_v_filename = 0; + PyObject *__pyx_v_fd; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__self,&__pyx_n_s__filename,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("setfile"); + __Pyx_TraceCall("setfile", __pyx_f[0], 2500); + __pyx_self = __pyx_self; + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__self); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__filename); + if (likely(values[1])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("setfile", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2500; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "setfile") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2500; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_self = values[0]; + __pyx_v_filename = values[1]; + } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_self = PyTuple_GET_ITEM(__pyx_args, 0); + __pyx_v_filename = PyTuple_GET_ITEM(__pyx_args, 1); + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("setfile", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2500; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Outs.setfile"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_v_fd = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2502 + * def setfile(self, filename): + * '''open a new file.''' + * fd = os.open(filename, os.O_WRONLY|os.O_CREAT, 0660); # <<<<<<<<<<<<<< + * self.setfd(fd) + * + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__open); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __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_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__O_WRONLY); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __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_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_4 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__O_CREAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyNumber_Or(__pyx_t_3, __pyx_t_4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __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; + __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_INCREF(__pyx_v_filename); + PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_filename); + __Pyx_GIVEREF(__pyx_v_filename); + PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + __Pyx_INCREF(__pyx_int_0660); + PyTuple_SET_ITEM(__pyx_t_4, 2, __pyx_int_0660); + __Pyx_GIVEREF(__pyx_int_0660); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_fd); + __pyx_v_fd = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2503 + * '''open a new file.''' + * fd = os.open(filename, os.O_WRONLY|os.O_CREAT, 0660); + * self.setfd(fd) # <<<<<<<<<<<<<< + * + * def setfd(self, fd): + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__setfd); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2503; __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 = 2503; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_INCREF(__pyx_v_fd); + PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_fd); + __Pyx_GIVEREF(__pyx_v_fd); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2503; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + __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_AddTraceback("csamtools.Outs.setfile"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_fd); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2505 + * self.setfd(fd) + * + * def setfd(self, fd): # <<<<<<<<<<<<<< + * ofd = os.dup(self.id) # Save old stream on new unit. + * self.streams.append(ofd) + */ + +static PyObject *__pyx_pf_9csamtools_4Outs_3setfd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyMethodDef __pyx_mdef_9csamtools_4Outs_3setfd = {__Pyx_NAMESTR("setfd"), (PyCFunction)__pyx_pf_9csamtools_4Outs_3setfd, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)}; +static PyObject *__pyx_pf_9csamtools_4Outs_3setfd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_self = 0; + PyObject *__pyx_v_fd = 0; + PyObject *__pyx_v_ofd; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__self,&__pyx_n_s__fd,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("setfd"); + __Pyx_TraceCall("setfd", __pyx_f[0], 2505); + __pyx_self = __pyx_self; + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__self); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__fd); + if (likely(values[1])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("setfd", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2505; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "setfd") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2505; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_self = values[0]; + __pyx_v_fd = values[1]; + } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_self = PyTuple_GET_ITEM(__pyx_args, 0); + __pyx_v_fd = PyTuple_GET_ITEM(__pyx_args, 1); + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("setfd", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2505; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.Outs.setfd"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_v_ofd = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2506 + * + * def setfd(self, fd): + * ofd = os.dup(self.id) # Save old stream on new unit. # <<<<<<<<<<<<<< + * self.streams.append(ofd) + * sys.stdout.flush() # Buffered data goes to old stream. + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2506; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__dup); 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(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2506; __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 = 2506; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2506; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_v_ofd); + __pyx_v_ofd = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2507 + * def setfd(self, fd): + * ofd = os.dup(self.id) # Save old stream on new unit. + * self.streams.append(ofd) # <<<<<<<<<<<<<< + * sys.stdout.flush() # Buffered data goes to old stream. + * os.dup2(fd, self.id) # Open unit 1 on new stream. + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__streams); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2507; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = __Pyx_PyObject_Append(__pyx_t_1, __pyx_v_ofd); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2507; __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2508 + * ofd = os.dup(self.id) # Save old stream on new unit. + * self.streams.append(ofd) + * sys.stdout.flush() # Buffered data goes to old stream. # <<<<<<<<<<<<<< + * os.dup2(fd, self.id) # Open unit 1 on new stream. + * os.close(fd) # Close other unit (look out, caller.) + */ + __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__sys); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2508; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__stdout); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2508; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__flush); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2508; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2508; __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_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2509 + * self.streams.append(ofd) + * sys.stdout.flush() # Buffered data goes to old stream. + * os.dup2(fd, self.id) # Open unit 1 on new stream. # <<<<<<<<<<<<<< + * os.close(fd) # Close other unit (look out, caller.) + * + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2509; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__dup2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2509; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2509; __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 = 2509; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_v_fd); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_fd); + __Pyx_GIVEREF(__pyx_v_fd); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2509; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2510 + * sys.stdout.flush() # Buffered data goes to old stream. + * os.dup2(fd, self.id) # Open unit 1 on new stream. + * os.close(fd) # Close other unit (look out, caller.) # <<<<<<<<<<<<<< + * + * def restore(self): + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2510; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__close); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2510; __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 = 2510; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(__pyx_v_fd); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_fd); + __Pyx_GIVEREF(__pyx_v_fd); + __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2510; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + __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("csamtools.Outs.setfd"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_ofd); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2512 + * os.close(fd) # Close other unit (look out, caller.) + * + * def restore(self): # <<<<<<<<<<<<<< + * '''restore previous output stream''' + * if self.streams: + */ + +static PyObject *__pyx_pf_9csamtools_4Outs_4restore(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_4Outs_4restore[] = "restore previous output stream"; +static PyMethodDef __pyx_mdef_9csamtools_4Outs_4restore = {__Pyx_NAMESTR("restore"), (PyCFunction)__pyx_pf_9csamtools_4Outs_4restore, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_4Outs_4restore)}; +static PyObject *__pyx_pf_9csamtools_4Outs_4restore(PyObject *__pyx_self, PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_5 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("restore"); + __Pyx_TraceCall("restore", __pyx_f[0], 2512); + __pyx_self = __pyx_self; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2514 + * def restore(self): + * '''restore previous output stream''' + * if self.streams: # <<<<<<<<<<<<<< + * # the following was not sufficient, hence flush both stderr and stdout + * # os.fsync( self.id ) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__streams); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2514; __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 = 2514; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2517 + * # the following was not sufficient, hence flush both stderr and stdout + * # os.fsync( self.id ) + * sys.stdout.flush() # <<<<<<<<<<<<<< + * sys.stderr.flush() + * os.dup2(self.streams[-1], self.id) + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__stdout); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__flush); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2517; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2517; __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2518 + * # os.fsync( self.id ) + * sys.stdout.flush() + * sys.stderr.flush() # <<<<<<<<<<<<<< + * os.dup2(self.streams[-1], self.id) + * os.close(self.streams[-1]) + */ + __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__sys); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2518; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__stderr); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2518; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__flush); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2518; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2518; __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_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2519 + * sys.stdout.flush() + * sys.stderr.flush() + * os.dup2(self.streams[-1], self.id) # <<<<<<<<<<<<<< + * os.close(self.streams[-1]) + * del self.streams[-1] + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2519; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__dup2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2519; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__streams); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2519; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_4 = __Pyx_GetItemInt(__pyx_t_1, -1, sizeof(long), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2519; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2519; __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 = 2519; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_4 = 0; + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2519; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2520 + * sys.stderr.flush() + * os.dup2(self.streams[-1], self.id) + * os.close(self.streams[-1]) # <<<<<<<<<<<<<< + * del self.streams[-1] + * + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2520; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_5 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__close); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2520; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__streams); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2520; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = __Pyx_GetItemInt(__pyx_t_1, -1, sizeof(long), PyInt_FromLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2520; __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 = 2520; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2520; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2521 + * os.dup2(self.streams[-1], self.id) + * os.close(self.streams[-1]) + * del self.streams[-1] # <<<<<<<<<<<<<< + * + * def _samtools_dispatch( method, + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__streams); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2521; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (__Pyx_DelItemInt(__pyx_t_3, -1, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2521; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + goto __pyx_L5; + } + __pyx_L5:; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_5); + __Pyx_AddTraceback("csamtools.Outs.restore"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2523 + * del self.streams[-1] + * + * def _samtools_dispatch( method, # <<<<<<<<<<<<<< + * args = (), + * catch_stdout = True, + */ + +static PyObject *__pyx_pf_9csamtools_0_samtools_dispatch(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_0_samtools_dispatch[] = "call ``method`` in samtools providing arguments in args.\n \n .. note:: \n This method redirects stdout and stderr to capture it \n from samtools. If for some reason stdout/stderr disappears\n the reason might be in this method.\n\n .. note::\n The current implementation might only work on linux.\n \n .. note:: \n This method captures stdout and stderr using temporary files, \n which are then read into memory in their entirety. This method\n is slow and might cause large memory overhead. \n\n See http://bytes.com/topic/c/answers/487231-how-capture-stdout-temporarily\n on the topic of redirecting stderr/stdout.\n "; +static PyMethodDef __pyx_mdef_9csamtools_0_samtools_dispatch = {__Pyx_NAMESTR("_samtools_dispatch"), (PyCFunction)__pyx_pf_9csamtools_0_samtools_dispatch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_0_samtools_dispatch)}; +static PyObject *__pyx_pf_9csamtools_0_samtools_dispatch(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_method = 0; + PyObject *__pyx_v_args = 0; + PyObject *__pyx_v_catch_stdout = 0; + PyObject *__pyx_v_catch_stderr = 0; + PyObject *__pyx_v_stderr_h; + PyObject *__pyx_v_stderr_f; + PyObject *__pyx_v_stderr_save; + PyObject *__pyx_v_stdout_h; + PyObject *__pyx_v_stdout_f; + PyObject *__pyx_v_stdout_save; + char **__pyx_v_cargs; + int __pyx_v_i; + int __pyx_v_n; + int __pyx_v_retval; + PyObject *__pyx_v_out_stdout; + PyObject *__pyx_v_out_stderr; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + int __pyx_t_5; + PyObject *__pyx_t_6 = NULL; + Py_ssize_t __pyx_t_7; + char *__pyx_t_8; + int __pyx_t_9; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__method,&__pyx_n_s__args,&__pyx_n_s__catch_stdout,&__pyx_n_s__catch_stderr,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_samtools_dispatch"); + __Pyx_TraceCall("_samtools_dispatch", __pyx_f[0], 2523); + __pyx_self = __pyx_self; + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[4] = {0,0,0,0}; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2524 + * + * def _samtools_dispatch( method, + * args = (), # <<<<<<<<<<<<<< + * catch_stdout = True, + * catch_stderr = False, + */ + values[1] = ((PyObject *)__pyx_empty_tuple); + values[2] = __pyx_k_149; + values[3] = __pyx_k_150; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__method); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__args); + if (value) { values[1] = value; kw_args--; } + } + case 2: + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__catch_stdout); + if (value) { values[2] = value; kw_args--; } + } + case 3: + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__catch_stderr); + if (value) { values[3] = value; kw_args--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "_samtools_dispatch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2523; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_method = values[0]; + __pyx_v_args = values[1]; + __pyx_v_catch_stdout = values[2]; + __pyx_v_catch_stderr = values[3]; + } else { + __pyx_v_args = ((PyObject *)__pyx_empty_tuple); + __pyx_v_catch_stdout = __pyx_k_149; + __pyx_v_catch_stderr = __pyx_k_150; + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 4: __pyx_v_catch_stderr = PyTuple_GET_ITEM(__pyx_args, 3); + case 3: __pyx_v_catch_stdout = PyTuple_GET_ITEM(__pyx_args, 2); + case 2: __pyx_v_args = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_method = PyTuple_GET_ITEM(__pyx_args, 0); + break; + default: goto __pyx_L5_argtuple_error; + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("_samtools_dispatch", 0, 1, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2523; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools._samtools_dispatch"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __Pyx_INCREF(__pyx_v_args); + __pyx_v_stderr_h = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_stderr_f = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_stderr_save = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_stdout_h = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_stdout_f = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_stdout_save = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_out_stdout = Py_None; __Pyx_INCREF(Py_None); + __pyx_v_out_stderr = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2551 + * + * # some special cases + * if method == "index": # <<<<<<<<<<<<<< + * if not os.path.exists( args[0] ): + * raise IOError( "No such file or directory: '%s'" % args[0] ) + */ + __pyx_t_1 = PyObject_RichCompare(__pyx_v_method, ((PyObject *)__pyx_n_s__index), Py_EQ); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2551; __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 = 2551; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2552 + * # some special cases + * if method == "index": + * if not os.path.exists( args[0] ): # <<<<<<<<<<<<<< + * raise IOError( "No such file or directory: '%s'" % args[0] ) + * + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__path); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__exists); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __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_GetItemInt(__pyx_v_args, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __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 = 2552; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_5 = (!__pyx_t_2); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2553 + * if method == "index": + * if not os.path.exists( args[0] ): + * raise IOError( "No such file or directory: '%s'" % args[0] ) # <<<<<<<<<<<<<< + * + * # redirect stderr and stdout to file + */ + __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_args, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2553; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_4 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_151), __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2553; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = 2553; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_t_4)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); + __pyx_t_4 = 0; + __pyx_t_4 = PyObject_Call(__pyx_builtin_IOError, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2553; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __Pyx_Raise(__pyx_t_4, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2553; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2556 + * + * # redirect stderr and stdout to file + * if catch_stderr: # <<<<<<<<<<<<<< + * stderr_h, stderr_f = tempfile.mkstemp() + * stderr_save = Outs( sys.stderr.fileno() ) + */ + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_v_catch_stderr); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2556; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2557 + * # redirect stderr and stdout to file + * if catch_stderr: + * stderr_h, stderr_f = tempfile.mkstemp() # <<<<<<<<<<<<<< + * stderr_save = Outs( sys.stderr.fileno() ) + * stderr_save.setfd( stderr_h ) + */ + __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__tempfile); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2557; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__mkstemp); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2557; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_t_4 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2557; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + if (PyTuple_CheckExact(__pyx_t_4) && likely(PyTuple_GET_SIZE(__pyx_t_4) == 2)) { + PyObject* tuple = __pyx_t_4; + __pyx_t_3 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_3); + __pyx_t_1 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_stderr_h); + __pyx_v_stderr_h = __pyx_t_3; + __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_v_stderr_f); + __pyx_v_stderr_f = __pyx_t_1; + __pyx_t_1 = 0; + } else { + __pyx_t_6 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2557; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_t_3 = __Pyx_UnpackItem(__pyx_t_6, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2557; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_1 = __Pyx_UnpackItem(__pyx_t_6, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2557; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + if (__Pyx_EndUnpack(__pyx_t_6, 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2557; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(__pyx_v_stderr_h); + __pyx_v_stderr_h = __pyx_t_3; + __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_v_stderr_f); + __pyx_v_stderr_f = __pyx_t_1; + __pyx_t_1 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2558 + * if catch_stderr: + * stderr_h, stderr_f = tempfile.mkstemp() + * stderr_save = Outs( sys.stderr.fileno() ) # <<<<<<<<<<<<<< + * stderr_save.setfd( stderr_h ) + * + */ + __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__Outs); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2558; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2558; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__stderr); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2558; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__fileno); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2558; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2558; __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 = 2558; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2558; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_stderr_save); + __pyx_v_stderr_save = __pyx_t_3; + __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2559 + * stderr_h, stderr_f = tempfile.mkstemp() + * stderr_save = Outs( sys.stderr.fileno() ) + * stderr_save.setfd( stderr_h ) # <<<<<<<<<<<<<< + * + * if catch_stdout: + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_stderr_save, __pyx_n_s__setfd); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2559; __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 = 2559; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(__pyx_v_stderr_h); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_stderr_h); + __Pyx_GIVEREF(__pyx_v_stderr_h); + __pyx_t_4 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2559; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2561 + * stderr_save.setfd( stderr_h ) + * + * if catch_stdout: # <<<<<<<<<<<<<< + * stdout_h, stdout_f = tempfile.mkstemp() + * stdout_save = Outs( sys.stdout.fileno() ) + */ + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_v_catch_stdout); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2561; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2562 + * + * if catch_stdout: + * stdout_h, stdout_f = tempfile.mkstemp() # <<<<<<<<<<<<<< + * stdout_save = Outs( sys.stdout.fileno() ) + * stdout_save.setfd( stdout_h ) + */ + __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__tempfile); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2562; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_1 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__mkstemp); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2562; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2562; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (PyTuple_CheckExact(__pyx_t_4) && likely(PyTuple_GET_SIZE(__pyx_t_4) == 2)) { + PyObject* tuple = __pyx_t_4; + __pyx_t_1 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_1); + __pyx_t_3 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_stdout_h); + __pyx_v_stdout_h = __pyx_t_1; + __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_stdout_f); + __pyx_v_stdout_f = __pyx_t_3; + __pyx_t_3 = 0; + } else { + __pyx_t_6 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2562; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_t_1 = __Pyx_UnpackItem(__pyx_t_6, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2562; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = __Pyx_UnpackItem(__pyx_t_6, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2562; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (__Pyx_EndUnpack(__pyx_t_6, 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2562; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_DECREF(__pyx_v_stdout_h); + __pyx_v_stdout_h = __pyx_t_1; + __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_stdout_f); + __pyx_v_stdout_f = __pyx_t_3; + __pyx_t_3 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2563 + * if catch_stdout: + * stdout_h, stdout_f = tempfile.mkstemp() + * stdout_save = Outs( sys.stdout.fileno() ) # <<<<<<<<<<<<<< + * stdout_save.setfd( stdout_h ) + * + */ + __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__Outs); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2563; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__sys); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2563; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__stdout); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2563; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__fileno); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2563; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2563; __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 = 2563; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2563; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_v_stdout_save); + __pyx_v_stdout_save = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2564 + * stdout_h, stdout_f = tempfile.mkstemp() + * stdout_save = Outs( sys.stdout.fileno() ) + * stdout_save.setfd( stdout_h ) # <<<<<<<<<<<<<< + * + * # patch for `samtools view` + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_stdout_save, __pyx_n_s__setfd); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2564; __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 = 2564; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + __Pyx_INCREF(__pyx_v_stdout_h); + PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_stdout_h); + __Pyx_GIVEREF(__pyx_v_stdout_h); + __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2564; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2569 + * # samtools `view` closes stdout, from which I can not + * # recover. Thus redirect output to file with -o option. + * if method == "view": # <<<<<<<<<<<<<< + * if "-o" in args: raise ValueError("option -o is forbidden in samtools view") + * args = ( "-o", stdout_f ) + args + */ + __pyx_t_4 = PyObject_RichCompare(__pyx_v_method, ((PyObject *)__pyx_n_s__view), Py_EQ); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2569; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2569; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2570 + * # recover. Thus redirect output to file with -o option. + * if method == "view": + * if "-o" in args: raise ValueError("option -o is forbidden in samtools view") # <<<<<<<<<<<<<< + * args = ( "-o", stdout_f ) + args + * + */ + __pyx_t_5 = ((PySequence_Contains(__pyx_v_args, ((PyObject *)__pyx_kp_s_152)))); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_5) { + __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_154), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_Raise(__pyx_t_4, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L11; + } + __pyx_L11:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2571 + * if method == "view": + * if "-o" in args: raise ValueError("option -o is forbidden in samtools view") + * args = ( "-o", stdout_f ) + args # <<<<<<<<<<<<<< + * + * + */ + __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2571; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_152)); + PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_kp_s_152)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_152)); + __Pyx_INCREF(__pyx_v_stdout_f); + PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_v_stdout_f); + __Pyx_GIVEREF(__pyx_v_stdout_f); + __pyx_t_3 = PyNumber_Add(((PyObject *)__pyx_t_4), __pyx_v_args); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2571; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_args); + __pyx_v_args = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L10; + } + __pyx_L10:; + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2578 + * cdef int i, n, retval + * + * n = len(args) # <<<<<<<<<<<<<< + * # allocate two more for first (dummy) argument (contains command) + * cargs = calloc( n+2, sizeof( char *) ) + */ + __pyx_t_7 = PyObject_Length(__pyx_v_args); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2578; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_n = __pyx_t_7; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2580 + * n = len(args) + * # allocate two more for first (dummy) argument (contains command) + * cargs = calloc( n+2, sizeof( char *) ) # <<<<<<<<<<<<<< + * cargs[0] = "samtools" + * cargs[1] = method + */ + __pyx_v_cargs = ((char **)calloc((__pyx_v_n + 2), (sizeof(char *)))); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2581 + * # allocate two more for first (dummy) argument (contains command) + * cargs = calloc( n+2, sizeof( char *) ) + * cargs[0] = "samtools" # <<<<<<<<<<<<<< + * cargs[1] = method + * for i from 0 <= i < n: cargs[i+2] = args[i] + */ + (__pyx_v_cargs[0]) = __pyx_k__samtools; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2582 + * cargs = calloc( n+2, sizeof( char *) ) + * cargs[0] = "samtools" + * cargs[1] = method # <<<<<<<<<<<<<< + * for i from 0 <= i < n: cargs[i+2] = args[i] + * retval = pysam_dispatch(n+2, cargs) + */ + __pyx_t_8 = PyBytes_AsString(__pyx_v_method); if (unlikely((!__pyx_t_8) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2582; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + (__pyx_v_cargs[1]) = __pyx_t_8; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2583 + * cargs[0] = "samtools" + * cargs[1] = method + * for i from 0 <= i < n: cargs[i+2] = args[i] # <<<<<<<<<<<<<< + * retval = pysam_dispatch(n+2, cargs) + * free( cargs ) + */ + __pyx_t_9 = __pyx_v_n; + for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_9; __pyx_v_i++) { + __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_args, __pyx_v_i, sizeof(int), PyInt_FromLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2583; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_8 = PyBytes_AsString(__pyx_t_3); if (unlikely((!__pyx_t_8) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2583; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + (__pyx_v_cargs[(__pyx_v_i + 2)]) = __pyx_t_8; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2584 + * cargs[1] = method + * for i from 0 <= i < n: cargs[i+2] = args[i] + * retval = pysam_dispatch(n+2, cargs) # <<<<<<<<<<<<<< + * free( cargs ) + * + */ + __pyx_v_retval = pysam_dispatch((__pyx_v_n + 2), __pyx_v_cargs); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2585 + * for i from 0 <= i < n: cargs[i+2] = args[i] + * retval = pysam_dispatch(n+2, cargs) + * free( cargs ) # <<<<<<<<<<<<<< + * + * # restore stdout/stderr. This will also flush, so + */ + free(__pyx_v_cargs); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2589 + * # restore stdout/stderr. This will also flush, so + * # needs to be before reading back the file contents + * if catch_stdout: # <<<<<<<<<<<<<< + * stdout_save.restore() + * out_stdout = open( stdout_f, "r").readlines() + */ + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_v_catch_stdout); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2589; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2590 + * # needs to be before reading back the file contents + * if catch_stdout: + * stdout_save.restore() # <<<<<<<<<<<<<< + * out_stdout = open( stdout_f, "r").readlines() + * os.remove( stdout_f ) + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_stdout_save, __pyx_n_s__restore); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2590; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_4 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); 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_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2591 + * if catch_stdout: + * stdout_save.restore() + * out_stdout = open( stdout_f, "r").readlines() # <<<<<<<<<<<<<< + * os.remove( stdout_f ) + * else: + */ + __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2591; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_INCREF(__pyx_v_stdout_f); + PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_stdout_f); + __Pyx_GIVEREF(__pyx_v_stdout_f); + __Pyx_INCREF(((PyObject *)__pyx_n_s__r)); + PyTuple_SET_ITEM(__pyx_t_4, 1, ((PyObject *)__pyx_n_s__r)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__r)); + __pyx_t_3 = PyObject_Call(__pyx_builtin_open, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2591; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; + __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__readlines); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2591; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2591; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_v_out_stdout); + __pyx_v_out_stdout = __pyx_t_3; + __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2592 + * stdout_save.restore() + * out_stdout = open( stdout_f, "r").readlines() + * os.remove( stdout_f ) # <<<<<<<<<<<<<< + * else: + * out_stdout = [] + */ + __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); 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_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__remove); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2592; __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 = 2592; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + __Pyx_INCREF(__pyx_v_stdout_f); + PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_stdout_f); + __Pyx_GIVEREF(__pyx_v_stdout_f); + __pyx_t_1 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2592; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + goto __pyx_L14; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2594 + * os.remove( stdout_f ) + * else: + * out_stdout = [] # <<<<<<<<<<<<<< + * + * if catch_stderr: + */ + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2594; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(__pyx_v_out_stdout); + __pyx_v_out_stdout = ((PyObject *)__pyx_t_1); + __pyx_t_1 = 0; + } + __pyx_L14:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2596 + * out_stdout = [] + * + * if catch_stderr: # <<<<<<<<<<<<<< + * stderr_save.restore() + * out_stderr = open( stderr_f, "r").readlines() + */ + __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_v_catch_stderr); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2597 + * + * if catch_stderr: + * stderr_save.restore() # <<<<<<<<<<<<<< + * out_stderr = open( stderr_f, "r").readlines() + * os.remove( stderr_f ) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_stderr_save, __pyx_n_s__restore); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2597; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2597; __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2598 + * if catch_stderr: + * stderr_save.restore() + * out_stderr = open( stderr_f, "r").readlines() # <<<<<<<<<<<<<< + * os.remove( stderr_f ) + * else: + */ + __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2598; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + __Pyx_INCREF(__pyx_v_stderr_f); + PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_stderr_f); + __Pyx_GIVEREF(__pyx_v_stderr_f); + __Pyx_INCREF(((PyObject *)__pyx_n_s__r)); + PyTuple_SET_ITEM(__pyx_t_3, 1, ((PyObject *)__pyx_n_s__r)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__r)); + __pyx_t_1 = PyObject_Call(__pyx_builtin_open, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2598; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__readlines); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2598; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2598; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(__pyx_v_out_stderr); + __pyx_v_out_stderr = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2599 + * stderr_save.restore() + * out_stderr = open( stderr_f, "r").readlines() + * os.remove( stderr_f ) # <<<<<<<<<<<<<< + * else: + * out_stderr = [] + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2599; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__remove); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2599; __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 = 2599; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(__pyx_v_stderr_f); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_stderr_f); + __Pyx_GIVEREF(__pyx_v_stderr_f); + __pyx_t_4 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2599; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + goto __pyx_L15; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2601 + * os.remove( stderr_f ) + * else: + * out_stderr = [] # <<<<<<<<<<<<<< + * + * return retval, out_stderr, out_stdout + */ + __pyx_t_4 = PyList_New(0); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2601; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_DECREF(__pyx_v_out_stderr); + __pyx_v_out_stderr = ((PyObject *)__pyx_t_4); + __pyx_t_4 = 0; + } + __pyx_L15:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2603 + * out_stderr = [] + * + * return retval, out_stderr, out_stdout # <<<<<<<<<<<<<< + * + * cdef class SNPCall: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_4 = PyInt_FromLong(__pyx_v_retval); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2603; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_1 = PyTuple_New(3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2603; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + __Pyx_INCREF(__pyx_v_out_stderr); + PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_out_stderr); + __Pyx_GIVEREF(__pyx_v_out_stderr); + __Pyx_INCREF(__pyx_v_out_stdout); + PyTuple_SET_ITEM(__pyx_t_1, 2, __pyx_v_out_stdout); + __Pyx_GIVEREF(__pyx_v_out_stdout); + __pyx_t_4 = 0; + __pyx_r = ((PyObject *)__pyx_t_1); + __pyx_t_1 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_AddTraceback("csamtools._samtools_dispatch"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_stderr_h); + __Pyx_DECREF(__pyx_v_stderr_f); + __Pyx_DECREF(__pyx_v_stderr_save); + __Pyx_DECREF(__pyx_v_stdout_h); + __Pyx_DECREF(__pyx_v_stdout_f); + __Pyx_DECREF(__pyx_v_stdout_save); + __Pyx_DECREF(__pyx_v_out_stdout); + __Pyx_DECREF(__pyx_v_out_stderr); + __Pyx_DECREF(__pyx_v_args); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2618 + * property tid: + * '''the chromosome ID as is defined in the header''' + * def __get__(self): # <<<<<<<<<<<<<< + * return self._tid + * + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_3tid_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_3tid_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2618); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2619 + * '''the chromosome ID as is defined in the header''' + * def __get__(self): + * return self._tid # <<<<<<<<<<<<<< + * + * property pos: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_SNPCall *)__pyx_v_self)->_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2619; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.SNPCall.tid.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2623 + * property pos: + * '''nucleotide position of SNP.''' + * def __get__(self): return self._pos # <<<<<<<<<<<<<< + * + * property reference_base: + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_3pos_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_3pos_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2623); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_SNPCall *)__pyx_v_self)->_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2623; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.SNPCall.pos.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2627 + * property reference_base: + * '''reference base at pos. ``N`` if no reference sequence supplied.''' + * def __get__(self): return PyString_FromStringAndSize( &self._reference_base, 1 ) # <<<<<<<<<<<<<< + * + * property genotype: + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_14reference_base_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_14reference_base_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2627); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyString_FromStringAndSize((&((struct __pyx_obj_9csamtools_SNPCall *)__pyx_v_self)->_reference_base), 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2627; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.SNPCall.reference_base.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2631 + * property genotype: + * '''the genotype called.''' + * def __get__(self): return PyString_FromStringAndSize( &self._genotype, 1 ) # <<<<<<<<<<<<<< + * + * property consensus_quality: + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_8genotype_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_8genotype_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2631); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyString_FromStringAndSize((&((struct __pyx_obj_9csamtools_SNPCall *)__pyx_v_self)->_genotype), 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2631; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.SNPCall.genotype.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2635 + * property consensus_quality: + * '''the genotype quality (Phred-scaled).''' + * def __get__(self): return self._consensus_quality # <<<<<<<<<<<<<< + * + * property snp_quality: + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_17consensus_quality_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_17consensus_quality_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2635); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_SNPCall *)__pyx_v_self)->_consensus_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2635; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.SNPCall.consensus_quality.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2639 + * 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: + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_11snp_quality_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_11snp_quality_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2639); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_SNPCall *)__pyx_v_self)->_snp_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2639; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.SNPCall.snp_quality.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2643 + * 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: + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_15mapping_quality_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_15mapping_quality_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2643); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_SNPCall *)__pyx_v_self)->_rms_mapping_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2643; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.SNPCall.mapping_quality.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2647 + * property coverage: + * '''coverage or read depth - the number of reads involved in the call.''' + * def __get__(self): return self._coverage # <<<<<<<<<<<<<< + * + * def __str__(self): + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_8coverage_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_8coverage_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2647); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_SNPCall *)__pyx_v_self)->_coverage); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2647; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.SNPCall.coverage.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2649 + * def __get__(self): return self._coverage + * + * def __str__(self): # <<<<<<<<<<<<<< + * + * return "\t".join( map(str, ( + */ + +static PyObject *__pyx_pf_9csamtools_7SNPCall_0__str__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_7SNPCall_0__str__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + 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; + PyObject *__pyx_t_10 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__str__"); + __Pyx_TraceCall("__str__", __pyx_f[0], 2649); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2651 + * def __str__(self): + * + * return "\t".join( map(str, ( # <<<<<<<<<<<<<< + * self.tid, + * self.pos, + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_1), __pyx_n_s__join); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2652 + * + * return "\t".join( map(str, ( + * self.tid, # <<<<<<<<<<<<<< + * self.pos, + * self.reference_base, + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__tid); 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); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2653 + * return "\t".join( map(str, ( + * self.tid, + * self.pos, # <<<<<<<<<<<<<< + * self.reference_base, + * self.genotype, + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__pos); 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); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2654 + * self.tid, + * self.pos, + * self.reference_base, # <<<<<<<<<<<<<< + * self.genotype, + * self.consensus_quality, + */ + __pyx_t_4 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__reference_base); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2654; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2655 + * self.pos, + * self.reference_base, + * self.genotype, # <<<<<<<<<<<<<< + * self.consensus_quality, + * self.snp_quality, + */ + __pyx_t_5 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__genotype); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2656 + * self.reference_base, + * self.genotype, + * self.consensus_quality, # <<<<<<<<<<<<<< + * self.snp_quality, + * self.mapping_quality, + */ + __pyx_t_6 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__consensus_quality); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2656; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2657 + * self.genotype, + * self.consensus_quality, + * self.snp_quality, # <<<<<<<<<<<<<< + * self.mapping_quality, + * self.coverage ) ) ) + */ + __pyx_t_7 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__snp_quality); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2657; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2658 + * self.consensus_quality, + * self.snp_quality, + * self.mapping_quality, # <<<<<<<<<<<<<< + * self.coverage ) ) ) + * + */ + __pyx_t_8 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__mapping_quality); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2658; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2659 + * self.snp_quality, + * self.mapping_quality, + * self.coverage ) ) ) # <<<<<<<<<<<<<< + * + * + */ + __pyx_t_9 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__coverage); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2659; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + __pyx_t_10 = PyTuple_New(8); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2652; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_10)); + PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_2); + __Pyx_GIVEREF(__pyx_t_2); + PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_t_3); + __Pyx_GIVEREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_10, 2, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_10, 3, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_10, 4, __pyx_t_6); + __Pyx_GIVEREF(__pyx_t_6); + PyTuple_SET_ITEM(__pyx_t_10, 5, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + PyTuple_SET_ITEM(__pyx_t_10, 6, __pyx_t_8); + __Pyx_GIVEREF(__pyx_t_8); + PyTuple_SET_ITEM(__pyx_t_10, 7, __pyx_t_9); + __Pyx_GIVEREF(__pyx_t_9); + __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; + __pyx_t_9 = 0; + __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_9)); + __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_9, 0, ((PyObject *)((PyObject*)(&PyString_Type)))); + __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_9, 1, ((PyObject *)__pyx_t_10)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_10)); + __pyx_t_10 = 0; + __pyx_t_10 = PyObject_Call(__pyx_builtin_map, ((PyObject *)__pyx_t_9), 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(((PyObject *)__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 = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_9)); + PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_10); + __Pyx_GIVEREF(__pyx_t_10); + __pyx_t_10 = 0; + __pyx_t_10 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_9), 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_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_9)); __pyx_t_9 = 0; + __pyx_r = __pyx_t_10; + __pyx_t_10 = 0; + goto __pyx_L0; + + __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_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("csamtools.SNPCall.__str__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2680 + * cdef IteratorColumn iter + * + * def __cinit__(self, # <<<<<<<<<<<<<< + * IteratorColumn iterator_column, + * **kwargs ): + */ + +static int __pyx_pf_9csamtools_13SNPCallerBase_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_13SNPCallerBase_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_iterator_column = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + float __pyx_t_4; + int __pyx_t_5; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__iterator_column,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 2680); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__iterator_column); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2680; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)PyTuple_GET_ITEM(__pyx_args, 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 = 2680; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.SNPCallerBase.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_iterator_column), __pyx_ptype_9csamtools_IteratorColumn, 1, "iterator_column", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2681; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2684 + * **kwargs ): + * + * self.iter = iterator_column # <<<<<<<<<<<<<< + * self.c = bam_maqcns_init() + * + */ + __Pyx_INCREF(((PyObject *)__pyx_v_iterator_column)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_iterator_column)); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->iter); + __Pyx_DECREF(((PyObject *)((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->iter)); + ((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->iter = __pyx_v_iterator_column; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2685 + * + * self.iter = iterator_column + * self.c = bam_maqcns_init() # <<<<<<<<<<<<<< + * + * # set the default parameterization according to + */ + ((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c = bam_maqcns_init(); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2691 + * + * # new default mode for samtools >0.1.10 + * self.c.errmod = kwargs.get( "errmod", BAM_ERRMOD_MAQ2 ) # <<<<<<<<<<<<<< + * + * self.c.min_baseQ = kwargs.get( "min_baseQ", 13 ) + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2691; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = PyInt_FromLong(BAM_ERRMOD_MAQ2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2691; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__errmod), __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2691; __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_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2691; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + ((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c->errmod = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2693 + * self.c.errmod = kwargs.get( "errmod", BAM_ERRMOD_MAQ2 ) + * + * self.c.min_baseQ = kwargs.get( "min_baseQ", 13 ) # <<<<<<<<<<<<<< + * # self.c.capQ_thres = kwargs.get( "capQ_threshold", 60 ) + * self.c.n_hap = kwargs.get( "n_haplotypes", 2 ) + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2693; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__min_baseQ), __pyx_int_13); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2693; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2693; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + ((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c->min_baseQ = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2695 + * self.c.min_baseQ = kwargs.get( "min_baseQ", 13 ) + * # self.c.capQ_thres = kwargs.get( "capQ_threshold", 60 ) + * self.c.n_hap = kwargs.get( "n_haplotypes", 2 ) # <<<<<<<<<<<<<< + * self.c.het_rate = kwargs.get( "het_rate", 0.001 ) + * self.c.theta = kwargs.get( "theta", 0.83 ) + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2695; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__n_haplotypes), __pyx_int_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2695; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2695; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + ((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c->n_hap = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2696 + * # self.c.capQ_thres = kwargs.get( "capQ_threshold", 60 ) + * self.c.n_hap = kwargs.get( "n_haplotypes", 2 ) + * self.c.het_rate = kwargs.get( "het_rate", 0.001 ) # <<<<<<<<<<<<<< + * self.c.theta = kwargs.get( "theta", 0.83 ) + * + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2696; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = PyFloat_FromDouble(0.001); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2696; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__het_rate), __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2696; __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_PyFloat_AsDouble(__pyx_t_1); if (unlikely((__pyx_t_4 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2696; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + ((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c->het_rate = __pyx_t_4; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2697 + * self.c.n_hap = kwargs.get( "n_haplotypes", 2 ) + * self.c.het_rate = kwargs.get( "het_rate", 0.001 ) + * self.c.theta = kwargs.get( "theta", 0.83 ) # <<<<<<<<<<<<<< + * + * if self.c.errmod != BAM_ERRMOD_MAQ2: + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2697; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = PyFloat_FromDouble(0.83); 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_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__theta), __pyx_t_1); 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_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_4 = __pyx_PyFloat_AsDouble(__pyx_t_2); if (unlikely((__pyx_t_4 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2697; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + ((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c->theta = __pyx_t_4; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2699 + * self.c.theta = kwargs.get( "theta", 0.83 ) + * + * if self.c.errmod != BAM_ERRMOD_MAQ2: # <<<<<<<<<<<<<< + * self.c.theta += 0.02 + * + */ + __pyx_t_5 = (((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c->errmod != BAM_ERRMOD_MAQ2); + if (__pyx_t_5) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2700 + * + * if self.c.errmod != BAM_ERRMOD_MAQ2: + * self.c.theta += 0.02 # <<<<<<<<<<<<<< + * + * # call prepare AFTER setting parameters + */ + ((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c->theta = (((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c->theta + 0.02); + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2703 + * + * # call prepare AFTER setting parameters + * bam_maqcns_prepare( self.c ) # <<<<<<<<<<<<<< + * + * def __dealloc__(self): + */ + bam_maqcns_prepare(((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c); + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.SNPCallerBase.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2705 + * bam_maqcns_prepare( self.c ) + * + * def __dealloc__(self): # <<<<<<<<<<<<<< + * bam_maqcns_destroy( self.c ) + * + */ + +static void __pyx_pf_9csamtools_13SNPCallerBase_1__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_13SNPCallerBase_1__dealloc__(PyObject *__pyx_v_self) { + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 2705); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2706 + * + * def __dealloc__(self): + * bam_maqcns_destroy( self.c ) # <<<<<<<<<<<<<< + * + * cdef __dump( self, glf1_t * g, uint32_t cns, int rb ): + */ + bam_maqcns_destroy(((struct __pyx_obj_9csamtools_SNPCallerBase *)__pyx_v_self)->c); + + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2708 + * bam_maqcns_destroy( self.c ) + * + * cdef __dump( self, glf1_t * g, uint32_t cns, int rb ): # <<<<<<<<<<<<<< + * '''debugging output.''' + * + */ + +static PyObject *__pyx_f_9csamtools_13SNPCallerBase___dump(struct __pyx_obj_9csamtools_SNPCallerBase *__pyx_v_self, glf1_t *__pyx_v_g, uint32_t __pyx_v_cns, int __pyx_v_rb) { + int __pyx_v_x; + PyObject *__pyx_r = NULL; + 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; + PyObject *__pyx_t_10 = NULL; + PyObject *__pyx_t_11 = NULL; + PyObject *__pyx_t_12 = NULL; + PyObject *__pyx_t_13 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dump"); + __Pyx_TraceCall("__dump", __pyx_f[0], 2708); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2711 + * '''debugging output.''' + * + * pysam_dump_glf( g, self.c ); # <<<<<<<<<<<<<< + * print "" + * for x in range(self.iter.n_plp): + */ + pysam_dump_glf(__pyx_v_g, __pyx_v_self->c); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2712 + * + * pysam_dump_glf( g, self.c ); + * print "" # <<<<<<<<<<<<<< + * for x in range(self.iter.n_plp): + * print "--> read %i %s %i" % (x, + */ + if (__Pyx_PrintOne(0, ((PyObject *)__pyx_kp_s_12)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2712; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2713 + * pysam_dump_glf( g, self.c ); + * print "" + * for x in range(self.iter.n_plp): # <<<<<<<<<<<<<< + * print "--> read %i %s %i" % (x, + * bam1_qname(self.iter.plp[x].b), + */ + __pyx_t_1 = __pyx_v_self->iter->n_plp; + for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) { + __pyx_v_x = __pyx_t_2; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2714 + * print "" + * for x in range(self.iter.n_plp): + * print "--> read %i %s %i" % (x, # <<<<<<<<<<<<<< + * bam1_qname(self.iter.plp[x].b), + * self.iter.plp[x].qpos, + */ + __pyx_t_3 = PyInt_FromLong(__pyx_v_x); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2714; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2715 + * for x in range(self.iter.n_plp): + * print "--> read %i %s %i" % (x, + * bam1_qname(self.iter.plp[x].b), # <<<<<<<<<<<<<< + * self.iter.plp[x].qpos, + * ) + */ + __pyx_t_4 = PyBytes_FromString(bam1_qname((__pyx_v_self->iter->plp[__pyx_v_x]).b)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2715; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2716 + * print "--> read %i %s %i" % (x, + * bam1_qname(self.iter.plp[x].b), + * self.iter.plp[x].qpos, # <<<<<<<<<<<<<< + * ) + * + */ + __pyx_t_5 = __Pyx_PyInt_to_py_int32_t((__pyx_v_self->iter->plp[__pyx_v_x]).qpos); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2716; __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 = 2714; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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, ((PyObject *)__pyx_t_4)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); + PyTuple_SET_ITEM(__pyx_t_6, 2, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + __pyx_t_3 = 0; + __pyx_t_4 = 0; + __pyx_t_5 = 0; + __pyx_t_5 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_156), ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2714; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; + if (__Pyx_PrintOne(0, ((PyObject *)__pyx_t_5)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2714; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2720 + * + * print "pos=%i, cns=%i, q_r = %f, depth=%i, n=%i, rb=%i, cns-cq=%i %i %i %i" \ + * % (self.iter.pos, # <<<<<<<<<<<<<< + * cns, + * self.c.q_r, + */ + __pyx_t_5 = PyInt_FromLong(__pyx_v_self->iter->pos); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2720; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2721 + * print "pos=%i, cns=%i, q_r = %f, depth=%i, n=%i, rb=%i, cns-cq=%i %i %i %i" \ + * % (self.iter.pos, + * cns, # <<<<<<<<<<<<<< + * self.c.q_r, + * self.iter.n_plp, + */ + __pyx_t_6 = __Pyx_PyInt_to_py_uint32_t(__pyx_v_cns); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2721; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2722 + * % (self.iter.pos, + * cns, + * self.c.q_r, # <<<<<<<<<<<<<< + * self.iter.n_plp, + * self.iter.n_plp, + */ + __pyx_t_4 = PyFloat_FromDouble(__pyx_v_self->c->q_r); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2722; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2723 + * cns, + * self.c.q_r, + * self.iter.n_plp, # <<<<<<<<<<<<<< + * self.iter.n_plp, + * rb, + */ + __pyx_t_3 = PyInt_FromLong(__pyx_v_self->iter->n_plp); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2723; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2724 + * self.c.q_r, + * self.iter.n_plp, + * self.iter.n_plp, # <<<<<<<<<<<<<< + * rb, + * cns >> 8 & 0xff, + */ + __pyx_t_7 = PyInt_FromLong(__pyx_v_self->iter->n_plp); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2724; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2725 + * self.iter.n_plp, + * self.iter.n_plp, + * rb, # <<<<<<<<<<<<<< + * cns >> 8 & 0xff, + * cns >> 16 & 0xff, + */ + __pyx_t_8 = PyInt_FromLong(__pyx_v_rb); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2725; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2726 + * self.iter.n_plp, + * rb, + * cns >> 8 & 0xff, # <<<<<<<<<<<<<< + * cns >> 16 & 0xff, + * cns & 0xff, + */ + __pyx_t_9 = PyInt_FromLong(((__pyx_v_cns >> 8) & 0xff)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2726; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2727 + * rb, + * cns >> 8 & 0xff, + * cns >> 16 & 0xff, # <<<<<<<<<<<<<< + * cns & 0xff, + * cns >> 28, + */ + __pyx_t_10 = PyInt_FromLong(((__pyx_v_cns >> 16) & 0xff)); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_10); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2728 + * cns >> 8 & 0xff, + * cns >> 16 & 0xff, + * cns & 0xff, # <<<<<<<<<<<<<< + * cns >> 28, + * ) + */ + __pyx_t_11 = PyInt_FromLong((__pyx_v_cns & 0xff)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2728; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_11); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2729 + * cns >> 16 & 0xff, + * cns & 0xff, + * cns >> 28, # <<<<<<<<<<<<<< + * ) + * + */ + __pyx_t_12 = PyInt_FromLong((__pyx_v_cns >> 28)); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2729; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_12); + __pyx_t_13 = PyTuple_New(10); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2720; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_13)); + PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_13, 1, __pyx_t_6); + __Pyx_GIVEREF(__pyx_t_6); + PyTuple_SET_ITEM(__pyx_t_13, 2, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_13, 3, __pyx_t_3); + __Pyx_GIVEREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_13, 4, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + PyTuple_SET_ITEM(__pyx_t_13, 5, __pyx_t_8); + __Pyx_GIVEREF(__pyx_t_8); + PyTuple_SET_ITEM(__pyx_t_13, 6, __pyx_t_9); + __Pyx_GIVEREF(__pyx_t_9); + PyTuple_SET_ITEM(__pyx_t_13, 7, __pyx_t_10); + __Pyx_GIVEREF(__pyx_t_10); + PyTuple_SET_ITEM(__pyx_t_13, 8, __pyx_t_11); + __Pyx_GIVEREF(__pyx_t_11); + PyTuple_SET_ITEM(__pyx_t_13, 9, __pyx_t_12); + __Pyx_GIVEREF(__pyx_t_12); + __pyx_t_5 = 0; + __pyx_t_6 = 0; + __pyx_t_4 = 0; + __pyx_t_3 = 0; + __pyx_t_7 = 0; + __pyx_t_8 = 0; + __pyx_t_9 = 0; + __pyx_t_10 = 0; + __pyx_t_11 = 0; + __pyx_t_12 = 0; + __pyx_t_12 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_157), ((PyObject *)__pyx_t_13)); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2720; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_12)); + __Pyx_DECREF(((PyObject *)__pyx_t_13)); __pyx_t_13 = 0; + if (__Pyx_PrintOne(0, ((PyObject *)__pyx_t_12)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2719; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_12)); __pyx_t_12 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2732 + * ) + * + * printf("-------------------------------------\n"); # <<<<<<<<<<<<<< + * sys.stdout.flush() + * + */ + printf(__pyx_k_158); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2733 + * + * printf("-------------------------------------\n"); + * sys.stdout.flush() # <<<<<<<<<<<<<< + * + * cdef class IteratorSNPCalls( SNPCallerBase ): + */ + __pyx_t_12 = __Pyx_GetName(__pyx_m, __pyx_n_s__sys); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2733; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_12); + __pyx_t_13 = PyObject_GetAttr(__pyx_t_12, __pyx_n_s__stdout); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2733; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_13); + __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0; + __pyx_t_12 = PyObject_GetAttr(__pyx_t_13, __pyx_n_s__flush); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2733; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_12); + __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0; + __pyx_t_13 = PyObject_Call(__pyx_t_12, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2733; __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_13); __pyx_t_13 = 0; + + __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_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_XDECREF(__pyx_t_13); + __Pyx_AddTraceback("csamtools.SNPCallerBase.__dump"); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2749 + * """ + * + * def __cinit__(self, # <<<<<<<<<<<<<< + * IteratorColumn iterator_column, + * **kwargs ): + */ + +static int __pyx_pf_9csamtools_16IteratorSNPCalls_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_16IteratorSNPCalls_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_iterator_column = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__iterator_column,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 2749); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__iterator_column); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2749; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)PyTuple_GET_ITEM(__pyx_args, 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 = 2749; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.IteratorSNPCalls.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_iterator_column), __pyx_ptype_9csamtools_IteratorColumn, 1, "iterator_column", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2750; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2753 + * **kwargs ): + * + * assert self.iter.hasReference(), "IteratorSNPCalls requires an pileup iterator with reference sequence" # <<<<<<<<<<<<<< + * + * def __iter__(self): + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + __pyx_t_1 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter), __pyx_n_s__hasReference); 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_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2753; __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 = 2753; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (unlikely(!__pyx_t_3)) { + PyErr_SetObject(PyExc_AssertionError, ((PyObject *)__pyx_kp_s_159)); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2753; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IteratorSNPCalls.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2755 + * assert self.iter.hasReference(), "IteratorSNPCalls requires an pileup iterator with reference sequence" + * + * def __iter__(self): # <<<<<<<<<<<<<< + * return self + * + */ + +static PyObject *__pyx_pf_9csamtools_16IteratorSNPCalls_1__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_16IteratorSNPCalls_1__iter__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__iter__"); + __Pyx_TraceCall("__iter__", __pyx_f[0], 2755); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2756 + * + * 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; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2758 + * return self + * + * def __next__(self): # <<<<<<<<<<<<<< + * """python version of next(). + * """ + */ + +static PyObject *__pyx_pf_9csamtools_16IteratorSNPCalls_2__next__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_16IteratorSNPCalls_2__next__[] = "python version of next().\n "; +struct wrapperbase __pyx_wrapperbase_9csamtools_16IteratorSNPCalls_2__next__; +static PyObject *__pyx_pf_9csamtools_16IteratorSNPCalls_2__next__(PyObject *__pyx_v_self) { + char *__pyx_v_seq; + int __pyx_v_seq_len; + int __pyx_v_rb; + uint32_t __pyx_v_cns; + glf1_t *__pyx_v_g; + struct __pyx_obj_9csamtools_SNPCall *__pyx_v_call; + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_5 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__next__"); + __Pyx_TraceCall("__next__", __pyx_f[0], 2758); + __pyx_v_call = ((struct __pyx_obj_9csamtools_SNPCall *)Py_None); __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2763 + * + * # the following code was adapted from bam_plcmd.c:pileup_func() + * self.iter.cnext() # <<<<<<<<<<<<<< + * + * if self.iter.n_plp < 0: + */ + ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->__pyx_vtab)->cnext(((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2765 + * self.iter.cnext() + * + * if self.iter.n_plp < 0: # <<<<<<<<<<<<<< + * raise ValueError("error during iteration" ) + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->n_plp < 0); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2766 + * + * if self.iter.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.iter.plp == NULL: + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_160), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2766; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2766; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2768 + * raise ValueError("error during iteration" ) + * + * if self.iter.plp == NULL: # <<<<<<<<<<<<<< + * raise StopIteration + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->plp == NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2769 + * + * if self.iter.plp == NULL: + * raise StopIteration # <<<<<<<<<<<<<< + * + * cdef char * seq = self.iter.getSequence() + */ + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2769; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2771 + * raise StopIteration + * + * cdef char * seq = self.iter.getSequence() # <<<<<<<<<<<<<< + * cdef int seq_len = self.iter.seq_len + * + */ + __pyx_v_seq = ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->__pyx_vtab)->getSequence(((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2772 + * + * cdef char * seq = self.iter.getSequence() + * cdef int seq_len = self.iter.seq_len # <<<<<<<<<<<<<< + * + * assert seq != NULL + */ + __pyx_t_2 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter), __pyx_n_s__seq_len); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2772; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2772; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_v_seq_len = __pyx_t_3; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2774 + * cdef int seq_len = self.iter.seq_len + * + * assert seq != NULL # <<<<<<<<<<<<<< + * + * # reference base + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + if (unlikely(!(__pyx_v_seq != NULL))) { + PyErr_SetNone(PyExc_AssertionError); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2774; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2777 + * + * # reference base + * if self.iter.pos >= seq_len: # <<<<<<<<<<<<<< + * raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->pos >= __pyx_v_seq_len); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2778 + * # reference base + * if self.iter.pos >= seq_len: + * raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) # <<<<<<<<<<<<<< + * + * cdef int rb = seq[self.iter.pos] + */ + __pyx_t_2 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2778; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_4 = PyInt_FromLong(__pyx_v_seq_len); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2778; __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 = 2778; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_4); + __Pyx_GIVEREF(__pyx_t_4); + __pyx_t_2 = 0; + __pyx_t_4 = 0; + __pyx_t_4 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_161), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2778; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_DECREF(((PyObject *)__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 = 2778; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_4)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); + __pyx_t_4 = 0; + __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2778; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __Pyx_Raise(__pyx_t_4, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2778; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2780 + * raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) + * + * cdef int rb = seq[self.iter.pos] # <<<<<<<<<<<<<< + * cdef uint32_t cns + * cdef glf1_t * g + */ + __pyx_v_rb = (__pyx_v_seq[((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->pos]); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2787 + * self.iter.plp, + * bam_nt16_table[rb], + * self.c ) # <<<<<<<<<<<<<< + * + * if pysam_glf_depth( g ) == 0: + */ + __pyx_v_g = bam_maqcns_glfgen(((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->n_plp, ((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->plp, (bam_nt16_table[__pyx_v_rb]), ((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.c); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2789 + * self.c ) + * + * if pysam_glf_depth( g ) == 0: # <<<<<<<<<<<<<< + * cns = 0xfu << 28 | 0xf << 24 + * else: + */ + __pyx_t_1 = (pysam_glf_depth(__pyx_v_g) == 0); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2790 + * + * if pysam_glf_depth( g ) == 0: + * cns = 0xfu << 28 | 0xf << 24 # <<<<<<<<<<<<<< + * else: + * cns = glf2cns(g, (self.c.q_r + .499)) + */ + __pyx_v_cns = 4278190080; + goto __pyx_L8; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2792 + * cns = 0xfu << 28 | 0xf << 24 + * else: + * cns = glf2cns(g, (self.c.q_r + .499)) # <<<<<<<<<<<<<< + * + * free(g) + */ + __pyx_v_cns = glf2cns(__pyx_v_g, ((int)(((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.c->q_r + .499))); + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2794 + * cns = glf2cns(g, (self.c.q_r + .499)) + * + * free(g) # <<<<<<<<<<<<<< + * + * cdef SNPCall call + */ + free(__pyx_v_g); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2798 + * cdef SNPCall call + * + * call = SNPCall() # <<<<<<<<<<<<<< + * call._tid = self.iter.tid + * call._pos = self.iter.pos + */ + __pyx_t_4 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_SNPCall)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2798; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__pyx_v_call)); + __pyx_v_call = ((struct __pyx_obj_9csamtools_SNPCall *)__pyx_t_4); + __pyx_t_4 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2799 + * + * call = SNPCall() + * call._tid = self.iter.tid # <<<<<<<<<<<<<< + * call._pos = self.iter.pos + * call._reference_base = rb + */ + __pyx_v_call->_tid = ((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->tid; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2800 + * call = SNPCall() + * call._tid = self.iter.tid + * call._pos = self.iter.pos # <<<<<<<<<<<<<< + * call._reference_base = rb + * call._genotype = bam_nt16_rev_table[cns>>28] + */ + __pyx_v_call->_pos = ((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->pos; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2801 + * call._tid = self.iter.tid + * call._pos = self.iter.pos + * call._reference_base = rb # <<<<<<<<<<<<<< + * call._genotype = bam_nt16_rev_table[cns>>28] + * call._consensus_quality = cns >> 8 & 0xff + */ + __pyx_v_call->_reference_base = __pyx_v_rb; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2802 + * call._pos = self.iter.pos + * call._reference_base = rb + * call._genotype = bam_nt16_rev_table[cns>>28] # <<<<<<<<<<<<<< + * call._consensus_quality = cns >> 8 & 0xff + * call._snp_quality = cns & 0xff + */ + __pyx_v_call->_genotype = (__pyx_v_9csamtools_bam_nt16_rev_table[(__pyx_v_cns >> 28)]); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2803 + * call._reference_base = rb + * call._genotype = bam_nt16_rev_table[cns>>28] + * call._consensus_quality = cns >> 8 & 0xff # <<<<<<<<<<<<<< + * call._snp_quality = cns & 0xff + * call._rms_mapping_quality = cns >> 16&0xff + */ + __pyx_v_call->_consensus_quality = ((__pyx_v_cns >> 8) & 0xff); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2804 + * call._genotype = bam_nt16_rev_table[cns>>28] + * call._consensus_quality = cns >> 8 & 0xff + * call._snp_quality = cns & 0xff # <<<<<<<<<<<<<< + * call._rms_mapping_quality = cns >> 16&0xff + * call._coverage = self.iter.n_plp + */ + __pyx_v_call->_snp_quality = (__pyx_v_cns & 0xff); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2805 + * call._consensus_quality = cns >> 8 & 0xff + * call._snp_quality = cns & 0xff + * call._rms_mapping_quality = cns >> 16&0xff # <<<<<<<<<<<<<< + * call._coverage = self.iter.n_plp + * + */ + __pyx_v_call->_rms_mapping_quality = ((__pyx_v_cns >> 16) & 0xff); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2806 + * call._snp_quality = cns & 0xff + * call._rms_mapping_quality = cns >> 16&0xff + * call._coverage = self.iter.n_plp # <<<<<<<<<<<<<< + * + * return call + */ + __pyx_v_call->_coverage = ((struct __pyx_obj_9csamtools_IteratorSNPCalls *)__pyx_v_self)->__pyx_base.iter->n_plp; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2808 + * call._coverage = self.iter.n_plp + * + * return call # <<<<<<<<<<<<<< + * + * cdef class SNPCaller( SNPCallerBase ): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_call)); + __pyx_r = ((PyObject *)__pyx_v_call); + goto __pyx_L0; + + __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("csamtools.IteratorSNPCalls.__next__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF((PyObject *)__pyx_v_call); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2824 + * + * + * def __cinit__(self, # <<<<<<<<<<<<<< + * IteratorColumn iterator_column, + * **kwargs ): + */ + +static int __pyx_pf_9csamtools_9SNPCaller_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_9SNPCaller_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_iterator_column = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__iterator_column,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 2824); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__iterator_column); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2824; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)PyTuple_GET_ITEM(__pyx_args, 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 = 2824; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.SNPCaller.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_iterator_column), __pyx_ptype_9csamtools_IteratorColumn, 1, "iterator_column", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2825; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.SNPCaller.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2830 + * pass + * + * def call(self, reference, int pos ): # <<<<<<<<<<<<<< + * """call a snp on chromosome *reference* + * and position *pos*. + */ + +static PyObject *__pyx_pf_9csamtools_9SNPCaller_1call(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_9SNPCaller_1call[] = "call a snp on chromosome *reference*\n and position *pos*.\n\n returns a :class:`SNPCall` object.\n "; +static PyObject *__pyx_pf_9csamtools_9SNPCaller_1call(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_reference = 0; + int __pyx_v_pos; + int __pyx_v_tid; + char *__pyx_v_seq; + int __pyx_v_seq_len; + int __pyx_v_rb; + uint32_t __pyx_v_cns; + glf1_t *__pyx_v_g; + struct __pyx_obj_9csamtools_SNPCall *__pyx_v_call; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + PyObject *__pyx_t_5 = NULL; + int __pyx_t_6; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__reference,&__pyx_n_s__pos,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("call"); + __Pyx_TraceCall("call", __pyx_f[0], 2830); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__reference); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__pos); + if (likely(values[1])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("call", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2830; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "call") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2830; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_reference = values[0]; + __pyx_v_pos = __Pyx_PyInt_AsInt(values[1]); if (unlikely((__pyx_v_pos == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2830; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_reference = PyTuple_GET_ITEM(__pyx_args, 0); + __pyx_v_pos = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 1)); if (unlikely((__pyx_v_pos == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2830; __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 = 2830; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.SNPCaller.call"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_v_call = ((struct __pyx_obj_9csamtools_SNPCall *)Py_None); __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2837 + * """ + * + * cdef int tid = self.iter.samfile.gettid( reference ) # <<<<<<<<<<<<<< + * + * self.iter.reset( tid, pos, pos + 1 ) + */ + __pyx_t_1 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->samfile), __pyx_n_s__gettid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2837; __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 = 2837; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2837; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __pyx_t_4 = __Pyx_PyInt_AsInt(__pyx_t_3); if (unlikely((__pyx_t_4 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2837; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_v_tid = __pyx_t_4; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2839 + * cdef int tid = self.iter.samfile.gettid( reference ) + * + * self.iter.reset( tid, pos, pos + 1 ) # <<<<<<<<<<<<<< + * + * while 1: + */ + __pyx_t_3 = PyInt_FromLong(__pyx_v_tid); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2839; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_2 = PyInt_FromLong(__pyx_v_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2839; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyInt_FromLong((__pyx_v_pos + 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2839; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_5 = ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->__pyx_vtab)->reset(((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter, __pyx_t_3, __pyx_t_2, __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2839; __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_2); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2841 + * self.iter.reset( tid, pos, pos + 1 ) + * + * while 1: # <<<<<<<<<<<<<< + * self.iter.cnext() + * + */ + while (1) { + if (!1) break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2842 + * + * while 1: + * self.iter.cnext() # <<<<<<<<<<<<<< + * + * if self.iter.n_plp < 0: + */ + ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->__pyx_vtab)->cnext(((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2844 + * self.iter.cnext() + * + * if self.iter.n_plp < 0: # <<<<<<<<<<<<<< + * raise ValueError("error during iteration" ) + * + */ + __pyx_t_6 = (((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->n_plp < 0); + if (__pyx_t_6) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2845 + * + * if self.iter.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.iter.plp == NULL: + */ + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_162), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2845; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2845; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2847 + * raise ValueError("error during iteration" ) + * + * if self.iter.plp == NULL: # <<<<<<<<<<<<<< + * raise ValueError( "no reads in region - no call" ) + * + */ + __pyx_t_6 = (((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->plp == NULL); + if (__pyx_t_6) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2848 + * + * if self.iter.plp == NULL: + * raise ValueError( "no reads in region - no call" ) # <<<<<<<<<<<<<< + * + * if self.iter.pos == pos: break + */ + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_164), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2848; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2848; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2850 + * raise ValueError( "no reads in region - no call" ) + * + * if self.iter.pos == pos: break # <<<<<<<<<<<<<< + * + * cdef char * seq = self.iter.getSequence() + */ + __pyx_t_6 = (((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->pos == __pyx_v_pos); + if (__pyx_t_6) { + goto __pyx_L7_break; + goto __pyx_L10; + } + __pyx_L10:; + } + __pyx_L7_break:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2852 + * if self.iter.pos == pos: break + * + * cdef char * seq = self.iter.getSequence() # <<<<<<<<<<<<<< + * cdef int seq_len = self.iter.seq_len + * + */ + __pyx_v_seq = ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->__pyx_vtab)->getSequence(((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2853 + * + * cdef char * seq = self.iter.getSequence() + * cdef int seq_len = self.iter.seq_len # <<<<<<<<<<<<<< + * + * assert seq != NULL + */ + __pyx_t_5 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter), __pyx_n_s__seq_len); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_4 = __Pyx_PyInt_AsInt(__pyx_t_5); if (unlikely((__pyx_t_4 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_seq_len = __pyx_t_4; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2855 + * cdef int seq_len = self.iter.seq_len + * + * assert seq != NULL # <<<<<<<<<<<<<< + * + * # reference base + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + if (unlikely(!(__pyx_v_seq != NULL))) { + PyErr_SetNone(PyExc_AssertionError); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2855; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2858 + * + * # reference base + * if self.iter.pos >= seq_len: # <<<<<<<<<<<<<< + * raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) + * + */ + __pyx_t_6 = (((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->pos >= __pyx_v_seq_len); + if (__pyx_t_6) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2859 + * # reference base + * if self.iter.pos >= seq_len: + * raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) # <<<<<<<<<<<<<< + * + * cdef int rb = seq[self.iter.pos] + */ + __pyx_t_5 = PyInt_FromLong(((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->pos); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2859; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_1 = PyInt_FromLong(__pyx_v_seq_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2859; __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 = 2859; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_5 = 0; + __pyx_t_1 = 0; + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_161), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2859; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__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 = 2859; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2859; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2859; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L11; + } + __pyx_L11:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2861 + * raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) + * + * cdef int rb = seq[self.iter.pos] # <<<<<<<<<<<<<< + * cdef uint32_t cns + * cdef glf1_t * g + */ + __pyx_v_rb = (__pyx_v_seq[((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->pos]); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2868 + * self.iter.plp, + * bam_nt16_table[rb], + * self.c ) # <<<<<<<<<<<<<< + * + * + */ + __pyx_v_g = bam_maqcns_glfgen(((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->n_plp, ((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->plp, (bam_nt16_table[__pyx_v_rb]), ((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.c); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2871 + * + * + * if pysam_glf_depth( g ) == 0: # <<<<<<<<<<<<<< + * cns = 0xfu << 28 | 0xf << 24 + * else: + */ + __pyx_t_6 = (pysam_glf_depth(__pyx_v_g) == 0); + if (__pyx_t_6) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2872 + * + * if pysam_glf_depth( g ) == 0: + * cns = 0xfu << 28 | 0xf << 24 # <<<<<<<<<<<<<< + * else: + * cns = glf2cns(g, (self.c.q_r + .499)) + */ + __pyx_v_cns = 4278190080; + goto __pyx_L12; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2874 + * cns = 0xfu << 28 | 0xf << 24 + * else: + * cns = glf2cns(g, (self.c.q_r + .499)) # <<<<<<<<<<<<<< + * + * free(g) + */ + __pyx_v_cns = glf2cns(__pyx_v_g, ((int)(((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.c->q_r + .499))); + } + __pyx_L12:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2876 + * cns = glf2cns(g, (self.c.q_r + .499)) + * + * free(g) # <<<<<<<<<<<<<< + * + * cdef SNPCall call + */ + free(__pyx_v_g); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2880 + * cdef SNPCall call + * + * call = SNPCall() # <<<<<<<<<<<<<< + * call._tid = self.iter.tid + * call._pos = self.iter.pos + */ + __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_SNPCall)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2880; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_v_call)); + __pyx_v_call = ((struct __pyx_obj_9csamtools_SNPCall *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2881 + * + * call = SNPCall() + * call._tid = self.iter.tid # <<<<<<<<<<<<<< + * call._pos = self.iter.pos + * call._reference_base = rb + */ + __pyx_v_call->_tid = ((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->tid; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2882 + * call = SNPCall() + * call._tid = self.iter.tid + * call._pos = self.iter.pos # <<<<<<<<<<<<<< + * call._reference_base = rb + * call._genotype = bam_nt16_rev_table[cns>>28] + */ + __pyx_v_call->_pos = ((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->pos; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2883 + * call._tid = self.iter.tid + * call._pos = self.iter.pos + * call._reference_base = rb # <<<<<<<<<<<<<< + * call._genotype = bam_nt16_rev_table[cns>>28] + * call._consensus_quality = cns >> 8 & 0xff + */ + __pyx_v_call->_reference_base = __pyx_v_rb; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2884 + * call._pos = self.iter.pos + * call._reference_base = rb + * call._genotype = bam_nt16_rev_table[cns>>28] # <<<<<<<<<<<<<< + * call._consensus_quality = cns >> 8 & 0xff + * call._snp_quality = cns & 0xff + */ + __pyx_v_call->_genotype = (__pyx_v_9csamtools_bam_nt16_rev_table[(__pyx_v_cns >> 28)]); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2885 + * call._reference_base = rb + * call._genotype = bam_nt16_rev_table[cns>>28] + * call._consensus_quality = cns >> 8 & 0xff # <<<<<<<<<<<<<< + * call._snp_quality = cns & 0xff + * call._rms_mapping_quality = cns >> 16&0xff + */ + __pyx_v_call->_consensus_quality = ((__pyx_v_cns >> 8) & 0xff); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2886 + * call._genotype = bam_nt16_rev_table[cns>>28] + * call._consensus_quality = cns >> 8 & 0xff + * call._snp_quality = cns & 0xff # <<<<<<<<<<<<<< + * call._rms_mapping_quality = cns >> 16&0xff + * call._coverage = self.iter.n_plp + */ + __pyx_v_call->_snp_quality = (__pyx_v_cns & 0xff); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2887 + * call._consensus_quality = cns >> 8 & 0xff + * call._snp_quality = cns & 0xff + * call._rms_mapping_quality = cns >> 16&0xff # <<<<<<<<<<<<<< + * call._coverage = self.iter.n_plp + * + */ + __pyx_v_call->_rms_mapping_quality = ((__pyx_v_cns >> 16) & 0xff); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2888 + * call._snp_quality = cns & 0xff + * call._rms_mapping_quality = cns >> 16&0xff + * call._coverage = self.iter.n_plp # <<<<<<<<<<<<<< + * + * return call + */ + __pyx_v_call->_coverage = ((struct __pyx_obj_9csamtools_SNPCaller *)__pyx_v_self)->__pyx_base.iter->n_plp; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2890 + * call._coverage = self.iter.n_plp + * + * return call # <<<<<<<<<<<<<< + * + * cdef class IndelCall: + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_call)); + __pyx_r = ((PyObject *)__pyx_v_call); + goto __pyx_L0; + + __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_5); + __Pyx_AddTraceback("csamtools.SNPCaller.call"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF((PyObject *)__pyx_v_call); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2900 + * cdef bam_maqindel_ret_t * _r + * + * def __cinit__(self): # <<<<<<<<<<<<<< + * #assert r != NULL + * #self._r = r + */ + +static int __pyx_pf_9csamtools_9IndelCall_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_9IndelCall_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + int __pyx_r; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 2900); + 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 = 0; + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2907 + * property tid: + * '''the chromosome ID as is defined in the header''' + * def __get__(self): # <<<<<<<<<<<<<< + * return self._tid + * + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_3tid_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_3tid_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2907); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2908 + * '''the chromosome ID as is defined in the header''' + * def __get__(self): + * return self._tid # <<<<<<<<<<<<<< + * + * property pos: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2908; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.tid.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2912 + * property pos: + * '''nucleotide position of SNP.''' + * def __get__(self): return self._pos # <<<<<<<<<<<<<< + * + * property genotype: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_3pos_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_3pos_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2912); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2912; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.pos.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2916 + * property genotype: + * '''the genotype called.''' + * def __get__(self): # <<<<<<<<<<<<<< + * if self._r.gt == 0: + * s = PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_8genotype_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_8genotype_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_v_s; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2916); + __pyx_v_s = Py_None; __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2920 + * s = PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) + * return "%s/%s" % (s,s) + * elif self._r.gt == 1: # <<<<<<<<<<<<<< + * s = PyString_FromStringAndSize( self._r.s[1], self._r.indel2 + 1) + * return "%s/%s" % (s,s) + */ + switch (((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->gt) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2917 + * '''the genotype called.''' + * def __get__(self): + * if self._r.gt == 0: # <<<<<<<<<<<<<< + * s = PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) + * return "%s/%s" % (s,s) + */ + case 0: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2918 + * def __get__(self): + * if self._r.gt == 0: + * s = PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) # <<<<<<<<<<<<<< + * return "%s/%s" % (s,s) + * elif self._r.gt == 1: + */ + __pyx_t_1 = PyString_FromStringAndSize((((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->s[0]), (((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->indel1 + 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2918; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_v_s); + __pyx_v_s = __pyx_t_1; + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2919 + * if self._r.gt == 0: + * s = PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) + * return "%s/%s" % (s,s) # <<<<<<<<<<<<<< + * elif self._r.gt == 1: + * s = PyString_FromStringAndSize( self._r.s[1], self._r.indel2 + 1) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2919; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(__pyx_v_s); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_s); + __Pyx_GIVEREF(__pyx_v_s); + __Pyx_INCREF(__pyx_v_s); + PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_s); + __Pyx_GIVEREF(__pyx_v_s); + __pyx_t_2 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_165), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2919; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __pyx_r = ((PyObject *)__pyx_t_2); + __pyx_t_2 = 0; + goto __pyx_L0; + break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2920 + * s = PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) + * return "%s/%s" % (s,s) + * elif self._r.gt == 1: # <<<<<<<<<<<<<< + * s = PyString_FromStringAndSize( self._r.s[1], self._r.indel2 + 1) + * return "%s/%s" % (s,s) + */ + case 1: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2921 + * return "%s/%s" % (s,s) + * elif self._r.gt == 1: + * s = PyString_FromStringAndSize( self._r.s[1], self._r.indel2 + 1) # <<<<<<<<<<<<<< + * return "%s/%s" % (s,s) + * else: + */ + __pyx_t_2 = PyString_FromStringAndSize((((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->s[1]), (((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->indel2 + 1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2921; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_v_s); + __pyx_v_s = __pyx_t_2; + __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2922 + * elif self._r.gt == 1: + * s = PyString_FromStringAndSize( self._r.s[1], self._r.indel2 + 1) + * return "%s/%s" % (s,s) # <<<<<<<<<<<<<< + * else: + * return "%s/%s" % (self.first_allele, self.second_allele ) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2922; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_v_s); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_s); + __Pyx_GIVEREF(__pyx_v_s); + __Pyx_INCREF(__pyx_v_s); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_s); + __Pyx_GIVEREF(__pyx_v_s); + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_165), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2922; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __pyx_r = ((PyObject *)__pyx_t_1); + __pyx_t_1 = 0; + goto __pyx_L0; + break; + default: + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2924 + * return "%s/%s" % (s,s) + * else: + * return "%s/%s" % (self.first_allele, self.second_allele ) # <<<<<<<<<<<<<< + * + * property consensus_quality: + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__first_allele); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2924; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__second_allele); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2924; __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 = 2924; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_2); + __Pyx_GIVEREF(__pyx_t_2); + __pyx_t_1 = 0; + __pyx_t_2 = 0; + __pyx_t_2 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_165), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2924; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __pyx_r = ((PyObject *)__pyx_t_2); + __pyx_t_2 = 0; + goto __pyx_L0; + break; + } + + __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("csamtools.IndelCall.genotype.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_s); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2928 + * property consensus_quality: + * '''the genotype quality (Phred-scaled).''' + * def __get__(self): return self._r.q_cns # <<<<<<<<<<<<<< + * + * property snp_quality: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_17consensus_quality_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_17consensus_quality_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2928); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->q_cns); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2928; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.consensus_quality.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2932 + * property snp_quality: + * '''the snp quality (Phred scaled) - probability of consensus being identical to reference sequence.''' + * def __get__(self): return self._r.q_ref # <<<<<<<<<<<<<< + * + * property mapping_quality: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_11snp_quality_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_11snp_quality_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2932); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->q_ref); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2932; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.snp_quality.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2936 + * 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: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_15mapping_quality_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_15mapping_quality_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2936); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_rms_mapping_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2936; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.mapping_quality.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2940 + * property coverage: + * '''coverage or read depth - the number of reads involved in the call.''' + * def __get__(self): return self._coverage # <<<<<<<<<<<<<< + * + * property first_allele: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_8coverage_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_8coverage_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2940); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_coverage); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2940; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.coverage.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2944 + * property first_allele: + * '''sequence of first allele.''' + * def __get__(self): return PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) # <<<<<<<<<<<<<< + * + * property second_allele: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_12first_allele_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_12first_allele_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2944); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyString_FromStringAndSize((((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->s[0]), (((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->indel1 + 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2944; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.first_allele.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2948 + * property second_allele: + * '''sequence of second allele.''' + * def __get__(self): return PyString_FromStringAndSize( self._r.s[1], self._r.indel2 + 1) # <<<<<<<<<<<<<< + * + * property reads_first: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_13second_allele_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_13second_allele_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2948); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyString_FromStringAndSize((((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->s[1]), (((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->indel2 + 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2948; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.second_allele.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2952 + * property reads_first: + * '''reads supporting first allele.''' + * def __get__(self): return self._r.cnt1 # <<<<<<<<<<<<<< + * + * property reads_second: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_11reads_first_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_11reads_first_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2952); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->cnt1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2952; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.reads_first.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2956 + * property reads_second: + * '''reads supporting first allele.''' + * def __get__(self): return self._r.cnt2 # <<<<<<<<<<<<<< + * + * property reads_diff: + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_12reads_second_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_12reads_second_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2956); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->cnt2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2956; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.reads_second.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2960 + * property reads_diff: + * '''reads supporting first allele.''' + * def __get__(self): return self._r.cnt_anti # <<<<<<<<<<<<<< + * + * def __str__(self): + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_10reads_diff_0__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_10reads_diff_0__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__get__"); + __Pyx_TraceCall("__get__", __pyx_f[0], 2960); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r->cnt_anti); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2960; __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_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("csamtools.IndelCall.reads_diff.__get__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2962 + * def __get__(self): return self._r.cnt_anti + * + * def __str__(self): # <<<<<<<<<<<<<< + * + * return "\t".join( map(str, ( + */ + +static PyObject *__pyx_pf_9csamtools_9IndelCall_1__str__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_9IndelCall_1__str__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + 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; + PyObject *__pyx_t_10 = NULL; + PyObject *__pyx_t_11 = NULL; + PyObject *__pyx_t_12 = NULL; + PyObject *__pyx_t_13 = NULL; + PyObject *__pyx_t_14 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__str__"); + __Pyx_TraceCall("__str__", __pyx_f[0], 2962); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2964 + * def __str__(self): + * + * return "\t".join( map(str, ( # <<<<<<<<<<<<<< + * self.tid, + * self.pos, + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_kp_s_1), __pyx_n_s__join); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2964; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2965 + * + * return "\t".join( map(str, ( + * self.tid, # <<<<<<<<<<<<<< + * self.pos, + * self.genotype, + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2965; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2966 + * return "\t".join( map(str, ( + * self.tid, + * self.pos, # <<<<<<<<<<<<<< + * self.genotype, + * self.consensus_quality, + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2966; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2967 + * self.tid, + * self.pos, + * self.genotype, # <<<<<<<<<<<<<< + * self.consensus_quality, + * self.snp_quality, + */ + __pyx_t_4 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__genotype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2968 + * self.pos, + * self.genotype, + * self.consensus_quality, # <<<<<<<<<<<<<< + * self.snp_quality, + * self.mapping_quality, + */ + __pyx_t_5 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__consensus_quality); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2968; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2969 + * self.genotype, + * self.consensus_quality, + * self.snp_quality, # <<<<<<<<<<<<<< + * self.mapping_quality, + * self.coverage, + */ + __pyx_t_6 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__snp_quality); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2969; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2970 + * self.consensus_quality, + * self.snp_quality, + * self.mapping_quality, # <<<<<<<<<<<<<< + * self.coverage, + * self.first_allele, + */ + __pyx_t_7 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__mapping_quality); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2970; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2971 + * self.snp_quality, + * self.mapping_quality, + * self.coverage, # <<<<<<<<<<<<<< + * self.first_allele, + * self.second_allele, + */ + __pyx_t_8 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__coverage); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2971; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2972 + * self.mapping_quality, + * self.coverage, + * self.first_allele, # <<<<<<<<<<<<<< + * self.second_allele, + * self.reads_first, + */ + __pyx_t_9 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__first_allele); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2972; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2973 + * self.coverage, + * self.first_allele, + * self.second_allele, # <<<<<<<<<<<<<< + * self.reads_first, + * self.reads_second, + */ + __pyx_t_10 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__second_allele); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_10); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2974 + * self.first_allele, + * self.second_allele, + * self.reads_first, # <<<<<<<<<<<<<< + * self.reads_second, + * self.reads_diff ) ) ) + */ + __pyx_t_11 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__reads_first); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2974; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_11); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2975 + * self.second_allele, + * self.reads_first, + * self.reads_second, # <<<<<<<<<<<<<< + * self.reads_diff ) ) ) + * + */ + __pyx_t_12 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__reads_second); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2975; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_12); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2976 + * self.reads_first, + * self.reads_second, + * self.reads_diff ) ) ) # <<<<<<<<<<<<<< + * + * def __dealloc__(self ): + */ + __pyx_t_13 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__reads_diff); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2976; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_13); + __pyx_t_14 = PyTuple_New(12); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2965; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_14)); + PyTuple_SET_ITEM(__pyx_t_14, 0, __pyx_t_2); + __Pyx_GIVEREF(__pyx_t_2); + PyTuple_SET_ITEM(__pyx_t_14, 1, __pyx_t_3); + __Pyx_GIVEREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_14, 2, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_14, 3, __pyx_t_5); + __Pyx_GIVEREF(__pyx_t_5); + PyTuple_SET_ITEM(__pyx_t_14, 4, __pyx_t_6); + __Pyx_GIVEREF(__pyx_t_6); + PyTuple_SET_ITEM(__pyx_t_14, 5, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + PyTuple_SET_ITEM(__pyx_t_14, 6, __pyx_t_8); + __Pyx_GIVEREF(__pyx_t_8); + PyTuple_SET_ITEM(__pyx_t_14, 7, __pyx_t_9); + __Pyx_GIVEREF(__pyx_t_9); + PyTuple_SET_ITEM(__pyx_t_14, 8, __pyx_t_10); + __Pyx_GIVEREF(__pyx_t_10); + PyTuple_SET_ITEM(__pyx_t_14, 9, __pyx_t_11); + __Pyx_GIVEREF(__pyx_t_11); + PyTuple_SET_ITEM(__pyx_t_14, 10, __pyx_t_12); + __Pyx_GIVEREF(__pyx_t_12); + PyTuple_SET_ITEM(__pyx_t_14, 11, __pyx_t_13); + __Pyx_GIVEREF(__pyx_t_13); + __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; + __pyx_t_9 = 0; + __pyx_t_10 = 0; + __pyx_t_11 = 0; + __pyx_t_12 = 0; + __pyx_t_13 = 0; + __pyx_t_13 = PyTuple_New(2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2964; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_13)); + __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_13, 0, ((PyObject *)((PyObject*)(&PyString_Type)))); + __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type)))); + PyTuple_SET_ITEM(__pyx_t_13, 1, ((PyObject *)__pyx_t_14)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_14)); + __pyx_t_14 = 0; + __pyx_t_14 = PyObject_Call(__pyx_builtin_map, ((PyObject *)__pyx_t_13), NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2964; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_14); + __Pyx_DECREF(((PyObject *)__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 = 2964; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_13), NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2964; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_14); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_13)); __pyx_t_13 = 0; + __pyx_r = __pyx_t_14; + __pyx_t_14 = 0; + goto __pyx_L0; + + __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_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_XDECREF(__pyx_t_13); + __Pyx_XDECREF(__pyx_t_14); + __Pyx_AddTraceback("csamtools.IndelCall.__str__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2978 + * self.reads_diff ) ) ) + * + * def __dealloc__(self ): # <<<<<<<<<<<<<< + * bam_maqindel_ret_destroy(self._r) + * + */ + +static void __pyx_pf_9csamtools_9IndelCall_2__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_9IndelCall_2__dealloc__(PyObject *__pyx_v_self) { + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 2978); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2979 + * + * def __dealloc__(self ): + * bam_maqindel_ret_destroy(self._r) # <<<<<<<<<<<<<< + * + * cdef class IndelCallerBase: + */ + bam_maqindel_ret_destroy(((struct __pyx_obj_9csamtools_IndelCall *)__pyx_v_self)->_r); + + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3001 + * cdef int max_depth + * + * def __cinit__(self, # <<<<<<<<<<<<<< + * IteratorColumn iterator_column, + * **kwargs ): + */ + +static int __pyx_pf_9csamtools_15IndelCallerBase_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_15IndelCallerBase_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_iterator_column = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_t_3; + float __pyx_t_4; + int __pyx_t_5; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__iterator_column,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 3001); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__iterator_column); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3001; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)PyTuple_GET_ITEM(__pyx_args, 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 = 3001; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.IndelCallerBase.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_iterator_column), __pyx_ptype_9csamtools_IteratorColumn, 1, "iterator_column", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3002; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3006 + * + * + * self.iter = iterator_column # <<<<<<<<<<<<<< + * + * assert iterator_column.hasReference(), "IndelCallerBase requires an pileup iterator with reference sequence" + */ + __Pyx_INCREF(((PyObject *)__pyx_v_iterator_column)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_iterator_column)); + __Pyx_GOTREF(((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter); + __Pyx_DECREF(((PyObject *)((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter)); + ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter = __pyx_v_iterator_column; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3008 + * self.iter = iterator_column + * + * assert iterator_column.hasReference(), "IndelCallerBase requires an pileup iterator with reference sequence" # <<<<<<<<<<<<<< + * + * self.options = bam_maqindel_opt_init() + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_iterator_column), __pyx_n_s__hasReference); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3008; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3008; __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 = 3008; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (unlikely(!__pyx_t_3)) { + PyErr_SetObject(PyExc_AssertionError, ((PyObject *)__pyx_kp_s_166)); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3008; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3010 + * assert iterator_column.hasReference(), "IndelCallerBase requires an pileup iterator with reference sequence" + * + * self.options = bam_maqindel_opt_init() # <<<<<<<<<<<<<< + * + * # set the default parameterization according to + */ + ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->options = bam_maqindel_opt_init(); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3015 + * # samtools + * + * self.options.r_indel = kwargs.get( "r_indel", 0.00015 ) # <<<<<<<<<<<<<< + * self.options.q_indel = kwargs.get( "q_indel", 40 ) + * self.cap_mapQ = kwargs.get( "cap_mapQ", 60 ) + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3015; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_2 = PyFloat_FromDouble(0.00015); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3015; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__r_indel), __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3015; __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_PyFloat_AsDouble(__pyx_t_1); if (unlikely((__pyx_t_4 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3015; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->options->r_indel = __pyx_t_4; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3016 + * + * self.options.r_indel = kwargs.get( "r_indel", 0.00015 ) + * self.options.q_indel = kwargs.get( "q_indel", 40 ) # <<<<<<<<<<<<<< + * self.cap_mapQ = kwargs.get( "cap_mapQ", 60 ) + * self.max_depth = kwargs.get( "max_depth", 1024 ) + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3016; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__q_indel), __pyx_int_40); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3016; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_5 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3016; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->options->q_indel = __pyx_t_5; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3017 + * self.options.r_indel = kwargs.get( "r_indel", 0.00015 ) + * self.options.q_indel = kwargs.get( "q_indel", 40 ) + * self.cap_mapQ = kwargs.get( "cap_mapQ", 60 ) # <<<<<<<<<<<<<< + * self.max_depth = kwargs.get( "max_depth", 1024 ) + * + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__cap_mapQ), __pyx_int_60); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_5 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->cap_mapQ = __pyx_t_5; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3018 + * self.options.q_indel = kwargs.get( "q_indel", 40 ) + * self.cap_mapQ = kwargs.get( "cap_mapQ", 60 ) + * self.max_depth = kwargs.get( "max_depth", 1024 ) # <<<<<<<<<<<<<< + * + * def __dealloc__(self): + */ + if (unlikely(__pyx_v_kwargs == Py_None)) { + PyErr_SetString(PyExc_AttributeError, "'NoneType' object has no attribute 'get'"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3018; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_1 = __Pyx_PyDict_GetItemDefault(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s__max_depth), __pyx_int_1024); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3018; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_5 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3018; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->max_depth = __pyx_t_5; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("csamtools.IndelCallerBase.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3020 + * self.max_depth = kwargs.get( "max_depth", 1024 ) + * + * def __dealloc__(self): # <<<<<<<<<<<<<< + * free( self.options ) + * + */ + +static void __pyx_pf_9csamtools_15IndelCallerBase_1__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pf_9csamtools_15IndelCallerBase_1__dealloc__(PyObject *__pyx_v_self) { + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__dealloc__"); + __Pyx_TraceCall("__dealloc__", __pyx_f[0], 3020); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3021 + * + * def __dealloc__(self): + * free( self.options ) # <<<<<<<<<<<<<< + * + * def _call( self ): + */ + free(((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->options); + + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3023 + * free( self.options ) + * + * def _call( self ): # <<<<<<<<<<<<<< + * + * cdef char * seq = self.iter.getSequence() + */ + +static PyObject *__pyx_pf_9csamtools_15IndelCallerBase_2_call(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pf_9csamtools_15IndelCallerBase_2_call(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + char *__pyx_v_seq; + int __pyx_v_seq_len; + bam_maqindel_ret_t *__pyx_v_r; + int __pyx_v_m; + struct __pyx_obj_9csamtools_IndelCall *__pyx_v_call; + uint64_t __pyx_v_rms_aux; + int __pyx_v_i; + bam_pileup1_t *__pyx_v_p; + int __pyx_v_tmp; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + int __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_5 = NULL; + int __pyx_t_6; + int __pyx_t_7; + double __pyx_t_8; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("_call"); + __Pyx_TraceCall("_call", __pyx_f[0], 3023); + __pyx_v_call = ((struct __pyx_obj_9csamtools_IndelCall *)Py_None); __Pyx_INCREF(Py_None); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3025 + * def _call( self ): + * + * cdef char * seq = self.iter.getSequence() # <<<<<<<<<<<<<< + * cdef int seq_len = self.iter.seq_len + * + */ + __pyx_v_seq = ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->__pyx_vtab)->getSequence(((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3026 + * + * cdef char * seq = self.iter.getSequence() + * cdef int seq_len = self.iter.seq_len # <<<<<<<<<<<<<< + * + * assert seq != NULL + */ + __pyx_t_1 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter), __pyx_n_s__seq_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3026; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = __Pyx_PyInt_AsInt(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3026; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_v_seq_len = __pyx_t_2; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3028 + * cdef int seq_len = self.iter.seq_len + * + * assert seq != NULL # <<<<<<<<<<<<<< + * + * # reference base + */ + #ifndef CYTHON_WITHOUT_ASSERTIONS + if (unlikely(!(__pyx_v_seq != NULL))) { + PyErr_SetNone(PyExc_AssertionError); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3028; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + #endif + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3031 + * + * # reference base + * if self.iter.pos >= seq_len: # <<<<<<<<<<<<<< + * raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) + * + */ + __pyx_t_3 = (((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->pos >= __pyx_v_seq_len); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3032 + * # reference base + * if self.iter.pos >= seq_len: + * raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) # <<<<<<<<<<<<<< + * + * cdef bam_maqindel_ret_t * r + */ + __pyx_t_1 = PyInt_FromLong(((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_4 = PyInt_FromLong(__pyx_v_seq_len); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __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 = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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_t_1 = 0; + __pyx_t_4 = 0; + __pyx_t_4 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_161), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __Pyx_DECREF(((PyObject *)__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 = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_5)); + PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_4)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); + __pyx_t_4 = 0; + __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; + __Pyx_Raise(__pyx_t_4, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3036 + * cdef bam_maqindel_ret_t * r + * + * cdef int m = min( self.max_depth, self.iter.n_plp ) # <<<<<<<<<<<<<< + * + * # printf("pysam: m=%i, q_indel=%i, r_indel=%f, r_snp=%i, mm_penalty=%i, indel_err=%i, ambi_thres=%i\n", + */ + __pyx_t_2 = ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->n_plp; + __pyx_t_6 = ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->max_depth; + if ((__pyx_t_2 < __pyx_t_6)) { + __pyx_t_7 = __pyx_t_2; + } else { + __pyx_t_7 = __pyx_t_6; + } + __pyx_v_m = __pyx_t_7; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3048 + * seq, + * 0, + * NULL) # <<<<<<<<<<<<<< + * + * if r == NULL: return None + */ + __pyx_v_r = bam_maqindel(__pyx_v_m, ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->pos, ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->options, ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->plp, __pyx_v_seq, 0, NULL); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3050 + * NULL) + * + * if r == NULL: return None # <<<<<<<<<<<<<< + * + * cdef IndelCall call + */ + __pyx_t_3 = (__pyx_v_r == NULL); + if (__pyx_t_3) { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(Py_None); + __pyx_r = Py_None; + goto __pyx_L0; + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3053 + * + * cdef IndelCall call + * call = IndelCall() # <<<<<<<<<<<<<< + * call._r = r + * call._tid = self.iter.tid + */ + __pyx_t_4 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_9csamtools_IndelCall)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3053; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__pyx_v_call)); + __pyx_v_call = ((struct __pyx_obj_9csamtools_IndelCall *)__pyx_t_4); + __pyx_t_4 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3054 + * cdef IndelCall call + * call = IndelCall() + * call._r = r # <<<<<<<<<<<<<< + * call._tid = self.iter.tid + * call._pos = self.iter.pos + */ + __pyx_v_call->_r = __pyx_v_r; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3055 + * call = IndelCall() + * call._r = r + * call._tid = self.iter.tid # <<<<<<<<<<<<<< + * call._pos = self.iter.pos + * call._coverage = self.iter.n_plp + */ + __pyx_v_call->_tid = ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->tid; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3056 + * call._r = r + * call._tid = self.iter.tid + * call._pos = self.iter.pos # <<<<<<<<<<<<<< + * call._coverage = self.iter.n_plp + * + */ + __pyx_v_call->_pos = ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->pos; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3057 + * call._tid = self.iter.tid + * call._pos = self.iter.pos + * call._coverage = self.iter.n_plp # <<<<<<<<<<<<<< + * + * cdef uint64_t rms_aux = 0 + */ + __pyx_v_call->_coverage = ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->n_plp; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3059 + * call._coverage = self.iter.n_plp + * + * cdef uint64_t rms_aux = 0 # <<<<<<<<<<<<<< + * cdef int i = 0 + * cdef bam_pileup1_t * p + */ + __pyx_v_rms_aux = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3060 + * + * cdef uint64_t rms_aux = 0 + * cdef int i = 0 # <<<<<<<<<<<<<< + * cdef bam_pileup1_t * p + * cdef int tmp + */ + __pyx_v_i = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3064 + * cdef int tmp + * + * for i from 0 <= i < self.iter.n_plp: # <<<<<<<<<<<<<< + * p = self.iter.plp + i + * if p.b.core.qual < self.cap_mapQ: + */ + __pyx_t_7 = ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->n_plp; + for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_7; __pyx_v_i++) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3065 + * + * for i from 0 <= i < self.iter.n_plp: + * p = self.iter.plp + i # <<<<<<<<<<<<<< + * if p.b.core.qual < self.cap_mapQ: + * tmp = p.b.core.qual + */ + __pyx_v_p = (((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->plp + __pyx_v_i); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3066 + * for i from 0 <= i < self.iter.n_plp: + * p = self.iter.plp + i + * if p.b.core.qual < self.cap_mapQ: # <<<<<<<<<<<<<< + * tmp = p.b.core.qual + * else: + */ + __pyx_t_3 = (__pyx_v_p->b->core.qual < ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->cap_mapQ); + if (__pyx_t_3) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3067 + * p = self.iter.plp + i + * if p.b.core.qual < self.cap_mapQ: + * tmp = p.b.core.qual # <<<<<<<<<<<<<< + * else: + * tmp = self.cap_mapQ + */ + __pyx_v_tmp = __pyx_v_p->b->core.qual; + goto __pyx_L9; + } + /*else*/ { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3069 + * tmp = p.b.core.qual + * else: + * tmp = self.cap_mapQ # <<<<<<<<<<<<<< + * rms_aux += tmp * tmp + * + */ + __pyx_v_tmp = ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->cap_mapQ; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3070 + * else: + * tmp = self.cap_mapQ + * rms_aux += tmp * tmp # <<<<<<<<<<<<<< + * + * call._rms_mapping_quality = (sqrt(rms_aux / self.iter.n_plp) + .499) + */ + __pyx_v_rms_aux = (__pyx_v_rms_aux + (__pyx_v_tmp * __pyx_v_tmp)); + } + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3072 + * rms_aux += tmp * tmp + * + * call._rms_mapping_quality = (sqrt(rms_aux / self.iter.n_plp) + .499) # <<<<<<<<<<<<<< + * + * return call + */ + __pyx_t_8 = ((double)__pyx_v_rms_aux); + if (unlikely(((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->n_plp == 0)) { + PyErr_Format(PyExc_ZeroDivisionError, "float division"); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3072; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_v_call->_rms_mapping_quality = ((uint64_t)(sqrt((__pyx_t_8 / ((struct __pyx_obj_9csamtools_IndelCallerBase *)__pyx_v_self)->iter->n_plp)) + .499)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3074 + * call._rms_mapping_quality = (sqrt(rms_aux / self.iter.n_plp) + .499) + * + * return call # <<<<<<<<<<<<<< + * + * cdef class IndelCaller( IndelCallerBase ): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_call)); + __pyx_r = ((PyObject *)__pyx_v_call); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_5); + __Pyx_AddTraceback("csamtools.IndelCallerBase._call"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF((PyObject *)__pyx_v_call); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3089 + * ''' + * + * def __cinit__(self, # <<<<<<<<<<<<<< + * IteratorColumn iterator_column, + * **kwargs ): + */ + +static int __pyx_pf_9csamtools_11IndelCaller_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_11IndelCaller_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_iterator_column = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__iterator_column,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 3089); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__iterator_column); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3089; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)PyTuple_GET_ITEM(__pyx_args, 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 = 3089; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.IndelCaller.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_iterator_column), __pyx_ptype_9csamtools_IteratorColumn, 1, "iterator_column", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3090; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.IndelCaller.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3095 + * pass + * + * def call(self, reference, int pos ): # <<<<<<<<<<<<<< + * """call a snp on chromosome *reference* + * and position *pos*. + */ + +static PyObject *__pyx_pf_9csamtools_11IndelCaller_1call(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_9csamtools_11IndelCaller_1call[] = "call a snp on chromosome *reference*\n and position *pos*.\n\n returns a :class:`SNPCall` object or None, if no indel call could be made.\n "; +static PyObject *__pyx_pf_9csamtools_11IndelCaller_1call(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_reference = 0; + int __pyx_v_pos; + int __pyx_v_tid; + PyObject *__pyx_r = NULL; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + PyObject *__pyx_t_5 = NULL; + int __pyx_t_6; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__reference,&__pyx_n_s__pos,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("call"); + __Pyx_TraceCall("call", __pyx_f[0], 3095); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[2] = {0,0}; + 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); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__reference); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__pos); + if (likely(values[1])) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("call", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3095; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "call") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3095; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_reference = values[0]; + __pyx_v_pos = __Pyx_PyInt_AsInt(values[1]); if (unlikely((__pyx_v_pos == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3095; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_reference = PyTuple_GET_ITEM(__pyx_args, 0); + __pyx_v_pos = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 1)); if (unlikely((__pyx_v_pos == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3095; __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 = 3095; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("csamtools.IndelCaller.call"); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3102 + * """ + * + * cdef int tid = self.iter.samfile.gettid( reference ) # <<<<<<<<<<<<<< + * + * self.iter.reset( tid, pos, pos + 1 ) + */ + __pyx_t_1 = PyObject_GetAttr(((PyObject *)((struct __pyx_obj_9csamtools_IndelCaller *)__pyx_v_self)->__pyx_base.iter->samfile), __pyx_n_s__gettid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3102; __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 = 3102; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__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 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3102; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __pyx_t_4 = __Pyx_PyInt_AsInt(__pyx_t_3); if (unlikely((__pyx_t_4 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3102; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_v_tid = __pyx_t_4; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3104 + * cdef int tid = self.iter.samfile.gettid( reference ) + * + * self.iter.reset( tid, pos, pos + 1 ) # <<<<<<<<<<<<<< + * + * while 1: + */ + __pyx_t_3 = PyInt_FromLong(__pyx_v_tid); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3104; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_2 = PyInt_FromLong(__pyx_v_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3104; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyInt_FromLong((__pyx_v_pos + 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3104; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_5 = ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_IndelCaller *)__pyx_v_self)->__pyx_base.iter->__pyx_vtab)->reset(((struct __pyx_obj_9csamtools_IndelCaller *)__pyx_v_self)->__pyx_base.iter, __pyx_t_3, __pyx_t_2, __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3104; __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_2); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3106 + * self.iter.reset( tid, pos, pos + 1 ) + * + * while 1: # <<<<<<<<<<<<<< + * self.iter.cnext() + * + */ + while (1) { + if (!1) break; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3107 + * + * while 1: + * self.iter.cnext() # <<<<<<<<<<<<<< + * + * if self.iter.n_plp < 0: + */ + ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_IndelCaller *)__pyx_v_self)->__pyx_base.iter->__pyx_vtab)->cnext(((struct __pyx_obj_9csamtools_IndelCaller *)__pyx_v_self)->__pyx_base.iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3109 + * self.iter.cnext() + * + * if self.iter.n_plp < 0: # <<<<<<<<<<<<<< + * raise ValueError("error during iteration" ) + * + */ + __pyx_t_6 = (((struct __pyx_obj_9csamtools_IndelCaller *)__pyx_v_self)->__pyx_base.iter->n_plp < 0); + if (__pyx_t_6) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3110 + * + * if self.iter.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.iter.plp == NULL: + */ + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_167), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3110; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3110; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L8; + } + __pyx_L8:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3112 + * raise ValueError("error during iteration" ) + * + * if self.iter.plp == NULL: # <<<<<<<<<<<<<< + * raise ValueError( "no reads in region - no call" ) + * + */ + __pyx_t_6 = (((struct __pyx_obj_9csamtools_IndelCaller *)__pyx_v_self)->__pyx_base.iter->plp == NULL); + if (__pyx_t_6) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3113 + * + * if self.iter.plp == NULL: + * raise ValueError( "no reads in region - no call" ) # <<<<<<<<<<<<<< + * + * if self.iter.pos == pos: break + */ + __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_168), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3113; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __Pyx_Raise(__pyx_t_5, 0, 0); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3113; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L9; + } + __pyx_L9:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3115 + * raise ValueError( "no reads in region - no call" ) + * + * if self.iter.pos == pos: break # <<<<<<<<<<<<<< + * + * return self._call() + */ + __pyx_t_6 = (((struct __pyx_obj_9csamtools_IndelCaller *)__pyx_v_self)->__pyx_base.iter->pos == __pyx_v_pos); + if (__pyx_t_6) { + goto __pyx_L7_break; + goto __pyx_L10; + } + __pyx_L10:; + } + __pyx_L7_break:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3117 + * if self.iter.pos == pos: break + * + * return self._call() # <<<<<<<<<<<<<< + * + * cdef class IteratorIndelCalls( IndelCallerBase ): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_5 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___call); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3117; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_1 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3117; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + __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_5); + __Pyx_AddTraceback("csamtools.IndelCaller.call"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3133 + * """ + * + * def __cinit__(self, # <<<<<<<<<<<<<< + * IteratorColumn iterator_column, + * **kwargs ): + */ + +static int __pyx_pf_9csamtools_18IteratorIndelCalls_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pf_9csamtools_18IteratorIndelCalls_0__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + struct __pyx_obj_9csamtools_IteratorColumn *__pyx_v_iterator_column = 0; + PyObject *__pyx_v_kwargs = 0; + int __pyx_r; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__iterator_column,0}; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__cinit__"); + __Pyx_TraceCall("__cinit__", __pyx_f[0], 3133); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1; + __Pyx_GOTREF(__pyx_v_kwargs); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); + PyObject* values[1] = {0}; + 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; + } + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__iterator_column); + if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)values[0]); + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + __pyx_v_iterator_column = ((struct __pyx_obj_9csamtools_IteratorColumn *)PyTuple_GET_ITEM(__pyx_args, 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 = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_AddTraceback("csamtools.IteratorIndelCalls.__cinit__"); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_iterator_column), __pyx_ptype_9csamtools_IteratorColumn, 1, "iterator_column", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3134; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("csamtools.IteratorIndelCalls.__cinit__"); + __pyx_r = -1; + __pyx_L0:; + __Pyx_DECREF(__pyx_v_kwargs); + __Pyx_TraceReturn(Py_None); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3139 + * + * + * def __iter__(self): # <<<<<<<<<<<<<< + * return self + * + */ + +static PyObject *__pyx_pf_9csamtools_18IteratorIndelCalls_1__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pf_9csamtools_18IteratorIndelCalls_1__iter__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__iter__"); + __Pyx_TraceCall("__iter__", __pyx_f[0], 3139); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3140 + * + * 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; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3142 + * return self + * + * def __next__(self): # <<<<<<<<<<<<<< + * """python version of next(). + * """ + */ + +static PyObject *__pyx_pf_9csamtools_18IteratorIndelCalls_2__next__(PyObject *__pyx_v_self); /*proto*/ +static char __pyx_doc_9csamtools_18IteratorIndelCalls_2__next__[] = "python version of next().\n "; +struct wrapperbase __pyx_wrapperbase_9csamtools_18IteratorIndelCalls_2__next__; +static PyObject *__pyx_pf_9csamtools_18IteratorIndelCalls_2__next__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = NULL; + int __pyx_t_1; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + __Pyx_TraceDeclarations + __Pyx_RefNannySetupContext("__next__"); + __Pyx_TraceCall("__next__", __pyx_f[0], 3142); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3147 + * + * # the following code was adapted from bam_plcmd.c:pileup_func() + * self.iter.cnext() # <<<<<<<<<<<<<< + * + * if self.iter.n_plp < 0: + */ + ((struct __pyx_vtabstruct_9csamtools_IteratorColumn *)((struct __pyx_obj_9csamtools_IteratorIndelCalls *)__pyx_v_self)->__pyx_base.iter->__pyx_vtab)->cnext(((struct __pyx_obj_9csamtools_IteratorIndelCalls *)__pyx_v_self)->__pyx_base.iter); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3149 + * self.iter.cnext() + * + * if self.iter.n_plp < 0: # <<<<<<<<<<<<<< + * raise ValueError("error during iteration" ) + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorIndelCalls *)__pyx_v_self)->__pyx_base.iter->n_plp < 0); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3150 + * + * if self.iter.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.iter.plp == NULL: + */ + __pyx_t_2 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_169), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3150; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_Raise(__pyx_t_2, 0, 0); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3150; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3152 + * raise ValueError("error during iteration" ) + * + * if self.iter.plp == NULL: # <<<<<<<<<<<<<< + * raise StopIteration + * + */ + __pyx_t_1 = (((struct __pyx_obj_9csamtools_IteratorIndelCalls *)__pyx_v_self)->__pyx_base.iter->plp == NULL); + if (__pyx_t_1) { + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3153 + * + * if self.iter.plp == NULL: + * raise StopIteration # <<<<<<<<<<<<<< + * + * return self._call() + */ + __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3153; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L6; + } + __pyx_L6:; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3155 + * raise StopIteration + * + * return self._call() # <<<<<<<<<<<<<< + * + * __all__ = ["Samfile", + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s___call); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3155; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3155; __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; + + __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_AddTraceback("csamtools.IteratorIndelCalls.__next__"); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_TraceReturn(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static PyObject *__pyx_tp_new_9csamtools_AlignedRead(PyTypeObject *t, PyObject *a, PyObject *k) { + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + return o; +} + +static void __pyx_tp_dealloc_9csamtools_AlignedRead(PyObject *o) { + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_11AlignedRead_1__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + (*Py_TYPE(o)->tp_free)(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_qname(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_5qname_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_qname(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_5qname_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_cigar(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_5cigar_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_cigar(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_5cigar_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_seq(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_3seq_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_seq(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_3seq_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_qual(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4qual_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_qual(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_4qual_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_query(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_5query_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_qqual(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_5qqual_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_qstart(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_6qstart_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_qend(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4qend_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_qlen(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4qlen_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_tags(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4tags_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_tags(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_4tags_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_flag(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4flag_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_flag(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_4flag_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_rname(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_5rname_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_rname(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_5rname_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_tid(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_3tid_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_tid(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_3tid_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_pos(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_3pos_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_pos(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_3pos_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_bin(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_3bin_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_bin(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_3bin_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_rlen(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4rlen_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_aend(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4aend_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_alen(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4alen_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_mapq(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4mapq_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_mapq(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_4mapq_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_mrnm(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4mrnm_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_mrnm(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_4mrnm_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_mpos(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_4mpos_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_mpos(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_4mpos_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_isize(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_5isize_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_isize(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_5isize_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_paired(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_9is_paired_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_paired(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_9is_paired_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_proper_pair(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_14is_proper_pair_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_proper_pair(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_14is_proper_pair_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_unmapped(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_11is_unmapped_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_unmapped(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_11is_unmapped_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_mate_is_unmapped(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_16mate_is_unmapped_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_mate_is_unmapped(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_16mate_is_unmapped_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_reverse(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_10is_reverse_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_reverse(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_10is_reverse_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_mate_is_reverse(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_15mate_is_reverse_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_mate_is_reverse(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_15mate_is_reverse_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_read1(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_8is_read1_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_read1(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_8is_read1_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_read2(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_8is_read2_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_read2(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_8is_read2_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_secondary(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_12is_secondary_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_secondary(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_12is_secondary_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_qcfail(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_9is_qcfail_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_qcfail(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_9is_qcfail_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11AlignedRead_is_duplicate(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11AlignedRead_12is_duplicate_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11AlignedRead_is_duplicate(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11AlignedRead_12is_duplicate_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyMethodDef __pyx_methods_9csamtools_AlignedRead[] = { + {__Pyx_NAMESTR("compare"), (PyCFunction)__pyx_pf_9csamtools_11AlignedRead_3compare, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_11AlignedRead_3compare)}, + {__Pyx_NAMESTR("opt"), (PyCFunction)__pyx_pf_9csamtools_11AlignedRead_5opt, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_11AlignedRead_5opt)}, + {__Pyx_NAMESTR("fancy_str"), (PyCFunction)__pyx_pf_9csamtools_11AlignedRead_6fancy_str, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_9csamtools_11AlignedRead_6fancy_str)}, + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_9csamtools_AlignedRead[] = { + {(char *)"qname", __pyx_getprop_9csamtools_11AlignedRead_qname, __pyx_setprop_9csamtools_11AlignedRead_qname, __Pyx_DOCSTR(__pyx_k_170), 0}, + {(char *)"cigar", __pyx_getprop_9csamtools_11AlignedRead_cigar, __pyx_setprop_9csamtools_11AlignedRead_cigar, __Pyx_DOCSTR(__pyx_k_171), 0}, + {(char *)"seq", __pyx_getprop_9csamtools_11AlignedRead_seq, __pyx_setprop_9csamtools_11AlignedRead_seq, __Pyx_DOCSTR(__pyx_k_172), 0}, + {(char *)"qual", __pyx_getprop_9csamtools_11AlignedRead_qual, __pyx_setprop_9csamtools_11AlignedRead_qual, __Pyx_DOCSTR(__pyx_k_173), 0}, + {(char *)"query", __pyx_getprop_9csamtools_11AlignedRead_query, 0, __Pyx_DOCSTR(__pyx_k_174), 0}, + {(char *)"qqual", __pyx_getprop_9csamtools_11AlignedRead_qqual, 0, __Pyx_DOCSTR(__pyx_k_175), 0}, + {(char *)"qstart", __pyx_getprop_9csamtools_11AlignedRead_qstart, 0, __Pyx_DOCSTR(__pyx_k_176), 0}, + {(char *)"qend", __pyx_getprop_9csamtools_11AlignedRead_qend, 0, __Pyx_DOCSTR(__pyx_k_177), 0}, + {(char *)"qlen", __pyx_getprop_9csamtools_11AlignedRead_qlen, 0, __Pyx_DOCSTR(__pyx_k_178), 0}, + {(char *)"tags", __pyx_getprop_9csamtools_11AlignedRead_tags, __pyx_setprop_9csamtools_11AlignedRead_tags, __Pyx_DOCSTR(__pyx_k_179), 0}, + {(char *)"flag", __pyx_getprop_9csamtools_11AlignedRead_flag, __pyx_setprop_9csamtools_11AlignedRead_flag, __Pyx_DOCSTR(__pyx_k_180), 0}, + {(char *)"rname", __pyx_getprop_9csamtools_11AlignedRead_rname, __pyx_setprop_9csamtools_11AlignedRead_rname, __Pyx_DOCSTR(__pyx_k_181), 0}, + {(char *)"tid", __pyx_getprop_9csamtools_11AlignedRead_tid, __pyx_setprop_9csamtools_11AlignedRead_tid, __Pyx_DOCSTR(__pyx_k_182), 0}, + {(char *)"pos", __pyx_getprop_9csamtools_11AlignedRead_pos, __pyx_setprop_9csamtools_11AlignedRead_pos, __Pyx_DOCSTR(__pyx_k_183), 0}, + {(char *)"bin", __pyx_getprop_9csamtools_11AlignedRead_bin, __pyx_setprop_9csamtools_11AlignedRead_bin, __Pyx_DOCSTR(__pyx_k_184), 0}, + {(char *)"rlen", __pyx_getprop_9csamtools_11AlignedRead_rlen, 0, __Pyx_DOCSTR(__pyx_k_185), 0}, + {(char *)"aend", __pyx_getprop_9csamtools_11AlignedRead_aend, 0, __Pyx_DOCSTR(__pyx_k_186), 0}, + {(char *)"alen", __pyx_getprop_9csamtools_11AlignedRead_alen, 0, __Pyx_DOCSTR(__pyx_k_187), 0}, + {(char *)"mapq", __pyx_getprop_9csamtools_11AlignedRead_mapq, __pyx_setprop_9csamtools_11AlignedRead_mapq, __Pyx_DOCSTR(__pyx_k_188), 0}, + {(char *)"mrnm", __pyx_getprop_9csamtools_11AlignedRead_mrnm, __pyx_setprop_9csamtools_11AlignedRead_mrnm, __Pyx_DOCSTR(__pyx_k_189), 0}, + {(char *)"mpos", __pyx_getprop_9csamtools_11AlignedRead_mpos, __pyx_setprop_9csamtools_11AlignedRead_mpos, __Pyx_DOCSTR(__pyx_k_190), 0}, + {(char *)"isize", __pyx_getprop_9csamtools_11AlignedRead_isize, __pyx_setprop_9csamtools_11AlignedRead_isize, __Pyx_DOCSTR(__pyx_k_191), 0}, + {(char *)"is_paired", __pyx_getprop_9csamtools_11AlignedRead_is_paired, __pyx_setprop_9csamtools_11AlignedRead_is_paired, __Pyx_DOCSTR(__pyx_k_192), 0}, + {(char *)"is_proper_pair", __pyx_getprop_9csamtools_11AlignedRead_is_proper_pair, __pyx_setprop_9csamtools_11AlignedRead_is_proper_pair, __Pyx_DOCSTR(__pyx_k_193), 0}, + {(char *)"is_unmapped", __pyx_getprop_9csamtools_11AlignedRead_is_unmapped, __pyx_setprop_9csamtools_11AlignedRead_is_unmapped, __Pyx_DOCSTR(__pyx_k_194), 0}, + {(char *)"mate_is_unmapped", __pyx_getprop_9csamtools_11AlignedRead_mate_is_unmapped, __pyx_setprop_9csamtools_11AlignedRead_mate_is_unmapped, __Pyx_DOCSTR(__pyx_k_195), 0}, + {(char *)"is_reverse", __pyx_getprop_9csamtools_11AlignedRead_is_reverse, __pyx_setprop_9csamtools_11AlignedRead_is_reverse, __Pyx_DOCSTR(__pyx_k_196), 0}, + {(char *)"mate_is_reverse", __pyx_getprop_9csamtools_11AlignedRead_mate_is_reverse, __pyx_setprop_9csamtools_11AlignedRead_mate_is_reverse, __Pyx_DOCSTR(__pyx_k_197), 0}, + {(char *)"is_read1", __pyx_getprop_9csamtools_11AlignedRead_is_read1, __pyx_setprop_9csamtools_11AlignedRead_is_read1, __Pyx_DOCSTR(__pyx_k_198), 0}, + {(char *)"is_read2", __pyx_getprop_9csamtools_11AlignedRead_is_read2, __pyx_setprop_9csamtools_11AlignedRead_is_read2, __Pyx_DOCSTR(__pyx_k_199), 0}, + {(char *)"is_secondary", __pyx_getprop_9csamtools_11AlignedRead_is_secondary, __pyx_setprop_9csamtools_11AlignedRead_is_secondary, __Pyx_DOCSTR(__pyx_k_200), 0}, + {(char *)"is_qcfail", __pyx_getprop_9csamtools_11AlignedRead_is_qcfail, __pyx_setprop_9csamtools_11AlignedRead_is_qcfail, __Pyx_DOCSTR(__pyx_k_201), 0}, + {(char *)"is_duplicate", __pyx_getprop_9csamtools_11AlignedRead_is_duplicate, __pyx_setprop_9csamtools_11AlignedRead_is_duplicate, __Pyx_DOCSTR(__pyx_k_202), 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_AlignedRead = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_AlignedRead = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_AlignedRead = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_AlignedRead = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_AlignedRead = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.AlignedRead"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_AlignedRead), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_AlignedRead, /*tp_as_number*/ + &__pyx_tp_as_sequence_AlignedRead, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_AlignedRead, /*tp_as_mapping*/ + __pyx_pf_9csamtools_11AlignedRead_4__hash__, /*tp_hash*/ + 0, /*tp_call*/ + __pyx_pf_9csamtools_11AlignedRead_2__str__, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_AlignedRead, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/ + __Pyx_DOCSTR("\n Class representing an aligned read. see SAM format specification for 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 in-place. This is\n not the most efficient way to build BAM entries, as the variable\n length data is concatenated and thus needs to resized if\n a field is updated. Furthermore, the BAM entry might be\n in an inconsistent state. The :meth:`~validate` method can\n be used to check if an entry is consistent.\n\n One issue to look out for is that the sequence should always\n be set *before* the quality scores. Setting the sequence will\n also erase any quality scores that were set previously.\n "), /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_AlignedRead, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_9csamtools_AlignedRead, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + __pyx_pf_9csamtools_11AlignedRead_0__init__, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_9csamtools_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 +}; + +static PyObject *__pyx_tp_new_9csamtools_PileupProxy(PyTypeObject *t, PyObject *a, PyObject *k) { + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + return o; +} + +static void __pyx_tp_dealloc_9csamtools_PileupProxy(PyObject *o) { + (*Py_TYPE(o)->tp_free)(o); +} + +static PyObject *__pyx_getprop_9csamtools_11PileupProxy_tid(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11PileupProxy_3tid_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11PileupProxy_n(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11PileupProxy_1n_0__get__(o); +} + +static int __pyx_setprop_9csamtools_11PileupProxy_n(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pf_9csamtools_11PileupProxy_1n_1__set__(o, v); + } + else { + PyErr_SetString(PyExc_NotImplementedError, "__del__"); + return -1; + } +} + +static PyObject *__pyx_getprop_9csamtools_11PileupProxy_pos(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11PileupProxy_3pos_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_11PileupProxy_pileups(PyObject *o, void *x) { + return __pyx_pf_9csamtools_11PileupProxy_7pileups_0__get__(o); +} + +static PyMethodDef __pyx_methods_9csamtools_PileupProxy[] = { + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_9csamtools_PileupProxy[] = { + {(char *)"tid", __pyx_getprop_9csamtools_11PileupProxy_tid, 0, __Pyx_DOCSTR(__pyx_k_203), 0}, + {(char *)"n", __pyx_getprop_9csamtools_11PileupProxy_n, __pyx_setprop_9csamtools_11PileupProxy_n, __Pyx_DOCSTR(__pyx_k_204), 0}, + {(char *)"pos", __pyx_getprop_9csamtools_11PileupProxy_pos, 0, 0, 0}, + {(char *)"pileups", __pyx_getprop_9csamtools_11PileupProxy_pileups, 0, __Pyx_DOCSTR(__pyx_k_205), 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_PileupProxy = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_PileupProxy = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_PileupProxy = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_PileupProxy = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_PileupProxy = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.PileupProxy"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_PileupProxy), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_PileupProxy, /*tp_as_number*/ + &__pyx_tp_as_sequence_PileupProxy, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_PileupProxy, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + __pyx_pf_9csamtools_11PileupProxy_1__str__, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_PileupProxy, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/ + __Pyx_DOCSTR("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\n This class is a proxy for results returned by the samtools pileup engine.\n If the underlying engine iterator advances, the results of this column\n will change.\n "), /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_PileupProxy, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_9csamtools_PileupProxy, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + __pyx_pf_9csamtools_11PileupProxy_0__init__, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_9csamtools_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 +}; + +static PyObject *__pyx_tp_new_9csamtools_PileupRead(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_PileupRead *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_PileupRead *)o); + p->_alignment = ((struct __pyx_obj_9csamtools_AlignedRead *)Py_None); Py_INCREF(Py_None); + return o; +} + +static void __pyx_tp_dealloc_9csamtools_PileupRead(PyObject *o) { + struct __pyx_obj_9csamtools_PileupRead *p = (struct __pyx_obj_9csamtools_PileupRead *)o; + Py_XDECREF(((PyObject *)p->_alignment)); + (*Py_TYPE(o)->tp_free)(o); +} + +static int __pyx_tp_traverse_9csamtools_PileupRead(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_9csamtools_PileupRead *p = (struct __pyx_obj_9csamtools_PileupRead *)o; + if (p->_alignment) { + e = (*v)(((PyObject*)p->_alignment), a); if (e) return e; + } + return 0; +} + +static int __pyx_tp_clear_9csamtools_PileupRead(PyObject *o) { + struct __pyx_obj_9csamtools_PileupRead *p = (struct __pyx_obj_9csamtools_PileupRead *)o; + PyObject* tmp; + tmp = ((PyObject*)p->_alignment); + p->_alignment = ((struct __pyx_obj_9csamtools_AlignedRead *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + return 0; +} + +static PyObject *__pyx_getprop_9csamtools_10PileupRead_alignment(PyObject *o, void *x) { + return __pyx_pf_9csamtools_10PileupRead_9alignment_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_10PileupRead_qpos(PyObject *o, void *x) { + return __pyx_pf_9csamtools_10PileupRead_4qpos_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_10PileupRead_indel(PyObject *o, void *x) { + return __pyx_pf_9csamtools_10PileupRead_5indel_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_10PileupRead_is_del(PyObject *o, void *x) { + return __pyx_pf_9csamtools_10PileupRead_6is_del_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_10PileupRead_is_head(PyObject *o, void *x) { + return __pyx_pf_9csamtools_10PileupRead_7is_head_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_10PileupRead_is_tail(PyObject *o, void *x) { + return __pyx_pf_9csamtools_10PileupRead_7is_tail_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_10PileupRead_level(PyObject *o, void *x) { + return __pyx_pf_9csamtools_10PileupRead_5level_0__get__(o); +} + +static PyMethodDef __pyx_methods_9csamtools_PileupRead[] = { + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_9csamtools_PileupRead[] = { + {(char *)"alignment", __pyx_getprop_9csamtools_10PileupRead_alignment, 0, __Pyx_DOCSTR(__pyx_k_206), 0}, + {(char *)"qpos", __pyx_getprop_9csamtools_10PileupRead_qpos, 0, __Pyx_DOCSTR(__pyx_k_207), 0}, + {(char *)"indel", __pyx_getprop_9csamtools_10PileupRead_indel, 0, __Pyx_DOCSTR(__pyx_k_208), 0}, + {(char *)"is_del", __pyx_getprop_9csamtools_10PileupRead_is_del, 0, __Pyx_DOCSTR(__pyx_k_209), 0}, + {(char *)"is_head", __pyx_getprop_9csamtools_10PileupRead_is_head, 0, 0, 0}, + {(char *)"is_tail", __pyx_getprop_9csamtools_10PileupRead_is_tail, 0, 0, 0}, + {(char *)"level", __pyx_getprop_9csamtools_10PileupRead_level, 0, 0, 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_PileupRead = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_PileupRead = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_PileupRead = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_PileupRead = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_PileupRead = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.PileupRead"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_PileupRead), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_PileupRead, /*tp_as_number*/ + &__pyx_tp_as_sequence_PileupRead, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_PileupRead, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + __pyx_pf_9csamtools_10PileupRead_1__str__, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_PileupRead, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("A read aligned to a column.\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_PileupRead, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_PileupRead, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_PileupRead, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_9csamtools_PileupRead, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + __pyx_pf_9csamtools_10PileupRead_0__init__, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_9csamtools_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 +}; +static struct __pyx_vtabstruct_9csamtools_Fastafile __pyx_vtable_9csamtools_Fastafile; + +static PyObject *__pyx_tp_new_9csamtools_Fastafile(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_Fastafile *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_Fastafile *)o); + p->__pyx_vtab = __pyx_vtabptr_9csamtools_Fastafile; + if (__pyx_pf_9csamtools_9Fastafile_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_Fastafile(PyObject *o) { + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_9Fastafile_5__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + (*Py_TYPE(o)->tp_free)(o); +} + +static PyObject *__pyx_getprop_9csamtools_9Fastafile_filename(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9Fastafile_8filename_0__get__(o); +} + +static PyMethodDef __pyx_methods_9csamtools_Fastafile[] = { + {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pf_9csamtools_9Fastafile_1_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_9csamtools_9Fastafile_1_isOpen)}, + {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pf_9csamtools_9Fastafile_3_open, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_9Fastafile_3_open)}, + {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pf_9csamtools_9Fastafile_4close, METH_NOARGS, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pf_9csamtools_9Fastafile_6fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_9Fastafile_6fetch)}, + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_9csamtools_Fastafile[] = { + {(char *)"filename", __pyx_getprop_9csamtools_9Fastafile_filename, 0, __Pyx_DOCSTR(__pyx_k_210), 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_Fastafile = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_Fastafile = { + __pyx_pf_9csamtools_9Fastafile_2__len__, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_Fastafile = { + __pyx_pf_9csamtools_9Fastafile_2__len__, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_Fastafile = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_Fastafile = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.Fastafile"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_Fastafile), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_Fastafile, /*tp_as_number*/ + &__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*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_Fastafile, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*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*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_Fastafile, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_9csamtools_Fastafile, /*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_9csamtools_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 +}; +static struct __pyx_vtabstruct_9csamtools_Samfile __pyx_vtable_9csamtools_Samfile; + +static PyObject *__pyx_tp_new_9csamtools_Samfile(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_Samfile *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_Samfile *)o); + p->__pyx_vtab = __pyx_vtabptr_9csamtools_Samfile; + if (__pyx_pf_9csamtools_7Samfile_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_Samfile(PyObject *o) { + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_7Samfile_14__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + (*Py_TYPE(o)->tp_free)(o); +} + +static PyObject *__pyx_getprop_9csamtools_7Samfile_filename(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7Samfile_8filename_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7Samfile_nreferences(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7Samfile_11nreferences_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7Samfile_references(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7Samfile_10references_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7Samfile_lengths(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7Samfile_7lengths_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7Samfile_text(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7Samfile_4text_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7Samfile_header(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7Samfile_6header_0__get__(o); +} + +static PyMethodDef __pyx_methods_9csamtools_Samfile[] = { + {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_1_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_1_isOpen)}, + {__Pyx_NAMESTR("_hasIndex"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_2_hasIndex, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_2_hasIndex)}, + {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_3_open, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_3_open)}, + {__Pyx_NAMESTR("getrname"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_4getrname, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_4getrname)}, + {__Pyx_NAMESTR("gettid"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_5gettid, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_5gettid)}, + {__Pyx_NAMESTR("_parseRegion"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_6_parseRegion, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_6_parseRegion)}, + {__Pyx_NAMESTR("seek"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_7seek, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_7seek)}, + {__Pyx_NAMESTR("tell"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_8tell, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_8tell)}, + {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_9fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_9fetch)}, + {__Pyx_NAMESTR("mate"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_10mate, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_10mate)}, + {__Pyx_NAMESTR("count"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_11count, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_11count)}, + {__Pyx_NAMESTR("pileup"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_12pileup, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_12pileup)}, + {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_13close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_13close)}, + {__Pyx_NAMESTR("write"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_15write, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_15write)}, + {__Pyx_NAMESTR("__enter__"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_16__enter__, METH_NOARGS, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("__exit__"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_17__exit__, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("_buildLine"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_18_buildLine, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_18_buildLine)}, + {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pf_9csamtools_7Samfile_20__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_9csamtools_7Samfile_20__next__)}, + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_9csamtools_Samfile[] = { + {(char *)"filename", __pyx_getprop_9csamtools_7Samfile_filename, 0, __Pyx_DOCSTR(__pyx_k_210), 0}, + {(char *)"nreferences", __pyx_getprop_9csamtools_7Samfile_nreferences, 0, __Pyx_DOCSTR(__pyx_k_211), 0}, + {(char *)"references", __pyx_getprop_9csamtools_7Samfile_references, 0, __Pyx_DOCSTR(__pyx_k_212), 0}, + {(char *)"lengths", __pyx_getprop_9csamtools_7Samfile_lengths, 0, __Pyx_DOCSTR(__pyx_k_213), 0}, + {(char *)"text", __pyx_getprop_9csamtools_7Samfile_text, 0, __Pyx_DOCSTR(__pyx_k_214), 0}, + {(char *)"header", __pyx_getprop_9csamtools_7Samfile_header, 0, __Pyx_DOCSTR(__pyx_k_215), 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_Samfile = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_Samfile = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_Samfile = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_Samfile = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_Samfile = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.Samfile"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_Samfile), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_Samfile, /*tp_as_number*/ + &__pyx_tp_as_sequence_Samfile, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_Samfile, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_Samfile, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/ + __Pyx_DOCSTR("*(filename, mode='r', template = None, referencenames = None, referencelengths = None, text = NULL, header = None)*\n \n A :term:`SAM`/:term:`BAM` formatted file. The file is automatically opened.\n \n *mode* should be ``r`` for reading or ``w`` for writing. The default is text mode so for binary \n (:term:`BAM`) I/O you should append ``b`` for compressed or ``u`` for uncompressed :term:`BAM` output. \n Use ``h`` to output header information in text (:term:`TAM`) mode.\n\n If ``b`` is present, it must immediately follow ``r`` or ``w``. \n Valid modes are ``r``, ``w``, ``wh``, ``rb``, ``wb`` and ``wbu``. For instance, to open \n a :term:`BAM` formatted file for reading, type::\n\n import pysam\n f = pysam.Samfile('ex1.bam','rb')\n\n If an index for a BAM file exists (.bai), it will be opened automatically. Without an index random\n access to reads via :meth:`fetch` and :meth:`pileup` is disabled.\n\n For writing, the header of a :term:`SAM` file/:term:`BAM` file can be constituted from several\n sources (see also the samtools format specification):\n\n 1. If *template* is given, the header is copied from a another *Samfile* \n (*template* must be of type *Samfile*).\n\n 2. If *header* is given, the header is built from a multi-level dictionary. The first level \n are the four types ('HD', 'SQ', ...). The second level are a list of lines, with each line \n being a list of tag-value pairs.\n\n 3. If *text* is given, new header text is copied from raw text.\n\n 4. The names (*referencenames*) and lengths (*referencelengths*) are supplied directly as lists. \n\n "), /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + __pyx_pf_9csamtools_7Samfile_19__iter__, /*tp_iter*/ + __pyx_pf_9csamtools_7Samfile_20__next__, /*tp_iternext*/ + __pyx_methods_9csamtools_Samfile, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_9csamtools_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_9csamtools_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 +}; + +static PyObject *__pyx_tp_new_9csamtools_IteratorRow(PyTypeObject *t, PyObject *a, PyObject *k) { + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + return o; +} + +static void __pyx_tp_dealloc_9csamtools_IteratorRow(PyObject *o) { + (*Py_TYPE(o)->tp_free)(o); +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorRow[] = { + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorRow = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorRow = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorRow = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorRow = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorRow = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorRow"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorRow), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_IteratorRow, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorRow, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorRow, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorRow, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/ + __Pyx_DOCSTR("abstract 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 iterate over all reads in all reference sequences.\n \n The method :meth:`Samfile.fetch` returns an IteratorRow.\n "), /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_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*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_9csamtools_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 +}; +static struct __pyx_vtabstruct_9csamtools_IteratorRowRegion __pyx_vtable_9csamtools_IteratorRowRegion; + +static PyObject *__pyx_tp_new_9csamtools_IteratorRowRegion(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_IteratorRowRegion *p; + PyObject *o = __pyx_tp_new_9csamtools_IteratorRow(t, a, k); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_IteratorRowRegion *)o); + p->__pyx_vtab = __pyx_vtabptr_9csamtools_IteratorRowRegion; + p->samfile = ((struct __pyx_obj_9csamtools_Samfile *)Py_None); Py_INCREF(Py_None); + if (__pyx_pf_9csamtools_17IteratorRowRegion_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_IteratorRowRegion(PyObject *o) { + struct __pyx_obj_9csamtools_IteratorRowRegion *p = (struct __pyx_obj_9csamtools_IteratorRowRegion *)o; + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_17IteratorRowRegion_3__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + Py_XDECREF(((PyObject *)p->samfile)); + __pyx_tp_dealloc_9csamtools_IteratorRow(o); +} + +static int __pyx_tp_traverse_9csamtools_IteratorRowRegion(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_9csamtools_IteratorRowRegion *p = (struct __pyx_obj_9csamtools_IteratorRowRegion *)o; + if (__pyx_ptype_9csamtools_IteratorRow->tp_traverse) { + e = __pyx_ptype_9csamtools_IteratorRow->tp_traverse(o, v, a); if (e) return e; + } + if (p->samfile) { + e = (*v)(((PyObject*)p->samfile), a); if (e) return e; + } + return 0; +} + +static int __pyx_tp_clear_9csamtools_IteratorRowRegion(PyObject *o) { + struct __pyx_obj_9csamtools_IteratorRowRegion *p = (struct __pyx_obj_9csamtools_IteratorRowRegion *)o; + PyObject* tmp; + if (__pyx_ptype_9csamtools_IteratorRow->tp_clear) { + __pyx_ptype_9csamtools_IteratorRow->tp_clear(o); + } + tmp = ((PyObject*)p->samfile); + p->samfile = ((struct __pyx_obj_9csamtools_Samfile *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + return 0; +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorRowRegion[] = { + {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pf_9csamtools_17IteratorRowRegion_2__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_9csamtools_17IteratorRowRegion_2__next__)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorRowRegion = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorRowRegion = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorRowRegion = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorRowRegion = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorRowRegion = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorRowRegion"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorRowRegion), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_IteratorRowRegion, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorRowRegion, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorRowRegion, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorRowRegion, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("*(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 if\n multiple operators work 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 consequence, no two iterators can work\n on the same file. To permit this, each iterator\n creates its own file handle by re-opening the\n file.\n\n Note that the index will be shared between \n samfile and the iterator.\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_IteratorRowRegion, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_IteratorRowRegion, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + __pyx_pf_9csamtools_17IteratorRowRegion_1__iter__, /*tp_iter*/ + __pyx_pf_9csamtools_17IteratorRowRegion_2__next__, /*tp_iternext*/ + __pyx_methods_9csamtools_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*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_9csamtools_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 +}; +static struct __pyx_vtabstruct_9csamtools_IteratorRowAll __pyx_vtable_9csamtools_IteratorRowAll; + +static PyObject *__pyx_tp_new_9csamtools_IteratorRowAll(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_IteratorRowAll *p; + PyObject *o = __pyx_tp_new_9csamtools_IteratorRow(t, a, k); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_IteratorRowAll *)o); + p->__pyx_vtab = __pyx_vtabptr_9csamtools_IteratorRowAll; + if (__pyx_pf_9csamtools_14IteratorRowAll_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_IteratorRowAll(PyObject *o) { + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_14IteratorRowAll_3__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + __pyx_tp_dealloc_9csamtools_IteratorRow(o); +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorRowAll[] = { + {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pf_9csamtools_14IteratorRowAll_2__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_9csamtools_14IteratorRowAll_2__next__)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorRowAll = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorRowAll = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorRowAll = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorRowAll = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorRowAll = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorRowAll"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorRowAll), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_IteratorRowAll, /*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*/ + &__pyx_tp_as_number_IteratorRowAll, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorRowAll, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorRowAll, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorRowAll, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/ + __Pyx_DOCSTR("*(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 if\n multiple operators work on the same file. Set *reopen* = False\n to not re-open *samfile*.\n "), /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + __pyx_pf_9csamtools_14IteratorRowAll_1__iter__, /*tp_iter*/ + __pyx_pf_9csamtools_14IteratorRowAll_2__next__, /*tp_iternext*/ + __pyx_methods_9csamtools_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*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_9csamtools_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 +}; + +static PyObject *__pyx_tp_new_9csamtools_IteratorRowAllRefs(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_IteratorRowAllRefs *p; + PyObject *o = __pyx_tp_new_9csamtools_IteratorRow(t, a, k); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_IteratorRowAllRefs *)o); + p->samfile = ((struct __pyx_obj_9csamtools_Samfile *)Py_None); Py_INCREF(Py_None); + p->rowiter = ((struct __pyx_obj_9csamtools_IteratorRowRegion *)Py_None); Py_INCREF(Py_None); + if (__pyx_pf_9csamtools_18IteratorRowAllRefs_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_IteratorRowAllRefs(PyObject *o) { + struct __pyx_obj_9csamtools_IteratorRowAllRefs *p = (struct __pyx_obj_9csamtools_IteratorRowAllRefs *)o; + Py_XDECREF(((PyObject *)p->samfile)); + Py_XDECREF(((PyObject *)p->rowiter)); + __pyx_tp_dealloc_9csamtools_IteratorRow(o); +} + +static int __pyx_tp_traverse_9csamtools_IteratorRowAllRefs(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_9csamtools_IteratorRowAllRefs *p = (struct __pyx_obj_9csamtools_IteratorRowAllRefs *)o; + if (__pyx_ptype_9csamtools_IteratorRow->tp_traverse) { + e = __pyx_ptype_9csamtools_IteratorRow->tp_traverse(o, v, a); if (e) return e; + } + if (p->samfile) { + e = (*v)(((PyObject*)p->samfile), 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_9csamtools_IteratorRowAllRefs(PyObject *o) { + struct __pyx_obj_9csamtools_IteratorRowAllRefs *p = (struct __pyx_obj_9csamtools_IteratorRowAllRefs *)o; + PyObject* tmp; + if (__pyx_ptype_9csamtools_IteratorRow->tp_clear) { + __pyx_ptype_9csamtools_IteratorRow->tp_clear(o); + } + tmp = ((PyObject*)p->samfile); + p->samfile = ((struct __pyx_obj_9csamtools_Samfile *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + tmp = ((PyObject*)p->rowiter); + p->rowiter = ((struct __pyx_obj_9csamtools_IteratorRowRegion *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + return 0; +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorRowAllRefs[] = { + {__Pyx_NAMESTR("nextiter"), (PyCFunction)__pyx_pf_9csamtools_18IteratorRowAllRefs_1nextiter, METH_NOARGS, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pf_9csamtools_18IteratorRowAllRefs_3__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_9csamtools_18IteratorRowAllRefs_3__next__)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorRowAllRefs = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorRowAllRefs = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorRowAllRefs = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorRowAllRefs = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorRowAllRefs = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorRowAllRefs"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorRowAllRefs), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_IteratorRowAllRefs, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorRowAllRefs, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorRowAllRefs, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorRowAllRefs, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("iterates over all mapped reads by chaining iterators over each reference\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_IteratorRowAllRefs, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_IteratorRowAllRefs, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + __pyx_pf_9csamtools_18IteratorRowAllRefs_2__iter__, /*tp_iter*/ + __pyx_pf_9csamtools_18IteratorRowAllRefs_3__next__, /*tp_iternext*/ + __pyx_methods_9csamtools_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*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_9csamtools_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 +}; +static struct __pyx_vtabstruct_9csamtools_IteratorColumn __pyx_vtable_9csamtools_IteratorColumn; + +static PyObject *__pyx_tp_new_9csamtools_IteratorColumn(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_IteratorColumn *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_IteratorColumn *)o); + p->__pyx_vtab = __pyx_vtabptr_9csamtools_IteratorColumn; + p->iter = ((struct __pyx_obj_9csamtools_IteratorRowRegion *)Py_None); Py_INCREF(Py_None); + p->samfile = ((struct __pyx_obj_9csamtools_Samfile *)Py_None); Py_INCREF(Py_None); + p->fastafile = ((struct __pyx_obj_9csamtools_Fastafile *)Py_None); Py_INCREF(Py_None); + p->stepper = Py_None; Py_INCREF(Py_None); + if (__pyx_pf_9csamtools_14IteratorColumn_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_IteratorColumn(PyObject *o) { + struct __pyx_obj_9csamtools_IteratorColumn *p = (struct __pyx_obj_9csamtools_IteratorColumn *)o; + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_14IteratorColumn_4__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + Py_XDECREF(((PyObject *)p->iter)); + Py_XDECREF(((PyObject *)p->samfile)); + Py_XDECREF(((PyObject *)p->fastafile)); + Py_XDECREF(p->stepper); + (*Py_TYPE(o)->tp_free)(o); +} + +static int __pyx_tp_traverse_9csamtools_IteratorColumn(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_9csamtools_IteratorColumn *p = (struct __pyx_obj_9csamtools_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_9csamtools_IteratorColumn(PyObject *o) { + struct __pyx_obj_9csamtools_IteratorColumn *p = (struct __pyx_obj_9csamtools_IteratorColumn *)o; + PyObject* tmp; + tmp = ((PyObject*)p->iter); + p->iter = ((struct __pyx_obj_9csamtools_IteratorRowRegion *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + tmp = ((PyObject*)p->samfile); + p->samfile = ((struct __pyx_obj_9csamtools_Samfile *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + tmp = ((PyObject*)p->fastafile); + p->fastafile = ((struct __pyx_obj_9csamtools_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_9csamtools_14IteratorColumn_seq_len(PyObject *o, void *x) { + return __pyx_pf_9csamtools_14IteratorColumn_7seq_len_0__get__(o); +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorColumn[] = { + {__Pyx_NAMESTR("addReference"), (PyCFunction)__pyx_pf_9csamtools_14IteratorColumn_2addReference, METH_O, __Pyx_DOCSTR(__pyx_doc_9csamtools_14IteratorColumn_2addReference)}, + {__Pyx_NAMESTR("hasReference"), (PyCFunction)__pyx_pf_9csamtools_14IteratorColumn_3hasReference, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_9csamtools_14IteratorColumn_3hasReference)}, + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_9csamtools_IteratorColumn[] = { + {(char *)"seq_len", __pyx_getprop_9csamtools_14IteratorColumn_seq_len, 0, __Pyx_DOCSTR(__pyx_k_216), 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorColumn = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorColumn = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorColumn = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorColumn = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorColumn = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorColumn"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorColumn), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_IteratorColumn, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorColumn, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorColumn, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorColumn, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|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 contain ``n`` objects of type :class:`PileupProxy` for ``n`` columns, \n but each object in ``result`` will contain the same information.\n \n The desired behaviour can be achieved by list comprehension::\n\n result = [ x.pileups() for x in f.pileup() ]\n\n ``result`` will be a list of ``n`` lists of objects of type :class:`PileupRead`.\n\n If the iterator is associated with a :class:`Fastafile` using the :meth:`addReference`\n method, then the iterator will export the current sequence via the methods :meth:`getSequence`\n and :meth:`seq_len`.\n\n Optional kwargs to the iterator\n\n stepper\n The stepper controlls how the iterator advances. \n Possible options for the stepper are \n \n all\n use all reads for pileup.\n samtools\n same filter and read processing as in :term:`csamtools` pileup\n fastafile\n A :class:`FastaFile` object\n mask\n Skip all reads with bits set in mask.\n \n \n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_IteratorColumn, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_IteratorColumn, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + __pyx_pf_9csamtools_14IteratorColumn_1__iter__, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_IteratorColumn, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_9csamtools_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_9csamtools_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 +}; +static struct __pyx_vtabstruct_9csamtools_IteratorColumnRegion __pyx_vtable_9csamtools_IteratorColumnRegion; + +static PyObject *__pyx_tp_new_9csamtools_IteratorColumnRegion(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_IteratorColumnRegion *p; + PyObject *o = __pyx_tp_new_9csamtools_IteratorColumn(t, a, k); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_IteratorColumnRegion *)o); + p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_9csamtools_IteratorColumn*)__pyx_vtabptr_9csamtools_IteratorColumnRegion; + if (__pyx_pf_9csamtools_20IteratorColumnRegion_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorColumnRegion[] = { + {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pf_9csamtools_20IteratorColumnRegion_1__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_9csamtools_20IteratorColumnRegion_1__next__)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorColumnRegion = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorColumnRegion = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorColumnRegion = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorColumnRegion = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorColumnRegion = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorColumnRegion"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorColumnRegion), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_IteratorColumnRegion, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorColumnRegion, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorColumnRegion, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorColumnRegion, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|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_9csamtools_IteratorColumn, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_IteratorColumn, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + __pyx_pf_9csamtools_20IteratorColumnRegion_1__next__, /*tp_iternext*/ + __pyx_methods_9csamtools_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_9csamtools_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 +}; +static struct __pyx_vtabstruct_9csamtools_IteratorColumnAllRefs __pyx_vtable_9csamtools_IteratorColumnAllRefs; + +static PyObject *__pyx_tp_new_9csamtools_IteratorColumnAllRefs(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_IteratorColumnAllRefs *p; + PyObject *o = __pyx_tp_new_9csamtools_IteratorColumn(t, a, k); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_IteratorColumnAllRefs *)o); + p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_9csamtools_IteratorColumn*)__pyx_vtabptr_9csamtools_IteratorColumnAllRefs; + if (__pyx_pf_9csamtools_21IteratorColumnAllRefs_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorColumnAllRefs[] = { + {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pf_9csamtools_21IteratorColumnAllRefs_1__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_9csamtools_21IteratorColumnAllRefs_1__next__)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorColumnAllRefs = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorColumnAllRefs = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorColumnAllRefs = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorColumnAllRefs = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorColumnAllRefs = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorColumnAllRefs"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorColumnAllRefs), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_IteratorColumnAllRefs, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorColumnAllRefs, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorColumnAllRefs, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorColumnAllRefs, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|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_9csamtools_IteratorColumn, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_IteratorColumn, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + __pyx_pf_9csamtools_21IteratorColumnAllRefs_1__next__, /*tp_iternext*/ + __pyx_methods_9csamtools_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_9csamtools_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 +}; + +static PyObject *__pyx_tp_new_9csamtools_SNPCall(PyTypeObject *t, PyObject *a, PyObject *k) { + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + return o; +} + +static void __pyx_tp_dealloc_9csamtools_SNPCall(PyObject *o) { + (*Py_TYPE(o)->tp_free)(o); +} + +static PyObject *__pyx_getprop_9csamtools_7SNPCall_tid(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7SNPCall_3tid_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7SNPCall_pos(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7SNPCall_3pos_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7SNPCall_reference_base(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7SNPCall_14reference_base_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7SNPCall_genotype(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7SNPCall_8genotype_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7SNPCall_consensus_quality(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7SNPCall_17consensus_quality_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7SNPCall_snp_quality(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7SNPCall_11snp_quality_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7SNPCall_mapping_quality(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7SNPCall_15mapping_quality_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_7SNPCall_coverage(PyObject *o, void *x) { + return __pyx_pf_9csamtools_7SNPCall_8coverage_0__get__(o); +} + +static PyMethodDef __pyx_methods_9csamtools_SNPCall[] = { + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_9csamtools_SNPCall[] = { + {(char *)"tid", __pyx_getprop_9csamtools_7SNPCall_tid, 0, __Pyx_DOCSTR(__pyx_k_203), 0}, + {(char *)"pos", __pyx_getprop_9csamtools_7SNPCall_pos, 0, __Pyx_DOCSTR(__pyx_k_217), 0}, + {(char *)"reference_base", __pyx_getprop_9csamtools_7SNPCall_reference_base, 0, __Pyx_DOCSTR(__pyx_k_218), 0}, + {(char *)"genotype", __pyx_getprop_9csamtools_7SNPCall_genotype, 0, __Pyx_DOCSTR(__pyx_k_219), 0}, + {(char *)"consensus_quality", __pyx_getprop_9csamtools_7SNPCall_consensus_quality, 0, __Pyx_DOCSTR(__pyx_k_220), 0}, + {(char *)"snp_quality", __pyx_getprop_9csamtools_7SNPCall_snp_quality, 0, __Pyx_DOCSTR(__pyx_k_221), 0}, + {(char *)"mapping_quality", __pyx_getprop_9csamtools_7SNPCall_mapping_quality, 0, __Pyx_DOCSTR(__pyx_k_222), 0}, + {(char *)"coverage", __pyx_getprop_9csamtools_7SNPCall_coverage, 0, __Pyx_DOCSTR(__pyx_k_223), 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_SNPCall = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_SNPCall = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_SNPCall = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_SNPCall = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_SNPCall = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.SNPCall"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_SNPCall), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_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*/ + &__pyx_tp_as_number_SNPCall, /*tp_as_number*/ + &__pyx_tp_as_sequence_SNPCall, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_SNPCall, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + __pyx_pf_9csamtools_7SNPCall_0__str__, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_SNPCall, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|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_9csamtools_SNPCall, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_9csamtools_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_9csamtools_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 +}; +static struct __pyx_vtabstruct_9csamtools_SNPCallerBase __pyx_vtable_9csamtools_SNPCallerBase; + +static PyObject *__pyx_tp_new_9csamtools_SNPCallerBase(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_SNPCallerBase *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_SNPCallerBase *)o); + p->__pyx_vtab = __pyx_vtabptr_9csamtools_SNPCallerBase; + p->iter = ((struct __pyx_obj_9csamtools_IteratorColumn *)Py_None); Py_INCREF(Py_None); + if (__pyx_pf_9csamtools_13SNPCallerBase_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_SNPCallerBase(PyObject *o) { + struct __pyx_obj_9csamtools_SNPCallerBase *p = (struct __pyx_obj_9csamtools_SNPCallerBase *)o; + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_13SNPCallerBase_1__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + Py_XDECREF(((PyObject *)p->iter)); + (*Py_TYPE(o)->tp_free)(o); +} + +static int __pyx_tp_traverse_9csamtools_SNPCallerBase(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_9csamtools_SNPCallerBase *p = (struct __pyx_obj_9csamtools_SNPCallerBase *)o; + if (p->iter) { + e = (*v)(((PyObject*)p->iter), a); if (e) return e; + } + return 0; +} + +static int __pyx_tp_clear_9csamtools_SNPCallerBase(PyObject *o) { + struct __pyx_obj_9csamtools_SNPCallerBase *p = (struct __pyx_obj_9csamtools_SNPCallerBase *)o; + PyObject* tmp; + tmp = ((PyObject*)p->iter); + p->iter = ((struct __pyx_obj_9csamtools_IteratorColumn *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + return 0; +} + +static PyMethodDef __pyx_methods_9csamtools_SNPCallerBase[] = { + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_SNPCallerBase = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_SNPCallerBase = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_SNPCallerBase = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_SNPCallerBase = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_SNPCallerBase = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.SNPCallerBase"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_SNPCallerBase), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_SNPCallerBase, /*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*/ + &__pyx_tp_as_number_SNPCallerBase, /*tp_as_number*/ + &__pyx_tp_as_sequence_SNPCallerBase, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_SNPCallerBase, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_SNPCallerBase, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("Base class for SNP callers.\n\n *min_baseQ*\n minimum base quality (possibly capped by BAQ)\n *capQ_threshold*\n coefficient for adjusting mapQ of poor mappings\n *theta*\n theta in maq consensus calling model\n *n_haplotypes*\n number of haplotypes in the sample\n *het_rate*\n prior of a difference between two haplotypes\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_SNPCallerBase, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_SNPCallerBase, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_SNPCallerBase, /*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_9csamtools_SNPCallerBase, /*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 +}; +static struct __pyx_vtabstruct_9csamtools_IteratorSNPCalls __pyx_vtable_9csamtools_IteratorSNPCalls; + +static PyObject *__pyx_tp_new_9csamtools_IteratorSNPCalls(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_IteratorSNPCalls *p; + PyObject *o = __pyx_tp_new_9csamtools_SNPCallerBase(t, a, k); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_IteratorSNPCalls *)o); + p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_9csamtools_SNPCallerBase*)__pyx_vtabptr_9csamtools_IteratorSNPCalls; + if (__pyx_pf_9csamtools_16IteratorSNPCalls_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorSNPCalls[] = { + {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pf_9csamtools_16IteratorSNPCalls_2__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_9csamtools_16IteratorSNPCalls_2__next__)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorSNPCalls = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorSNPCalls = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorSNPCalls = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorSNPCalls = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorSNPCalls = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorSNPCalls"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorSNPCalls), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_SNPCallerBase, /*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*/ + &__pyx_tp_as_number_IteratorSNPCalls, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorSNPCalls, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorSNPCalls, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorSNPCalls, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("*(IteratorColumn iterator)*\n\n call SNPs within a region.\n\n *iterator* is a pileup iterator. SNPs will be called\n on all positions returned by this iterator.\n\n This caller is fast if SNPs are called over large continuous\n regions. It is slow, if instantiated frequently and in random\n order as the sequence will have to be reloaded.\n\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_SNPCallerBase, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_SNPCallerBase, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + __pyx_pf_9csamtools_16IteratorSNPCalls_1__iter__, /*tp_iter*/ + __pyx_pf_9csamtools_16IteratorSNPCalls_2__next__, /*tp_iternext*/ + __pyx_methods_9csamtools_IteratorSNPCalls, /*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_9csamtools_IteratorSNPCalls, /*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 +}; +static struct __pyx_vtabstruct_9csamtools_SNPCaller __pyx_vtable_9csamtools_SNPCaller; + +static PyObject *__pyx_tp_new_9csamtools_SNPCaller(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_SNPCaller *p; + PyObject *o = __pyx_tp_new_9csamtools_SNPCallerBase(t, a, k); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_SNPCaller *)o); + p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_9csamtools_SNPCallerBase*)__pyx_vtabptr_9csamtools_SNPCaller; + if (__pyx_pf_9csamtools_9SNPCaller_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static PyMethodDef __pyx_methods_9csamtools_SNPCaller[] = { + {__Pyx_NAMESTR("call"), (PyCFunction)__pyx_pf_9csamtools_9SNPCaller_1call, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_9SNPCaller_1call)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_SNPCaller = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_SNPCaller = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_SNPCaller = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_SNPCaller = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_SNPCaller = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.SNPCaller"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_SNPCaller), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_SNPCallerBase, /*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*/ + &__pyx_tp_as_number_SNPCaller, /*tp_as_number*/ + &__pyx_tp_as_sequence_SNPCaller, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_SNPCaller, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_SNPCaller, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("*(IteratorColumn iterator_column )*\n\n The samtools SNP caller.\n\n This object will call SNPs in *samfile* against the reference\n sequence in *fasta*.\n\n This caller is fast for calling few SNPs in selected regions.\n\n It is slow, if called over large genomic regions.\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_SNPCallerBase, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_SNPCallerBase, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_SNPCaller, /*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_9csamtools_SNPCaller, /*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 +}; + +static PyObject *__pyx_tp_new_9csamtools_IndelCall(PyTypeObject *t, PyObject *a, PyObject *k) { + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + if (__pyx_pf_9csamtools_9IndelCall_0__cinit__(o, __pyx_empty_tuple, NULL) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_IndelCall(PyObject *o) { + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_9IndelCall_2__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + (*Py_TYPE(o)->tp_free)(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_tid(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_3tid_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_pos(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_3pos_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_genotype(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_8genotype_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_consensus_quality(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_17consensus_quality_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_snp_quality(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_11snp_quality_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_mapping_quality(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_15mapping_quality_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_coverage(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_8coverage_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_first_allele(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_12first_allele_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_second_allele(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_13second_allele_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_reads_first(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_11reads_first_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_reads_second(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_12reads_second_0__get__(o); +} + +static PyObject *__pyx_getprop_9csamtools_9IndelCall_reads_diff(PyObject *o, void *x) { + return __pyx_pf_9csamtools_9IndelCall_10reads_diff_0__get__(o); +} + +static PyMethodDef __pyx_methods_9csamtools_IndelCall[] = { + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_9csamtools_IndelCall[] = { + {(char *)"tid", __pyx_getprop_9csamtools_9IndelCall_tid, 0, __Pyx_DOCSTR(__pyx_k_203), 0}, + {(char *)"pos", __pyx_getprop_9csamtools_9IndelCall_pos, 0, __Pyx_DOCSTR(__pyx_k_217), 0}, + {(char *)"genotype", __pyx_getprop_9csamtools_9IndelCall_genotype, 0, __Pyx_DOCSTR(__pyx_k_219), 0}, + {(char *)"consensus_quality", __pyx_getprop_9csamtools_9IndelCall_consensus_quality, 0, __Pyx_DOCSTR(__pyx_k_220), 0}, + {(char *)"snp_quality", __pyx_getprop_9csamtools_9IndelCall_snp_quality, 0, __Pyx_DOCSTR(__pyx_k_221), 0}, + {(char *)"mapping_quality", __pyx_getprop_9csamtools_9IndelCall_mapping_quality, 0, __Pyx_DOCSTR(__pyx_k_222), 0}, + {(char *)"coverage", __pyx_getprop_9csamtools_9IndelCall_coverage, 0, __Pyx_DOCSTR(__pyx_k_223), 0}, + {(char *)"first_allele", __pyx_getprop_9csamtools_9IndelCall_first_allele, 0, __Pyx_DOCSTR(__pyx_k_224), 0}, + {(char *)"second_allele", __pyx_getprop_9csamtools_9IndelCall_second_allele, 0, __Pyx_DOCSTR(__pyx_k_225), 0}, + {(char *)"reads_first", __pyx_getprop_9csamtools_9IndelCall_reads_first, 0, __Pyx_DOCSTR(__pyx_k_226), 0}, + {(char *)"reads_second", __pyx_getprop_9csamtools_9IndelCall_reads_second, 0, __Pyx_DOCSTR(__pyx_k_226), 0}, + {(char *)"reads_diff", __pyx_getprop_9csamtools_9IndelCall_reads_diff, 0, __Pyx_DOCSTR(__pyx_k_226), 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IndelCall = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IndelCall = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IndelCall = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IndelCall = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IndelCall = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IndelCall"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IndelCall), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_IndelCall, /*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*/ + &__pyx_tp_as_number_IndelCall, /*tp_as_number*/ + &__pyx_tp_as_sequence_IndelCall, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IndelCall, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + __pyx_pf_9csamtools_9IndelCall_1__str__, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IndelCall, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/ + __Pyx_DOCSTR("the results of an indel call."), /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_IndelCall, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_9csamtools_IndelCall, /*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_9csamtools_IndelCall, /*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 +}; + +static PyObject *__pyx_tp_new_9csamtools_IndelCallerBase(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_9csamtools_IndelCallerBase *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_9csamtools_IndelCallerBase *)o); + p->iter = ((struct __pyx_obj_9csamtools_IteratorColumn *)Py_None); Py_INCREF(Py_None); + if (__pyx_pf_9csamtools_15IndelCallerBase_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_9csamtools_IndelCallerBase(PyObject *o) { + struct __pyx_obj_9csamtools_IndelCallerBase *p = (struct __pyx_obj_9csamtools_IndelCallerBase *)o; + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pf_9csamtools_15IndelCallerBase_1__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + Py_XDECREF(((PyObject *)p->iter)); + (*Py_TYPE(o)->tp_free)(o); +} + +static int __pyx_tp_traverse_9csamtools_IndelCallerBase(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_9csamtools_IndelCallerBase *p = (struct __pyx_obj_9csamtools_IndelCallerBase *)o; + if (p->iter) { + e = (*v)(((PyObject*)p->iter), a); if (e) return e; + } + return 0; +} + +static int __pyx_tp_clear_9csamtools_IndelCallerBase(PyObject *o) { + struct __pyx_obj_9csamtools_IndelCallerBase *p = (struct __pyx_obj_9csamtools_IndelCallerBase *)o; + PyObject* tmp; + tmp = ((PyObject*)p->iter); + p->iter = ((struct __pyx_obj_9csamtools_IteratorColumn *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + return 0; +} + +static PyMethodDef __pyx_methods_9csamtools_IndelCallerBase[] = { + {__Pyx_NAMESTR("_call"), (PyCFunction)__pyx_pf_9csamtools_15IndelCallerBase_2_call, METH_NOARGS, __Pyx_DOCSTR(0)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IndelCallerBase = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IndelCallerBase = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IndelCallerBase = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IndelCallerBase = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IndelCallerBase = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IndelCallerBase"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IndelCallerBase), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_IndelCallerBase, /*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*/ + &__pyx_tp_as_number_IndelCallerBase, /*tp_as_number*/ + &__pyx_tp_as_sequence_IndelCallerBase, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IndelCallerBase, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IndelCallerBase, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("Base class for SNP callers.\n\n *min_baseQ*\n minimum base quality (possibly capped by BAQ)\n *capQ_threshold*\n coefficient for adjusting mapQ of poor mappings\n *theta*\n theta in maq consensus calling model\n *n_haplotypes*\n number of haplotypes in the sample\n *het_rate*\n prior of a difference between two haplotypes\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_IndelCallerBase, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_IndelCallerBase, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_IndelCallerBase, /*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_9csamtools_IndelCallerBase, /*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 +}; + +static PyObject *__pyx_tp_new_9csamtools_IndelCaller(PyTypeObject *t, PyObject *a, PyObject *k) { + PyObject *o = __pyx_tp_new_9csamtools_IndelCallerBase(t, a, k); + if (!o) return 0; + if (__pyx_pf_9csamtools_11IndelCaller_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static PyMethodDef __pyx_methods_9csamtools_IndelCaller[] = { + {__Pyx_NAMESTR("call"), (PyCFunction)__pyx_pf_9csamtools_11IndelCaller_1call, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_9csamtools_11IndelCaller_1call)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IndelCaller = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IndelCaller = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IndelCaller = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IndelCaller = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IndelCaller = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IndelCaller"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IndelCaller), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_IndelCallerBase, /*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*/ + &__pyx_tp_as_number_IndelCaller, /*tp_as_number*/ + &__pyx_tp_as_sequence_IndelCaller, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IndelCaller, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IndelCaller, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("*(IteratorColumn iterator_column )*\n\n The samtools SNP caller.\n\n This object will call SNPs in *samfile* against the reference\n sequence in *fasta*.\n\n This caller is fast for calling few SNPs in selected regions.\n\n It is slow, if called over large genomic regions.\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_IndelCallerBase, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_IndelCallerBase, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_9csamtools_IndelCaller, /*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_9csamtools_IndelCaller, /*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 +}; + +static PyObject *__pyx_tp_new_9csamtools_IteratorIndelCalls(PyTypeObject *t, PyObject *a, PyObject *k) { + PyObject *o = __pyx_tp_new_9csamtools_IndelCallerBase(t, a, k); + if (!o) return 0; + if (__pyx_pf_9csamtools_18IteratorIndelCalls_0__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static PyMethodDef __pyx_methods_9csamtools_IteratorIndelCalls[] = { + {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pf_9csamtools_18IteratorIndelCalls_2__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_9csamtools_18IteratorIndelCalls_2__next__)}, + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_IteratorIndelCalls = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_IteratorIndelCalls = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_IteratorIndelCalls = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_IteratorIndelCalls = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +PyTypeObject __pyx_type_9csamtools_IteratorIndelCalls = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("csamtools.IteratorIndelCalls"), /*tp_name*/ + sizeof(struct __pyx_obj_9csamtools_IteratorIndelCalls), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_9csamtools_IndelCallerBase, /*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*/ + &__pyx_tp_as_number_IteratorIndelCalls, /*tp_as_number*/ + &__pyx_tp_as_sequence_IteratorIndelCalls, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_IteratorIndelCalls, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_IteratorIndelCalls, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + __Pyx_DOCSTR("*(IteratorColumn iterator)*\n\n call indels within a region.\n\n *iterator* is a pileup iterator. SNPs will be called\n on all positions returned by this iterator.\n\n This caller is fast if SNPs are called over large continuous\n regions. It is slow, if instantiated frequently and in random\n order as the sequence will have to be reloaded.\n\n "), /*tp_doc*/ + __pyx_tp_traverse_9csamtools_IndelCallerBase, /*tp_traverse*/ + __pyx_tp_clear_9csamtools_IndelCallerBase, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + __pyx_pf_9csamtools_18IteratorIndelCalls_1__iter__, /*tp_iter*/ + __pyx_pf_9csamtools_18IteratorIndelCalls_2__next__, /*tp_iternext*/ + __pyx_methods_9csamtools_IteratorIndelCalls, /*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_9csamtools_IteratorIndelCalls, /*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 +}; + +static PyMethodDef __pyx_methods[] = { + {0, 0, 0, 0} +}; + +#if PY_MAJOR_VERSION >= 3 +static struct PyModuleDef __pyx_moduledef = { + PyModuleDef_HEAD_INIT, + __Pyx_NAMESTR("csamtools"), + 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_1, __pyx_k_1, sizeof(__pyx_k_1), 0, 0, 1, 0}, + {&__pyx_kp_s_102, __pyx_k_102, sizeof(__pyx_k_102), 0, 0, 1, 0}, + {&__pyx_kp_s_103, __pyx_k_103, sizeof(__pyx_k_103), 0, 0, 1, 0}, + {&__pyx_kp_s_105, __pyx_k_105, sizeof(__pyx_k_105), 0, 0, 1, 0}, + {&__pyx_kp_s_109, __pyx_k_109, sizeof(__pyx_k_109), 0, 0, 1, 0}, + {&__pyx_kp_s_11, __pyx_k_11, sizeof(__pyx_k_11), 0, 0, 1, 0}, + {&__pyx_kp_s_110, __pyx_k_110, sizeof(__pyx_k_110), 0, 0, 1, 0}, + {&__pyx_n_s_111, __pyx_k_111, sizeof(__pyx_k_111), 0, 0, 1, 1}, + {&__pyx_kp_s_112, __pyx_k_112, sizeof(__pyx_k_112), 0, 0, 1, 0}, + {&__pyx_kp_s_113, __pyx_k_113, sizeof(__pyx_k_113), 0, 0, 1, 0}, + {&__pyx_kp_s_114, __pyx_k_114, sizeof(__pyx_k_114), 0, 0, 1, 0}, + {&__pyx_kp_s_115, __pyx_k_115, sizeof(__pyx_k_115), 0, 0, 1, 0}, + {&__pyx_kp_s_116, __pyx_k_116, sizeof(__pyx_k_116), 0, 0, 1, 0}, + {&__pyx_kp_s_117, __pyx_k_117, sizeof(__pyx_k_117), 0, 0, 1, 0}, + {&__pyx_kp_s_118, __pyx_k_118, sizeof(__pyx_k_118), 0, 0, 1, 0}, + {&__pyx_kp_s_119, __pyx_k_119, sizeof(__pyx_k_119), 0, 0, 1, 0}, + {&__pyx_kp_s_12, __pyx_k_12, sizeof(__pyx_k_12), 0, 0, 1, 0}, + {&__pyx_kp_s_120, __pyx_k_120, sizeof(__pyx_k_120), 0, 0, 1, 0}, + {&__pyx_kp_s_121, __pyx_k_121, sizeof(__pyx_k_121), 0, 0, 1, 0}, + {&__pyx_kp_s_122, __pyx_k_122, sizeof(__pyx_k_122), 0, 0, 1, 0}, + {&__pyx_kp_s_124, __pyx_k_124, sizeof(__pyx_k_124), 0, 0, 1, 0}, + {&__pyx_kp_s_125, __pyx_k_125, sizeof(__pyx_k_125), 0, 0, 1, 0}, + {&__pyx_kp_s_126, __pyx_k_126, sizeof(__pyx_k_126), 0, 0, 1, 0}, + {&__pyx_kp_s_127, __pyx_k_127, sizeof(__pyx_k_127), 0, 0, 1, 0}, + {&__pyx_kp_s_128, __pyx_k_128, sizeof(__pyx_k_128), 0, 0, 1, 0}, + {&__pyx_kp_s_129, __pyx_k_129, sizeof(__pyx_k_129), 0, 0, 1, 0}, + {&__pyx_kp_s_13, __pyx_k_13, sizeof(__pyx_k_13), 0, 0, 1, 0}, + {&__pyx_kp_s_130, __pyx_k_130, sizeof(__pyx_k_130), 0, 0, 1, 0}, + {&__pyx_kp_s_131, __pyx_k_131, sizeof(__pyx_k_131), 0, 0, 1, 0}, + {&__pyx_kp_s_132, __pyx_k_132, sizeof(__pyx_k_132), 0, 0, 1, 0}, + {&__pyx_kp_s_133, __pyx_k_133, sizeof(__pyx_k_133), 0, 0, 1, 0}, + {&__pyx_kp_s_134, __pyx_k_134, sizeof(__pyx_k_134), 0, 0, 1, 0}, + {&__pyx_kp_s_135, __pyx_k_135, sizeof(__pyx_k_135), 0, 0, 1, 0}, + {&__pyx_kp_s_136, __pyx_k_136, sizeof(__pyx_k_136), 0, 0, 1, 0}, + {&__pyx_kp_s_137, __pyx_k_137, sizeof(__pyx_k_137), 0, 0, 1, 0}, + {&__pyx_kp_s_138, __pyx_k_138, sizeof(__pyx_k_138), 0, 0, 1, 0}, + {&__pyx_kp_s_139, __pyx_k_139, sizeof(__pyx_k_139), 0, 0, 1, 0}, + {&__pyx_kp_s_14, __pyx_k_14, sizeof(__pyx_k_14), 0, 0, 1, 0}, + {&__pyx_kp_s_140, __pyx_k_140, sizeof(__pyx_k_140), 0, 0, 1, 0}, + {&__pyx_kp_s_141, __pyx_k_141, sizeof(__pyx_k_141), 0, 0, 1, 0}, + {&__pyx_kp_s_142, __pyx_k_142, sizeof(__pyx_k_142), 0, 0, 1, 0}, + {&__pyx_kp_s_143, __pyx_k_143, sizeof(__pyx_k_143), 0, 0, 1, 0}, + {&__pyx_kp_s_144, __pyx_k_144, sizeof(__pyx_k_144), 0, 0, 1, 0}, + {&__pyx_kp_s_145, __pyx_k_145, sizeof(__pyx_k_145), 0, 0, 1, 0}, + {&__pyx_kp_s_146, __pyx_k_146, sizeof(__pyx_k_146), 0, 0, 1, 0}, + {&__pyx_kp_s_15, __pyx_k_15, sizeof(__pyx_k_15), 0, 0, 1, 0}, + {&__pyx_kp_s_151, __pyx_k_151, sizeof(__pyx_k_151), 0, 0, 1, 0}, + {&__pyx_kp_s_152, __pyx_k_152, sizeof(__pyx_k_152), 0, 0, 1, 0}, + {&__pyx_kp_s_153, __pyx_k_153, sizeof(__pyx_k_153), 0, 0, 1, 0}, + {&__pyx_n_s_155, __pyx_k_155, sizeof(__pyx_k_155), 0, 0, 1, 1}, + {&__pyx_kp_s_156, __pyx_k_156, sizeof(__pyx_k_156), 0, 0, 1, 0}, + {&__pyx_kp_s_157, __pyx_k_157, sizeof(__pyx_k_157), 0, 0, 1, 0}, + {&__pyx_kp_s_159, __pyx_k_159, sizeof(__pyx_k_159), 0, 0, 1, 0}, + {&__pyx_kp_s_16, __pyx_k_16, sizeof(__pyx_k_16), 0, 0, 1, 0}, + {&__pyx_kp_s_161, __pyx_k_161, sizeof(__pyx_k_161), 0, 0, 1, 0}, + {&__pyx_kp_s_163, __pyx_k_163, sizeof(__pyx_k_163), 0, 0, 1, 0}, + {&__pyx_kp_s_165, __pyx_k_165, sizeof(__pyx_k_165), 0, 0, 1, 0}, + {&__pyx_kp_s_166, __pyx_k_166, sizeof(__pyx_k_166), 0, 0, 1, 0}, + {&__pyx_kp_s_19, __pyx_k_19, sizeof(__pyx_k_19), 0, 0, 1, 0}, + {&__pyx_kp_s_2, __pyx_k_2, sizeof(__pyx_k_2), 0, 0, 1, 0}, + {&__pyx_kp_s_20, __pyx_k_20, sizeof(__pyx_k_20), 0, 0, 1, 0}, + {&__pyx_kp_s_22, __pyx_k_22, sizeof(__pyx_k_22), 0, 0, 1, 0}, + {&__pyx_kp_s_228, __pyx_k_228, sizeof(__pyx_k_228), 0, 0, 1, 0}, + {&__pyx_kp_s_23, __pyx_k_23, sizeof(__pyx_k_23), 0, 0, 1, 0}, + {&__pyx_kp_s_230, __pyx_k_230, sizeof(__pyx_k_230), 0, 0, 1, 0}, + {&__pyx_kp_s_231, __pyx_k_231, sizeof(__pyx_k_231), 0, 0, 1, 0}, + {&__pyx_kp_s_237, __pyx_k_237, sizeof(__pyx_k_237), 0, 0, 1, 0}, + {&__pyx_kp_s_27, __pyx_k_27, sizeof(__pyx_k_27), 0, 0, 1, 0}, + {&__pyx_kp_s_28, __pyx_k_28, sizeof(__pyx_k_28), 0, 0, 1, 0}, + {&__pyx_kp_s_3, __pyx_k_3, sizeof(__pyx_k_3), 0, 0, 1, 0}, + {&__pyx_kp_s_30, __pyx_k_30, sizeof(__pyx_k_30), 0, 0, 1, 0}, + {&__pyx_kp_s_32, __pyx_k_32, sizeof(__pyx_k_32), 0, 0, 1, 0}, + {&__pyx_kp_s_34, __pyx_k_34, sizeof(__pyx_k_34), 0, 0, 1, 0}, + {&__pyx_kp_s_35, __pyx_k_35, sizeof(__pyx_k_35), 0, 0, 1, 0}, + {&__pyx_kp_s_37, __pyx_k_37, sizeof(__pyx_k_37), 0, 0, 1, 0}, + {&__pyx_kp_s_43, __pyx_k_43, sizeof(__pyx_k_43), 0, 0, 1, 0}, + {&__pyx_kp_s_45, __pyx_k_45, sizeof(__pyx_k_45), 0, 0, 1, 0}, + {&__pyx_kp_s_47, __pyx_k_47, sizeof(__pyx_k_47), 0, 0, 1, 0}, + {&__pyx_kp_s_49, __pyx_k_49, sizeof(__pyx_k_49), 0, 0, 1, 0}, + {&__pyx_kp_s_5, __pyx_k_5, sizeof(__pyx_k_5), 0, 0, 1, 0}, + {&__pyx_kp_s_51, __pyx_k_51, sizeof(__pyx_k_51), 0, 0, 1, 0}, + {&__pyx_kp_s_53, __pyx_k_53, sizeof(__pyx_k_53), 0, 0, 1, 0}, + {&__pyx_kp_s_55, __pyx_k_55, sizeof(__pyx_k_55), 0, 0, 1, 0}, + {&__pyx_kp_s_57, __pyx_k_57, sizeof(__pyx_k_57), 0, 0, 1, 0}, + {&__pyx_kp_s_59, __pyx_k_59, sizeof(__pyx_k_59), 0, 0, 1, 0}, + {&__pyx_kp_s_6, __pyx_k_6, sizeof(__pyx_k_6), 0, 0, 1, 0}, + {&__pyx_kp_s_64, __pyx_k_64, sizeof(__pyx_k_64), 0, 0, 1, 0}, + {&__pyx_kp_s_67, __pyx_k_67, sizeof(__pyx_k_67), 0, 0, 1, 0}, + {&__pyx_kp_s_70, __pyx_k_70, sizeof(__pyx_k_70), 0, 0, 1, 0}, + {&__pyx_kp_s_73, __pyx_k_73, sizeof(__pyx_k_73), 0, 0, 1, 0}, + {&__pyx_kp_s_83, __pyx_k_83, sizeof(__pyx_k_83), 0, 0, 1, 0}, + {&__pyx_kp_s_85, __pyx_k_85, sizeof(__pyx_k_85), 0, 0, 1, 0}, + {&__pyx_kp_s_87, __pyx_k_87, sizeof(__pyx_k_87), 0, 0, 1, 0}, + {&__pyx_kp_s_88, __pyx_k_88, sizeof(__pyx_k_88), 0, 0, 1, 0}, + {&__pyx_kp_s_9, __pyx_k_9, sizeof(__pyx_k_9), 0, 0, 1, 0}, + {&__pyx_kp_s_90, __pyx_k_90, sizeof(__pyx_k_90), 0, 0, 1, 0}, + {&__pyx_kp_s_91, __pyx_k_91, sizeof(__pyx_k_91), 0, 0, 1, 0}, + {&__pyx_kp_s_92, __pyx_k_92, sizeof(__pyx_k_92), 0, 0, 1, 0}, + {&__pyx_kp_s_93, __pyx_k_93, sizeof(__pyx_k_93), 0, 0, 1, 0}, + {&__pyx_kp_s_94, __pyx_k_94, sizeof(__pyx_k_94), 0, 0, 1, 0}, + {&__pyx_kp_s_95, __pyx_k_95, sizeof(__pyx_k_95), 0, 0, 1, 0}, + {&__pyx_kp_s_98, __pyx_k_98, sizeof(__pyx_k_98), 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_n_s__AlignedRead, __pyx_k__AlignedRead, sizeof(__pyx_k__AlignedRead), 0, 0, 1, 1}, + {&__pyx_n_s__C, __pyx_k__C, sizeof(__pyx_k__C), 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_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__DictType, __pyx_k__DictType, sizeof(__pyx_k__DictType), 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__FloatType, __pyx_k__FloatType, sizeof(__pyx_k__FloatType), 0, 0, 1, 1}, + {&__pyx_n_s__GO, __pyx_k__GO, sizeof(__pyx_k__GO), 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__IndelCaller, __pyx_k__IndelCaller, sizeof(__pyx_k__IndelCaller), 0, 0, 1, 1}, + {&__pyx_n_s__IntType, __pyx_k__IntType, sizeof(__pyx_k__IntType), 0, 0, 1, 1}, + {&__pyx_n_s__IteratorColumn, __pyx_k__IteratorColumn, sizeof(__pyx_k__IteratorColumn), 0, 0, 1, 1}, + {&__pyx_n_s__IteratorIndelCalls, __pyx_k__IteratorIndelCalls, sizeof(__pyx_k__IteratorIndelCalls), 0, 0, 1, 1}, + {&__pyx_n_s__IteratorRow, __pyx_k__IteratorRow, sizeof(__pyx_k__IteratorRow), 0, 0, 1, 1}, + {&__pyx_n_s__IteratorSNPCalls, __pyx_k__IteratorSNPCalls, sizeof(__pyx_k__IteratorSNPCalls), 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_n_s__M5, __pyx_k__M5, sizeof(__pyx_k__M5), 0, 0, 1, 1}, + {&__pyx_n_s__NotImplementedError, __pyx_k__NotImplementedError, sizeof(__pyx_k__NotImplementedError), 0, 0, 1, 1}, + {&__pyx_n_s__O_CREAT, __pyx_k__O_CREAT, sizeof(__pyx_k__O_CREAT), 0, 0, 1, 1}, + {&__pyx_n_s__O_WRONLY, __pyx_k__O_WRONLY, sizeof(__pyx_k__O_WRONLY), 0, 0, 1, 1}, + {&__pyx_n_s__Outs, __pyx_k__Outs, sizeof(__pyx_k__Outs), 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__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__PileupProxy, __pyx_k__PileupProxy, sizeof(__pyx_k__PileupProxy), 0, 0, 1, 1}, + {&__pyx_n_s__PileupRead, __pyx_k__PileupRead, sizeof(__pyx_k__PileupRead), 0, 0, 1, 1}, + {&__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__SNPCaller, __pyx_k__SNPCaller, sizeof(__pyx_k__SNPCaller), 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_n_s__Samfile, __pyx_k__Samfile, sizeof(__pyx_k__Samfile), 0, 0, 1, 1}, + {&__pyx_n_s__StderrStore, __pyx_k__StderrStore, sizeof(__pyx_k__StderrStore), 0, 0, 1, 1}, + {&__pyx_n_s__StopIteration, __pyx_k__StopIteration, sizeof(__pyx_k__StopIteration), 0, 0, 1, 1}, + {&__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_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____all__, __pyx_k____all__, sizeof(__pyx_k____all__), 0, 0, 1, 1}, + {&__pyx_n_s____del__, __pyx_k____del__, sizeof(__pyx_k____del__), 0, 0, 1, 1}, + {&__pyx_n_s____dict__, __pyx_k____dict__, sizeof(__pyx_k____dict__), 0, 0, 1, 1}, + {&__pyx_n_s____getattribute__, __pyx_k____getattribute__, sizeof(__pyx_k____getattribute__), 0, 0, 1, 1}, + {&__pyx_n_s____init__, __pyx_k____init__, sizeof(__pyx_k____init__), 0, 0, 1, 1}, + {&__pyx_n_s____main__, __pyx_k____main__, sizeof(__pyx_k____main__), 0, 0, 1, 1}, + {&__pyx_n_s____str__, __pyx_k____str__, sizeof(__pyx_k____str__), 0, 0, 1, 1}, + {&__pyx_n_s____test__, __pyx_k____test__, sizeof(__pyx_k____test__), 0, 0, 1, 1}, + {&__pyx_n_s___alignment, __pyx_k___alignment, sizeof(__pyx_k___alignment), 0, 0, 1, 1}, + {&__pyx_n_s___buildHeader, __pyx_k___buildHeader, sizeof(__pyx_k___buildHeader), 0, 0, 1, 1}, + {&__pyx_n_s___buildLine, __pyx_k___buildLine, sizeof(__pyx_k___buildLine), 0, 0, 1, 1}, + {&__pyx_n_s___call, __pyx_k___call, sizeof(__pyx_k___call), 0, 0, 1, 1}, + {&__pyx_n_s___consensus_quality, __pyx_k___consensus_quality, sizeof(__pyx_k___consensus_quality), 0, 0, 1, 1}, + {&__pyx_n_s___coverage, __pyx_k___coverage, sizeof(__pyx_k___coverage), 0, 0, 1, 1}, + {&__pyx_n_s___delegate, __pyx_k___delegate, sizeof(__pyx_k___delegate), 0, 0, 1, 1}, + {&__pyx_n_s___filename, __pyx_k___filename, sizeof(__pyx_k___filename), 0, 0, 1, 1}, + {&__pyx_n_s___genotype, __pyx_k___genotype, sizeof(__pyx_k___genotype), 0, 0, 1, 1}, + {&__pyx_n_s___hasIndex, __pyx_k___hasIndex, sizeof(__pyx_k___hasIndex), 0, 0, 1, 1}, + {&__pyx_n_s___indel, __pyx_k___indel, sizeof(__pyx_k___indel), 0, 0, 1, 1}, + {&__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___level, __pyx_k___level, sizeof(__pyx_k___level), 0, 0, 1, 1}, + {&__pyx_n_s___logfile, __pyx_k___logfile, sizeof(__pyx_k___logfile), 0, 0, 1, 1}, + {&__pyx_n_s___open, __pyx_k___open, sizeof(__pyx_k___open), 0, 0, 1, 1}, + {&__pyx_n_s___parseRegion, __pyx_k___parseRegion, sizeof(__pyx_k___parseRegion), 0, 0, 1, 1}, + {&__pyx_n_s___pos, __pyx_k___pos, sizeof(__pyx_k___pos), 0, 0, 1, 1}, + {&__pyx_n_s___qpos, __pyx_k___qpos, sizeof(__pyx_k___qpos), 0, 0, 1, 1}, + {&__pyx_n_s___r, __pyx_k___r, sizeof(__pyx_k___r), 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___samtools_dispatch, __pyx_k___samtools_dispatch, sizeof(__pyx_k___samtools_dispatch), 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___tid, __pyx_k___tid, sizeof(__pyx_k___tid), 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__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__b, __pyx_k__b, sizeof(__pyx_k__b), 0, 0, 1, 1}, + {&__pyx_n_s__bam, __pyx_k__bam, sizeof(__pyx_k__bam), 0, 0, 1, 1}, + {&__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__c, __pyx_k__c, sizeof(__pyx_k__c), 0, 0, 1, 1}, + {&__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_n_s__cap_mapQ, __pyx_k__cap_mapQ, sizeof(__pyx_k__cap_mapQ), 0, 0, 1, 1}, + {&__pyx_n_s__catch_stderr, __pyx_k__catch_stderr, sizeof(__pyx_k__catch_stderr), 0, 0, 1, 1}, + {&__pyx_n_s__catch_stdout, __pyx_k__catch_stdout, sizeof(__pyx_k__catch_stdout), 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__cmp, __pyx_k__cmp, sizeof(__pyx_k__cmp), 0, 0, 1, 1}, + {&__pyx_n_s__cnext, __pyx_k__cnext, sizeof(__pyx_k__cnext), 0, 0, 1, 1}, + {&__pyx_n_s__cnt1, __pyx_k__cnt1, sizeof(__pyx_k__cnt1), 0, 0, 1, 1}, + {&__pyx_n_s__cnt2, __pyx_k__cnt2, sizeof(__pyx_k__cnt2), 0, 0, 1, 1}, + {&__pyx_n_s__cnt_anti, __pyx_k__cnt_anti, sizeof(__pyx_k__cnt_anti), 0, 0, 1, 1}, + {&__pyx_n_s__collections, __pyx_k__collections, sizeof(__pyx_k__collections), 0, 0, 1, 1}, + {&__pyx_n_s__consensus_quality, __pyx_k__consensus_quality, sizeof(__pyx_k__consensus_quality), 0, 0, 1, 1}, + {&__pyx_n_s__core, __pyx_k__core, sizeof(__pyx_k__core), 0, 0, 1, 1}, + {&__pyx_n_s__coverage, __pyx_k__coverage, sizeof(__pyx_k__coverage), 0, 0, 1, 1}, + {&__pyx_n_s__csamtools, __pyx_k__csamtools, sizeof(__pyx_k__csamtools), 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_n_s__data, __pyx_k__data, sizeof(__pyx_k__data), 0, 0, 1, 1}, + {&__pyx_n_s__data_len, __pyx_k__data_len, sizeof(__pyx_k__data_len), 0, 0, 1, 1}, + {&__pyx_n_s__dup, __pyx_k__dup, sizeof(__pyx_k__dup), 0, 0, 1, 1}, + {&__pyx_n_s__dup2, __pyx_k__dup2, sizeof(__pyx_k__dup2), 0, 0, 1, 1}, + {&__pyx_n_s__end, __pyx_k__end, sizeof(__pyx_k__end), 0, 0, 1, 1}, + {&__pyx_n_s__errmod, __pyx_k__errmod, sizeof(__pyx_k__errmod), 0, 0, 1, 1}, + {&__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__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__fd, __pyx_k__fd, sizeof(__pyx_k__fd), 0, 0, 1, 1}, + {&__pyx_n_s__fields, __pyx_k__fields, sizeof(__pyx_k__fields), 0, 0, 1, 1}, + {&__pyx_n_s__filename, __pyx_k__filename, sizeof(__pyx_k__filename), 0, 0, 1, 1}, + {&__pyx_n_s__fileno, __pyx_k__fileno, sizeof(__pyx_k__fileno), 0, 0, 1, 1}, + {&__pyx_n_s__first_allele, __pyx_k__first_allele, sizeof(__pyx_k__first_allele), 0, 0, 1, 1}, + {&__pyx_n_s__flag, __pyx_k__flag, sizeof(__pyx_k__flag), 0, 0, 1, 1}, + {&__pyx_n_s__flush, __pyx_k__flush, sizeof(__pyx_k__flush), 0, 0, 1, 1}, + {&__pyx_n_s__fp, __pyx_k__fp, sizeof(__pyx_k__fp), 0, 0, 1, 1}, + {&__pyx_n_s__genotype, __pyx_k__genotype, sizeof(__pyx_k__genotype), 0, 0, 1, 1}, + {&__pyx_n_s__getSequence, __pyx_k__getSequence, sizeof(__pyx_k__getSequence), 0, 0, 1, 1}, + {&__pyx_n_s__gettid, __pyx_k__gettid, sizeof(__pyx_k__gettid), 0, 0, 1, 1}, + {&__pyx_n_s__gt, __pyx_k__gt, sizeof(__pyx_k__gt), 0, 0, 1, 1}, + {&__pyx_n_s__hasReference, __pyx_k__hasReference, sizeof(__pyx_k__hasReference), 0, 0, 1, 1}, + {&__pyx_n_s__hash, __pyx_k__hash, sizeof(__pyx_k__hash), 0, 0, 1, 1}, + {&__pyx_n_s__header, __pyx_k__header, sizeof(__pyx_k__header), 0, 0, 1, 1}, + {&__pyx_n_s__het_rate, __pyx_k__het_rate, sizeof(__pyx_k__het_rate), 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__indel, __pyx_k__indel, sizeof(__pyx_k__indel), 0, 0, 1, 1}, + {&__pyx_n_s__indel1, __pyx_k__indel1, sizeof(__pyx_k__indel1), 0, 0, 1, 1}, + {&__pyx_n_s__indel2, __pyx_k__indel2, sizeof(__pyx_k__indel2), 0, 0, 1, 1}, + {&__pyx_n_s__index, __pyx_k__index, sizeof(__pyx_k__index), 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_paired, __pyx_k__is_paired, sizeof(__pyx_k__is_paired), 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__isbam, __pyx_k__isbam, sizeof(__pyx_k__isbam), 0, 0, 1, 1}, + {&__pyx_n_s__isize, __pyx_k__isize, sizeof(__pyx_k__isize), 0, 0, 1, 1}, + {&__pyx_n_s__isremote, __pyx_k__isremote, sizeof(__pyx_k__isremote), 0, 0, 1, 1}, + {&__pyx_n_s__iter, __pyx_k__iter, sizeof(__pyx_k__iter), 0, 0, 1, 1}, + {&__pyx_n_s__iterator_column, __pyx_k__iterator_column, sizeof(__pyx_k__iterator_column), 0, 0, 1, 1}, + {&__pyx_n_s__iterdata, __pyx_k__iterdata, sizeof(__pyx_k__iterdata), 0, 0, 1, 1}, + {&__pyx_n_s__join, __pyx_k__join, sizeof(__pyx_k__join), 0, 0, 1, 1}, + {&__pyx_n_s__l_aux, __pyx_k__l_aux, sizeof(__pyx_k__l_aux), 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__l_text, __pyx_k__l_text, sizeof(__pyx_k__l_text), 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__map, __pyx_k__map, sizeof(__pyx_k__map), 0, 0, 1, 1}, + {&__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_n_s__mask, __pyx_k__mask, sizeof(__pyx_k__mask), 0, 0, 1, 1}, + {&__pyx_n_s__mate, __pyx_k__mate, sizeof(__pyx_k__mate), 0, 0, 1, 1}, + {&__pyx_n_s__mate_is_unmapped, __pyx_k__mate_is_unmapped, sizeof(__pyx_k__mate_is_unmapped), 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__method, __pyx_k__method, sizeof(__pyx_k__method), 0, 0, 1, 1}, + {&__pyx_n_s__min_baseQ, __pyx_k__min_baseQ, sizeof(__pyx_k__min_baseQ), 0, 0, 1, 1}, + {&__pyx_n_s__mkstemp, __pyx_k__mkstemp, sizeof(__pyx_k__mkstemp), 0, 0, 1, 1}, + {&__pyx_n_s__mode, __pyx_k__mode, sizeof(__pyx_k__mode), 0, 0, 1, 1}, + {&__pyx_n_s__mpos, __pyx_k__mpos, sizeof(__pyx_k__mpos), 0, 0, 1, 1}, + {&__pyx_n_s__mtid, __pyx_k__mtid, sizeof(__pyx_k__mtid), 0, 0, 1, 1}, + {&__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__n_hap, __pyx_k__n_hap, sizeof(__pyx_k__n_hap), 0, 0, 1, 1}, + {&__pyx_n_s__n_haplotypes, __pyx_k__n_haplotypes, sizeof(__pyx_k__n_haplotypes), 0, 0, 1, 1}, + {&__pyx_n_s__n_plp, __pyx_k__n_plp, sizeof(__pyx_k__n_plp), 0, 0, 1, 1}, + {&__pyx_n_s__n_pu, __pyx_k__n_pu, sizeof(__pyx_k__n_pu), 0, 0, 1, 1}, + {&__pyx_n_s__n_targets, __pyx_k__n_targets, sizeof(__pyx_k__n_targets), 0, 0, 1, 1}, + {&__pyx_n_s__name, __pyx_k__name, sizeof(__pyx_k__name), 0, 0, 1, 1}, + {&__pyx_n_s__nextiter, __pyx_k__nextiter, sizeof(__pyx_k__nextiter), 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__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__options, __pyx_k__options, sizeof(__pyx_k__options), 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__path, __pyx_k__path, sizeof(__pyx_k__path), 0, 0, 1, 1}, + {&__pyx_n_s__pileup_iter, __pyx_k__pileup_iter, sizeof(__pyx_k__pileup_iter), 0, 0, 1, 1}, + {&__pyx_n_s__pileups, __pyx_k__pileups, sizeof(__pyx_k__pileups), 0, 0, 1, 1}, + {&__pyx_n_s__plp, __pyx_k__plp, sizeof(__pyx_k__plp), 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__q_cns, __pyx_k__q_cns, sizeof(__pyx_k__q_cns), 0, 0, 1, 1}, + {&__pyx_n_s__q_indel, __pyx_k__q_indel, sizeof(__pyx_k__q_indel), 0, 0, 1, 1}, + {&__pyx_n_s__q_r, __pyx_k__q_r, sizeof(__pyx_k__q_r), 0, 0, 1, 1}, + {&__pyx_n_s__q_ref, __pyx_k__q_ref, sizeof(__pyx_k__q_ref), 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__r, __pyx_k__r, sizeof(__pyx_k__r), 0, 0, 1, 1}, + {&__pyx_n_s__r_indel, __pyx_k__r_indel, sizeof(__pyx_k__r_indel), 0, 0, 1, 1}, + {&__pyx_n_s__range, __pyx_k__range, sizeof(__pyx_k__range), 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_n_s__readAndRelease, __pyx_k__readAndRelease, sizeof(__pyx_k__readAndRelease), 0, 0, 1, 1}, + {&__pyx_n_s__readlines, __pyx_k__readlines, sizeof(__pyx_k__readlines), 0, 0, 1, 1}, + {&__pyx_n_s__reads_diff, __pyx_k__reads_diff, sizeof(__pyx_k__reads_diff), 0, 0, 1, 1}, + {&__pyx_n_s__reads_first, __pyx_k__reads_first, sizeof(__pyx_k__reads_first), 0, 0, 1, 1}, + {&__pyx_n_s__reads_second, __pyx_k__reads_second, sizeof(__pyx_k__reads_second), 0, 0, 1, 1}, + {&__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__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__region, __pyx_k__region, sizeof(__pyx_k__region), 0, 0, 1, 1}, + {&__pyx_n_s__release, __pyx_k__release, sizeof(__pyx_k__release), 0, 0, 1, 1}, + {&__pyx_n_s__remove, __pyx_k__remove, sizeof(__pyx_k__remove), 0, 0, 1, 1}, + {&__pyx_n_s__reopen, __pyx_k__reopen, sizeof(__pyx_k__reopen), 0, 0, 1, 1}, + {&__pyx_n_s__reset, __pyx_k__reset, sizeof(__pyx_k__reset), 0, 0, 1, 1}, + {&__pyx_n_s__restore, __pyx_k__restore, sizeof(__pyx_k__restore), 0, 0, 1, 1}, + {&__pyx_n_s__retval, __pyx_k__retval, sizeof(__pyx_k__retval), 0, 0, 1, 1}, + {&__pyx_n_s__rg2lib, __pyx_k__rg2lib, sizeof(__pyx_k__rg2lib), 0, 0, 1, 1}, + {&__pyx_n_s__rname, __pyx_k__rname, sizeof(__pyx_k__rname), 0, 0, 1, 1}, + {&__pyx_n_s__rowiter, __pyx_k__rowiter, sizeof(__pyx_k__rowiter), 0, 0, 1, 1}, + {&__pyx_n_s__s, __pyx_k__s, sizeof(__pyx_k__s), 0, 0, 1, 1}, + {&__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__second_allele, __pyx_k__second_allele, sizeof(__pyx_k__second_allele), 0, 0, 1, 1}, + {&__pyx_n_s__self, __pyx_k__self, sizeof(__pyx_k__self), 0, 0, 1, 1}, + {&__pyx_n_s__seq, __pyx_k__seq, sizeof(__pyx_k__seq), 0, 0, 1, 1}, + {&__pyx_n_s__seq_len, __pyx_k__seq_len, sizeof(__pyx_k__seq_len), 0, 0, 1, 1}, + {&__pyx_n_s__setdevice, __pyx_k__setdevice, sizeof(__pyx_k__setdevice), 0, 0, 1, 1}, + {&__pyx_n_s__setfd, __pyx_k__setfd, sizeof(__pyx_k__setfd), 0, 0, 1, 1}, + {&__pyx_n_s__setfile, __pyx_k__setfile, sizeof(__pyx_k__setfile), 0, 0, 1, 1}, + {&__pyx_n_s__setupIteratorData, __pyx_k__setupIteratorData, sizeof(__pyx_k__setupIteratorData), 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__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_n_s__startswith, __pyx_k__startswith, sizeof(__pyx_k__startswith), 0, 0, 1, 1}, + {&__pyx_n_s__stderr, __pyx_k__stderr, sizeof(__pyx_k__stderr), 0, 0, 1, 1}, + {&__pyx_n_s__stderr_f, __pyx_k__stderr_f, sizeof(__pyx_k__stderr_f), 0, 0, 1, 1}, + {&__pyx_n_s__stderr_h, __pyx_k__stderr_h, sizeof(__pyx_k__stderr_h), 0, 0, 1, 1}, + {&__pyx_n_s__stderr_save, __pyx_k__stderr_save, sizeof(__pyx_k__stderr_save), 0, 0, 1, 1}, + {&__pyx_n_s__stdout, __pyx_k__stdout, sizeof(__pyx_k__stdout), 0, 0, 1, 1}, + {&__pyx_n_s__stepper, __pyx_k__stepper, sizeof(__pyx_k__stepper), 0, 0, 1, 1}, + {&__pyx_n_s__streams, __pyx_k__streams, sizeof(__pyx_k__streams), 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__tags, __pyx_k__tags, sizeof(__pyx_k__tags), 0, 0, 1, 1}, + {&__pyx_n_s__target_len, __pyx_k__target_len, sizeof(__pyx_k__target_len), 0, 0, 1, 1}, + {&__pyx_n_s__target_name, __pyx_k__target_name, sizeof(__pyx_k__target_name), 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__text, __pyx_k__text, sizeof(__pyx_k__text), 0, 0, 1, 1}, + {&__pyx_n_s__theta, __pyx_k__theta, sizeof(__pyx_k__theta), 0, 0, 1, 1}, + {&__pyx_n_s__tid, __pyx_k__tid, sizeof(__pyx_k__tid), 0, 0, 1, 1}, + {&__pyx_n_s__traceback, __pyx_k__traceback, sizeof(__pyx_k__traceback), 0, 0, 1, 1}, + {&__pyx_n_s__types, __pyx_k__types, sizeof(__pyx_k__types), 0, 0, 1, 1}, + {&__pyx_n_s__until_eof, __pyx_k__until_eof, sizeof(__pyx_k__until_eof), 0, 0, 1, 1}, + {&__pyx_n_s__view, __pyx_k__view, sizeof(__pyx_k__view), 0, 0, 1, 1}, + {&__pyx_n_s__w, __pyx_k__w, sizeof(__pyx_k__w), 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__x, __pyx_k__x, sizeof(__pyx_k__x), 0, 0, 1, 1}, + {0, 0, 0, 0, 0, 0, 0} +}; +static int __Pyx_InitCachedBuiltins(void) { + __pyx_builtin_open = __Pyx_GetName(__pyx_b, __pyx_n_s__open); if (!__pyx_builtin_open) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_object = __Pyx_GetName(__pyx_b, __pyx_n_s__object); if (!__pyx_builtin_object) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_map = __Pyx_GetName(__pyx_b, __pyx_n_s__map); if (!__pyx_builtin_map) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_ValueError = __Pyx_GetName(__pyx_b, __pyx_n_s__ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_IOError = __Pyx_GetName(__pyx_b, __pyx_n_s__IOError); if (!__pyx_builtin_IOError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_NotImplementedError = __Pyx_GetName(__pyx_b, __pyx_n_s__NotImplementedError); if (!__pyx_builtin_NotImplementedError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_KeyError = __Pyx_GetName(__pyx_b, __pyx_n_s__KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1074; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_StopIteration = __Pyx_GetName(__pyx_b, __pyx_n_s__StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1120; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_cmp = __Pyx_GetName(__pyx_b, __pyx_n_s__cmp); if (!__pyx_builtin_cmp) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_chr = __Pyx_GetName(__pyx_b, __pyx_n_s__chr); if (!__pyx_builtin_chr) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2337; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_TypeError = __Pyx_GetName(__pyx_b, __pyx_n_s__TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2414; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_range = __Pyx_GetName(__pyx_b, __pyx_n_s__range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2713; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + return 0; + __pyx_L1_error:; + return -1; +} + +static int __Pyx_InitCachedConstants(void) { + __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants"); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":249 + * def __len__(self): + * if self.fastafile == NULL: + * raise ValueError( "calling len() on closed file" ) # <<<<<<<<<<<<<< + * + * return faidx_fetch_nseq(self.fastafile) + */ + __pyx_k_tuple_4 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_4)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_3)); + PyTuple_SET_ITEM(__pyx_k_tuple_4, 0, ((PyObject *)__pyx_kp_s_3)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_3)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_4)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":281 + * '''number of :term:`filename` associated with this object.''' + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * return self._filename + * + */ + __pyx_k_tuple_7 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 281; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_7)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_7, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_7)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":306 + * + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * cdef int length + */ + __pyx_k_tuple_8 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 306; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_8)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_8, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_8)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":312 + * + * if not region: + * if reference is None: raise ValueError( 'no sequence/region supplied.' ) # <<<<<<<<<<<<<< + * if start is None: start = 0 + * if end is None: end = max_pos -1 + */ + __pyx_k_tuple_10 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 312; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_10)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_9)); + PyTuple_SET_ITEM(__pyx_k_tuple_10, 0, ((PyObject *)__pyx_kp_s_9)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_9)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_10)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":555 + * self.samfile = samopen( filename, mode, NULL ) + * if self.samfile == NULL: + * raise ValueError( "could not open file - is it SAM/BAM format?") # <<<<<<<<<<<<<< + * + * if self.samfile.header == NULL: + */ + __pyx_k_tuple_24 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_24)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_23)); + PyTuple_SET_ITEM(__pyx_k_tuple_24, 0, ((PyObject *)__pyx_kp_s_23)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_23)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_24)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":558 + * + * if self.samfile.header == NULL: + * raise ValueError( "could not open file - is it SAM/BAM format?") # <<<<<<<<<<<<<< + * + * if self.samfile.header.n_targets == 0: + */ + __pyx_k_tuple_25 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_25)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_23)); + PyTuple_SET_ITEM(__pyx_k_tuple_25, 0, ((PyObject *)__pyx_kp_s_23)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_23)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_25)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":561 + * + * if self.samfile.header.n_targets == 0: + * raise ValueError( "could not open file - is it SAM/BAM format?") # <<<<<<<<<<<<<< + * + * if self.samfile == NULL: + */ + __pyx_k_tuple_26 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_26)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_23)); + PyTuple_SET_ITEM(__pyx_k_tuple_26, 0, ((PyObject *)__pyx_kp_s_23)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_23)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_26)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":585 + * ''' + * 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.samfile.header.n_targets: + * raise ValueError( "tid out of range 0<=tid<%i" % self.samfile.header.n_targets ) + */ + __pyx_k_tuple_29 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_29)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_29)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_29, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_29)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":596 + * returns -1 if reference is not known. + * ''' + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * return pysam_reference2tid( self.samfile.header, reference ) + * + */ + __pyx_k_tuple_31 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_31)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_31, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_31)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":634 + * if len(parts) >= 3: rend = int(parts[2]) + * + * if not reference: return 0, 0, 0, 0 # <<<<<<<<<<<<<< + * + * rtid = self.gettid( reference ) + */ + __pyx_k_tuple_33 = PyTuple_New(4); if (unlikely(!__pyx_k_tuple_33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 634; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_33)); + __Pyx_INCREF(__pyx_int_0); + PyTuple_SET_ITEM(__pyx_k_tuple_33, 0, __pyx_int_0); + __Pyx_GIVEREF(__pyx_int_0); + __Pyx_INCREF(__pyx_int_0); + PyTuple_SET_ITEM(__pyx_k_tuple_33, 1, __pyx_int_0); + __Pyx_GIVEREF(__pyx_int_0); + __Pyx_INCREF(__pyx_int_0); + PyTuple_SET_ITEM(__pyx_k_tuple_33, 2, __pyx_int_0); + __Pyx_GIVEREF(__pyx_int_0); + __Pyx_INCREF(__pyx_int_0); + PyTuple_SET_ITEM(__pyx_k_tuple_33, 3, __pyx_int_0); + __Pyx_GIVEREF(__pyx_int_0); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_33)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":650 + * + * 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_k_tuple_36 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_36)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_36, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_36)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":652 + * raise ValueError( "I/O operation on closed file" ) + * if not self.isbam: + * raise NotImplementedError("seek only available in bam files") # <<<<<<<<<<<<<< + * return bam_seek( self.samfile.x.bam, offset, where ) + * + */ + __pyx_k_tuple_38 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_38)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_38)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_37)); + PyTuple_SET_ITEM(__pyx_k_tuple_38, 0, ((PyObject *)__pyx_kp_s_37)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_37)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_38)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":660 + * ''' + * 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_k_tuple_39 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_39)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 660; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_39)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_39, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_39)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":662 + * raise ValueError( "I/O operation on closed file" ) + * if not self.isbam: + * raise NotImplementedError("seek only available in bam files") # <<<<<<<<<<<<<< + * + * return bam_tell( self.samfile.x.bam ) + */ + __pyx_k_tuple_40 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_40)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 662; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_40)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_37)); + PyTuple_SET_ITEM(__pyx_k_tuple_40, 0, ((PyObject *)__pyx_kp_s_37)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_37)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_40)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":696 + * + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * has_coord, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + */ + __pyx_k_tuple_42 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_42)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_42)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_42, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_42)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":702 + * if self.isbam: + * if not until_eof and not self._hasIndex() and not self.isremote: + * raise ValueError( "fetch called on bamfile without index" ) # <<<<<<<<<<<<<< + * + * if callback: + */ + __pyx_k_tuple_44 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_44)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_44)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_43)); + PyTuple_SET_ITEM(__pyx_k_tuple_44, 0, ((PyObject *)__pyx_kp_s_43)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_43)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_44)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":705 + * + * if callback: + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) # <<<<<<<<<<<<<< + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) + * return bam_fetch(self.samfile.x.bam, + */ + __pyx_k_tuple_46 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_46)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_46)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_45)); + PyTuple_SET_ITEM(__pyx_k_tuple_46, 0, ((PyObject *)__pyx_kp_s_45)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_45)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_46)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":706 + * if callback: + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) # <<<<<<<<<<<<<< + * return bam_fetch(self.samfile.x.bam, + * self.index, + */ + __pyx_k_tuple_48 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_48)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_48)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_47)); + PyTuple_SET_ITEM(__pyx_k_tuple_48, 0, ((PyObject *)__pyx_kp_s_47)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_47)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_48)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":726 + * # this happens if a bamfile is opened with mode 'r' + * if self.samfile.header.n_targets == 0: + * raise ValueError( "fetch called for samfile without header") # <<<<<<<<<<<<<< + * + * if region != None: + */ + __pyx_k_tuple_50 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_50)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_50)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_49)); + PyTuple_SET_ITEM(__pyx_k_tuple_50, 0, ((PyObject *)__pyx_kp_s_49)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_49)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_50)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":729 + * + * if region != None: + * raise ValueError ("fetch for a region is not available for sam files" ) # <<<<<<<<<<<<<< + * if callback: + * raise NotImplementedError( "callback not implemented yet" ) + */ + __pyx_k_tuple_52 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_52)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_52)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_51)); + PyTuple_SET_ITEM(__pyx_k_tuple_52, 0, ((PyObject *)__pyx_kp_s_51)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_51)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_52)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":731 + * raise ValueError ("fetch for a region is not available for sam files" ) + * if callback: + * raise NotImplementedError( "callback not implemented yet" ) # <<<<<<<<<<<<<< + * else: + * return IteratorRowAll( self ) + */ + __pyx_k_tuple_54 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_54)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 731; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_54)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_53)); + PyTuple_SET_ITEM(__pyx_k_tuple_54, 0, ((PyObject *)__pyx_kp_s_53)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_53)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_54)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":749 + * ''' + * if not read.is_paired: + * raise ValueError( "read is unpaired" ) # <<<<<<<<<<<<<< + * if read.mate_is_unmapped: + * raise ValueError( "mate is unmapped" ) + */ + __pyx_k_tuple_56 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_56)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 749; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_56)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_55)); + PyTuple_SET_ITEM(__pyx_k_tuple_56, 0, ((PyObject *)__pyx_kp_s_55)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_55)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_56)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":751 + * raise ValueError( "read is unpaired" ) + * if read.mate_is_unmapped: + * raise ValueError( "mate is unmapped" ) # <<<<<<<<<<<<<< + * + * cdef MateData mate_data + */ + __pyx_k_tuple_58 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_58)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 751; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_58)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_57)); + PyTuple_SET_ITEM(__pyx_k_tuple_58, 0, ((PyObject *)__pyx_kp_s_57)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_57)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_58)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":770 + * + * if mate_data.mate == NULL: + * raise ValueError( "mate not found" ) # <<<<<<<<<<<<<< + * + * cdef AlignedRead dest = AlignedRead.__new__(AlignedRead) + */ + __pyx_k_tuple_60 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_60)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_60)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_59)); + PyTuple_SET_ITEM(__pyx_k_tuple_60, 0, ((PyObject *)__pyx_kp_s_59)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_59)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_60)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":795 + * + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * region, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + */ + __pyx_k_tuple_62 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_62)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_62)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_62, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_62)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":804 + * if self.isbam: + * if not until_eof and not self._hasIndex() and not self.isremote: + * raise ValueError( "fetch called on bamfile without index" ) # <<<<<<<<<<<<<< + * + * if not region: + */ + __pyx_k_tuple_63 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_63)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_63)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_43)); + PyTuple_SET_ITEM(__pyx_k_tuple_63, 0, ((PyObject *)__pyx_kp_s_43)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_43)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_63)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":807 + * + * if not region: + * raise ValueError( "counting functionality requires a region/reference" ) # <<<<<<<<<<<<<< + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) + * bam_fetch(self.samfile.x.bam, + */ + __pyx_k_tuple_65 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_65)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_65)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_64)); + PyTuple_SET_ITEM(__pyx_k_tuple_65, 0, ((PyObject *)__pyx_kp_s_64)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_64)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_65)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":808 + * if not region: + * raise ValueError( "counting functionality requires a region/reference" ) + * if not self._hasIndex(): raise ValueError( "no index available for fetch" ) # <<<<<<<<<<<<<< + * bam_fetch(self.samfile.x.bam, + * self.index, + */ + __pyx_k_tuple_66 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_66)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 808; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_66)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_47)); + PyTuple_SET_ITEM(__pyx_k_tuple_66, 0, ((PyObject *)__pyx_kp_s_47)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_47)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_66)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":818 + * return counter + * else: + * raise ValueError ("count for a region is not available for sam files" ) # <<<<<<<<<<<<<< + * + * + */ + __pyx_k_tuple_68 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_68)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_68)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_67)); + PyTuple_SET_ITEM(__pyx_k_tuple_68, 0, ((PyObject *)__pyx_kp_s_67)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_67)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_68)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":871 + * + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * has_coord, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + */ + __pyx_k_tuple_69 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_69)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_69)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_69, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_69)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":876 + * + * if self.isbam: + * if not self._hasIndex(): raise ValueError( "no index available for pileup" ) # <<<<<<<<<<<<<< + * + * if callback: + */ + __pyx_k_tuple_71 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_71)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 876; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_71)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_70)); + PyTuple_SET_ITEM(__pyx_k_tuple_71, 0, ((PyObject *)__pyx_kp_s_70)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_70)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_71)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":879 + * + * if callback: + * if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) # <<<<<<<<<<<<<< + * + * buf = bam_plbuf_init( pileup_callback, callback ) + */ + __pyx_k_tuple_72 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_72)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_72)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_45)); + PyTuple_SET_ITEM(__pyx_k_tuple_72, 0, ((PyObject *)__pyx_kp_s_45)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_45)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_72)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":900 + * + * else: + * raise NotImplementedError( "pileup of samfiles not implemented yet" ) # <<<<<<<<<<<<<< + * + * def close( self ): + */ + __pyx_k_tuple_74 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_74)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_74)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_73)); + PyTuple_SET_ITEM(__pyx_k_tuple_74, 0, ((PyObject *)__pyx_kp_s_73)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_73)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_74)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":925 + * ''' + * if not self._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * return samwrite( self.samfile, read._delegate ) + */ + __pyx_k_tuple_75 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_75)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 925; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_75)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_75, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_75)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":944 + * '''number of :term:`filename` associated with this object.''' + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * return self._filename + * + */ + __pyx_k_tuple_76 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_76)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_76)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_76, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_76)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":950 + * '''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.samfile.header.n_targets + * + */ + __pyx_k_tuple_77 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_77)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_77)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_77, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_77)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":956 + * """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.samfile.header.n_targets: + */ + __pyx_k_tuple_78 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_78)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_78)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_78, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_78)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":967 + * """ + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * t = [] + * for x from 0 <= x < self.samfile.header.n_targets: + */ + __pyx_k_tuple_79 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_79)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 967; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_79)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_79, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_79)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":976 + * '''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 PyString_FromStringAndSize(self.samfile.header.text, self.samfile.header.l_text) + * + */ + __pyx_k_tuple_80 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_80)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_80)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_80, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_80)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":984 + * ''' + * def __get__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * result = {} + */ + __pyx_k_tuple_81 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_81)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_81)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_81, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_81)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":991 + * # 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_k_tuple_82 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_82)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_82)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_2)); + PyTuple_SET_ITEM(__pyx_k_tuple_82, 0, ((PyObject *)__pyx_kp_s_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_82)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":993 + * 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_k_tuple_84 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_84)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_84)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_83)); + PyTuple_SET_ITEM(__pyx_k_tuple_84, 0, ((PyObject *)__pyx_kp_s_83)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_83)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_84)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":994 + * 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_k_tuple_86 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_86)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_86)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_1)); + PyTuple_SET_ITEM(__pyx_k_tuple_86, 0, ((PyObject *)__pyx_kp_s_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_86)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1007 + * x = {} + * for field in fields[1:]: + * key, value = field.split(":",1) # <<<<<<<<<<<<<< + * if key not in VALID_HEADER_FIELDS[record]: + * raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) ) + */ + __pyx_k_tuple_89 = PyTuple_New(2); if (unlikely(!__pyx_k_tuple_89)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_89)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_88)); + PyTuple_SET_ITEM(__pyx_k_tuple_89, 0, ((PyObject *)__pyx_kp_s_88)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_88)); + __Pyx_INCREF(__pyx_int_1); + PyTuple_SET_ITEM(__pyx_k_tuple_89, 1, __pyx_int_1); + __Pyx_GIVEREF(__pyx_int_1); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_89)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1098 + * ############################################################### + * def __iter__(self): + * if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * return self + * + */ + __pyx_k_tuple_96 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_96)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1098; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_96)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_96, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_96)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1173 + * + * if not samfile._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * if not samfile._hasIndex(): + */ + __pyx_k_tuple_97 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_97)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1173; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_97)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_97, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_97)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1176 + * + * if not samfile._hasIndex(): + * raise ValueError( "no index available for iteration" ) # <<<<<<<<<<<<<< + * + * # makes sure that samfile stays alive as long as the + */ + __pyx_k_tuple_99 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_99)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1176; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_99)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_98)); + PyTuple_SET_ITEM(__pyx_k_tuple_99, 0, ((PyObject *)__pyx_kp_s_98)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_98)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_99)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1240 + * + * if not samfile._isOpen(): + * raise ValueError( "I/O operation on closed file" ) # <<<<<<<<<<<<<< + * + * if samfile.isbam: mode = "rb" + */ + __pyx_k_tuple_100 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_100)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1240; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_100)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_6)); + PyTuple_SET_ITEM(__pyx_k_tuple_100, 0, ((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_100)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1292 + * def __cinit__(self, Samfile samfile): + * assert samfile._isOpen() + * if not samfile._hasIndex(): raise ValueError("no index available for fetch") # <<<<<<<<<<<<<< + * self.samfile = samfile + * self.tid = -1 + */ + __pyx_k_tuple_101 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_101)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_101)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_47)); + PyTuple_SET_ITEM(__pyx_k_tuple_101, 0, ((PyObject *)__pyx_kp_s_47)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_47)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_101)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1586 + * self.cnext() + * if self.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.plp == NULL: + */ + __pyx_k_tuple_106 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_106)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1586; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_106)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_105)); + PyTuple_SET_ITEM(__pyx_k_tuple_106, 0, ((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_106)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1618 + * + * if self.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * # return result, if within same reference + */ + __pyx_k_tuple_107 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_107)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1618; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_107)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_105)); + PyTuple_SET_ITEM(__pyx_k_tuple_107, 0, ((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_107)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2139 + * size = struct.calcsize(fmt) + * if offset + size > max_size: + * raise NotImplementedError("tags field too large") # <<<<<<<<<<<<<< + * + * struct.pack_into( fmt, + */ + __pyx_k_tuple_123 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_123)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2139; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_123)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_122)); + PyTuple_SET_ITEM(__pyx_k_tuple_123, 0, ((PyObject *)__pyx_kp_s_122)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_122)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_123)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2414 + * + * def __init__(self): + * raise TypeError("This class cannot be instantiated from Python") # <<<<<<<<<<<<<< + * + * def __str__(self): + */ + __pyx_k_tuple_147 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_147)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2414; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_147)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_146)); + PyTuple_SET_ITEM(__pyx_k_tuple_147, 0, ((PyObject *)__pyx_kp_s_146)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_146)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_147)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2458 + * + * def __init__(self): + * raise TypeError("This class cannot be instantiated from Python") # <<<<<<<<<<<<<< + * + * def __str__(self): + */ + __pyx_k_tuple_148 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_148)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2458; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_148)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_146)); + PyTuple_SET_ITEM(__pyx_k_tuple_148, 0, ((PyObject *)__pyx_kp_s_146)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_146)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_148)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2570 + * # recover. Thus redirect output to file with -o option. + * if method == "view": + * if "-o" in args: raise ValueError("option -o is forbidden in samtools view") # <<<<<<<<<<<<<< + * args = ( "-o", stdout_f ) + args + * + */ + __pyx_k_tuple_154 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_154)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2570; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_154)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_153)); + PyTuple_SET_ITEM(__pyx_k_tuple_154, 0, ((PyObject *)__pyx_kp_s_153)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_153)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_154)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2766 + * + * if self.iter.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.iter.plp == NULL: + */ + __pyx_k_tuple_160 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_160)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2766; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_160)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_105)); + PyTuple_SET_ITEM(__pyx_k_tuple_160, 0, ((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_160)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2845 + * + * if self.iter.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.iter.plp == NULL: + */ + __pyx_k_tuple_162 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_162)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2845; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_162)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_105)); + PyTuple_SET_ITEM(__pyx_k_tuple_162, 0, ((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_162)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2848 + * + * if self.iter.plp == NULL: + * raise ValueError( "no reads in region - no call" ) # <<<<<<<<<<<<<< + * + * if self.iter.pos == pos: break + */ + __pyx_k_tuple_164 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_164)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2848; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_164)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_163)); + PyTuple_SET_ITEM(__pyx_k_tuple_164, 0, ((PyObject *)__pyx_kp_s_163)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_163)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_164)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3110 + * + * if self.iter.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.iter.plp == NULL: + */ + __pyx_k_tuple_167 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_167)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3110; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_167)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_105)); + PyTuple_SET_ITEM(__pyx_k_tuple_167, 0, ((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_167)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3113 + * + * if self.iter.plp == NULL: + * raise ValueError( "no reads in region - no call" ) # <<<<<<<<<<<<<< + * + * if self.iter.pos == pos: break + */ + __pyx_k_tuple_168 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_168)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3113; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_168)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_163)); + PyTuple_SET_ITEM(__pyx_k_tuple_168, 0, ((PyObject *)__pyx_kp_s_163)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_163)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_168)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3150 + * + * if self.iter.n_plp < 0: + * raise ValueError("error during iteration" ) # <<<<<<<<<<<<<< + * + * if self.iter.plp == NULL: + */ + __pyx_k_tuple_169 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_169)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3150; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_169)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_105)); + PyTuple_SET_ITEM(__pyx_k_tuple_169, 0, ((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_105)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_169)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":56 + * + * # redirect stderr to 0 + * _logfile = open("/dev/null", "w") # <<<<<<<<<<<<<< + * pysam_set_stderr( PyFile_AsFile( _logfile ) ) + * + */ + __pyx_k_tuple_229 = PyTuple_New(2); if (unlikely(!__pyx_k_tuple_229)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_229)); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_228)); + PyTuple_SET_ITEM(__pyx_k_tuple_229, 0, ((PyObject *)__pyx_kp_s_228)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_228)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__w)); + PyTuple_SET_ITEM(__pyx_k_tuple_229, 1, ((PyObject *)__pyx_n_s__w)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__w)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_229)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":202 + * + * # order of records within sam headers + * VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO" ) # <<<<<<<<<<<<<< + * + * # type conversions within sam header records + */ + __pyx_k_tuple_232 = PyTuple_New(5); if (unlikely(!__pyx_k_tuple_232)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 202; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_232)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__HD)); + PyTuple_SET_ITEM(__pyx_k_tuple_232, 0, ((PyObject *)__pyx_n_s__HD)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__HD)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__SQ)); + PyTuple_SET_ITEM(__pyx_k_tuple_232, 1, ((PyObject *)__pyx_n_s__SQ)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__SQ)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__RG)); + PyTuple_SET_ITEM(__pyx_k_tuple_232, 2, ((PyObject *)__pyx_n_s__RG)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__RG)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__PG)); + PyTuple_SET_ITEM(__pyx_k_tuple_232, 3, ((PyObject *)__pyx_n_s__PG)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__PG)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__CO)); + PyTuple_SET_ITEM(__pyx_k_tuple_232, 4, ((PyObject *)__pyx_n_s__CO)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__CO)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_232)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":211 + * + * # 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" ), + */ + __pyx_k_tuple_233 = PyTuple_New(3); if (unlikely(!__pyx_k_tuple_233)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_233)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__VN)); + PyTuple_SET_ITEM(__pyx_k_tuple_233, 0, ((PyObject *)__pyx_n_s__VN)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__VN)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__SO)); + PyTuple_SET_ITEM(__pyx_k_tuple_233, 1, ((PyObject *)__pyx_n_s__SO)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__SO)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__GO)); + PyTuple_SET_ITEM(__pyx_k_tuple_233, 2, ((PyObject *)__pyx_n_s__GO)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__GO)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_233)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":212 + * # 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" ), + * "PG" : ( "PN", "ID", "VN", "CL" ), } + */ + __pyx_k_tuple_234 = PyTuple_New(6); if (unlikely(!__pyx_k_tuple_234)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_234)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__SN)); + PyTuple_SET_ITEM(__pyx_k_tuple_234, 0, ((PyObject *)__pyx_n_s__SN)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__SN)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__LN)); + PyTuple_SET_ITEM(__pyx_k_tuple_234, 1, ((PyObject *)__pyx_n_s__LN)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__LN)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__AS)); + PyTuple_SET_ITEM(__pyx_k_tuple_234, 2, ((PyObject *)__pyx_n_s__AS)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__AS)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__M5)); + PyTuple_SET_ITEM(__pyx_k_tuple_234, 3, ((PyObject *)__pyx_n_s__M5)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__M5)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__UR)); + PyTuple_SET_ITEM(__pyx_k_tuple_234, 4, ((PyObject *)__pyx_n_s__UR)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__UR)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__SP)); + PyTuple_SET_ITEM(__pyx_k_tuple_234, 5, ((PyObject *)__pyx_n_s__SP)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__SP)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_234)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":213 + * VALID_HEADER_ORDER = { "HD" : ( "VN", "SO", "GO" ), + * "SQ" : ( "SN", "LN", "AS", "M5" , "UR" , "SP" ), + * "RG" : ( "ID", "SM", "LB", "DS" , "PU" , "PI" , "CN" , "DT", "PL" ), # <<<<<<<<<<<<<< + * "PG" : ( "PN", "ID", "VN", "CL" ), } + * + */ + __pyx_k_tuple_235 = PyTuple_New(9); if (unlikely(!__pyx_k_tuple_235)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_235)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__ID)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 0, ((PyObject *)__pyx_n_s__ID)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__ID)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__SM)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 1, ((PyObject *)__pyx_n_s__SM)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__SM)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__LB)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 2, ((PyObject *)__pyx_n_s__LB)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__LB)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__DS)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 3, ((PyObject *)__pyx_n_s__DS)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__DS)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__PU)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 4, ((PyObject *)__pyx_n_s__PU)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__PU)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__PI)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 5, ((PyObject *)__pyx_n_s__PI)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__PI)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__CN)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 6, ((PyObject *)__pyx_n_s__CN)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__CN)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__DT)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 7, ((PyObject *)__pyx_n_s__DT)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__DT)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__PL)); + PyTuple_SET_ITEM(__pyx_k_tuple_235, 8, ((PyObject *)__pyx_n_s__PL)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__PL)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_235)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":214 + * "SQ" : ( "SN", "LN", "AS", "M5" , "UR" , "SP" ), + * "RG" : ( "ID", "SM", "LB", "DS" , "PU" , "PI" , "CN" , "DT", "PL" ), + * "PG" : ( "PN", "ID", "VN", "CL" ), } # <<<<<<<<<<<<<< + * + * + */ + __pyx_k_tuple_236 = PyTuple_New(4); if (unlikely(!__pyx_k_tuple_236)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_k_tuple_236)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__PN)); + PyTuple_SET_ITEM(__pyx_k_tuple_236, 0, ((PyObject *)__pyx_n_s__PN)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__PN)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__ID)); + PyTuple_SET_ITEM(__pyx_k_tuple_236, 1, ((PyObject *)__pyx_n_s__ID)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__ID)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__VN)); + PyTuple_SET_ITEM(__pyx_k_tuple_236, 2, ((PyObject *)__pyx_n_s__VN)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__VN)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__CL)); + PyTuple_SET_ITEM(__pyx_k_tuple_236, 3, ((PyObject *)__pyx_n_s__CL)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__CL)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_236)); + __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 = 2; __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 = 2; __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 = 2; __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 = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_4 = PyInt_FromLong(4); if (unlikely(!__pyx_int_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_8 = PyInt_FromLong(8); if (unlikely(!__pyx_int_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_13 = PyInt_FromLong(13); if (unlikely(!__pyx_int_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_16 = PyInt_FromLong(16); if (unlikely(!__pyx_int_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_32 = PyInt_FromLong(32); if (unlikely(!__pyx_int_32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_40 = PyInt_FromLong(40); if (unlikely(!__pyx_int_40)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_60 = PyInt_FromLong(60); if (unlikely(!__pyx_int_60)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_64 = PyInt_FromLong(64); if (unlikely(!__pyx_int_64)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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 = 2; __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 = 2; __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 = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_512 = PyInt_FromLong(512); if (unlikely(!__pyx_int_512)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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 = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_0660 = PyInt_FromLong(0660); if (unlikely(!__pyx_int_0660)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_1024 = PyInt_FromLong(1024); if (unlikely(!__pyx_int_1024)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_65535 = PyInt_FromLong(65535); if (unlikely(!__pyx_int_65535)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_neg_32767 = PyInt_FromLong(-32767); if (unlikely(!__pyx_int_neg_32767)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_536870912 = PyInt_FromLong(536870912); if (unlikely(!__pyx_int_536870912)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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 = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_neg_2147483648 = PyInt_FromLong(-2147483648); if (unlikely(!__pyx_int_neg_2147483648)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + return 0; + __pyx_L1_error:; + return -1; +} + +#if PY_MAJOR_VERSION < 3 +PyMODINIT_FUNC initcsamtools(void); /*proto*/ +PyMODINIT_FUNC initcsamtools(void) +#else +PyMODINIT_FUNC PyInit_csamtools(void); /*proto*/ +PyMODINIT_FUNC PyInit_csamtools(void) +#endif +{ + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + #if CYTHON_REFNANNY + void* __pyx_refnanny = NULL; + __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_refnanny = __Pyx_RefNanny->SetupContext("PyMODINIT_FUNC PyInit_csamtools(void)", __LINE__, __FILE__); + #endif + __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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 = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + #ifdef __pyx_binding_PyCFunctionType_USED + if (__pyx_binding_PyCFunctionType_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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("csamtools"), __pyx_methods, 0, 0, PYTHON_API_VERSION); + #else + __pyx_m = PyModule_Create(&__pyx_moduledef); + #endif + if (!__pyx_m) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + #if PY_MAJOR_VERSION < 3 + Py_INCREF(__pyx_m); + #endif + __pyx_b = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME)); + if (!__pyx_b) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + if (__Pyx_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + /*--- Initialize various global constants etc. ---*/ + if (unlikely(__Pyx_InitGlobals() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_module_is_main_csamtools) { + if (__Pyx_SetAttrString(__pyx_m, "__name__", __pyx_n_s____main__) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + } + /*--- Builtin init code ---*/ + if (unlikely(__Pyx_InitCachedBuiltins() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + /*--- Constants init code ---*/ + if (unlikely(__Pyx_InitCachedConstants() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + /*--- Global init code ---*/ + /*--- Function export code ---*/ + /*--- Type init code ---*/ + __pyx_ptype_9csamtools_file = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "file", sizeof(PyFileObject), 0); if (unlikely(!__pyx_ptype_9csamtools_file)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyType_Ready(&__pyx_type_9csamtools_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1722; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_AlignedRead, "__str__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1722; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_11AlignedRead_2__str__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_11AlignedRead_2__str__.doc = __pyx_doc_9csamtools_11AlignedRead_2__str__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_11AlignedRead_2__str__; + } + } + if (__Pyx_SetAttrString(__pyx_m, "AlignedRead", (PyObject *)&__pyx_type_9csamtools_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1722; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_AlignedRead = &__pyx_type_9csamtools_AlignedRead; + if (PyType_Ready(&__pyx_type_9csamtools_PileupProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2391; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "PileupProxy", (PyObject *)&__pyx_type_9csamtools_PileupProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2391; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_PileupProxy = &__pyx_type_9csamtools_PileupProxy; + if (PyType_Ready(&__pyx_type_9csamtools_PileupRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2444; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "PileupRead", (PyObject *)&__pyx_type_9csamtools_PileupRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2444; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_PileupRead = &__pyx_type_9csamtools_PileupRead; + __pyx_vtabptr_9csamtools_Fastafile = &__pyx_vtable_9csamtools_Fastafile; + __pyx_vtable_9csamtools_Fastafile._fetch = (char *(*)(struct __pyx_obj_9csamtools_Fastafile *, char *, int, int, int *))__pyx_f_9csamtools_9Fastafile__fetch; + if (PyType_Ready(&__pyx_type_9csamtools_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetVtable(__pyx_type_9csamtools_Fastafile.tp_dict, __pyx_vtabptr_9csamtools_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "Fastafile", (PyObject *)&__pyx_type_9csamtools_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_Fastafile = &__pyx_type_9csamtools_Fastafile; + __pyx_vtabptr_9csamtools_Samfile = &__pyx_vtable_9csamtools_Samfile; + __pyx_vtable_9csamtools_Samfile._buildHeader = (bam_header_t *(*)(struct __pyx_obj_9csamtools_Samfile *, PyObject *))__pyx_f_9csamtools_7Samfile__buildHeader; + __pyx_vtable_9csamtools_Samfile.getCurrent = (bam1_t *(*)(struct __pyx_obj_9csamtools_Samfile *))__pyx_f_9csamtools_7Samfile_getCurrent; + __pyx_vtable_9csamtools_Samfile.cnext = (int (*)(struct __pyx_obj_9csamtools_Samfile *))__pyx_f_9csamtools_7Samfile_cnext; + if (PyType_Ready(&__pyx_type_9csamtools_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_Samfile, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_7Samfile_20__next__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_7Samfile_20__next__.doc = __pyx_doc_9csamtools_7Samfile_20__next__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_7Samfile_20__next__; + } + } + if (__Pyx_SetVtable(__pyx_type_9csamtools_Samfile.tp_dict, __pyx_vtabptr_9csamtools_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "Samfile", (PyObject *)&__pyx_type_9csamtools_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_Samfile = &__pyx_type_9csamtools_Samfile; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorRow) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1125; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IteratorRow", (PyObject *)&__pyx_type_9csamtools_IteratorRow) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1125; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorRow = &__pyx_type_9csamtools_IteratorRow; + __pyx_vtabptr_9csamtools_IteratorRowRegion = &__pyx_vtable_9csamtools_IteratorRowRegion; + __pyx_vtable_9csamtools_IteratorRowRegion.getCurrent = (bam1_t *(*)(struct __pyx_obj_9csamtools_IteratorRowRegion *))__pyx_f_9csamtools_17IteratorRowRegion_getCurrent; + __pyx_vtable_9csamtools_IteratorRowRegion.cnext = (int (*)(struct __pyx_obj_9csamtools_IteratorRowRegion *))__pyx_f_9csamtools_17IteratorRowRegion_cnext; + __pyx_type_9csamtools_IteratorRowRegion.tp_base = __pyx_ptype_9csamtools_IteratorRow; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_IteratorRowRegion, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_17IteratorRowRegion_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_17IteratorRowRegion_2__next__.doc = __pyx_doc_9csamtools_17IteratorRowRegion_2__next__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_17IteratorRowRegion_2__next__; + } + } + if (__Pyx_SetVtable(__pyx_type_9csamtools_IteratorRowRegion.tp_dict, __pyx_vtabptr_9csamtools_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IteratorRowRegion", (PyObject *)&__pyx_type_9csamtools_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorRowRegion = &__pyx_type_9csamtools_IteratorRowRegion; + __pyx_vtabptr_9csamtools_IteratorRowAll = &__pyx_vtable_9csamtools_IteratorRowAll; + __pyx_vtable_9csamtools_IteratorRowAll.getCurrent = (bam1_t *(*)(struct __pyx_obj_9csamtools_IteratorRowAll *))__pyx_f_9csamtools_14IteratorRowAll_getCurrent; + __pyx_vtable_9csamtools_IteratorRowAll.cnext = (int (*)(struct __pyx_obj_9csamtools_IteratorRowAll *))__pyx_f_9csamtools_14IteratorRowAll_cnext; + __pyx_type_9csamtools_IteratorRowAll.tp_base = __pyx_ptype_9csamtools_IteratorRow; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1224; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_IteratorRowAll, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1224; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_14IteratorRowAll_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_14IteratorRowAll_2__next__.doc = __pyx_doc_9csamtools_14IteratorRowAll_2__next__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_14IteratorRowAll_2__next__; + } + } + if (__Pyx_SetVtable(__pyx_type_9csamtools_IteratorRowAll.tp_dict, __pyx_vtabptr_9csamtools_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1224; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IteratorRowAll", (PyObject *)&__pyx_type_9csamtools_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1224; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorRowAll = &__pyx_type_9csamtools_IteratorRowAll; + __pyx_type_9csamtools_IteratorRowAllRefs.tp_base = __pyx_ptype_9csamtools_IteratorRow; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorRowAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1283; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_IteratorRowAllRefs, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1283; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_18IteratorRowAllRefs_3__next__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_18IteratorRowAllRefs_3__next__.doc = __pyx_doc_9csamtools_18IteratorRowAllRefs_3__next__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_18IteratorRowAllRefs_3__next__; + } + } + if (__Pyx_SetAttrString(__pyx_m, "IteratorRowAllRefs", (PyObject *)&__pyx_type_9csamtools_IteratorRowAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1283; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorRowAllRefs = &__pyx_type_9csamtools_IteratorRowAllRefs; + __pyx_vtabptr_9csamtools_IteratorColumn = &__pyx_vtable_9csamtools_IteratorColumn; + __pyx_vtable_9csamtools_IteratorColumn.cnext = (int (*)(struct __pyx_obj_9csamtools_IteratorColumn *))__pyx_f_9csamtools_14IteratorColumn_cnext; + __pyx_vtable_9csamtools_IteratorColumn.getSequence = (char *(*)(struct __pyx_obj_9csamtools_IteratorColumn *))__pyx_f_9csamtools_14IteratorColumn_getSequence; + __pyx_vtable_9csamtools_IteratorColumn.setMask = (PyObject *(*)(struct __pyx_obj_9csamtools_IteratorColumn *, PyObject *))__pyx_f_9csamtools_14IteratorColumn_setMask; + __pyx_vtable_9csamtools_IteratorColumn.setupIteratorData = (PyObject *(*)(struct __pyx_obj_9csamtools_IteratorColumn *, int, int, int, struct __pyx_opt_args_9csamtools_14IteratorColumn_setupIteratorData *__pyx_optional_args))__pyx_f_9csamtools_14IteratorColumn_setupIteratorData; + __pyx_vtable_9csamtools_IteratorColumn.reset = (PyObject *(*)(struct __pyx_obj_9csamtools_IteratorColumn *, PyObject *, PyObject *, PyObject *))__pyx_f_9csamtools_14IteratorColumn_reset; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1397; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetVtable(__pyx_type_9csamtools_IteratorColumn.tp_dict, __pyx_vtabptr_9csamtools_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1397; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IteratorColumn", (PyObject *)&__pyx_type_9csamtools_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1397; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorColumn = &__pyx_type_9csamtools_IteratorColumn; + __pyx_vtabptr_9csamtools_IteratorColumnRegion = &__pyx_vtable_9csamtools_IteratorColumnRegion; + __pyx_vtable_9csamtools_IteratorColumnRegion.__pyx_base = *__pyx_vtabptr_9csamtools_IteratorColumn; + __pyx_type_9csamtools_IteratorColumnRegion.tp_base = __pyx_ptype_9csamtools_IteratorColumn; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1567; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_IteratorColumnRegion, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1567; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_20IteratorColumnRegion_1__next__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_20IteratorColumnRegion_1__next__.doc = __pyx_doc_9csamtools_20IteratorColumnRegion_1__next__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_20IteratorColumnRegion_1__next__; + } + } + if (__Pyx_SetVtable(__pyx_type_9csamtools_IteratorColumnRegion.tp_dict, __pyx_vtabptr_9csamtools_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1567; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IteratorColumnRegion", (PyObject *)&__pyx_type_9csamtools_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1567; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorColumnRegion = &__pyx_type_9csamtools_IteratorColumnRegion; + __pyx_vtabptr_9csamtools_IteratorColumnAllRefs = &__pyx_vtable_9csamtools_IteratorColumnAllRefs; + __pyx_vtable_9csamtools_IteratorColumnAllRefs.__pyx_base = *__pyx_vtabptr_9csamtools_IteratorColumn; + __pyx_type_9csamtools_IteratorColumnAllRefs.tp_base = __pyx_ptype_9csamtools_IteratorColumn; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_IteratorColumnAllRefs, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_21IteratorColumnAllRefs_1__next__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_21IteratorColumnAllRefs_1__next__.doc = __pyx_doc_9csamtools_21IteratorColumnAllRefs_1__next__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_21IteratorColumnAllRefs_1__next__; + } + } + if (__Pyx_SetVtable(__pyx_type_9csamtools_IteratorColumnAllRefs.tp_dict, __pyx_vtabptr_9csamtools_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IteratorColumnAllRefs", (PyObject *)&__pyx_type_9csamtools_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1596; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorColumnAllRefs = &__pyx_type_9csamtools_IteratorColumnAllRefs; + if (PyType_Ready(&__pyx_type_9csamtools_SNPCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2605; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "SNPCall", (PyObject *)&__pyx_type_9csamtools_SNPCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2605; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_SNPCall = &__pyx_type_9csamtools_SNPCall; + __pyx_vtabptr_9csamtools_SNPCallerBase = &__pyx_vtable_9csamtools_SNPCallerBase; + __pyx_vtable_9csamtools_SNPCallerBase.__dump = (PyObject *(*)(struct __pyx_obj_9csamtools_SNPCallerBase *, glf1_t *, uint32_t, int))__pyx_f_9csamtools_13SNPCallerBase___dump; + if (PyType_Ready(&__pyx_type_9csamtools_SNPCallerBase) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2662; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetVtable(__pyx_type_9csamtools_SNPCallerBase.tp_dict, __pyx_vtabptr_9csamtools_SNPCallerBase) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2662; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "SNPCallerBase", (PyObject *)&__pyx_type_9csamtools_SNPCallerBase) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2662; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_SNPCallerBase = &__pyx_type_9csamtools_SNPCallerBase; + __pyx_vtabptr_9csamtools_IteratorSNPCalls = &__pyx_vtable_9csamtools_IteratorSNPCalls; + __pyx_vtable_9csamtools_IteratorSNPCalls.__pyx_base = *__pyx_vtabptr_9csamtools_SNPCallerBase; + __pyx_type_9csamtools_IteratorSNPCalls.tp_base = __pyx_ptype_9csamtools_SNPCallerBase; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorSNPCalls) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2735; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_IteratorSNPCalls, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2735; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_16IteratorSNPCalls_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_16IteratorSNPCalls_2__next__.doc = __pyx_doc_9csamtools_16IteratorSNPCalls_2__next__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_16IteratorSNPCalls_2__next__; + } + } + if (__Pyx_SetVtable(__pyx_type_9csamtools_IteratorSNPCalls.tp_dict, __pyx_vtabptr_9csamtools_IteratorSNPCalls) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2735; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IteratorSNPCalls", (PyObject *)&__pyx_type_9csamtools_IteratorSNPCalls) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2735; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorSNPCalls = &__pyx_type_9csamtools_IteratorSNPCalls; + __pyx_vtabptr_9csamtools_SNPCaller = &__pyx_vtable_9csamtools_SNPCaller; + __pyx_vtable_9csamtools_SNPCaller.__pyx_base = *__pyx_vtabptr_9csamtools_SNPCallerBase; + __pyx_type_9csamtools_SNPCaller.tp_base = __pyx_ptype_9csamtools_SNPCallerBase; + if (PyType_Ready(&__pyx_type_9csamtools_SNPCaller) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2810; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetVtable(__pyx_type_9csamtools_SNPCaller.tp_dict, __pyx_vtabptr_9csamtools_SNPCaller) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2810; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "SNPCaller", (PyObject *)&__pyx_type_9csamtools_SNPCaller) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2810; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_SNPCaller = &__pyx_type_9csamtools_SNPCaller; + if (PyType_Ready(&__pyx_type_9csamtools_IndelCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2892; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IndelCall", (PyObject *)&__pyx_type_9csamtools_IndelCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2892; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IndelCall = &__pyx_type_9csamtools_IndelCall; + if (PyType_Ready(&__pyx_type_9csamtools_IndelCallerBase) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2981; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IndelCallerBase", (PyObject *)&__pyx_type_9csamtools_IndelCallerBase) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2981; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IndelCallerBase = &__pyx_type_9csamtools_IndelCallerBase; + __pyx_type_9csamtools_IndelCaller.tp_base = __pyx_ptype_9csamtools_IndelCallerBase; + if (PyType_Ready(&__pyx_type_9csamtools_IndelCaller) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3076; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "IndelCaller", (PyObject *)&__pyx_type_9csamtools_IndelCaller) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3076; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IndelCaller = &__pyx_type_9csamtools_IndelCaller; + __pyx_type_9csamtools_IteratorIndelCalls.tp_base = __pyx_ptype_9csamtools_IndelCallerBase; + if (PyType_Ready(&__pyx_type_9csamtools_IteratorIndelCalls) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3119; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + { + PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_9csamtools_IteratorIndelCalls, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3119; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) { + __pyx_wrapperbase_9csamtools_18IteratorIndelCalls_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base; + __pyx_wrapperbase_9csamtools_18IteratorIndelCalls_2__next__.doc = __pyx_doc_9csamtools_18IteratorIndelCalls_2__next__; + ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_9csamtools_18IteratorIndelCalls_2__next__; + } + } + if (__Pyx_SetAttrString(__pyx_m, "IteratorIndelCalls", (PyObject *)&__pyx_type_9csamtools_IteratorIndelCalls) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3119; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_9csamtools_IteratorIndelCalls = &__pyx_type_9csamtools_IteratorIndelCalls; + /*--- Type import code ---*/ + __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[2]; __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[3]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + /*--- Function import code ---*/ + /*--- Execution code ---*/ + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":4 + * # cython: profile=True + * # adds doc-strings for sphinx + * import tempfile, os, sys, types, struct, ctypes, collections, re # <<<<<<<<<<<<<< + * + * from cpython cimport PyString_FromStringAndSize, PyString_AS_STRING + */ + __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__tempfile), 0); 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 (PyObject_SetAttr(__pyx_m, __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; + __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__os), 0); 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 (PyObject_SetAttr(__pyx_m, __pyx_n_s__os, __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; + __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__sys), 0); 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 (PyObject_SetAttr(__pyx_m, __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; + __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__types), 0); 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 (PyObject_SetAttr(__pyx_m, __pyx_n_s__types, __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; + __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__struct), 0); 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 (PyObject_SetAttr(__pyx_m, __pyx_n_s__struct, __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; + __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__ctypes), 0); 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 (PyObject_SetAttr(__pyx_m, __pyx_n_s__ctypes, __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; + __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__collections), 0); 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 (PyObject_SetAttr(__pyx_m, __pyx_n_s__collections, __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; + __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__re), 0); 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 (PyObject_SetAttr(__pyx_m, __pyx_n_s__re, __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; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":52 + * ##################################################################### + * # hard-coded constants + * cdef char * bam_nt16_rev_table = "=ACMGRSVTWYHKDBN" # <<<<<<<<<<<<<< + * cdef int max_pos = 2 << 29 + * + */ + __pyx_v_9csamtools_bam_nt16_rev_table = __pyx_k_227; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":53 + * # hard-coded constants + * cdef char * bam_nt16_rev_table = "=ACMGRSVTWYHKDBN" + * cdef int max_pos = 2 << 29 # <<<<<<<<<<<<<< + * + * # redirect stderr to 0 + */ + __pyx_v_9csamtools_max_pos = 1073741824; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":56 + * + * # redirect stderr to 0 + * _logfile = open("/dev/null", "w") # <<<<<<<<<<<<<< + * pysam_set_stderr( PyFile_AsFile( _logfile ) ) + * + */ + __pyx_t_1 = PyObject_Call(__pyx_builtin_open, ((PyObject *)__pyx_k_tuple_229), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + if (PyObject_SetAttr(__pyx_m, __pyx_n_s___logfile, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":57 + * # redirect stderr to 0 + * _logfile = open("/dev/null", "w") + * pysam_set_stderr( PyFile_AsFile( _logfile ) ) # <<<<<<<<<<<<<< + * + * ##################################################################### + */ + __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s___logfile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + pysam_set_stderr(PyFile_AsFile(__pyx_t_1)); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":106 + * (f)(a) + * + * class PileupColumn(object): # <<<<<<<<<<<<<< + * '''A pileup column. A pileup column contains + * all the reads that map to a certain target base. + */ + __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":119 + * 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_2 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_12PileupColumn_0__str__, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 119; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s____str__, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 119; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":106 + * (f)(a) + * + * class PileupColumn(object): # <<<<<<<<<<<<<< + * '''A pileup column. A pileup column contains + * all the reads that map to a certain target base. + */ + __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + __Pyx_INCREF(__pyx_builtin_object); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_builtin_object); + __Pyx_GIVEREF(__pyx_builtin_object); + if (PyDict_SetItemString(((PyObject *)__pyx_t_1), "__doc__", ((PyObject *)__pyx_kp_s_230)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = __Pyx_CreateClass(((PyObject *)__pyx_t_2), ((PyObject *)__pyx_t_1), __pyx_n_s__PileupColumn, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + if (PyObject_SetAttr(__pyx_m, __pyx_n_s__PileupColumn, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":163 + * return 0 + * + * class StderrStore(): # <<<<<<<<<<<<<< + * ''' + * stderr is captured. + */ + __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":167 + * stderr is captured. + * ''' + * def __init__(self): # <<<<<<<<<<<<<< + * return + * self.stderr_h, self.stderr_f = tempfile.mkstemp() + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_11StderrStore_0__init__, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s____init__, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":173 + * self.stderr_save.setfd( self.stderr_h ) + * + * def readAndRelease( self ): # <<<<<<<<<<<<<< + * return [] + * self.stderr_save.restore() + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_11StderrStore_1readAndRelease, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 173; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s__readAndRelease, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 173; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":182 + * return lines + * + * def release(self): # <<<<<<<<<<<<<< + * return + * self.stderr_save.restore() + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_11StderrStore_2release, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s__release, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":188 + * os.remove( self.stderr_f ) + * + * def __del__(self): # <<<<<<<<<<<<<< + * self.release() + * + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_11StderrStore_3__del__, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s____del__, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":163 + * return 0 + * + * class StderrStore(): # <<<<<<<<<<<<<< + * ''' + * stderr is captured. + */ + if (PyDict_SetItemString(((PyObject *)__pyx_t_1), "__doc__", ((PyObject *)__pyx_kp_s_231)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = __Pyx_CreateClass(((PyObject *)__pyx_empty_tuple), ((PyObject *)__pyx_t_1), __pyx_n_s__StderrStore, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetAttr(__pyx_m, __pyx_n_s__StderrStore, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":195 + * ###################################################################### + * # 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 = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__HD), ((PyObject *)((PyObject*)(&PyDict_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":196 + * # valid types for sam headers + * VALID_HEADER_TYPES = { "HD" : dict, + * "SQ" : list, # <<<<<<<<<<<<<< + * "RG" : list, + * "PG" : list, + */ + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__SQ), ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":197 + * VALID_HEADER_TYPES = { "HD" : dict, + * "SQ" : list, + * "RG" : list, # <<<<<<<<<<<<<< + * "PG" : list, + * "CO" : list } + */ + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__RG), ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":198 + * "SQ" : list, + * "RG" : list, + * "PG" : list, # <<<<<<<<<<<<<< + * "CO" : list } + * + */ + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__PG), ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":199 + * "RG" : list, + * "PG" : list, + * "CO" : list } # <<<<<<<<<<<<<< + * + * # order of records within sam headers + */ + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__CO), ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyObject_SetAttr(__pyx_m, __pyx_n_s__VALID_HEADER_TYPES, ((PyObject *)__pyx_t_1)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":202 + * + * # order of records within sam headers + * VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO" ) # <<<<<<<<<<<<<< + * + * # type conversions within sam header records + */ + if (PyObject_SetAttr(__pyx_m, __pyx_n_s__VALID_HEADERS, ((PyObject *)__pyx_k_tuple_232)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 202; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":205 + * + * # 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, "UR" : str, "SP" : str }, + * "RG" : { "ID" : str, "SM" : str, "LB" : str, "DS" : str, "PU" : str, "PI" : str, "CN" : str, "DT" : str, "PL" : str, }, + */ + __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__VN), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__SO), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__GO), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__HD), ((PyObject *)__pyx_t_3)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":206 + * # 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, "UR" : str, "SP" : str }, # <<<<<<<<<<<<<< + * "RG" : { "ID" : str, "SM" : str, "LB" : str, "DS" : str, "PU" : str, "PI" : str, "CN" : str, "DT" : str, "PL" : str, }, + * "PG" : { "PN" : str, "ID" : str, "VN" : str, "CL" : str }, } + */ + __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__SN), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__LN), ((PyObject *)((PyObject*)(&PyInt_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__AS), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__M5), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__UR), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__SP), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__SQ), ((PyObject *)__pyx_t_3)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":207 + * VALID_HEADER_FIELDS = { "HD" : { "VN" : str, "SO" : str, "GO" : str }, + * "SQ" : { "SN" : str, "LN" : int, "AS" : str, "M5" : str, "UR" : str, "SP" : str }, + * "RG" : { "ID" : str, "SM" : str, "LB" : str, "DS" : str, "PU" : str, "PI" : str, "CN" : str, "DT" : str, "PL" : str, }, # <<<<<<<<<<<<<< + * "PG" : { "PN" : str, "ID" : str, "VN" : str, "CL" : str }, } + * + */ + __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__ID), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__SM), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__LB), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__DS), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__PU), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__PI), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__CN), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__DT), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__PL), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__RG), ((PyObject *)__pyx_t_3)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":208 + * "SQ" : { "SN" : str, "LN" : int, "AS" : str, "M5" : str, "UR" : str, "SP" : str }, + * "RG" : { "ID" : str, "SM" : str, "LB" : str, "DS" : str, "PU" : str, "PI" : str, "CN" : str, "DT" : str, "PL" : str, }, + * "PG" : { "PN" : str, "ID" : str, "VN" : str, "CL" : str }, } # <<<<<<<<<<<<<< + * + * # output order of fields within records + */ + __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__PN), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__ID), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__VN), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__CL), ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__PG), ((PyObject *)__pyx_t_3)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + if (PyObject_SetAttr(__pyx_m, __pyx_n_s__VALID_HEADER_FIELDS, ((PyObject *)__pyx_t_1)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":211 + * + * # 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" ), + */ + __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__HD), ((PyObject *)__pyx_k_tuple_233)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":212 + * # 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" ), + * "PG" : ( "PN", "ID", "VN", "CL" ), } + */ + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__SQ), ((PyObject *)__pyx_k_tuple_234)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":213 + * VALID_HEADER_ORDER = { "HD" : ( "VN", "SO", "GO" ), + * "SQ" : ( "SN", "LN", "AS", "M5" , "UR" , "SP" ), + * "RG" : ( "ID", "SM", "LB", "DS" , "PU" , "PI" , "CN" , "DT", "PL" ), # <<<<<<<<<<<<<< + * "PG" : ( "PN", "ID", "VN", "CL" ), } + * + */ + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__RG), ((PyObject *)__pyx_k_tuple_235)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":214 + * "SQ" : ( "SN", "LN", "AS", "M5" , "UR" , "SP" ), + * "RG" : ( "ID", "SM", "LB", "DS" , "PU" , "PI" , "CN" , "DT", "PL" ), + * "PG" : ( "PN", "ID", "VN", "CL" ), } # <<<<<<<<<<<<<< + * + * + */ + if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__PG), ((PyObject *)__pyx_k_tuple_236)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyObject_SetAttr(__pyx_m, __pyx_n_s__VALID_HEADER_ORDER, ((PyObject *)__pyx_t_1)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":672 + * region = None, + * callback = None, + * until_eof = False ): # <<<<<<<<<<<<<< + * ''' + * fetch aligned reads in a :term:`region` using 0-based indexing. The region is specified by + */ + __pyx_t_1 = __Pyx_PyBool_FromLong(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 672; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_k_41 = __pyx_t_1; + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":781 + * end = None, + * region = None, + * until_eof = False ): # <<<<<<<<<<<<<< + * '''*(reference = None, start = None, end = None, region = None, callback = None, until_eof = False)* + * + */ + __pyx_t_1 = __Pyx_PyBool_FromLong(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_k_61 = __pyx_t_1; + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":1573 + * int tid = 0, + * int start = 0, + * int end = max_pos, # <<<<<<<<<<<<<< + * **kwargs ): + * + */ + __pyx_k_104 = __pyx_v_9csamtools_max_pos; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2489 + * return self._level + * + * class Outs: # <<<<<<<<<<<<<< + * '''http://mail.python.org/pipermail/python-list/2000-June/038406.html''' + * def __init__(self, id = 1): + */ + __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2489; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2491 + * class Outs: + * '''http://mail.python.org/pipermail/python-list/2000-June/038406.html''' + * def __init__(self, id = 1): # <<<<<<<<<<<<<< + * self.streams = [] + * self.id = id + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_4Outs_0__init__, NULL, __pyx_n_s__csamtools); 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); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s____init__, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2491; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2495 + * self.id = id + * + * def setdevice(self, filename): # <<<<<<<<<<<<<< + * '''open an existing file, like "/dev/null"''' + * fd = os.open(filename, os.O_WRONLY) + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_4Outs_1setdevice, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2495; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s__setdevice, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2495; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2500 + * self.setfd(fd) + * + * def setfile(self, filename): # <<<<<<<<<<<<<< + * '''open a new file.''' + * fd = os.open(filename, os.O_WRONLY|os.O_CREAT, 0660); + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_4Outs_2setfile, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2500; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s__setfile, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2500; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2505 + * self.setfd(fd) + * + * def setfd(self, fd): # <<<<<<<<<<<<<< + * ofd = os.dup(self.id) # Save old stream on new unit. + * self.streams.append(ofd) + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_4Outs_3setfd, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2505; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s__setfd, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2505; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2512 + * os.close(fd) # Close other unit (look out, caller.) + * + * def restore(self): # <<<<<<<<<<<<<< + * '''restore previous output stream''' + * if self.streams: + */ + __pyx_t_3 = __pyx_binding_PyCFunctionType_NewEx(&__pyx_mdef_9csamtools_4Outs_4restore, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetItem(__pyx_t_1, __pyx_n_s__restore, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2489 + * return self._level + * + * class Outs: # <<<<<<<<<<<<<< + * '''http://mail.python.org/pipermail/python-list/2000-June/038406.html''' + * def __init__(self, id = 1): + */ + if (PyDict_SetItemString(((PyObject *)__pyx_t_1), "__doc__", ((PyObject *)__pyx_kp_s_237)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2489; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = __Pyx_CreateClass(((PyObject *)__pyx_empty_tuple), ((PyObject *)__pyx_t_1), __pyx_n_s__Outs, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2489; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + if (PyObject_SetAttr(__pyx_m, __pyx_n_s__Outs, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2489; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2525 + * def _samtools_dispatch( method, + * args = (), + * catch_stdout = True, # <<<<<<<<<<<<<< + * catch_stderr = False, + * ): + */ + __pyx_t_1 = __Pyx_PyBool_FromLong(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2525; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_k_149 = __pyx_t_1; + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2526 + * args = (), + * catch_stdout = True, + * catch_stderr = False, # <<<<<<<<<<<<<< + * ): + * '''call ``method`` in samtools providing arguments in args. + */ + __pyx_t_1 = __Pyx_PyBool_FromLong(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2526; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_k_150 = __pyx_t_1; + __Pyx_GIVEREF(__pyx_t_1); + __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2523 + * del self.streams[-1] + * + * def _samtools_dispatch( method, # <<<<<<<<<<<<<< + * args = (), + * catch_stdout = True, + */ + __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_9csamtools_0_samtools_dispatch, NULL, __pyx_n_s__csamtools); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2523; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + if (PyObject_SetAttr(__pyx_m, __pyx_n_s___samtools_dispatch, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2523; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":3157 + * return self._call() + * + * __all__ = ["Samfile", # <<<<<<<<<<<<<< + * "Fastafile", + * "IteratorRow", + */ + __pyx_t_1 = PyList_New(12); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3157; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__Samfile)); + PyList_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_n_s__Samfile)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__Samfile)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__Fastafile)); + PyList_SET_ITEM(__pyx_t_1, 1, ((PyObject *)__pyx_n_s__Fastafile)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__Fastafile)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__IteratorRow)); + PyList_SET_ITEM(__pyx_t_1, 2, ((PyObject *)__pyx_n_s__IteratorRow)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__IteratorRow)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__IteratorColumn)); + PyList_SET_ITEM(__pyx_t_1, 3, ((PyObject *)__pyx_n_s__IteratorColumn)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__IteratorColumn)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__AlignedRead)); + PyList_SET_ITEM(__pyx_t_1, 4, ((PyObject *)__pyx_n_s__AlignedRead)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__AlignedRead)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__PileupColumn)); + PyList_SET_ITEM(__pyx_t_1, 5, ((PyObject *)__pyx_n_s__PileupColumn)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__PileupColumn)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__PileupProxy)); + PyList_SET_ITEM(__pyx_t_1, 6, ((PyObject *)__pyx_n_s__PileupProxy)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__PileupProxy)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__PileupRead)); + PyList_SET_ITEM(__pyx_t_1, 7, ((PyObject *)__pyx_n_s__PileupRead)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__PileupRead)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__IteratorSNPCalls)); + PyList_SET_ITEM(__pyx_t_1, 8, ((PyObject *)__pyx_n_s__IteratorSNPCalls)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__IteratorSNPCalls)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__SNPCaller)); + PyList_SET_ITEM(__pyx_t_1, 9, ((PyObject *)__pyx_n_s__SNPCaller)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__SNPCaller)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__IndelCaller)); + PyList_SET_ITEM(__pyx_t_1, 10, ((PyObject *)__pyx_n_s__IndelCaller)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__IndelCaller)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__IteratorIndelCalls)); + PyList_SET_ITEM(__pyx_t_1, 11, ((PyObject *)__pyx_n_s__IteratorIndelCalls)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__IteratorIndelCalls)); + if (PyObject_SetAttr(__pyx_m, __pyx_n_s____all__, ((PyObject *)__pyx_t_1)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3157; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/home/andreas/pysam/pysam/pysam/csamtools.pyx":2 + * # cython: embedsignature=True + * # cython: profile=True # <<<<<<<<<<<<<< + * # adds doc-strings for sphinx + * import tempfile, os, sys, types, struct, ctypes, collections, re + */ + __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + if (PyObject_SetAttr(__pyx_m, __pyx_n_s____test__, ((PyObject *)__pyx_t_1)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + + /* "/usr/lib64/python2.7/site-packages/Cython/Includes/cpython/iterator.pxd":1 + * cdef extern from "Python.h": # <<<<<<<<<<<<<< + * + * ############################################################################ + */ + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_3); + if (__pyx_m) { + __Pyx_AddTraceback("init csamtools"); + Py_DECREF(__pyx_m); __pyx_m = 0; + } else if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "init csamtools"); + } + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + #if PY_MAJOR_VERSION < 3 + return; + #else + return __pyx_m; + #endif +} + +/* Runtime support code */ + +static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) { + PyObject *result; + result = PyObject_GetAttr(dict, name); + if (!result) + PyErr_SetObject(PyExc_NameError, name); + return result; +} + + +#if CYTHON_PROFILE + +static int __Pyx_TraceSetupAndCall(PyCodeObject** code, + PyFrameObject** frame, + const char *funcname, + const char *srcfile, + int firstlineno) { + if (*frame == NULL || !CYTHON_PROFILE_REUSE_FRAME) { + if (*code == NULL) { + *code = __Pyx_createFrameCodeObject(funcname, srcfile, firstlineno); + if (*code == NULL) return 0; + } + *frame = PyFrame_New( + PyThreadState_GET(), /*PyThreadState *tstate*/ + *code, /*PyCodeObject *code*/ + PyModule_GetDict(__pyx_m), /*PyObject *globals*/ + 0 /*PyObject *locals*/ + ); + if (*frame == NULL) return 0; + } + else { + (*frame)->f_tstate = PyThreadState_GET(); + } + return PyThreadState_GET()->c_profilefunc(PyThreadState_GET()->c_profileobj, *frame, PyTrace_CALL, NULL) == 0; +} + +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 */ + +static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) { + if (unlikely(!type)) { + PyErr_Format(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; +} + +static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) { + PyErr_Format(PyExc_ValueError, + #if PY_VERSION_HEX < 0x02050000 + "need more than %d value%s to unpack", (int)index, + #else + "need more than %zd value%s to unpack", index, + #endif + (index == 1) ? "" : "s"); +} + +static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) { + PyErr_Format(PyExc_ValueError, + #if PY_VERSION_HEX < 0x02050000 + "too many values to unpack (expected %d)", (int)expected); + #else + "too many values to unpack (expected %zd)", expected); + #endif +} + +static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) { + PyObject *item; + if (!(item = PyIter_Next(iter))) { + if (!PyErr_Occurred()) { + __Pyx_RaiseNeedMoreValuesError(index); + } + } + return item; +} + +static int __Pyx_EndUnpack(PyObject *iter, Py_ssize_t expected) { + PyObject *item; + if ((item = PyIter_Next(iter))) { + Py_DECREF(item); + __Pyx_RaiseTooManyValuesError(expected); + return -1; + } + else if (!PyErr_Occurred()) + return 0; + else + return -1; +} + +static CYTHON_INLINE int __Pyx_CheckKeywordStrings( + PyObject *kwdict, + const char* function_name, + int kw_allowed) +{ + PyObject* key = 0; + Py_ssize_t pos = 0; + while (PyDict_Next(kwdict, &pos, &key, 0)) { + #if PY_MAJOR_VERSION < 3 + if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) + #else + if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) + #endif + goto invalid_keyword_type; + } + if ((!kw_allowed) && unlikely(key)) + goto invalid_keyword; + return 1; +invalid_keyword_type: + PyErr_Format(PyExc_TypeError, + "%s() keywords must be strings", function_name); + return 0; +invalid_keyword: + PyErr_Format(PyExc_TypeError, + #if PY_MAJOR_VERSION < 3 + "%s() got an unexpected keyword argument '%s'", + 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_AS_STRING(kw_name)); + #endif +} + +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 *number, *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"; + } + number = (num_expected == 1) ? "" : "s"; + PyErr_Format(PyExc_TypeError, + #if PY_VERSION_HEX < 0x02050000 + "%s() takes %s %d positional argument%s (%d given)", + #else + "%s() takes %s %zd positional argument%s (%zd given)", + #endif + func_name, more_or_less, num_expected, number, num_found); +} + +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; + } else { + #if PY_MAJOR_VERSION < 3 + if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) { + #else + if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) { + #endif + goto invalid_keyword_type; + } else { + for (name = first_kw_arg; *name; name++) { + #if PY_MAJOR_VERSION >= 3 + if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) && + PyUnicode_Compare(**name, key) == 0) break; + #else + if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) && + _PyString_Eq(**name, key)) break; + #endif + } + if (*name) { + values[name-argnames] = value; + } else { + /* unexpected keyword found */ + for (name=argnames; name != first_kw_arg; name++) { + if (**name == key) goto arg_passed_twice; + #if PY_MAJOR_VERSION >= 3 + if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) && + PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice; + #else + if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) && + _PyString_Eq(**name, key)) goto arg_passed_twice; + #endif + } + if (kwds2) { + if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad; + } else { + goto invalid_keyword; + } + } + } + } + } + return 0; +arg_passed_twice: + __Pyx_RaiseDoubleKeywordsError(function_name, **name); + goto bad; +invalid_keyword_type: + PyErr_Format(PyExc_TypeError, + "%s() keywords must be strings", function_name); + goto bad; +invalid_keyword: + PyErr_Format(PyExc_TypeError, + #if PY_MAJOR_VERSION < 3 + "%s() got an unexpected keyword argument '%s'", + function_name, PyString_AsString(key)); + #else + "%s() got an unexpected keyword argument '%U'", + function_name, key); + #endif +bad: + return -1; +} + +static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) { + 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); +} + +static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) { + 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; +} + + +static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, + const char *name, int exact) +{ + if (!type) { + PyErr_Format(PyExc_SystemError, "Missing type object"); + return 0; + } + if (none_allowed && obj == Py_None) return 1; + else if (exact) { + if (Py_TYPE(obj) == type) return 1; + } + else { + if (PyObject_TypeCheck(obj, type)) return 1; + } + PyErr_Format(PyExc_TypeError, + "Argument '%s' has incorrect type (expected %s, got %s)", + name, type->tp_name, Py_TYPE(obj)->tp_name); + return 0; +} + + +static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is unsubscriptable"); +} + +static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb) { + PyObject *local_type, *local_value, *local_tb; + 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; + PyErr_NormalizeException(&local_type, &local_value, &local_tb); + if (unlikely(tstate->curexc_type)) + goto bad; + #if PY_MAJOR_VERSION >= 3 + if (unlikely(PyException_SetTraceback(local_value, local_tb) < 0)) + goto bad; + #endif + *type = local_type; + *value = local_value; + *tb = local_tb; + Py_INCREF(local_type); + Py_INCREF(local_value); + Py_INCREF(local_tb); + 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; + /* Make sure tstate is in a consistent state when we XDECREF + these objects (XDECREF may run arbitrary code). */ + Py_XDECREF(tmp_type); + Py_XDECREF(tmp_value); + Py_XDECREF(tmp_tb); + 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 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_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb) { + 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); +} + +static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb) { + 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); +} + +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) { + PyObject *py_import = 0; + PyObject *empty_list = 0; + PyObject *module = 0; + PyObject *global_dict = 0; + PyObject *empty_dict = 0; + PyObject *list; + py_import = __Pyx_GetAttrString(__pyx_b, "__import__"); + if (!py_import) + goto bad; + 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; + module = PyObject_CallFunctionObjArgs(py_import, + name, global_dict, empty_dict, list, NULL); +bad: + Py_XDECREF(empty_list); + Py_XDECREF(py_import); + Py_XDECREF(empty_dict); + return module; +} + +static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases) { + PyObject *metaclass; + /* Default metaclass */ +#if PY_MAJOR_VERSION < 3 + if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { + PyObject *base = PyTuple_GET_ITEM(bases, 0); + metaclass = PyObject_GetAttrString(base, "__class__"); + if (!metaclass) { + PyErr_Clear(); + metaclass = (PyObject*) Py_TYPE(base); + } + } else { + metaclass = (PyObject *) &PyClass_Type; + } +#else + if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { + PyObject *base = PyTuple_GET_ITEM(bases, 0); + metaclass = (PyObject*) Py_TYPE(base); + } else { + metaclass = (PyObject *) &PyType_Type; + } +#endif + Py_INCREF(metaclass); + return metaclass; +} + +static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, + PyObject *modname) { + PyObject *result; + PyObject *metaclass; + + if (PyDict_SetItemString(dict, "__module__", modname) < 0) + return NULL; + + /* Python2 __metaclass__ */ + metaclass = PyDict_GetItemString(dict, "__metaclass__"); + if (metaclass) { + Py_INCREF(metaclass); + } else { + metaclass = __Pyx_FindPy2Metaclass(bases); + } + result = PyObject_CallFunctionObjArgs(metaclass, name, bases, dict, NULL); + Py_DECREF(metaclass); + return result; +} + + +PyObject *__pyx_binding_PyCFunctionType_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) { + __pyx_binding_PyCFunctionType_object *op = PyObject_GC_New(__pyx_binding_PyCFunctionType_object, __pyx_binding_PyCFunctionType); + if (op == NULL) + return NULL; + op->func.m_ml = ml; + Py_XINCREF(self); + op->func.m_self = self; + Py_XINCREF(module); + op->func.m_module = module; + PyObject_GC_Track(op); + return (PyObject *)op; +} + +static void __pyx_binding_PyCFunctionType_dealloc(__pyx_binding_PyCFunctionType_object *m) { + PyObject_GC_UnTrack(m); + Py_XDECREF(m->func.m_self); + Py_XDECREF(m->func.m_module); + PyObject_GC_Del(m); +} + +static PyObject *__pyx_binding_PyCFunctionType_descr_get(PyObject *func, PyObject *obj, PyObject *type) { + if (obj == Py_None) + obj = NULL; + return PyMethod_New(func, obj, type); +} + +int __pyx_binding_PyCFunctionType_init(void) { + __pyx_binding_PyCFunctionType_type = PyCFunction_Type; + __pyx_binding_PyCFunctionType_type.tp_name = __Pyx_NAMESTR("cython_binding_builtin_function_or_method"); + __pyx_binding_PyCFunctionType_type.tp_dealloc = (destructor)__pyx_binding_PyCFunctionType_dealloc; + __pyx_binding_PyCFunctionType_type.tp_descr_get = __pyx_binding_PyCFunctionType_descr_get; + if (PyType_Ready(&__pyx_binding_PyCFunctionType_type) < 0) { + return -1; + } + __pyx_binding_PyCFunctionType = &__pyx_binding_PyCFunctionType_type; + return 0; + +} + +static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_uint32_t(uint32_t val) { + const uint32_t neg_one = (uint32_t)-1, const_zero = (uint32_t)0; + const int is_unsigned = const_zero < neg_one; + if ((sizeof(uint32_t) == sizeof(char)) || + (sizeof(uint32_t) == sizeof(short))) { + return PyInt_FromLong((long)val); + } else if ((sizeof(uint32_t) == sizeof(int)) || + (sizeof(uint32_t) == sizeof(long))) { + if (is_unsigned) + return PyLong_FromUnsignedLong((unsigned long)val); + else + return PyInt_FromLong((long)val); + } else if (sizeof(uint32_t) == sizeof(PY_LONG_LONG)) { + if (is_unsigned) + return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val); + else + return PyLong_FromLongLong((PY_LONG_LONG)val); + } else { + int one = 1; int little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&val; + return _PyLong_FromByteArray(bytes, sizeof(uint32_t), + little, !is_unsigned); + } +} + +#if PY_MAJOR_VERSION < 3 +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) { + Py_XINCREF(type); + Py_XINCREF(value); + Py_XINCREF(tb); + /* First, check the traceback argument, replacing None with NULL. */ + if (tb == Py_None) { + Py_DECREF(tb); + tb = 0; + } + else if (tb != NULL && !PyTraceBack_Check(tb)) { + PyErr_SetString(PyExc_TypeError, + "raise: arg 3 must be a traceback or None"); + goto raise_error; + } + /* Next, replace a missing value with None */ + if (value == NULL) { + value = Py_None; + Py_INCREF(value); + } + #if PY_VERSION_HEX < 0x02050000 + if (!PyClass_Check(type)) + #else + if (!PyType_Check(type)) + #endif + { + /* Raising an instance. The value should be a dummy. */ + if (value != Py_None) { + PyErr_SetString(PyExc_TypeError, + "instance exception may not have a separate value"); + goto raise_error; + } + /* Normalize to raise , */ + Py_DECREF(value); + 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) { + 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)) { + PyErr_SetString(PyExc_TypeError, + "raise: exception class must be a subclass of BaseException"); + goto bad; + } + + 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: + return; +} +#endif + +static CYTHON_INLINE uint32_t __Pyx_PyInt_from_py_uint32_t(PyObject* x) { + const uint32_t neg_one = (uint32_t)-1, const_zero = (uint32_t)0; + const int is_unsigned = const_zero < neg_one; + if (sizeof(uint32_t) == sizeof(char)) { + if (is_unsigned) + return (uint32_t)__Pyx_PyInt_AsUnsignedChar(x); + else + return (uint32_t)__Pyx_PyInt_AsSignedChar(x); + } else if (sizeof(uint32_t) == sizeof(short)) { + if (is_unsigned) + return (uint32_t)__Pyx_PyInt_AsUnsignedShort(x); + else + return (uint32_t)__Pyx_PyInt_AsSignedShort(x); + } else if (sizeof(uint32_t) == sizeof(int)) { + if (is_unsigned) + return (uint32_t)__Pyx_PyInt_AsUnsignedInt(x); + else + return (uint32_t)__Pyx_PyInt_AsSignedInt(x); + } else if (sizeof(uint32_t) == sizeof(long)) { + if (is_unsigned) + return (uint32_t)__Pyx_PyInt_AsUnsignedLong(x); + else + return (uint32_t)__Pyx_PyInt_AsSignedLong(x); + } else if (sizeof(uint32_t) == sizeof(PY_LONG_LONG)) { + if (is_unsigned) + return (uint32_t)__Pyx_PyInt_AsUnsignedLongLong(x); + else + return (uint32_t)__Pyx_PyInt_AsSignedLongLong(x); + } else { + uint32_t val; + PyObject *v = __Pyx_PyNumber_Int(x); + #if PY_VERSION_HEX < 0x03000000 + 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; + } + return (uint32_t)-1; + } +} + +static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_int32_t(int32_t val) { + const int32_t neg_one = (int32_t)-1, const_zero = (int32_t)0; + const int is_unsigned = const_zero < neg_one; + if ((sizeof(int32_t) == sizeof(char)) || + (sizeof(int32_t) == sizeof(short))) { + return PyInt_FromLong((long)val); + } else if ((sizeof(int32_t) == sizeof(int)) || + (sizeof(int32_t) == sizeof(long))) { + if (is_unsigned) + return PyLong_FromUnsignedLong((unsigned long)val); + else + return PyInt_FromLong((long)val); + } else if (sizeof(int32_t) == sizeof(PY_LONG_LONG)) { + if (is_unsigned) + return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val); + else + return PyLong_FromLongLong((PY_LONG_LONG)val); + } else { + int one = 1; int little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&val; + return _PyLong_FromByteArray(bytes, sizeof(int32_t), + little, !is_unsigned); + } +} + +static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_int64_t(int64_t val) { + const int64_t neg_one = (int64_t)-1, const_zero = (int64_t)0; + const int is_unsigned = const_zero < neg_one; + if ((sizeof(int64_t) == sizeof(char)) || + (sizeof(int64_t) == sizeof(short))) { + return PyInt_FromLong((long)val); + } else if ((sizeof(int64_t) == sizeof(int)) || + (sizeof(int64_t) == sizeof(long))) { + if (is_unsigned) + return PyLong_FromUnsignedLong((unsigned long)val); + else + return PyInt_FromLong((long)val); + } else if (sizeof(int64_t) == sizeof(PY_LONG_LONG)) { + if (is_unsigned) + return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val); + else + return PyLong_FromLongLong((PY_LONG_LONG)val); + } else { + int one = 1; int little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&val; + return _PyLong_FromByteArray(bytes, sizeof(int64_t), + little, !is_unsigned); + } +} + +static CYTHON_INLINE int32_t __Pyx_PyInt_from_py_int32_t(PyObject* x) { + const int32_t neg_one = (int32_t)-1, const_zero = (int32_t)0; + const int is_unsigned = const_zero < neg_one; + if (sizeof(int32_t) == sizeof(char)) { + if (is_unsigned) + return (int32_t)__Pyx_PyInt_AsUnsignedChar(x); + else + return (int32_t)__Pyx_PyInt_AsSignedChar(x); + } else if (sizeof(int32_t) == sizeof(short)) { + if (is_unsigned) + return (int32_t)__Pyx_PyInt_AsUnsignedShort(x); + else + return (int32_t)__Pyx_PyInt_AsSignedShort(x); + } else if (sizeof(int32_t) == sizeof(int)) { + if (is_unsigned) + return (int32_t)__Pyx_PyInt_AsUnsignedInt(x); + else + return (int32_t)__Pyx_PyInt_AsSignedInt(x); + } else if (sizeof(int32_t) == sizeof(long)) { + if (is_unsigned) + return (int32_t)__Pyx_PyInt_AsUnsignedLong(x); + else + return (int32_t)__Pyx_PyInt_AsSignedLong(x); + } else if (sizeof(int32_t) == sizeof(PY_LONG_LONG)) { + if (is_unsigned) + return (int32_t)__Pyx_PyInt_AsUnsignedLongLong(x); + else + return (int32_t)__Pyx_PyInt_AsSignedLongLong(x); + } else { + int32_t val; + PyObject *v = __Pyx_PyNumber_Int(x); + #if PY_VERSION_HEX < 0x03000000 + 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; + } + return (int32_t)-1; + } +} + +static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_uint8_t(uint8_t val) { + const uint8_t neg_one = (uint8_t)-1, const_zero = (uint8_t)0; + const int is_unsigned = const_zero < neg_one; + if ((sizeof(uint8_t) == sizeof(char)) || + (sizeof(uint8_t) == sizeof(short))) { + return PyInt_FromLong((long)val); + } else if ((sizeof(uint8_t) == sizeof(int)) || + (sizeof(uint8_t) == sizeof(long))) { + if (is_unsigned) + return PyLong_FromUnsignedLong((unsigned long)val); + else + return PyInt_FromLong((long)val); + } else if (sizeof(uint8_t) == sizeof(PY_LONG_LONG)) { + if (is_unsigned) + return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val); + else + return PyLong_FromLongLong((PY_LONG_LONG)val); + } else { + int one = 1; int little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&val; + return _PyLong_FromByteArray(bytes, sizeof(uint8_t), + little, !is_unsigned); + } +} + +#if PY_MAJOR_VERSION < 3 +static PyObject *__Pyx_GetStdout(void) { + PyObject *f = PySys_GetObject((char *)"stdout"); + if (!f) { + PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); + } + return f; +} + +static int __Pyx_Print(PyObject* f, PyObject *arg_tuple, int newline) { + PyObject* v; + int i; + + if (!f) { + if (!(f = __Pyx_GetStdout())) + return -1; + } + for (i=0; i < PyTuple_GET_SIZE(arg_tuple); i++) { + if (PyFile_SoftSpace(f, 1)) { + if (PyFile_WriteString(" ", f) < 0) + return -1; + } + v = PyTuple_GET_ITEM(arg_tuple, i); + if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0) + return -1; + if (PyString_Check(v)) { + char *s = PyString_AsString(v); + Py_ssize_t len = PyString_Size(v); + if (len > 0 && + isspace(Py_CHARMASK(s[len-1])) && + s[len-1] != ' ') + PyFile_SoftSpace(f, 0); + } + } + if (newline) { + if (PyFile_WriteString("\n", f) < 0) + return -1; + PyFile_SoftSpace(f, 0); + } + return 0; +} + +#else /* Python 3 has a print function */ + +static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) { + PyObject* kwargs = 0; + PyObject* result = 0; + PyObject* end_string; + if (unlikely(!__pyx_print)) { + __pyx_print = __Pyx_GetAttrString(__pyx_b, "print"); + if (!__pyx_print) + return -1; + } + if (stream) { + kwargs = PyDict_New(); + if (unlikely(!kwargs)) + return -1; + if (unlikely(PyDict_SetItemString(kwargs, "file", stream) < 0)) + goto bad; + if (!newline) { + end_string = PyUnicode_FromStringAndSize(" ", 1); + if (unlikely(!end_string)) + goto bad; + if (PyDict_SetItemString(kwargs, "end", end_string) < 0) { + Py_DECREF(end_string); + goto bad; + } + Py_DECREF(end_string); + } + } else if (!newline) { + if (unlikely(!__pyx_print_kwargs)) { + __pyx_print_kwargs = PyDict_New(); + if (unlikely(!__pyx_print_kwargs)) + return -1; + end_string = PyUnicode_FromStringAndSize(" ", 1); + if (unlikely(!end_string)) + return -1; + if (PyDict_SetItemString(__pyx_print_kwargs, "end", end_string) < 0) { + Py_DECREF(end_string); + return -1; + } + Py_DECREF(end_string); + } + kwargs = __pyx_print_kwargs; + } + result = PyObject_Call(__pyx_print, arg_tuple, kwargs); + if (unlikely(kwargs) && (kwargs != __pyx_print_kwargs)) + Py_DECREF(kwargs); + if (!result) + return -1; + Py_DECREF(result); + return 0; +bad: + if (kwargs != __pyx_print_kwargs) + Py_XDECREF(kwargs); + return -1; +} + +#endif + +#if PY_MAJOR_VERSION < 3 + +static int __Pyx_PrintOne(PyObject* f, PyObject *o) { + if (!f) { + if (!(f = __Pyx_GetStdout())) + return -1; + } + if (PyFile_SoftSpace(f, 0)) { + if (PyFile_WriteString(" ", f) < 0) + return -1; + } + if (PyFile_WriteObject(o, f, Py_PRINT_RAW) < 0) + return -1; + if (PyFile_WriteString("\n", f) < 0) + return -1; + return 0; + /* the line below is just to avoid compiler + * compiler warnings about unused functions */ + return __Pyx_Print(f, NULL, 0); +} + +#else /* Python 3 has a print function */ + +static int __Pyx_PrintOne(PyObject* stream, PyObject *o) { + int res; + PyObject* arg_tuple = PyTuple_New(1); + if (unlikely(!arg_tuple)) + return -1; + Py_INCREF(o); + PyTuple_SET_ITEM(arg_tuple, 0, o); + res = __Pyx_Print(stream, arg_tuple, 1); + Py_DECREF(arg_tuple); + return res; +} + +#endif + +static CYTHON_INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject* x) { + const unsigned char neg_one = (unsigned char)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(unsigned char) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(unsigned char)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to unsigned char" : + "value too large to convert to unsigned char"); + } + return (unsigned char)-1; + } + return (unsigned char)val; + } + return (unsigned char)__Pyx_PyInt_AsUnsignedLong(x); +} + +static CYTHON_INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject* x) { + const unsigned short neg_one = (unsigned short)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(unsigned short) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(unsigned short)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to unsigned short" : + "value too large to convert to unsigned short"); + } + return (unsigned short)-1; + } + return (unsigned short)val; + } + return (unsigned short)__Pyx_PyInt_AsUnsignedLong(x); +} + +static CYTHON_INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject* x) { + const unsigned int neg_one = (unsigned int)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(unsigned int) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(unsigned int)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to unsigned int" : + "value too large to convert to unsigned int"); + } + return (unsigned int)-1; + } + return (unsigned int)val; + } + return (unsigned int)__Pyx_PyInt_AsUnsignedLong(x); +} + +static CYTHON_INLINE char __Pyx_PyInt_AsChar(PyObject* x) { + const char neg_one = (char)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(char) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(char)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to char" : + "value too large to convert to char"); + } + return (char)-1; + } + return (char)val; + } + return (char)__Pyx_PyInt_AsLong(x); +} + +static CYTHON_INLINE short __Pyx_PyInt_AsShort(PyObject* x) { + const short neg_one = (short)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(short) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(short)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to short" : + "value too large to convert to short"); + } + return (short)-1; + } + return (short)val; + } + return (short)__Pyx_PyInt_AsLong(x); +} + +static CYTHON_INLINE int __Pyx_PyInt_AsInt(PyObject* x) { + const int neg_one = (int)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(int) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(int)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to int" : + "value too large to convert to int"); + } + return (int)-1; + } + return (int)val; + } + return (int)__Pyx_PyInt_AsLong(x); +} + +static CYTHON_INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject* x) { + const signed char neg_one = (signed char)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(signed char) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(signed char)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to signed char" : + "value too large to convert to signed char"); + } + return (signed char)-1; + } + return (signed char)val; + } + return (signed char)__Pyx_PyInt_AsSignedLong(x); +} + +static CYTHON_INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject* x) { + const signed short neg_one = (signed short)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(signed short) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(signed short)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to signed short" : + "value too large to convert to signed short"); + } + return (signed short)-1; + } + return (signed short)val; + } + return (signed short)__Pyx_PyInt_AsSignedLong(x); +} + +static CYTHON_INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject* x) { + const signed int neg_one = (signed int)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(signed int) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(signed int)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to signed int" : + "value too large to convert to signed int"); + } + return (signed int)-1; + } + return (signed int)val; + } + return (signed int)__Pyx_PyInt_AsSignedLong(x); +} + +static CYTHON_INLINE int __Pyx_PyInt_AsLongDouble(PyObject* x) { + const int neg_one = (int)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; + if (sizeof(int) < sizeof(long)) { + long val = __Pyx_PyInt_AsLong(x); + if (unlikely(val != (long)(int)val)) { + if (!unlikely(val == -1 && PyErr_Occurred())) { + PyErr_SetString(PyExc_OverflowError, + (is_unsigned && unlikely(val < 0)) ? + "can't convert negative value to int" : + "value too large to convert to int"); + } + return (int)-1; + } + return (int)val; + } + return (int)__Pyx_PyInt_AsLong(x); +} + +static CYTHON_INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject* x) { + const unsigned long neg_one = (unsigned long)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; +#if PY_VERSION_HEX < 0x03000000 + if (likely(PyInt_Check(x))) { + long val = PyInt_AS_LONG(x); + if (is_unsigned && unlikely(val < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to unsigned long"); + return (unsigned long)-1; + } + return (unsigned long)val; + } else +#endif + if (likely(PyLong_Check(x))) { + if (is_unsigned) { + if (unlikely(Py_SIZE(x) < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to unsigned long"); + return (unsigned long)-1; + } + return PyLong_AsUnsignedLong(x); + } else { + return PyLong_AsLong(x); + } + } else { + unsigned long val; + PyObject *tmp = __Pyx_PyNumber_Int(x); + if (!tmp) return (unsigned long)-1; + val = __Pyx_PyInt_AsUnsignedLong(tmp); + Py_DECREF(tmp); + return val; + } +} + +static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject* x) { + const unsigned PY_LONG_LONG neg_one = (unsigned PY_LONG_LONG)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; +#if PY_VERSION_HEX < 0x03000000 + if (likely(PyInt_Check(x))) { + long val = PyInt_AS_LONG(x); + if (is_unsigned && unlikely(val < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to unsigned PY_LONG_LONG"); + return (unsigned PY_LONG_LONG)-1; + } + return (unsigned PY_LONG_LONG)val; + } else +#endif + if (likely(PyLong_Check(x))) { + if (is_unsigned) { + if (unlikely(Py_SIZE(x) < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to unsigned PY_LONG_LONG"); + return (unsigned PY_LONG_LONG)-1; + } + return PyLong_AsUnsignedLongLong(x); + } else { + return PyLong_AsLongLong(x); + } + } else { + unsigned PY_LONG_LONG val; + PyObject *tmp = __Pyx_PyNumber_Int(x); + if (!tmp) return (unsigned PY_LONG_LONG)-1; + val = __Pyx_PyInt_AsUnsignedLongLong(tmp); + Py_DECREF(tmp); + return val; + } +} + +static CYTHON_INLINE long __Pyx_PyInt_AsLong(PyObject* x) { + const long neg_one = (long)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; +#if PY_VERSION_HEX < 0x03000000 + if (likely(PyInt_Check(x))) { + 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; + } + return (long)val; + } else +#endif + if (likely(PyLong_Check(x))) { + if (is_unsigned) { + if (unlikely(Py_SIZE(x) < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to long"); + return (long)-1; + } + return PyLong_AsUnsignedLong(x); + } else { + return PyLong_AsLong(x); + } + } else { + long val; + PyObject *tmp = __Pyx_PyNumber_Int(x); + if (!tmp) return (long)-1; + val = __Pyx_PyInt_AsLong(tmp); + Py_DECREF(tmp); + return val; + } +} + +static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(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_VERSION_HEX < 0x03000000 + if (likely(PyInt_Check(x))) { + 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 (unlikely(Py_SIZE(x) < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to PY_LONG_LONG"); + return (PY_LONG_LONG)-1; + } + return PyLong_AsUnsignedLongLong(x); + } else { + return PyLong_AsLongLong(x); + } + } else { + PY_LONG_LONG val; + PyObject *tmp = __Pyx_PyNumber_Int(x); + if (!tmp) return (PY_LONG_LONG)-1; + val = __Pyx_PyInt_AsLongLong(tmp); + Py_DECREF(tmp); + return val; + } +} + +static CYTHON_INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject* x) { + const signed long neg_one = (signed long)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; +#if PY_VERSION_HEX < 0x03000000 + if (likely(PyInt_Check(x))) { + long val = PyInt_AS_LONG(x); + if (is_unsigned && unlikely(val < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to signed long"); + return (signed long)-1; + } + return (signed long)val; + } else +#endif + if (likely(PyLong_Check(x))) { + if (is_unsigned) { + if (unlikely(Py_SIZE(x) < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to signed long"); + return (signed long)-1; + } + return PyLong_AsUnsignedLong(x); + } else { + return PyLong_AsLong(x); + } + } else { + signed long val; + PyObject *tmp = __Pyx_PyNumber_Int(x); + if (!tmp) return (signed long)-1; + val = __Pyx_PyInt_AsSignedLong(tmp); + Py_DECREF(tmp); + return val; + } +} + +static CYTHON_INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject* x) { + const signed PY_LONG_LONG neg_one = (signed PY_LONG_LONG)-1, const_zero = 0; + const int is_unsigned = neg_one > const_zero; +#if PY_VERSION_HEX < 0x03000000 + if (likely(PyInt_Check(x))) { + long val = PyInt_AS_LONG(x); + if (is_unsigned && unlikely(val < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to signed PY_LONG_LONG"); + return (signed PY_LONG_LONG)-1; + } + return (signed PY_LONG_LONG)val; + } else +#endif + if (likely(PyLong_Check(x))) { + if (is_unsigned) { + if (unlikely(Py_SIZE(x) < 0)) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to signed PY_LONG_LONG"); + return (signed PY_LONG_LONG)-1; + } + return PyLong_AsUnsignedLongLong(x); + } else { + return PyLong_AsLongLong(x); + } + } else { + signed PY_LONG_LONG val; + PyObject *tmp = __Pyx_PyNumber_Int(x); + if (!tmp) return (signed PY_LONG_LONG)-1; + val = __Pyx_PyInt_AsSignedLongLong(tmp); + Py_DECREF(tmp); + return val; + } +} + +static void __Pyx_WriteUnraisable(const char *name) { + PyObject *old_exc, *old_val, *old_tb; + PyObject *ctx; + __Pyx_ErrFetch(&old_exc, &old_val, &old_tb); + #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 uint64_t __Pyx_PyInt_from_py_uint64_t(PyObject* x) { + const uint64_t neg_one = (uint64_t)-1, const_zero = (uint64_t)0; + const int is_unsigned = const_zero < neg_one; + if (sizeof(uint64_t) == sizeof(char)) { + if (is_unsigned) + return (uint64_t)__Pyx_PyInt_AsUnsignedChar(x); + else + return (uint64_t)__Pyx_PyInt_AsSignedChar(x); + } else if (sizeof(uint64_t) == sizeof(short)) { + if (is_unsigned) + return (uint64_t)__Pyx_PyInt_AsUnsignedShort(x); + else + return (uint64_t)__Pyx_PyInt_AsSignedShort(x); + } else if (sizeof(uint64_t) == sizeof(int)) { + if (is_unsigned) + return (uint64_t)__Pyx_PyInt_AsUnsignedInt(x); + else + return (uint64_t)__Pyx_PyInt_AsSignedInt(x); + } else if (sizeof(uint64_t) == sizeof(long)) { + if (is_unsigned) + return (uint64_t)__Pyx_PyInt_AsUnsignedLong(x); + else + return (uint64_t)__Pyx_PyInt_AsSignedLong(x); + } else if (sizeof(uint64_t) == sizeof(PY_LONG_LONG)) { + if (is_unsigned) + return (uint64_t)__Pyx_PyInt_AsUnsignedLongLong(x); + else + return (uint64_t)__Pyx_PyInt_AsSignedLongLong(x); + } else { + uint64_t val; + PyObject *v = __Pyx_PyNumber_Int(x); + #if PY_VERSION_HEX < 0x03000000 + 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; + } + return (uint64_t)-1; + } +} + +#ifndef __PYX_HAVE_RT_ImportType +#define __PYX_HAVE_RT_ImportType +static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, + long size, int strict) +{ + PyObject *py_module = 0; + PyObject *result = 0; + PyObject *py_name = 0; + char warning[200]; + + py_module = __Pyx_ImportModule(module_name); + if (!py_module) + goto bad; + #if PY_MAJOR_VERSION < 3 + py_name = PyString_FromString(class_name); + #else + py_name = PyUnicode_FromString(class_name); + #endif + if (!py_name) + goto bad; + result = PyObject_GetAttr(py_module, py_name); + Py_DECREF(py_name); + py_name = 0; + Py_DECREF(py_module); + py_module = 0; + if (!result) + goto bad; + if (!PyType_Check(result)) { + PyErr_Format(PyExc_TypeError, + "%s.%s is not a type object", + module_name, class_name); + goto bad; + } + if (!strict && ((PyTypeObject *)result)->tp_basicsize > size) { + PyOS_snprintf(warning, sizeof(warning), + "%s.%s size changed, may indicate binary incompatibility", + module_name, class_name); + #if PY_VERSION_HEX < 0x02050000 + PyErr_Warn(NULL, warning); + #else + PyErr_WarnEx(NULL, warning, 0); + #endif + } + else if (((PyTypeObject *)result)->tp_basicsize != size) { + PyErr_Format(PyExc_ValueError, + "%s.%s has the wrong size, try recompiling", + module_name, class_name); + goto bad; + } + return (PyTypeObject *)result; +bad: + Py_XDECREF(py_module); + Py_XDECREF(result); + return 0; +} +#endif + +#ifndef __PYX_HAVE_RT_ImportModule +#define __PYX_HAVE_RT_ImportModule +static PyObject *__Pyx_ImportModule(const char *name) { + PyObject *py_name = 0; + PyObject *py_module = 0; + + #if PY_MAJOR_VERSION < 3 + py_name = PyString_FromString(name); + #else + py_name = PyUnicode_FromString(name); + #endif + if (!py_name) + goto bad; + py_module = PyImport_Import(py_name); + Py_DECREF(py_name); + return py_module; +bad: + Py_XDECREF(py_name); + return 0; +} +#endif + +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_SetItemString(dict, "__pyx_vtable__", ob) < 0) + goto bad; + Py_DECREF(ob); + return 0; +bad: + Py_XDECREF(ob); + return -1; +} + +#include "compile.h" +#include "frameobject.h" +#include "traceback.h" + +static void __Pyx_AddTraceback(const char *funcname) { + PyObject *py_srcfile = 0; + PyObject *py_funcname = 0; + PyObject *py_globals = 0; + PyCodeObject *py_code = 0; + PyFrameObject *py_frame = 0; + + #if PY_MAJOR_VERSION < 3 + py_srcfile = PyString_FromString(__pyx_filename); + #else + py_srcfile = PyUnicode_FromString(__pyx_filename); + #endif + if (!py_srcfile) goto bad; + if (__pyx_clineno) { + #if PY_MAJOR_VERSION < 3 + py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno); + #else + py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno); + #endif + } + else { + #if PY_MAJOR_VERSION < 3 + py_funcname = PyString_FromString(funcname); + #else + py_funcname = PyUnicode_FromString(funcname); + #endif + } + if (!py_funcname) goto bad; + py_globals = PyModule_GetDict(__pyx_m); + if (!py_globals) 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,*/ + __pyx_lineno, /*int firstlineno,*/ + __pyx_empty_bytes /*PyObject *lnotab*/ + ); + if (!py_code) goto bad; + py_frame = PyFrame_New( + PyThreadState_GET(), /*PyThreadState *tstate,*/ + py_code, /*PyCodeObject *code,*/ + py_globals, /*PyObject *globals,*/ + 0 /*PyObject *locals*/ + ); + if (!py_frame) goto bad; + py_frame->f_lineno = __pyx_lineno; + PyTraceBack_Here(py_frame); +bad: + Py_XDECREF(py_srcfile); + Py_XDECREF(py_funcname); + Py_XDECREF(py_code); + Py_XDECREF(py_frame); +} + +static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) { + while (t->p) { + #if PY_MAJOR_VERSION < 3 + if (t->is_unicode) { + *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL); + } else if (t->intern) { + *t->p = PyString_InternFromString(t->s); + } else { + *t->p = PyString_FromStringAndSize(t->s, t->n - 1); + } + #else /* Python 3+ has unicode identifiers */ + if (t->is_unicode | t->is_str) { + if (t->intern) { + *t->p = PyUnicode_InternFromString(t->s); + } else if (t->encoding) { + *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL); + } else { + *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1); + } + } else { + *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1); + } + #endif + if (!*t->p) + return -1; + ++t; + } + return 0; +} + +/* Type Conversion Functions */ + +static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) { + int is_true = x == Py_True; + if (is_true | (x == Py_False) | (x == Py_None)) return is_true; + else return PyObject_IsTrue(x); +} + +static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) { + PyNumberMethods *m; + const char *name = NULL; + PyObject *res = NULL; +#if PY_VERSION_HEX < 0x03000000 + if (PyInt_Check(x) || PyLong_Check(x)) +#else + if (PyLong_Check(x)) +#endif + return Py_INCREF(x), x; + m = Py_TYPE(x)->tp_as_number; +#if PY_VERSION_HEX < 0x03000000 + if (m && m->nb_int) { + name = "int"; + res = PyNumber_Int(x); + } + else if (m && m->nb_long) { + name = "long"; + res = PyNumber_Long(x); + } +#else + if (m && m->nb_int) { + name = "int"; + res = PyNumber_Long(x); + } +#endif + if (res) { +#if PY_VERSION_HEX < 0x03000000 + if (!PyInt_Check(res) && !PyLong_Check(res)) { +#else + if (!PyLong_Check(res)) { +#endif + PyErr_Format(PyExc_TypeError, + "__%s__ returned non-%s (type %.200s)", + name, name, Py_TYPE(res)->tp_name); + Py_DECREF(res); + return NULL; + } + } + else if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, + "an integer is required"); + } + return res; +} + +static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) { + Py_ssize_t ival; + PyObject* x = PyNumber_Index(b); + if (!x) return -1; + ival = PyInt_AsSsize_t(x); + Py_DECREF(x); + return ival; +} + +static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) { +#if PY_VERSION_HEX < 0x02050000 + if (ival <= LONG_MAX) + return PyInt_FromLong((long)ival); + else { + unsigned char *bytes = (unsigned char *) &ival; + int one = 1; int little = (int)*(unsigned char*)&one; + return _PyLong_FromByteArray(bytes, sizeof(size_t), little, 0); + } +#else + return PyInt_FromSize_t(ival); +#endif +} + +static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject* x) { + unsigned PY_LONG_LONG val = __Pyx_PyInt_AsUnsignedLongLong(x); + if (unlikely(val == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())) { + return (size_t)-1; + } else if (unlikely(val != (unsigned PY_LONG_LONG)(size_t)val)) { + PyErr_SetString(PyExc_OverflowError, + "value too large to convert to size_t"); + return (size_t)-1; + } + return (size_t)val; +} + + +#endif /* Py_PYTHON_H */ diff --git a/pysam/csamtools.pxd b/pysam/csamtools.pxd index b614a84..43cfe0c 100644 --- a/pysam/csamtools.pxd +++ b/pysam/csamtools.pxd @@ -14,6 +14,9 @@ cdef extern from "stdlib.h": void qsort(void *base, size_t nmemb, size_t size, int (*compar)(void *,void *)) +cdef extern from "math.h": + double sqrt(double x) + cdef extern from "stdio.h": ctypedef struct FILE: pass @@ -52,6 +55,11 @@ cdef extern from "string.h": cdef extern from "Python.h": long _Py_HashPointer(void*) + FILE* PyFile_AsFile(object) + +cdef extern from "fileobject.h": + ctypedef class __builtin__.file [object PyFileObject]: + pass cdef extern from "razf.h": pass @@ -66,6 +74,8 @@ cdef extern from "stdint.h": cdef extern from "bam.h": + # constants + int BAM_DEF_MASK # IF _IOLIB=2, bamFile = BGZF, see bgzf.h # samtools uses KNETFILE, check how this works @@ -123,8 +133,16 @@ cdef extern from "bam.h": ctypedef struct bam_plbuf_t: pass + ctypedef struct pair64_t: + uint64_t u, v + ctypedef struct bam_iter_t: - pass + int from_first + int tid, beg, end, n_off, i, finished + uint64_t curr_off + pair64_t *off + + # ctypedef __bam_iter_t * bam_iter_t bam1_t * bam_init1() void bam_destroy1(bam1_t *) @@ -176,20 +194,22 @@ cdef extern from "bam.h": ctypedef struct bam_plp_t: pass + ctypedef bam_pileup1_t * const_bam_pileup1_t_ptr "const bam_pileup1_t *" + ctypedef int (*bam_plp_auto_f)(void *data, bam1_t *b) bam_plp_t bam_plp_init( bam_plp_auto_f func, void *data) int bam_plp_push( bam_plp_t iter, bam1_t *b) - bam_pileup1_t *bam_plp_next( bam_plp_t iter, int *_tid, int *_pos, int *_n_plp) - bam_pileup1_t *bam_plp_auto( bam_plp_t iter, int *_tid, int *_pos, int *_n_plp) + bam_pileup1_t * bam_plp_next( bam_plp_t iter, int *_tid, int *_pos, int *_n_plp) + 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_reset(bam_plp_t iter) void bam_plp_destroy(bam_plp_t iter) ################################################## - int bam_read1(bamFile fp, bam1_t *b) - + int bam_read1( bamFile fp, bam1_t *b) + int bam_validate1( bam_header_t *header, bam1_t *b) int bam_write1( bamFile fp, bam1_t *b) bam_header_t *bam_header_init() @@ -206,7 +226,7 @@ cdef extern from "bam.h": uint8_t *bam_aux_get(bam1_t *b, char tag[2]) - int bam_aux2i(uint8_t *s) + int32_t bam_aux2i(uint8_t *s) float bam_aux2f(uint8_t *s) double bam_aux2d(uint8_t *s) char bam_aux2A( uint8_t *s) @@ -238,6 +258,83 @@ cdef extern from "sam.h": int samwrite(samfile_t *fp, bam1_t *b) + int bam_prob_realn(bam1_t *b, char *ref) + int bam_cap_mapQ(bam1_t *b, char *ref, int thres) + + +cdef extern from "glf.h": + ctypedef struct glf1_t: + pass + +# unsigned char ref_base, dummy +# unsigned char max_mapQ +# unsigned char lk[10] +# unsigned int min_lk, depth +# unsigned char ref_base:4, dummy:4 +# unsigned char max_mapQ +# unsigned char lk[10] +# unsigned min_lk:8, depth:24; + + +cdef extern from "bam_maqcns.h": + + ctypedef struct bam_maqcns_t: + float het_rate, theta + int n_hap, cap_mapQ, errmod, min_baseQ + float eta, q_r + double *fk, *coef + double *lhet + void *aux + + glf1_t *bam_maqcns_glfgen(int n, + bam_pileup1_t *pl, + uint8_t ref_base, + bam_maqcns_t *bm) + + ctypedef struct bam_maqindel_opt_t: + int q_indel + float r_indel + float r_snp + int mm_penalty, indel_err, ambi_thres + + uint32_t bam_maqcns_call(int n, bam_pileup1_t *pl, bam_maqcns_t *bm) + bam_maqcns_t * bam_maqcns_init() + void bam_maqcns_destroy(bam_maqcns_t *bm) + void bam_maqcns_prepare(bam_maqcns_t *bm) + + uint32_t glf2cns(glf1_t *g, int q_r) + + int BAM_ERRMOD_MAQ2 + int BAM_ERRMOD_MAQ + int BAM_ERRMOD_SOAP + + ctypedef struct bam_maqindel_ret_t: + int indel1 + int indel2 + int cnt1 + int cnt2 + int cnt_anti + int cnt_ref + int cnt_ambi + char *s[2] + int gt + int gl[2] + int q_cns + int q_ref + + void bam_maqindel_ret_destroy( bam_maqindel_ret_t * ) + + bam_maqindel_opt_t *bam_maqindel_opt_init() + + bam_maqindel_ret_t * bam_maqindel(int n, + int pos, + bam_maqindel_opt_t * mi, + bam_pileup1_t * pl, + char *ref, + int _n_types, + int * _types ) + + cdef extern from "faidx.h": ctypedef struct faidx_t: @@ -256,6 +353,7 @@ cdef extern from "faidx.h": char *faidx_fetch_seq(faidx_t *fai, char *c_name, int p_beg_i, int p_end_i, int *len) + cdef extern from "pysam_util.h": int pysam_pileup_next(bam1_t *b, @@ -280,4 +378,12 @@ cdef extern from "pysam_util.h": # translate char to unsigned char unsigned char pysam_translate_sequence( char s ) - + unsigned char * bam_nt16_table + + int pysam_reference2tid( bam_header_t *header, char * s ) + + void pysam_set_stderr( FILE * file ) + + uint32_t pysam_glf_depth( glf1_t * g ) + + void pysam_dump_glf( glf1_t * g, bam_maqcns_t * c ) diff --git a/pysam/csamtools.pyx b/pysam/csamtools.pyx index e94e0e6..105866b 100644 --- a/pysam/csamtools.pyx +++ b/pysam/csamtools.pyx @@ -1,11 +1,10 @@ -# cython: embedsignature=True + # cython: embedsignature=True # cython: profile=True # adds doc-strings for sphinx +import tempfile, os, sys, types, struct, ctypes, collections, re -import tempfile, os, sys, types, itertools, struct, ctypes - -from python_string cimport PyString_FromStringAndSize, PyString_AS_STRING -from python_exc cimport PyErr_SetString +from cpython cimport PyString_FromStringAndSize, PyString_AS_STRING +from cpython cimport PyErr_SetString # defines imported from samtools DEF SEEK_SET = 0 @@ -48,26 +47,30 @@ DEF BAM_CSOFT_CLIP = 4 DEF BAM_CHARD_CLIP = 5 DEF BAM_CPAD = 6 +##################################################################### +# hard-coded constants +cdef char * bam_nt16_rev_table = "=ACMGRSVTWYHKDBN" +cdef int max_pos = 2 << 29 + +# redirect stderr to 0 +_logfile = open("/dev/null", "w") +pysam_set_stderr( PyFile_AsFile( _logfile ) ) + ##################################################################### ##################################################################### ##################################################################### ## private factory methods ##################################################################### cdef class AlignedRead -cdef makeAlignedRead( bam1_t * src): +cdef makeAlignedRead(bam1_t * src): '''enter src into AlignedRead.''' - cdef AlignedRead dest - dest = AlignedRead() - # destroy dummy delegate created in constructor - # to prevent memory leak. - bam_destroy1(dest._delegate) + 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 ): - cdef PileupProxy dest - dest = PileupProxy() + cdef PileupProxy dest = PileupProxy.__new__(PileupProxy) dest.plp = plp dest.tid = tid dest.pos = pos @@ -77,8 +80,7 @@ cdef makePileupProxy( bam_pileup1_t * plp, int tid, int pos, int n ): cdef class PileupRead cdef makePileupRead( bam_pileup1_t * src ): '''fill a PileupRead object from a bam_pileup1_t * object.''' - cdef PileupRead dest - dest = PileupRead() + cdef PileupRead dest = PileupRead.__new__(PileupRead) dest._alignment = makeAlignedRead( src.b ) dest._qpos = src.qpos dest._indel = src.indel @@ -163,11 +165,13 @@ class StderrStore(): stderr is captured. ''' def __init__(self): + return self.stderr_h, self.stderr_f = tempfile.mkstemp() self.stderr_save = Outs( sys.stderr.fileno() ) self.stderr_save.setfd( self.stderr_h ) def readAndRelease( self ): + return [] self.stderr_save.restore() lines = [] if os.path.exists(self.stderr_f): @@ -176,6 +180,7 @@ class StderrStore(): return lines def release(self): + return self.stderr_save.restore() if os.path.exists(self.stderr_f): os.remove( self.stderr_f ) @@ -200,13 +205,13 @@ VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO" ) VALID_HEADER_FIELDS = { "HD" : { "VN" : str, "SO" : str, "GO" : str }, "SQ" : { "SN" : str, "LN" : int, "AS" : str, "M5" : str, "UR" : str, "SP" : str }, "RG" : { "ID" : str, "SM" : str, "LB" : str, "DS" : str, "PU" : str, "PI" : str, "CN" : str, "DT" : str, "PL" : str, }, - "PG" : { "ID" : str, "VN" : str, "CL" : str }, } + "PG" : { "PN" : str, "ID" : str, "VN" : str, "CL" : 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" ), - "PG" : ( "ID", "VN", "CL" ), } + "PG" : ( "PN", "ID", "VN", "CL" ), } ###################################################################### @@ -214,39 +219,213 @@ VALID_HEADER_ORDER = { "HD" : ( "VN", "SO", "GO" ), ###################################################################### ## Public methods ###################################################################### +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. + ''' + + cdef char * _filename + # pointer to fastafile + cdef faidx_t * fastafile + + def __cinit__(self, *args, **kwargs ): + self.fastafile = NULL + self._filename = NULL + self._open( *args, **kwargs ) + + def _isOpen( self ): + '''return true if samfile has been opened.''' + return self.fastafile != NULL + + def __len__(self): + if self.fastafile == NULL: + raise ValueError( "calling len() on closed file" ) + + return faidx_fetch_nseq(self.fastafile) + + def _open( self, + char * filename ): + '''open an indexed fasta file. + + This method expects an indexed fasta file. + ''' + + # close a previously opened file + if self.fastafile != NULL: self.close() + if self._filename != NULL: free(self._filename) + self._filename = strdup(filename) + self.fastafile = fai_load( filename ) + + if self.fastafile == NULL: + raise IOError("could not open file `%s`" % filename ) + + def close( self ): + if self.fastafile != NULL: + fai_destroy( self.fastafile ) + self.fastafile = NULL + + def __dealloc__(self): + self.close() + if self._filename != NULL: free(self._filename) + + property filename: + '''number of :term:`filename` associated with this object.''' + def __get__(self): + if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + return self._filename + + def fetch( self, + reference = None, + start = None, + end = None, + region = None): + + '''*(reference = None, start = None, end = None, region = None)* + + fetch :meth:`AlignedRead` objects 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*. + + If *reference* is given and *start* is None, the sequence from the + first base is returned. Similarly, if *end* is None, the sequence + until the last base is returned. + + Alternatively, a samtools :term:`region` string can be supplied. + ''' + + if not self._isOpen(): + raise ValueError( "I/O operation on closed file" ) + + cdef int length + cdef char * seq + + if not region: + if reference is None: raise ValueError( 'no sequence/region supplied.' ) + if start is None: start = 0 + if end is None: end = max_pos -1 + + if start > end: raise ValueError( 'invalid region: start (%i) > end (%i)' % (start, end) ) + if start == end: return "" + # valid ranges are from 0 to 2^29-1 + if not 0 <= start < max_pos: raise ValueError( 'start out of range (%i)' % start ) + if not 0 <= end < max_pos: raise ValueError( '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, + # but use fai_fetch instead + # seq = faidx_fetch_seq(self.fastafile, + # reference, + # start, + # end-1, + # &length) + region = "%s:%i-%i" % (reference, start+1, end) + seq = fai_fetch( self.fastafile, + region, + &length ) + else: + # samtools adds a '\0' at the end + seq = fai_fetch( self.fastafile, region, &length ) + + # copy to python + if seq == NULL: + return "" + else: + try: + py_seq = PyString_FromStringAndSize(seq, length) + finally: + free(seq) + + return py_seq + + cdef char * _fetch( self, char * reference, int start, int end, int * length ): + '''fetch sequence for reference, start and end''' + + return faidx_fetch_seq(self.fastafile, + reference, + start, + end-1, + length ) + +#------------------------------------------------------------------------ +#------------------------------------------------------------------------ +#------------------------------------------------------------------------ +cdef int count_callback( bam1_t *alignment, void *f): + '''callback for bam_fetch - count number of reads. + ''' + cdef int* counter = (f) + counter[0] += 1; + +ctypedef struct MateData: + char * name + bam1_t * mate + uint32_t flag + +#------------------------------------------------------------------------ +#------------------------------------------------------------------------ +#------------------------------------------------------------------------ +cdef int mate_callback( bam1_t *alignment, void *f): + '''callback for bam_fetch = filter mate + ''' + cdef MateData * d = (f) + # printf("mate = %p, name1 = %s, name2=%s\t%i\t%i\t%i\n", + # d.mate, d.name, bam1_qname(alignment), + # d.flag, alignment.core.flag, alignment.core.flag & d.flag) + + if d.mate == NULL: + # could be sped up by comparing the lengths of query strings first + # using l_qname + # + # also, make sure that we get the other read by comparing + # the flags + if alignment.core.flag & d.flag != 0 and \ + strcmp( bam1_qname( alignment ), d.name ) == 0: + d.mate = bam_dup1( alignment ) + + cdef class Samfile: '''*(filename, mode='r', template = None, referencenames = None, referencelengths = None, text = NULL, header = None)* - A *SAM* file. The file is automatically opened. + 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 so 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``. - Currently valid modes are ``r``, ``w``, ``wh``, ``rb``, ``wb`` and ``wbu``. - - so to open a :term:`BAM` file for reading:: + Valid modes are ``r``, ``w``, ``wh``, ``rb``, ``wb`` and ``wbu``. For instance, to open + a :term:`BAM` formatted file for reading, type:: - f=Samfile('ex1.bam','rb') + import pysam + f = pysam.Samfile('ex1.bam','rb') + 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:`TAM` file/:term:`BAM` file can be constituted from several - sources: + 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*). + 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 build from a multi-level dictionary. The first level are the four types ('HD', 'SQ', ...). The second level is then a list of lines, with each line being a list of tag-value pairs. + 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. 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. - 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. ''' - cdef char * filename + cdef char * _filename # pointer to samfile cdef samfile_t * samfile # pointer to index @@ -262,6 +441,7 @@ cdef class Samfile: def __cinit__(self, *args, **kwargs ): self.samfile = NULL + self._filename = NULL self.isbam = False self._open( *args, **kwargs ) @@ -293,6 +473,7 @@ cdef class Samfile: ''' assert mode in ( "r","w","rb","wb", "wh", "wbu" ), "invalid file opening mode `%s`" % mode + assert filename != NULL # close a previously opened file if self.samfile != NULL: self.close() @@ -300,8 +481,9 @@ cdef class Samfile: cdef bam_header_t * header_to_write header_to_write = NULL - - self.filename = filename + + if self._filename != NULL: free(self._filename ) + self._filename = strdup( filename ) self.isbam = len(mode) > 1 and mode[1] == 'b' @@ -367,15 +549,16 @@ cdef class Samfile: not os.path.exists( filename ): raise IOError( "file `%s` not found" % filename) - store = StderrStore() + # try to detect errors self.samfile = samopen( filename, mode, NULL ) - result = store.readAndRelease() - # test for specific messages as open also outputs status messages - # that can be ignored. - if "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n" in result: - raise ValueError( "invalid BAM binary header (is this a BAM file?)" ) - elif '[samopen] no @SQ lines in the header.\n' in result: - raise ValueError( "no @SQ lines in the header (is this a SAM file?)") + if self.samfile == NULL: + raise ValueError( "could not open file - is it SAM/BAM format?") + + if self.samfile.header == NULL: + raise ValueError( "could not open file - is it SAM/BAM format?") + + if self.samfile.header.n_targets == 0: + raise ValueError( "could not open file - is it SAM/BAM format?") if self.samfile == NULL: raise IOError("could not open file `%s`" % filename ) @@ -397,25 +580,34 @@ cdef class Samfile: raise IOError("error while opening index `%s` " % filename ) def getrname( self, tid ): - '''(tid ) - convert numerical :term:`tid` into :ref:`reference` name.''' + ''' + 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.samfile.header.n_targets: raise ValueError( "tid out of range 0<=tid<%i" % self.samfile.header.n_targets ) return self.samfile.header.target_name[tid] + 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" ) + return pysam_reference2tid( self.samfile.header, reference ) + def _parseRegion( self, reference = None, start = None, - end = None, + end = None, region = None ): - '''parse region information. + ''' + parse region information. - raise Value for for invalid regions. + raise ValueError for for invalid regions. - returns a tuple of region, tid, start and end. Region - is a valid samtools :term:`region` or None if the region - extends over the whole file. + 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. ''' @@ -426,32 +618,33 @@ cdef class Samfile: cdef int rtid cdef int rstart cdef int rend - cdef int max_pos - max_pos = 2 << 29 - rtid = rstart = rend = 0 - - # translate to a region - if reference: - if start != None and end != None: - if start > end: raise ValueError( 'invalid region: start (%i) > end (%i)' % (start, end) ) - region = "%s:%i-%i" % (reference, start+1, end) - else: - region = reference + rtid = -1 + rstart = 0 + rend = max_pos + if start != None: rstart = start + if end != None: rend = end if region: - # this function might be called often (multiprocessing) - # thus avoid using StderrStore, see issue 46. - bam_parse_region( self.samfile.header, region, &rtid, &rstart, &rend) - if rtid < 0: raise ValueError( "invalid region `%s`" % region ) - if rstart > rend: raise ValueError( 'invalid region: 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 region, rtid, rstart, rend + 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 + + 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 seek( self, uint64_t offset, int where = 0): - '''move to current file to position *offset*''' + ''' + move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`. + ''' if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) @@ -460,7 +653,9 @@ cdef class Samfile: return bam_seek( self.samfile.x.bam, offset, where ) def tell( self ): - '''return current file position''' + ''' + return current file position + ''' if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) if not self.isbam: @@ -475,42 +670,39 @@ cdef class Samfile: region = None, callback = None, until_eof = False ): - '''*(reference = None, start = None, end = None, region = None, callback = None, until_eof = False)* - - fetch :meth:`AlignedRead` objects 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. + ''' + 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 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 - *as they are sorted within the file*. + *in order as they are within the file*. - If only *reference* is set, all reads matching on *reference* will be fetched. + If only *reference* is set, all reads aligned to *reference* will be fetched. The method returns an iterator of type :class:`pysam.IteratorRow` unless a *callback is provided. If *callback* is given, the callback will be executed for each position within the :term:`region`. Note that callbacks currently work only, if *region* or *reference* is given. - Note that a :term:`TAM` file does not allow random access. If *region* or *reference* are given, + Note that a :term:`SAM` file does not allow random access. If *region* or *reference* are given, an exception is raised. ''' - cdef int rtid - cdef int rstart - cdef int rend + cdef int rtid, rstart, rend, has_coord if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) - - region, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + has_coord, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + if self.isbam: if not until_eof and not self._hasIndex() and not self.isremote: raise ValueError( "fetch called on bamfile without index" ) if callback: - if not region: - raise ValueError( "callback functionality requires a region/reference" ) + if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) if not self._hasIndex(): raise ValueError( "no index available for fetch" ) return bam_fetch(self.samfile.x.bam, self.index, @@ -520,20 +712,13 @@ cdef class Samfile: callback, fetch_callback ) else: - if region: - return IteratorRow( self, rtid, rstart, rend ) + if has_coord: + return IteratorRowRegion( self, rtid, rstart, rend ) else: if until_eof: return IteratorRowAll( self ) else: - # return all targets by chaining the individual targets together. - if not self._hasIndex(): raise ValueError( "no index available for fetch" ) - i = [] - rstart = 0 - rend = 1<<29 - for rtid from 0 <= rtid < self.nreferences: - i.append( IteratorRow( self, rtid, rstart, rend)) - return itertools.chain( *i ) + return IteratorRowAllRefs(self) else: # check if header is present - otherwise sam_read1 aborts # this happens if a bamfile is opened with mode 'r' @@ -547,41 +732,151 @@ cdef class Samfile: else: return IteratorRowAll( self ) - def pileup( self, reference = None, start = None, end = None, region = None, callback = None ): - '''run a pileup within a :term:`region` using 0-based indexing. The region is specified by - :term:`reference`, *start* and *end*. Alternatively, a samtools *region* string can be supplied. + def mate( self, + AlignedRead read ): + '''return the mate of :class:`AlignedRead` *read*. - Without *reference* or *region* all reads will be fetched. The reads will be returned + 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. + + ''' + if not read.is_paired: + raise ValueError( "read is unpaired" ) + if read.mate_is_unmapped: + raise ValueError( "mate is unmapped" ) + + cdef MateData mate_data + + mate_data.name = bam1_qname(read._delegate) + mate_data.mate = NULL + # xor flags to get the other mate + cdef int x = BAM_FREAD1 + BAM_FREAD2 + mate_data.flag = ( read._delegate.core.flag ^ x) & x + + bam_fetch(self.samfile.x.bam, + self.index, + read._delegate.core.mtid, + read._delegate.core.mpos, + read._delegate.core.mpos + 1, + &mate_data, + mate_callback ) + + if mate_data.mate == NULL: + raise ValueError( "mate not found" ) + + cdef AlignedRead dest = AlignedRead.__new__(AlignedRead) + dest._delegate = mate_data.mate + return dest + + 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 int rtid + cdef int rstart + cdef int rend + + if not self._isOpen(): + raise ValueError( "I/O operation on closed file" ) + + region, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) + + cdef int counter + counter = 0; + + if self.isbam: + if not until_eof and not self._hasIndex() and not self.isremote: + raise ValueError( "fetch called on bamfile without index" ) + + if not region: + raise ValueError( "counting functionality requires a region/reference" ) + if not self._hasIndex(): raise ValueError( "no index available for fetch" ) + bam_fetch(self.samfile.x.bam, + self.index, + rtid, + rstart, + rend, + &counter, + count_callback ) + return counter + else: + raise ValueError ("count for a region is not available for sam files" ) + + + def pileup( self, + reference = None, + start = None, + end = None, + region = None, + callback = 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 *callback* is given, the callback will be executed - for each position within the :term:`region`. + a *callback is provided. If a *callback* is given, the callback will be executed + for each column within the :term:`region`. - Note that samfiles do not allow random access. If *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. - .. Note:: + 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. + + + .. 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 - cdef int rstart - cdef int rend + cdef int rtid, rstart, rend, has_coord cdef bam_plbuf_t *buf if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) - region, rtid, rstart, rend = self._parseRegion( reference, start, end, region ) - + 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 callback: - if not region: - raise ValueError( "callback functionality requires a region/reference" ) + if not has_coord: raise ValueError( "callback functionality requires a region/reference" ) buf = bam_plbuf_init( pileup_callback, callback ) bam_fetch(self.samfile.x.bam, @@ -592,22 +887,21 @@ cdef class Samfile: bam_plbuf_push( NULL, buf) bam_plbuf_destroy(buf) else: - if region: - return IteratorColumn( self, rtid, rstart, rend ) + if has_coord: + return IteratorColumnRegion( self, + tid = rtid, + start = rstart, + end = rend, + **kwargs ) else: - # return all targets by chaining the individual targets together. - i = [] - rstart = 0 - rend = 1<<29 - for rtid from 0 <= rtid < self.nreferences: - i.append( IteratorColumn( self, rtid, rstart, rend)) - return itertools.chain( *i ) + return IteratorColumnAllRefs(self, **kwargs ) else: raise NotImplementedError( "pileup of samfiles not implemented yet" ) def close( self ): - '''closes file.''' + ''' + closes the :class:`pysam.Samfile`.''' if self.samfile != NULL: samclose( self.samfile ) bam_index_destroy(self.index); @@ -619,12 +913,13 @@ cdef class Samfile: # note: __del__ is not called. self.close() bam_destroy1(self.b) + if self._filename != NULL: free( self._filename ) def write( self, AlignedRead read ): - '''(AlignedRead read ) - write a single :class:`pysam.AlignedRead`.. + ''' + write a single :class:`pysam.AlignedRead` to disk. - return the number of bytes written. + returns the number of bytes written. ''' if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) @@ -638,6 +933,17 @@ cdef class Samfile: self.close() return False + ############################################################### + ############################################################### + ############################################################### + ## properties + ############################################################### + property filename: + '''number of :term:`filename` associated with this object.''' + def __get__(self): + if not self._isOpen(): raise ValueError( "I/O operation on closed file" ) + return self._filename + property nreferences: '''number of :term:`reference` sequences in the file.''' def __get__(self): @@ -654,7 +960,8 @@ 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.reference` + """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" ) @@ -779,6 +1086,14 @@ cdef class Samfile: 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" ) return self @@ -787,14 +1102,15 @@ cdef class Samfile: return self.b cdef int cnext(self): - '''cversion of iterator. Used by IteratorColumn''' + ''' + cversion of iterator. Used by :class:`pysam.Samfile.IteratorColumn`. + ''' cdef int ret return samread(self.samfile, self.b) def __next__(self): - """python version of next(). - - pyrex uses this non-standard name instead of next() + """ + python version of next(). """ cdef int ret ret = samread(self.samfile, self.b) @@ -803,121 +1119,36 @@ cdef class Samfile: else: raise StopIteration -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. - ''' - - cdef char * filename - # pointer to fastafile - cdef faidx_t * fastafile - - def __cinit__(self, *args, **kwargs ): - self.fastafile = NULL - self._open( *args, **kwargs ) - - def _isOpen( self ): - '''return true if samfile has been opened.''' - return self.fastafile != NULL - - def __len__(self): - if self.fastafile == NULL: - raise ValueError( "calling len() on closed file" ) - - return faidx_fetch_nseq(self.fastafile) - - def _open( self, - char * filename ): - '''open an indexed fasta file. - - This method expects an indexed fasta file. - ''' - - # close a previously opened file - if self.fastafile != NULL: self.close() - self.filename = filename - self.fastafile = fai_load( filename ) +##------------------------------------------------------------------- +##------------------------------------------------------------------- +##------------------------------------------------------------------- +cdef class IteratorRow: + '''abstract base class for iterators over mapped reads. - if self.fastafile == NULL: - raise IOError("could not open file `%s`" % filename ) + Various iterators implement different behaviours for wrapping around + contig boundaries. Examples include: - def close( self ): - if self.fastafile != NULL: - fai_destroy( self.fastafile ) - self.fastafile = NULL + :class:`pysam.IteratorRowRegion` + iterate within a single contig and a defined region. - def fetch( self, - reference = None, - start = None, - end = None, - region = None): - - '''*(reference = None, start = None, end = None, region = None)* - - fetch :meth:`AlignedRead` objects in a :term:`region` using 0-based indexing. - The region is specified by :term:`reference`, *start* and *end*. + :class:`pysam.IteratorRowAll` + iterate until EOF. This iterator will also include unmapped reads. - If *reference* is given and *start* is None, the sequence from the - first base is returned. Similarly, if *end* is None, the sequence - until the last base is returned. + :class:`pysam.IteratorRowAllRefs` + iterate over all reads in all reference sequences. - Alternatively, a samtools :term:`region` string can be supplied. - ''' - - if not self._isOpen(): - raise ValueError( "I/O operation on closed file" ) - - cdef int length, max_pos - cdef char * seq - max_pos = 2 << 29 - - if not region: - if reference is None: raise ValueError( 'no sequence/region supplied.' ) - if start is None: start = 0 - if end is None: end = max_pos -1 + The method :meth:`Samfile.fetch` returns an IteratorRow. + ''' + pass - if start > end: raise ValueError( 'invalid region: start (%i) > end (%i)' % (start, end) ) - if start == end: return "" - # valid ranges are from 0 to 2^29-1 - if not 0 <= start < max_pos: raise ValueError( 'start out of range (%i)' % start ) - if not 0 <= end < max_pos: raise ValueError( 'end out of range (%i)' % end ) - seq = faidx_fetch_seq(self.fastafile, - reference, - start, - end-1, - &length) - else: - # samtools adds a '\0' at the end - seq = fai_fetch( self.fastafile, region, &length ) +cdef class IteratorRowRegion(IteratorRow): + """*(Samfile samfile, int tid, int beg, int end, int reopen = True )* - # copy to python - if seq == NULL: - return "" - else: - try: - py_seq = PyString_FromStringAndSize(seq, length) - finally: - free(seq) + iterate over mapped reads in a region. - return py_seq - -########################################################################### -########################################################################### -########################################################################### -## turning callbacks elegantly into iterators is an unsolved problem, see the following threads: -## http://groups.google.com/group/comp.lang.python/browse_frm/thread/0ce55373f128aa4e/1d27a78ca6408134?hl=en&pli=1 -## http://www.velocityreviews.com/forums/t359277-turning-a-callback-function-into-a-generator.html -## Thus I chose to rewrite the functions requiring callbacks. The downside is that if the samtools C-API or code -## changes, the changes have to be manually entered. -cdef class IteratorRow: - """iterates over mapped reads in a region. + 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*. The samtools iterators assume that the file position between iterations do not change. @@ -936,13 +1167,13 @@ cdef class IteratorRow: cdef Samfile samfile cdef samfile_t * fp - def __cinit__(self, Samfile samfile, int tid, int beg, int end ): + def __cinit__(self, Samfile samfile, int tid, int beg, int end, int reopen = True ): if not samfile._isOpen(): raise ValueError( "I/O operation on closed file" ) if not samfile._hasIndex(): - raise ValueError( "no index available for pileup" ) + raise ValueError( "no index available for iteration" ) # makes sure that samfile stays alive as long as the # iterator is alive @@ -951,10 +1182,13 @@ cdef class IteratorRow: if samfile.isbam: mode = "rb" else: mode = "r" - # reopen the file - store = StderrStore() - self.fp = samopen( samfile.filename, mode, NULL ) - store.release() + # reopen the file - note that this makes the iterator + # slow and causes pileup to slow down significantly. + if reopen: + store = StderrStore() + self.fp = samopen( samfile._filename, mode, NULL ) + store.release() + assert self.fp != NULL self.retval = 0 @@ -987,14 +1221,20 @@ cdef class IteratorRow: bam_destroy1(self.b) samclose( self.fp ) -cdef class IteratorRowAll: - """iterates over all mapped reads +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 if + multiple operators work on the same file. Set *reopen* = False + to not re-open *samfile*. """ cdef bam1_t * b cdef samfile_t * fp - def __cinit__(self, Samfile samfile): + def __cinit__(self, Samfile samfile, int reopen = True ): if not samfile._isOpen(): raise ValueError( "I/O operation on closed file" ) @@ -1003,122 +1243,397 @@ cdef class IteratorRowAll: else: mode = "r" # reopen the file to avoid iterator conflict - store = StderrStore() - self.fp = samopen( samfile.filename, mode, NULL ) - store.release() + if reopen: + store = StderrStore() + self.fp = samopen( samfile._filename, mode, NULL ) + store.release() + assert self.fp != NULL # allocate memory for alignment self.b = calloc(1, sizeof(bam1_t)) - def __iter__(self): - return self + def __iter__(self): + return self + + cdef bam1_t * getCurrent( self ): + return self.b + + cdef int cnext(self): + '''cversion of iterator. Used by IteratorColumn''' + cdef int ret + return samread(self.fp, self.b) + + def __next__(self): + """python version of next(). + + pyrex uses this non-standard name instead of next() + """ + cdef int ret + ret = samread(self.fp, self.b) + if (ret > 0): + return makeAlignedRead( self.b ) + else: + raise StopIteration + + def __dealloc__(self): + bam_destroy1(self.b) + samclose( self.fp ) + + +cdef class IteratorRowAllRefs(IteratorRow): + """iterates over all mapped reads by chaining iterators over each reference + """ + cdef Samfile samfile + cdef int tid + cdef IteratorRowRegion rowiter + + def __cinit__(self, Samfile samfile): + assert samfile._isOpen() + if not samfile._hasIndex(): raise ValueError("no index available for fetch") + self.samfile = samfile + 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.tiddata + return bam_iter_read( d.samfile.x.bam, d.iter, b ) + +cdef int __advance_snpcalls( void * data, bam1_t * b ): + '''advance using same filter and read processing as in + the samtools pileup. + ''' + cdef __iterdata * d + d = <__iterdata*>data + + cdef int ret = bam_iter_read( d.samfile.x.bam, 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.samfile.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.samfile.header.target_name[d.tid], + d.tid)) + - cdef bam1_t * getCurrent( self ): - return self.b + while ret >= 0: - cdef int cnext(self): - '''cversion of iterator. Used by IteratorColumn''' - cdef int ret - return samread(self.fp, self.b) + skip = 0 - def __next__(self): - """python version of next(). + # realign read - changes base qualities + if d.seq != NULL and is_cns and not is_nobaq: bam_prob_realn( b, d.seq ) - pyrex uses this non-standard name instead of next() - """ - cdef int ret - ret = samread(self.fp, self.b) - if (ret > 0): - return makeAlignedRead( self.b ) - else: - raise StopIteration + 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 - def __dealloc__(self): - bam_destroy1(self.b) - samclose( self.fp ) + if not skip: break + # additional filters -ctypedef struct __iterdata: - bamFile fp - bam_iter_t iter + ret = bam_iter_read( d.samfile.x.bam, d.iter, b ) -cdef int __advance( void * data, bam1_t * b ): - cdef __iterdata * d - d = <__iterdata*>data - return bam_iter_read( d.fp, d.iter, b ) + return ret cdef class IteratorColumn: - '''iterates over columns. + '''abstract base class for iterators over columns. - This iterator wraps the pileup functionality of samtools. + IteratorColumn objects wrap the pileup functionality of samtools. - For reasons of efficiency, the iterator returns the current - pileup buffer. As this buffer is updated at every iteration, - the contents of this iterator will change accordingly. Hence the conversion to - a list will not produce the expected result:: + 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 will contain the same information. + Here, ``result`` will contain ``n`` objects of type :class:`PileupProxy` for ``n`` columns, + but each object in ``result`` will contain the same information. - If the results of several columns are required at the same time, the results - need to be stored explicitely:: + The desired behaviour can be achieved by list comprehension:: result = [ x.pileups() for x in f.pileup() ] - Here, 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 current sequence via the methods :meth:`getSequence` + and :meth:`seq_len`. + 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. + + ''' # result of the last plbuf_push - cdef IteratorRow iter + cdef IteratorRowRegion iter cdef int tid cdef int pos cdef int n_plp - cdef bam_pileup1_t * plp + cdef int mask + cdef const_bam_pileup1_t_ptr plp cdef bam_plp_t pileup_iter cdef __iterdata iterdata - def __cinit__(self, Samfile samfile, int tid, int start, int end ): - - self.iter = IteratorRow( samfile, tid, start, end ) - self.iterdata.fp = samfile.samfile.x.bam - self.iterdata.iter = self.iter.iter + cdef Samfile samfile + cdef Fastafile fastafile + cdef stepper - self.pileup_iter = bam_plp_init( &__advance, &self.iterdata ) - self.n_plp = 0 + def __cinit__( self, Samfile samfile, **kwargs ): + self.samfile = samfile + self.mask = kwargs.get("mask", BAM_DEF_MASK ) + self.fastafile = kwargs.get( "fastafile", None ) + self.stepper = kwargs.get( "stepper", None ) + self.iterdata.seq = NULL self.tid = 0 self.pos = 0 + self.n_plp = 0 self.plp = NULL + self.pileup_iter = NULL def __iter__(self): return self cdef int cnext(self): '''perform next iteration. + + This method is analogous to the samtools bam_plp_auto method. + It has been re-implemented to permit for filtering. ''' 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. + ''' + 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.samfile = self.samfile.samfile + self.iterdata.iter = self.iter.iter + self.iterdata.seq = NULL + self.iterdata.tid = -1 + + 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( &__advance_all, &self.iterdata ) + elif self.stepper == "samtools": + self.pileup_iter = bam_plp_init( &__advance_snpcalls, &self.iterdata ) + else: + raise ValueError( "unknown stepper option `%s` in IteratorColumn" % self.stepper) + + 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 != NULL: + bam_plp_reset(self.pileup_iter) + bam_plp_destroy(self.pileup_iter) + self.pileup_iter = 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, + **kwargs ): + + # initialize iterator + self.setupIteratorData( tid, start, end, 1 ) + def __next__(self): """python version of next(). - - pyrex uses this non-standard name instead of next() """ - self.cnext() - if self.n_plp < 0: - raise ValueError("error during iteration" ) + + while 1: + self.cnext() + if self.n_plp < 0: + raise ValueError("error during iteration" ) - if self.plp == NULL: - raise StopIteration + if self.plp == NULL: + 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 + """ - return makePileupProxy( self.plp, self.tid, self.pos, self.n_plp ) + def __cinit__(self, + Samfile samfile, + **kwargs ): - def __dealloc__(self): - bam_plp_destroy(self.pileup_iter) + # 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, op cdef uint32_t k @@ -1139,7 +1654,9 @@ 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 uint32_t * cigar_p, op cdef uint32_t k @@ -1168,7 +1685,6 @@ 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 - cdef char * bam_nt16_rev_table = "=ACMGRSVTWYHKDBN" if not src.core.l_qseq: return None @@ -1226,7 +1742,8 @@ cdef class AlignedRead: cdef: bam1_t * _delegate - def __cinit__( self ): + # Now only called when instances are created from Python + def __init__(self): # see bam_init1 self._delegate = calloc( 1, sizeof( bam1_t) ) # allocate some memory @@ -1355,7 +1872,7 @@ cdef class AlignedRead: pysam_bam_update( src, src.core.n_cigar * 4, len(values) * 4, - p ) + p ) # length is number of cigar operations, not bytes src.core.n_cigar = len(values) @@ -1377,7 +1894,6 @@ cdef class AlignedRead: def __get__(self): cdef bam1_t * src cdef char * s - src = self._delegate if src.core.l_qseq == 0: return None @@ -1599,13 +2115,13 @@ cdef class AlignedRead: for pytag, value in tags: t = type(value) if t == types.FloatType: - fmt = "= -127: fmt, pytype = "= -32767: fmt, pytype = "bam_aux2i(v) - elif type == 'f': + elif type == 'i' or type == 'I': + return bam_aux2i(v) + elif type == 'f' or type == 'F': return bam_aux2f(v) - elif type == 'd': + elif type == 'd' or type == 'D': return bam_aux2d(v) elif type == 'A': # there might a more efficient way @@ -1871,8 +2410,8 @@ cdef class PileupProxy: cdef int pos cdef int n_pu - def __cinit__(self ): - pass + 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))) +\ @@ -1915,8 +2454,8 @@ cdef class PileupRead: uint32_t _is_head uint32_t _is_tail - def __cinit__( self ): - 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 ) ) ) @@ -1981,7 +2520,11 @@ class Outs: os.close(self.streams[-1]) del self.streams[-1] -def _samtools_dispatch( method, args = () ): +def _samtools_dispatch( method, + args = (), + catch_stdout = True, + catch_stderr = False, + ): '''call ``method`` in samtools providing arguments in args. .. note:: @@ -2004,23 +2547,29 @@ def _samtools_dispatch( method, args = () ): # note that debugging this module can be a problem # as stdout/stderr will not appear + # some special cases + if method == "index": + if not os.path.exists( args[0] ): + raise IOError( "No such file or directory: '%s'" % args[0] ) + # redirect stderr and stdout to file + if catch_stderr: + stderr_h, stderr_f = tempfile.mkstemp() + stderr_save = Outs( sys.stderr.fileno() ) + stderr_save.setfd( stderr_h ) - # open files and redirect into it - stderr_h, stderr_f = tempfile.mkstemp() - stdout_h, stdout_f = tempfile.mkstemp() + if catch_stdout: + stdout_h, stdout_f = tempfile.mkstemp() + stdout_save = Outs( sys.stdout.fileno() ) + stdout_save.setfd( stdout_h ) - # patch for `samtools view` - # samtools `view` closes stdout, from which I can not - # recover. Thus redirect output to file with -o option. - if method == "view": - if "-o" in args: raise ValueError("option -o is forbidden in samtools view") - args = ( "-o", stdout_f ) + args + # patch for `samtools view` + # samtools `view` closes stdout, from which I can not + # recover. Thus redirect output to file with -o option. + if method == "view": + if "-o" in args: raise ValueError("option -o is forbidden in samtools view") + args = ( "-o", stdout_f ) + args - stdout_save = Outs( sys.stdout.fileno() ) - stdout_save.setfd( stdout_h ) - stderr_save = Outs( sys.stderr.fileno() ) - stderr_save.setfd( stderr_h ) # do the function call to samtools cdef char ** cargs @@ -2037,28 +2586,586 @@ def _samtools_dispatch( method, args = () ): # restore stdout/stderr. This will also flush, so # needs to be before reading back the file contents - stdout_save.restore() - stderr_save.restore() + if catch_stdout: + stdout_save.restore() + out_stdout = open( stdout_f, "r").readlines() + os.remove( stdout_f ) + else: + out_stdout = [] + + if catch_stderr: + stderr_save.restore() + out_stderr = open( stderr_f, "r").readlines() + os.remove( stderr_f ) + else: + out_stderr = [] + + return retval, out_stderr, out_stdout + +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 - # capture stderr/stdout. - out_stderr = open( stderr_f, "r").readlines() - out_stdout = open( stdout_f, "r").readlines() + property reference_base: + '''reference base at pos. ``N`` if no reference sequence supplied.''' + def __get__(self): return PyString_FromStringAndSize( &self._reference_base, 1 ) - # clean up files - os.remove( stderr_f ) - os.remove( stdout_f ) + property genotype: + '''the genotype called.''' + def __get__(self): return PyString_FromStringAndSize( &self._genotype, 1 ) - return retval, out_stderr, out_stdout + 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 SNPCallerBase: + '''Base class for SNP callers. + + *min_baseQ* + minimum base quality (possibly capped by BAQ) + *capQ_threshold* + coefficient for adjusting mapQ of poor mappings + *theta* + theta in maq consensus calling model + *n_haplotypes* + number of haplotypes in the sample + *het_rate* + prior of a difference between two haplotypes + ''' + + cdef bam_maqcns_t * c + cdef IteratorColumn iter + + def __cinit__(self, + IteratorColumn iterator_column, + **kwargs ): + + self.iter = iterator_column + self.c = bam_maqcns_init() + + # set the default parameterization according to + # samtools + + # new default mode for samtools >0.1.10 + self.c.errmod = kwargs.get( "errmod", BAM_ERRMOD_MAQ2 ) + + self.c.min_baseQ = kwargs.get( "min_baseQ", 13 ) + # self.c.capQ_thres = kwargs.get( "capQ_threshold", 60 ) + self.c.n_hap = kwargs.get( "n_haplotypes", 2 ) + self.c.het_rate = kwargs.get( "het_rate", 0.001 ) + self.c.theta = kwargs.get( "theta", 0.83 ) + + if self.c.errmod != BAM_ERRMOD_MAQ2: + self.c.theta += 0.02 + + # call prepare AFTER setting parameters + bam_maqcns_prepare( self.c ) + + def __dealloc__(self): + bam_maqcns_destroy( self.c ) + + cdef __dump( self, glf1_t * g, uint32_t cns, int rb ): + '''debugging output.''' + + pysam_dump_glf( g, self.c ); + print "" + for x in range(self.iter.n_plp): + print "--> read %i %s %i" % (x, + bam1_qname(self.iter.plp[x].b), + self.iter.plp[x].qpos, + ) + + print "pos=%i, cns=%i, q_r = %f, depth=%i, n=%i, rb=%i, cns-cq=%i %i %i %i" \ + % (self.iter.pos, + cns, + self.c.q_r, + self.iter.n_plp, + self.iter.n_plp, + rb, + cns >> 8 & 0xff, + cns >> 16 & 0xff, + cns & 0xff, + cns >> 28, + ) + + printf("-------------------------------------\n"); + sys.stdout.flush() + +cdef class IteratorSNPCalls( SNPCallerBase ): + """*(IteratorColumn iterator)* + + call SNPs within a region. + + *iterator* is a pileup iterator. SNPs will be called + on all positions returned by this iterator. + + This caller is fast if SNPs are called over large continuous + regions. It is slow, if instantiated frequently and in random + order as the sequence will have to be reloaded. + + """ + + def __cinit__(self, + IteratorColumn iterator_column, + **kwargs ): + + assert self.iter.hasReference(), "IteratorSNPCalls requires an pileup iterator with reference sequence" + + def __iter__(self): + return self + + def __next__(self): + """python version of next(). + """ + + # the following code was adapted from bam_plcmd.c:pileup_func() + self.iter.cnext() + + if self.iter.n_plp < 0: + raise ValueError("error during iteration" ) + + if self.iter.plp == NULL: + raise StopIteration + + cdef char * seq = self.iter.getSequence() + cdef int seq_len = self.iter.seq_len + + assert seq != NULL + + # reference base + if self.iter.pos >= seq_len: + raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) + + cdef int rb = seq[self.iter.pos] + cdef uint32_t cns + cdef glf1_t * g + + g = bam_maqcns_glfgen( self.iter.n_plp, + self.iter.plp, + bam_nt16_table[rb], + self.c ) + + if pysam_glf_depth( g ) == 0: + cns = 0xfu << 28 | 0xf << 24 + else: + cns = glf2cns(g, (self.c.q_r + .499)) + + free(g) + + cdef SNPCall call + + call = SNPCall() + call._tid = self.iter.tid + call._pos = self.iter.pos + call._reference_base = rb + call._genotype = bam_nt16_rev_table[cns>>28] + call._consensus_quality = cns >> 8 & 0xff + call._snp_quality = cns & 0xff + call._rms_mapping_quality = cns >> 16&0xff + call._coverage = self.iter.n_plp + + return call + +cdef class SNPCaller( SNPCallerBase ): + '''*(IteratorColumn iterator_column )* + + The samtools SNP caller. + + This object will call SNPs in *samfile* against the reference + sequence in *fasta*. + + This caller is fast for calling few SNPs in selected regions. + + It is slow, if called over large genomic regions. + ''' + + + def __cinit__(self, + IteratorColumn iterator_column, + **kwargs ): + + pass + + def call(self, reference, int pos ): + """call a snp on chromosome *reference* + and position *pos*. + + returns a :class:`SNPCall` object. + """ + + cdef int tid = self.iter.samfile.gettid( reference ) + + self.iter.reset( tid, pos, pos + 1 ) + + while 1: + self.iter.cnext() + + if self.iter.n_plp < 0: + raise ValueError("error during iteration" ) + + if self.iter.plp == NULL: + raise ValueError( "no reads in region - no call" ) + + if self.iter.pos == pos: break + + cdef char * seq = self.iter.getSequence() + cdef int seq_len = self.iter.seq_len + + assert seq != NULL + + # reference base + if self.iter.pos >= seq_len: + raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) + + cdef int rb = seq[self.iter.pos] + cdef uint32_t cns + cdef glf1_t * g + + g = bam_maqcns_glfgen( self.iter.n_plp, + self.iter.plp, + bam_nt16_table[rb], + self.c ) + + + if pysam_glf_depth( g ) == 0: + cns = 0xfu << 28 | 0xf << 24 + else: + cns = glf2cns(g, (self.c.q_r + .499)) + + free(g) + + cdef SNPCall call + + call = SNPCall() + call._tid = self.iter.tid + call._pos = self.iter.pos + call._reference_base = rb + call._genotype = bam_nt16_rev_table[cns>>28] + call._consensus_quality = cns >> 8 & 0xff + call._snp_quality = cns & 0xff + call._rms_mapping_quality = cns >> 16&0xff + call._coverage = self.iter.n_plp + + return call + +cdef class IndelCall: + '''the results of an indel call.''' + cdef int _tid + cdef int _pos + cdef int _coverage + cdef int _rms_mapping_quality + cdef bam_maqindel_ret_t * _r + + def __cinit__(self): + #assert r != NULL + #self._r = r + pass + + 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 genotype: + '''the genotype called.''' + def __get__(self): + if self._r.gt == 0: + s = PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) + return "%s/%s" % (s,s) + elif self._r.gt == 1: + s = PyString_FromStringAndSize( self._r.s[1], self._r.indel2 + 1) + return "%s/%s" % (s,s) + else: + return "%s/%s" % (self.first_allele, self.second_allele ) + + property consensus_quality: + '''the genotype quality (Phred-scaled).''' + def __get__(self): return self._r.q_cns + + property snp_quality: + '''the snp quality (Phred scaled) - probability of consensus being identical to reference sequence.''' + def __get__(self): return self._r.q_ref + + 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 + + property first_allele: + '''sequence of first allele.''' + def __get__(self): return PyString_FromStringAndSize( self._r.s[0], self._r.indel1 + 1) + + property second_allele: + '''sequence of second allele.''' + def __get__(self): return PyString_FromStringAndSize( self._r.s[1], self._r.indel2 + 1) + + property reads_first: + '''reads supporting first allele.''' + def __get__(self): return self._r.cnt1 + + property reads_second: + '''reads supporting first allele.''' + def __get__(self): return self._r.cnt2 + + property reads_diff: + '''reads supporting first allele.''' + def __get__(self): return self._r.cnt_anti + + def __str__(self): + + return "\t".join( map(str, ( + self.tid, + self.pos, + self.genotype, + self.consensus_quality, + self.snp_quality, + self.mapping_quality, + self.coverage, + self.first_allele, + self.second_allele, + self.reads_first, + self.reads_second, + self.reads_diff ) ) ) + + def __dealloc__(self ): + bam_maqindel_ret_destroy(self._r) + +cdef class IndelCallerBase: + '''Base class for SNP callers. + + *min_baseQ* + minimum base quality (possibly capped by BAQ) + *capQ_threshold* + coefficient for adjusting mapQ of poor mappings + *theta* + theta in maq consensus calling model + *n_haplotypes* + number of haplotypes in the sample + *het_rate* + prior of a difference between two haplotypes + ''' + + cdef bam_maqindel_opt_t * options + cdef IteratorColumn iter + cdef int cap_mapQ + cdef int max_depth + + def __cinit__(self, + IteratorColumn iterator_column, + **kwargs ): + + + self.iter = iterator_column + + assert iterator_column.hasReference(), "IndelCallerBase requires an pileup iterator with reference sequence" + + self.options = bam_maqindel_opt_init() + + # set the default parameterization according to + # samtools + + self.options.r_indel = kwargs.get( "r_indel", 0.00015 ) + self.options.q_indel = kwargs.get( "q_indel", 40 ) + self.cap_mapQ = kwargs.get( "cap_mapQ", 60 ) + self.max_depth = kwargs.get( "max_depth", 1024 ) + + def __dealloc__(self): + free( self.options ) + + def _call( self ): + + cdef char * seq = self.iter.getSequence() + cdef int seq_len = self.iter.seq_len + + assert seq != NULL + + # reference base + if self.iter.pos >= seq_len: + raise ValueError( "position %i out of bounds on reference sequence (len=%i)" % (self.iter.pos, seq_len) ) + + cdef bam_maqindel_ret_t * r + + cdef int m = min( self.max_depth, self.iter.n_plp ) + + # printf("pysam: m=%i, q_indel=%i, r_indel=%f, r_snp=%i, mm_penalty=%i, indel_err=%i, ambi_thres=%i\n", + # m, self.options.q_indel, self.options.r_indel, self.options.r_snp, self.options.mm_penalty, + # self.options.indel_err, self.options.ambi_thres ); + + r = bam_maqindel(m, + self.iter.pos, + self.options, + self.iter.plp, + seq, + 0, + NULL) + + if r == NULL: return None + + cdef IndelCall call + call = IndelCall() + call._r = r + call._tid = self.iter.tid + call._pos = self.iter.pos + call._coverage = self.iter.n_plp + + cdef uint64_t rms_aux = 0 + cdef int i = 0 + cdef bam_pileup1_t * p + cdef int tmp + + for i from 0 <= i < self.iter.n_plp: + p = self.iter.plp + i + if p.b.core.qual < self.cap_mapQ: + tmp = p.b.core.qual + else: + tmp = self.cap_mapQ + rms_aux += tmp * tmp + + call._rms_mapping_quality = (sqrt(rms_aux / self.iter.n_plp) + .499) + + return call + +cdef class IndelCaller( IndelCallerBase ): + '''*(IteratorColumn iterator_column )* + + The samtools SNP caller. + + This object will call SNPs in *samfile* against the reference + sequence in *fasta*. + + This caller is fast for calling few SNPs in selected regions. + + It is slow, if called over large genomic regions. + ''' + + def __cinit__(self, + IteratorColumn iterator_column, + **kwargs ): + + pass + + def call(self, reference, int pos ): + """call a snp on chromosome *reference* + and position *pos*. + + returns a :class:`SNPCall` object or None, if no indel call could be made. + """ + + cdef int tid = self.iter.samfile.gettid( reference ) + + self.iter.reset( tid, pos, pos + 1 ) + + while 1: + self.iter.cnext() + + if self.iter.n_plp < 0: + raise ValueError("error during iteration" ) + + if self.iter.plp == NULL: + raise ValueError( "no reads in region - no call" ) + + if self.iter.pos == pos: break + + return self._call() + +cdef class IteratorIndelCalls( IndelCallerBase ): + """*(IteratorColumn iterator)* + + call indels within a region. + + *iterator* is a pileup iterator. SNPs will be called + on all positions returned by this iterator. + + This caller is fast if SNPs are called over large continuous + regions. It is slow, if instantiated frequently and in random + order as the sequence will have to be reloaded. + + """ + + def __cinit__(self, + IteratorColumn iterator_column, + **kwargs ): + pass + + + def __iter__(self): + return self + + def __next__(self): + """python version of next(). + """ + + # the following code was adapted from bam_plcmd.c:pileup_func() + self.iter.cnext() + + if self.iter.n_plp < 0: + raise ValueError("error during iteration" ) + + if self.iter.plp == NULL: + raise StopIteration + + return self._call() __all__ = ["Samfile", "Fastafile", "IteratorRow", - "IteratorRowAll", "IteratorColumn", "AlignedRead", "PileupColumn", "PileupProxy", - "PileupRead" ] + "PileupRead", + "IteratorSNPCalls", + "SNPCaller", + "IndelCaller", + "IteratorIndelCalls", ] diff --git a/pysam/pysam_util.c b/pysam/pysam_util.c index 91b6fa7..6b554d0 100644 --- a/pysam/pysam_util.c +++ b/pysam/pysam_util.c @@ -6,6 +6,17 @@ #include "bam_endian.h" #include "knetfile.h" #include "pysam_util.h" +#include "errmod.h" // for pysam_dump + +// Definition of pysamerr +#include "stdio.h" +FILE * pysamerr = NULL; + +FILE * pysam_set_stderr( FILE * f ) +{ + pysamerr = f; + return f; +} // ####################################################### // utility routines to avoid using callbacks in bam_fetch @@ -32,6 +43,7 @@ typedef struct { KSORT_INIT(my_off, pair64_t, pair64_lt); KHASH_MAP_INIT_INT(my_i, bam_binlist_t); +KHASH_MAP_INIT_STR(s, int) struct __bam_index_t { @@ -164,6 +176,60 @@ int pysam_pileup_next(const bam1_t *b, return 1; } +typedef struct __bmc_aux_t { + int max; + uint32_t *info; + uint16_t *info16; + errmod_t *em; +} bmc_aux_t; + +uint32_t pysam_glf_depth( glf1_t * g ) +{ + return g->depth; +} + + +void pysam_dump_glf( glf1_t * g, bam_maqcns_t * c ) +{ + fprintf(stderr, + "glf: ref_base=%i, max_mapQ=%i, min_lk=%i, depth=%i", + g->ref_base, + g->max_mapQ, + g->min_lk, + g->depth ); + + int x = 0; + for (x = 0; x < 10; ++x) + fprintf(stderr, ", lk%x=%i, ", x, g->lk[x]); + + fprintf(stderr, + "maqcns: het_rate=%f, theta=%f, n_hap=%i, cap_mapQ=%i, errmod=%i, min_baseQ=%i, eta=%f, q_r=%f, aux_max=%i", + c->het_rate, + c->theta, + c->n_hap, + c->cap_mapQ, + c->errmod, + c->min_baseQ, + c->eta, + c->q_r, + c->aux->max); + + for (x = 0; x < c->aux->max; ++x) + { + fprintf(stderr, ", info-%i=%i ", x, c->aux->info[x]); + if (c->aux->info[x] == 0) break; + } + + for (x = 0; x < c->aux->max; ++x) + { + fprintf(stderr, ", info16-%i=%i ", x, c->aux->info16[x]); + if (c->aux->info16[x] == 0) break; + } +} + + + + // pysam dispatch function to emulate the samtools // command line within python. // taken from the main function in bamtk.c @@ -204,7 +270,7 @@ int pysam_dispatch(int argc, char *argv[] ) else if (strcmp(argv[1], "pileup") == 0) return bam_pileup(argc-1, argv+1); else if (strcmp(argv[1], "merge") == 0) return bam_merge(argc-1, argv+1); else if (strcmp(argv[1], "sort") == 0) return bam_sort(argc-1, argv+1); - else if (strcmp(argv[1], "index") == 0) return bam_index(argc-1, argv+1); + else if (strcmp(argv[1], "index") == 0) return bam_index(argc-1, argv+1); else if (strcmp(argv[1], "faidx") == 0) return faidx_main(argc-1, argv+1); else if (strcmp(argv[1], "fixmate") == 0) return bam_mating(argc-1, argv+1); else if (strcmp(argv[1], "rmdup") == 0) return bam_rmdup(argc-1, argv+1); @@ -286,5 +352,31 @@ unsigned char pysam_translate_sequence( const unsigned char s ) } +void bam_init_header_hash(bam_header_t *header); + +// translate a reference string *s* to a tid +// code taken from bam_parse_region +int pysam_reference2tid( bam_header_t *header, const char * s ) +{ + + khiter_t iter; + khash_t(s) *h; + + bam_init_header_hash(header); + h = (khash_t(s)*)header->hash; + + iter = kh_get(s, h, s); /* get the ref_id */ + if (iter == kh_end(h)) { // name not found + return -1; + } + + return kh_value(h, iter); +} + + + + + + diff --git a/pysam/pysam_util.h b/pysam/pysam_util.h index bfbd6dd..89e381b 100644 --- a/pysam/pysam_util.h +++ b/pysam/pysam_util.h @@ -1,6 +1,10 @@ #ifndef PYSAM_UTIL_H #define PYSAM_UTIL_H +////////////////////////////////////////////////////////////////// +// set pysam standard error to point to stream +FILE * pysam_set_stderr( FILE * f ); + ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// @@ -38,5 +42,18 @@ bam1_t * pysam_bam_update( bam1_t * b, // translate a nucleotide character to binary code unsigned char pysam_translate_sequence( const unsigned char s ); +// defined in bam_import.c +extern unsigned char bam_nt16_table[256]; + +// translate a reference string *s* to a tid +int pysam_reference2tid( bam_header_t *header, const char * s ); + +// debugging functions +#include "glf.h" +uint32_t pysam_glf_depth( glf1_t * g); + +#include "bam_maqcns.h" +void pysam_dump_glf( glf1_t * g, bam_maqcns_t * c ); + #endif diff --git a/pysam/tabix_util.c b/pysam/tabix_util.c new file mode 100644 index 0000000..b06a614 --- /dev/null +++ b/pysam/tabix_util.c @@ -0,0 +1,4 @@ +// Definition of pysamerr +#include "stdio.h" +FILE * pysamerr = NULL; + diff --git a/pysam/version.py b/pysam/version.py index 3b6c92a..454a67e 100644 --- a/pysam/version.py +++ b/pysam/version.py @@ -1,7 +1,7 @@ # pysam versioning information -__version__ = "0.3.1" +__version__ = "0.4" -__samtools_version__ = "0.1.8" +__samtools_version__ = "0.1.12a" -__tabix_version__ = "0.2.1" +__tabix_version__ = "0.2.3" diff --git a/samtools/bam.c b/samtools/bam.c deleted file mode 100644 index 94b0aa8..0000000 --- a/samtools/bam.c +++ /dev/null @@ -1,308 +0,0 @@ -#include -#include -#include -#include -#include "bam.h" -#include "bam_endian.h" -#include "kstring.h" -#include "sam_header.h" - -int bam_is_be = 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) -{ - uint32_t k, end; - end = c->pos; - for (k = 0; k < c->n_cigar; ++k) { - int op = cigar[k] & BAM_CIGAR_MASK; - if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP) - end += cigar[k] >> BAM_CIGAR_SHIFT; - } - 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) { - int op = cigar[k] & BAM_CIGAR_MASK; - if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CSOFT_CLIP) - l += cigar[k] >> BAM_CIGAR_SHIFT; - } - 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(stderr, "[bam_header_read] EOF marker is absent.\n"); - // read "BAM1" - magic_len = bam_read(fp, buf, 4); - if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) { - fprintf(stderr, "[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; -} - -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; } - } -} - -int bam_read1(bamFile fp, bam1_t *b) -{ - bam1_core_t *c = &b->core; - int32_t block_len, ret, i; - uint32_t x[8]; - - 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); - return 4 + block_len; -} - -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; -} - -int bam_write1(bamFile fp, const bam1_t *b) -{ - return bam_write1_core(fp, &b->core, b->data_len, b->data); -} - -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<tid < 0) kputsn("*\t", 2, &str); - else { kputs(header->target_name[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 { - for (i = 0; i < c->n_cigar; ++i) { - kputw(bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, &str); - kputc("MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK], &str); - } - } - kputc('\t', &str); - if (c->mtid < 0) kputsn("*\t", 2, &str); - else if (c->mtid == c->tid) kputsn("=\t", 2, &str); - else { kputs(header->target_name[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; } - } - return str.s; -} - -char *bam_format1(const bam_header_t *header, const bam1_t *b) -{ - return bam_format1_core(header, b, BAM_OFDEC); -} - -void bam_view1(const bam_header_t *header, const bam1_t *b) -{ - char *s = bam_format1(header, b); - puts(s); - free(s); -} - -// 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)); -} diff --git a/samtools/bam.c.pysam.c b/samtools/bam.c.pysam.c new file mode 100644 index 0000000..c205977 --- /dev/null +++ b/samtools/bam.c.pysam.c @@ -0,0 +1,334 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include "bam.h" +#include "bam_endian.h" +#include "kstring.h" +#include "sam_header.h" + +int bam_is_be = 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) +{ + uint32_t k, end; + end = c->pos; + for (k = 0; k < c->n_cigar; ++k) { + int op = cigar[k] & BAM_CIGAR_MASK; + if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP) + end += cigar[k] >> BAM_CIGAR_SHIFT; + } + 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) { + int op = cigar[k] & BAM_CIGAR_MASK; + if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CSOFT_CLIP) + l += cigar[k] >> BAM_CIGAR_SHIFT; + } + 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.\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; +} + +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; } + } +} + +int bam_read1(bamFile fp, bam1_t *b) +{ + bam1_core_t *c = &b->core; + int32_t block_len, ret, i; + uint32_t x[8]; + + 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); + return 4 + block_len; +} + +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; +} + +int bam_write1(bamFile fp, const bam1_t *b) +{ + return bam_write1_core(fp, &b->core, b->data_len, b->data); +} + +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<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 { + for (i = 0; i < c->n_cigar; ++i) { + kputw(bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, &str); + kputc("MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK], &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; } + } + return str.s; +} + +char *bam_format1(const bam_header_t *header, const bam1_t *b) +{ + return bam_format1_core(header, b, BAM_OFDEC); +} + +void bam_view1(const bam_header_t *header, const bam1_t *b) +{ + char *s = bam_format1(header, b); + puts(s); + free(s); +} + +int bam_validate1(const bam_header_t *header, const bam1_t *b) +{ + 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->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 + + 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)); +} diff --git a/samtools/bam.h b/samtools/bam.h index 8e26ea6..eef2ea9 100644 --- a/samtools/bam.h +++ b/samtools/bam.h @@ -1,6 +1,6 @@ /* The MIT License - Copyright (c) 2008 Genome Research Ltd (GRL). + Copyright (c) 2008-2010 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 @@ -230,7 +230,7 @@ typedef struct __bam_iter_t *bam_iter_t; @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)/2) +#define bam1_qual(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + (((b)->core.l_qseq + 1)>>1)) /*! @function @abstract Get a base on read @@ -455,6 +455,21 @@ extern "C" { 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); @@ -480,7 +495,7 @@ extern "C" { bam1_t *b; int32_t qpos; int indel, level; - uint32_t is_del:1, is_head:1, is_tail:1; + 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); @@ -493,6 +508,7 @@ extern "C" { 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); @@ -501,6 +517,7 @@ extern "C" { 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 @@ -693,8 +710,8 @@ 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->m_data) { // double the capacity - m_data = bsrc->m_data; kroundup32(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 diff --git a/samtools/bam2bcf.c.pysam.c b/samtools/bam2bcf.c.pysam.c new file mode 100644 index 0000000..066e0ac --- /dev/null +++ b/samtools/bam2bcf.c.pysam.c @@ -0,0 +1,258 @@ +#include "pysam.h" + +#include +#include +#include "bam.h" +#include "kstring.h" +#include "bam2bcf.h" +#include "errmod.h" +#include "bcftools/bcf.h" + +extern void ks_introsort_uint32_t(size_t n, uint32_t a[]); + +#define CALL_ETA 0.03f +#define CALL_MAX 256 +#define CALL_DEFTHETA 0.83f + +#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); + return bca; +} + +void bcf_call_destroy(bcf_callaux_t *bca) +{ + if (bca == 0) return; + errmod_destroy(bca->e); + free(bca->bases); free(bca->inscns); free(bca); +} +/* ref_base is the 4-bit representation of the reference base. It is + * negative if we are looking at an indel. */ +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 + memset(r, 0, sizeof(bcf_callret1_t)); + 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; + 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 < bca->capQ? p->b->core.qual : 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); + } + bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b; + // collect annotations + 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; + } + r->depth = n; r->ori_depth = ori_depth; + // glfgen + errmod_cal(bca->e, n, 5, bca->bases, r->p); + return r->depth; +} + +int bcf_call_combine(int n, const bcf_callret1_t *calls, 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]; + 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) call->a[j++] = qsum[i]&3; + else break; + } + } + 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 = i; j < call->n_alleles; ++j) + g[z++] = call->a[i] * 5 + call->a[j]; + 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]; + } + return 0; +} + +int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int is_SP, + 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) kputs("INDEL;", &s); + 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); + } + kputc('\0', &s); + // FMT + kputs("PL", &s); + if (bcr) { + kputs(":DP", &s); + if (is_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) { + uint16_t *dp = (uint16_t*)b->gi[1].data; + uint8_t *sp = is_SP? b->gi[2].data : 0; + for (i = 0; i < bc->n; ++i) { + bcf_callret1_t *p = bcr + i; + dp[i] = p->depth < 0xffff? p->depth : 0xffff; + if (is_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; +} diff --git a/samtools/bam2bcf.h b/samtools/bam2bcf.h new file mode 100644 index 0000000..26b022c --- /dev/null +++ b/samtools/bam2bcf.h @@ -0,0 +1,53 @@ +#ifndef BAM2BCF_H +#define BAM2BCF_H + +#include +#include "errmod.h" +#include "bcftools/bcf.h" + +#define B2B_INDEL_NULL 10000 + +typedef struct __bcf_callaux_t { + int capQ, min_baseQ; + int openQ, extQ, tandemQ; + // for internal uses + int max_bases; + int indel_types[4]; + int maxins, indelreg; + char *inscns; + uint16_t *bases; + errmod_t *e; + void *rghash; +} bcf_callaux_t; + +typedef struct { + int depth, ori_depth, qsum[4]; + int anno[16]; + float p[25]; +} bcf_callret1_t; + +typedef struct { + int a[5]; // alleles: ref, alt, alt2, alt3 + int n, n_alleles, shift, ori_ref, unseen; + int anno[16], depth, ori_depth; + uint8_t *PL; +} 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, 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 is_SP, + 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); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/samtools/bam2bcf_indel.c.pysam.c b/samtools/bam2bcf_indel.c.pysam.c new file mode 100644 index 0000000..7205688 --- /dev/null +++ b/samtools/bam2bcf_indel.c.pysam.c @@ -0,0 +1,414 @@ +#include "pysam.h" + +#include +#include +#include +#include "bam.h" +#include "bam2bcf.h" +#include "ksort.h" +#include "kaln.h" +#include "kprobaln.h" +#include "khash.h" +KHASH_SET_INIT_STR(rg) + +#define MINUS_CONST 0x10000000 +#define INDEL_WINDOW_SIZE 50 +#define MIN_SUPPORT_COEF 500 + +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; +} + +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); +} + +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) { + 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; +} + +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 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) +{ + extern void ks_introsort_uint32_t(int, uint32_t*); + 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; + 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 m, n_alt = 0, n_tot = 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) { + for (i = 0; i < n_plp[s]; ++i) { + const bam_pileup1_t *p = plp[s] + i; + if (rghash == 0 || p->aux == 0) { + ++n_tot; + if (p->indel != 0) { + ++n_alt; + 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; + } + } + 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; + if (n_types == 1 || n_alt * MIN_SUPPORT_COEF < n_tot) { // no indels or too few supporting reads + free(aux); 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) + assert(n_types < 64); + } + { // 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 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(4 * 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)]; + if (c < 4) ++inscns_aux[(t*max_ins+(k-1))*4 + 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)*4]; + for (k = 0; k < 4; ++k) + if (ia[k] > max) + max = ia[k], max_k = k; + inscns[t*max_ins + j] = max? max_k : 4; + } + } + 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); + // write ref2 + for (k = 0, j = left; j <= pos; ++j) + ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]]; + if (types[t] <= 0) j += -types[t]; + else for (l = 0; l < types[t]; ++l) + ref2[k++] = inscns[t*max_ins + l]; + if (types[0] < 0) { // mask deleted sequences to avoid a particular error in the model. + int jj, tmp = types[t] >= 0? -types[0] : -types[0] + types[t]; + for (jj = 0; jj < tmp && j < right && ref[j]; ++jj, ++j) + ref2[k++] = 4; + } + for (; j < right && ref[j]; ++j) + ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]]; + for (; k < max_ref2; ++k) ref2[k] = 4; + if (j < right) right = j; + // align each read to ref2 + for (s = K = 0; s < n; ++s) { + for (i = 0; i < n_plp[s]; ++i, ++K) { + bam_pileup1_t *p = plp[s] + i; + int qbeg, qend, tbeg, tend, sc; + uint8_t *seq = bam1_seq(p->b); + // 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); +*/ + } + } + } + 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; + p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ; + sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ; +// fprintf(pysamerr, "pos=%d read=%d:%d name=%s call=%d q=%d\n", pos, s, i, bam1_qname(p->b), types[sc[0]&0x3f], indelQ); + } + } + // 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 + free(types); free(inscns); + return n_alt > 0? 0 : -1; +} diff --git a/samtools/bam_aux.c b/samtools/bam_aux.c deleted file mode 100644 index fbcd982..0000000 --- a/samtools/bam_aux.c +++ /dev/null @@ -1,182 +0,0 @@ -#include -#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 == 'C' || type == 'A') ++(s); \ - else if (type == 'S') (s) += 2; \ - else if (type == 'I' || type == 'F') (s) += 4; \ - else if (type == 'D') (s) += 8; \ - else if (type == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \ - } 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; -} - -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; - } - } -} - -void bam_destroy_header_hash(bam_header_t *header) -{ - if (header->hash) - kh_destroy(s, (khash_t(s)*)header->hash); -} - -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); -} - -int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end) -{ - char *s, *p; - int i, l, k; - khiter_t iter; - khash_t(s) *h; - - bam_init_header_hash(header); - h = (khash_t(s)*)header->hash; - - 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; - iter = kh_get(s, h, s); /* get the ref_id */ - if (iter == kh_end(h)) { // name not found - *ref_id = -1; free(s); - return -1; - } - *ref_id = kh_value(h, iter); - 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) { - fprintf(stderr, "[bam_parse_region] invalid region.\n"); - return -1; - } - return 0; -} - -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; -} - -float bam_aux2f(const uint8_t *s) -{ - int type; - type = *s++; - if (s == 0) return 0.0; - if (type == 'f') return *(float*)s; - else return 0.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; -} - -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 type; - type = *s++; - if (s == 0) return 0; - if (type == 'Z' || type == 'H') return (char*)s; - else return 0; -} diff --git a/samtools/bam_aux.c.pysam.c b/samtools/bam_aux.c.pysam.c new file mode 100644 index 0000000..d02621d --- /dev/null +++ b/samtools/bam_aux.c.pysam.c @@ -0,0 +1,184 @@ +#include "pysam.h" + +#include +#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 == 'C' || type == 'A') ++(s); \ + else if (type == 'S') (s) += 2; \ + else if (type == 'I' || type == 'F') (s) += 4; \ + else if (type == 'D') (s) += 8; \ + else if (type == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \ + } 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; +} + +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; + } + } +} + +void bam_destroy_header_hash(bam_header_t *header) +{ + if (header->hash) + kh_destroy(s, (khash_t(s)*)header->hash); +} + +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); +} + +int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end) +{ + char *s, *p; + int i, l, k; + khiter_t iter; + khash_t(s) *h; + + bam_init_header_hash(header); + h = (khash_t(s)*)header->hash; + + 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; + iter = kh_get(s, h, s); /* get the ref_id */ + if (iter == kh_end(h)) { // name not found + *ref_id = -1; free(s); + return -1; + } + *ref_id = kh_value(h, iter); + 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) { + fprintf(pysamerr, "[bam_parse_region] invalid region.\n"); + return -1; + } + return 0; +} + +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; +} + +float bam_aux2f(const uint8_t *s) +{ + int type; + type = *s++; + if (s == 0) return 0.0; + if (type == 'f') return *(float*)s; + else return 0.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; +} + +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 type; + type = *s++; + if (s == 0) return 0; + if (type == 'Z' || type == 'H') return (char*)s; + else return 0; +} diff --git a/samtools/bam_color.c b/samtools/bam_color.c deleted file mode 100644 index ce637f7..0000000 --- a/samtools/bam_color.c +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include "bam.h" - -/*! - @abstract Get the color encoding the previous and current base - @param b pointer to an alignment - @param i The i-th position, 0-based - @return color - - @discussion Returns 0 no color information is found. - */ -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; - else i++; - return cs[i]; -} - -/*! - @abstract Get the color quality of the color encoding the previous and current base - @param b pointer to an alignment - @param i The i-th position, 0-based - @return color quality - - @discussion Returns 0 no color information is found. - */ -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; - 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; - } -} - -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)]; -} - -/*! - @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 - - @discussion Returns 0 no color information is found. - */ -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; - // 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_color.c.pysam.c b/samtools/bam_color.c.pysam.c new file mode 100644 index 0000000..f71c65f --- /dev/null +++ b/samtools/bam_color.c.pysam.c @@ -0,0 +1,129 @@ +#include "pysam.h" + +#include +#include "bam.h" + +/*! + @abstract Get the color encoding the previous and current base + @param b pointer to an alignment + @param i The i-th position, 0-based + @return color + + @discussion Returns 0 no color information is found. + */ +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; + else i++; + return cs[i]; +} + +/*! + @abstract Get the color quality of the color encoding the previous and current base + @param b pointer to an alignment + @param i The i-th position, 0-based + @return color quality + + @discussion Returns 0 no color information is found. + */ +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; + 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; + } +} + +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)]; +} + +/*! + @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 + + @discussion Returns 0 no color information is found. + */ +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; + // 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_import.c b/samtools/bam_import.c deleted file mode 100644 index 9d84328..0000000 --- a/samtools/bam_import.c +++ /dev/null @@ -1,459 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#ifdef _WIN32 -#include -#endif -#include "kstring.h" -#include "bam.h" -#include "sam_header.h" -#include "kseq.h" -#include "khash.h" - -KSTREAM_INIT(gzFile, gzread, 8192) -KHASH_MAP_INIT_STR(ref, uint64_t) - -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); - -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 -}; - -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 -}; - -char *bam_nt16_rev_table = "=ACMGRSVTWYHKDBN"; - -struct __tamFile_t { - gzFile fp; - kstream_t *ks; - kstring_t *str; - uint64_t n_lines; - int is_first; -}; - -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; -} - -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(stderr, "[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(stderr, "[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(stderr, "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(stderr,"realloc failed to alloc %ld bytes\n", y); - abort(); - } - } - // Sanity check - if ( header->l_text+str->l+1 >= header->n_text ) - { - fprintf(stderr,"append_text FIXME: %ld>=%ld, x=%ld,y=%ld\n", header->l_text+str->l+1,header->n_text,x,y); - abort(); - } - 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(stderr, "[sam_read1] missing header? Abort!\n"); - exit(1); - } else fprintf(stderr, "[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] != '*') { - for (s = str->s; *s; ++s) { - if (isalpha(*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); - for (i = 0, s = str->s; i != c->n_cigar; ++i) { - x = strtol(s, &t, 10); - op = toupper(*t); - if (op == 'M' || op == '=' || op == 'X') 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 parse_error(fp->n_lines, "invalid CIGAR operation"); - s = t + 1; - bam1_cigar(b)[i] = x << BAM_CIGAR_SHIFT | op; - } - if (*s) parse_error(fp->n_lines, "unmatched CIGAR operation"); - c->bin = bam_reg2bin(c->pos, bam_calend(c, bam1_cigar(b))); - doff += c->n_cigar * 4; - } else { - if (!(c->flag&BAM_FUNMAP)) { - fprintf(stderr, "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))) - 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(stderr, "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(stderr, "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 parse_error(fp->n_lines, "unrecognized type"); - if (dret == '\n' || dret == '\r') break; - } - } - b->l_aux = doff - doff0; - b->data_len = doff; - 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); - } -} diff --git a/samtools/bam_import.c.pysam.c b/samtools/bam_import.c.pysam.c new file mode 100644 index 0000000..238b05b --- /dev/null +++ b/samtools/bam_import.c.pysam.c @@ -0,0 +1,461 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#endif +#include "kstring.h" +#include "bam.h" +#include "sam_header.h" +#include "kseq.h" +#include "khash.h" + +KSTREAM_INIT(gzFile, gzread, 8192) +KHASH_MAP_INIT_STR(ref, uint64_t) + +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); + +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 +}; + +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 +}; + +char *bam_nt16_rev_table = "=ACMGRSVTWYHKDBN"; + +struct __tamFile_t { + gzFile fp; + kstream_t *ks; + kstring_t *str; + uint64_t n_lines; + int is_first; +}; + +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; +} + +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,header->n_text,x,y); + abort(); + } + 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] != '*') { + for (s = str->s; *s; ++s) { + if (isalpha(*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); + for (i = 0, s = str->s; i != c->n_cigar; ++i) { + x = strtol(s, &t, 10); + op = toupper(*t); + if (op == 'M' || op == '=' || op == 'X') 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 parse_error(fp->n_lines, "invalid CIGAR operation"); + s = t + 1; + bam1_cigar(b)[i] = x << BAM_CIGAR_SHIFT | op; + } + if (*s) parse_error(fp->n_lines, "unmatched CIGAR operation"); + c->bin = bam_reg2bin(c->pos, bam_calend(c, bam1_cigar(b))); + 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))) + 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 parse_error(fp->n_lines, "unrecognized type"); + if (dret == '\n' || dret == '\r') break; + } + } + b->l_aux = doff - doff0; + b->data_len = doff; + 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); + } +} diff --git a/samtools/bam_index.c b/samtools/bam_index.c deleted file mode 100644 index 4152f20..0000000 --- a/samtools/bam_index.c +++ /dev/null @@ -1,696 +0,0 @@ -#include -#include -#include "bam.h" -#include "khash.h" -#include "ksort.h" -#include "bam_endian.h" -#ifdef _USE_KNETFILE -#include "knetfile.h" -#endif - -/*! - @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; - - idx = (bam_index_t*)calloc(1, sizeof(bam_index_t)); - b = (bam1_t*)calloc(1, sizeof(bam1_t)); - h = bam_header_read(fp); - 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) { // change of chromosomes - last_tid = c->tid; - last_bin = 0xffffffffu; - } else if (last_coor > c->pos) { - fprintf(stderr, "[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); - exit(1); - } - if (c->tid >= 0) 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(stderr, "[bam_index_core] bug in BGZF/RAZF: %llx < %llx\n", - (unsigned long long)bam_tell(fp), (unsigned long long)last_off); - exit(1); - } - 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, off_end); - 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 (ret < -1) fprintf(stderr, "[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(stderr, "[bam_index_load_core] fail to load index.\n"); - return 0; - } - fread(magic, 1, 4, fp); - if (strncmp(magic, "BAI\1", 4)) { - fprintf(stderr, "[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(stderr, "[download_from_remote] fail to open remote file.\n"); - return; - } - if ((fp = fopen(fn, "wb")) == 0) { - fprintf(stderr, "[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(stderr, "[bam_index_load] attempting to download the remote index file.\n"); - download_from_remote(fnidx); - idx = bam_index_load_local(fn); - } - if (idx == 0) fprintf(stderr, "[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(stderr, "[bam_index_build2] fail to open the BAM file.\n"); - return -1; - } - idx = bam_index_core(fp); - bam_close(fp); - 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(stderr, "[bam_index_build2] fail to create the index file.\n"); - free(fnidx); - return -1; - } - bam_index_save(idx, fpidx); - bam_index_destroy(idx); - fclose(fpidx); - free(fnidx); - return 0; -} - -int bam_index_build(const char *fn) -{ - return bam_index_build2(fn, 0); -} - -int bam_index(int argc, char *argv[]) -{ - if (argc < 2) { - fprintf(stderr, "Usage: samtools index [out.index]\n"); - return 1; - } - if (argc >= 3) bam_index_build2(argv[1], argv[2]); - else bam_index_build(argv[1]); - 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(stderr, "Usage: samtools idxstats \n"); - return 1; - } - fp = bam_open(argv[1], "r"); - if (fp == 0) { fprintf(stderr, "[%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(stderr, "[%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); - { - 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) -{ - if (iter->finished) return -1; - if (iter->from_first) { - int ret = bam_read1(fp, b); - if (ret < 0) iter->finished = 1; - return ret; - } - if (iter->off == 0) return -1; - for (;;) { - int ret; - 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) 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) break; // no need to proceed - else if (is_overlap(iter->beg, iter->end, b)) return ret; - } else break; // end of file - } - iter->finished = 1; - return -1; -} - -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 iter; - bam1_t *b; - b = bam_init1(); - iter = bam_iter_query(idx, tid, beg, end); - while (bam_iter_read(fp, iter, b) >= 0) func(b, data); - bam_destroy1(b); - return 0; -} diff --git a/samtools/bam_index.c.pysam.c b/samtools/bam_index.c.pysam.c new file mode 100644 index 0000000..1e94275 --- /dev/null +++ b/samtools/bam_index.c.pysam.c @@ -0,0 +1,706 @@ +#include "pysam.h" + +#include +#include +#include "bam.h" +#include "khash.h" +#include "ksort.h" +#include "bam_endian.h" +#ifdef _USE_KNETFILE +#include "knetfile.h" +#endif + +/*! + @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; + + idx = (bam_index_t*)calloc(1, sizeof(bam_index_t)); + b = (bam1_t*)calloc(1, sizeof(bam1_t)); + h = bam_header_read(fp); + 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) { // change of chromosomes + last_tid = c->tid; + last_bin = 0xffffffffu; + } else if (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); + exit(1); + } + if (c->tid >= 0) 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); + exit(1); + } + 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 (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); + 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 (_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); + return -1; + } + bam_index_save(idx, fpidx); + bam_index_destroy(idx); + fclose(fpidx); + free(fnidx); + return 0; +} + +int bam_index_build(const char *fn) +{ + return bam_index_build2(fn, 0); +} + +int bam_index(int argc, char *argv[]) +{ + if (argc < 2) { + fprintf(pysamerr, "Usage: samtools index [out.index]\n"); + return 1; + } + if (argc >= 3) bam_index_build2(argv[1], argv[2]); + else bam_index_build(argv[1]); + 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 \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; +} diff --git a/samtools/bam_lpileup.c b/samtools/bam_lpileup.c deleted file mode 100644 index d4dd63b..0000000 --- a/samtools/bam_lpileup.c +++ /dev/null @@ -1,198 +0,0 @@ -#include -#include -#include -#include "bam.h" -#include "ksort.h" - -#define TV_GAP 2 - -typedef struct __freenode_t { - 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)) -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; -} mempool_t; - -static mempool_t *mp_init() -{ - 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); -} -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]; -} -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; -} - -/* 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; -}; - -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; -} - -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; -/* - fprintf(stderr, "%d\t", pos+1); - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pl + i; - if (p->is_head) fprintf(stderr, "^"); - if (p->is_tail) fprintf(stderr, "$"); - fprintf(stderr, "%d,", p->level); - } - fprintf(stderr, "\n"); -*/ - 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; -} - -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); -} - -int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *tv) -{ - return bam_plbuf_push(b, tv->plbuf); -} diff --git a/samtools/bam_lpileup.c.pysam.c b/samtools/bam_lpileup.c.pysam.c new file mode 100644 index 0000000..a5c5b04 --- /dev/null +++ b/samtools/bam_lpileup.c.pysam.c @@ -0,0 +1,200 @@ +#include "pysam.h" + +#include +#include +#include +#include "bam.h" +#include "ksort.h" + +#define TV_GAP 2 + +typedef struct __freenode_t { + 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)) +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; +} mempool_t; + +static mempool_t *mp_init() +{ + 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); +} +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]; +} +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; +} + +/* 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; +}; + +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; +} + +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; +/* + 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; +} + +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; +} + +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); +} + +int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *tv) +{ + return bam_plbuf_push(b, tv->plbuf); +} diff --git a/samtools/bam_maqcns.c b/samtools/bam_maqcns.c deleted file mode 100644 index cad63d7..0000000 --- a/samtools/bam_maqcns.c +++ /dev/null @@ -1,614 +0,0 @@ -#include -#include -#include "bam.h" -#include "bam_maqcns.h" -#include "ksort.h" -#include "kaln.h" -KSORT_INIT_GENERIC(uint32_t) - -#define INDEL_WINDOW_SIZE 50 -#define INDEL_EXT_DEP 0.9 - -typedef struct __bmc_aux_t { - int max; - uint32_t *info; -} bmc_aux_t; - -typedef struct { - float esum[4], fsum[4]; - uint32_t c[4]; - uint32_t rms_mapQ; -} glf_call_aux_t; - -char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 }; - -/* - P() = \theta \sum_{i=1}^{N-1} 1/i - P(D|) = \sum_{k=1}^{N-1} p_k 1/2 [(k/N)^n_2(1-k/N)^n_1 + (k/N)^n1(1-k/N)^n_2] - p_k = 1/k / \sum_{i=1}^{N-1} 1/i - */ -static void cal_het(bam_maqcns_t *aa) -{ - int k, n1, n2; - double sum_harmo; // harmonic sum - double poly_rate; - - free(aa->lhet); - aa->lhet = (double*)calloc(256 * 256, sizeof(double)); - sum_harmo = 0.0; - for (k = 1; k <= aa->n_hap - 1; ++k) - sum_harmo += 1.0 / k; - for (n1 = 0; n1 < 256; ++n1) { - for (n2 = 0; n2 < 256; ++n2) { - long double sum = 0.0; - double lC = aa->is_soap? 0 : lgamma(n1+n2+1) - lgamma(n1+1) - lgamma(n2+1); // \binom{n1+n2}{n1} - for (k = 1; k <= aa->n_hap - 1; ++k) { - double pk = 1.0 / k / sum_harmo; - double log1 = log((double)k/aa->n_hap); - double log2 = log(1.0 - (double)k/aa->n_hap); - sum += pk * 0.5 * (expl(log1*n2) * expl(log2*n1) + expl(log1*n1) * expl(log2*n2)); - } - aa->lhet[n1<<8|n2] = lC + logl(sum); - } - } - poly_rate = aa->het_rate * sum_harmo; - aa->q_r = -4.343 * log(2.0 * poly_rate / (1.0 - poly_rate)); -} - -/** initialize the helper structure */ -static void cal_coef(bam_maqcns_t *aa) -{ - int k, n, q; - long double sum_a[257], b[256], q_c[256], tmp[256], fk2[256]; - double *lC; - - // aa->lhet will be allocated and initialized - free(aa->fk); free(aa->coef); - aa->coef = 0; - aa->fk = (double*)calloc(256, sizeof(double)); - aa->fk[0] = fk2[0] = 1.0; - for (n = 1; n != 256; ++n) { - aa->fk[n] = pow(aa->theta, n) * (1.0 - aa->eta) + aa->eta; - fk2[n] = aa->fk[n>>1]; // this is an approximation, assuming reads equally likely come from both strands - } - if (aa->is_soap) return; - aa->coef = (double*)calloc(256*256*64, sizeof(double)); - lC = (double*)calloc(256 * 256, sizeof(double)); - for (n = 1; n != 256; ++n) - for (k = 1; k <= n; ++k) - lC[n<<8|k] = lgamma(n+1) - 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 != 256; ++n) { - double *coef = aa->coef + (q<<16|n<<8); - sum_a[n+1] = 0.0; - for (k = n; k >= 0; --k) { // a_k = \sum_{i=k}^n C^n_k \epsilon^k (1-\epsilon)^{n-k} - sum_a[k] = sum_a[k+1] + expl(lC[n<<8|k] + k*le + (n-k)*le1); - b[k] = sum_a[k+1] / sum_a[k]; - if (b[k] > 0.99) b[k] = 0.99; - } - for (k = 0; k != n; ++k) // log(\bar\beta_{nk}(\bar\epsilon)^{f_k}) - q_c[k] = -4.343 * fk2[k] * logl(b[k] / e); - for (k = 1; k != n; ++k) q_c[k] += q_c[k-1]; // \prod_{i=0}^k c_i - for (k = 0; k <= n; ++k) { // powl() in 64-bit mode seems broken on my Mac OS X 10.4.9 - tmp[k] = -4.343 * logl(1.0 - expl(fk2[k] * logl(b[k]))); - coef[k] = (k? q_c[k-1] : 0) + tmp[k]; // this is the final c_{nk} - } - } - } - free(lC); -} - -bam_maqcns_t *bam_maqcns_init() -{ - bam_maqcns_t *bm; - bm = (bam_maqcns_t*)calloc(1, sizeof(bam_maqcns_t)); - bm->aux = (bmc_aux_t*)calloc(1, sizeof(bmc_aux_t)); - bm->het_rate = 0.001; - bm->theta = 0.85; - bm->n_hap = 2; - bm->eta = 0.03; - bm->cap_mapQ = 60; - return bm; -} - -void bam_maqcns_prepare(bam_maqcns_t *bm) -{ - cal_coef(bm); cal_het(bm); -} - -void bam_maqcns_destroy(bam_maqcns_t *bm) -{ - if (bm == 0) return; - free(bm->lhet); free(bm->fk); free(bm->coef); free(bm->aux->info); - free(bm->aux); free(bm); -} - -glf1_t *bam_maqcns_glfgen(int _n, const bam_pileup1_t *pl, uint8_t ref_base, bam_maqcns_t *bm) -{ - glf_call_aux_t *b; - int i, j, k, w[8], c, n; - glf1_t *g = (glf1_t*)calloc(1, sizeof(glf1_t)); - float p[16], min_p = 1e30; - uint64_t rms; - - g->ref_base = ref_base; - if (_n == 0) return g; - - // construct aux array - if (bm->aux->max < _n) { - bm->aux->max = _n; - kroundup32(bm->aux->max); - bm->aux->info = (uint32_t*)realloc(bm->aux->info, 4 * bm->aux->max); - } - for (i = n = 0; i < _n; ++i) { - const bam_pileup1_t *p = pl + i; - uint32_t q, x = 0, qq; - if (p->is_del || (p->b->core.flag&BAM_FUNMAP)) continue; - q = (uint32_t)bam1_qual(p->b)[p->qpos]; - x |= (uint32_t)bam1_strand(p->b) << 18 | q << 8 | p->b->core.qual; - if (p->b->core.qual < q) q = p->b->core.qual; - x |= q << 24; - qq = bam1_seqi(bam1_seq(p->b), p->qpos); - q = bam_nt16_nt4_table[qq? qq : ref_base]; - if (!p->is_del && q < 4) x |= 1 << 21 | q << 16; - bm->aux->info[n++] = x; - } - ks_introsort(uint32_t, n, bm->aux->info); - // generate esum and fsum - b = (glf_call_aux_t*)calloc(1, sizeof(glf_call_aux_t)); - for (k = 0; k != 8; ++k) w[k] = 0; - rms = 0; - for (j = n - 1; j >= 0; --j) { // calculate esum and fsum - uint32_t info = bm->aux->info[j]; - int tmp; - if (info>>24 < 4 && (info>>8&0x3f) != 0) info = 4<<24 | (info&0xffffff); - k = info>>16&7; - if (info>>24 > 0) { - b->esum[k&3] += bm->fk[w[k]] * (info>>24); - b->fsum[k&3] += bm->fk[w[k]]; - if (w[k] < 0xff) ++w[k]; - ++b->c[k&3]; - } - tmp = (int)(info&0xff) < bm->cap_mapQ? (int)(info&0xff) : bm->cap_mapQ; - rms += tmp * tmp; - } - b->rms_mapQ = (uint8_t)(sqrt((double)rms / n) + .499); - // rescale ->c[] - for (j = c = 0; j != 4; ++j) c += b->c[j]; - if (c > 255) { - for (j = 0; j != 4; ++j) b->c[j] = (int)(254.0 * b->c[j] / c + 0.5); - for (j = c = 0; j != 4; ++j) c += b->c[j]; - } - if (!bm->is_soap) { - // generate likelihood - for (j = 0; j != 4; ++j) { - // homozygous - float tmp1, tmp3; - int tmp2, bar_e; - for (k = 0, tmp1 = tmp3 = 0.0, tmp2 = 0; k != 4; ++k) { - if (j == k) continue; - tmp1 += b->esum[k]; tmp2 += b->c[k]; tmp3 += b->fsum[k]; - } - if (tmp2) { - bar_e = (int)(tmp1 / tmp3 + 0.5); - if (bar_e < 4) bar_e = 4; // should not happen - if (bar_e > 63) bar_e = 63; - p[j<<2|j] = tmp1 + bm->coef[bar_e<<16|c<<8|tmp2]; - } else p[j<<2|j] = 0.0; // all the bases are j - // heterozygous - for (k = j + 1; k < 4; ++k) { - for (i = 0, tmp2 = 0, tmp1 = tmp3 = 0.0; i != 4; ++i) { - if (i == j || i == k) continue; - tmp1 += b->esum[i]; tmp2 += b->c[i]; tmp3 += b->fsum[i]; - } - if (tmp2) { - bar_e = (int)(tmp1 / tmp3 + 0.5); - if (bar_e < 4) bar_e = 4; - if (bar_e > 63) bar_e = 63; - p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]] + tmp1 + bm->coef[bar_e<<16|c<<8|tmp2]; - } else p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]]; // all the bases are either j or k - } - // - for (k = 0; k != 4; ++k) - if (p[j<<2|k] < 0.0) p[j<<2|k] = 0.0; - } - - { // fix p[k<<2|k] - float max1, max2, min1, min2; - int max_k, min_k; - max_k = min_k = -1; - max1 = max2 = -1.0; min1 = min2 = 1e30; - for (k = 0; k < 4; ++k) { - if (b->esum[k] > max1) { - max2 = max1; max1 = b->esum[k]; max_k = k; - } else if (b->esum[k] > max2) max2 = b->esum[k]; - } - for (k = 0; k < 4; ++k) { - if (p[k<<2|k] < min1) { - min2 = min1; min1 = p[k<<2|k]; min_k = k; - } else if (p[k<<2|k] < min2) min2 = p[k<<2|k]; - } - if (max1 > max2 && (min_k != max_k || min1 + 1.0 > min2)) - p[max_k<<2|max_k] = min1 > 1.0? min1 - 1.0 : 0.0; - } - } else { // apply the SOAP model - // generate likelihood - for (j = 0; j != 4; ++j) { - float tmp; - // homozygous - for (k = 0, tmp = 0.0; k != 4; ++k) - if (j != k) tmp += b->esum[k]; - p[j<<2|j] = tmp; - // heterozygous - for (k = j + 1; k < 4; ++k) { - for (i = 0, tmp = 0.0; i != 4; ++i) - if (i != j && i != k) tmp += b->esum[i]; - p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]] + tmp; - } - } - } - - // convert necessary information to glf1_t - g->ref_base = ref_base; g->max_mapQ = b->rms_mapQ; - g->depth = n > 16777215? 16777215 : n; - for (j = 0; j != 4; ++j) - for (k = j; k < 4; ++k) - if (p[j<<2|k] < min_p) min_p = p[j<<2|k]; - g->min_lk = min_p > 255.0? 255 : (int)(min_p + 0.5); - for (j = c = 0; j != 4; ++j) - for (k = j; k < 4; ++k) - g->lk[c++] = p[j<<2|k]-min_p > 255.0? 255 : (int)(p[j<<2|k]-min_p + 0.5); - - free(b); - return g; -} - -uint32_t glf2cns(const glf1_t *g, int q_r) -{ - int i, j, k, tmp[16], min = 10000, min2 = 10000, min3 = 10000, min_g = -1, min_g2 = -1; - uint32_t x = 0; - for (i = k = 0; i < 4; ++i) - for (j = i; j < 4; ++j) { - tmp[j<<2|i] = -1; - tmp[i<<2|j] = g->lk[k++] + (i == j? 0 : q_r); - } - for (i = 0; i < 16; ++i) { - if (tmp[i] < 0) continue; - if (tmp[i] < min) { - min3 = min2; min2 = min; min = tmp[i]; min_g2 = min_g; min_g = i; - } else if (tmp[i] < min2) { - min3 = min2; min2 = tmp[i]; min_g2 = i; - } else if (tmp[i] < min3) min3 = tmp[i]; - } - x = min_g >= 0? (1U<<(min_g>>2&3) | 1U<<(min_g&3)) << 28 : 0xf << 28; - x |= min_g2 >= 0? (1U<<(min_g2>>2&3) | 1U<<(min_g2&3)) << 24 : 0xf << 24; - x |= (uint32_t)g->max_mapQ << 16; - x |= min2 < 10000? (min2 - min < 256? min2 - min : 255) << 8 : 0xff << 8; - x |= min2 < 10000 && min3 < 10000? (min3 - min2 < 256? min3 - min2 : 255) : 0xff; - return x; -} - -uint32_t bam_maqcns_call(int n, const bam_pileup1_t *pl, bam_maqcns_t *bm) -{ - glf1_t *g; - uint32_t x; - if (n) { - g = bam_maqcns_glfgen(n, pl, 0xf, bm); - x = glf2cns(g, (int)(bm->q_r + 0.5)); - free(g); - } else x = 0xfU<<28 | 0xfU<<24; - return x; -} - -/************** *****************/ - -bam_maqindel_opt_t *bam_maqindel_opt_init() -{ - bam_maqindel_opt_t *mi = (bam_maqindel_opt_t*)calloc(1, sizeof(bam_maqindel_opt_t)); - mi->q_indel = 40; - mi->r_indel = 0.00015; - mi->r_snp = 0.001; - // - mi->mm_penalty = 3; - mi->indel_err = 4; - mi->ambi_thres = 10; - return mi; -} - -void bam_maqindel_ret_destroy(bam_maqindel_ret_t *mir) -{ - if (mir == 0) return; - free(mir->s[0]); free(mir->s[1]); free(mir); -} - -int bam_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) { - 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; -} - -#define MINUS_CONST 0x10000000 - -bam_maqindel_ret_t *bam_maqindel(int n, int pos, const bam_maqindel_opt_t *mi, const bam_pileup1_t *pl, const char *ref, - int _n_types, int *_types) -{ - int i, j, n_types, *types, left, right, max_rd_len = 0; - bam_maqindel_ret_t *ret = 0; - // if there is no proposed indel, check if there is an indel from the alignment - if (_n_types == 0) { - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pl + i; - if (!(p->b->core.flag&BAM_FUNMAP) && p->indel != 0) break; - } - if (i == n) return 0; // no indel - } - { // calculate how many types of indels are available (set n_types and types) - int m; - uint32_t *aux; - aux = (uint32_t*)calloc(n + _n_types + 1, 4); - m = 0; - aux[m++] = MINUS_CONST; // zero indel is always a type - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pl + i; - if (!(p->b->core.flag&BAM_FUNMAP) && p->indel != 0) - 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; - } - if (_n_types) // then also add this to aux[] - for (i = 0; i < _n_types; ++i) - if (_types[i]) aux[m++] = MINUS_CONST + _types[i]; - 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; - types = (int*)calloc(n_types, sizeof(int)); - j = 0; - types[j++] = aux[0] - MINUS_CONST; - for (i = 1; i < m; ++i) { - if (aux[i] != aux[i-1]) - types[j++] = aux[i] - MINUS_CONST; - } - free(aux); - } - { // 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 core part - char *ref2, *rs, *inscns = 0; - int qr_snp, k, l, *score, *pscore, max_ins = types[n_types-1]; - qr_snp = (int)(-4.343 * log(mi->r_snp) + .499); - if (max_ins > 0) { // get the consensus of inserted sequences - int *inscns_aux = (int*)calloc(4 * n_types * max_ins, sizeof(int)); - // count occurrences - for (i = 0; i < n_types; ++i) { - if (types[i] <= 0) continue; // not insertion - for (j = 0; j < n; ++j) { - const bam_pileup1_t *p = pl + j; - if (!(p->b->core.flag&BAM_FUNMAP) && p->indel == types[i]) { - for (k = 1; k <= p->indel; ++k) { - int c = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos + k)]; - if (c < 4) ++inscns_aux[i*max_ins*4 + (k-1)*4 + c]; - } - } - } - } - // construct the consensus of inserted sequence - inscns = (char*)calloc(n_types * max_ins, sizeof(char)); - for (i = 0; i < n_types; ++i) { - for (j = 0; j < types[i]; ++j) { - int max = 0, max_k = -1, *ia = inscns_aux + i*max_ins*4 + j*4; - for (k = 0; k < 4; ++k) { - if (ia[k] > max) { - max = ia[k]; - max_k = k; - } - } - inscns[i*max_ins + j] = max? 1<= 0? -types[0] : -types[0] + types[i]; - for (jj = 0; jj < tmp && j < right && ref[j]; ++jj, ++j) - ref2[k++] = 4; - } - for (; j < right && ref[j]; ++j) - ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]]; - if (j < right) right = j; - // calculate score for each read - for (j = 0; j < n; ++j) { - const bam_pileup1_t *p = pl + j; - int qbeg, qend, tbeg, tend; - if (p->b->core.flag & BAM_FUNMAP) continue; - qbeg = bam_tpos2qpos(&p->b->core, bam1_cigar(p->b), left, 0, &tbeg); - qend = bam_tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend); - assert(tbeg >= left); - for (l = qbeg; l < qend; ++l) - rs[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), l)]; - { - int x, y, n_acigar, ps; - uint32_t *acigar; - ps = 0; - if (tend - tbeg + types[i] <= 0) { - score[i*n+j] = -(1<<20); - pscore[i*n+j] = 1<<20; - continue; - } - acigar = ka_global_core((uint8_t*)ref2 + tbeg - left, tend - tbeg + types[i], (uint8_t*)rs, qend - qbeg, &ap, &score[i*n+j], &n_acigar); - x = tbeg - left; y = 0; - for (l = 0; l < n_acigar; ++l) { - int op = acigar[l]&0xf; - int len = acigar[l]>>4; - if (op == BAM_CMATCH) { - int k; - for (k = 0; k < len; ++k) - if (ref2[x+k] != rs[y+k] && ref2[x+k] < 4) - ps += bam1_qual(p->b)[y+k] < qr_snp? bam1_qual(p->b)[y+k] : qr_snp; - x += len; y += len; - } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) { - if (op == BAM_CINS && l > 0 && l < n_acigar - 1) ps += mi->q_indel * len; - y += len; - } else if (op == BAM_CDEL) { - if (l > 0 && l < n_acigar - 1) ps += mi->q_indel * len; - x += len; - } - } - pscore[i*n+j] = ps; - /*if (1) { // for debugging only - fprintf(stderr, "id=%d, pos=%d, type=%d, j=%d, score=%d, psore=%d, %d, %d, %d, %d, %d, ", - j, pos+1, types[i], j, score[i*n+j], pscore[i*n+j], tbeg, tend, qbeg, qend, mi->q_indel); - for (l = 0; l < n_acigar; ++l) fprintf(stderr, "%d%c", acigar[l]>>4, "MIDS"[acigar[l]&0xf]); - fprintf(stderr, "\n"); - for (l = 0; l < tend - tbeg + types[i]; ++l) fputc("ACGTN"[ref2[l+tbeg-left]], stderr); - fputc('\n', stderr); - for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[rs[l]], stderr); - fputc('\n', stderr); - }*/ - free(acigar); - } - } - } - { // get final result - int *sum, max1, max2, max1_i, max2_i; - // pick up the best two score - sum = (int*)calloc(n_types, sizeof(int)); - for (i = 0; i < n_types; ++i) - for (j = 0; j < n; ++j) - sum[i] += -pscore[i*n+j]; - max1 = max2 = -0x7fffffff; max1_i = max2_i = -1; - for (i = 0; i < n_types; ++i) { - if (sum[i] > max1) { - max2 = max1; max2_i = max1_i; max1 = sum[i]; max1_i = i; - } else if (sum[i] > max2) { - max2 = sum[i]; max2_i = i; - } - } - free(sum); - // write ret - ret = (bam_maqindel_ret_t*)calloc(1, sizeof(bam_maqindel_ret_t)); - ret->indel1 = types[max1_i]; ret->indel2 = types[max2_i]; - ret->s[0] = (char*)calloc(abs(ret->indel1) + 2, 1); - ret->s[1] = (char*)calloc(abs(ret->indel2) + 2, 1); - // write indel sequence - if (ret->indel1 > 0) { - ret->s[0][0] = '+'; - for (k = 0; k < ret->indel1; ++k) - ret->s[0][k+1] = bam_nt16_rev_table[(int)inscns[max1_i*max_ins + k]]; - } else if (ret->indel1 < 0) { - ret->s[0][0] = '-'; - for (k = 0; k < -ret->indel1 && ref[pos + k + 1]; ++k) - ret->s[0][k+1] = ref[pos + k + 1]; - } else ret->s[0][0] = '*'; - if (ret->indel2 > 0) { - ret->s[1][0] = '+'; - for (k = 0; k < ret->indel2; ++k) - ret->s[1][k+1] = bam_nt16_rev_table[(int)inscns[max2_i*max_ins + k]]; - } else if (ret->indel2 < 0) { - ret->s[1][0] = '-'; - for (k = 0; k < -ret->indel2 && ref[pos + k + 1]; ++k) - ret->s[1][k+1] = ref[pos + k + 1]; - } else ret->s[1][0] = '*'; - // write count - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pl + i; - if (p->indel == ret->indel1) ++ret->cnt1; - else if (p->indel == ret->indel2) ++ret->cnt2; - else ++ret->cnt_anti; - } - { // write gl[] - int tmp, seq_err = 0; - double x = 1.0; - tmp = max1_i - max2_i; - if (tmp < 0) tmp = -tmp; - for (j = 0; j < tmp + 1; ++j) x *= INDEL_EXT_DEP; - seq_err = mi->q_indel * (1.0 - x) / (1.0 - INDEL_EXT_DEP); - ret->gl[0] = ret->gl[1] = 0; - for (j = 0; j < n; ++j) { - int s1 = pscore[max1_i*n + j], s2 = pscore[max2_i*n + j]; - //fprintf(stderr, "id=%d, %d, %d, %d, %d, %d\n", j, pl[j].b->core.pos+1, types[max1_i], types[max2_i], s1, s2); - if (s1 > s2) ret->gl[0] += s1 - s2 < seq_err? s1 - s2 : seq_err; - else ret->gl[1] += s2 - s1 < seq_err? s2 - s1 : seq_err; - } - } - // write cnt_ref and cnt_ambi - if (max1_i != 0 && max2_i != 0) { - for (j = 0; j < n; ++j) { - int diff1 = score[j] - score[max1_i * n + j]; - int diff2 = score[j] - score[max2_i * n + j]; - if (diff1 > 0 && diff2 > 0) ++ret->cnt_ref; - else if (diff1 == 0 || diff2 == 0) ++ret->cnt_ambi; - } - } - } - free(score); free(pscore); free(ref2); free(rs); free(inscns); - } - { // call genotype - int q[3], qr_indel = (int)(-4.343 * log(mi->r_indel) + 0.5); - int min1, min2, min1_i; - q[0] = ret->gl[0] + (ret->s[0][0] != '*'? 0 : 0) * qr_indel; - q[1] = ret->gl[1] + (ret->s[1][0] != '*'? 0 : 0) * qr_indel; - q[2] = n * 3 + (ret->s[0][0] == '*' || ret->s[1][0] == '*'? 1 : 1) * qr_indel; - min1 = min2 = 0x7fffffff; min1_i = -1; - for (i = 0; i < 3; ++i) { - if (q[i] < min1) { - min2 = min1; min1 = q[i]; min1_i = i; - } else if (q[i] < min2) min2 = q[i]; - } - ret->gt = min1_i; - ret->q_cns = min2 - min1; - // set q_ref - if (ret->gt < 2) ret->q_ref = (ret->s[ret->gt][0] == '*')? 0 : q[1-ret->gt] - q[ret->gt] - qr_indel - 3; - else ret->q_ref = (ret->s[0][0] == '*')? q[0] - q[2] : q[1] - q[2]; - if (ret->q_ref < 0) ret->q_ref = 0; - } - free(types); - return ret; -} diff --git a/samtools/bam_maqcns.c.pysam.c b/samtools/bam_maqcns.c.pysam.c new file mode 100644 index 0000000..afc3a9d --- /dev/null +++ b/samtools/bam_maqcns.c.pysam.c @@ -0,0 +1,630 @@ +#include "pysam.h" + +#include +#include +#include "bam.h" +#include "bam_maqcns.h" +#include "ksort.h" +#include "errmod.h" +#include "kaln.h" +KSORT_INIT_GENERIC(uint32_t) + +#define INDEL_WINDOW_SIZE 50 +#define INDEL_EXT_DEP 0.9 + +typedef struct __bmc_aux_t { + int max; + uint32_t *info; + uint16_t *info16; + errmod_t *em; +} bmc_aux_t; + +typedef struct { + float esum[4], fsum[4]; + uint32_t c[4]; +} glf_call_aux_t; + +char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 }; + +/* + P() = \theta \sum_{i=1}^{N-1} 1/i + P(D|) = \sum_{k=1}^{N-1} p_k 1/2 [(k/N)^n_2(1-k/N)^n_1 + (k/N)^n1(1-k/N)^n_2] + p_k = 1/k / \sum_{i=1}^{N-1} 1/i + */ +static void cal_het(bam_maqcns_t *aa) +{ + int k, n1, n2; + double sum_harmo; // harmonic sum + double poly_rate; + + free(aa->lhet); + aa->lhet = (double*)calloc(256 * 256, sizeof(double)); + sum_harmo = 0.0; + for (k = 1; k <= aa->n_hap - 1; ++k) + sum_harmo += 1.0 / k; + for (n1 = 0; n1 < 256; ++n1) { + for (n2 = 0; n2 < 256; ++n2) { + long double sum = 0.0; + double lC = aa->errmod == BAM_ERRMOD_SOAP? 0 : lgamma(n1+n2+1) - lgamma(n1+1) - lgamma(n2+1); + for (k = 1; k <= aa->n_hap - 1; ++k) { + double pk = 1.0 / k / sum_harmo; + double log1 = log((double)k/aa->n_hap); + double log2 = log(1.0 - (double)k/aa->n_hap); + sum += pk * 0.5 * (expl(log1*n2) * expl(log2*n1) + expl(log1*n1) * expl(log2*n2)); + } + aa->lhet[n1<<8|n2] = lC + logl(sum); + } + } + poly_rate = aa->het_rate * sum_harmo; + aa->q_r = -4.343 * log(2.0 * poly_rate / (1.0 - poly_rate)); +} + +/** initialize the helper structure */ +static void cal_coef(bam_maqcns_t *aa) +{ + int k, n, q; + long double sum_a[257], b[256], q_c[256], tmp[256], fk2[256]; + double *lC; + + if (aa->errmod == BAM_ERRMOD_MAQ2) return; // no need to do the following + // aa->lhet will be allocated and initialized + free(aa->fk); free(aa->coef); + aa->coef = 0; + aa->fk = (double*)calloc(256, sizeof(double)); + aa->fk[0] = fk2[0] = 1.0; + for (n = 1; n != 256; ++n) { + aa->fk[n] = pow(aa->theta, n) * (1.0 - aa->eta) + aa->eta; + fk2[n] = aa->fk[n>>1]; // this is an approximation, assuming reads equally likely come from both strands + } + if (aa->errmod == BAM_ERRMOD_SOAP) return; + aa->coef = (double*)calloc(256*256*64, sizeof(double)); + lC = (double*)calloc(256 * 256, sizeof(double)); + for (n = 1; n != 256; ++n) + for (k = 1; k <= n; ++k) + lC[n<<8|k] = lgamma(n+1) - 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 != 256; ++n) { + double *coef = aa->coef + (q<<16|n<<8); + sum_a[n+1] = 0.0; + for (k = n; k >= 0; --k) { // a_k = \sum_{i=k}^n C^n_k \epsilon^k (1-\epsilon)^{n-k} + sum_a[k] = sum_a[k+1] + expl(lC[n<<8|k] + k*le + (n-k)*le1); + b[k] = sum_a[k+1] / sum_a[k]; + if (b[k] > 0.99) b[k] = 0.99; + } + for (k = 0; k != n; ++k) // log(\bar\beta_{nk}(\bar\epsilon)^{f_k}) + q_c[k] = -4.343 * fk2[k] * logl(b[k] / e); + for (k = 1; k != n; ++k) q_c[k] += q_c[k-1]; // \prod_{i=0}^k c_i + for (k = 0; k <= n; ++k) { // powl() in 64-bit mode seems broken on my Mac OS X 10.4.9 + tmp[k] = -4.343 * logl(1.0 - expl(fk2[k] * logl(b[k]))); + coef[k] = (k? q_c[k-1] : 0) + tmp[k]; // this is the final c_{nk} + } + } + } + free(lC); +} + +bam_maqcns_t *bam_maqcns_init() +{ + bam_maqcns_t *bm; + bm = (bam_maqcns_t*)calloc(1, sizeof(bam_maqcns_t)); + bm->aux = (bmc_aux_t*)calloc(1, sizeof(bmc_aux_t)); + bm->het_rate = 0.001; + bm->theta = 0.83f; + bm->n_hap = 2; + bm->eta = 0.03; + bm->cap_mapQ = 60; + bm->min_baseQ = 13; + return bm; +} + +void bam_maqcns_prepare(bam_maqcns_t *bm) +{ + if (bm->errmod == BAM_ERRMOD_MAQ2) bm->aux->em = errmod_init(1. - bm->theta); + cal_coef(bm); cal_het(bm); +} + +void bam_maqcns_destroy(bam_maqcns_t *bm) +{ + if (bm == 0) return; + free(bm->lhet); free(bm->fk); free(bm->coef); free(bm->aux->info); free(bm->aux->info16); + if (bm->aux->em) errmod_destroy(bm->aux->em); + free(bm->aux); free(bm); +} + +glf1_t *bam_maqcns_glfgen(int _n, const bam_pileup1_t *pl, uint8_t ref_base, bam_maqcns_t *bm) +{ + glf_call_aux_t *b = 0; + int i, j, k, w[8], c, n; + glf1_t *g = (glf1_t*)calloc(1, sizeof(glf1_t)); + float p[16], min_p = 1e30; + uint64_t rms; + + g->ref_base = ref_base; + if (_n == 0) return g; + + // construct aux array + if (bm->aux->max < _n) { + bm->aux->max = _n; + kroundup32(bm->aux->max); + bm->aux->info = (uint32_t*)realloc(bm->aux->info, 4 * bm->aux->max); + bm->aux->info16 = (uint16_t*)realloc(bm->aux->info16, 2 * bm->aux->max); + } + for (i = n = 0, rms = 0; i < _n; ++i) { + const bam_pileup1_t *p = pl + i; + uint32_t q, x = 0, qq; + uint16_t y = 0; + if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue; + q = (uint32_t)bam1_qual(p->b)[p->qpos]; + if (q < bm->min_baseQ) continue; + x |= (uint32_t)bam1_strand(p->b) << 18 | q << 8 | p->b->core.qual; + y |= bam1_strand(p->b)<<4; + if (p->b->core.qual < q) q = p->b->core.qual; + c = p->b->core.qual < bm->cap_mapQ? p->b->core.qual : bm->cap_mapQ; + rms += c * c; + x |= q << 24; + y |= q << 5; + qq = bam1_seqi(bam1_seq(p->b), p->qpos); + q = bam_nt16_nt4_table[qq? qq : ref_base]; + if (!p->is_del && !p->is_refskip && q < 4) x |= 1 << 21 | q << 16, y |= q; + bm->aux->info16[n] = y; + bm->aux->info[n++] = x; + } + rms = (uint8_t)(sqrt((double)rms / n) + .499); + if (bm->errmod == BAM_ERRMOD_MAQ2) { + errmod_cal(bm->aux->em, n, 4, bm->aux->info16, p); + goto goto_glf; + } + ks_introsort(uint32_t, n, bm->aux->info); + // generate esum and fsum + b = (glf_call_aux_t*)calloc(1, sizeof(glf_call_aux_t)); + for (k = 0; k != 8; ++k) w[k] = 0; + for (j = n - 1; j >= 0; --j) { // calculate esum and fsum + uint32_t info = bm->aux->info[j]; + if (info>>24 < 4 && (info>>8&0x3f) != 0) info = 4<<24 | (info&0xffffff); + k = info>>16&7; + if (info>>24 > 0) { + b->esum[k&3] += bm->fk[w[k]] * (info>>24); + b->fsum[k&3] += bm->fk[w[k]]; + if (w[k] < 0xff) ++w[k]; + ++b->c[k&3]; + } + } + // rescale ->c[] + for (j = c = 0; j != 4; ++j) c += b->c[j]; + if (c > 255) { + for (j = 0; j != 4; ++j) b->c[j] = (int)(254.0 * b->c[j] / c + 0.5); + for (j = c = 0; j != 4; ++j) c += b->c[j]; + } + if (bm->errmod == BAM_ERRMOD_MAQ) { + // generate likelihood + for (j = 0; j != 4; ++j) { + // homozygous + float tmp1, tmp3; + int tmp2, bar_e; + for (k = 0, tmp1 = tmp3 = 0.0, tmp2 = 0; k != 4; ++k) { + if (j == k) continue; + tmp1 += b->esum[k]; tmp2 += b->c[k]; tmp3 += b->fsum[k]; + } + if (tmp2) { + bar_e = (int)(tmp1 / tmp3 + 0.5); + if (bar_e < 4) bar_e = 4; // should not happen + if (bar_e > 63) bar_e = 63; + p[j<<2|j] = tmp1 + bm->coef[bar_e<<16|c<<8|tmp2]; + } else p[j<<2|j] = 0.0; // all the bases are j + // heterozygous + for (k = j + 1; k < 4; ++k) { + for (i = 0, tmp2 = 0, tmp1 = tmp3 = 0.0; i != 4; ++i) { + if (i == j || i == k) continue; + tmp1 += b->esum[i]; tmp2 += b->c[i]; tmp3 += b->fsum[i]; + } + if (tmp2) { + bar_e = (int)(tmp1 / tmp3 + 0.5); + if (bar_e < 4) bar_e = 4; + if (bar_e > 63) bar_e = 63; + p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]] + tmp1 + bm->coef[bar_e<<16|c<<8|tmp2]; + } else p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]]; // all the bases are either j or k + } + // + for (k = 0; k != 4; ++k) + if (p[j<<2|k] < 0.0) p[j<<2|k] = 0.0; + } + + { // fix p[k<<2|k] + float max1, max2, min1, min2; + int max_k, min_k; + max_k = min_k = -1; + max1 = max2 = -1.0; min1 = min2 = 1e30; + for (k = 0; k < 4; ++k) { + if (b->esum[k] > max1) { + max2 = max1; max1 = b->esum[k]; max_k = k; + } else if (b->esum[k] > max2) max2 = b->esum[k]; + } + for (k = 0; k < 4; ++k) { + if (p[k<<2|k] < min1) { + min2 = min1; min1 = p[k<<2|k]; min_k = k; + } else if (p[k<<2|k] < min2) min2 = p[k<<2|k]; + } + if (max1 > max2 && (min_k != max_k || min1 + 1.0 > min2)) + p[max_k<<2|max_k] = min1 > 1.0? min1 - 1.0 : 0.0; + } + } else if (bm->errmod == BAM_ERRMOD_SOAP) { // apply the SOAP model + // generate likelihood + for (j = 0; j != 4; ++j) { + float tmp; + // homozygous + for (k = 0, tmp = 0.0; k != 4; ++k) + if (j != k) tmp += b->esum[k]; + p[j<<2|j] = tmp; + // heterozygous + for (k = j + 1; k < 4; ++k) { + for (i = 0, tmp = 0.0; i != 4; ++i) + if (i != j && i != k) tmp += b->esum[i]; + p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]] + tmp; + } + } + } + +goto_glf: + // convert necessary information to glf1_t + g->ref_base = ref_base; g->max_mapQ = rms; + g->depth = n > 16777215? 16777215 : n; + for (j = 0; j != 4; ++j) + for (k = j; k < 4; ++k) + if (p[j<<2|k] < min_p) min_p = p[j<<2|k]; + g->min_lk = min_p > 255.0? 255 : (int)(min_p + 0.5); + for (j = c = 0; j != 4; ++j) + for (k = j; k < 4; ++k) + g->lk[c++] = p[j<<2|k]-min_p > 255.0? 255 : (int)(p[j<<2|k]-min_p + 0.5); + + free(b); + return g; +} + +uint32_t glf2cns(const glf1_t *g, int q_r) +{ + int i, j, k, p[10], ref4; + uint32_t x = 0; + ref4 = bam_nt16_nt4_table[g->ref_base]; + for (i = k = 0; i < 4; ++i) + for (j = i; j < 4; ++j) { + int prior = (i == ref4 && j == ref4? 0 : i == ref4 || j == ref4? q_r : q_r + 3); + p[k] = (g->lk[k] + prior)<<4 | i<<2 | j; + ++k; + } + for (i = 1; i < 10; ++i) // insertion sort + for (j = i; j > 0 && p[j] < p[j-1]; --j) + k = p[j], p[j] = p[j-1], p[j-1] = k; + x = (1u<<(p[0]&3) | 1u<<(p[0]>>2&3)) << 28; // the best genotype + x |= (uint32_t)g->max_mapQ << 16; // rms mapQ + x |= ((p[1]>>4) - (p[0]>>4) < 256? (p[1]>>4) - (p[0]>>4) : 255) << 8; // consensus Q + for (k = 0; k < 10; ++k) + if ((p[k]&0xf) == (ref4<<2|ref4)) break; + if (k == 10) k = 9; + x |= (p[k]>>4) - (p[0]>>4) < 256? (p[k]>>4) - (p[0]>>4) : 255; // snp Q + return x; +} + +uint32_t bam_maqcns_call(int n, const bam_pileup1_t *pl, bam_maqcns_t *bm) +{ + glf1_t *g; + uint32_t x; + if (n) { + g = bam_maqcns_glfgen(n, pl, 0xf, bm); + x = g->depth == 0? (0xfU<<28 | 0xfU<<24) : glf2cns(g, (int)(bm->q_r + 0.5)); + free(g); + } else x = 0xfU<<28 | 0xfU<<24; + return x; +} + +/************** *****************/ + +bam_maqindel_opt_t *bam_maqindel_opt_init() +{ + bam_maqindel_opt_t *mi = (bam_maqindel_opt_t*)calloc(1, sizeof(bam_maqindel_opt_t)); + mi->q_indel = 40; + mi->r_indel = 0.00015; + mi->r_snp = 0.001; + // + mi->mm_penalty = 3; + mi->indel_err = 4; + mi->ambi_thres = 10; + return mi; +} + +void bam_maqindel_ret_destroy(bam_maqindel_ret_t *mir) +{ + if (mir == 0) return; + free(mir->s[0]); free(mir->s[1]); free(mir); +} + +int bam_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) { + 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; +} + +#define MINUS_CONST 0x10000000 + +bam_maqindel_ret_t *bam_maqindel(int n, int pos, const bam_maqindel_opt_t *mi, const bam_pileup1_t *pl, const char *ref, + int _n_types, int *_types) +{ + int i, j, n_types, *types, left, right, max_rd_len = 0; + bam_maqindel_ret_t *ret = 0; + // if there is no proposed indel, check if there is an indel from the alignment + if (_n_types == 0) { + for (i = 0; i < n; ++i) { + const bam_pileup1_t *p = pl + i; + if (!(p->b->core.flag&BAM_FUNMAP) && p->indel != 0) break; + } + if (i == n) return 0; // no indel + } + { // calculate how many types of indels are available (set n_types and types) + int m; + uint32_t *aux; + aux = (uint32_t*)calloc(n + _n_types + 1, 4); + m = 0; + aux[m++] = MINUS_CONST; // zero indel is always a type + for (i = 0; i < n; ++i) { + const bam_pileup1_t *p = pl + i; + if (!(p->b->core.flag&BAM_FUNMAP) && p->indel != 0) + 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; + } + if (_n_types) // then also add this to aux[] + for (i = 0; i < _n_types; ++i) + if (_types[i]) aux[m++] = MINUS_CONST + _types[i]; + 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; + types = (int*)calloc(n_types, sizeof(int)); + j = 0; + types[j++] = aux[0] - MINUS_CONST; + for (i = 1; i < m; ++i) { + if (aux[i] != aux[i-1]) + types[j++] = aux[i] - MINUS_CONST; + } + free(aux); + } + { // 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 core part + char *ref2, *rs, *inscns = 0; + int qr_snp, k, l, *score, *pscore, max_ins = types[n_types-1]; + qr_snp = (int)(-4.343 * log(mi->r_snp) + .499); + if (max_ins > 0) { // get the consensus of inserted sequences + int *inscns_aux = (int*)calloc(4 * n_types * max_ins, sizeof(int)); + // count occurrences + for (i = 0; i < n_types; ++i) { + if (types[i] <= 0) continue; // not insertion + for (j = 0; j < n; ++j) { + const bam_pileup1_t *p = pl + j; + if (!(p->b->core.flag&BAM_FUNMAP) && p->indel == types[i]) { + for (k = 1; k <= p->indel; ++k) { + int c = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos + k)]; + if (c < 4) ++inscns_aux[i*max_ins*4 + (k-1)*4 + c]; + } + } + } + } + // construct the consensus of inserted sequence + inscns = (char*)calloc(n_types * max_ins, sizeof(char)); + for (i = 0; i < n_types; ++i) { + for (j = 0; j < types[i]; ++j) { + int max = 0, max_k = -1, *ia = inscns_aux + i*max_ins*4 + j*4; + for (k = 0; k < 4; ++k) { + if (ia[k] > max) { + max = ia[k]; + max_k = k; + } + } + inscns[i*max_ins + j] = max? 1<= 0? -types[0] : -types[0] + types[i]; + for (jj = 0; jj < tmp && j < right && ref[j]; ++jj, ++j) + ref2[k++] = 4; + } + for (; j < right && ref[j]; ++j) + ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]]; + if (j < right) right = j; + // calculate score for each read + for (j = 0; j < n; ++j) { + const bam_pileup1_t *p = pl + j; + int qbeg, qend, tbeg, tend; + if (p->b->core.flag & BAM_FUNMAP) continue; + qbeg = bam_tpos2qpos(&p->b->core, bam1_cigar(p->b), left, 0, &tbeg); + qend = bam_tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend); + assert(tbeg >= left); + for (l = qbeg; l < qend; ++l) + rs[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), l)]; + { + int x, y, n_acigar, ps; + uint32_t *acigar; + ps = 0; + if (tend - tbeg + types[i] <= 0) { + score[i*n+j] = -(1<<20); + pscore[i*n+j] = 1<<20; + continue; + } + acigar = ka_global_core((uint8_t*)ref2 + tbeg - left, tend - tbeg + types[i], (uint8_t*)rs, qend - qbeg, &ap, &score[i*n+j], &n_acigar); + x = tbeg - left; y = 0; + for (l = 0; l < n_acigar; ++l) { + int op = acigar[l]&0xf; + int len = acigar[l]>>4; + if (op == BAM_CMATCH) { + int k; + for (k = 0; k < len; ++k) + if (ref2[x+k] != rs[y+k] && ref2[x+k] < 4) + ps += bam1_qual(p->b)[y+k] < qr_snp? bam1_qual(p->b)[y+k] : qr_snp; + x += len; y += len; + } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) { + if (op == BAM_CINS && l > 0 && l < n_acigar - 1) ps += mi->q_indel * len; + y += len; + } else if (op == BAM_CDEL) { + if (l > 0 && l < n_acigar - 1) ps += mi->q_indel * len; + x += len; + } + } + pscore[i*n+j] = ps; + /*if (1) { // for debugging only + fprintf(pysamerr, "id=%d, pos=%d, type=%d, j=%d, score=%d, psore=%d, %d, %d, %d, %d, %d, ", + j, pos+1, types[i], j, score[i*n+j], pscore[i*n+j], tbeg, tend, qbeg, qend, mi->q_indel); + for (l = 0; l < n_acigar; ++l) fprintf(pysamerr, "%d%c", acigar[l]>>4, "MIDS"[acigar[l]&0xf]); + fprintf(pysamerr, "\n"); + for (l = 0; l < tend - tbeg + types[i]; ++l) fputc("ACGTN"[ref2[l+tbeg-left]], pysamerr); + fputc('\n', pysamerr); + for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[rs[l]], pysamerr); + fputc('\n', pysamerr); + }*/ + free(acigar); + } + } + } + { // get final result + int *sum, max1, max2, max1_i, max2_i; + // pick up the best two score + sum = (int*)calloc(n_types, sizeof(int)); + for (i = 0; i < n_types; ++i) + for (j = 0; j < n; ++j) + sum[i] += -pscore[i*n+j]; + max1 = max2 = -0x7fffffff; max1_i = max2_i = -1; + for (i = 0; i < n_types; ++i) { + if (sum[i] > max1) { + max2 = max1; max2_i = max1_i; max1 = sum[i]; max1_i = i; + } else if (sum[i] > max2) { + max2 = sum[i]; max2_i = i; + } + } + free(sum); + // write ret + ret = (bam_maqindel_ret_t*)calloc(1, sizeof(bam_maqindel_ret_t)); + ret->indel1 = types[max1_i]; ret->indel2 = types[max2_i]; + ret->s[0] = (char*)calloc(abs(ret->indel1) + 2, 1); + ret->s[1] = (char*)calloc(abs(ret->indel2) + 2, 1); + // write indel sequence + if (ret->indel1 > 0) { + ret->s[0][0] = '+'; + for (k = 0; k < ret->indel1; ++k) + ret->s[0][k+1] = bam_nt16_rev_table[(int)inscns[max1_i*max_ins + k]]; + } else if (ret->indel1 < 0) { + ret->s[0][0] = '-'; + for (k = 0; k < -ret->indel1 && ref[pos + k + 1]; ++k) + ret->s[0][k+1] = ref[pos + k + 1]; + } else ret->s[0][0] = '*'; + if (ret->indel2 > 0) { + ret->s[1][0] = '+'; + for (k = 0; k < ret->indel2; ++k) + ret->s[1][k+1] = bam_nt16_rev_table[(int)inscns[max2_i*max_ins + k]]; + } else if (ret->indel2 < 0) { + ret->s[1][0] = '-'; + for (k = 0; k < -ret->indel2 && ref[pos + k + 1]; ++k) + ret->s[1][k+1] = ref[pos + k + 1]; + } else ret->s[1][0] = '*'; + // write count + for (i = 0; i < n; ++i) { + const bam_pileup1_t *p = pl + i; + if (p->indel == ret->indel1) ++ret->cnt1; + else if (p->indel == ret->indel2) ++ret->cnt2; + else ++ret->cnt_anti; + } + { // write gl[] + int tmp, seq_err = 0; + double x = 1.0; + tmp = max1_i - max2_i; + if (tmp < 0) tmp = -tmp; + for (j = 0; j < tmp + 1; ++j) x *= INDEL_EXT_DEP; + seq_err = mi->q_indel * (1.0 - x) / (1.0 - INDEL_EXT_DEP); + ret->gl[0] = ret->gl[1] = 0; + for (j = 0; j < n; ++j) { + int s1 = pscore[max1_i*n + j], s2 = pscore[max2_i*n + j]; + //fprintf(pysamerr, "id=%d, %d, %d, %d, %d, %d\n", j, pl[j].b->core.pos+1, types[max1_i], types[max2_i], s1, s2); + if (s1 > s2) ret->gl[0] += s1 - s2 < seq_err? s1 - s2 : seq_err; + else ret->gl[1] += s2 - s1 < seq_err? s2 - s1 : seq_err; + } + } + // write cnt_ref and cnt_ambi + if (max1_i != 0 && max2_i != 0) { + for (j = 0; j < n; ++j) { + int diff1 = score[j] - score[max1_i * n + j]; + int diff2 = score[j] - score[max2_i * n + j]; + if (diff1 > 0 && diff2 > 0) ++ret->cnt_ref; + else if (diff1 == 0 || diff2 == 0) ++ret->cnt_ambi; + } + } + } + free(score); free(pscore); free(ref2); free(rs); free(inscns); + } + { // call genotype + int q[3], qr_indel = (int)(-4.343 * log(mi->r_indel) + 0.5); + int min1, min2, min1_i; + q[0] = ret->gl[0] + (ret->s[0][0] != '*'? 0 : 0) * qr_indel; + q[1] = ret->gl[1] + (ret->s[1][0] != '*'? 0 : 0) * qr_indel; + q[2] = n * 3 + (ret->s[0][0] == '*' || ret->s[1][0] == '*'? 1 : 1) * qr_indel; + min1 = min2 = 0x7fffffff; min1_i = -1; + for (i = 0; i < 3; ++i) { + if (q[i] < min1) { + min2 = min1; min1 = q[i]; min1_i = i; + } else if (q[i] < min2) min2 = q[i]; + } + ret->gt = min1_i; + ret->q_cns = min2 - min1; + // set q_ref + if (ret->gt < 2) ret->q_ref = (ret->s[ret->gt][0] == '*')? 0 : q[1-ret->gt] - q[ret->gt] - qr_indel - 3; + else ret->q_ref = (ret->s[0][0] == '*')? q[0] - q[2] : q[1] - q[2]; + if (ret->q_ref < 0) ret->q_ref = 0; + } + free(types); + return ret; +} diff --git a/samtools/bam_maqcns.h b/samtools/bam_maqcns.h index 6cc5355..291ae53 100644 --- a/samtools/bam_maqcns.h +++ b/samtools/bam_maqcns.h @@ -3,11 +3,15 @@ #include "glf.h" +#define BAM_ERRMOD_MAQ2 0 +#define BAM_ERRMOD_MAQ 1 +#define BAM_ERRMOD_SOAP 2 + struct __bmc_aux_t; typedef struct { float het_rate, theta; - int n_hap, cap_mapQ, is_soap; + int n_hap, cap_mapQ, errmod, min_baseQ; float eta, q_r; double *fk, *coef; diff --git a/samtools/bam_mate.c b/samtools/bam_mate.c deleted file mode 100644 index 61f808a..0000000 --- a/samtools/bam_mate.c +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include "bam.h" - -// currently, this function ONLY works if each read has one hit -void bam_mating_core(bamFile in, bamFile out) -{ - bam_header_t *header; - bam1_t *b[2]; - int curr, has_prev; - - 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 (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))) - { - uint32_t cur5, pre5; - cur5 = (cur->core.flag&BAM_FREVERSE)? bam_calend(&cur->core, bam1_cigar(cur)) : cur->core.pos; - pre5 = (pre->core.flag&BAM_FREVERSE)? bam_calend(&pre->core, bam1_cigar(pre)) : 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_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; - } - if (has_prev) bam_write1(out, b[1-curr]); - bam_header_destroy(header); - bam_destroy1(b[0]); - bam_destroy1(b[1]); -} - -int bam_mating(int argc, char *argv[]) -{ - bamFile in, out; - if (argc < 3) { - fprintf(stderr, "samtools fixmate \n"); - return 1; - } - in = (strcmp(argv[1], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[1], "r"); - out = (strcmp(argv[2], "-") == 0)? bam_dopen(fileno(stdout), "w") : bam_open(argv[2], "w"); - bam_mating_core(in, out); - bam_close(in); bam_close(out); - return 0; -} diff --git a/samtools/bam_mate.c.pysam.c b/samtools/bam_mate.c.pysam.c new file mode 100644 index 0000000..ebb797b --- /dev/null +++ b/samtools/bam_mate.c.pysam.c @@ -0,0 +1,72 @@ +#include "pysam.h" + +#include +#include +#include "bam.h" + +// currently, this function ONLY works if each read has one hit +void bam_mating_core(bamFile in, bamFile out) +{ + bam_header_t *header; + bam1_t *b[2]; + int curr, has_prev; + + 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 (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))) + { + uint32_t cur5, pre5; + cur5 = (cur->core.flag&BAM_FREVERSE)? bam_calend(&cur->core, bam1_cigar(cur)) : cur->core.pos; + pre5 = (pre->core.flag&BAM_FREVERSE)? bam_calend(&pre->core, bam1_cigar(pre)) : 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_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; + } + if (has_prev) bam_write1(out, b[1-curr]); + bam_header_destroy(header); + bam_destroy1(b[0]); + bam_destroy1(b[1]); +} + +int bam_mating(int argc, char *argv[]) +{ + bamFile in, out; + if (argc < 3) { + fprintf(pysamerr, "samtools fixmate \n"); + return 1; + } + in = (strcmp(argv[1], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[1], "r"); + out = (strcmp(argv[2], "-") == 0)? bam_dopen(fileno(stdout), "w") : bam_open(argv[2], "w"); + bam_mating_core(in, out); + bam_close(in); bam_close(out); + return 0; +} diff --git a/samtools/bam_md.c b/samtools/bam_md.c deleted file mode 100644 index 17b0a4a..0000000 --- a/samtools/bam_md.c +++ /dev/null @@ -1,175 +0,0 @@ -#include -#include -#include -#include -#include "faidx.h" -#include "sam.h" -#include "kstring.h" - -void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, 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; - uint8_t *old_md, *old_nm; - 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) { - 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 (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f; - ++u; - } else { - ksprintf(str, "%d", u); - kputc(ref[x+j], str); - u = 0; ++nm; - } - } - if (j < l) break; - x += l; y += l; - } else if (op == BAM_CDEL) { - ksprintf(str, "%d", u); - 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; - } - } - ksprintf(str, "%d", u); - // 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) { - 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 - 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(stderr, "[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 - 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(stderr, "[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); - } - } - free(str->s); free(str); -} - -void bam_fillmd1(bam1_t *b, char *ref, int is_equal) -{ - bam_fillmd1_core(b, ref, is_equal, 0); -} - -int bam_fillmd(int argc, char *argv[]) -{ - int c, is_equal = 0, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm = 0; - samfile_t *fp, *fpout = 0; - faidx_t *fai; - char *ref = 0, mode_w[8], mode_r[8]; - bam1_t *b; - - is_bam_out = is_sam_in = is_uncompressed = 0; - mode_w[0] = mode_r[0] = 0; - strcpy(mode_r, "r"); strcpy(mode_w, "w"); - while ((c = getopt(argc, argv, "eubSn:")) >= 0) { - switch (c) { - case 'e': is_equal = 1; 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; - default: fprintf(stderr, "[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(stderr, "\n"); - fprintf(stderr, "Usage: samtools fillmd [-eubS] \n\n"); - fprintf(stderr, "Options: -e change identical bases to '='\n"); - fprintf(stderr, " -u uncompressed BAM output (for piping)\n"); - fprintf(stderr, " -b compressed BAM output\n"); - fprintf(stderr, " -S the input is SAM with header\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(stderr, "[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(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n", - fp->header->target_name[tid]); - } - if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm); - } - samwrite(fpout, b); - } - bam_destroy1(b); - - free(ref); - fai_destroy(fai); - samclose(fp); samclose(fpout); - return 0; -} diff --git a/samtools/bam_md.c.pysam.c b/samtools/bam_md.c.pysam.c new file mode 100644 index 0000000..75e8f6e --- /dev/null +++ b/samtools/bam_md.c.pysam.c @@ -0,0 +1,331 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#include "faidx.h" +#include "sam.h" +#include "kstring.h" +#include "kaln.h" +#include "kprobaln.h" + +void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, 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; + uint8_t *old_md, *old_nm; + 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) { + 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 (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f; + ++u; + } else { + ksprintf(str, "%d", u); + kputc(ref[x+j], str); + u = 0; ++nm; + } + } + if (j < l) break; + x += l; y += l; + } else if (op == BAM_CDEL) { + ksprintf(str, "%d", u); + 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; + } + } + ksprintf(str, "%d", u); + // 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) { + 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 + 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 + 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); + } + } + free(str->s); free(str); +} + +void bam_fillmd1(bam1_t *b, char *ref, int is_equal) +{ + bam_fillmd1_core(b, ref, is_equal, 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) { + 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 apply_baq) +{ + int k, i, bw, x, y, yb, ye, xb, xe; + 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) 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 && 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) { + 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) + 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); + 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) { + 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 + 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); +} + +int bam_fillmd(int argc, char *argv[]) +{ + int c, is_equal, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, apply_baq; + samfile_t *fp, *fpout = 0; + faidx_t *fai; + char *ref = 0, mode_w[8], mode_r[8]; + bam1_t *b; + + is_equal = is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = apply_baq = capQ = 0; + mode_w[0] = mode_r[0] = 0; + strcpy(mode_r, "r"); strcpy(mode_w, "w"); + while ((c = getopt(argc, argv, "reubSC:n:A")) >= 0) { + switch (c) { + case 'r': is_realn = 1; break; + case 'e': is_equal = 1; 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': apply_baq = 1; 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] \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 read-independent local realignment\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, apply_baq); + 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, is_equal, max_nm); + } + samwrite(fpout, b); + } + bam_destroy1(b); + + free(ref); + fai_destroy(fai); + samclose(fp); samclose(fpout); + return 0; +} diff --git a/samtools/bam_pileup.c b/samtools/bam_pileup.c deleted file mode 100644 index 3c41a16..0000000 --- a/samtools/bam_pileup.c +++ /dev/null @@ -1,396 +0,0 @@ -#include -#include -#include -#include -#include "sam.h" - -typedef struct __linkbuf_t { - bam1_t b; - uint32_t beg, end; - 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() -{ - 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 */ - -static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos) -{ - unsigned k; - bam1_t *b = p->b; - bam1_core_t *c = &b->core; - uint32_t x = c->pos, y = 0; - int ret = 1, is_restart = 1; - - if (c->flag&BAM_FUNMAP) return 0; // unmapped read - assert(x <= pos); // otherwise a bug - p->qpos = -1; p->indel = 0; p->is_del = p->is_head = p->is_tail = 0; - for (k = 0; k < c->n_cigar; ++k) { - int op = bam1_cigar(b)[k] & BAM_CIGAR_MASK; // operation - int l = bam1_cigar(b)[k] >> BAM_CIGAR_SHIFT; // length - if (op == BAM_CMATCH) { // NOTE: this assumes the first and the last operation MUST BE a match or a clip - if (x + l > pos) { // overlap with pos - p->indel = p->is_del = 0; - p->qpos = y + (pos - x); - if (x == pos && is_restart) p->is_head = 1; - if (x + l - 1 == pos) { // come to the end of a match - int has_next_match = 0; - unsigned i; - for (i = k + 1; i < c->n_cigar; ++i) { - uint32_t cigar = bam1_cigar(b)[i]; - int opi = cigar&BAM_CIGAR_MASK; - if (opi == BAM_CMATCH) { - has_next_match = 1; - break; - } else if (opi == BAM_CSOFT_CLIP || opi == BAM_CREF_SKIP || opi == BAM_CHARD_CLIP) break; - } - if (!has_next_match) p->is_tail = 1; - if (k < c->n_cigar - 1 && has_next_match) { // there are additional operation(s) - uint32_t cigar = bam1_cigar(b)[k+1]; // next CIGAR - int op_next = cigar&BAM_CIGAR_MASK; // next CIGAR operation - if (op_next == BAM_CDEL) p->indel = -(int32_t)(cigar>>BAM_CIGAR_SHIFT); // del - else if (op_next == BAM_CINS) p->indel = cigar>>BAM_CIGAR_SHIFT; // ins - else if (op_next == BAM_CPAD && k + 2 < c->n_cigar) { // no working for adjacent padding - cigar = bam1_cigar(b)[k+2]; op_next = cigar&BAM_CIGAR_MASK; - if (op_next == BAM_CDEL) p->indel = -(int32_t)(cigar>>BAM_CIGAR_SHIFT); // del - else if (op_next == BAM_CINS) p->indel = cigar>>BAM_CIGAR_SHIFT; // ins - } - } - } - } - x += l; y += l; - } else if (op == BAM_CDEL) { // then set ->is_del - if (x + l > pos) { - p->indel = 0; p->is_del = 1; - p->qpos = y + (pos - x); - } - x += l; - } else if (op == BAM_CREF_SKIP) x += l; - else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l; - if (is_restart) is_restart ^= (op == BAM_CMATCH); - else is_restart ^= (op == BAM_CREF_SKIP || op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP); - if (x > pos) { - if (op == BAM_CREF_SKIP) ret = 0; // then do not put it into pileup at all - break; - } - } - assert(x > pos); // otherwise a bug - return ret; -} - -/* --- 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; - 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; - 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(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); -} - -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_cigar(iter->plp + n_plp, iter->pos)) ++n_plp; // skip the read if we are looking at ref-skip - } - } - 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) return 0; - if (b->core.flag & iter->flag_mask) return 0; - bam_copy1(&iter->tail->b, b); - iter->tail->beg = b->core.pos; iter->tail->end = bam_calend(&b->core, bam1_cigar(b)); - 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 { - *_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; - } - 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); -} - -/***************** - * 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_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) { - 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_pileup.c.pysam.c b/samtools/bam_pileup.c.pysam.c new file mode 100644 index 0000000..59e54c0 --- /dev/null +++ b/samtools/bam_pileup.c.pysam.c @@ -0,0 +1,439 @@ +#include "pysam.h" + +#include +#include +#include +#include +#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() +{ + 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) 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) 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) { // jump to the next without a loop + if (_cop(cigar[s->k]) == BAM_CMATCH) s->y += l; + s->x += l; + ++s->k; + } else { // find the next M/D/N + if (_cop(cigar[s->k]) == BAM_CMATCH) 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) 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) break; + } + if (l3 > 0) p->indel = l3; + } + } + if (op == BAM_CMATCH) { + 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_plcmd.c b/samtools/bam_plcmd.c deleted file mode 100644 index 6804795..0000000 --- a/samtools/bam_plcmd.c +++ /dev/null @@ -1,570 +0,0 @@ -#include -#include -#include -#include -#include "sam.h" -#include "faidx.h" -#include "bam_maqcns.h" -#include "khash.h" -#include "glf.h" -#include "kstring.h" - -typedef int *indel_list_t; -KHASH_MAP_INIT_INT64(64, indel_list_t) - -#define BAM_PLF_SIMPLE 0x01 -#define BAM_PLF_CNS 0x02 -#define BAM_PLF_INDEL_ONLY 0x04 -#define BAM_PLF_GLF 0x08 -#define BAM_PLF_VAR_ONLY 0x10 -#define BAM_PLF_2ND 0x20 -#define BAM_PLF_RANBASE 0x40 -#define BAM_PLF_1STBASE 0x80 -#define BAM_PLF_ALLBASE 0x100 -#define BAM_PLF_READPOS 0x200 - -typedef struct { - bam_header_t *h; - bam_maqcns_t *c; - bam_maqindel_opt_t *ido; - faidx_t *fai; - khash_t(64) *hash; - uint32_t format; - int tid, len, last_pos; - int mask; - int max_depth; // for indel calling, ignore reads with the depth too high. 0 for unlimited - char *ref; - glfFile fp_glf; // for glf output only -} pu_data_t; - -char **__bam_get_lines(const char *fn, int *_n); -void bam_init_header_hash(bam_header_t *header); -int32_t bam_get_tid(const bam_header_t *header, const char *seq_name); - -static khash_t(64) *load_pos(const char *fn, bam_header_t *h) -{ - char **list; - int i, j, n, *fields, max_fields; - khash_t(64) *hash; - bam_init_header_hash(h); - list = __bam_get_lines(fn, &n); - hash = kh_init(64); - max_fields = 0; fields = 0; - for (i = 0; i < n; ++i) { - char *str = list[i]; - int chr, n_fields, ret; - khint_t k; - uint64_t x; - n_fields = ksplit_core(str, 0, &max_fields, &fields); - if (n_fields < 2) continue; - chr = bam_get_tid(h, str + fields[0]); - if (chr < 0) { - fprintf(stderr, "[load_pos] unknown reference sequence name: %s\n", str + fields[0]); - continue; - } - x = (uint64_t)chr << 32 | (atoi(str + fields[1]) - 1); - k = kh_put(64, hash, x, &ret); - if (ret == 0) { - fprintf(stderr, "[load_pos] position %s:%s has been loaded.\n", str+fields[0], str+fields[1]); - continue; - } - kh_val(hash, k) = 0; - if (n_fields > 2) { - // count - for (j = 2; j < n_fields; ++j) { - char *s = str + fields[j]; - if ((*s != '+' && *s != '-') || !isdigit(s[1])) break; - } - if (j > 2) { // update kh_val() - int *q, y, z; - q = kh_val(hash, k) = (int*)calloc(j - 1, sizeof(int)); - q[0] = j - 2; z = j; y = 1; - for (j = 2; j < z; ++j) - q[y++] = atoi(str + fields[j]); - } - } - free(str); - } - free(list); free(fields); - return hash; -} - -// an analogy to pileup_func() below -static int glt3_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data) -{ - pu_data_t *d = (pu_data_t*)data; - bam_maqindel_ret_t *r = 0; - int rb, *proposed_indels = 0; - glf1_t *g; - glf3_t *g3; - - if (d->fai == 0) { - fprintf(stderr, "[glt3_func] reference sequence is required for generating GLT. Abort!\n"); - exit(1); - } - if (d->hash) { // only output a list of sites - khint_t k = kh_get(64, d->hash, (uint64_t)tid<<32|pos); - if (k == kh_end(d->hash)) return 0; - proposed_indels = kh_val(d->hash, k); - } - g3 = glf3_init1(); - if (d->fai && (int)tid != d->tid) { - if (d->ref) { // then write the end mark - g3->rtype = GLF3_RTYPE_END; - glf3_write1(d->fp_glf, g3); - } - glf3_ref_write(d->fp_glf, d->h->target_name[tid], d->h->target_len[tid]); // write reference - free(d->ref); - d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len); - d->tid = tid; - d->last_pos = 0; - } - rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N'; - g = bam_maqcns_glfgen(n, pu, bam_nt16_table[rb], d->c); - memcpy(g3, g, sizeof(glf1_t)); - g3->rtype = GLF3_RTYPE_SUB; - g3->offset = pos - d->last_pos; - d->last_pos = pos; - glf3_write1(d->fp_glf, g3); - if (pos < d->len) { - int m = (!d->max_depth || d->max_depth>n) ? n : d->max_depth; - if (proposed_indels) - r = bam_maqindel(m, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1); - else r = bam_maqindel(m, pos, d->ido, pu, d->ref, 0, 0); - } - if (r) { // then write indel line - int het = 3 * n, min; - min = het; - if (min > r->gl[0]) min = r->gl[0]; - if (min > r->gl[1]) min = r->gl[1]; - g3->ref_base = 0; - g3->rtype = GLF3_RTYPE_INDEL; - memset(g3->lk, 0, 10); - g3->lk[0] = r->gl[0] - min < 255? r->gl[0] - min : 255; - g3->lk[1] = r->gl[1] - min < 255? r->gl[1] - min : 255; - g3->lk[2] = het - min < 255? het - min : 255; - g3->offset = 0; - g3->indel_len[0] = r->indel1; - g3->indel_len[1] = r->indel2; - g3->min_lk = min < 255? min : 255; - g3->max_len = (abs(r->indel1) > abs(r->indel2)? abs(r->indel1) : abs(r->indel2)) + 1; - g3->indel_seq[0] = strdup(r->s[0]+1); - g3->indel_seq[1] = strdup(r->s[1]+1); - glf3_write1(d->fp_glf, g3); - bam_maqindel_ret_destroy(r); - } - free(g); - glf3_destroy1(g3); - return 0; -} - -static void pileup_seq(const bam_pileup1_t *p, int pos, int ref_len, const char *ref) -{ - if (p->is_head) printf("^%c", p->b->core.qual > 93? 126 : p->b->core.qual + 33); - if (!p->is_del) { - int j, rb, c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; - rb = (ref && pos < ref_len)? ref[pos] : 'N'; - if (c == '=' || toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.'; - else c = bam1_strand(p->b)? tolower(c) : toupper(c); - putchar(c); - if (p->indel > 0) { - printf("+%d", p->indel); - for (j = 1; j <= p->indel; ++j) { - 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) { - printf("%d", p->indel); - for (j = 1; j <= -p->indel; ++j) { - c = (ref && (int)pos+j < ref_len)? ref[pos+j] : 'N'; - putchar(bam1_strand(p->b)? tolower(c) : toupper(c)); - } - } - } else putchar('*'); - if (p->is_tail) putchar('$'); -} - -static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data) -{ - pu_data_t *d = (pu_data_t*)data; - bam_maqindel_ret_t *r = 0; - int i, rb, rms_mapq = -1, *proposed_indels = 0; - uint64_t rms_aux; - uint32_t cns = 0; - - // if GLF is required, suppress -c completely - if (d->format & BAM_PLF_GLF) return glt3_func(tid, pos, n, pu, data); - // if d->hash is initialized, only output the sites in the hash table - if (d->hash) { - khint_t k = kh_get(64, d->hash, (uint64_t)tid<<32|pos); - if (k == kh_end(d->hash)) return 0; - proposed_indels = kh_val(d->hash, k); - } - // update d->ref if necessary - if (d->fai && (int)tid != d->tid) { - free(d->ref); - d->ref = faidx_fetch_seq(d->fai, d->h->target_name[tid], 0, 0x7fffffff, &d->len); - d->tid = tid; - } - rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N'; - // when the indel-only mode is asked for, return if no reads mapped with indels - if (d->format & BAM_PLF_INDEL_ONLY) { - for (i = 0; i < n; ++i) - if (pu[i].indel != 0) break; - if (i == n) return 0; - } - // call the consensus and indel - if (d->format & BAM_PLF_CNS) { // call consensus - if (d->format & (BAM_PLF_RANBASE|BAM_PLF_1STBASE)) { // use a random base or the 1st base as the consensus call - const bam_pileup1_t *p = (d->format & BAM_PLF_1STBASE)? pu : pu + (int)(drand48() * n); - int q = bam1_qual(p->b)[p->qpos]; - int mapQ = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ; - uint32_t b = bam1_seqi(bam1_seq(p->b), p->qpos); - cns = b<<28 | 0xf<<24 | mapQ<<16 | q<<8; - } else if (d->format & BAM_PLF_ALLBASE) { // collapse all bases - uint64_t rmsQ = 0; - uint32_t b = 0; - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pu + i; - int q = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ; - b |= bam1_seqi(bam1_seq(p->b), p->qpos); - rmsQ += q * q; - } - rmsQ = (uint64_t)(sqrt((double)rmsQ / n) + .499); - cns = b<<28 | 0xf<<24 | rmsQ<<16 | 60<<8; - } else cns = bam_maqcns_call(n, pu, d->c); - } - if ((d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY)) && d->ref && pos < d->len) { // call indels - int m = (!d->max_depth || d->max_depth>n) ? n : d->max_depth; - if (proposed_indels) // the first element gives the size of the array - r = bam_maqindel(m, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1); - else r = bam_maqindel(m, pos, d->ido, pu, d->ref, 0, 0); - } - // when only variant sites are asked for, test if the site is a variant - if ((d->format & BAM_PLF_CNS) && (d->format & BAM_PLF_VAR_ONLY)) { - if (!(bam_nt16_table[rb] != 15 && cns>>28 != bam_nt16_table[rb])) { // not a SNP - if (!(r && (r->gt == 2 || strcmp(r->s[r->gt], "*")))) { // not an indel - if (r) bam_maqindel_ret_destroy(r); - return 0; - } - } - } - // print the first 3 columns - printf("%s\t%d\t%c\t", d->h->target_name[tid], pos + 1, rb); - // print consensus information if required - if (d->format & BAM_PLF_CNS) { - int ref_q, rb4 = bam_nt16_table[rb]; - ref_q = 0; - if (rb4 != 15 && cns>>28 != 15 && cns>>28 != rb4) { // a SNP - ref_q = ((cns>>24&0xf) == rb4)? cns>>8&0xff : (cns>>8&0xff) + (cns&0xff); - if (ref_q > 255) ref_q = 255; - } - rms_mapq = cns>>16&0xff; - printf("%c\t%d\t%d\t%d\t", bam_nt16_rev_table[cns>>28], cns>>8&0xff, ref_q, rms_mapq); - } - // print pileup sequences - printf("%d\t", n); - rms_aux = 0; // we need to recalculate rms_mapq when -c is not flagged on the command line - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pu + i; - int tmp = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ; - rms_aux += tmp * tmp; - pileup_seq(p, pos, d->len, d->ref); - } - // finalize rms_mapq - rms_aux = (uint64_t)(sqrt((double)rms_aux / n) + .499); - if (rms_mapq < 0) rms_mapq = rms_aux; - putchar('\t'); - // print quality - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pu + i; - int c = bam1_qual(p->b)[p->qpos] + 33; - if (c > 126) c = 126; - putchar(c); - } - if (d->format & BAM_PLF_2ND) { // print 2nd calls and qualities - const unsigned char *q; - putchar('\t'); - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pu + i; - q = bam_aux_get(p->b, "E2"); - putchar(q? q[p->qpos + 1] : 'N'); - } - putchar('\t'); - for (i = 0; i < n; ++i) { - const bam_pileup1_t *p = pu + i; - q = bam_aux_get(p->b, "U2"); - putchar(q? q[p->qpos + 1] : '!'); - } - } - // print mapping quality if -s is flagged on the command line - if (d->format & BAM_PLF_SIMPLE) { - putchar('\t'); - for (i = 0; i < n; ++i) { - int c = pu[i].b->core.qual + 33; - if (c > 126) c = 126; - putchar(c); - } - } - // print read position - if (d->format & BAM_PLF_READPOS) { - putchar('\t'); - for (i = 0; i < n; ++i) { - int x = pu[i].qpos; - int l = pu[i].b->core.l_qseq; - printf("%d,", x < l/2? x+1 : -((l-1)-x+1)); - } - } - putchar('\n'); - // print the indel line if r has been calculated. This only happens if: - // a) -c or -i are flagged, AND b) the reference sequence is available - if (r) { - printf("%s\t%d\t*\t", d->h->target_name[tid], pos + 1); - if (r->gt < 2) printf("%s/%s\t", r->s[r->gt], r->s[r->gt]); - else printf("%s/%s\t", r->s[0], r->s[1]); - printf("%d\t%d\t", r->q_cns, r->q_ref); - printf("%d\t%d\t", rms_mapq, n); - printf("%s\t%s\t", r->s[0], r->s[1]); - //printf("%d\t%d\t", r->gl[0], r->gl[1]); - printf("%d\t%d\t%d\t", r->cnt1, r->cnt2, r->cnt_anti); - printf("%d\t%d\n", r->cnt_ref, r->cnt_ambi); - bam_maqindel_ret_destroy(r); - } - return 0; -} - -int bam_pileup(int argc, char *argv[]) -{ - int c, is_SAM = 0; - char *fn_list = 0, *fn_fa = 0, *fn_pos = 0; - pu_data_t *d = (pu_data_t*)calloc(1, sizeof(pu_data_t)); - d->max_depth = 0; - d->tid = -1; d->mask = BAM_DEF_MASK; - d->c = bam_maqcns_init(); - d->c->is_soap = 1; // change the default model - d->ido = bam_maqindel_opt_init(); - while ((c = getopt(argc, argv, "st:f:cT:N:r:l:d:im:gI:G:vM:S2aR:PA")) >= 0) { - switch (c) { - case 'a': d->c->is_soap = 1; break; - case 'A': d->c->is_soap = 0; break; - case 's': d->format |= BAM_PLF_SIMPLE; break; - case 't': fn_list = strdup(optarg); break; - case 'l': fn_pos = strdup(optarg); break; - case 'f': fn_fa = strdup(optarg); break; - case 'T': d->c->theta = atof(optarg); break; - case 'N': d->c->n_hap = atoi(optarg); break; - case 'r': d->c->het_rate = atof(optarg); d->ido->r_snp = d->c->het_rate; break; - case 'M': d->c->cap_mapQ = atoi(optarg); break; - case 'd': d->max_depth = atoi(optarg); break; - case 'c': d->format |= BAM_PLF_CNS; break; - case 'i': d->format |= BAM_PLF_INDEL_ONLY; break; - case 'v': d->format |= BAM_PLF_VAR_ONLY; break; - case 'm': d->mask = strtol(optarg, 0, 0); break; - case 'g': d->format |= BAM_PLF_GLF; break; - case '2': d->format |= BAM_PLF_2ND; break; - case 'P': d->format |= BAM_PLF_READPOS; break; - case 'I': d->ido->q_indel = atoi(optarg); break; - case 'G': d->ido->r_indel = atof(optarg); break; - case 'S': is_SAM = 1; break; - case 'R': - if (strcmp(optarg, "random") == 0) d->format |= BAM_PLF_RANBASE; - else if (strcmp(optarg, "first") == 0) d->format |= BAM_PLF_1STBASE; - else if (strcmp(optarg, "all") == 0) d->format |= BAM_PLF_ALLBASE; - else fprintf(stderr, "[bam_pileup] unrecognized -R\n"); - break; - default: fprintf(stderr, "Unrecognizd option '-%c'.\n", c); return 1; - } - } - if (fn_list) is_SAM = 1; - if (optind == argc) { - fprintf(stderr, "\n"); - fprintf(stderr, "Usage: samtools pileup [options] |\n\n"); - fprintf(stderr, "Option: -s simple (yet incomplete) pileup format\n"); - fprintf(stderr, " -S the input is in SAM\n"); - fprintf(stderr, " -A use the MAQ model for SNP calling\n"); - fprintf(stderr, " -2 output the 2nd best call and quality\n"); - fprintf(stderr, " -i only show lines/consensus with indels\n"); - fprintf(stderr, " -m INT filtering reads with bits in INT [%d]\n", d->mask); - fprintf(stderr, " -M INT cap mapping quality at INT [%d]\n", d->c->cap_mapQ); - fprintf(stderr, " -d INT limit maximum depth for indels [unlimited]\n"); - fprintf(stderr, " -t FILE list of reference sequences (force -S)\n"); - fprintf(stderr, " -l FILE list of sites at which pileup is output\n"); - fprintf(stderr, " -f FILE reference sequence in the FASTA format\n\n"); - fprintf(stderr, " -c output the SOAPsnp consensus sequence\n"); - fprintf(stderr, " -v print variants only (for -c)\n"); - fprintf(stderr, " -g output in the GLFv3 format (suppressing -c/-i/-s)\n"); - fprintf(stderr, " -T FLOAT theta in maq consensus calling model (for -c/-g) [%f]\n", d->c->theta); - fprintf(stderr, " -N INT number of haplotypes in the sample (for -c/-g) [%d]\n", d->c->n_hap); - fprintf(stderr, " -r FLOAT prior of a difference between two haplotypes (for -c/-g) [%f]\n", d->c->het_rate); - fprintf(stderr, " -G FLOAT prior of an indel between two haplotypes (for -c/-g) [%f]\n", d->ido->r_indel); - fprintf(stderr, " -I INT phred prob. of an indel in sequencing/prep. (for -c/-g) [%d]\n", d->ido->q_indel); - fprintf(stderr, "\n"); - free(fn_list); free(fn_fa); free(d); - return 1; - } - if (d->format & (BAM_PLF_RANBASE|BAM_PLF_1STBASE|BAM_PLF_ALLBASE)) d->format |= BAM_PLF_CNS; - if (fn_fa) d->fai = fai_load(fn_fa); - if (d->format & (BAM_PLF_CNS|BAM_PLF_GLF)) bam_maqcns_prepare(d->c); // consensus calling - if (d->format & BAM_PLF_GLF) { // for glf output - glf3_header_t *h; - h = glf3_header_init(); - d->fp_glf = bgzf_fdopen(fileno(stdout), "w"); - glf3_header_write(d->fp_glf, h); - glf3_header_destroy(h); - } - if (d->fai == 0 && (d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY))) - fprintf(stderr, "[bam_pileup] indels will not be called when -f is absent.\n"); - if (fn_fa && is_SAM && fn_list == 0) fn_list = samfaipath(fn_fa); - - { - samfile_t *fp; - fp = is_SAM? samopen(argv[optind], "r", fn_list) : samopen(argv[optind], "rb", 0); - if (fp == 0 || fp->header == 0) { - fprintf(stderr, "[bam_pileup] fail to read the header: non-exisiting file or wrong format.\n"); - return 1; - } - d->h = fp->header; - if (fn_pos) d->hash = load_pos(fn_pos, d->h); - sampileup(fp, d->mask, pileup_func, d); - samclose(fp); // d->h will be destroyed here - } - - // free - if (d->format & BAM_PLF_GLF) bgzf_close(d->fp_glf); - if (fn_pos) { // free the hash table - khint_t k; - for (k = kh_begin(d->hash); k < kh_end(d->hash); ++k) - if (kh_exist(d->hash, k)) free(kh_val(d->hash, k)); - kh_destroy(64, d->hash); - } - free(fn_pos); free(fn_list); free(fn_fa); - if (d->fai) fai_destroy(d->fai); - bam_maqcns_destroy(d->c); - free(d->ido); free(d->ref); free(d); - return 0; -} - -/*********** - * mpileup * - ***********/ - -typedef struct { - char *reg; - faidx_t *fai; -} mplp_conf_t; - -typedef struct { - bamFile fp; - bam_iter_t iter; -} mplp_aux_t; - -static int mplp_func(void *data, bam1_t *b) -{ - mplp_aux_t *ma = (mplp_aux_t*)data; - if (ma->iter) return bam_iter_read(ma->fp, ma->iter, b); - return bam_read1(ma->fp, b); -} - -static int mpileup(mplp_conf_t *conf, int n, char **fn) -{ - mplp_aux_t **data; - int i, tid, pos, *n_plp, beg0 = 0, end0 = 1u<<29, ref_len, ref_tid; - const bam_pileup1_t **plp; - bam_mplp_t iter; - bam_header_t *h = 0; - char *ref; - // allocate - data = calloc(n, sizeof(void*)); - plp = calloc(n, sizeof(void*)); - n_plp = calloc(n, sizeof(int*)); - // 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 = bam_open(fn[i], "r"); - h_tmp = bam_header_read(data[i]->fp); - if (conf->reg) { - int beg, end; - bam_index_t *idx; - idx = bam_index_load(fn[i]); - if (idx == 0) { - fprintf(stderr, "[%s] fail to load index for %d-th input.\n", __func__, i+1); - exit(1); - } - if (bam_parse_region(h_tmp, conf->reg, &tid, &beg, &end) < 0) { - fprintf(stderr, "[%s] malformatted region or wrong seqname for %d-th input.\n", __func__, i+1); - exit(1); - } - if (i == 0) 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); - } - } - // mpileup - ref_tid = -1; ref = 0; - iter = bam_mplp_init(n, mplp_func, (void**)data); - 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 (tid != ref_tid) { - free(ref); - if (conf->fai) ref = fai_fetch(conf->fai, h->target_name[tid], &ref_len); - ref_tid = tid; - } - 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; - printf("\t%d\t", n_plp[i]); - if (n_plp[i] == 0) printf("*\t*"); - else { - for (j = 0; j < n_plp[i]; ++j) - 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] + 33; - if (c > 126) c = 126; - putchar(c); - } - } - } - putchar('\n'); - } - 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; -} - -int bam_mpileup(int argc, char *argv[]) -{ - int c; - mplp_conf_t mplp; - memset(&mplp, 0, sizeof(mplp_conf_t)); - while ((c = getopt(argc, argv, "f:r:")) >= 0) { - switch (c) { - case 'f': - mplp.fai = fai_load(optarg); - if (mplp.fai == 0) return 1; - break; - case 'r': mplp.reg = strdup(optarg); - } - } - if (argc == 1) { - fprintf(stderr, "Usage: samtools mpileup [-r reg] [-f in.fa] in1.bam [in2.bam [...]]\n"); - return 1; - } - mpileup(&mplp, argc - optind, argv + optind); - free(mplp.reg); - if (mplp.fai) fai_destroy(mplp.fai); - return 0; -} diff --git a/samtools/bam_plcmd.c.pysam.c b/samtools/bam_plcmd.c.pysam.c new file mode 100644 index 0000000..1137b8d --- /dev/null +++ b/samtools/bam_plcmd.c.pysam.c @@ -0,0 +1,931 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#include +#include "sam.h" +#include "faidx.h" +#include "bam_maqcns.h" +#include "khash.h" +#include "glf.h" +#include "kstring.h" + +typedef int *indel_list_t; +KHASH_MAP_INIT_INT64(64, indel_list_t) + +#define BAM_PLF_SIMPLE 0x01 +#define BAM_PLF_CNS 0x02 +#define BAM_PLF_INDEL_ONLY 0x04 +#define BAM_PLF_GLF 0x08 +#define BAM_PLF_VAR_ONLY 0x10 +#define BAM_PLF_2ND 0x20 +#define BAM_PLF_RANBASE 0x40 +#define BAM_PLF_1STBASE 0x80 +#define BAM_PLF_ALLBASE 0x100 +#define BAM_PLF_READPOS 0x200 +#define BAM_PLF_NOBAQ 0x400 + +typedef struct { + bam_header_t *h; + bam_maqcns_t *c; + bam_maqindel_opt_t *ido; + faidx_t *fai; + khash_t(64) *hash; + uint32_t format; + int tid, len, last_pos; + int mask; + int capQ_thres, min_baseQ; + int max_depth; // for indel calling, ignore reads with the depth too high. 0 for unlimited + char *ref; + glfFile fp_glf; // for glf output only +} pu_data_t; + +char **__bam_get_lines(const char *fn, int *_n); +void bam_init_header_hash(bam_header_t *header); +int32_t bam_get_tid(const bam_header_t *header, const char *seq_name); + +static khash_t(64) *load_pos(const char *fn, bam_header_t *h) +{ + char **list; + int i, j, n, *fields, max_fields; + khash_t(64) *hash; + bam_init_header_hash(h); + list = __bam_get_lines(fn, &n); + hash = kh_init(64); + max_fields = 0; fields = 0; + for (i = 0; i < n; ++i) { + char *str = list[i]; + int chr, n_fields, ret; + khint_t k; + uint64_t x; + n_fields = ksplit_core(str, 0, &max_fields, &fields); + if (n_fields < 2) continue; + chr = bam_get_tid(h, str + fields[0]); + if (chr < 0) { + fprintf(pysamerr, "[load_pos] unknown reference sequence name: %s\n", str + fields[0]); + continue; + } + x = (uint64_t)chr << 32 | (atoi(str + fields[1]) - 1); + k = kh_put(64, hash, x, &ret); + if (ret == 0) { + fprintf(pysamerr, "[load_pos] position %s:%s has been loaded.\n", str+fields[0], str+fields[1]); + continue; + } + kh_val(hash, k) = 0; + if (n_fields > 2) { + // count + for (j = 2; j < n_fields; ++j) { + char *s = str + fields[j]; + if ((*s != '+' && *s != '-') || !isdigit(s[1])) break; + } + if (j > 2) { // update kh_val() + int *q, y, z; + q = kh_val(hash, k) = (int*)calloc(j - 1, sizeof(int)); + q[0] = j - 2; z = j; y = 1; + for (j = 2; j < z; ++j) + q[y++] = atoi(str + fields[j]); + } + } + free(str); + } + free(list); free(fields); + return hash; +} + +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; +} + +// an analogy to pileup_func() below +static int glt3_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data) +{ + pu_data_t *d = (pu_data_t*)data; + bam_maqindel_ret_t *r = 0; + int rb, *proposed_indels = 0; + glf1_t *g; + glf3_t *g3; + + if (d->fai == 0) { + fprintf(pysamerr, "[glt3_func] reference sequence is required for generating GLT. Abort!\n"); + exit(1); + } + if (d->hash) { // only output a list of sites + khint_t k = kh_get(64, d->hash, (uint64_t)tid<<32|pos); + if (k == kh_end(d->hash)) return 0; + proposed_indels = kh_val(d->hash, k); + } + g3 = glf3_init1(); + if (d->fai && (int)tid != d->tid) { + if (d->ref) { // then write the end mark + g3->rtype = GLF3_RTYPE_END; + glf3_write1(d->fp_glf, g3); + } + glf3_ref_write(d->fp_glf, d->h->target_name[tid], d->h->target_len[tid]); // write reference + free(d->ref); + d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len); + d->tid = tid; + d->last_pos = 0; + } + rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N'; + g = bam_maqcns_glfgen(n, pu, bam_nt16_table[rb], d->c); + memcpy(g3, g, sizeof(glf1_t)); + g3->rtype = GLF3_RTYPE_SUB; + g3->offset = pos - d->last_pos; + d->last_pos = pos; + glf3_write1(d->fp_glf, g3); + if (pos < d->len) { + int m = (!d->max_depth || d->max_depth>n) ? n : d->max_depth; + if (proposed_indels) + r = bam_maqindel(m, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1); + else r = bam_maqindel(m, pos, d->ido, pu, d->ref, 0, 0); + } + if (r) { // then write indel line + int het = 3 * n, min; + min = het; + if (min > r->gl[0]) min = r->gl[0]; + if (min > r->gl[1]) min = r->gl[1]; + g3->ref_base = 0; + g3->rtype = GLF3_RTYPE_INDEL; + memset(g3->lk, 0, 10); + g3->lk[0] = r->gl[0] - min < 255? r->gl[0] - min : 255; + g3->lk[1] = r->gl[1] - min < 255? r->gl[1] - min : 255; + g3->lk[2] = het - min < 255? het - min : 255; + g3->offset = 0; + g3->indel_len[0] = r->indel1; + g3->indel_len[1] = r->indel2; + g3->min_lk = min < 255? min : 255; + g3->max_len = (abs(r->indel1) > abs(r->indel2)? abs(r->indel1) : abs(r->indel2)) + 1; + g3->indel_seq[0] = strdup(r->s[0]+1); + g3->indel_seq[1] = strdup(r->s[1]+1); + glf3_write1(d->fp_glf, g3); + bam_maqindel_ret_destroy(r); + } + free(g); + glf3_destroy1(g3); + return 0; +} + +static inline void pileup_seq(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('$'); +} + +static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data) +{ + pu_data_t *d = (pu_data_t*)data; + bam_maqindel_ret_t *r = 0; + int i, rb, rms_mapq = -1, *proposed_indels = 0; + uint64_t rms_aux; + uint32_t cns = 0; + + // if GLF is required, suppress -c completely + if (d->format & BAM_PLF_GLF) return glt3_func(tid, pos, n, pu, data); + // if d->hash is initialized, only output the sites in the hash table + if (d->hash) { + khint_t k = kh_get(64, d->hash, (uint64_t)tid<<32|pos); + if (k == kh_end(d->hash)) return 0; + proposed_indels = kh_val(d->hash, k); + } + // update d->ref if necessary + if (d->fai && (int)tid != d->tid) { + free(d->ref); + d->ref = faidx_fetch_seq(d->fai, d->h->target_name[tid], 0, 0x7fffffff, &d->len); + d->tid = tid; + } + rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N'; + // when the indel-only mode is asked for, return if no reads mapped with indels + if (d->format & BAM_PLF_INDEL_ONLY) { + for (i = 0; i < n; ++i) + if (pu[i].indel != 0) break; + if (i == n) return 0; + } + // call the consensus and indel + if (d->format & BAM_PLF_CNS) { // call consensus + if (d->format & (BAM_PLF_RANBASE|BAM_PLF_1STBASE)) { // use a random base or the 1st base as the consensus call + const bam_pileup1_t *p = (d->format & BAM_PLF_1STBASE)? pu : pu + (int)(drand48() * n); + int q = bam1_qual(p->b)[p->qpos]; + int mapQ = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ; + uint32_t b = bam1_seqi(bam1_seq(p->b), p->qpos); + cns = b<<28 | 0xf<<24 | mapQ<<16 | q<<8; + } else if (d->format & BAM_PLF_ALLBASE) { // collapse all bases + uint64_t rmsQ = 0; + uint32_t b = 0; + for (i = 0; i < n; ++i) { + const bam_pileup1_t *p = pu + i; + int q = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ; + b |= bam1_seqi(bam1_seq(p->b), p->qpos); + rmsQ += q * q; + } + rmsQ = (uint64_t)(sqrt((double)rmsQ / n) + .499); + cns = b<<28 | 0xf<<24 | rmsQ<<16 | 60<<8; + } else { + glf1_t *g = bam_maqcns_glfgen(n, pu, bam_nt16_table[rb], d->c); + cns = g->depth == 0? (0xfu<<28 | 0xf<<24) : glf2cns(g, (int)(d->c->q_r + .499)); + free(g); + } + } + if ((d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY)) && d->ref && pos < d->len) { // call indels + int m = (!d->max_depth || d->max_depth>n) ? n : d->max_depth; + if (proposed_indels) // the first element gives the size of the array + r = bam_maqindel(m, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1); + else r = bam_maqindel(m, pos, d->ido, pu, d->ref, 0, 0); + + } + // when only variant sites are asked for, test if the site is a variant + if ((d->format & BAM_PLF_CNS) && (d->format & BAM_PLF_VAR_ONLY)) { + if (!(bam_nt16_table[rb] != 15 && cns>>28 != 15 && cns>>28 != bam_nt16_table[rb])) { // not a SNP + if (!(r && (r->gt == 2 || strcmp(r->s[r->gt], "*")))) { // not an indel + if (r) bam_maqindel_ret_destroy(r); + return 0; + } + } + } + // print the first 3 columns + fputs(d->h->target_name[tid], stdout); putchar('\t'); + printw(pos+1, stdout); putchar('\t'); putchar(rb); putchar('\t'); + // print consensus information if required + if (d->format & BAM_PLF_CNS) { + putchar(bam_nt16_rev_table[cns>>28]); putchar('\t'); + printw(cns>>8&0xff, stdout); putchar('\t'); + printw(cns&0xff, stdout); putchar('\t'); + printw(cns>>16&0xff, stdout); putchar('\t'); + } + // print pileup sequences + printw(n, stdout); putchar('\t'); + for (i = 0; i < n; ++i) + pileup_seq(pu + i, pos, d->len, d->ref); + // finalize rms_mapq + if (d->format & BAM_PLF_CNS) { + for (i = rms_aux = 0; i < n; ++i) { + const bam_pileup1_t *p = pu + i; + int tmp = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ; + rms_aux += tmp * tmp; + } + rms_aux = (uint64_t)(sqrt((double)rms_aux / n) + .499); + if (rms_mapq < 0) rms_mapq = rms_aux; + } + putchar('\t'); + // print quality + for (i = 0; i < n; ++i) { + const bam_pileup1_t *p = pu + i; + int c = bam1_qual(p->b)[p->qpos] + 33; + if (c > 126) c = 126; + putchar(c); + } + if (d->format & BAM_PLF_2ND) { // print 2nd calls and qualities + const unsigned char *q; + putchar('\t'); + for (i = 0; i < n; ++i) { + const bam_pileup1_t *p = pu + i; + q = bam_aux_get(p->b, "E2"); + putchar(q? q[p->qpos + 1] : 'N'); + } + putchar('\t'); + for (i = 0; i < n; ++i) { + const bam_pileup1_t *p = pu + i; + q = bam_aux_get(p->b, "U2"); + putchar(q? q[p->qpos + 1] : '!'); + } + } + // print mapping quality if -s is flagged on the command line + if (d->format & BAM_PLF_SIMPLE) { + putchar('\t'); + for (i = 0; i < n; ++i) { + int c = pu[i].b->core.qual + 33; + if (c > 126) c = 126; + putchar(c); + } + } + // print read position + if (d->format & BAM_PLF_READPOS) { + putchar('\t'); + for (i = 0; i < n; ++i) { + int x = pu[i].qpos; + int l = pu[i].b->core.l_qseq; + printw(x < l/2? x+1 : -((l-1)-x+1), stdout); putchar(','); + } + } + putchar('\n'); + // print the indel line if r has been calculated. This only happens if: + // a) -c or -i are flagged, AND b) the reference sequence is available + if (r) { + printf("%s\t%d\t*\t", d->h->target_name[tid], pos + 1); + if (r->gt < 2) printf("%s/%s\t", r->s[r->gt], r->s[r->gt]); + else printf("%s/%s\t", r->s[0], r->s[1]); + printf("%d\t%d\t", r->q_cns, r->q_ref); + printf("%d\t%d\t", rms_mapq, n); + printf("%s\t%s\t", r->s[0], r->s[1]); + //printf("%d\t%d\t", r->gl[0], r->gl[1]); + printf("%d\t%d\t%d\t", r->cnt1, r->cnt2, r->cnt_anti); + printf("%d\t%d\n", r->cnt_ref, r->cnt_ambi); + bam_maqindel_ret_destroy(r); + } + return 0; +} + +int bam_pileup(int argc, char *argv[]) +{ + int c, is_SAM = 0; + char *fn_list = 0, *fn_fa = 0, *fn_pos = 0; + pu_data_t *d = (pu_data_t*)calloc(1, sizeof(pu_data_t)); + d->max_depth = 1024; d->tid = -1; d->mask = BAM_DEF_MASK; d->min_baseQ = 13; + d->c = bam_maqcns_init(); + d->c->errmod = BAM_ERRMOD_MAQ2; // change the default model + d->ido = bam_maqindel_opt_init(); + while ((c = getopt(argc, argv, "st:f:cT:N:r:l:d:im:gI:G:vM:S2aR:PAQ:C:B")) >= 0) { + switch (c) { + case 'Q': d->c->min_baseQ = atoi(optarg); break; + case 'C': d->capQ_thres = atoi(optarg); break; + case 'B': d->format |= BAM_PLF_NOBAQ; break; + case 'a': d->c->errmod = BAM_ERRMOD_SOAP; break; + case 'A': d->c->errmod = BAM_ERRMOD_MAQ; break; + case 's': d->format |= BAM_PLF_SIMPLE; break; + case 't': fn_list = strdup(optarg); break; + case 'l': fn_pos = strdup(optarg); break; + case 'f': fn_fa = strdup(optarg); break; + case 'T': d->c->theta = atof(optarg); break; + case 'N': d->c->n_hap = atoi(optarg); break; + case 'r': d->c->het_rate = atof(optarg); d->ido->r_snp = d->c->het_rate; break; + case 'M': d->c->cap_mapQ = atoi(optarg); break; + case 'd': d->max_depth = atoi(optarg); break; + case 'c': d->format |= BAM_PLF_CNS; break; + case 'i': d->format |= BAM_PLF_INDEL_ONLY; break; + case 'v': d->format |= BAM_PLF_VAR_ONLY; break; + case 'm': d->mask = strtol(optarg, 0, 0); break; + case 'g': d->format |= BAM_PLF_GLF; break; + case '2': d->format |= BAM_PLF_2ND; break; + case 'P': d->format |= BAM_PLF_READPOS; break; + case 'I': d->ido->q_indel = atoi(optarg); break; + case 'G': d->ido->r_indel = atof(optarg); break; + case 'S': is_SAM = 1; break; + case 'R': + if (strcmp(optarg, "random") == 0) d->format |= BAM_PLF_RANBASE; + else if (strcmp(optarg, "first") == 0) d->format |= BAM_PLF_1STBASE; + else if (strcmp(optarg, "all") == 0) d->format |= BAM_PLF_ALLBASE; + else fprintf(pysamerr, "[bam_pileup] unrecognized -R\n"); + break; + default: fprintf(pysamerr, "Unrecognizd option '-%c'.\n", c); return 1; + } + } + if (d->c->errmod != BAM_ERRMOD_MAQ2) d->c->theta += 0.02; + if (d->c->theta > 1.0) d->c->theta = 1.0; + if (fn_list) is_SAM = 1; + if (optind == argc) { + fprintf(pysamerr, "\n"); + fprintf(pysamerr, "Usage: samtools pileup [options] |\n\n"); + fprintf(pysamerr, "Option: -s simple (yet incomplete) pileup format\n"); + fprintf(pysamerr, " -S the input is in SAM\n"); + fprintf(pysamerr, " -B disable BAQ computation\n"); + fprintf(pysamerr, " -A use the original MAQ model for SNP calling (DEPRECATED)\n"); + fprintf(pysamerr, " -2 output the 2nd best call and quality\n"); + fprintf(pysamerr, " -i only show lines/consensus with indels\n"); + fprintf(pysamerr, " -Q INT min base quality (possibly capped by BAQ) [%d]\n", d->c->min_baseQ); + fprintf(pysamerr, " -C INT coefficient for adjusting mapQ of poor mappings [%d]\n", d->capQ_thres); + fprintf(pysamerr, " -m INT filtering reads with bits in INT [0x%x]\n", d->mask); + fprintf(pysamerr, " -M INT cap mapping quality at INT [%d]\n", d->c->cap_mapQ); + fprintf(pysamerr, " -d INT limit maximum depth for indels [%d]\n", d->max_depth); + fprintf(pysamerr, " -t FILE list of reference sequences (force -S)\n"); + fprintf(pysamerr, " -l FILE list of sites at which pileup is output\n"); + fprintf(pysamerr, " -f FILE reference sequence in the FASTA format\n\n"); + fprintf(pysamerr, " -c compute the consensus sequence\n"); + fprintf(pysamerr, " -v print variants only (for -c)\n"); + fprintf(pysamerr, " -g output in the GLFv3 format (DEPRECATED)\n"); + fprintf(pysamerr, " -T FLOAT theta in maq consensus calling model (for -c) [%.4g]\n", d->c->theta); + fprintf(pysamerr, " -N INT number of haplotypes in the sample (for -c) [%d]\n", d->c->n_hap); + fprintf(pysamerr, " -r FLOAT prior of a difference between two haplotypes (for -c) [%.4g]\n", d->c->het_rate); + fprintf(pysamerr, " -G FLOAT prior of an indel between two haplotypes (for -c) [%.4g]\n", d->ido->r_indel); + fprintf(pysamerr, " -I INT phred prob. of an indel in sequencing/prep. (for -c) [%d]\n", d->ido->q_indel); + fprintf(pysamerr, "\n"); + free(fn_list); free(fn_fa); free(d); + return 1; + } + if (d->format & (BAM_PLF_RANBASE|BAM_PLF_1STBASE|BAM_PLF_ALLBASE)) d->format |= BAM_PLF_CNS; + if (fn_fa) d->fai = fai_load(fn_fa); + if (d->format & (BAM_PLF_CNS|BAM_PLF_GLF)) bam_maqcns_prepare(d->c); // consensus calling + if (d->format & BAM_PLF_GLF) { // for glf output + glf3_header_t *h; + h = glf3_header_init(); + d->fp_glf = bgzf_fdopen(fileno(stdout), "w"); + glf3_header_write(d->fp_glf, h); + glf3_header_destroy(h); + } + if (d->fai == 0 && (d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY))) + fprintf(pysamerr, "[bam_pileup] indels will not be called when -f is absent.\n"); + if (fn_fa && is_SAM && fn_list == 0) fn_list = samfaipath(fn_fa); + + { + samfile_t *fp; + fp = is_SAM? samopen(argv[optind], "r", fn_list) : samopen(argv[optind], "rb", 0); + if (fp == 0 || fp->header == 0) { + fprintf(pysamerr, "[bam_pileup] fail to read the header: non-exisiting file or wrong format.\n"); + return 1; + } + d->h = fp->header; + if (fn_pos) d->hash = load_pos(fn_pos, d->h); + { // run pileup + extern int bam_prob_realn(bam1_t *b, const char *ref); + extern int bam_cap_mapQ(bam1_t *b, char *ref, int thres); + bam1_t *b; + int ret, tid, pos, n_plp; + bam_plp_t iter; + const bam_pileup1_t *plp; + b = bam_init1(); + iter = bam_plp_init(0, 0); + bam_plp_set_mask(iter, d->mask); + while ((ret = samread(fp, b)) >= 0) { + int skip = 0; + if ((int)b->core.tid < 0) break; + // update d->ref if necessary + if (d->fai && (int)b->core.tid != d->tid) { + free(d->ref); + d->ref = faidx_fetch_seq(d->fai, d->h->target_name[b->core.tid], 0, 0x7fffffff, &d->len); + d->tid = b->core.tid; + } + if (d->ref && (d->format&BAM_PLF_CNS) && !(d->format&BAM_PLF_NOBAQ)) bam_prob_realn(b, d->ref); + if (d->ref && (d->format&BAM_PLF_CNS) && d->capQ_thres > 10) { + int q = bam_cap_mapQ(b, d->ref, d->capQ_thres); + if (q < 0) skip = 1; + else if (b->core.qual > q) b->core.qual = q; + } else if (b->core.flag&BAM_FUNMAP) skip = 1; + else if ((d->format&BAM_PLF_CNS) && (b->core.flag&1) && !(b->core.flag&2)) skip = 1; + if (skip) continue; + bam_plp_push(iter, b); + while ((plp = bam_plp_next(iter, &tid, &pos, &n_plp)) != 0) + pileup_func(tid, pos, n_plp, plp, d); + } + bam_plp_push(iter, 0); + while ((plp = bam_plp_next(iter, &tid, &pos, &n_plp)) != 0) + pileup_func(tid, pos, n_plp, plp, d); + bam_plp_destroy(iter); + bam_destroy1(b); + } + samclose(fp); // d->h will be destroyed here + } + + // free + if (d->format & BAM_PLF_GLF) bgzf_close(d->fp_glf); + if (fn_pos) { // free the hash table + khint_t k; + for (k = kh_begin(d->hash); k < kh_end(d->hash); ++k) + if (kh_exist(d->hash, k)) free(kh_val(d->hash, k)); + kh_destroy(64, d->hash); + } + free(fn_pos); free(fn_list); free(fn_fa); + if (d->fai) fai_destroy(d->fai); + bam_maqcns_destroy(d->c); + free(d->ido); free(d->ref); free(d); + return 0; +} + +/*********** + * mpileup * + ***********/ + +#include +#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_FMT_DP 0x100 +#define MPLP_FMT_SP 0x200 +#define MPLP_NO_INDEL 0x400 + +typedef struct { + int max_mq, min_mq, flag, min_baseQ, capQ_thres, max_depth; + int openQ, extQ, tandemQ; + char *reg, *fn_pos, *pl_list; + faidx_t *fai; + kh_64_t *hash; +} mplp_conf_t; + +typedef struct { + bamFile fp; + bam_iter_t iter; + int min_mq, flag, ref_id, capQ_thres; + char *ref; +} mplp_aux_t; + +typedef struct { + 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; + has_ref = (ma->ref && ma->ref_id == b->core.tid)? 1 : 0; + skip = 0; + if (has_ref && (ma->flag&MPLP_REALN)) bam_prob_realn_core(b, ma->ref, 1); + if (has_ref && ma->capQ_thres > 10) { + int q = bam_cap_mapQ(b, ma->ref, ma->capQ_thres); + if (q < 0) skip = 1; + else if (b->core.qual > q) b->core.qual = q; + } else if (b->core.flag&BAM_FUNMAP) skip = 1; + else if (b->core.qual < ma->min_mq) skip = 1; + else if ((ma->flag&MPLP_NO_ORPHAN) && (b->core.flag&1) && !(b->core.flag&2)) 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 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 = 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); + assert(id >= 0 && id < m->n); + 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; + } + } +} + +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, beg0 = 0, end0 = 1u<<29, ref_len, ref_tid, max_depth; + const bam_pileup1_t **plp; + bam_mplp_t iter; + bam_header_t *h = 0; + char *ref; + khash_t(64) *hash = 0; + 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]->min_mq = conf->min_mq; + data[i]->flag = conf->flag; + data[i]->capQ_thres = conf->capQ_thres; + data[i]->fp = strcmp(fn[i], "-") == 0? bam_dopen(fileno(stdin), "r") : bam_open(fn[i], "r"); + h_tmp = bam_header_read(data[i]->fp); + bam_smpl_add(sm, fn[i], 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 %d-th input.\n", __func__, i+1); + exit(1); + } + if (bam_parse_region(h_tmp, conf->reg, &tid, &beg, &end) < 0) { + fprintf(pysamerr, "[%s] malformatted region or wrong seqname for %d-th input.\n", __func__, i+1); + exit(1); + } + if (i == 0) 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); + if (conf->fn_pos) hash = load_pos(conf->fn_pos, h); + // 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); + bh->l_txt = 0; + free(s.s); + 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; + } + 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-sample depth to %d\n", __func__, max_depth); + } + 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 (hash) { + khint_t k; + k = kh_get(64, hash, (uint64_t)tid<<32 | pos); + if (k == kh_end(hash)) continue; + } + if (tid != ref_tid) { + free(ref); ref = 0; + if (conf->fai) ref = fai_fetch(conf->fai, h->target_name[tid], &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 _ref0, ref16; + bcf1_t *b = calloc(1, sizeof(bcf1_t)); + group_smpl(&gplp, sm, &buf, n, fn, n_plp, plp); + _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, ref16, &bc); + bcf_call2bcf(tid, pos, &bc, b, (conf->flag&(MPLP_FMT_DP|MPLP_FMT_SP))? bcr : 0, + (conf->flag&MPLP_FMT_SP), 0, 0); + bcf_write(bp, bh, b); + bcf_destroy(b); + // call indels + if (!(conf->flag&MPLP_NO_INDEL) && 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, -1, &bc) >= 0) { + b = calloc(1, sizeof(bcf1_t)); + bcf_call2bcf(tid, pos, &bc, b, (conf->flag&(MPLP_FMT_DP|MPLP_FMT_SP))? bcr : 0, + (conf->flag&MPLP_FMT_SP), 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; + printf("\t%d\t", n_plp[i]); + if (n_plp[i] == 0) printf("*\t*"); + else { + for (j = 0; j < n_plp[i]; ++j) + 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] + 33; + if (c > 126) c = 126; + putchar(c); + } + } + } + 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); + if (hash) { // free the hash table + khint_t k; + for (k = kh_begin(hash); k < kh_end(hash); ++k) + if (kh_exist(hash, k)) free(kh_val(hash, k)); + kh_destroy(64, hash); + } + 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; +} + +#define MAX_PATH_LEN 1024 +int read_file_list(const char *file_list,int *n,char **argv[]) +{ + char buf[MAX_PATH_LEN]; + int len, nfiles; + char **files; + + FILE *fh = fopen(file_list,"r"); + if ( !fh ) + { + fprintf(pysamerr,"%s: %s\n", file_list,strerror(errno)); + return 1; + } + + // Speed is not an issue here, determine the number of files by reading the file twice + nfiles = 0; + while ( fgets(buf,MAX_PATH_LEN,fh) ) nfiles++; + + if ( fseek(fh, 0L, SEEK_SET) ) + { + fprintf(pysamerr,"%s: %s\n", file_list,strerror(errno)); + return 1; + } + + files = calloc(nfiles,sizeof(char*)); + nfiles = 0; + while ( fgets(buf,MAX_PATH_LEN,fh) ) + { + len = strlen(buf); + while ( len>0 && isspace(buf[len-1]) ) len--; + if ( !len ) continue; + + files[nfiles] = malloc(sizeof(char)*(len+1)); + strncpy(files[nfiles],buf,len); + files[nfiles][len] = 0; + nfiles++; + } + fclose(fh); + if ( !nfiles ) + { + fprintf(pysamerr,"No files read from %s\n", file_list); + return 1; + } + *argv = files; + *n = nfiles; + return 0; +} +#undef MAX_PATH_LEN + +int bam_mpileup(int argc, char *argv[]) +{ + int c; + const char *file_list = NULL; + char **fn = NULL; + int nfiles = 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.openQ = 40; mplp.extQ = 20; mplp.tandemQ = 100; + mplp.flag = MPLP_NO_ORPHAN | MPLP_REALN; + while ((c = getopt(argc, argv, "gf:r:l:M:q:Q:uaORC:BDSd:b:P:o:e:h:I")) >= 0) { + switch (c) { + case 'f': + mplp.fai = fai_load(optarg); + if (mplp.fai == 0) return 1; + break; + case 'd': mplp.max_depth = atoi(optarg); break; + case 'r': mplp.reg = strdup(optarg); break; + case 'l': mplp.fn_pos = strdup(optarg); break; + case 'P': mplp.pl_list = strdup(optarg); 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 & ~MPLP_NO_ORPHAN; break; + case 'O': mplp.flag |= MPLP_NO_ORPHAN; break; + case 'R': mplp.flag |= MPLP_REALN; break; + case 'D': mplp.flag |= MPLP_FMT_DP; break; + case 'S': mplp.flag |= MPLP_FMT_SP; break; + case 'I': mplp.flag |= MPLP_NO_INDEL; 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; + 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; + } + } + if (argc == 1) { + fprintf(pysamerr, "\n"); + fprintf(pysamerr, "Usage: samtools mpileup [options] in1.bam [in2.bam [...]]\n\n"); + fprintf(pysamerr, "Options: -f FILE reference sequence file [null]\n"); + fprintf(pysamerr, " -r STR region in which pileup is generated [null]\n"); + fprintf(pysamerr, " -l FILE list of positions (format: chr pos) [null]\n"); + fprintf(pysamerr, " -b FILE list of input BAM files [null]\n"); + fprintf(pysamerr, " -M INT cap mapping quality at INT [%d]\n", mplp.max_mq); + fprintf(pysamerr, " -Q INT min base quality [%d]\n", mplp.min_baseQ); + fprintf(pysamerr, " -q INT filter out alignment with MQ smaller than INT [%d]\n", mplp.min_mq); + fprintf(pysamerr, " -d INT max per-sample depth [%d]\n", mplp.max_depth); + fprintf(pysamerr, " -P STR comma separated list of platforms for indels [all]\n"); + fprintf(pysamerr, " -o INT Phred-scaled gap open sequencing error probability [%d]\n", mplp.openQ); + fprintf(pysamerr, " -e INT Phred-scaled gap extension seq error probability [%d]\n", mplp.extQ); + fprintf(pysamerr, " -h INT coefficient for homopolyer errors [%d]\n", mplp.tandemQ); + fprintf(pysamerr, " -g generate BCF output\n"); + fprintf(pysamerr, " -u do not compress BCF output\n"); + fprintf(pysamerr, " -B disable BAQ computation\n"); + fprintf(pysamerr, " -D output per-sample DP\n"); + fprintf(pysamerr, " -S output per-sample SP (strand bias P-value, slow)\n"); + fprintf(pysamerr, " -I do not perform indel calling\n"); + fprintf(pysamerr, "\n"); + fprintf(pysamerr, "Notes: Assuming diploid individuals.\n\n"); + return 1; + } + if ( file_list ) + { + if ( read_file_list(file_list,&nfiles,&fn) ) return 1; + mpileup(&mplp,nfiles,fn); + for (c=0; c -#include -#include "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->open_mode != 'r') 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->x.fpr, buf, BUF_SIZE)) > 0) -#else - while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0) -#endif - fwrite(buf, 1, len, fp->x.fpw); - 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(stderr, "Usage: samtools reheader \n"); - return 1; - } - { // read the header - tamFile fph = sam_open(argv[1]); - if (fph == 0) { - fprintf(stderr, "[%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(stderr, "[%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 b/samtools/bam_rmdup.c deleted file mode 100644 index f0d2b5d..0000000 --- a/samtools/bam_rmdup.c +++ /dev/null @@ -1,206 +0,0 @@ -#include -#include -#include -#include -#include -#include "sam.h" - -typedef bam1_t *bam1_p; - -#include "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; -} lib_aux_t; -KHASH_MAP_INIT_STR(lib, lib_aux_t) - -typedef struct { - 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; -} - -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; -} - -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); -} - -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); -} - -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); - } - } -} - -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; -} - -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(stderr, "[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(stderr, "[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(stderr, "[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(stderr, "[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(stderr, "\n"); - fprintf(stderr, "Usage: samtools rmdup [-sS] \n\n"); - fprintf(stderr, "Option: -s rmdup for SE reads\n"); - fprintf(stderr, " -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(stderr, "[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_rmdup.c.pysam.c b/samtools/bam_rmdup.c.pysam.c new file mode 100644 index 0000000..2d5a718 --- /dev/null +++ b/samtools/bam_rmdup.c.pysam.c @@ -0,0 +1,208 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#include "sam.h" + +typedef bam1_t *bam1_p; + +#include "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; +} lib_aux_t; +KHASH_MAP_INIT_STR(lib, lib_aux_t) + +typedef struct { + 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; +} + +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; +} + +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); +} + +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); +} + +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); + } + } +} + +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; +} + +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); +} + +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] \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 b/samtools/bam_rmdupse.c deleted file mode 100644 index e7dbdc7..0000000 --- a/samtools/bam_rmdupse.c +++ /dev/null @@ -1,159 +0,0 @@ -#include -#include "sam.h" -#include "khash.h" -#include "klist.h" - -#define QUEUE_CLEAR_SIZE 0x100000 -#define MAX_POS 0x7fffffff - -typedef struct { - 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) -typedef klist_t(q) queue_t; - -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; -} 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); -} - -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; -} - -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; -} - -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); -} - -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); - } - } - } -} - -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(stderr, "[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_rmdupse.c.pysam.c b/samtools/bam_rmdupse.c.pysam.c new file mode 100644 index 0000000..f2cd8b7 --- /dev/null +++ b/samtools/bam_rmdupse.c.pysam.c @@ -0,0 +1,161 @@ +#include "pysam.h" + +#include +#include "sam.h" +#include "khash.h" +#include "klist.h" + +#define QUEUE_CLEAR_SIZE 0x100000 +#define MAX_POS 0x7fffffff + +typedef struct { + 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) +typedef klist_t(q) queue_t; + +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; +} 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); +} + +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; +} + +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; +} + +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); +} + +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); + } + } + } +} + +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); +} diff --git a/samtools/bam_sort.c b/samtools/bam_sort.c deleted file mode 100644 index 12b1b54..0000000 --- a/samtools/bam_sort.c +++ /dev/null @@ -1,357 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "bam.h" -#include "ksort.h" - -static int g_is_by_qname = 0; - -static inline int strnum_cmp(const char *a, const char *b) -{ - char *pa, *pb; - pa = (char*)a; pb = (char*)b; - while (*pa && *pb) { - if (isdigit(*pa) && isdigit(*pb)) { - long ai, bi; - ai = strtol(pa, &pa, 10); - bi = strtol(pb, &pb, 10); - if (ai != bi) return aibi? 1 : 0; - } else { - if (*pa != *pb) break; - ++pa; ++pb; - } - } - if (*pa == *pb) - return (pa-a) < (pb-b)? -1 : (pa-a) > (pb-b)? 1 : 0; - return *pa<*pb? -1 : *pa>*pb? 1 : 0; -} - -#define HEAP_EMPTY 0xffffffffffffffffull - -typedef struct { - 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)))) - -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 && __pos_cmp(a, b))); - } else return __pos_cmp(a, b); -} - -KSORT_INIT(heap, heap1_t, heap_lt) - -static void swap_header_text(bam_header_t *h1, bam_header_t *h2) -{ - 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; -} - -/*! - @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 - - @discussion Padding information may NOT correctly maintained. This - function is NOT thread safe. - */ -void bam_merge_core(int by_qname, const char *out, const char *headers, int n, char * const *fn, int add_RG) -{ - 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; - - if (headers) { - tamFile fpheaders = sam_open(headers); - if (fpheaders == 0) { - fprintf(stderr, "[bam_merge_core] Cannot open file `%s'. Continue anyway.\n", headers); - } else { - 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)); - // prepare RG tag - if (add_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) { - heap1_t *h; - bam_header_t *hin; - fp[i] = bam_open(fn[i], "r"); - if (fp[i] == 0) { - int j; - fprintf(stderr, "[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; - } - hin = bam_header_read(fp[i]); - if (i == 0) { // the first SAM - hout = 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(stderr, "[bam_merge_core] number of @SQ headers in `%s' differs from number of target sequences", headers); - for (j = 0; j < hout->n_targets; ++j) - if (strcmp(hout->target_name[j], hheaders->target_name[j]) != 0) - fprintf(stderr, "[bam_merge_core] @SQ header '%s' in '%s' differs from target sequence", hheaders->target_name[j], headers); - } - swap_header_text(hout, hheaders); - bam_header_destroy(hheaders); - hheaders = NULL; - } - } else { // validate multiple baf - if (hout->n_targets != hin->n_targets) { - fprintf(stderr, "[bam_merge_core] file '%s' has different number of target sequences. Abort!\n", fn[i]); - exit(1); - } - for (j = 0; j < hout->n_targets; ++j) { - if (strcmp(hout->target_name[j], hin->target_name[j])) { - fprintf(stderr, "[bam_merge_core] different target sequence name: '%s' != '%s' in file '%s'. Abort!\n", - hout->target_name[j], hin->target_name[j], fn[i]); - exit(1); - } - } - bam_header_destroy(hin); - } - h = heap + i; - h->i = i; - h->b = (bam1_t*)calloc(1, sizeof(bam1_t)); - if (bam_read1(fp[i], h->b) >= 0) { - h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)h->b->core.pos<<1 | bam1_strand(h->b); - h->idx = idx++; - } - else h->pos = HEAP_EMPTY; - } - fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w"); - assert(fpout); - bam_header_write(fpout, hout); - bam_header_destroy(hout); - - ks_heapmake(heap, n, heap); - while (heap->pos != HEAP_EMPTY) { - bam1_t *b = heap->b; - if (add_RG && bam_aux_get(b, "RG") == 0) - 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_read1(fp[heap->i], b)) >= 0) { - heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)b->core.pos<<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(stderr, "[bam_merge_core] '%s' is truncated. Continue anyway.\n", fn[heap->i]); - ks_heapadjust(heap, 0, n, heap); - } - - if (add_RG) { - for (i = 0; i != n; ++i) free(RG[i]); - free(RG); free(RG_len); - } - for (i = 0; i != n; ++i) bam_close(fp[i]); - bam_close(fpout); - free(fp); free(heap); -} -int bam_merge(int argc, char *argv[]) -{ - int c, is_by_qname = 0, add_RG = 0; - char *fn_headers = NULL; - - while ((c = getopt(argc, argv, "h:nr")) >= 0) { - switch (c) { - case 'r': add_RG = 1; break; - case 'h': fn_headers = strdup(optarg); break; - case 'n': is_by_qname = 1; break; - } - } - if (optind + 2 >= argc) { - fprintf(stderr, "\n"); - fprintf(stderr, "Usage: samtools merge [-nr] [-h inh.sam] [...]\n\n"); - fprintf(stderr, "Options: -n sort by read names\n"); - fprintf(stderr, " -r attach RG tag (inferred from file names)\n"); - fprintf(stderr, " -h FILE copy the header in FILE to [in1.bam]\n\n"); - fprintf(stderr, "Note: Samtools' merge does not reconstruct the @RG dictionary in the header. Users\n"); - fprintf(stderr, " must provide the correct header with -h, or uses Picard which properly maintains\n"); - fprintf(stderr, " the header dictionary in merging.\n\n"); - return 1; - } - bam_merge_core(is_by_qname, argv[optind], fn_headers, argc - optind - 1, argv + optind + 1, add_RG); - free(fn_headers); - return 0; -} - -typedef bam1_t *bam1_p; - -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 && (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos)))); - } else return (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos)); -} -KSORT_INIT(sort, bam1_p, bam1_lt) - -static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam_header_t *h, int is_stdout) -{ - char *name; - int i; - bamFile fp; - ks_mergesort(sort, k, buf, 0); - name = (char*)calloc(strlen(prefix) + 20, 1); - if (n >= 0) sprintf(name, "%s.%.4d.bam", prefix, n); - else sprintf(name, "%s.bam", prefix); - fp = is_stdout? bam_dopen(fileno(stdout), "w") : bam_open(name, "w"); - if (fp == 0) { - fprintf(stderr, "[sort_blocks] fail to create file %s.\n", name); - free(name); - // FIXME: possible memory leak - return; - } - free(name); - bam_header_write(fp, h); - for (i = 0; i < k; ++i) - bam_write1_core(fp, &buf[i]->core, buf[i]->data_len, buf[i]->data); - bam_close(fp); -} - -/*! - @abstract Sort an unsorted BAM file based on the chromosome order - and the leftmost position of an alignment - - @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 max_mem approxiate maximum memory (very inaccurate) - - @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, ret, k, i; - size_t mem; - bam_header_t *header; - bamFile fp; - bam1_t *b, **buf; - - g_is_by_qname = is_by_qname; - n = k = 0; mem = 0; - fp = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r"); - if (fp == 0) { - fprintf(stderr, "[bam_sort_core] fail to open file %s\n", fn); - return; - } - header = bam_header_read(fp); - buf = (bam1_t**)calloc(max_mem / BAM_CORE_SIZE, sizeof(bam1_t*)); - // write sub files - for (;;) { - if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t)); - b = buf[k]; - if ((ret = bam_read1(fp, b)) < 0) break; - mem += ret; - ++k; - if (mem >= max_mem) { - sort_blocks(n++, k, buf, prefix, header, 0); - mem = 0; k = 0; - } - } - if (ret != -1) - fprintf(stderr, "[bam_sort_core] truncated file. Continue anyway.\n"); - if (n == 0) sort_blocks(-1, k, buf, prefix, header, is_stdout); - else { // then merge - char **fns, *fnout; - fprintf(stderr, "[bam_sort_core] merging from %d files...\n", n+1); - sort_blocks(n++, k, buf, prefix, header, 0); - fnout = (char*)calloc(strlen(prefix) + 20, 1); - if (is_stdout) sprintf(fnout, "-"); - else sprintf(fnout, "%s.bam", prefix); - fns = (char**)calloc(n, sizeof(char*)); - for (i = 0; i < n; ++i) { - fns[i] = (char*)calloc(strlen(prefix) + 20, 1); - sprintf(fns[i], "%s.%.4d.bam", prefix, i); - } - bam_merge_core(is_by_qname, fnout, 0, n, fns, 0); - free(fnout); - for (i = 0; i < n; ++i) { - unlink(fns[i]); - free(fns[i]); - } - free(fns); - } - for (k = 0; k < max_mem / BAM_CORE_SIZE; ++k) { - if (buf[k]) { - free(buf[k]->data); - free(buf[k]); - } - } - free(buf); - bam_header_destroy(header); - bam_close(fp); -} - -void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem) -{ - bam_sort_core_ext(is_by_qname, fn, prefix, max_mem, 0); -} - -int bam_sort(int argc, char *argv[]) -{ - size_t max_mem = 500000000; - int c, is_by_qname = 0, is_stdout = 0; - while ((c = getopt(argc, argv, "nom:")) >= 0) { - switch (c) { - case 'o': is_stdout = 1; break; - case 'n': is_by_qname = 1; break; - case 'm': max_mem = atol(optarg); break; - } - } - if (optind + 2 > argc) { - fprintf(stderr, "Usage: samtools sort [-on] [-m ] \n"); - return 1; - } - bam_sort_core_ext(is_by_qname, argv[optind], argv[optind+1], max_mem, is_stdout); - return 0; -} diff --git a/samtools/bam_sort.c.pysam.c b/samtools/bam_sort.c.pysam.c new file mode 100644 index 0000000..fa81d05 --- /dev/null +++ b/samtools/bam_sort.c.pysam.c @@ -0,0 +1,420 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#include +#include +#include "bam.h" +#include "ksort.h" + +static int g_is_by_qname = 0; + +static inline int strnum_cmp(const char *a, const char *b) +{ + char *pa, *pb; + pa = (char*)a; pb = (char*)b; + while (*pa && *pb) { + if (isdigit(*pa) && isdigit(*pb)) { + long ai, bi; + ai = strtol(pa, &pa, 10); + bi = strtol(pb, &pb, 10); + if (ai != bi) return aibi? 1 : 0; + } else { + if (*pa != *pb) break; + ++pa; ++pb; + } + } + if (*pa == *pb) + return (pa-a) < (pb-b)? -1 : (pa-a) > (pb-b)? 1 : 0; + return *pa<*pb? -1 : *pa>*pb? 1 : 0; +} + +#define HEAP_EMPTY 0xffffffffffffffffull + +typedef struct { + 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)))) + +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 && __pos_cmp(a, b))); + } 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) +{ + 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; +} + +static void swap_header_text(bam_header_t *h1, bam_header_t *h2) +{ + 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; +} + +#define MERGE_RG 1 +#define MERGE_UNCOMP 2 + +/*! + @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 + + @discussion Padding information may NOT correctly maintained. This + function is NOT thread safe. + */ +int bam_merge_core(int by_qname, const char *out, const char *headers, int n, char * const *fn, + int flag, const char *reg) +{ + 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; + 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)h->b->core.pos<<1 | bam1_strand(h->b); + h->idx = idx++; + } + else h->pos = HEAP_EMPTY; + } + if (flag & MERGE_UNCOMP) { + fpout = strcmp(out, "-")? bam_open(out, "wu") : bam_dopen(fileno(stdout), "wu"); + } else { + fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w"); + } + if (fpout == 0) { + fprintf(pysamerr, "[%s] fail to create the output file.\n", __func__); + return -1; + } + bam_header_write(fpout, hout); + bam_header_destroy(hout); + + ks_heapmake(heap, n, heap); + while (heap->pos != HEAP_EMPTY) { + bam1_t *b = heap->b; + if ((flag & MERGE_RG) && bam_aux_get(b, "RG") == 0) + 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)b->core.pos<<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; +} + +int bam_merge(int argc, char *argv[]) +{ + int c, is_by_qname = 0, flag = 0, ret = 0; + char *fn_headers = NULL, *reg = 0; + + while ((c = getopt(argc, argv, "h:nruR:")) >= 0) { + switch (c) { + case 'r': flag |= MERGE_RG; break; + case 'h': fn_headers = strdup(optarg); break; + case 'n': is_by_qname = 1; break; + case 'u': flag |= MERGE_UNCOMP; break; + case 'R': reg = strdup(optarg); break; + } + } + if (optind + 2 >= argc) { + fprintf(pysamerr, "\n"); + fprintf(pysamerr, "Usage: samtools merge [-nr] [-h inh.sam] [...]\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, " -R STR merge file in the specified region STR [all]\n"); + fprintf(pysamerr, " -h FILE copy the header in FILE to [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 (bam_merge_core(is_by_qname, argv[optind], fn_headers, argc - optind - 1, argv + optind + 1, flag, reg) < 0) ret = 1; + free(reg); + free(fn_headers); + return ret; +} + +typedef bam1_t *bam1_p; + +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 && (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos)))); + } else return (((uint64_t)a->core.tid<<32|a->core.pos) < ((uint64_t)b->core.tid<<32|b->core.pos)); +} +KSORT_INIT(sort, bam1_p, bam1_lt) + +static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam_header_t *h, int is_stdout) +{ + char *name; + int i; + bamFile fp; + ks_mergesort(sort, k, buf, 0); + name = (char*)calloc(strlen(prefix) + 20, 1); + if (n >= 0) sprintf(name, "%s.%.4d.bam", prefix, n); + else sprintf(name, "%s.bam", prefix); + fp = is_stdout? bam_dopen(fileno(stdout), "w") : bam_open(name, "w"); + if (fp == 0) { + fprintf(pysamerr, "[sort_blocks] fail to create file %s.\n", name); + free(name); + // FIXME: possible memory leak + return; + } + free(name); + bam_header_write(fp, h); + for (i = 0; i < k; ++i) + bam_write1_core(fp, &buf[i]->core, buf[i]->data_len, buf[i]->data); + bam_close(fp); +} + +/*! + @abstract Sort an unsorted BAM file based on the chromosome order + and the leftmost position of an alignment + + @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 max_mem approxiate maximum memory (very inaccurate) + + @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, ret, k, i; + size_t mem; + bam_header_t *header; + bamFile fp; + bam1_t *b, **buf; + + g_is_by_qname = is_by_qname; + n = k = 0; mem = 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); + buf = (bam1_t**)calloc(max_mem / BAM_CORE_SIZE, sizeof(bam1_t*)); + // write sub files + for (;;) { + if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t)); + b = buf[k]; + if ((ret = bam_read1(fp, b)) < 0) break; + mem += ret; + ++k; + if (mem >= max_mem) { + sort_blocks(n++, k, buf, prefix, header, 0); + mem = 0; k = 0; + } + } + if (ret != -1) + fprintf(pysamerr, "[bam_sort_core] truncated file. Continue anyway.\n"); + if (n == 0) sort_blocks(-1, k, buf, prefix, header, is_stdout); + else { // then merge + char **fns, *fnout; + fprintf(pysamerr, "[bam_sort_core] merging from %d files...\n", n+1); + sort_blocks(n++, k, buf, prefix, header, 0); + fnout = (char*)calloc(strlen(prefix) + 20, 1); + if (is_stdout) sprintf(fnout, "-"); + else sprintf(fnout, "%s.bam", prefix); + fns = (char**)calloc(n, sizeof(char*)); + for (i = 0; i < n; ++i) { + fns[i] = (char*)calloc(strlen(prefix) + 20, 1); + sprintf(fns[i], "%s.%.4d.bam", prefix, i); + } + bam_merge_core(is_by_qname, fnout, 0, n, fns, 0, 0); + free(fnout); + for (i = 0; i < n; ++i) { + unlink(fns[i]); + free(fns[i]); + } + free(fns); + } + for (k = 0; k < max_mem / BAM_CORE_SIZE; ++k) { + if (buf[k]) { + free(buf[k]->data); + free(buf[k]); + } + } + free(buf); + bam_header_destroy(header); + bam_close(fp); +} + +void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem) +{ + bam_sort_core_ext(is_by_qname, fn, prefix, max_mem, 0); +} + +int bam_sort(int argc, char *argv[]) +{ + size_t max_mem = 500000000; + int c, is_by_qname = 0, is_stdout = 0; + while ((c = getopt(argc, argv, "nom:")) >= 0) { + switch (c) { + case 'o': is_stdout = 1; break; + case 'n': is_by_qname = 1; break; + case 'm': max_mem = atol(optarg); break; + } + } + if (optind + 2 > argc) { + fprintf(pysamerr, "Usage: samtools sort [-on] [-m ] \n"); + return 1; + } + bam_sort_core_ext(is_by_qname, argv[optind], argv[optind+1], max_mem, is_stdout); + return 0; +} diff --git a/samtools/bam_stat.c b/samtools/bam_stat.c deleted file mode 100644 index ea9deee..0000000 --- a/samtools/bam_stat.c +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include "bam.h" - -typedef struct { - long long n_reads, n_mapped, n_pair_all, n_pair_map, n_pair_good; - long long n_sgltn, n_read1, n_read2; - long long n_qcfail, n_dup; - long long n_diffchr, n_diffhigh; -} bam_flagstat_t; - -#define flagstat_loop(s, c) do { \ - ++(s)->n_reads; \ - if ((c)->flag & BAM_FPAIRED) { \ - ++(s)->n_pair_all; \ - if ((c)->flag & BAM_FPROPER_PAIR) ++(s)->n_pair_good; \ - if ((c)->flag & BAM_FREAD1) ++(s)->n_read1; \ - if ((c)->flag & BAM_FREAD2) ++(s)->n_read2; \ - if (((c)->flag & BAM_FMUNMAP) && !((c)->flag & BAM_FUNMAP)) ++(s)->n_sgltn; \ - if (!((c)->flag & BAM_FUNMAP) && !((c)->flag & BAM_FMUNMAP)) { \ - ++(s)->n_pair_map; \ - if ((c)->mtid != (c)->tid) { \ - ++(s)->n_diffchr; \ - if ((c)->qual >= 5) ++(s)->n_diffhigh; \ - } \ - } \ - } \ - if (!((c)->flag & BAM_FUNMAP)) ++(s)->n_mapped; \ - if ((c)->flag & BAM_FQCFAIL) ++(s)->n_qcfail; \ - if ((c)->flag & BAM_FDUP) ++(s)->n_dup; \ - } 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(stderr, "[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(stderr, "Usage: samtools flagstat \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 in total\n", s->n_reads); - printf("%lld QC failure\n", s->n_qcfail); - printf("%lld duplicates\n", s->n_dup); - printf("%lld mapped (%.2f%%)\n", s->n_mapped, (float)s->n_mapped / s->n_reads * 100.0); - printf("%lld paired in sequencing\n", s->n_pair_all); - printf("%lld read1\n", s->n_read1); - printf("%lld read2\n", s->n_read2); - printf("%lld properly paired (%.2f%%)\n", s->n_pair_good, (float)s->n_pair_good / s->n_pair_all * 100.0); - printf("%lld with itself and mate mapped\n", s->n_pair_map); - printf("%lld singletons (%.2f%%)\n", s->n_sgltn, (float)s->n_sgltn / s->n_pair_all * 100.0); - printf("%lld with mate mapped to a different chr\n", s->n_diffchr); - printf("%lld with mate mapped to a different chr (mapQ>=5)\n", s->n_diffhigh); - free(s); - bam_header_destroy(header); - bam_close(fp); - return 0; -} diff --git a/samtools/bam_stat.c.pysam.c b/samtools/bam_stat.c.pysam.c new file mode 100644 index 0000000..4d89ed8 --- /dev/null +++ b/samtools/bam_stat.c.pysam.c @@ -0,0 +1,80 @@ +#include "pysam.h" + +#include +#include +#include "bam.h" + +typedef struct { + long long n_reads, n_mapped, n_pair_all, n_pair_map, n_pair_good; + long long n_sgltn, n_read1, n_read2; + long long n_qcfail, n_dup; + long long n_diffchr, n_diffhigh; +} bam_flagstat_t; + +#define flagstat_loop(s, c) do { \ + ++(s)->n_reads; \ + if ((c)->flag & BAM_FPAIRED) { \ + ++(s)->n_pair_all; \ + if ((c)->flag & BAM_FPROPER_PAIR) ++(s)->n_pair_good; \ + if ((c)->flag & BAM_FREAD1) ++(s)->n_read1; \ + if ((c)->flag & BAM_FREAD2) ++(s)->n_read2; \ + if (((c)->flag & BAM_FMUNMAP) && !((c)->flag & BAM_FUNMAP)) ++(s)->n_sgltn; \ + if (!((c)->flag & BAM_FUNMAP) && !((c)->flag & BAM_FMUNMAP)) { \ + ++(s)->n_pair_map; \ + if ((c)->mtid != (c)->tid) { \ + ++(s)->n_diffchr; \ + if ((c)->qual >= 5) ++(s)->n_diffhigh; \ + } \ + } \ + } \ + if (!((c)->flag & BAM_FUNMAP)) ++(s)->n_mapped; \ + if ((c)->flag & BAM_FQCFAIL) ++(s)->n_qcfail; \ + if ((c)->flag & BAM_FDUP) ++(s)->n_dup; \ + } 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; +} +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 \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 in total\n", s->n_reads); + printf("%lld QC failure\n", s->n_qcfail); + printf("%lld duplicates\n", s->n_dup); + printf("%lld mapped (%.2f%%)\n", s->n_mapped, (float)s->n_mapped / s->n_reads * 100.0); + printf("%lld paired in sequencing\n", s->n_pair_all); + printf("%lld read1\n", s->n_read1); + printf("%lld read2\n", s->n_read2); + printf("%lld properly paired (%.2f%%)\n", s->n_pair_good, (float)s->n_pair_good / s->n_pair_all * 100.0); + printf("%lld with itself and mate mapped\n", s->n_pair_map); + printf("%lld singletons (%.2f%%)\n", s->n_sgltn, (float)s->n_sgltn / s->n_pair_all * 100.0); + printf("%lld with mate mapped to a different chr\n", s->n_diffchr); + printf("%lld with mate mapped to a different chr (mapQ>=5)\n", s->n_diffhigh); + free(s); + bam_header_destroy(header); + bam_close(fp); + return 0; +} diff --git a/samtools/bam_tview.c b/samtools/bam_tview.c deleted file mode 100644 index 7b326fc..0000000 --- a/samtools/bam_tview.c +++ /dev/null @@ -1,424 +0,0 @@ -#undef _HAVE_CURSES - -#if _CURSES_LIB == 0 -#elif _CURSES_LIB == 1 -#include -#ifndef NCURSES_VERSION -#warning "_CURSES_LIB=1 but NCURSES_VERSION not defined; tview is NOT compiled" -#else -#define _HAVE_CURSES -#endif -#elif _CURSES_LIB == 2 -#include -#define _HAVE_CURSES -#else -#warning "_CURSES_LIB is not 0, 1 or 2; tview is NOT compiled" -#endif - -#ifdef _HAVE_CURSES -#include -#include -#include -#include "bam.h" -#include "faidx.h" -#include "bam_maqcns.h" - -char bam_aux_getCEi(bam1_t *b, int i); -char bam_aux_getCSi(bam1_t *b, int i); -char bam_aux_getCQi(bam1_t *b, int i); - -#define TV_MIN_ALNROW 2 -#define TV_MAX_GOTO 40 -#define TV_LOW_MAPQ 10 - -#define TV_COLOR_MAPQ 0 -#define TV_COLOR_BASEQ 1 -#define TV_COLOR_NUCL 2 -#define TV_COLOR_COL 3 -#define TV_COLOR_COLQ 4 - -#define TV_BASE_NUCL 0 -#define TV_BASE_COLOR_SPACE 1 - -typedef struct { - int mrow, mcol; - WINDOW *wgoto, *whelp; - - bam_index_t *idx; - bam_lplbuf_t *lplbuf; - bam_header_t *header; - bamFile fp; - int curr_tid, left_pos; - faidx_t *fai; - bam_maqcns_t *bmc; - - int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins, no_skip, show_name; - char *ref; -} tview_t; - -int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data) -{ - 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) mvprintw(0, tv->ccol, "%-d", i+1); - c = tv->ref? tv->ref[i - tv->left_pos] : 'N'; - mvaddch(1, tv->ccol++, c); - } - if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1); - // print consensus - call = bam_maqcns_call(n, pl, tv->bmc); - attr = A_UNDERLINE; - c = ",ACMGRSVTWYHKDBN"[call>>28&0xf]; - i = (call>>8&0xff)/10+1; - if (i > 4) i = 4; - attr |= COLOR_PAIR(i); - if (c == toupper(rb)) c = '.'; - attron(attr); - mvaddch(2, tv->ccol, c); - attroff(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))) { - 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 = '*'; - } 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 |= A_UNDERLINE; - if (tv->color_for == TV_COLOR_BASEQ) { - x = bam1_qual(p->b)[p->qpos]/10 + 1; - if (x > 4) x = 4; - attr |= COLOR_PAIR(x); - } else if (tv->color_for == TV_COLOR_MAPQ) { - x = p->b->core.qual/10 + 1; - if (x > 4) x = 4; - attr |= COLOR_PAIR(x); - } else if (tv->color_for == TV_COLOR_NUCL) { - x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5; - attr |= COLOR_PAIR(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 |= COLOR_PAIR(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 |= COLOR_PAIR(x); - } - attron(attr); - mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c)); - attroff(attr); - } - } - c = j? '*' : rb; - if (c == '*') { - attr = COLOR_PAIR(8); - attron(attr); - mvaddch(1, tv->ccol++, c); - attroff(attr); - } else mvaddch(1, tv->ccol++, c); - } - tv->last_pos = pos; - return 0; -} - -tview_t *tv_init(const char *fn, const char *fn_fa) -{ - tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t)); - tv->is_dot = 1; - tv->idx = bam_index_load(fn); - if (tv->idx == 0) exit(1); - tv->fp = bam_open(fn, "r"); - bgzf_set_cache_size(tv->fp, 8 * 1024 *1024); - assert(tv->fp); - tv->header = bam_header_read(tv->fp); - tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv); - if (fn_fa) tv->fai = fai_load(fn_fa); - tv->bmc = bam_maqcns_init(); - tv->ins = 1; - bam_maqcns_prepare(tv->bmc); - - initscr(); - keypad(stdscr, TRUE); - clear(); - noecho(); - cbreak(); - tv->mrow = 24; tv->mcol = 80; - getmaxyx(stdscr, tv->mrow, tv->mcol); - tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5); - tv->whelp = newwin(29, 40, 5, 5); - tv->color_for = TV_COLOR_MAPQ; - 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 tv; -} - -void tv_destroy(tview_t *tv) -{ - delwin(tv->wgoto); delwin(tv->whelp); - endwin(); - - bam_lplbuf_destroy(tv->lplbuf); - bam_maqcns_destroy(tv->bmc); - bam_index_destroy(tv->idx); - if (tv->fai) fai_destroy(tv->fai); - free(tv->ref); - bam_header_destroy(tv->header); - bam_close(tv->fp); - free(tv); -} - -int tv_fetch_func(const bam1_t *b, void *data) -{ - tview_t *tv = (tview_t*)data; - if (tv->no_skip) { - uint32_t *cigar = bam1_cigar(b); // this is cheating... - int i; - for (i = 0; i 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 tv_draw_aln(tview_t *tv, int tid, int pos) -{ - // reset - clear(); - 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); - str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1); - 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) mvprintw(0, tv->ccol, "%-d", pos+1); - mvaddch(1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N'); - ++tv->last_pos; - } - return 0; -} - -static void tv_win_goto(tview_t *tv, int *tid, int *pos) -{ - char str[256], *p; - int i, l = 0; - 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') { - --l; - } else if (c == KEY_ENTER || c == '\012' || c == '\015') { - int _tid = -1, _beg, _end; - if (str[0] == '=') { - _beg = strtol(str+1, &p, 10); - if (_beg > 0) { - *pos = _beg; - return; - } - } else { - bam_parse_region(tv->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); - } -} - -static void tv_win_help(tview_t *tv) { - int r = 1; - 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); -} - -void tv_loop(tview_t *tv) -{ - int tid, pos; - tid = tv->curr_tid; pos = tv->left_pos; - while (1) { - int c = getch(); - switch (c) { - case '?': tv_win_help(tv); break; - case '\033': - case 'q': goto end_loop; - case '/': - case 'g': tv_win_goto(tv, &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_draw_aln(tv, tid, pos); - } -end_loop: - return; -} - -int bam_tview_main(int argc, char *argv[]) -{ - tview_t *tv; - if (argc == 1) { - fprintf(stderr, "Usage: bamtk tview [ref.fasta]\n"); - return 1; - } - tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]); - tv_draw_aln(tv, 0, 0); - tv_loop(tv); - tv_destroy(tv); - return 0; -} -#else // #ifdef _HAVE_CURSES -#include -#warning "No curses library is available; tview is disabled." -int bam_tview_main(int argc, char *argv[]) -{ - fprintf(stderr, "[bam_tview_main] The ncurses library is unavailable; tview is not compiled.\n"); - return 1; -} -#endif // #ifdef _HAVE_CURSES diff --git a/samtools/bam_tview.c.pysam.c b/samtools/bam_tview.c.pysam.c new file mode 100644 index 0000000..6e02762 --- /dev/null +++ b/samtools/bam_tview.c.pysam.c @@ -0,0 +1,426 @@ +#include "pysam.h" + +#undef _HAVE_CURSES + +#if _CURSES_LIB == 0 +#elif _CURSES_LIB == 1 +#include +#ifndef NCURSES_VERSION +#warning "_CURSES_LIB=1 but NCURSES_VERSION not defined; tview is NOT compiled" +#else +#define _HAVE_CURSES +#endif +#elif _CURSES_LIB == 2 +#include +#define _HAVE_CURSES +#else +#warning "_CURSES_LIB is not 0, 1 or 2; tview is NOT compiled" +#endif + +#ifdef _HAVE_CURSES +#include +#include +#include +#include "bam.h" +#include "faidx.h" +#include "bam_maqcns.h" + +char bam_aux_getCEi(bam1_t *b, int i); +char bam_aux_getCSi(bam1_t *b, int i); +char bam_aux_getCQi(bam1_t *b, int i); + +#define TV_MIN_ALNROW 2 +#define TV_MAX_GOTO 40 +#define TV_LOW_MAPQ 10 + +#define TV_COLOR_MAPQ 0 +#define TV_COLOR_BASEQ 1 +#define TV_COLOR_NUCL 2 +#define TV_COLOR_COL 3 +#define TV_COLOR_COLQ 4 + +#define TV_BASE_NUCL 0 +#define TV_BASE_COLOR_SPACE 1 + +typedef struct { + int mrow, mcol; + WINDOW *wgoto, *whelp; + + bam_index_t *idx; + bam_lplbuf_t *lplbuf; + bam_header_t *header; + bamFile fp; + int curr_tid, left_pos; + faidx_t *fai; + bam_maqcns_t *bmc; + + int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins, no_skip, show_name; + char *ref; +} tview_t; + +int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data) +{ + 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) mvprintw(0, tv->ccol, "%-d", i+1); + c = tv->ref? tv->ref[i - tv->left_pos] : 'N'; + mvaddch(1, tv->ccol++, c); + } + if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1); + // print consensus + call = bam_maqcns_call(n, pl, tv->bmc); + attr = A_UNDERLINE; + c = ",ACMGRSVTWYHKDBN"[call>>28&0xf]; + i = (call>>8&0xff)/10+1; + if (i > 4) i = 4; + attr |= COLOR_PAIR(i); + if (c == toupper(rb)) c = '.'; + attron(attr); + mvaddch(2, tv->ccol, c); + attroff(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))) { + 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 |= A_UNDERLINE; + if (tv->color_for == TV_COLOR_BASEQ) { + x = bam1_qual(p->b)[p->qpos]/10 + 1; + if (x > 4) x = 4; + attr |= COLOR_PAIR(x); + } else if (tv->color_for == TV_COLOR_MAPQ) { + x = p->b->core.qual/10 + 1; + if (x > 4) x = 4; + attr |= COLOR_PAIR(x); + } else if (tv->color_for == TV_COLOR_NUCL) { + x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5; + attr |= COLOR_PAIR(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 |= COLOR_PAIR(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 |= COLOR_PAIR(x); + } + attron(attr); + mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c)); + attroff(attr); + } + } + c = j? '*' : rb; + if (c == '*') { + attr = COLOR_PAIR(8); + attron(attr); + mvaddch(1, tv->ccol++, c); + attroff(attr); + } else mvaddch(1, tv->ccol++, c); + } + tv->last_pos = pos; + return 0; +} + +tview_t *tv_init(const char *fn, const char *fn_fa) +{ + tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t)); + tv->is_dot = 1; + tv->idx = bam_index_load(fn); + if (tv->idx == 0) exit(1); + tv->fp = bam_open(fn, "r"); + bgzf_set_cache_size(tv->fp, 8 * 1024 *1024); + assert(tv->fp); + tv->header = bam_header_read(tv->fp); + tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv); + if (fn_fa) tv->fai = fai_load(fn_fa); + tv->bmc = bam_maqcns_init(); + tv->ins = 1; + bam_maqcns_prepare(tv->bmc); + + initscr(); + keypad(stdscr, TRUE); + clear(); + noecho(); + cbreak(); + tv->mrow = 24; tv->mcol = 80; + getmaxyx(stdscr, tv->mrow, tv->mcol); + tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5); + tv->whelp = newwin(29, 40, 5, 5); + tv->color_for = TV_COLOR_MAPQ; + 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 tv; +} + +void tv_destroy(tview_t *tv) +{ + delwin(tv->wgoto); delwin(tv->whelp); + endwin(); + + bam_lplbuf_destroy(tv->lplbuf); + bam_maqcns_destroy(tv->bmc); + bam_index_destroy(tv->idx); + if (tv->fai) fai_destroy(tv->fai); + free(tv->ref); + bam_header_destroy(tv->header); + bam_close(tv->fp); + free(tv); +} + +int tv_fetch_func(const bam1_t *b, void *data) +{ + tview_t *tv = (tview_t*)data; + if (tv->no_skip) { + uint32_t *cigar = bam1_cigar(b); // this is cheating... + int i; + for (i = 0; i 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 tv_draw_aln(tview_t *tv, int tid, int pos) +{ + // reset + clear(); + 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); + str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1); + 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) mvprintw(0, tv->ccol, "%-d", pos+1); + mvaddch(1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N'); + ++tv->last_pos; + } + return 0; +} + +static void tv_win_goto(tview_t *tv, int *tid, int *pos) +{ + char str[256], *p; + int i, l = 0; + 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') { + --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(tv->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); + } +} + +static void tv_win_help(tview_t *tv) { + int r = 1; + 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); +} + +void tv_loop(tview_t *tv) +{ + int tid, pos; + tid = tv->curr_tid; pos = tv->left_pos; + while (1) { + int c = getch(); + switch (c) { + case '?': tv_win_help(tv); break; + case '\033': + case 'q': goto end_loop; + case '/': + case 'g': tv_win_goto(tv, &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_draw_aln(tv, tid, pos); + } +end_loop: + return; +} + +int bam_tview_main(int argc, char *argv[]) +{ + tview_t *tv; + if (argc == 1) { + fprintf(pysamerr, "Usage: bamtk tview [ref.fasta]\n"); + return 1; + } + tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]); + tv_draw_aln(tv, 0, 0); + tv_loop(tv); + tv_destroy(tv); + return 0; +} +#else // #ifdef _HAVE_CURSES +#include +#warning "No curses library is available; tview is disabled." +int bam_tview_main(int argc, char *argv[]) +{ + fprintf(pysamerr, "[bam_tview_main] The ncurses library is unavailable; tview is not compiled.\n"); + return 1; +} +#endif // #ifdef _HAVE_CURSES diff --git a/samtools/bcftools/bcf.c.pysam.c b/samtools/bcftools/bcf.c.pysam.c new file mode 100644 index 0000000..14307c9 --- /dev/null +++ b/samtools/bcftools/bcf.c.pysam.c @@ -0,0 +1,322 @@ +#include "pysam.h" + +#include +#include +#include +#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); + } +#ifndef BCF_LITE + b->fp->owned_file = 1; +#endif + 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) tmp[n++] = p + 1; + if (n != 5) { + fprintf(pysamerr, "[%s] incorrect number of fields (%d != 5). Corrupted file?\n", __func__, n); + 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].len = 2; + } else if (b->gi[i].fmt == bcf_str2int("GQ", 2) || b->gi[i].fmt == bcf_str2int("GT", 2) + || b->gi[i].fmt == bcf_str2int("SP", 2)) + { + b->gi[i].len = 1; + } 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; + for (j = 0; j < h->n_smpl; ++j) { + 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; + 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)) { + kputw(((uint16_t*)b->gi[i].data)[j], s); + } else if (b->gi[i].fmt == bcf_str2int("GQ", 2) || b->gi[i].fmt == bcf_str2int("SP", 2)) { + kputw(((uint8_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 (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]); + } + } + } + } +} + +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 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 new file mode 100644 index 0000000..f87ac1e --- /dev/null +++ b/samtools/bcftools/bcf.h @@ -0,0 +1,175 @@ +/* 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 */ + +#ifndef BCF_H +#define BCF_H + +#include +#include + +#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) +} 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); + // 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 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); + // convert GL to PL + int bcf_gl2pl(bcf1_t *b); + // if the site is an indel + int bcf_is_indel(const bcf1_t *b); + + // string hash table + void *bcf_build_refhash(bcf_hdr_t *h); + void bcf_str2id_destroy(void *_hash); + int bcf_str2id_add(void *_hash, const char *str); + int bcf_str2id(void *_hash, const char *str); + void *bcf_str2id_init(); + + // 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 new file mode 100644 index 0000000..e512a55 --- /dev/null +++ b/samtools/bcftools/bcf2qcall.c.pysam.c @@ -0,0 +1,93 @@ +#include "pysam.h" + +#include +#include +#include +#include +#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; + g[j++] = p[x * b->n_alleles - x * (x-1) / 2 + (y - 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 new file mode 100644 index 0000000..edacdb4 --- /dev/null +++ b/samtools/bcftools/bcfutils.c.pysam.c @@ -0,0 +1,111 @@ +#include "pysam.h" + +#include "bcf.h" +#include "kstring.h" +#include "khash.h" +KHASH_MAP_INIT_STR(str2id, int) + +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. +} + +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); +} + +int bcf_shrink_alt(bcf1_t *b, int n) +{ + char *p; + int i, j, k, *z, n_smpl = b->n_smpl; + if (b->n_alleles <= n) return -1; + 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; + z = alloca(sizeof(int) / 2 * n * (n+1)); + for (i = k = 0; i < n; ++i) + for (j = 0; j < n - i; ++j) + z[k++] = i * b->n_alleles + j; + 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[z[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; +} diff --git a/samtools/bcftools/call1.c.pysam.c b/samtools/bcftools/call1.c.pysam.c new file mode 100644 index 0000000..2e21fe5 --- /dev/null +++ b/samtools/bcftools/call1.c.pysam.c @@ -0,0 +1,388 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#include "bcf.h" +#include "prob1.h" +#include "kstring.h" + +#include "khash.h" +KHASH_SET_INIT_INT64(set64) + +#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_HWE 128 +#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_FOLDED 16384 + +typedef struct { + int flag, prior_type, n1; + char *fn_list, *prior_file; + double theta, pref, indel_frac; +} viewconf_t; + +khash_t(set64) *bcf_load_pos(const char *fn, bcf_hdr_t *_h) +{ + void *str2id; + gzFile fp; + kstream_t *ks; + int ret, dret, lineno = 1; + kstring_t *str; + khash_t(set64) *hash = 0; + + hash = kh_init(set64); + str2id = bcf_build_refhash(_h); + 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 = bcf_str2id(str2id, 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; + } else fprintf(pysamerr, "[%s] %s is not a reference name (line %d).\n", __func__, str->s, lineno); + if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n'); + if (dret < 0) break; + ++lineno; + } + bcf_str2id_destroy(str2id); + ks_destroy(ks); + gzclose(fp); + free(str->s); free(str); + return hash; +} + +static double test_hwe(const double g[3]) +{ + extern double kf_gammaq(double p, double x); + double fexp, chi2, f[3], n; + int i; + n = g[0] + g[1] + g[2]; + fexp = (2. * g[2] + g[1]) / (2. * n); + if (fexp > 1. - 1e-10) fexp = 1. - 1e-10; + if (fexp < 1e-10) fexp = 1e-10; + f[0] = n * (1. - fexp) * (1. - fexp); + f[1] = n * 2. * fexp * (1. - fexp); + f[2] = n * fexp * fexp; + for (i = 0, chi2 = 0.; i < 3; ++i) + chi2 += (g[i] - f[i]) * (g[i] - f[i]) / f[i]; + return kf_gammaq(.5, chi2 / 2.); +} + +typedef struct { + double p[4]; + int mq, depth, is_tested, d[4]; +} anno16_t; + +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; +} + +static 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 void rm_info(bcf1_t *b, const char *key) +{ + char *p, *q; + if ((p = strstr(b->info, key)) == 0) return; + for (q = p; *q && *q != ';'; ++q); + if (p > b->info && *(p-1) == ';') --p; + memmove(p, q, b->l_str - (q - b->str)); + b->l_str -= q - p; + bcf_sync(b); +} + +static int update_bcf1(int n_smpl, bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p1rst_t *pr, double pref, int flag) +{ + kstring_t s; + int is_var = (pr->p_ref < pref); + double p_hwe, r = is_var? pr->p_ref : 1. - pr->p_ref; + anno16_t a; + + p_hwe = pr->g[0] >= 0.? test_hwe(pr->g) : 1.0; // only do HWE g[] is calculated + test16(b, &a); + rm_info(b, "I16="); + + 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); +// ksprintf(&s, "AF1=%.4lg;AFE=%.4lg;CI95=%.4lg,%.4lg", 1.-pr->f_em, 1.-pr->f_exp, pr->cil, pr->cih); + ksprintf(&s, "AF1=%.4lg;CI95=%.4lg,%.4lg", 1.-pr->f_em, pr->cil, pr->cih); + ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq); + if (a.is_tested) { + if (pr->pc[0] >= 0.) ksprintf(&s, ";PC4=%lg,%lg,%lg,%lg", pr->pc[0], pr->pc[1], pr->pc[2], pr->pc[3]); + ksprintf(&s, ";PV4=%.2lg,%.2lg,%.2lg,%.2lg", a.p[0], a.p[1], a.p[2], a.p[3]); + } + if (pr->g[0] >= 0. && p_hwe <= .2) + ksprintf(&s, ";GC=%.2lf,%.2lf,%.2lf;HWE=%.3lf", pr->g[2], pr->g[1], pr->g[0], p_hwe); + 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; + b->qual = r < 1e-100? 99 : -4.343 * log(r); + if (b->qual > 99) b->qual = 99; + 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_em, 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; +} + +double bcf_ld_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); + bcf_t *bp, *bout = 0; + bcf1_t *b, *blast; + int c; + uint64_t n_processed = 0; + viewconf_t vc; + bcf_p1aux_t *p1 = 0; + bcf_hdr_t *h; + int tid, begin, end; + char moder[4], modew[4]; + khash_t(set64) *hash = 0; + + 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.; + while ((c = getopt(argc, argv, "fN1:l:cHAGvbSuP:t:p:QgLi:I")) >= 0) { + switch (c) { + case 'f': vc.flag |= VC_FOLDED; break; + case '1': vc.n1 = atoi(optarg); break; + case 'l': vc.fn_list = strdup(optarg); 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 'v': vc.flag |= VC_VARONLY | VC_CALL; break; + case 'u': vc.flag |= VC_UNCOMP | VC_BCFOUT; break; + case 'H': vc.flag |= VC_HWE; break; + case 'g': vc.flag |= VC_CALL_GT | VC_CALL; break; + case 'I': vc.flag |= VC_NO_INDEL; 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 '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] [reg]\n\n"); + fprintf(pysamerr, "Options: -c SNP calling\n"); + fprintf(pysamerr, " -v output potential variant sites only (force -c)\n"); + fprintf(pysamerr, " -g call genotypes at variant sites (force -c)\n"); + fprintf(pysamerr, " -b output BCF instead of VCF\n"); + fprintf(pysamerr, " -u uncompressed BCF output (force -b)\n"); + fprintf(pysamerr, " -S input is VCF\n"); + fprintf(pysamerr, " -A keep all possible alternate alleles at variant sites\n"); + fprintf(pysamerr, " -G suppress all individual genotype information\n"); + fprintf(pysamerr, " -H perform Hardy-Weinberg test (slower)\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, " -L calculate LD for adjacent sites\n"); + fprintf(pysamerr, " -I skip indels\n"); + fprintf(pysamerr, " -f reference-free variant calling\n"); + fprintf(pysamerr, " -1 INT number of group-1 samples [0]\n"); + fprintf(pysamerr, " -l FILE list of sites to output [all sites]\n"); + fprintf(pysamerr, " -t FLOAT scaled substitution mutation rate [%.4lg]\n", vc.theta); + fprintf(pysamerr, " -i FLOAT indel-to-substitution ratio [%.4lg]\n", vc.indel_frac); + fprintf(pysamerr, " -p FLOAT variant if P(ref|D)n_smpl); + 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) { + 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 (vc.flag & VC_FOLDED) bcf_p1_set_folded(p1); + } + if (vc.fn_list) hash = bcf_load_pos(vc.fn_list, h); + if (optind + 1 < argc && !(vc.flag&VC_VCFIN)) { + void *str2id = bcf_build_refhash(h); + 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); + } + } + } + while (vcf_read(bp, h, b) > 0) { + int is_indel = bcf_is_indel(b); + if ((vc.flag & VC_NO_INDEL) && 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 (hash) { + uint64_t x = (uint64_t)b->tid<<32 | b->pos; + khint_t k = kh_get(set64, hash, x); + if (kh_size(hash) == 0) break; + if (k == kh_end(hash)) continue; + kh_del(set64, hash, k); + } + 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_QCALL) { // output QCALL format; STOP here + bcf_2qcall(h, b); + continue; + } + if (vc.flag & (VC_CALL|VC_ADJLD)) bcf_gl2pl(b); + if (vc.flag & VC_CALL) { // call variants + bcf_p1rst_t pr; + bcf_p1_cal(b, p1, &pr); // pr.g[3] is not calculated here + if (vc.flag&VC_HWE) bcf_p1_cal_g3(p1, pr.g); + 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; + update_bcf1(h->n_smpl, b, p1, &pr, vc.pref, vc.flag); + } + if (vc.flag & VC_ADJLD) { // compute LD + double f[4], r2; + if ((r2 = bcf_ld_freq(blast, b, f)) >= 0) { + kstring_t s; + s.m = s.l = 0; s.s = 0; + if (*b->info) kputc(';', &s); + ksprintf(&s, "NEIR=%.3lf;NEIF=%.3lf,%.3lf", r2, f[0]+f[2], f[0]+f[1]); + bcf_append_info(b, s.s, s.l); + free(s.s); + } + bcf_cpy(blast, b); + } + if (vc.flag & VC_NO_GENO) { // do not output GENO fields + b->n_gi = 0; + b->fmt[0] = '\0'; + } + vcf_write(bout, h, b); + } + if (vc.prior_file) free(vc.prior_file); + if (vc.flag & VC_CALL) bcf_p1_dump_afs(p1); + bcf_hdr_destroy(h); + bcf_destroy(b); bcf_destroy(blast); + vcf_close(bp); vcf_close(bout); + if (hash) kh_destroy(set64, hash); + if (vc.fn_list) free(vc.fn_list); + if (p1) bcf_p1_destroy(p1); + return 0; +} diff --git a/samtools/bcftools/fet.c.pysam.c b/samtools/bcftools/fet.c.pysam.c new file mode 100644 index 0000000..a4fc775 --- /dev/null +++ b/samtools/bcftools/fet.c.pysam.c @@ -0,0 +1,112 @@ +#include "pysam.h" + +#include +#include + +/* 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 < 0) ? 0 : (n1_ + n_1 - n < 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); + 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 + +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 new file mode 100644 index 0000000..8258854 --- /dev/null +++ b/samtools/bcftools/index.c.pysam.c @@ -0,0 +1,337 @@ +#include "pysam.h" + +#include +#include +#include +#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); + 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 \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 new file mode 100644 index 0000000..4de836e --- /dev/null +++ b/samtools/bcftools/kfunc.c.pysam.c @@ -0,0 +1,164 @@ +#include "pysam.h" + +#include + + +/* 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 +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/ld.c.pysam.c b/samtools/bcftools/ld.c.pysam.c new file mode 100644 index 0000000..2b7bf47 --- /dev/null +++ b/samtools/bcftools/ld.c.pysam.c @@ -0,0 +1,102 @@ +#include "pysam.h" + +#include +#include +#include +#include "bcf.h" + +static double g_q2p[256]; + +#define LD_ITER_MAX 50 +#define LD_ITER_EPS 1e-4 + +#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 freq_iter(int n, double *pdg[2], double f[4]) +{ + double ff[4]; + int i, k, h; + 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_ld_freq(const bcf1_t *b0, const bcf1_t *b1, double f[4]) +{ + const bcf1_t *b[2]; + uint8_t *PL[2]; + int i, j, PL_len[2], n_smpl; + double *pdg[2], flast[4], r; + // initialize g_q2p if necessary + if (g_q2p[0] == 0.) + for (i = 0; i < 256; ++i) + g_q2p[i] = pow(10., -i / 10.); + // 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 + // set PL and PL_len + for (j = 0; j < 2; ++j) { + const bcf1_t *bj = b[j]; + for (i = 0; i < bj->n_gi; ++i) { + if (bj->gi[i].fmt == bcf_str2int("PL", 2)) { + PL[j] = (uint8_t*)bj->gi[i].data; + PL_len[j] = bj->gi[i].len; + break; + } + } + if (i == bj->n_gi) return -1; // no PL + } + // fill pdg[2] + pdg[0] = malloc(3 * n_smpl * sizeof(double)); + pdg[1] = malloc(3 * n_smpl * sizeof(double)); + for (j = 0; j < 2; ++j) { + for (i = 0; i < n_smpl; ++i) { + const uint8_t *pi = PL[j] + i * PL_len[j]; + double *p = pdg[j] + i * 3; + p[0] = g_q2p[pi[b[j]->n_alleles]]; p[1] = g_q2p[pi[1]]; p[2] = g_q2p[pi[0]]; + } + } + // iteration + f[0] = f[1] = f[2] = f[3] = 0.25; // this is a really bad guess... + for (j = 0; j < LD_ITER_MAX; ++j) { + double eps = 0; + memcpy(flast, f, 4 * sizeof(double)); + 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 < LD_ITER_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])); + // fprintf(pysamerr, "R(%lf,%lf,%lf,%lf)=%lf\n", f[0], f[1], f[2], f[3], r2); + if (isnan(r)) r = -1.; + } + return r; +} diff --git a/samtools/bcftools/prob1.c.pysam.c b/samtools/bcftools/prob1.c.pysam.c new file mode 100644 index 0000000..7738c39 --- /dev/null +++ b/samtools/bcftools/prob1.c.pysam.c @@ -0,0 +1,439 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#include "prob1.h" + +#include "kseq.h" +KSTREAM_INIT(gzFile, gzread, 16384) + +#define MC_MAX_EM_ITER 16 +#define MC_EM_EPS 1e-4 +#define MC_DEF_INDEL 0.15 + +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, is_folded; + 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 t, t1, t2; + double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution + const uint8_t *PL; // point to PL + int PL_len; +}; + +static void fold_array(int M, double *x) +{ + int k; + for (k = 0; k < M/2; ++k) + x[k] = x[M-k] = (x[k] + x[M-k]) / 2.; +} + +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) +{ + bcf_p1aux_t *ma; + int i; + ma = calloc(1, sizeof(bcf_p1aux_t)); + ma->n1 = -1; + ma->n = n; ma->M = 2 * n; + 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(2 * ma->n + 1, sizeof(double)); + ma->zswap = calloc(2 * ma->n + 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(2 * ma->n + 1, sizeof(double)); + ma->afs1 = calloc(2 * ma->n + 1, sizeof(double)); + for (i = 0; i < 256; ++i) + ma->q2p[i] = pow(10., -i / 10.); + bcf_p1_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior + return ma; +} + +int bcf_p1_set_n1(bcf_p1aux_t *b, int n1) +{ + if (n1 == 0 || n1 >= b->n) return -1; + b->n1 = n1; + return 0; +} + +void bcf_p1_set_folded(bcf_p1aux_t *p1a) +{ + if (p1a->n1 < 0) { + p1a->is_folded = 1; + fold_array(p1a->M, p1a->phi); + fold_array(p1a->M, p1a->phi_indel); + } +} + +void bcf_p1_destroy(bcf_p1aux_t *ma) +{ + if (ma) { + 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); + } +} + +static int cal_pdg(const bcf1_t *b, bcf_p1aux_t *ma) +{ + int i, j, k; + 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[b->n_alleles]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]]; + for (i = k = 0; i < b->n_alleles; ++i) { + p[i] += (int)pi[k]; + k += b->n_alleles - i; + } + } + 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; +} +// f0 is the reference allele frequency +static double mc_freq_iter(double f0, const bcf_p1aux_t *ma) +{ + double f, f3[3]; + int i; + f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0; + for (i = 0, f = 0.; i < ma->n; ++i) { + double *pdg; + pdg = ma->pdg + i * 3; + f += (pdg[1] * f3[1] + 2. * pdg[2] * f3[2]) + / (pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2]); + } + f /= ma->n * 2.; + return f; +} + +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; + f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*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; + 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.; + for (_j = beg; _j < ma->n; ++_j) { + int k, j = _j - beg, _min = last_min, _max = last_max; + double p[3], sum; + 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] = (2*j+2-k)*(2*j-k+1) * p[0] * z[0][k]; + if (_min <= 1) + k = 1, z[1][k] = (2*j+2-k)*(2*j-k+1) * p[0] * z[0][k] + k*(2*j+2-k) * p[1] * z[0][k-1]; + for (k = _min < 2? 2 : _min; k <= _max; ++k) + z[1][k] = (2*j+2-k)*(2*j-k+1) * p[0] * z[0][k] + + k*(2*j+2-k) * 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 / ((2. * j + 2) * (2. * j + 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->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; + } + if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1)); +} + +static void mc_cal_y(bcf_p1aux_t *ma) +{ + if (ma->n1 > 0 && ma->n1 < ma->n) { + 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); +} + +static void contrast(bcf_p1aux_t *ma, double pc[4]) // mc_cal_y() must be called before hand +{ + int k, n1 = ma->n1, n2 = ma->n - ma->n1; + long double sum1, sum2; + pc[0] = pc[1] = pc[2] = pc[3] = -1.; + if (n1 <= 0 || n2 <= 0) return; + for (k = 0, sum1 = 0.; k <= 2*n1; ++k) sum1 += ma->phi1[k] * ma->z1[k]; + for (k = 0, sum2 = 0.; k <= 2*n2; ++k) sum2 += ma->phi2[k] * ma->z2[k]; + pc[2] = ma->phi1[2*n1] * ma->z1[2*n1] / sum1; + pc[3] = ma->phi2[2*n2] * ma->z2[2*n2] / sum2; + for (k = 2; k < 4; ++k) { + pc[k] = pc[k] > .5? -(-4.343 * log(1. - pc[k] + TINY) + .499) : -4.343 * log(pc[k] + TINY) + .499; + pc[k] = (int)pc[k]; + if (pc[k] > 99) pc[k] = 99; + if (pc[k] < -99) pc[k] = -99; + } + pc[0] = ma->phi2[2*n2] * ma->z2[2*n2] / sum2 * (1. - ma->phi1[2*n1] * ma->z1[2*n1] / sum1); + pc[1] = ma->phi1[2*n1] * ma->z1[2*n1] / sum1 * (1. - ma->phi2[2*n2] * ma->z2[2*n2] / sum2); + pc[0] = pc[0] == 1.? 99 : (int)(-4.343 * log(1. - pc[0]) + .499); + pc[1] = pc[1] == 1.? 99 : (int)(-4.343 * log(1. - pc[1]) + .499); +} + +static double mc_cal_afs(bcf_p1aux_t *ma) +{ + int k; + long double sum = 0.; + double *phi = ma->is_indel? ma->phi_indel : ma->phi; + memset(ma->afs1, 0, sizeof(double) * (ma->M + 1)); + mc_cal_y(ma); + 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.; + } + for (k = 0, sum = 0.; k <= ma->M; ++k) { + ma->afs[k] += ma->afs1[k]; + sum += k * ma->afs1[k]; + } + return sum / ma->M; +} + +long double bcf_p1_cal_g3(bcf_p1aux_t *p1a, double g[3]) +{ + long double pd = 0., g2[3]; + int i, k; + memset(g2, 0, sizeof(long double) * 3); + for (k = 0; k < p1a->M; ++k) { + double f = (double)k / p1a->M, f3[3], g1[3]; + long double z = 1.; + g1[0] = g1[1] = g1[2] = 0.; + f3[0] = (1. - f) * (1. - f); f3[1] = 2. * f * (1. - f); f3[2] = f * f; + for (i = 0; i < p1a->n; ++i) { + double *pdg = p1a->pdg + i * 3; + double x = pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2]; + z *= x; + g1[0] += pdg[0] * f3[0] / x; + g1[1] += pdg[1] * f3[1] / x; + g1[2] += pdg[2] * f3[2] / x; + } + pd += p1a->phi[k] * z; + for (i = 0; i < 3; ++i) + g2[i] += p1a->phi[k] * z * g1[i]; + } + for (i = 0; i < 3; ++i) g[i] = g2[i] / pd; + return pd; +} + +int bcf_p1_cal(bcf1_t *b, bcf_p1aux_t *ma, bcf_p1rst_t *rst) +{ + int i, k; + long double sum = 0.; + ma->is_indel = bcf_is_indel(b); + // 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 (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 = ma->is_folded? ma->afs1[ma->M] + ma->afs1[0] : ma->afs1[ma->M]; + // 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; + { // calculate f_em + double flast = rst->f_flat; + for (i = 0; i < MC_MAX_EM_ITER; ++i) { + rst->f_em = mc_freq_iter(flast, ma); + if (fabs(rst->f_em - flast) < MC_EM_EPS) break; + flast = rst->f_em; + } + } + { // 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-1, 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; + } + rst->g[0] = rst->g[1] = rst->g[2] = -1.; + contrast(ma, rst->pc); + return 0; +} + +void bcf_p1_dump_afs(bcf_p1aux_t *ma) +{ + int k; + if (ma->is_folded) fold_array(ma->M, ma->afs); + 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 new file mode 100644 index 0000000..3827534 --- /dev/null +++ b/samtools/bcftools/prob1.h @@ -0,0 +1,41 @@ +#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; + double f_em, f_exp, f_flat, p_ref; + double cil, cih; + double pc[4]; + double g[3]; +} bcf_p1rst_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); + 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); + int bcf_p1_cal(bcf1_t *b, bcf_p1aux_t *ma, bcf_p1rst_t *rst); + 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); + long double bcf_p1_cal_g3(bcf_p1aux_t *p1a, double g[3]); + 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 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/samtools/bcftools/vcf.c.pysam.c b/samtools/bcftools/vcf.c.pysam.c new file mode 100644 index 0000000..5bad5c6 --- /dev/null +++ b/samtools/bcftools/vcf.c.pysam.c @@ -0,0 +1,214 @@ +#include "pysam.h" + +#include +#include +#include +#include +#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] != '#') 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_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_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_ref = 0, 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.0\n"); + fwrite(h->txt, 1, h->l_txt - 1, v->fpout); + if (strstr(h->txt, "##SQ=")) has_ref = 1; + } + if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.0\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, 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) || b->gi[i].fmt == bcf_str2int("SP", 2)) { + ((uint8_t*)b->gi[i].data)[k-9] = 0; + } else if (b->gi[i].fmt == bcf_str2int("DP", 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) || b->gi[i].fmt == bcf_str2int("SP", 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("DP", 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; + ++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/bgzf.c b/samtools/bgzf.c deleted file mode 100644 index a6923da..0000000 --- a/samtools/bgzf.c +++ /dev/null @@ -1,671 +0,0 @@ -/* The MIT License - - Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology - - 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. -*/ - -/* - 2009-06-29 by lh3: cache recent uncompressed blocks. - 2009-06-25 by lh3: optionally use my knetfile library to access file on a FTP. - 2009-06-12 by lh3: support a mode string like "wu" where 'u' for uncompressed output */ - -#include -#include -#include -#include -#include -#include -#include -#include "bgzf.h" - -#include "khash.h" -typedef struct { - int size; - uint8_t *block; - int64_t end_offset; -} cache_t; -KHASH_MAP_INIT_INT64(cache, cache_t) - -#if defined(_WIN32) || defined(_MSC_VER) -#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 - -typedef int8_t bgzf_byte_t; - -static const int DEFAULT_BLOCK_SIZE = 64 * 1024; -static const int MAX_BLOCK_SIZE = 64 * 1024; - -static const int BLOCK_HEADER_LENGTH = 18; -static const int BLOCK_FOOTER_LENGTH = 8; - -static const int GZIP_ID1 = 31; -static const int GZIP_ID2 = 139; -static const int CM_DEFLATE = 8; -static const int FLG_FEXTRA = 4; -static const int OS_UNKNOWN = 255; -static const int BGZF_ID1 = 66; // 'B' -static const int BGZF_ID2 = 67; // 'C' -static const int BGZF_LEN = 2; -static const int BGZF_XLEN = 6; // BGZF_LEN+4 - -static const int GZIP_WINDOW_BITS = -15; // no zlib header -static const int Z_DEFAULT_MEM_LEVEL = 8; - - -inline -void -packInt16(uint8_t* buffer, uint16_t value) -{ - buffer[0] = value; - buffer[1] = value >> 8; -} - -inline -int -unpackInt16(const uint8_t* buffer) -{ - return (buffer[0] | (buffer[1] << 8)); -} - -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 inline -int -bgzf_min(int x, int y) -{ - return (x < y) ? x : y; -} - -static -void -report_error(BGZF* fp, const char* message) { - fp->error = message; -} - -static BGZF *bgzf_read_init() -{ - BGZF *fp; - fp = calloc(1, sizeof(BGZF)); - fp->uncompressed_block_size = MAX_BLOCK_SIZE; - fp->uncompressed_block = malloc(MAX_BLOCK_SIZE); - fp->compressed_block_size = MAX_BLOCK_SIZE; - fp->compressed_block = malloc(MAX_BLOCK_SIZE); - fp->cache_size = 0; - fp->cache = kh_init(cache); - return fp; -} - -static -BGZF* -open_read(int fd) -{ -#ifdef _USE_KNETFILE - knetFile *file = knet_dopen(fd, "r"); -#else - FILE* file = fdopen(fd, "r"); -#endif - BGZF* fp; - if (file == 0) return 0; - fp = bgzf_read_init(); - fp->file_descriptor = fd; - fp->open_mode = 'r'; -#ifdef _USE_KNETFILE - fp->x.fpr = file; -#else - fp->file = file; -#endif - return fp; -} - -static -BGZF* -open_write(int fd, bool is_uncompressed) -{ - FILE* file = fdopen(fd, "w"); - BGZF* fp; - if (file == 0) return 0; - fp = malloc(sizeof(BGZF)); - fp->file_descriptor = fd; - fp->open_mode = 'w'; - fp->owned_file = 0; fp->is_uncompressed = is_uncompressed; -#ifdef _USE_KNETFILE - fp->x.fpw = file; -#else - fp->file = file; -#endif - fp->uncompressed_block_size = DEFAULT_BLOCK_SIZE; - fp->uncompressed_block = NULL; - fp->compressed_block_size = MAX_BLOCK_SIZE; - fp->compressed_block = malloc(MAX_BLOCK_SIZE); - fp->block_address = 0; - fp->block_offset = 0; - fp->block_length = 0; - fp->error = NULL; - return fp; -} - -BGZF* -bgzf_open(const char* __restrict path, const char* __restrict mode) -{ - BGZF* fp = NULL; - if (mode[0] == 'r' || mode[0] == 'R') { /* The reading mode is preferred. */ -#ifdef _USE_KNETFILE - knetFile *file = knet_open(path, mode); - if (file == 0) return 0; - fp = bgzf_read_init(); - fp->file_descriptor = -1; - fp->open_mode = 'r'; - fp->x.fpr = file; -#else - int fd, oflag = O_RDONLY; -#ifdef _WIN32 - oflag |= O_BINARY; -#endif - fd = open(path, oflag); - if (fd == -1) return 0; - fp = open_read(fd); -#endif - } else if (mode[0] == 'w' || mode[0] == 'W') { - int fd, oflag = O_WRONLY | O_CREAT | O_TRUNC; -#ifdef _WIN32 - oflag |= O_BINARY; -#endif - fd = open(path, oflag, 0666); - if (fd == -1) return 0; - fp = open_write(fd, strstr(mode, "u")? 1 : 0); - } - if (fp != NULL) fp->owned_file = 1; - return fp; -} - -BGZF* -bgzf_fdopen(int fd, const char * __restrict mode) -{ - if (fd == -1) return 0; - if (mode[0] == 'r' || mode[0] == 'R') { - return open_read(fd); - } else if (mode[0] == 'w' || mode[0] == 'W') { - return open_write(fd, strstr(mode, "u")? 1 : 0); - } else { - return NULL; - } -} - -static -int -deflate_block(BGZF* fp, int block_length) -{ - // Deflate the block in fp->uncompressed_block into fp->compressed_block. - // Also adds an extra field that stores the compressed block length. - - bgzf_byte_t* buffer = fp->compressed_block; - int buffer_size = fp->compressed_block_size; - - // Init gzip header - buffer[0] = GZIP_ID1; - buffer[1] = GZIP_ID2; - buffer[2] = CM_DEFLATE; - buffer[3] = FLG_FEXTRA; - buffer[4] = 0; // mtime - buffer[5] = 0; - buffer[6] = 0; - buffer[7] = 0; - buffer[8] = 0; - buffer[9] = OS_UNKNOWN; - buffer[10] = BGZF_XLEN; - buffer[11] = 0; - buffer[12] = BGZF_ID1; - buffer[13] = BGZF_ID2; - buffer[14] = BGZF_LEN; - buffer[15] = 0; - buffer[16] = 0; // placeholder for block length - buffer[17] = 0; - - // loop to retry for blocks that do not compress enough - int input_length = block_length; - int compressed_length = 0; - while (1) { - int compress_level = fp->is_uncompressed? 0 : Z_DEFAULT_COMPRESSION; - z_stream zs; - zs.zalloc = NULL; - zs.zfree = NULL; - zs.next_in = fp->uncompressed_block; - zs.avail_in = input_length; - zs.next_out = (void*)&buffer[BLOCK_HEADER_LENGTH]; - zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH; - - int status = deflateInit2(&zs, compress_level, Z_DEFLATED, - GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY); - if (status != Z_OK) { - report_error(fp, "deflate init failed"); - return -1; - } - status = deflate(&zs, Z_FINISH); - if (status != Z_STREAM_END) { - deflateEnd(&zs); - if (status == Z_OK) { - // Not enough space in buffer. - // Can happen in the rare case the input doesn't compress enough. - // Reduce the amount of input until it fits. - input_length -= 1024; - if (input_length <= 0) { - // should never happen - report_error(fp, "input reduction failed"); - return -1; - } - continue; - } - report_error(fp, "deflate failed"); - return -1; - } - status = deflateEnd(&zs); - if (status != Z_OK) { - report_error(fp, "deflate end failed"); - return -1; - } - compressed_length = zs.total_out; - compressed_length += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH; - if (compressed_length > MAX_BLOCK_SIZE) { - // should never happen - report_error(fp, "deflate overflow"); - return -1; - } - break; - } - - packInt16((uint8_t*)&buffer[16], compressed_length-1); - uint32_t crc = crc32(0L, NULL, 0L); - crc = crc32(crc, fp->uncompressed_block, input_length); - packInt32((uint8_t*)&buffer[compressed_length-8], crc); - packInt32((uint8_t*)&buffer[compressed_length-4], input_length); - - int remaining = block_length - input_length; - if (remaining > 0) { - if (remaining > input_length) { - // should never happen (check so we can use memcpy) - report_error(fp, "remainder too large"); - return -1; - } - memcpy(fp->uncompressed_block, - fp->uncompressed_block + input_length, - remaining); - } - fp->block_offset = remaining; - return compressed_length; -} - -static -int -inflate_block(BGZF* fp, int block_length) -{ - // Inflate the block in fp->compressed_block into fp->uncompressed_block - - 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 = fp->uncompressed_block_size; - - int status = inflateInit2(&zs, GZIP_WINDOW_BITS); - if (status != Z_OK) { - report_error(fp, "inflate init failed"); - return -1; - } - status = inflate(&zs, Z_FINISH); - if (status != Z_STREAM_END) { - inflateEnd(&zs); - report_error(fp, "inflate failed"); - return -1; - } - status = inflateEnd(&zs); - if (status != Z_OK) { - report_error(fp, "inflate failed"); - return -1; - } - return zs.total_out; -} - -static -int -check_header(const bgzf_byte_t* header) -{ - return (header[0] == GZIP_ID1 && - header[1] == (bgzf_byte_t) GZIP_ID2 && - header[2] == Z_DEFLATED && - (header[3] & FLG_FEXTRA) != 0 && - unpackInt16((uint8_t*)&header[10]) == BGZF_XLEN && - header[12] == BGZF_ID1 && - header[13] == BGZF_ID2 && - unpackInt16((uint8_t*)&header[14]) == BGZF_LEN); -} - -static void free_cache(BGZF *fp) -{ - khint_t k; - khash_t(cache) *h = (khash_t(cache)*)fp->cache; - if (fp->open_mode != 'r') 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, MAX_BLOCK_SIZE); -#ifdef _USE_KNETFILE - knet_seek(fp->x.fpr, p->end_offset, SEEK_SET); -#else - fseeko(fp->file, p->end_offset, SEEK_SET); -#endif - 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 (MAX_BLOCK_SIZE >= fp->cache_size) return; - if ((kh_size(h) + 1) * 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(MAX_BLOCK_SIZE); - memcpy(kh_val(h, k).block, fp->uncompressed_block, MAX_BLOCK_SIZE); -} - -int -bgzf_read_block(BGZF* fp) -{ - bgzf_byte_t header[BLOCK_HEADER_LENGTH]; - int count, size = 0; -#ifdef _USE_KNETFILE - int64_t block_address = knet_tell(fp->x.fpr); - if (load_block_from_cache(fp, block_address)) return 0; - count = knet_read(fp->x.fpr, header, sizeof(header)); -#else - int64_t block_address = ftello(fp->file); - if (load_block_from_cache(fp, block_address)) return 0; - count = fread(header, 1, sizeof(header), fp->file); -#endif - if (count == 0) { - fp->block_length = 0; - return 0; - } - size = count; - if (count != sizeof(header)) { - report_error(fp, "read failed"); - return -1; - } - if (!check_header(header)) { - report_error(fp, "invalid block header"); - return -1; - } - int block_length = unpackInt16((uint8_t*)&header[16]) + 1; - bgzf_byte_t* compressed_block = (bgzf_byte_t*) fp->compressed_block; - memcpy(compressed_block, header, BLOCK_HEADER_LENGTH); - int remaining = block_length - BLOCK_HEADER_LENGTH; -#ifdef _USE_KNETFILE - count = knet_read(fp->x.fpr, &compressed_block[BLOCK_HEADER_LENGTH], remaining); -#else - count = fread(&compressed_block[BLOCK_HEADER_LENGTH], 1, remaining, fp->file); -#endif - if (count != remaining) { - report_error(fp, "read failed"); - return -1; - } - size += count; - count = inflate_block(fp, block_length); - if (count < 0) return -1; - if (fp->block_length != 0) { - // Do not reset offset if this read follows a seek. - fp->block_offset = 0; - } - fp->block_address = block_address; - fp->block_length = count; - cache_block(fp, size); - return 0; -} - -int -bgzf_read(BGZF* fp, void* data, int length) -{ - if (length <= 0) { - return 0; - } - if (fp->open_mode != 'r') { - report_error(fp, "file not open for reading"); - return -1; - } - - int bytes_read = 0; - bgzf_byte_t* output = data; - while (bytes_read < length) { - int available = fp->block_length - fp->block_offset; - if (available <= 0) { - if (bgzf_read_block(fp) != 0) { - return -1; - } - available = fp->block_length - fp->block_offset; - if (available <= 0) { - break; - } - } - int copy_length = bgzf_min(length-bytes_read, available); - bgzf_byte_t* 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) { -#ifdef _USE_KNETFILE - fp->block_address = knet_tell(fp->x.fpr); -#else - fp->block_address = ftello(fp->file); -#endif - fp->block_offset = 0; - fp->block_length = 0; - } - return bytes_read; -} - -int bgzf_flush(BGZF* fp) -{ - while (fp->block_offset > 0) { - int count, block_length; - block_length = deflate_block(fp, fp->block_offset); - if (block_length < 0) return -1; -#ifdef _USE_KNETFILE - count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw); -#else - count = fwrite(fp->compressed_block, 1, block_length, fp->file); -#endif - if (count != block_length) { - report_error(fp, "write failed"); - return -1; - } - fp->block_address += block_length; - } - return 0; -} - -int bgzf_flush_try(BGZF *fp, int size) -{ - if (fp->block_offset + size > fp->uncompressed_block_size) - return bgzf_flush(fp); - return -1; -} - -int bgzf_write(BGZF* fp, const void* data, int length) -{ - if (fp->open_mode != 'w') { - report_error(fp, "file not open for writing"); - return -1; - } - - if (fp->uncompressed_block == NULL) - fp->uncompressed_block = malloc(fp->uncompressed_block_size); - - const bgzf_byte_t* input = data; - int block_length = fp->uncompressed_block_size; - int bytes_written = 0; - while (bytes_written < length) { - int copy_length = bgzf_min(block_length - fp->block_offset, length - bytes_written); - bgzf_byte_t* buffer = fp->uncompressed_block; - 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) { - if (bgzf_flush(fp) != 0) { - break; - } - } - } - return bytes_written; -} - -int bgzf_close(BGZF* fp) -{ - if (fp->open_mode == 'w') { - if (bgzf_flush(fp) != 0) return -1; - { // add an empty block - int count, block_length = deflate_block(fp, 0); -#ifdef _USE_KNETFILE - count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw); -#else - count = fwrite(fp->compressed_block, 1, block_length, fp->file); -#endif - } -#ifdef _USE_KNETFILE - if (fflush(fp->x.fpw) != 0) { -#else - if (fflush(fp->file) != 0) { -#endif - report_error(fp, "flush failed"); - return -1; - } - } - if (fp->owned_file) { -#ifdef _USE_KNETFILE - int ret; - if (fp->open_mode == 'w') ret = fclose(fp->x.fpw); - else ret = knet_close(fp->x.fpr); - if (ret != 0) return -1; -#else - if (fclose(fp->file) != 0) return -1; -#endif - } - 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; -#ifdef _USE_KNETFILE - offset = knet_tell(fp->x.fpr); - if (knet_seek(fp->x.fpr, -28, SEEK_END) != 0) return -1; - knet_read(fp->x.fpr, buf, 28); - knet_seek(fp->x.fpr, offset, SEEK_SET); -#else - offset = ftello(fp->file); - if (fseeko(fp->file, -28, SEEK_END) != 0) return -1; - fread(buf, 1, 28, fp->file); - fseeko(fp->file, offset, SEEK_SET); -#endif - 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->open_mode != 'r') { - report_error(fp, "file not open for read"); - return -1; - } - if (where != SEEK_SET) { - report_error(fp, "unimplemented seek option"); - return -1; - } - block_offset = pos & 0xFFFF; - block_address = (pos >> 16) & 0xFFFFFFFFFFFFLL; -#ifdef _USE_KNETFILE - if (knet_seek(fp->x.fpr, block_address, SEEK_SET) != 0) { -#else - if (fseeko(fp->file, block_address, SEEK_SET) != 0) { -#endif - report_error(fp, "seek failed"); - return -1; - } - fp->block_length = 0; // indicates current block is not loaded - fp->block_address = block_address; - fp->block_offset = block_offset; - return 0; -} diff --git a/samtools/bgzf.c.pysam.c b/samtools/bgzf.c.pysam.c new file mode 100644 index 0000000..269c271 --- /dev/null +++ b/samtools/bgzf.c.pysam.c @@ -0,0 +1,673 @@ +#include "pysam.h" + +/* The MIT License + + Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology + + 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. +*/ + +/* + 2009-06-29 by lh3: cache recent uncompressed blocks. + 2009-06-25 by lh3: optionally use my knetfile library to access file on a FTP. + 2009-06-12 by lh3: support a mode string like "wu" where 'u' for uncompressed output */ + +#include +#include +#include +#include +#include +#include +#include +#include "bgzf.h" + +#include "khash.h" +typedef struct { + int size; + uint8_t *block; + int64_t end_offset; +} cache_t; +KHASH_MAP_INIT_INT64(cache, cache_t) + +#if defined(_WIN32) || defined(_MSC_VER) +#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 + +typedef int8_t bgzf_byte_t; + +static const int DEFAULT_BLOCK_SIZE = 64 * 1024; +static const int MAX_BLOCK_SIZE = 64 * 1024; + +static const int BLOCK_HEADER_LENGTH = 18; +static const int BLOCK_FOOTER_LENGTH = 8; + +static const int GZIP_ID1 = 31; +static const int GZIP_ID2 = 139; +static const int CM_DEFLATE = 8; +static const int FLG_FEXTRA = 4; +static const int OS_UNKNOWN = 255; +static const int BGZF_ID1 = 66; // 'B' +static const int BGZF_ID2 = 67; // 'C' +static const int BGZF_LEN = 2; +static const int BGZF_XLEN = 6; // BGZF_LEN+4 + +static const int GZIP_WINDOW_BITS = -15; // no zlib header +static const int Z_DEFAULT_MEM_LEVEL = 8; + + +inline +void +packInt16(uint8_t* buffer, uint16_t value) +{ + buffer[0] = value; + buffer[1] = value >> 8; +} + +inline +int +unpackInt16(const uint8_t* buffer) +{ + return (buffer[0] | (buffer[1] << 8)); +} + +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 inline +int +bgzf_min(int x, int y) +{ + return (x < y) ? x : y; +} + +static +void +report_error(BGZF* fp, const char* message) { + fp->error = message; +} + +static BGZF *bgzf_read_init() +{ + BGZF *fp; + fp = calloc(1, sizeof(BGZF)); + fp->uncompressed_block_size = MAX_BLOCK_SIZE; + fp->uncompressed_block = malloc(MAX_BLOCK_SIZE); + fp->compressed_block_size = MAX_BLOCK_SIZE; + fp->compressed_block = malloc(MAX_BLOCK_SIZE); + fp->cache_size = 0; + fp->cache = kh_init(cache); + return fp; +} + +static +BGZF* +open_read(int fd) +{ +#ifdef _USE_KNETFILE + knetFile *file = knet_dopen(fd, "r"); +#else + FILE* file = fdopen(fd, "r"); +#endif + BGZF* fp; + if (file == 0) return 0; + fp = bgzf_read_init(); + fp->file_descriptor = fd; + fp->open_mode = 'r'; +#ifdef _USE_KNETFILE + fp->x.fpr = file; +#else + fp->file = file; +#endif + return fp; +} + +static +BGZF* +open_write(int fd, bool is_uncompressed) +{ + FILE* file = fdopen(fd, "w"); + BGZF* fp; + if (file == 0) return 0; + fp = malloc(sizeof(BGZF)); + fp->file_descriptor = fd; + fp->open_mode = 'w'; + fp->owned_file = 0; fp->is_uncompressed = is_uncompressed; +#ifdef _USE_KNETFILE + fp->x.fpw = file; +#else + fp->file = file; +#endif + fp->uncompressed_block_size = DEFAULT_BLOCK_SIZE; + fp->uncompressed_block = NULL; + fp->compressed_block_size = MAX_BLOCK_SIZE; + fp->compressed_block = malloc(MAX_BLOCK_SIZE); + fp->block_address = 0; + fp->block_offset = 0; + fp->block_length = 0; + fp->error = NULL; + return fp; +} + +BGZF* +bgzf_open(const char* __restrict path, const char* __restrict mode) +{ + BGZF* fp = NULL; + if (strchr(mode, 'r') || strchr(mode, 'R')) { /* The reading mode is preferred. */ +#ifdef _USE_KNETFILE + knetFile *file = knet_open(path, mode); + if (file == 0) return 0; + fp = bgzf_read_init(); + fp->file_descriptor = -1; + fp->open_mode = 'r'; + fp->x.fpr = file; +#else + int fd, oflag = O_RDONLY; +#ifdef _WIN32 + oflag |= O_BINARY; +#endif + fd = open(path, oflag); + if (fd == -1) return 0; + fp = open_read(fd); +#endif + } else if (strchr(mode, 'w') || strchr(mode, 'W')) { + int fd, oflag = O_WRONLY | O_CREAT | O_TRUNC; +#ifdef _WIN32 + oflag |= O_BINARY; +#endif + fd = open(path, oflag, 0666); + if (fd == -1) return 0; + fp = open_write(fd, strchr(mode, 'u')? 1 : 0); + } + if (fp != NULL) fp->owned_file = 1; + return fp; +} + +BGZF* +bgzf_fdopen(int fd, const char * __restrict mode) +{ + if (fd == -1) return 0; + if (mode[0] == 'r' || mode[0] == 'R') { + return open_read(fd); + } else if (mode[0] == 'w' || mode[0] == 'W') { + return open_write(fd, strstr(mode, "u")? 1 : 0); + } else { + return NULL; + } +} + +static +int +deflate_block(BGZF* fp, int block_length) +{ + // Deflate the block in fp->uncompressed_block into fp->compressed_block. + // Also adds an extra field that stores the compressed block length. + + bgzf_byte_t* buffer = fp->compressed_block; + int buffer_size = fp->compressed_block_size; + + // Init gzip header + buffer[0] = GZIP_ID1; + buffer[1] = GZIP_ID2; + buffer[2] = CM_DEFLATE; + buffer[3] = FLG_FEXTRA; + buffer[4] = 0; // mtime + buffer[5] = 0; + buffer[6] = 0; + buffer[7] = 0; + buffer[8] = 0; + buffer[9] = OS_UNKNOWN; + buffer[10] = BGZF_XLEN; + buffer[11] = 0; + buffer[12] = BGZF_ID1; + buffer[13] = BGZF_ID2; + buffer[14] = BGZF_LEN; + buffer[15] = 0; + buffer[16] = 0; // placeholder for block length + buffer[17] = 0; + + // loop to retry for blocks that do not compress enough + int input_length = block_length; + int compressed_length = 0; + while (1) { + int compress_level = fp->is_uncompressed? 0 : Z_DEFAULT_COMPRESSION; + z_stream zs; + zs.zalloc = NULL; + zs.zfree = NULL; + zs.next_in = fp->uncompressed_block; + zs.avail_in = input_length; + zs.next_out = (void*)&buffer[BLOCK_HEADER_LENGTH]; + zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH; + + int status = deflateInit2(&zs, compress_level, Z_DEFLATED, + GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY); + if (status != Z_OK) { + report_error(fp, "deflate init failed"); + return -1; + } + status = deflate(&zs, Z_FINISH); + if (status != Z_STREAM_END) { + deflateEnd(&zs); + if (status == Z_OK) { + // Not enough space in buffer. + // Can happen in the rare case the input doesn't compress enough. + // Reduce the amount of input until it fits. + input_length -= 1024; + if (input_length <= 0) { + // should never happen + report_error(fp, "input reduction failed"); + return -1; + } + continue; + } + report_error(fp, "deflate failed"); + return -1; + } + status = deflateEnd(&zs); + if (status != Z_OK) { + report_error(fp, "deflate end failed"); + return -1; + } + compressed_length = zs.total_out; + compressed_length += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH; + if (compressed_length > MAX_BLOCK_SIZE) { + // should never happen + report_error(fp, "deflate overflow"); + return -1; + } + break; + } + + packInt16((uint8_t*)&buffer[16], compressed_length-1); + uint32_t crc = crc32(0L, NULL, 0L); + crc = crc32(crc, fp->uncompressed_block, input_length); + packInt32((uint8_t*)&buffer[compressed_length-8], crc); + packInt32((uint8_t*)&buffer[compressed_length-4], input_length); + + int remaining = block_length - input_length; + if (remaining > 0) { + if (remaining > input_length) { + // should never happen (check so we can use memcpy) + report_error(fp, "remainder too large"); + return -1; + } + memcpy(fp->uncompressed_block, + fp->uncompressed_block + input_length, + remaining); + } + fp->block_offset = remaining; + return compressed_length; +} + +static +int +inflate_block(BGZF* fp, int block_length) +{ + // Inflate the block in fp->compressed_block into fp->uncompressed_block + + 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 = fp->uncompressed_block_size; + + int status = inflateInit2(&zs, GZIP_WINDOW_BITS); + if (status != Z_OK) { + report_error(fp, "inflate init failed"); + return -1; + } + status = inflate(&zs, Z_FINISH); + if (status != Z_STREAM_END) { + inflateEnd(&zs); + report_error(fp, "inflate failed"); + return -1; + } + status = inflateEnd(&zs); + if (status != Z_OK) { + report_error(fp, "inflate failed"); + return -1; + } + return zs.total_out; +} + +static +int +check_header(const bgzf_byte_t* header) +{ + return (header[0] == GZIP_ID1 && + header[1] == (bgzf_byte_t) GZIP_ID2 && + header[2] == Z_DEFLATED && + (header[3] & FLG_FEXTRA) != 0 && + unpackInt16((uint8_t*)&header[10]) == BGZF_XLEN && + header[12] == BGZF_ID1 && + header[13] == BGZF_ID2 && + unpackInt16((uint8_t*)&header[14]) == BGZF_LEN); +} + +static void free_cache(BGZF *fp) +{ + khint_t k; + khash_t(cache) *h = (khash_t(cache)*)fp->cache; + if (fp->open_mode != 'r') 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, MAX_BLOCK_SIZE); +#ifdef _USE_KNETFILE + knet_seek(fp->x.fpr, p->end_offset, SEEK_SET); +#else + fseeko(fp->file, p->end_offset, SEEK_SET); +#endif + 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 (MAX_BLOCK_SIZE >= fp->cache_size) return; + if ((kh_size(h) + 1) * 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(MAX_BLOCK_SIZE); + memcpy(kh_val(h, k).block, fp->uncompressed_block, MAX_BLOCK_SIZE); +} + +int +bgzf_read_block(BGZF* fp) +{ + bgzf_byte_t header[BLOCK_HEADER_LENGTH]; + int count, size = 0; +#ifdef _USE_KNETFILE + int64_t block_address = knet_tell(fp->x.fpr); + if (load_block_from_cache(fp, block_address)) return 0; + count = knet_read(fp->x.fpr, header, sizeof(header)); +#else + int64_t block_address = ftello(fp->file); + if (load_block_from_cache(fp, block_address)) return 0; + count = fread(header, 1, sizeof(header), fp->file); +#endif + if (count == 0) { + fp->block_length = 0; + return 0; + } + size = count; + if (count != sizeof(header)) { + report_error(fp, "read failed"); + return -1; + } + if (!check_header(header)) { + report_error(fp, "invalid block header"); + return -1; + } + int block_length = unpackInt16((uint8_t*)&header[16]) + 1; + bgzf_byte_t* compressed_block = (bgzf_byte_t*) fp->compressed_block; + memcpy(compressed_block, header, BLOCK_HEADER_LENGTH); + int remaining = block_length - BLOCK_HEADER_LENGTH; +#ifdef _USE_KNETFILE + count = knet_read(fp->x.fpr, &compressed_block[BLOCK_HEADER_LENGTH], remaining); +#else + count = fread(&compressed_block[BLOCK_HEADER_LENGTH], 1, remaining, fp->file); +#endif + if (count != remaining) { + report_error(fp, "read failed"); + return -1; + } + size += count; + count = inflate_block(fp, block_length); + if (count < 0) return -1; + if (fp->block_length != 0) { + // Do not reset offset if this read follows a seek. + fp->block_offset = 0; + } + fp->block_address = block_address; + fp->block_length = count; + cache_block(fp, size); + return 0; +} + +int +bgzf_read(BGZF* fp, void* data, int length) +{ + if (length <= 0) { + return 0; + } + if (fp->open_mode != 'r') { + report_error(fp, "file not open for reading"); + return -1; + } + + int bytes_read = 0; + bgzf_byte_t* output = data; + while (bytes_read < length) { + int available = fp->block_length - fp->block_offset; + if (available <= 0) { + if (bgzf_read_block(fp) != 0) { + return -1; + } + available = fp->block_length - fp->block_offset; + if (available <= 0) { + break; + } + } + int copy_length = bgzf_min(length-bytes_read, available); + bgzf_byte_t* 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) { +#ifdef _USE_KNETFILE + fp->block_address = knet_tell(fp->x.fpr); +#else + fp->block_address = ftello(fp->file); +#endif + fp->block_offset = 0; + fp->block_length = 0; + } + return bytes_read; +} + +int bgzf_flush(BGZF* fp) +{ + while (fp->block_offset > 0) { + int count, block_length; + block_length = deflate_block(fp, fp->block_offset); + if (block_length < 0) return -1; +#ifdef _USE_KNETFILE + count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw); +#else + count = fwrite(fp->compressed_block, 1, block_length, fp->file); +#endif + if (count != block_length) { + report_error(fp, "write failed"); + return -1; + } + fp->block_address += block_length; + } + return 0; +} + +int bgzf_flush_try(BGZF *fp, int size) +{ + if (fp->block_offset + size > fp->uncompressed_block_size) + return bgzf_flush(fp); + return -1; +} + +int bgzf_write(BGZF* fp, const void* data, int length) +{ + if (fp->open_mode != 'w') { + report_error(fp, "file not open for writing"); + return -1; + } + + if (fp->uncompressed_block == NULL) + fp->uncompressed_block = malloc(fp->uncompressed_block_size); + + const bgzf_byte_t* input = data; + int block_length = fp->uncompressed_block_size; + int bytes_written = 0; + while (bytes_written < length) { + int copy_length = bgzf_min(block_length - fp->block_offset, length - bytes_written); + bgzf_byte_t* buffer = fp->uncompressed_block; + 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) { + if (bgzf_flush(fp) != 0) { + break; + } + } + } + return bytes_written; +} + +int bgzf_close(BGZF* fp) +{ + if (fp->open_mode == 'w') { + if (bgzf_flush(fp) != 0) return -1; + { // add an empty block + int count, block_length = deflate_block(fp, 0); +#ifdef _USE_KNETFILE + count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw); +#else + count = fwrite(fp->compressed_block, 1, block_length, fp->file); +#endif + } +#ifdef _USE_KNETFILE + if (fflush(fp->x.fpw) != 0) { +#else + if (fflush(fp->file) != 0) { +#endif + report_error(fp, "flush failed"); + return -1; + } + } + if (fp->owned_file) { +#ifdef _USE_KNETFILE + int ret; + if (fp->open_mode == 'w') ret = fclose(fp->x.fpw); + else ret = knet_close(fp->x.fpr); + if (ret != 0) return -1; +#else + if (fclose(fp->file) != 0) return -1; +#endif + } + 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; +#ifdef _USE_KNETFILE + offset = knet_tell(fp->x.fpr); + if (knet_seek(fp->x.fpr, -28, SEEK_END) != 0) return -1; + knet_read(fp->x.fpr, buf, 28); + knet_seek(fp->x.fpr, offset, SEEK_SET); +#else + offset = ftello(fp->file); + if (fseeko(fp->file, -28, SEEK_END) != 0) return -1; + fread(buf, 1, 28, fp->file); + fseeko(fp->file, offset, SEEK_SET); +#endif + 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->open_mode != 'r') { + report_error(fp, "file not open for read"); + return -1; + } + if (where != SEEK_SET) { + report_error(fp, "unimplemented seek option"); + return -1; + } + block_offset = pos & 0xFFFF; + block_address = (pos >> 16) & 0xFFFFFFFFFFFFLL; +#ifdef _USE_KNETFILE + if (knet_seek(fp->x.fpr, block_address, SEEK_SET) != 0) { +#else + if (fseeko(fp->file, block_address, SEEK_SET) != 0) { +#endif + report_error(fp, "seek failed"); + return -1; + } + fp->block_length = 0; // indicates current block is not loaded + fp->block_address = block_address; + fp->block_offset = block_offset; + return 0; +} diff --git a/samtools/errmod.c.pysam.c b/samtools/errmod.c.pysam.c new file mode 100644 index 0000000..bf62418 --- /dev/null +++ b/samtools/errmod.c.pysam.c @@ -0,0 +1,132 @@ +#include "pysam.h" + +#include +#include "errmod.h" +#include "ksort.h" +KSORT_INIT_GENERIC(uint16_t) + +typedef struct __errmod_coef_t { + double *fk, *beta, *lhet; +} errmod_coef_t; + +typedef struct { + double fsum[16], bsum[16]; + uint32_t c[16]; +} call_aux_t; + +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; +} + +errmod_t *errmod_init(float depcorr) +{ + errmod_t *em; + em = (errmod_t*)calloc(1, sizeof(errmod_t)); + em->depcorr = depcorr; + em->coef = cal_coef(depcorr, 0.03); + return em; +} + +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); +} +// qual:6, strand:1, base:4 +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; +} diff --git a/samtools/errmod.h b/samtools/errmod.h new file mode 100644 index 0000000..e3e9a90 --- /dev/null +++ b/samtools/errmod.h @@ -0,0 +1,17 @@ +#ifndef ERRMOD_H +#define ERRMOD_H + +#include + +struct __errmod_coef_t; + +typedef struct { + double depcorr; + struct __errmod_coef_t *coef; +} errmod_t; + +errmod_t *errmod_init(float depcorr); +void errmod_destroy(errmod_t *em); +int errmod_cal(const errmod_t *em, int n, int m, uint16_t *bases, float *q); + +#endif diff --git a/samtools/faidx.c b/samtools/faidx.c deleted file mode 100644 index dbd8b3e..0000000 --- a/samtools/faidx.c +++ /dev/null @@ -1,422 +0,0 @@ -#include -#include -#include -#include -#include "faidx.h" -#include "khash.h" - -typedef struct { - uint64_t len:32, line_len:16, line_blen:16; - uint64_t offset; -} faidx1_t; -KHASH_MAP_INIT_STR(s, faidx1_t) - -#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 - -struct __faidx_t { - RAZF *rz; - int n, m; - char **name; - khash_t(s) *hash; -}; - -#ifndef kroundup32 -#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) -#endif - -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; -} - -faidx_t *fai_build_core(RAZF *rz) -{ - char c, *name; - int l_name, m_name, ret; - int len, line_len, line_blen, state; - int l1, l2; - faidx_t *idx; - uint64_t offset; - - 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(stderr, "[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(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 ((ret = razf_read(rz, &c, 1)) && 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 (l2 >= 0x10000) { - fprintf(stderr, "[fai_build_core] line length exceeds 65535 in sequence '%s'.\n", name); - free(name); fai_destroy(idx); - return 0; - } - 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; -} - -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->rz) razf_close(fai->rz); - free(fai); -} - -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(stderr, "[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(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 -FILE *download_and_open(const char *fn) -{ - 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(stderr, "[download_from_remote] fail to open remote file %s\n",url); - return NULL; - } - if ((fp = fopen(fn, "wb")) == 0) { - fprintf(stderr, "[download_from_remote] fail to create file in the working directory %s\n",fn); - knet_close(fp_remote); - return NULL; - } - 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); - -#ifdef _USE_KNETFILE - if (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn) - { - fp = download_and_open(str); - if ( !fp ) - { - fprintf(stderr, "[fai_load] failed to open remote FASTA index %s\n", str); - free(str); - return 0; - } - } - 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->rz = razf_open(fn, "rb"); - free(str); - if (fai->rz == 0) { - fprintf(stderr, "[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, *p, c; - int i, l, k; - khiter_t iter; - faidx1_t val; - khash_t(s) *h; - int beg, end; - - beg = end = -1; - h = fai->hash; - 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; - iter = kh_get(s, h, s); /* get the ref_id */ - if (iter == kh_end(h)) { - *len = 0; - free(s); return 0; - } - val = kh_value(h, iter); - if (i == k) { /* dump the whole sequence */ - beg = 0; end = val.len; - } else { - for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break; - beg = atoi(p); - if (i < k) { - p = s + i + 1; - end = atoi(p); - } else end = val.len; - } - if (beg > 0) --beg; - 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(stderr, "Usage: faidx [ [...]]\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; - - // 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; -} - -#ifdef FAIDX_MAIN -int main(int argc, char *argv[]) { return faidx_main(argc, argv); } -#endif diff --git a/samtools/faidx.c.pysam.c b/samtools/faidx.c.pysam.c new file mode 100644 index 0000000..b4ce846 --- /dev/null +++ b/samtools/faidx.c.pysam.c @@ -0,0 +1,424 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include "faidx.h" +#include "khash.h" + +typedef struct { + uint64_t len:32, line_len:16, line_blen:16; + uint64_t offset; +} faidx1_t; +KHASH_MAP_INIT_STR(s, faidx1_t) + +#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 + +struct __faidx_t { + RAZF *rz; + int n, m; + char **name; + khash_t(s) *hash; +}; + +#ifndef kroundup32 +#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) +#endif + +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; +} + +faidx_t *fai_build_core(RAZF *rz) +{ + char c, *name; + int l_name, m_name, ret; + int len, line_len, line_blen, state; + int l1, l2; + faidx_t *idx; + uint64_t offset; + + 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 (l2 >= 0x10000) { + fprintf(pysamerr, "[fai_build_core] line length exceeds 65535 in sequence '%s'.\n", name); + free(name); fai_destroy(idx); + return 0; + } + 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; +} + +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->rz) razf_close(fai->rz); + free(fai); +} + +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) +{ + 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; + } + 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; + } + 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); + +#ifdef _USE_KNETFILE + if (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn) + { + fp = download_and_open(str); + if ( !fp ) + { + fprintf(pysamerr, "[fai_load] failed to open remote FASTA index %s\n", str); + free(str); + return 0; + } + } + 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, *p, c; + int i, l, k; + khiter_t iter; + faidx1_t val; + khash_t(s) *h; + int beg, end; + + beg = end = -1; + h = fai->hash; + 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; + iter = kh_get(s, h, s); /* get the ref_id */ + if (iter == kh_end(h)) { + *len = 0; + free(s); return 0; + } + val = kh_value(h, iter); + if (i == k) { /* dump the whole sequence */ + beg = 0; end = val.len; + } else { + for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break; + beg = atoi(p); + if (i < k) { + p = s + i + 1; + end = atoi(p); + } else end = val.len; + } + if (beg > 0) --beg; + 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 [ [...]]\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; + + // 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; +} + +#ifdef FAIDX_MAIN +int main(int argc, char *argv[]) { return faidx_main(argc, argv); } +#endif diff --git a/samtools/glf.c b/samtools/glf.c deleted file mode 100644 index 8d5346a..0000000 --- a/samtools/glf.c +++ /dev/null @@ -1,236 +0,0 @@ -#include -#include -#include "glf.h" - -#ifdef _NO_BGZF -// then alias bgzf_*() functions -#endif - -static int glf3_is_BE = 0; - -static inline uint32_t bam_swap_endian_4(uint32_t v) -{ - v = ((v & 0x0000FFFFU) << 16) | (v >> 16); - return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8); -} - -static inline uint16_t bam_swap_endian_2(uint16_t v) -{ - return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8)); -} - -static inline int bam_is_big_endian() -{ - long one= 1; - return !(*((char *)(&one))); -} - -glf3_header_t *glf3_header_init() -{ - glf3_is_BE = bam_is_big_endian(); - return (glf3_header_t*)calloc(1, sizeof(glf3_header_t)); -} - -glf3_header_t *glf3_header_read(glfFile fp) -{ - glf3_header_t *h; - char magic[4]; - h = glf3_header_init(); - bgzf_read(fp, magic, 4); - if (strncmp(magic, "GLF\3", 4)) { - fprintf(stderr, "[glf3_header_read] invalid magic.\n"); - glf3_header_destroy(h); - return 0; - } - bgzf_read(fp, &h->l_text, 4); - if (glf3_is_BE) h->l_text = bam_swap_endian_4(h->l_text); - if (h->l_text) { - h->text = (uint8_t*)calloc(h->l_text + 1, 1); - bgzf_read(fp, h->text, h->l_text); - } - return h; -} - -void glf3_header_write(glfFile fp, const glf3_header_t *h) -{ - int32_t x; - bgzf_write(fp, "GLF\3", 4); - x = glf3_is_BE? bam_swap_endian_4(h->l_text) : h->l_text; - bgzf_write(fp, &x, 4); - if (h->l_text) bgzf_write(fp, h->text, h->l_text); -} - -void glf3_header_destroy(glf3_header_t *h) -{ - free(h->text); - free(h); -} - -char *glf3_ref_read(glfFile fp, int *len) -{ - int32_t n, x; - char *str; - *len = 0; - if (bgzf_read(fp, &n, 4) != 4) return 0; - if (glf3_is_BE) n = bam_swap_endian_4(n); - if (n < 0) { - fprintf(stderr, "[glf3_ref_read] invalid reference name length: %d.\n", n); - return 0; - } - str = (char*)calloc(n + 1, 1); // not necesarily n+1 in fact - x = bgzf_read(fp, str, n); - x += bgzf_read(fp, len, 4); - if (x != n + 4) { - free(str); *len = -1; return 0; // truncated - } - if (glf3_is_BE) *len = bam_swap_endian_4(*len); - return str; -} - -void glf3_ref_write(glfFile fp, const char *str, int len) -{ - int32_t m, n = strlen(str) + 1; - m = glf3_is_BE? bam_swap_endian_4(n) : n; - bgzf_write(fp, &m, 4); - bgzf_write(fp, str, n); - if (glf3_is_BE) len = bam_swap_endian_4(len); - bgzf_write(fp, &len, 4); -} - -void glf3_view1(const char *ref_name, const glf3_t *g3, int pos) -{ - int j; - if (g3->rtype == GLF3_RTYPE_END) return; - printf("%s\t%d\t%c\t%d\t%d\t%d", ref_name, pos + 1, - g3->rtype == GLF3_RTYPE_INDEL? '*' : "XACMGRSVTWYHKDBN"[g3->ref_base], - g3->depth, g3->rms_mapQ, g3->min_lk); - if (g3->rtype == GLF3_RTYPE_SUB) - for (j = 0; j != 10; ++j) printf("\t%d", g3->lk[j]); - else { - printf("\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t", g3->lk[0], g3->lk[1], g3->lk[2], g3->indel_len[0], g3->indel_len[1], - g3->indel_len[0]? g3->indel_seq[0] : "*", g3->indel_len[1]? g3->indel_seq[1] : "*"); - } - printf("\n"); -} - -int glf3_write1(glfFile fp, const glf3_t *g3) -{ - int r; - uint8_t c; - uint32_t y[2]; - c = g3->rtype<<4 | g3->ref_base; - r = bgzf_write(fp, &c, 1); - if (g3->rtype == GLF3_RTYPE_END) return r; - y[0] = g3->offset; - y[1] = g3->min_lk<<24 | g3->depth; - if (glf3_is_BE) { - y[0] = bam_swap_endian_4(y[0]); - y[1] = bam_swap_endian_4(y[1]); - } - r += bgzf_write(fp, y, 8); - r += bgzf_write(fp, &g3->rms_mapQ, 1); - if (g3->rtype == GLF3_RTYPE_SUB) r += bgzf_write(fp, g3->lk, 10); - else { - int16_t x[2]; - r += bgzf_write(fp, g3->lk, 3); - x[0] = glf3_is_BE? bam_swap_endian_2(g3->indel_len[0]) : g3->indel_len[0]; - x[1] = glf3_is_BE? bam_swap_endian_2(g3->indel_len[1]) : g3->indel_len[1]; - r += bgzf_write(fp, x, 4); - if (g3->indel_len[0]) r += bgzf_write(fp, g3->indel_seq[0], abs(g3->indel_len[0])); - if (g3->indel_len[1]) r += bgzf_write(fp, g3->indel_seq[1], abs(g3->indel_len[1])); - } - return r; -} - -#ifndef kv_roundup32 -#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) -#endif - -int glf3_read1(glfFile fp, glf3_t *g3) -{ - int r; - uint8_t c; - uint32_t y[2]; - r = bgzf_read(fp, &c, 1); - if (r == 0) return 0; - g3->ref_base = c & 0xf; - g3->rtype = c>>4; - if (g3->rtype == GLF3_RTYPE_END) return r; - r += bgzf_read(fp, y, 8); - if (glf3_is_BE) { - y[0] = bam_swap_endian_4(y[0]); - y[1] = bam_swap_endian_4(y[1]); - } - g3->offset = y[0]; - g3->min_lk = y[1]>>24; - g3->depth = y[1]<<8>>8; - r += bgzf_read(fp, &g3->rms_mapQ, 1); - if (g3->rtype == GLF3_RTYPE_SUB) r += bgzf_read(fp, g3->lk, 10); - else { - int16_t x[2], max; - r += bgzf_read(fp, g3->lk, 3); - r += bgzf_read(fp, x, 4); - if (glf3_is_BE) { - x[0] = bam_swap_endian_2(x[0]); - x[1] = bam_swap_endian_2(x[1]); - } - g3->indel_len[0] = x[0]; - g3->indel_len[1] = x[1]; - x[0] = abs(x[0]); x[1] = abs(x[1]); - max = (x[0] > x[1]? x[0] : x[1]) + 1; - if (g3->max_len < max) { - g3->max_len = max; - kv_roundup32(g3->max_len); - g3->indel_seq[0] = (char*)realloc(g3->indel_seq[0], g3->max_len); - g3->indel_seq[1] = (char*)realloc(g3->indel_seq[1], g3->max_len); - } - r += bgzf_read(fp, g3->indel_seq[0], x[0]); - r += bgzf_read(fp, g3->indel_seq[1], x[1]); - g3->indel_seq[0][x[0]] = g3->indel_seq[1][x[1]] = 0; - } - return r; -} - -void glf3_view(glfFile fp) -{ - glf3_header_t *h; - char *name; - glf3_t *g3; - int len; - h = glf3_header_read(fp); - g3 = glf3_init1(); - while ((name = glf3_ref_read(fp, &len)) != 0) { - int pos = 0; - while (glf3_read1(fp, g3) && g3->rtype != GLF3_RTYPE_END) { - pos += g3->offset; - glf3_view1(name, g3, pos); - } - free(name); - } - glf3_header_destroy(h); - glf3_destroy1(g3); -} - -int glf3_view_main(int argc, char *argv[]) -{ - glfFile fp; - if (argc == 1) { - fprintf(stderr, "Usage: glfview \n"); - return 1; - } - fp = (strcmp(argv[1], "-") == 0)? bgzf_fdopen(fileno(stdin), "r") : bgzf_open(argv[1], "r"); - if (fp == 0) { - fprintf(stderr, "Fail to open file '%s'\n", argv[1]); - return 1; - } - glf3_view(fp); - bgzf_close(fp); - return 0; -} - -#ifdef GLFVIEW_MAIN -int main(int argc, char *argv[]) -{ - return glf3_view_main(argc, argv); -} -#endif diff --git a/samtools/glf.c.pysam.c b/samtools/glf.c.pysam.c new file mode 100644 index 0000000..099d4ac --- /dev/null +++ b/samtools/glf.c.pysam.c @@ -0,0 +1,238 @@ +#include "pysam.h" + +#include +#include +#include "glf.h" + +#ifdef _NO_BGZF +// then alias bgzf_*() functions +#endif + +static int glf3_is_BE = 0; + +static inline uint32_t bam_swap_endian_4(uint32_t v) +{ + v = ((v & 0x0000FFFFU) << 16) | (v >> 16); + return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8); +} + +static inline uint16_t bam_swap_endian_2(uint16_t v) +{ + return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8)); +} + +static inline int bam_is_big_endian() +{ + long one= 1; + return !(*((char *)(&one))); +} + +glf3_header_t *glf3_header_init() +{ + glf3_is_BE = bam_is_big_endian(); + return (glf3_header_t*)calloc(1, sizeof(glf3_header_t)); +} + +glf3_header_t *glf3_header_read(glfFile fp) +{ + glf3_header_t *h; + char magic[4]; + h = glf3_header_init(); + bgzf_read(fp, magic, 4); + if (strncmp(magic, "GLF\3", 4)) { + fprintf(pysamerr, "[glf3_header_read] invalid magic.\n"); + glf3_header_destroy(h); + return 0; + } + bgzf_read(fp, &h->l_text, 4); + if (glf3_is_BE) h->l_text = bam_swap_endian_4(h->l_text); + if (h->l_text) { + h->text = (uint8_t*)calloc(h->l_text + 1, 1); + bgzf_read(fp, h->text, h->l_text); + } + return h; +} + +void glf3_header_write(glfFile fp, const glf3_header_t *h) +{ + int32_t x; + bgzf_write(fp, "GLF\3", 4); + x = glf3_is_BE? bam_swap_endian_4(h->l_text) : h->l_text; + bgzf_write(fp, &x, 4); + if (h->l_text) bgzf_write(fp, h->text, h->l_text); +} + +void glf3_header_destroy(glf3_header_t *h) +{ + free(h->text); + free(h); +} + +char *glf3_ref_read(glfFile fp, int *len) +{ + int32_t n, x; + char *str; + *len = 0; + if (bgzf_read(fp, &n, 4) != 4) return 0; + if (glf3_is_BE) n = bam_swap_endian_4(n); + if (n < 0) { + fprintf(pysamerr, "[glf3_ref_read] invalid reference name length: %d.\n", n); + return 0; + } + str = (char*)calloc(n + 1, 1); // not necesarily n+1 in fact + x = bgzf_read(fp, str, n); + x += bgzf_read(fp, len, 4); + if (x != n + 4) { + free(str); *len = -1; return 0; // truncated + } + if (glf3_is_BE) *len = bam_swap_endian_4(*len); + return str; +} + +void glf3_ref_write(glfFile fp, const char *str, int len) +{ + int32_t m, n = strlen(str) + 1; + m = glf3_is_BE? bam_swap_endian_4(n) : n; + bgzf_write(fp, &m, 4); + bgzf_write(fp, str, n); + if (glf3_is_BE) len = bam_swap_endian_4(len); + bgzf_write(fp, &len, 4); +} + +void glf3_view1(const char *ref_name, const glf3_t *g3, int pos) +{ + int j; + if (g3->rtype == GLF3_RTYPE_END) return; + printf("%s\t%d\t%c\t%d\t%d\t%d", ref_name, pos + 1, + g3->rtype == GLF3_RTYPE_INDEL? '*' : "XACMGRSVTWYHKDBN"[g3->ref_base], + g3->depth, g3->rms_mapQ, g3->min_lk); + if (g3->rtype == GLF3_RTYPE_SUB) + for (j = 0; j != 10; ++j) printf("\t%d", g3->lk[j]); + else { + printf("\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t", g3->lk[0], g3->lk[1], g3->lk[2], g3->indel_len[0], g3->indel_len[1], + g3->indel_len[0]? g3->indel_seq[0] : "*", g3->indel_len[1]? g3->indel_seq[1] : "*"); + } + printf("\n"); +} + +int glf3_write1(glfFile fp, const glf3_t *g3) +{ + int r; + uint8_t c; + uint32_t y[2]; + c = g3->rtype<<4 | g3->ref_base; + r = bgzf_write(fp, &c, 1); + if (g3->rtype == GLF3_RTYPE_END) return r; + y[0] = g3->offset; + y[1] = g3->min_lk<<24 | g3->depth; + if (glf3_is_BE) { + y[0] = bam_swap_endian_4(y[0]); + y[1] = bam_swap_endian_4(y[1]); + } + r += bgzf_write(fp, y, 8); + r += bgzf_write(fp, &g3->rms_mapQ, 1); + if (g3->rtype == GLF3_RTYPE_SUB) r += bgzf_write(fp, g3->lk, 10); + else { + int16_t x[2]; + r += bgzf_write(fp, g3->lk, 3); + x[0] = glf3_is_BE? bam_swap_endian_2(g3->indel_len[0]) : g3->indel_len[0]; + x[1] = glf3_is_BE? bam_swap_endian_2(g3->indel_len[1]) : g3->indel_len[1]; + r += bgzf_write(fp, x, 4); + if (g3->indel_len[0]) r += bgzf_write(fp, g3->indel_seq[0], abs(g3->indel_len[0])); + if (g3->indel_len[1]) r += bgzf_write(fp, g3->indel_seq[1], abs(g3->indel_len[1])); + } + return r; +} + +#ifndef kv_roundup32 +#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) +#endif + +int glf3_read1(glfFile fp, glf3_t *g3) +{ + int r; + uint8_t c; + uint32_t y[2]; + r = bgzf_read(fp, &c, 1); + if (r == 0) return 0; + g3->ref_base = c & 0xf; + g3->rtype = c>>4; + if (g3->rtype == GLF3_RTYPE_END) return r; + r += bgzf_read(fp, y, 8); + if (glf3_is_BE) { + y[0] = bam_swap_endian_4(y[0]); + y[1] = bam_swap_endian_4(y[1]); + } + g3->offset = y[0]; + g3->min_lk = y[1]>>24; + g3->depth = y[1]<<8>>8; + r += bgzf_read(fp, &g3->rms_mapQ, 1); + if (g3->rtype == GLF3_RTYPE_SUB) r += bgzf_read(fp, g3->lk, 10); + else { + int16_t x[2], max; + r += bgzf_read(fp, g3->lk, 3); + r += bgzf_read(fp, x, 4); + if (glf3_is_BE) { + x[0] = bam_swap_endian_2(x[0]); + x[1] = bam_swap_endian_2(x[1]); + } + g3->indel_len[0] = x[0]; + g3->indel_len[1] = x[1]; + x[0] = abs(x[0]); x[1] = abs(x[1]); + max = (x[0] > x[1]? x[0] : x[1]) + 1; + if (g3->max_len < max) { + g3->max_len = max; + kv_roundup32(g3->max_len); + g3->indel_seq[0] = (char*)realloc(g3->indel_seq[0], g3->max_len); + g3->indel_seq[1] = (char*)realloc(g3->indel_seq[1], g3->max_len); + } + r += bgzf_read(fp, g3->indel_seq[0], x[0]); + r += bgzf_read(fp, g3->indel_seq[1], x[1]); + g3->indel_seq[0][x[0]] = g3->indel_seq[1][x[1]] = 0; + } + return r; +} + +void glf3_view(glfFile fp) +{ + glf3_header_t *h; + char *name; + glf3_t *g3; + int len; + h = glf3_header_read(fp); + g3 = glf3_init1(); + while ((name = glf3_ref_read(fp, &len)) != 0) { + int pos = 0; + while (glf3_read1(fp, g3) && g3->rtype != GLF3_RTYPE_END) { + pos += g3->offset; + glf3_view1(name, g3, pos); + } + free(name); + } + glf3_header_destroy(h); + glf3_destroy1(g3); +} + +int glf3_view_main(int argc, char *argv[]) +{ + glfFile fp; + if (argc == 1) { + fprintf(pysamerr, "Usage: glfview \n"); + return 1; + } + fp = (strcmp(argv[1], "-") == 0)? bgzf_fdopen(fileno(stdin), "r") : bgzf_open(argv[1], "r"); + if (fp == 0) { + fprintf(pysamerr, "Fail to open file '%s'\n", argv[1]); + return 1; + } + glf3_view(fp); + bgzf_close(fp); + return 0; +} + +#ifdef GLFVIEW_MAIN +int main(int argc, char *argv[]) +{ + return glf3_view_main(argc, argv); +} +#endif diff --git a/samtools/kaln.c b/samtools/kaln.c deleted file mode 100644 index 9fa40d0..0000000 --- a/samtools/kaln.c +++ /dev/null @@ -1,370 +0,0 @@ -/* The MIT License - - Copyright (c) 2003-2006, 2008, 2009, by Heng Li - - 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 -#include -#include -#include -#include "kaln.h" - -#define FROM_M 0 -#define FROM_I 1 -#define FROM_D 2 - -typedef struct { - int i, j; - unsigned char ctype; -} path_t; - -int aln_sm_blosum62[] = { -/* A R N D C Q E G H I L K M F P S T W Y V * X */ - 4,-1,-2,-2, 0,-1,-1, 0,-2,-1,-1,-1,-1,-2,-1, 1, 0,-3,-2, 0,-4, 0, - -1, 5, 0,-2,-3, 1, 0,-2, 0,-3,-2, 2,-1,-3,-2,-1,-1,-3,-2,-3,-4,-1, - -2, 0, 6, 1,-3, 0, 0, 0, 1,-3,-3, 0,-2,-3,-2, 1, 0,-4,-2,-3,-4,-1, - -2,-2, 1, 6,-3, 0, 2,-1,-1,-3,-4,-1,-3,-3,-1, 0,-1,-4,-3,-3,-4,-1, - 0,-3,-3,-3, 9,-3,-4,-3,-3,-1,-1,-3,-1,-2,-3,-1,-1,-2,-2,-1,-4,-2, - -1, 1, 0, 0,-3, 5, 2,-2, 0,-3,-2, 1, 0,-3,-1, 0,-1,-2,-1,-2,-4,-1, - -1, 0, 0, 2,-4, 2, 5,-2, 0,-3,-3, 1,-2,-3,-1, 0,-1,-3,-2,-2,-4,-1, - 0,-2, 0,-1,-3,-2,-2, 6,-2,-4,-4,-2,-3,-3,-2, 0,-2,-2,-3,-3,-4,-1, - -2, 0, 1,-1,-3, 0, 0,-2, 8,-3,-3,-1,-2,-1,-2,-1,-2,-2, 2,-3,-4,-1, - -1,-3,-3,-3,-1,-3,-3,-4,-3, 4, 2,-3, 1, 0,-3,-2,-1,-3,-1, 3,-4,-1, - -1,-2,-3,-4,-1,-2,-3,-4,-3, 2, 4,-2, 2, 0,-3,-2,-1,-2,-1, 1,-4,-1, - -1, 2, 0,-1,-3, 1, 1,-2,-1,-3,-2, 5,-1,-3,-1, 0,-1,-3,-2,-2,-4,-1, - -1,-1,-2,-3,-1, 0,-2,-3,-2, 1, 2,-1, 5, 0,-2,-1,-1,-1,-1, 1,-4,-1, - -2,-3,-3,-3,-2,-3,-3,-3,-1, 0, 0,-3, 0, 6,-4,-2,-2, 1, 3,-1,-4,-1, - -1,-2,-2,-1,-3,-1,-1,-2,-2,-3,-3,-1,-2,-4, 7,-1,-1,-4,-3,-2,-4,-2, - 1,-1, 1, 0,-1, 0, 0, 0,-1,-2,-2, 0,-1,-2,-1, 4, 1,-3,-2,-2,-4, 0, - 0,-1, 0,-1,-1,-1,-1,-2,-2,-1,-1,-1,-1,-2,-1, 1, 5,-2,-2, 0,-4, 0, - -3,-3,-4,-4,-2,-2,-3,-2,-2,-3,-2,-3,-1, 1,-4,-3,-2,11, 2,-3,-4,-2, - -2,-2,-2,-3,-2,-1,-2,-3, 2,-1,-1,-2,-1, 3,-3,-2,-2, 2, 7,-1,-4,-1, - 0,-3,-3,-3,-1,-2,-2,-3,-3, 3, 1,-2, 1,-1,-2,-2, 0,-3,-1, 4,-4,-1, - -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, 1,-4, - 0,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-2, 0, 0,-2,-1,-1,-4,-1 -}; - -int aln_sm_blast[] = { - 1, -3, -3, -3, -2, - -3, 1, -3, -3, -2, - -3, -3, 1, -3, -2, - -3, -3, -3, 1, -2, - -2, -2, -2, -2, -2 -}; - -ka_param_t ka_param_blast = { 5, 2, 2, aln_sm_blast, 5, 50 }; -ka_param_t ka_param_aa2aa = { 10, 2, 2, aln_sm_blosum62, 22, 50 }; - -static uint32_t *ka_path2cigar32(const path_t *path, int path_len, int *n_cigar) -{ - int i, n; - uint32_t *cigar; - unsigned char last_type; - - if (path_len == 0 || path == 0) { - *n_cigar = 0; - return 0; - } - - last_type = path->ctype; - for (i = n = 1; i < path_len; ++i) { - if (last_type != path[i].ctype) ++n; - last_type = path[i].ctype; - } - *n_cigar = n; - cigar = (uint32_t*)calloc(*n_cigar, 4); - - cigar[0] = 1u << 4 | path[path_len-1].ctype; - last_type = path[path_len-1].ctype; - for (i = path_len - 2, n = 0; i >= 0; --i) { - if (path[i].ctype == last_type) cigar[n] += 1u << 4; - else { - cigar[++n] = 1u << 4 | path[i].ctype; - last_type = path[i].ctype; - } - } - - return cigar; -} - -/***************************/ -/* START OF common_align.c */ -/***************************/ - -#define SET_INF(s) (s).M = (s).I = (s).D = MINOR_INF; - -#define set_M(MM, cur, p, sc) \ -{ \ - if ((p)->M >= (p)->I) { \ - if ((p)->M >= (p)->D) { \ - (MM) = (p)->M + (sc); (cur)->Mt = FROM_M; \ - } else { \ - (MM) = (p)->D + (sc); (cur)->Mt = FROM_D; \ - } \ - } else { \ - if ((p)->I > (p)->D) { \ - (MM) = (p)->I + (sc); (cur)->Mt = FROM_I; \ - } else { \ - (MM) = (p)->D + (sc); (cur)->Mt = FROM_D; \ - } \ - } \ -} -#define set_I(II, cur, p) \ -{ \ - if ((p)->M - gap_open > (p)->I) { \ - (cur)->It = FROM_M; \ - (II) = (p)->M - gap_open - gap_ext; \ - } else { \ - (cur)->It = FROM_I; \ - (II) = (p)->I - gap_ext; \ - } \ -} -#define set_end_I(II, cur, p) \ -{ \ - if (gap_end >= 0) { \ - if ((p)->M - gap_open > (p)->I) { \ - (cur)->It = FROM_M; \ - (II) = (p)->M - gap_open - gap_end; \ - } else { \ - (cur)->It = FROM_I; \ - (II) = (p)->I - gap_end; \ - } \ - } else set_I(II, cur, p); \ -} -#define set_D(DD, cur, p) \ -{ \ - if ((p)->M - gap_open > (p)->D) { \ - (cur)->Dt = FROM_M; \ - (DD) = (p)->M - gap_open - gap_ext; \ - } else { \ - (cur)->Dt = FROM_D; \ - (DD) = (p)->D - gap_ext; \ - } \ -} -#define set_end_D(DD, cur, p) \ -{ \ - if (gap_end >= 0) { \ - if ((p)->M - gap_open > (p)->D) { \ - (cur)->Dt = FROM_M; \ - (DD) = (p)->M - gap_open - gap_end; \ - } else { \ - (cur)->Dt = FROM_D; \ - (DD) = (p)->D - gap_end; \ - } \ - } else set_D(DD, cur, p); \ -} - -typedef struct { - uint8_t Mt:3, It:2, Dt:2; -} dpcell_t; - -typedef struct { - int M, I, D; -} dpscore_t; - -/*************************** - * banded global alignment * - ***************************/ -uint32_t *ka_global_core(uint8_t *seq1, int len1, uint8_t *seq2, int len2, const ka_param_t *ap, int *_score, int *n_cigar) -{ - int i, j; - dpcell_t **dpcell, *q; - dpscore_t *curr, *last, *s; - int b1, b2, tmp_end; - int *mat, end, max = 0; - uint8_t type, ctype; - uint32_t *cigar = 0; - - int gap_open, gap_ext, gap_end, b; - int *score_matrix, N_MATRIX_ROW; - - /* initialize some align-related parameters. just for compatibility */ - gap_open = ap->gap_open; - gap_ext = ap->gap_ext; - gap_end = ap->gap_end; - b = ap->band_width; - score_matrix = ap->matrix; - N_MATRIX_ROW = ap->row; - - *n_cigar = 0; - if (len1 == 0 || len2 == 0) return 0; - - /* calculate b1 and b2 */ - if (len1 > len2) { - b1 = len1 - len2 + b; - b2 = b; - } else { - b1 = b; - b2 = len2 - len1 + b; - } - if (b1 > len1) b1 = len1; - if (b2 > len2) b2 = len2; - --seq1; --seq2; - - /* allocate memory */ - end = (b1 + b2 <= len1)? (b1 + b2 + 1) : (len1 + 1); - dpcell = (dpcell_t**)malloc(sizeof(dpcell_t*) * (len2 + 1)); - for (j = 0; j <= len2; ++j) - dpcell[j] = (dpcell_t*)malloc(sizeof(dpcell_t) * end); - for (j = b2 + 1; j <= len2; ++j) - dpcell[j] -= j - b2; - curr = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1)); - last = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1)); - - /* set first row */ - SET_INF(*curr); curr->M = 0; - for (i = 1, s = curr + 1; i < b1; ++i, ++s) { - SET_INF(*s); - set_end_D(s->D, dpcell[0] + i, s - 1); - } - s = curr; curr = last; last = s; - - /* core dynamic programming, part 1 */ - tmp_end = (b2 < len2)? b2 : len2 - 1; - for (j = 1; j <= tmp_end; ++j) { - q = dpcell[j]; s = curr; SET_INF(*s); - set_end_I(s->I, q, last); - end = (j + b1 <= len1 + 1)? (j + b1 - 1) : len1; - mat = score_matrix + seq2[j] * N_MATRIX_ROW; - ++s; ++q; - for (i = 1; i != end; ++i, ++s, ++q) { - set_M(s->M, q, last + i - 1, mat[seq1[i]]); /* this will change s->M ! */ - set_I(s->I, q, last + i); - set_D(s->D, q, s - 1); - } - set_M(s->M, q, last + i - 1, mat[seq1[i]]); - set_D(s->D, q, s - 1); - if (j + b1 - 1 > len1) { /* bug fixed, 040227 */ - set_end_I(s->I, q, last + i); - } else s->I = MINOR_INF; - s = curr; curr = last; last = s; - } - /* last row for part 1, use set_end_D() instead of set_D() */ - if (j == len2 && b2 != len2 - 1) { - q = dpcell[j]; s = curr; SET_INF(*s); - set_end_I(s->I, q, last); - end = (j + b1 <= len1 + 1)? (j + b1 - 1) : len1; - mat = score_matrix + seq2[j] * N_MATRIX_ROW; - ++s; ++q; - for (i = 1; i != end; ++i, ++s, ++q) { - set_M(s->M, q, last + i - 1, mat[seq1[i]]); /* this will change s->M ! */ - set_I(s->I, q, last + i); - set_end_D(s->D, q, s - 1); - } - set_M(s->M, q, last + i - 1, mat[seq1[i]]); - set_end_D(s->D, q, s - 1); - if (j + b1 - 1 > len1) { /* bug fixed, 040227 */ - set_end_I(s->I, q, last + i); - } else s->I = MINOR_INF; - s = curr; curr = last; last = s; - ++j; - } - - /* core dynamic programming, part 2 */ - for (; j <= len2 - b2 + 1; ++j) { - SET_INF(curr[j - b2]); - mat = score_matrix + seq2[j] * N_MATRIX_ROW; - end = j + b1 - 1; - for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i != end; ++i, ++s, ++q) { - set_M(s->M, q, last + i - 1, mat[seq1[i]]); - set_I(s->I, q, last + i); - set_D(s->D, q, s - 1); - } - set_M(s->M, q, last + i - 1, mat[seq1[i]]); - set_D(s->D, q, s - 1); - s->I = MINOR_INF; - s = curr; curr = last; last = s; - } - - /* core dynamic programming, part 3 */ - for (; j < len2; ++j) { - SET_INF(curr[j - b2]); - mat = score_matrix + seq2[j] * N_MATRIX_ROW; - for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i < len1; ++i, ++s, ++q) { - set_M(s->M, q, last + i - 1, mat[seq1[i]]); - set_I(s->I, q, last + i); - set_D(s->D, q, s - 1); - } - set_M(s->M, q, last + len1 - 1, mat[seq1[i]]); - set_end_I(s->I, q, last + i); - set_D(s->D, q, s - 1); - s = curr; curr = last; last = s; - } - /* last row */ - if (j == len2) { - SET_INF(curr[j - b2]); - mat = score_matrix + seq2[j] * N_MATRIX_ROW; - for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i < len1; ++i, ++s, ++q) { - set_M(s->M, q, last + i - 1, mat[seq1[i]]); - set_I(s->I, q, last + i); - set_end_D(s->D, q, s - 1); - } - set_M(s->M, q, last + len1 - 1, mat[seq1[i]]); - set_end_I(s->I, q, last + i); - set_end_D(s->D, q, s - 1); - s = curr; curr = last; last = s; - } - - *_score = last[len1].M; - if (n_cigar) { /* backtrace */ - path_t *p, *path = (path_t*)malloc(sizeof(path_t) * (len1 + len2 + 2)); - i = len1; j = len2; - q = dpcell[j] + i; - s = last + len1; - max = s->M; type = q->Mt; ctype = FROM_M; - if (s->I > max) { max = s->I; type = q->It; ctype = FROM_I; } - if (s->D > max) { max = s->D; type = q->Dt; ctype = FROM_D; } - - p = path; - p->ctype = ctype; p->i = i; p->j = j; /* bug fixed 040408 */ - ++p; - do { - switch (ctype) { - case FROM_M: --i; --j; break; - case FROM_I: --j; break; - case FROM_D: --i; break; - } - q = dpcell[j] + i; - ctype = type; - switch (type) { - case FROM_M: type = q->Mt; break; - case FROM_I: type = q->It; break; - case FROM_D: type = q->Dt; break; - } - p->ctype = ctype; p->i = i; p->j = j; - ++p; - } while (i || j); - cigar = ka_path2cigar32(path, p - path - 1, n_cigar); - free(path); - } - - /* free memory */ - for (j = b2 + 1; j <= len2; ++j) - dpcell[j] += j - b2; - for (j = 0; j <= len2; ++j) - free(dpcell[j]); - free(dpcell); - free(curr); free(last); - - return cigar; -} diff --git a/samtools/kaln.c.pysam.c b/samtools/kaln.c.pysam.c new file mode 100644 index 0000000..953a214 --- /dev/null +++ b/samtools/kaln.c.pysam.c @@ -0,0 +1,488 @@ +#include "pysam.h" + +/* The MIT License + + Copyright (c) 2003-2006, 2008, 2009, by Heng Li + + 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 +#include +#include +#include +#include +#include "kaln.h" + +#define FROM_M 0 +#define FROM_I 1 +#define FROM_D 2 + +typedef struct { + int i, j; + unsigned char ctype; +} path_t; + +int aln_sm_blosum62[] = { +/* A R N D C Q E G H I L K M F P S T W Y V * X */ + 4,-1,-2,-2, 0,-1,-1, 0,-2,-1,-1,-1,-1,-2,-1, 1, 0,-3,-2, 0,-4, 0, + -1, 5, 0,-2,-3, 1, 0,-2, 0,-3,-2, 2,-1,-3,-2,-1,-1,-3,-2,-3,-4,-1, + -2, 0, 6, 1,-3, 0, 0, 0, 1,-3,-3, 0,-2,-3,-2, 1, 0,-4,-2,-3,-4,-1, + -2,-2, 1, 6,-3, 0, 2,-1,-1,-3,-4,-1,-3,-3,-1, 0,-1,-4,-3,-3,-4,-1, + 0,-3,-3,-3, 9,-3,-4,-3,-3,-1,-1,-3,-1,-2,-3,-1,-1,-2,-2,-1,-4,-2, + -1, 1, 0, 0,-3, 5, 2,-2, 0,-3,-2, 1, 0,-3,-1, 0,-1,-2,-1,-2,-4,-1, + -1, 0, 0, 2,-4, 2, 5,-2, 0,-3,-3, 1,-2,-3,-1, 0,-1,-3,-2,-2,-4,-1, + 0,-2, 0,-1,-3,-2,-2, 6,-2,-4,-4,-2,-3,-3,-2, 0,-2,-2,-3,-3,-4,-1, + -2, 0, 1,-1,-3, 0, 0,-2, 8,-3,-3,-1,-2,-1,-2,-1,-2,-2, 2,-3,-4,-1, + -1,-3,-3,-3,-1,-3,-3,-4,-3, 4, 2,-3, 1, 0,-3,-2,-1,-3,-1, 3,-4,-1, + -1,-2,-3,-4,-1,-2,-3,-4,-3, 2, 4,-2, 2, 0,-3,-2,-1,-2,-1, 1,-4,-1, + -1, 2, 0,-1,-3, 1, 1,-2,-1,-3,-2, 5,-1,-3,-1, 0,-1,-3,-2,-2,-4,-1, + -1,-1,-2,-3,-1, 0,-2,-3,-2, 1, 2,-1, 5, 0,-2,-1,-1,-1,-1, 1,-4,-1, + -2,-3,-3,-3,-2,-3,-3,-3,-1, 0, 0,-3, 0, 6,-4,-2,-2, 1, 3,-1,-4,-1, + -1,-2,-2,-1,-3,-1,-1,-2,-2,-3,-3,-1,-2,-4, 7,-1,-1,-4,-3,-2,-4,-2, + 1,-1, 1, 0,-1, 0, 0, 0,-1,-2,-2, 0,-1,-2,-1, 4, 1,-3,-2,-2,-4, 0, + 0,-1, 0,-1,-1,-1,-1,-2,-2,-1,-1,-1,-1,-2,-1, 1, 5,-2,-2, 0,-4, 0, + -3,-3,-4,-4,-2,-2,-3,-2,-2,-3,-2,-3,-1, 1,-4,-3,-2,11, 2,-3,-4,-2, + -2,-2,-2,-3,-2,-1,-2,-3, 2,-1,-1,-2,-1, 3,-3,-2,-2, 2, 7,-1,-4,-1, + 0,-3,-3,-3,-1,-2,-2,-3,-3, 3, 1,-2, 1,-1,-2,-2, 0,-3,-1, 4,-4,-1, + -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, 1,-4, + 0,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-2, 0, 0,-2,-1,-1,-4,-1 +}; + +int aln_sm_blast[] = { + 1, -3, -3, -3, -2, + -3, 1, -3, -3, -2, + -3, -3, 1, -3, -2, + -3, -3, -3, 1, -2, + -2, -2, -2, -2, -2 +}; + +int aln_sm_qual[] = { + 0, -23, -23, -23, 0, + -23, 0, -23, -23, 0, + -23, -23, 0, -23, 0, + -23, -23, -23, 0, 0, + 0, 0, 0, 0, 0 +}; + +ka_param_t ka_param_blast = { 5, 2, 5, 2, aln_sm_blast, 5, 50 }; +ka_param_t ka_param_aa2aa = { 10, 2, 10, 2, aln_sm_blosum62, 22, 50 }; + +ka_param2_t ka_param2_qual = { 37, 11, 37, 11, 37, 11, 0, 0, aln_sm_qual, 5, 50 }; + +static uint32_t *ka_path2cigar32(const path_t *path, int path_len, int *n_cigar) +{ + int i, n; + uint32_t *cigar; + unsigned char last_type; + + if (path_len == 0 || path == 0) { + *n_cigar = 0; + return 0; + } + + last_type = path->ctype; + for (i = n = 1; i < path_len; ++i) { + if (last_type != path[i].ctype) ++n; + last_type = path[i].ctype; + } + *n_cigar = n; + cigar = (uint32_t*)calloc(*n_cigar, 4); + + cigar[0] = 1u << 4 | path[path_len-1].ctype; + last_type = path[path_len-1].ctype; + for (i = path_len - 2, n = 0; i >= 0; --i) { + if (path[i].ctype == last_type) cigar[n] += 1u << 4; + else { + cigar[++n] = 1u << 4 | path[i].ctype; + last_type = path[i].ctype; + } + } + + return cigar; +} + +/***************************/ +/* START OF common_align.c */ +/***************************/ + +#define SET_INF(s) (s).M = (s).I = (s).D = MINOR_INF; + +#define set_M(MM, cur, p, sc) \ +{ \ + if ((p)->M >= (p)->I) { \ + if ((p)->M >= (p)->D) { \ + (MM) = (p)->M + (sc); (cur)->Mt = FROM_M; \ + } else { \ + (MM) = (p)->D + (sc); (cur)->Mt = FROM_D; \ + } \ + } else { \ + if ((p)->I > (p)->D) { \ + (MM) = (p)->I + (sc); (cur)->Mt = FROM_I; \ + } else { \ + (MM) = (p)->D + (sc); (cur)->Mt = FROM_D; \ + } \ + } \ +} +#define set_I(II, cur, p) \ +{ \ + if ((p)->M - gap_open > (p)->I) { \ + (cur)->It = FROM_M; \ + (II) = (p)->M - gap_open - gap_ext; \ + } else { \ + (cur)->It = FROM_I; \ + (II) = (p)->I - gap_ext; \ + } \ +} +#define set_end_I(II, cur, p) \ +{ \ + if (gap_end_ext >= 0) { \ + if ((p)->M - gap_end_open > (p)->I) { \ + (cur)->It = FROM_M; \ + (II) = (p)->M - gap_end_open - gap_end_ext; \ + } else { \ + (cur)->It = FROM_I; \ + (II) = (p)->I - gap_end_ext; \ + } \ + } else set_I(II, cur, p); \ +} +#define set_D(DD, cur, p) \ +{ \ + if ((p)->M - gap_open > (p)->D) { \ + (cur)->Dt = FROM_M; \ + (DD) = (p)->M - gap_open - gap_ext; \ + } else { \ + (cur)->Dt = FROM_D; \ + (DD) = (p)->D - gap_ext; \ + } \ +} +#define set_end_D(DD, cur, p) \ +{ \ + if (gap_end_ext >= 0) { \ + if ((p)->M - gap_end_open > (p)->D) { \ + (cur)->Dt = FROM_M; \ + (DD) = (p)->M - gap_end_open - gap_end_ext; \ + } else { \ + (cur)->Dt = FROM_D; \ + (DD) = (p)->D - gap_end_ext; \ + } \ + } else set_D(DD, cur, p); \ +} + +typedef struct { + uint8_t Mt:3, It:2, Dt:3; +} dpcell_t; + +typedef struct { + int M, I, D; +} dpscore_t; + +/*************************** + * banded global alignment * + ***************************/ +uint32_t *ka_global_core(uint8_t *seq1, int len1, uint8_t *seq2, int len2, const ka_param_t *ap, int *_score, int *n_cigar) +{ + int i, j; + dpcell_t **dpcell, *q; + dpscore_t *curr, *last, *s; + int b1, b2, tmp_end; + int *mat, end, max = 0; + uint8_t type, ctype; + uint32_t *cigar = 0; + + int gap_open, gap_ext, gap_end_open, gap_end_ext, b; + int *score_matrix, N_MATRIX_ROW; + + /* initialize some align-related parameters. just for compatibility */ + gap_open = ap->gap_open; + gap_ext = ap->gap_ext; + gap_end_open = ap->gap_end_open; + gap_end_ext = ap->gap_end_ext; + b = ap->band_width; + score_matrix = ap->matrix; + N_MATRIX_ROW = ap->row; + + if (n_cigar) *n_cigar = 0; + if (len1 == 0 || len2 == 0) return 0; + + /* calculate b1 and b2 */ + if (len1 > len2) { + b1 = len1 - len2 + b; + b2 = b; + } else { + b1 = b; + b2 = len2 - len1 + b; + } + if (b1 > len1) b1 = len1; + if (b2 > len2) b2 = len2; + --seq1; --seq2; + + /* allocate memory */ + end = (b1 + b2 <= len1)? (b1 + b2 + 1) : (len1 + 1); + dpcell = (dpcell_t**)malloc(sizeof(dpcell_t*) * (len2 + 1)); + for (j = 0; j <= len2; ++j) + dpcell[j] = (dpcell_t*)malloc(sizeof(dpcell_t) * end); + for (j = b2 + 1; j <= len2; ++j) + dpcell[j] -= j - b2; + curr = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1)); + last = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1)); + + /* set first row */ + SET_INF(*curr); curr->M = 0; + for (i = 1, s = curr + 1; i < b1; ++i, ++s) { + SET_INF(*s); + set_end_D(s->D, dpcell[0] + i, s - 1); + } + s = curr; curr = last; last = s; + + /* core dynamic programming, part 1 */ + tmp_end = (b2 < len2)? b2 : len2 - 1; + for (j = 1; j <= tmp_end; ++j) { + q = dpcell[j]; s = curr; SET_INF(*s); + set_end_I(s->I, q, last); + end = (j + b1 <= len1 + 1)? (j + b1 - 1) : len1; + mat = score_matrix + seq2[j] * N_MATRIX_ROW; + ++s; ++q; + for (i = 1; i != end; ++i, ++s, ++q) { + set_M(s->M, q, last + i - 1, mat[seq1[i]]); /* this will change s->M ! */ + set_I(s->I, q, last + i); + set_D(s->D, q, s - 1); + } + set_M(s->M, q, last + i - 1, mat[seq1[i]]); + set_D(s->D, q, s - 1); + if (j + b1 - 1 > len1) { /* bug fixed, 040227 */ + set_end_I(s->I, q, last + i); + } else s->I = MINOR_INF; + s = curr; curr = last; last = s; + } + /* last row for part 1, use set_end_D() instead of set_D() */ + if (j == len2 && b2 != len2 - 1) { + q = dpcell[j]; s = curr; SET_INF(*s); + set_end_I(s->I, q, last); + end = (j + b1 <= len1 + 1)? (j + b1 - 1) : len1; + mat = score_matrix + seq2[j] * N_MATRIX_ROW; + ++s; ++q; + for (i = 1; i != end; ++i, ++s, ++q) { + set_M(s->M, q, last + i - 1, mat[seq1[i]]); /* this will change s->M ! */ + set_I(s->I, q, last + i); + set_end_D(s->D, q, s - 1); + } + set_M(s->M, q, last + i - 1, mat[seq1[i]]); + set_end_D(s->D, q, s - 1); + if (j + b1 - 1 > len1) { /* bug fixed, 040227 */ + set_end_I(s->I, q, last + i); + } else s->I = MINOR_INF; + s = curr; curr = last; last = s; + ++j; + } + + /* core dynamic programming, part 2 */ + for (; j <= len2 - b2 + 1; ++j) { + SET_INF(curr[j - b2]); + mat = score_matrix + seq2[j] * N_MATRIX_ROW; + end = j + b1 - 1; + for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i != end; ++i, ++s, ++q) { + set_M(s->M, q, last + i - 1, mat[seq1[i]]); + set_I(s->I, q, last + i); + set_D(s->D, q, s - 1); + } + set_M(s->M, q, last + i - 1, mat[seq1[i]]); + set_D(s->D, q, s - 1); + s->I = MINOR_INF; + s = curr; curr = last; last = s; + } + + /* core dynamic programming, part 3 */ + for (; j < len2; ++j) { + SET_INF(curr[j - b2]); + mat = score_matrix + seq2[j] * N_MATRIX_ROW; + for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i < len1; ++i, ++s, ++q) { + set_M(s->M, q, last + i - 1, mat[seq1[i]]); + set_I(s->I, q, last + i); + set_D(s->D, q, s - 1); + } + set_M(s->M, q, last + len1 - 1, mat[seq1[i]]); + set_end_I(s->I, q, last + i); + set_D(s->D, q, s - 1); + s = curr; curr = last; last = s; + } + /* last row */ + if (j == len2) { + SET_INF(curr[j - b2]); + mat = score_matrix + seq2[j] * N_MATRIX_ROW; + for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i < len1; ++i, ++s, ++q) { + set_M(s->M, q, last + i - 1, mat[seq1[i]]); + set_I(s->I, q, last + i); + set_end_D(s->D, q, s - 1); + } + set_M(s->M, q, last + len1 - 1, mat[seq1[i]]); + set_end_I(s->I, q, last + i); + set_end_D(s->D, q, s - 1); + s = curr; curr = last; last = s; + } + + *_score = last[len1].M; + if (n_cigar) { /* backtrace */ + path_t *p, *path = (path_t*)malloc(sizeof(path_t) * (len1 + len2 + 2)); + i = len1; j = len2; + q = dpcell[j] + i; + s = last + len1; + max = s->M; type = q->Mt; ctype = FROM_M; + if (s->I > max) { max = s->I; type = q->It; ctype = FROM_I; } + if (s->D > max) { max = s->D; type = q->Dt; ctype = FROM_D; } + + p = path; + p->ctype = ctype; p->i = i; p->j = j; /* bug fixed 040408 */ + ++p; + do { + switch (ctype) { + case FROM_M: --i; --j; break; + case FROM_I: --j; break; + case FROM_D: --i; break; + } + q = dpcell[j] + i; + ctype = type; + switch (type) { + case FROM_M: type = q->Mt; break; + case FROM_I: type = q->It; break; + case FROM_D: type = q->Dt; break; + } + p->ctype = ctype; p->i = i; p->j = j; + ++p; + } while (i || j); + cigar = ka_path2cigar32(path, p - path - 1, n_cigar); + free(path); + } + + /* free memory */ + for (j = b2 + 1; j <= len2; ++j) + dpcell[j] += j - b2; + for (j = 0; j <= len2; ++j) + free(dpcell[j]); + free(dpcell); + free(curr); free(last); + + return cigar; +} + +typedef struct { + int M, I, D; +} score_aux_t; + +#define MINUS_INF -0x40000000 + +// matrix: len2 rows and len1 columns +int ka_global_score(const uint8_t *_seq1, int len1, const uint8_t *_seq2, int len2, const ka_param2_t *ap) +{ + +#define __score_aux(_p, _q0, _sc, _io, _ie, _do, _de) { \ + int t1, t2; \ + score_aux_t *_q; \ + _q = _q0; \ + _p->M = _q->M >= _q->I? _q->M : _q->I; \ + _p->M = _p->M >= _q->D? _p->M : _q->D; \ + _p->M += (_sc); \ + ++_q; t1 = _q->M - _io - _ie; t2 = _q->I - _ie; _p->I = t1 >= t2? t1 : t2; \ + _q = _p-1; t1 = _q->M - _do - _de; t2 = _q->D - _de; _p->D = t1 >= t2? t1 : t2; \ + } + + int i, j, bw, scmat_size = ap->row, *scmat = ap->matrix, ret; + const uint8_t *seq1, *seq2; + score_aux_t *curr, *last, *swap; + bw = abs(len1 - len2) + ap->band_width; + i = len1 > len2? len1 : len2; + if (bw > i + 1) bw = i + 1; + seq1 = _seq1 - 1; seq2 = _seq2 - 1; + curr = calloc(len1 + 2, sizeof(score_aux_t)); + last = calloc(len1 + 2, sizeof(score_aux_t)); + { // the zero-th row + int x, end = len1; + score_aux_t *p; + j = 0; + x = j + bw; end = len1 < x? len1 : x; // band end + p = curr; + p->M = 0; p->I = p->D = MINUS_INF; + for (i = 1, p = &curr[1]; i <= end; ++i, ++p) + p->M = p->I = MINUS_INF, p->D = -(ap->edo + ap->ede * i); + p->M = p->I = p->D = MINUS_INF; + swap = curr; curr = last; last = swap; + } + for (j = 1; j < len2; ++j) { + int x, beg = 0, end = len1, *scrow, col_end; + score_aux_t *p; + x = j - bw; beg = 0 > x? 0 : x; // band start + x = j + bw; end = len1 < x? len1 : x; // band end + if (beg == 0) { // from zero-th column + p = curr; + p->M = p->D = MINUS_INF; p->I = -(ap->eio + ap->eie * j); + ++beg; // then beg = 1 + } + scrow = scmat + seq2[j] * scmat_size; + if (end == len1) col_end = 1, --end; + else col_end = 0; + for (i = beg, p = &curr[beg]; i <= end; ++i, ++p) + __score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->iio, ap->iie, ap->ido, ap->ide); + if (col_end) { + __score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->eio, ap->eie, ap->ido, ap->ide); + ++p; + } + p->M = p->I = p->D = MINUS_INF; +// for (i = 0; i <= len1; ++i) printf("(%d,%d,%d) ", curr[i].M, curr[i].I, curr[i].D); putchar('\n'); + swap = curr; curr = last; last = swap; + } + { // the last row + int x, beg = 0, *scrow; + score_aux_t *p; + j = len2; + x = j - bw; beg = 0 > x? 0 : x; // band start + if (beg == 0) { // from zero-th column + p = curr; + p->M = p->D = MINUS_INF; p->I = -(ap->eio + ap->eie * j); + ++beg; // then beg = 1 + } + scrow = scmat + seq2[j] * scmat_size; + for (i = beg, p = &curr[beg]; i < len1; ++i, ++p) + __score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->iio, ap->iie, ap->edo, ap->ede); + __score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->eio, ap->eie, ap->edo, ap->ede); +// for (i = 0; i <= len1; ++i) printf("(%d,%d,%d) ", curr[i].M, curr[i].I, curr[i].D); putchar('\n'); + } + ret = curr[len1].M >= curr[len1].I? curr[len1].M : curr[len1].I; + ret = ret >= curr[len1].D? ret : curr[len1].D; + free(curr); free(last); + return ret; +} + +#ifdef _MAIN +int main(int argc, char *argv[]) +{ +// int len1 = 35, len2 = 35; +// uint8_t *seq1 = (uint8_t*)"\0\0\3\3\2\0\0\0\1\0\2\1\2\1\3\2\3\3\3\0\2\3\2\1\1\3\3\3\2\3\3\1\0\0\1"; +// uint8_t *seq2 = (uint8_t*)"\0\0\3\3\2\0\0\0\1\0\2\1\2\1\3\2\3\3\3\0\2\3\2\1\1\3\3\3\2\3\3\1\0\1\0"; + int len1 = 4, len2 = 4; + uint8_t *seq1 = (uint8_t*)"\1\0\0\1"; + uint8_t *seq2 = (uint8_t*)"\1\0\1\0"; + int sc; +// ka_global_core(seq1, 2, seq2, 1, &ka_param_qual, &sc, 0); + sc = ka_global_score(seq1, len1, seq2, len2, &ka_param2_qual); + printf("%d\n", sc); + return 0; +} +#endif diff --git a/samtools/kaln.h b/samtools/kaln.h index b04d8cc..1ece132 100644 --- a/samtools/kaln.h +++ b/samtools/kaln.h @@ -33,23 +33,35 @@ typedef struct { int gap_open; int gap_ext; - int gap_end; + int gap_end_open; + int gap_end_ext; int *matrix; int row; int band_width; } ka_param_t; +typedef struct { + int iio, iie, ido, ide; + int eio, eie, edo, ede; + int *matrix; + int row; + int band_width; +} ka_param2_t; + #ifdef __cplusplus extern "C" { #endif - uint32_t *ka_global_core(uint8_t *seq1, int len1, uint8_t *seq2, int len2, const ka_param_t *ap, int *_score, int *n_cigar); - + uint32_t *ka_global_core(uint8_t *seq1, int len1, uint8_t *seq2, int len2, const ka_param_t *ap, + int *_score, int *n_cigar); + int ka_global_score(const uint8_t *_seq1, int len1, const uint8_t *_seq2, int len2, const ka_param2_t *ap); #ifdef __cplusplus } #endif -extern ka_param_t ka_param_blast; /* = { 5, 2, 2, aln_sm_blast, 5, 50 }; */ +extern ka_param_t ka_param_blast; /* = { 5, 2, 5, 2, aln_sm_blast, 5, 50 }; */ +extern ka_param_t ka_param_qual; // only use this for global alignment!!! +extern ka_param2_t ka_param2_qual; // only use this for global alignment!!! #endif diff --git a/samtools/knetfile.c b/samtools/knetfile.c deleted file mode 100644 index e1be4d6..0000000 --- a/samtools/knetfile.c +++ /dev/null @@ -1,630 +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 */ - -/* 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. -lh3 */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef _WIN32 -#include -#include -#include -#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(stderr, "select time-out\n"); - else if (ret == SOCKET_ERROR) - fprintf(stderr, "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; - 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(stderr, "%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, stderr); - 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(stderr, "[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(stderr,"[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(stderr, "[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(stderr, "[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(stderr, "[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) { - curr = read(fp->fd, buf + l, rest); - 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(stderr,"[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(stderr, "[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(stderr,"[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.c.pysam.c b/samtools/knetfile.c.pysam.c new file mode 100644 index 0000000..e3a8845 --- /dev/null +++ b/samtools/knetfile.c.pysam.c @@ -0,0 +1,635 @@ +#include "pysam.h" + +/* 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 */ + +/* 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. -lh3 */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef _WIN32 +#include +#include +#include +#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; + 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/kprobaln.c.pysam.c b/samtools/kprobaln.c.pysam.c new file mode 100644 index 0000000..b87dcf0 --- /dev/null +++ b/samtools/kprobaln.c.pysam.c @@ -0,0 +1,280 @@ +#include "pysam.h" + +/* The MIT License + + Copyright (c) 2003-2006, 2008-2010, by Heng Li + + 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 +#include +#include +#include +#include +#include "kprobaln.h" + +/***************************************** + * Probabilistic banded glocal alignment * + *****************************************/ + +#define EI .25 +#define EM .33333333333 + +static float g_qual2prob[256]; + +#define set_u(u, b, i, k) { int x=(i)-(b); x=x>0?x:0; (u)=((k)-x+1)*3; } + +kpa_par_t kpa_par_def = { 0.001, 0.1, 10 }; +kpa_par_t kpa_par_alt = { 0.0001, 0.01, 10 }; + +/* + The topology of the profile HMM: + + /\ /\ /\ /\ + I[1] I[k-1] I[k] I[L] + ^ \ \ ^ \ ^ \ \ ^ + | \ \ | \ | \ \ | + M[0] M[1] -> ... -> M[k-1] -> M[k] -> ... -> M[L] M[L+1] + \ \/ \/ \/ / + \ /\ /\ /\ / + -> D[k-1] -> D[k] -> + + M[0] points to every {M,I}[k] and every {M,I}[k] points M[L+1]. + + On input, _ref is the reference sequence and _query is the query + sequence. Both are sequences of 0/1/2/3/4 where 4 stands for an + ambiguous residue. iqual is the base quality. c sets the gap open + probability, gap extension probability and band width. + + On output, state and q are arrays of length l_query. The higher 30 + bits give the reference position the query base is matched to and the + lower two bits can be 0 (an alignment match) or 1 (an + insertion). q[i] gives the phred scaled posterior probability of + state[i] being wrong. + */ +int kpa_glocal(const uint8_t *_ref, int l_ref, const uint8_t *_query, int l_query, const uint8_t *iqual, + const kpa_par_t *c, int *state, uint8_t *q) +{ + double **f, **b = 0, *s, m[9], sI, sM, bI, bM, pb; + float *qual, *_qual; + const uint8_t *ref, *query; + int bw, bw2, i, k, is_diff = 0, is_backward = 1, Pr; + + /*** initialization ***/ + is_backward = state && q? 1 : 0; + ref = _ref - 1; query = _query - 1; // change to 1-based coordinate + bw = l_ref > l_query? l_ref : l_query; + if (bw > c->bw) bw = c->bw; + 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*)); + for (i = 0; i <= l_query; ++i) { + 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)); + } + s = calloc(l_query+2, sizeof(double)); // s[] is the scaling factor to avoid underflow + // initialize qual + _qual = calloc(l_query, sizeof(float)); + if (g_qual2prob[0] == 0) + for (i = 0; i < 256; ++i) + g_qual2prob[i] = pow(10, -i/10.); + for (i = 0; i < l_query; ++i) _qual[i] = g_qual2prob[iqual? iqual[i] : 30]; + qual = _qual - 1; + // initialize transition probability + sM = sI = 1. / (2 * l_query + 2); // the value here seems not to affect results; FIXME: need proof + m[0*3+0] = (1 - c->d - c->d) * (1 - sM); m[0*3+1] = m[0*3+2] = c->d * (1 - sM); + m[1*3+0] = (1 - c->e) * (1 - sI); m[1*3+1] = c->e * (1 - sI); m[1*3+2] = 0.; + m[2*3+0] = 1 - c->e; m[2*3+1] = 0.; m[2*3+2] = c->e; + bM = (1 - c->d) / l_ref; bI = c->d / l_ref; // (bM+bI)*l_ref==1 + /*** forward ***/ + // f[0] + set_u(k, bw, 0, 0); + f[0][k] = s[0] = 1.; + { // f[1] + double *fi = f[1], sum; + int beg = 1, end = l_ref < bw + 1? l_ref : bw + 1, _beg, _end; + for (k = beg, sum = 0.; k <= end; ++k) { + int u; + double e = (ref[k] > 3 || query[1] > 3)? 1. : ref[k] == query[1]? 1. - qual[1] : qual[1] * EM; + set_u(u, bw, 1, k); + fi[u+0] = e * bM; fi[u+1] = EI * bI; + sum += fi[u] + fi[u+1]; + } + // rescale + s[1] = sum; + set_u(_beg, bw, 1, beg); set_u(_end, bw, 1, end); _end += 2; + for (k = _beg; k <= _end; ++k) fi[k] /= sum; + } + // f[2..l_query] + for (i = 2; i <= l_query; ++i) { + double *fi = f[i], *fi1 = f[i-1], sum, qli = qual[i]; + int beg = 1, end = l_ref, x, _beg, _end; + uint8_t qyi = query[i]; + x = i - bw; beg = beg > x? beg : x; // band start + x = i + bw; end = end < x? end : x; // band end + for (k = beg, sum = 0.; k <= end; ++k) { + int u, v11, v01, v10; + double e; + e = (ref[k] > 3 || qyi > 3)? 1. : ref[k] == qyi? 1. - qli : qli * EM; + set_u(u, bw, i, k); set_u(v11, bw, i-1, k-1); set_u(v10, bw, i-1, k); set_u(v01, bw, i, k-1); + fi[u+0] = e * (m[0] * fi1[v11+0] + m[3] * fi1[v11+1] + m[6] * fi1[v11+2]); + fi[u+1] = EI * (m[1] * fi1[v10+0] + m[4] * fi1[v10+1]); + fi[u+2] = m[2] * fi[v01+0] + m[8] * fi[v01+2]; + sum += fi[u] + fi[u+1] + fi[u+2]; +// fprintf(pysamerr, "F (%d,%d;%d): %lg,%lg,%lg\n", i, k, u, fi[u], fi[u+1], fi[u+2]); // DEBUG + } + // rescale + s[i] = sum; + set_u(_beg, bw, i, beg); set_u(_end, bw, i, end); _end += 2; + for (k = _beg, sum = 1./sum; k <= _end; ++k) fi[k] *= sum; + } + { // f[l_query+1] + double sum; + for (k = 1, sum = 0.; k <= l_ref; ++k) { + int u; + set_u(u, bw, l_query, k); + if (u < 3 || u >= bw2*3+3) continue; + sum += f[l_query][u+0] * sM + f[l_query][u+1] * sI; + } + s[l_query+1] = sum; // the last scaling factor + } + { // compute likelihood + double p = 1., Pr1 = 0.; + for (i = 0; i <= l_query + 1; ++i) { + p *= s[i]; + if (p < 1e-100) Pr += -4.343 * log(p), p = 1.; + } + Pr1 += -4.343 * log(p * l_ref * l_query); + Pr = (int)(Pr1 + .499); + if (!is_backward) { // skip backward and MAP + for (i = 0; i <= l_query; ++i) free(f[i]); + free(f); free(s); free(_qual); + return Pr; + } + } + /*** backward ***/ + // b[l_query] (b[l_query+1][0]=1 and thus \tilde{b}[][]=1/s[l_query+1]; this is where s[l_query+1] comes from) + for (k = 1; k <= l_ref; ++k) { + int u; + double *bi = b[l_query]; + set_u(u, bw, l_query, k); + if (u < 3 || u >= bw2*3+3) continue; + bi[u+0] = sM / s[l_query] / s[l_query+1]; bi[u+1] = sI / s[l_query] / s[l_query+1]; + } + // b[l_query-1..1] + for (i = l_query - 1; i >= 1; --i) { + int beg = 1, end = l_ref, x, _beg, _end; + double *bi = b[i], *bi1 = b[i+1], y = (i > 1), qli1 = qual[i+1]; + uint8_t qyi1 = query[i+1]; + x = i - bw; beg = beg > x? beg : x; + x = i + bw; end = end < x? end : x; + for (k = end; k >= beg; --k) { + int u, v11, v01, v10; + double e; + set_u(u, bw, i, k); set_u(v11, bw, i+1, k+1); set_u(v10, bw, i+1, k); set_u(v01, bw, i, k+1); + e = (k >= l_ref? 0 : (ref[k+1] > 3 || qyi1 > 3)? 1. : ref[k+1] == qyi1? 1. - qli1 : qli1 * EM) * bi1[v11]; + bi[u+0] = e * m[0] + EI * m[1] * bi1[v10+1] + m[2] * bi[v01+2]; // bi1[v11] has been foled into e. + bi[u+1] = e * m[3] + EI * m[4] * bi1[v10+1]; + bi[u+2] = (e * m[6] + m[8] * bi[v01+2]) * y; +// fprintf(pysamerr, "B (%d,%d;%d): %lg,%lg,%lg\n", i, k, u, bi[u], bi[u+1], bi[u+2]); // DEBUG + } + // rescale + set_u(_beg, bw, i, beg); set_u(_end, bw, i, end); _end += 2; + for (k = _beg, y = 1./s[i]; k <= _end; ++k) bi[k] *= y; + } + { // b[0] + int beg = 1, end = l_ref < bw + 1? l_ref : bw + 1; + double sum = 0.; + for (k = end; k >= beg; --k) { + int u; + double e = (ref[k] > 3 || query[1] > 3)? 1. : ref[k] == query[1]? 1. - qual[1] : qual[1] * EM; + set_u(u, bw, 1, k); + if (u < 3 || u >= bw2*3+3) continue; + sum += e * b[1][u+0] * bM + EI * b[1][u+1] * bI; + } + set_u(k, bw, 0, 0); + pb = b[0][k] = sum / s[0]; // if everything works as is expected, pb == 1.0 + } + is_diff = fabs(pb - 1.) > 1e-7? 1 : 0; + /*** MAP ***/ + for (i = 1; i <= l_query; ++i) { + double sum = 0., *fi = f[i], *bi = b[i], max = 0.; + int beg = 1, end = l_ref, x, max_k = -1; + x = i - bw; beg = beg > x? beg : x; + x = i + bw; end = end < x? end : x; + for (k = beg; k <= end; ++k) { + int u; + double z; + set_u(u, bw, i, k); + z = fi[u+0] * bi[u+0]; if (z > max) max = z, max_k = (k-1)<<2 | 0; sum += z; + z = fi[u+1] * bi[u+1]; if (z > max) max = z, max_k = (k-1)<<2 | 1; sum += z; + } + max /= sum; sum *= s[i]; // if everything works as is expected, sum == 1.0 + if (state) state[i-1] = max_k; + if (q) k = (int)(-4.343 * log(1. - max) + .499), q[i-1] = k > 100? 99 : k; +#ifdef _MAIN + fprintf(pysamerr, "(%.10lg,%.10lg) (%d,%d:%c,%c:%d) %lg\n", pb, sum, i-1, max_k>>2, + "ACGT"[query[i]], "ACGT"[ref[(max_k>>2)+1]], max_k&3, max); // DEBUG +#endif + } + /*** free ***/ + for (i = 0; i <= l_query; ++i) { + free(f[i]); free(b[i]); + } + free(f); free(b); free(s); free(_qual); + return Pr; +} + +#ifdef _MAIN +#include +int main(int argc, char *argv[]) +{ + uint8_t conv[256], *iqual, *ref, *query; + int c, l_ref, l_query, i, q = 30, b = 10, P; + while ((c = getopt(argc, argv, "b:q:")) >= 0) { + switch (c) { + case 'b': b = atoi(optarg); break; + case 'q': q = atoi(optarg); break; + } + } + if (optind + 2 > argc) { + fprintf(pysamerr, "Usage: %s [-q %d] [-b %d] \n", argv[0], q, b); // example: acttc attc + return 1; + } + memset(conv, 4, 256); + conv['a'] = conv['A'] = 0; conv['c'] = conv['C'] = 1; + conv['g'] = conv['G'] = 2; conv['t'] = conv['T'] = 3; + ref = (uint8_t*)argv[optind]; query = (uint8_t*)argv[optind+1]; + l_ref = strlen((char*)ref); l_query = strlen((char*)query); + for (i = 0; i < l_ref; ++i) ref[i] = conv[ref[i]]; + for (i = 0; i < l_query; ++i) query[i] = conv[query[i]]; + iqual = malloc(l_query); + memset(iqual, q, l_query); + kpa_par_def.bw = b; + P = kpa_glocal(ref, l_ref, query, l_query, iqual, &kpa_par_alt, 0, 0); + fprintf(pysamerr, "%d\n", P); + free(iqual); + return 0; +} +#endif diff --git a/samtools/kprobaln.h b/samtools/kprobaln.h new file mode 100644 index 0000000..0357dcc --- /dev/null +++ b/samtools/kprobaln.h @@ -0,0 +1,49 @@ +/* The MIT License + + Copyright (c) 2003-2006, 2008, 2009 by Heng Li + + 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 LH3_KPROBALN_H_ +#define LH3_KPROBALN_H_ + +#include + +typedef struct { + float d, e; + int bw; +} kpa_par_t; + +#ifdef __cplusplus +extern "C" { +#endif + + int kpa_glocal(const uint8_t *_ref, int l_ref, const uint8_t *_query, int l_query, const uint8_t *iqual, + const kpa_par_t *c, int *state, uint8_t *q); + +#ifdef __cplusplus +} +#endif + +extern kpa_par_t kpa_par_def, kpa_par_alt; + +#endif diff --git a/samtools/ksort.h b/samtools/ksort.h index 16a03fd..fa850ab 100644 --- a/samtools/ksort.h +++ b/samtools/ksort.h @@ -250,6 +250,15 @@ typedef struct { 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) @@ -259,6 +268,7 @@ typedef struct { #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) diff --git a/samtools/kstring.c b/samtools/kstring.c deleted file mode 100644 index e0203fa..0000000 --- a/samtools/kstring.c +++ /dev/null @@ -1,165 +0,0 @@ -#include -#include -#include -#include -#include -#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; -} - -// 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 * - **********************/ - -// reference: http://www-igm.univ-mlv.fr/~lecroq/string/node14.html -int *ksBM_prep(const uint8_t *pat, int m) -{ - int i, *suff, *prep, *bmGs, *bmBc; - prep = calloc(m + 256, 1); - 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 = 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; -} - -int *ksBM_search(const uint8_t *str, int n, const uint8_t *pat, int m, int *_prep, int *n_matches) -{ - int i, j, *prep, *bmGs, *bmBc; - int *matches = 0, mm = 0, nm = 0; - prep = _prep? _prep : ksBM_prep(pat, m); - 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) { - if (nm == mm) { - mm = mm? mm<<1 : 1; - matches = realloc(matches, mm * sizeof(int)); - } - matches[nm++] = j; - j += bmGs[0]; - } else { - int max = bmBc[str[i+j]] - m + 1 + i; - if (max < bmGs[i]) max = bmGs[i]; - j += max; - } - } - *n_matches = nm; - if (_prep == 0) free(prep); - return matches; -} - -#ifdef KSTRING_MAIN -#include -int main() -{ - kstring_t *s; - int *fields, n, i; - 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]); - free(s); - - { - static char *str = "abcdefgcdg"; - static char *pat = "cd"; - int n, *matches; - matches = ksBM_search(str, strlen(str), pat, strlen(pat), 0, &n); - printf("%d: \n", n); - for (i = 0; i < n; ++i) - printf("- %d\n", matches[i]); - free(matches); - } - return 0; -} -#endif diff --git a/samtools/kstring.c.pysam.c b/samtools/kstring.c.pysam.c new file mode 100644 index 0000000..9eb9721 --- /dev/null +++ b/samtools/kstring.c.pysam.c @@ -0,0 +1,206 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#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->tab[0] = aux->tab[1] = aux->tab[2] = aux->tab[3] = 0; + for (p = sep; *p; ++p) + aux->tab[*p/64] |= 1ull<<(*p%64); + } + if (str) aux->p = str - 1, aux->tab[0] &= ~1ull; + else if (aux->tab[0]&1) return 0; + for (p = start = aux->p + 1; *p; ++p) + if (aux->tab[*p/64]>>(*p%64)&1) break; + aux->p = p; // end of token + if (*p == 0) aux->tab[0] |= 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 = 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 = 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 +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 index 925117a..c46a62b 100644 --- a/samtools/kstring.h +++ b/samtools/kstring.h @@ -17,17 +17,31 @@ typedef struct __kstring_t { } kstring_t; #endif -int ksprintf(kstring_t *s, const char *fmt, ...); -int ksplit_core(char *s, int delimiter, int *_max, int **_offsets); +typedef struct { + uint64_t tab[4]; + const char *p; // end of the current token +} ks_tokaux_t; -// calculate the auxiliary array, allocated by calloc() -int *ksBM_prep(const uint8_t *pat, int m); +#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); -/* Search pat in str and returned the list of matches. The size of the - * list is returned as n_matches. _prep is the array returned by - * ksBM_prep(). If it is a NULL pointer, ksBM_prep() will be called. */ -int *ksBM_search(const uint8_t *str, int n, const uint8_t *pat, int m, int *_prep, int *n_matches); + /* 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 int kputsn(const char *p, int l, kstring_t *s) { if (s->l + l + 1 >= s->m) { @@ -35,7 +49,7 @@ static inline int kputsn(const char *p, int l, kstring_t *s) kroundup32(s->m); s->s = (char*)realloc(s->s, s->m); } - strncpy(s->s + s->l, p, l); + memcpy(s->s + s->l, p, l); s->l += l; s->s[s->l] = 0; return l; diff --git a/samtools/pysam.h b/samtools/pysam.h new file mode 100644 index 0000000..008cbbd --- /dev/null +++ b/samtools/pysam.h @@ -0,0 +1,5 @@ +#ifndef PYSAM_H +#define PYSAM_H +#include "stdio.h" +extern FILE * pysamerr; +#endif diff --git a/samtools/razf.c b/samtools/razf.c deleted file mode 100644 index e7499f9..0000000 --- a/samtools/razf.c +++ /dev/null @@ -1,853 +0,0 @@ -/* - * RAZF : Random Access compressed(Z) File - * Version: 1.0 - * Release Date: 2008-10-27 - * - * Copyright 2008, Jue Ruan , Heng Li - * - * 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 -#include -#include -#include -#include -#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(){ - 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;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); - for(i=0;iindex->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;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); - for(i=0;iindex->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(stderr, "[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;iinbuf + rz->buf_len)[i] = ((char*)data)[i]; - rz->buf_len += size; - return; - } else { - n = RZ_BUFFER_SIZE - rz->buf_len; - for(i=0;iinbuf + 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(stderr, " -- 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(stderr,"[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(stderr,"[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(stderr, "[_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(stderr, "[_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;ioutbuf + 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;ibuf_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(stderr, "[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.c.pysam.c b/samtools/razf.c.pysam.c new file mode 100644 index 0000000..4021637 --- /dev/null +++ b/samtools/razf.c.pysam.c @@ -0,0 +1,855 @@ +#include "pysam.h" + +/* + * RAZF : Random Access compressed(Z) File + * Version: 1.0 + * Release Date: 2008-10-27 + * + * Copyright 2008, Jue Ruan , Heng Li + * + * 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 +#include +#include +#include +#include +#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(){ + 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;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); + for(i=0;iindex->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;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); + for(i=0;iindex->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;iinbuf + rz->buf_len)[i] = ((char*)data)[i]; + rz->buf_len += size; + return; + } else { + n = RZ_BUFFER_SIZE - rz->buf_len; + for(i=0;iinbuf + 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;ioutbuf + 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;ibuf_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/sam.c b/samtools/sam.c deleted file mode 100644 index ecdee02..0000000 --- a/samtools/sam.c +++ /dev/null @@ -1,175 +0,0 @@ -#include -#include -#include "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; -} - -samfile_t *samopen(const char *fn, const char *mode, const void *aux) -{ - samfile_t *fp; - fp = (samfile_t*)calloc(1, sizeof(samfile_t)); - if (mode[0] == 'r') { // read - fp->type |= TYPE_READ; - if (mode[1] == '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) - fprintf(stderr, "[samopen] no @SQ lines in the header.\n"); - } else fprintf(stderr, "[samopen] SAM header is present: %d sequences.\n", fp->header->n_targets); - } - } else if (mode[0] == 'w') { // write - fp->header = bam_header_dup((const bam_header_t*)aux); - if (mode[1] == 'b') { // binary - char bmode[3]; - bmode[0] = 'w'; bmode[1] = strstr(mode, "u")? 'u' : 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.tamr == 0) goto open_err_ret; - if (strstr(mode, "X")) fp->type |= BAM_OFSTR<<2; - else if (strstr(mode, "x")) fp->type |= BAM_OFHEX<<2; - else fp->type |= BAM_OFDEC<<2; - // write header - if (strstr(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); - if (alt->n_targets) { // then write the header text without dumping ->target_{name,len} - if (alt->n_targets != fp->header->n_targets) - fprintf(stderr, "[samopen] inconsistent number of target sequences.\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; - -open_err_ret: - free(fp); - return 0; -} - -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); -} - -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) -{ - 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; - } -} - -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; -} - -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(stderr, "[samfaipath] fail to read file %s.\n", fn_ref); - } else { - fprintf(stderr, "[samfaipath] build FASTA index...\n"); - if (fai_build(fn_ref) == -1) { - fprintf(stderr, "[samfaipath] fail to build FASTA index.\n"); - free(fn_list); fn_list = 0; - } - } - } - return fn_list; -} diff --git a/samtools/sam.c.pysam.c b/samtools/sam.c.pysam.c new file mode 100644 index 0000000..091b20d --- /dev/null +++ b/samtools/sam.c.pysam.c @@ -0,0 +1,177 @@ +#include "pysam.h" + +#include +#include +#include "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; +} + +samfile_t *samopen(const char *fn, const char *mode, const void *aux) +{ + samfile_t *fp; + fp = (samfile_t*)calloc(1, sizeof(samfile_t)); + if (mode[0] == 'r') { // read + fp->type |= TYPE_READ; + if (mode[1] == '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) + fprintf(pysamerr, "[samopen] no @SQ lines in the header.\n"); + } else fprintf(pysamerr, "[samopen] SAM header is present: %d sequences.\n", fp->header->n_targets); + } + } else if (mode[0] == 'w') { // write + fp->header = bam_header_dup((const bam_header_t*)aux); + if (mode[1] == 'b') { // binary + char bmode[3]; + bmode[0] = 'w'; bmode[1] = strstr(mode, "u")? 'u' : 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.tamr == 0) goto open_err_ret; + if (strstr(mode, "X")) fp->type |= BAM_OFSTR<<2; + else if (strstr(mode, "x")) fp->type |= BAM_OFHEX<<2; + else fp->type |= BAM_OFDEC<<2; + // write header + if (strstr(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); + if (alt->n_targets) { // then write the header text without dumping ->target_{name,len} + if (alt->n_targets != fp->header->n_targets) + fprintf(pysamerr, "[samopen] inconsistent number of target sequences.\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; + +open_err_ret: + free(fp); + return 0; +} + +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); +} + +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) +{ + 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; + } +} + +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; +} + +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 { + 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_header.c b/samtools/sam_header.c deleted file mode 100644 index 05d75de..0000000 --- a/samtools/sam_header.c +++ /dev/null @@ -1,735 +0,0 @@ -#include "sam_header.h" -#include -#include -#include -#include -#include - -#include "khash.h" -KHASH_MAP_INIT_STR(str, const char *) - -struct _HeaderList -{ - struct _HeaderList *last; // Hack: Used and maintained only by list_append_to_end. Maintained in the root node only. - struct _HeaderList *next; - void *data; -}; -typedef struct _HeaderList list_t; -typedef list_t HeaderDict; - -typedef struct -{ - char key[2]; - char *value; -} -HeaderTag; - -typedef struct -{ - char type[2]; - list_t *tags; -} -HeaderLine; - -const char *o_hd_tags[] = {"SO","GO",NULL}; -const char *r_hd_tags[] = {"VN",NULL}; - -const char *o_sq_tags[] = {"AS","M5","UR","SP",NULL}; -const char *r_sq_tags[] = {"SN","LN",NULL}; -const char *u_sq_tags[] = {"SN",NULL}; - -const char *o_rg_tags[] = {"LB","DS","PU","PI","CN","DT","PL",NULL}; -const char *r_rg_tags[] = {"ID",NULL}; -const char *u_rg_tags[] = {"ID",NULL}; - -const char *o_pg_tags[] = {"VN","CL",NULL}; -const char *r_pg_tags[] = {"ID",NULL}; - -const char *types[] = {"HD","SQ","RG","PG","CO",NULL}; -const char **optional_tags[] = {o_hd_tags,o_sq_tags,o_rg_tags,o_pg_tags,NULL,NULL}; -const char **required_tags[] = {r_hd_tags,r_sq_tags,r_rg_tags,r_pg_tags,NULL,NULL}; -const char **unique_tags[] = {NULL, u_sq_tags,u_rg_tags,NULL,NULL,NULL}; - - -static void debug(const char *format, ...) -{ - va_list ap; - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); -} - -#if 0 -// Replaced by list_append_to_end -static list_t *list_prepend(list_t *root, void *data) -{ - list_t *l = malloc(sizeof(list_t)); - l->next = root; - l->data = data; - return l; -} -#endif - -// Relies on the root->last being correct. Do not use with the other list_* -// routines unless they are fixed to modify root->last as well. -static list_t *list_append_to_end(list_t *root, void *data) -{ - list_t *l = malloc(sizeof(list_t)); - l->last = l; - l->next = NULL; - l->data = data; - - if ( !root ) - return l; - - root->last->next = l; - root->last = l; - return root; -} - -static list_t *list_append(list_t *root, void *data) -{ - list_t *l = root; - while (l && l->next) - l = l->next; - if ( l ) - { - l->next = malloc(sizeof(list_t)); - l = l->next; - } - else - { - l = malloc(sizeof(list_t)); - root = l; - } - l->data = data; - l->next = NULL; - return root; -} - -static void list_free(list_t *root) -{ - list_t *l = root; - while (root) - { - l = root; - root = root->next; - free(l); - } -} - - - -// Look for a tag "XY" in a predefined const char *[] array. -static int tag_exists(const char *tag, const char **tags) -{ - int itag=0; - if ( !tags ) return -1; - while ( tags[itag] ) - { - if ( tags[itag][0]==tag[0] && tags[itag][1]==tag[1] ) return itag; - itag++; - } - return -1; -} - - - -// Mimics the behaviour of getline, except it returns pointer to the next chunk of the text -// or NULL if everything has been read. The lineptr should be freed by the caller. The -// newline character is stripped. -static const char *nextline(char **lineptr, size_t *n, const char *text) -{ - int len; - const char *to = text; - - if ( !*to ) return NULL; - - while ( *to && *to!='\n' && *to!='\r' ) to++; - len = to - text + 1; - - if ( *to ) - { - // Advance the pointer for the next call - if ( *to=='\n' ) to++; - else if ( *to=='\r' && *(to+1)=='\n' ) to+=2; - } - if ( !len ) - return to; - - if ( !*lineptr ) - { - *lineptr = malloc(len); - *n = len; - } - else if ( *nkey[0] = name[0]; - tag->key[1] = name[1]; - tag->value = malloc(len+1); - memcpy(tag->value,value_from,len+1); - tag->value[len] = 0; - return tag; -} - -static HeaderTag *header_line_has_tag(HeaderLine *hline, const char *key) -{ - list_t *tags = hline->tags; - while (tags) - { - HeaderTag *tag = tags->data; - if ( tag->key[0]==key[0] && tag->key[1]==key[1] ) return tag; - tags = tags->next; - } - return NULL; -} - - -// Return codes: -// 0 .. different types or unique tags differ or conflicting tags, cannot be merged -// 1 .. all tags identical -> no need to merge, drop one -// 2 .. the unique tags match and there are some conflicting tags (same tag, different value) -> error, cannot be merged nor duplicated -// 3 .. there are some missing complementary tags and no unique conflict -> can be merged into a single line -static int sam_header_compare_lines(HeaderLine *hline1, HeaderLine *hline2) -{ - HeaderTag *t1, *t2; - - if ( hline1->type[0]!=hline2->type[0] || hline1->type[1]!=hline2->type[1] ) - return 0; - - 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 - } - - if ( unique_tags[itype] ) - { - t1 = header_line_has_tag(hline1,unique_tags[itype][0]); - t2 = header_line_has_tag(hline2,unique_tags[itype][0]); - if ( !t1 || !t2 ) // this should never happen, the unique tags are required - return 2; - - if ( strcmp(t1->value,t2->value) ) - return 0; // the unique tags differ, cannot be merged - } - if ( !required_tags[itype] && !optional_tags[itype] ) - { - t1 = hline1->tags->data; - t2 = hline2->tags->data; - if ( !strcmp(t1->value,t2->value) ) return 1; // identical comments - return 0; - } - - int missing=0, itag=0; - while ( required_tags[itype] && required_tags[itype][itag] ) - { - t1 = header_line_has_tag(hline1,required_tags[itype][itag]); - t2 = header_line_has_tag(hline2,required_tags[itype][itag]); - if ( !t1 && !t2 ) - return 2; // this should never happen - else if ( !t1 || !t2 ) - missing = 1; // there is some tag missing in one of the hlines - else if ( strcmp(t1->value,t2->value) ) - { - 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++; - } - itag = 0; - while ( optional_tags[itype] && optional_tags[itype][itag] ) - { - t1 = header_line_has_tag(hline1,optional_tags[itype][itag]); - t2 = header_line_has_tag(hline2,optional_tags[itype][itag]); - if ( !t1 && !t2 ) - { - itag++; - continue; - } - if ( !t1 || !t2 ) - missing = 1; // there is some tag missing in one of the hlines - else if ( strcmp(t1->value,t2->value) ) - { - 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++; - } - if ( missing ) return 3; // there are some missing complementary tags with no conflicts, can be merged - return 1; -} - - -static HeaderLine *sam_header_line_clone(const HeaderLine *hline) -{ - list_t *tags; - HeaderLine *out = malloc(sizeof(HeaderLine)); - out->type[0] = hline->type[0]; - out->type[1] = hline->type[1]; - out->tags = NULL; - - tags = hline->tags; - while (tags) - { - HeaderTag *old = tags->data; - - HeaderTag *new = malloc(sizeof(HeaderTag)); - new->key[0] = old->key[0]; - new->key[1] = old->key[1]; - new->value = strdup(old->value); - out->tags = list_append(out->tags, new); - - tags = tags->next; - } - return out; -} - -static int sam_header_line_merge_with(HeaderLine *out_hline, const HeaderLine *tmpl_hline) -{ - list_t *tmpl_tags; - - 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) - { - HeaderTag *tmpl_tag = tmpl_tags->data; - HeaderTag *out_tag = header_line_has_tag(out_hline, tmpl_tag->key); - if ( !out_tag ) - { - HeaderTag *tag = malloc(sizeof(HeaderTag)); - tag->key[0] = tmpl_tag->key[0]; - tag->key[1] = tmpl_tag->key[1]; - tag->value = strdup(tmpl_tag->value); - out_hline->tags = list_append(out_hline->tags,tag); - } - tmpl_tags = tmpl_tags->next; - } - return 1; -} - - -static HeaderLine *sam_header_line_parse(const char *headerLine) -{ - HeaderLine *hline; - HeaderTag *tag; - const char *from, *to; - from = headerLine; - - if ( *from != '@' ) { - 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; - } - - 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)); - return 0; - } - from = to; - while (*from) - { - while (*to && *to!='\t') to++; - - if ( !required_tags[itype] && !optional_tags[itype] ) - { - // CO is a special case, it can contain anything, including tabs - if ( *to ) { to++; continue; } - tag = new_tag(" ",from,to-1); - } - else - tag = new_tag(from,from+3,to-1); - - 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; - } - - from = to; - } - return hline; -} - - -// Must be of an existing type, all tags must be recognised and all required tags must be present -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 ) - { - debug("The type [%c%c] not recognised.\n", hline->type[0],hline->type[1]); - return 0; - } - - // Has all required tags? - itag = 0; - while ( required_tags[itype] && required_tags[itype][itag] ) - { - if ( !header_line_has_tag(hline,required_tags[itype][itag]) ) - { - debug("The tag [%c%c] required for [%c%c] not present.\n", required_tags[itype][itag][0],required_tags[itype][itag][1], - hline->type[0],hline->type[1]); - return 0; - } - itag++; - } - - // Are all tags recognised? - tags = hline->tags; - while ( tags ) - { - tag = tags->data; - if ( !tag_exists(tag->key,required_tags[itype]) && !tag_exists(tag->key,optional_tags[itype]) ) - { - debug("Unknown tag [%c%c] for [%c%c].\n", tag->key[0],tag->key[1], hline->type[0],hline->type[1]); - return 0; - } - tags = tags->next; - } - - return 1; -} - - -static void print_header_line(FILE *fp, HeaderLine *hline) -{ - list_t *tags = hline->tags; - HeaderTag *tag; - - fprintf(fp, "@%c%c", hline->type[0],hline->type[1]); - while (tags) - { - tag = tags->data; - - fprintf(fp, "\t"); - if ( tag->key[0]!=' ' || tag->key[1]!=' ' ) - fprintf(fp, "%c%c:", tag->key[0],tag->key[1]); - fprintf(fp, "%s", tag->value); - - tags = tags->next; - } - fprintf(fp,"\n"); -} - - -static void sam_header_line_free(HeaderLine *hline) -{ - list_t *tags = hline->tags; - while (tags) - { - HeaderTag *tag = tags->data; - free(tag->value); - free(tag); - tags = tags->next; - } - list_free(hline->tags); - free(hline); -} - -void sam_header_free(void *_header) -{ - HeaderDict *header = (HeaderDict*)_header; - list_t *hlines = header; - while (hlines) - { - sam_header_line_free(hlines->data); - hlines = hlines->next; - } - list_free(header); -} - -HeaderDict *sam_header_clone(const HeaderDict *dict) -{ - HeaderDict *out = NULL; - while (dict) - { - HeaderLine *hline = dict->data; - out = list_append(out, sam_header_line_clone(hline)); - dict = dict->next; - } - return out; -} - -// Returns a newly allocated string -char *sam_header_write(const void *_header) -{ - const HeaderDict *header = (const HeaderDict*)_header; - char *out = NULL; - int len=0, nout=0; - const list_t *hlines; - - // Calculate the length of the string to allocate - hlines = header; - while (hlines) - { - len += 4; // @XY and \n - - HeaderLine *hline = hlines->data; - list_t *tags = hline->tags; - while (tags) - { - HeaderTag *tag = tags->data; - len += strlen(tag->value) + 1; // \t - if ( tag->key[0]!=' ' || tag->key[1]!=' ' ) - len += strlen(tag->value) + 3; // XY: - tags = tags->next; - } - hlines = hlines->next; - } - - nout = 0; - out = malloc(len+1); - hlines = header; - while (hlines) - { - HeaderLine *hline = hlines->data; - - nout += sprintf(out+nout,"@%c%c",hline->type[0],hline->type[1]); - - list_t *tags = hline->tags; - while (tags) - { - HeaderTag *tag = tags->data; - nout += sprintf(out+nout,"\t"); - if ( tag->key[0]!=' ' || tag->key[1]!=' ' ) - nout += sprintf(out+nout,"%c%c:", tag->key[0],tag->key[1]); - nout += sprintf(out+nout,"%s", tag->value); - tags = tags->next; - } - hlines = hlines->next; - nout += sprintf(out+nout,"\n"); - } - out[len] = 0; - return out; -} - -void *sam_header_parse2(const char *headerText) -{ - list_t *hlines = NULL; - HeaderLine *hline; - const char *text; - char *buf=NULL; - size_t nbuf = 0; - - if ( !headerText ) - return 0; - - text = headerText; - while ( (text=nextline(&buf, &nbuf, text)) ) - { - hline = sam_header_line_parse(buf); - if ( hline && sam_header_line_validate(hline) ) - // With too many (~250,000) reference sequences the header parsing was too slow with list_append. - hlines = list_append_to_end(hlines, hline); - else - { - if (hline) sam_header_line_free(hline); - sam_header_free(hlines); - if ( buf ) free(buf); - return NULL; - } - } - if ( buf ) free(buf); - - return hlines; -} - -void *sam_header2tbl(const void *_dict, char type[2], char key_tag[2], char value_tag[2]) -{ - 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 - while (l) - { - HeaderLine *hline = l->data; - 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); - 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); - k = kh_put(str, tbl, key->value, &ret); - kh_value(tbl, k) = value->value; - - l = l->next; - } - return tbl; -} - -char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n) -{ - const HeaderDict *dict = (const HeaderDict*)_dict; - const list_t *l = dict; - int max, n; - char **ret; - - ret = 0; *_n = max = n = 0; - while (l) - { - HeaderLine *hline = l->data; - 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 ) - { - l = l->next; - continue; - } - - if (n == max) { - max = max? max<<1 : 4; - ret = realloc(ret, max * sizeof(void*)); - } - ret[n++] = key->value; - - l = l->next; - } - *_n = n; - return ret; -} - -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); -} - -int sam_tbl_size(void *h) -{ - 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); -} - -void *sam_header_merge(int n, const void **_dicts) -{ - const HeaderDict **dicts = (const HeaderDict**)_dicts; - HeaderDict *out_dict; - int idict, status; - - if ( n<2 ) return NULL; - - out_dict = sam_header_clone(dicts[0]); - - for (idict=1; idictdata, out_hlines->data); - if ( status==0 ) - { - out_hlines = out_hlines->next; - continue; - } - - if ( status==2 ) - { - print_header_line(stderr,tmpl_hlines->data); - print_header_line(stderr,out_hlines->data); - debug("Conflicting lines, cannot merge the headers.\n"); - return 0; - } - if ( status==3 ) - sam_header_line_merge_with(out_hlines->data, tmpl_hlines->data); - - inserted = 1; - break; - } - if ( !inserted ) - out_dict = list_append(out_dict, sam_header_line_clone(tmpl_hlines->data)); - - tmpl_hlines = tmpl_hlines->next; - } - } - - return out_dict; -} - - diff --git a/samtools/sam_header.c.pysam.c b/samtools/sam_header.c.pysam.c new file mode 100644 index 0000000..91a4dcb --- /dev/null +++ b/samtools/sam_header.c.pysam.c @@ -0,0 +1,737 @@ +#include "pysam.h" + +#include "sam_header.h" +#include +#include +#include +#include +#include + +#include "khash.h" +KHASH_MAP_INIT_STR(str, const char *) + +struct _HeaderList +{ + struct _HeaderList *last; // Hack: Used and maintained only by list_append_to_end. Maintained in the root node only. + struct _HeaderList *next; + void *data; +}; +typedef struct _HeaderList list_t; +typedef list_t HeaderDict; + +typedef struct +{ + char key[2]; + char *value; +} +HeaderTag; + +typedef struct +{ + char type[2]; + list_t *tags; +} +HeaderLine; + +const char *o_hd_tags[] = {"SO","GO",NULL}; +const char *r_hd_tags[] = {"VN",NULL}; + +const char *o_sq_tags[] = {"AS","M5","UR","SP",NULL}; +const char *r_sq_tags[] = {"SN","LN",NULL}; +const char *u_sq_tags[] = {"SN",NULL}; + +const char *o_rg_tags[] = {"LB","DS","PU","PI","CN","DT","PL",NULL}; +const char *r_rg_tags[] = {"ID",NULL}; +const char *u_rg_tags[] = {"ID",NULL}; + +const char *o_pg_tags[] = {"VN","CL",NULL}; +const char *r_pg_tags[] = {"ID",NULL}; + +const char *types[] = {"HD","SQ","RG","PG","CO",NULL}; +const char **optional_tags[] = {o_hd_tags,o_sq_tags,o_rg_tags,o_pg_tags,NULL,NULL}; +const char **required_tags[] = {r_hd_tags,r_sq_tags,r_rg_tags,r_pg_tags,NULL,NULL}; +const char **unique_tags[] = {NULL, u_sq_tags,u_rg_tags,NULL,NULL,NULL}; + + +static void debug(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + vfprintf(pysamerr, format, ap); + va_end(ap); +} + +#if 0 +// Replaced by list_append_to_end +static list_t *list_prepend(list_t *root, void *data) +{ + list_t *l = malloc(sizeof(list_t)); + l->next = root; + l->data = data; + return l; +} +#endif + +// Relies on the root->last being correct. Do not use with the other list_* +// routines unless they are fixed to modify root->last as well. +static list_t *list_append_to_end(list_t *root, void *data) +{ + list_t *l = malloc(sizeof(list_t)); + l->last = l; + l->next = NULL; + l->data = data; + + if ( !root ) + return l; + + root->last->next = l; + root->last = l; + return root; +} + +static list_t *list_append(list_t *root, void *data) +{ + list_t *l = root; + while (l && l->next) + l = l->next; + if ( l ) + { + l->next = malloc(sizeof(list_t)); + l = l->next; + } + else + { + l = malloc(sizeof(list_t)); + root = l; + } + l->data = data; + l->next = NULL; + return root; +} + +static void list_free(list_t *root) +{ + list_t *l = root; + while (root) + { + l = root; + root = root->next; + free(l); + } +} + + + +// Look for a tag "XY" in a predefined const char *[] array. +static int tag_exists(const char *tag, const char **tags) +{ + int itag=0; + if ( !tags ) return -1; + while ( tags[itag] ) + { + if ( tags[itag][0]==tag[0] && tags[itag][1]==tag[1] ) return itag; + itag++; + } + return -1; +} + + + +// Mimics the behaviour of getline, except it returns pointer to the next chunk of the text +// or NULL if everything has been read. The lineptr should be freed by the caller. The +// newline character is stripped. +static const char *nextline(char **lineptr, size_t *n, const char *text) +{ + int len; + const char *to = text; + + if ( !*to ) return NULL; + + while ( *to && *to!='\n' && *to!='\r' ) to++; + len = to - text + 1; + + if ( *to ) + { + // Advance the pointer for the next call + if ( *to=='\n' ) to++; + else if ( *to=='\r' && *(to+1)=='\n' ) to+=2; + } + if ( !len ) + return to; + + if ( !*lineptr ) + { + *lineptr = malloc(len); + *n = len; + } + else if ( *nkey[0] = name[0]; + tag->key[1] = name[1]; + tag->value = malloc(len+1); + memcpy(tag->value,value_from,len+1); + tag->value[len] = 0; + return tag; +} + +static HeaderTag *header_line_has_tag(HeaderLine *hline, const char *key) +{ + list_t *tags = hline->tags; + while (tags) + { + HeaderTag *tag = tags->data; + if ( tag->key[0]==key[0] && tag->key[1]==key[1] ) return tag; + tags = tags->next; + } + return NULL; +} + + +// Return codes: +// 0 .. different types or unique tags differ or conflicting tags, cannot be merged +// 1 .. all tags identical -> no need to merge, drop one +// 2 .. the unique tags match and there are some conflicting tags (same tag, different value) -> error, cannot be merged nor duplicated +// 3 .. there are some missing complementary tags and no unique conflict -> can be merged into a single line +static int sam_header_compare_lines(HeaderLine *hline1, HeaderLine *hline2) +{ + HeaderTag *t1, *t2; + + if ( hline1->type[0]!=hline2->type[0] || hline1->type[1]!=hline2->type[1] ) + return 0; + + 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 + } + + if ( unique_tags[itype] ) + { + t1 = header_line_has_tag(hline1,unique_tags[itype][0]); + t2 = header_line_has_tag(hline2,unique_tags[itype][0]); + if ( !t1 || !t2 ) // this should never happen, the unique tags are required + return 2; + + if ( strcmp(t1->value,t2->value) ) + return 0; // the unique tags differ, cannot be merged + } + if ( !required_tags[itype] && !optional_tags[itype] ) + { + t1 = hline1->tags->data; + t2 = hline2->tags->data; + if ( !strcmp(t1->value,t2->value) ) return 1; // identical comments + return 0; + } + + int missing=0, itag=0; + while ( required_tags[itype] && required_tags[itype][itag] ) + { + t1 = header_line_has_tag(hline1,required_tags[itype][itag]); + t2 = header_line_has_tag(hline2,required_tags[itype][itag]); + if ( !t1 && !t2 ) + return 2; // this should never happen + else if ( !t1 || !t2 ) + missing = 1; // there is some tag missing in one of the hlines + else if ( strcmp(t1->value,t2->value) ) + { + 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++; + } + itag = 0; + while ( optional_tags[itype] && optional_tags[itype][itag] ) + { + t1 = header_line_has_tag(hline1,optional_tags[itype][itag]); + t2 = header_line_has_tag(hline2,optional_tags[itype][itag]); + if ( !t1 && !t2 ) + { + itag++; + continue; + } + if ( !t1 || !t2 ) + missing = 1; // there is some tag missing in one of the hlines + else if ( strcmp(t1->value,t2->value) ) + { + 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++; + } + if ( missing ) return 3; // there are some missing complementary tags with no conflicts, can be merged + return 1; +} + + +static HeaderLine *sam_header_line_clone(const HeaderLine *hline) +{ + list_t *tags; + HeaderLine *out = malloc(sizeof(HeaderLine)); + out->type[0] = hline->type[0]; + out->type[1] = hline->type[1]; + out->tags = NULL; + + tags = hline->tags; + while (tags) + { + HeaderTag *old = tags->data; + + HeaderTag *new = malloc(sizeof(HeaderTag)); + new->key[0] = old->key[0]; + new->key[1] = old->key[1]; + new->value = strdup(old->value); + out->tags = list_append(out->tags, new); + + tags = tags->next; + } + return out; +} + +static int sam_header_line_merge_with(HeaderLine *out_hline, const HeaderLine *tmpl_hline) +{ + list_t *tmpl_tags; + + 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) + { + HeaderTag *tmpl_tag = tmpl_tags->data; + HeaderTag *out_tag = header_line_has_tag(out_hline, tmpl_tag->key); + if ( !out_tag ) + { + HeaderTag *tag = malloc(sizeof(HeaderTag)); + tag->key[0] = tmpl_tag->key[0]; + tag->key[1] = tmpl_tag->key[1]; + tag->value = strdup(tmpl_tag->value); + out_hline->tags = list_append(out_hline->tags,tag); + } + tmpl_tags = tmpl_tags->next; + } + return 1; +} + + +static HeaderLine *sam_header_line_parse(const char *headerLine) +{ + HeaderLine *hline; + HeaderTag *tag; + const char *from, *to; + from = headerLine; + + if ( *from != '@' ) { + 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; + } + + 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)); + return 0; + } + from = to; + while (*from) + { + while (*to && *to!='\t') to++; + + if ( !required_tags[itype] && !optional_tags[itype] ) + { + // CO is a special case, it can contain anything, including tabs + if ( *to ) { to++; continue; } + tag = new_tag(" ",from,to-1); + } + else + tag = new_tag(from,from+3,to-1); + + 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; + } + + from = to; + } + return hline; +} + + +// Must be of an existing type, all tags must be recognised and all required tags must be present +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 ) + { + debug("The type [%c%c] not recognised.\n", hline->type[0],hline->type[1]); + return 0; + } + + // Has all required tags? + itag = 0; + while ( required_tags[itype] && required_tags[itype][itag] ) + { + if ( !header_line_has_tag(hline,required_tags[itype][itag]) ) + { + debug("The tag [%c%c] required for [%c%c] not present.\n", required_tags[itype][itag][0],required_tags[itype][itag][1], + hline->type[0],hline->type[1]); + return 0; + } + itag++; + } + + // Are all tags recognised? + tags = hline->tags; + while ( tags ) + { + tag = tags->data; + if ( !tag_exists(tag->key,required_tags[itype]) && !tag_exists(tag->key,optional_tags[itype]) ) + { + debug("Unknown tag [%c%c] for [%c%c].\n", tag->key[0],tag->key[1], hline->type[0],hline->type[1]); + return 0; + } + tags = tags->next; + } + + return 1; +} + + +static void print_header_line(FILE *fp, HeaderLine *hline) +{ + list_t *tags = hline->tags; + HeaderTag *tag; + + fprintf(fp, "@%c%c", hline->type[0],hline->type[1]); + while (tags) + { + tag = tags->data; + + fprintf(fp, "\t"); + if ( tag->key[0]!=' ' || tag->key[1]!=' ' ) + fprintf(fp, "%c%c:", tag->key[0],tag->key[1]); + fprintf(fp, "%s", tag->value); + + tags = tags->next; + } + fprintf(fp,"\n"); +} + + +static void sam_header_line_free(HeaderLine *hline) +{ + list_t *tags = hline->tags; + while (tags) + { + HeaderTag *tag = tags->data; + free(tag->value); + free(tag); + tags = tags->next; + } + list_free(hline->tags); + free(hline); +} + +void sam_header_free(void *_header) +{ + HeaderDict *header = (HeaderDict*)_header; + list_t *hlines = header; + while (hlines) + { + sam_header_line_free(hlines->data); + hlines = hlines->next; + } + list_free(header); +} + +HeaderDict *sam_header_clone(const HeaderDict *dict) +{ + HeaderDict *out = NULL; + while (dict) + { + HeaderLine *hline = dict->data; + out = list_append(out, sam_header_line_clone(hline)); + dict = dict->next; + } + return out; +} + +// Returns a newly allocated string +char *sam_header_write(const void *_header) +{ + const HeaderDict *header = (const HeaderDict*)_header; + char *out = NULL; + int len=0, nout=0; + const list_t *hlines; + + // Calculate the length of the string to allocate + hlines = header; + while (hlines) + { + len += 4; // @XY and \n + + HeaderLine *hline = hlines->data; + list_t *tags = hline->tags; + while (tags) + { + HeaderTag *tag = tags->data; + len += strlen(tag->value) + 1; // \t + if ( tag->key[0]!=' ' || tag->key[1]!=' ' ) + len += strlen(tag->value) + 3; // XY: + tags = tags->next; + } + hlines = hlines->next; + } + + nout = 0; + out = malloc(len+1); + hlines = header; + while (hlines) + { + HeaderLine *hline = hlines->data; + + nout += sprintf(out+nout,"@%c%c",hline->type[0],hline->type[1]); + + list_t *tags = hline->tags; + while (tags) + { + HeaderTag *tag = tags->data; + nout += sprintf(out+nout,"\t"); + if ( tag->key[0]!=' ' || tag->key[1]!=' ' ) + nout += sprintf(out+nout,"%c%c:", tag->key[0],tag->key[1]); + nout += sprintf(out+nout,"%s", tag->value); + tags = tags->next; + } + hlines = hlines->next; + nout += sprintf(out+nout,"\n"); + } + out[len] = 0; + return out; +} + +void *sam_header_parse2(const char *headerText) +{ + list_t *hlines = NULL; + HeaderLine *hline; + const char *text; + char *buf=NULL; + size_t nbuf = 0; + + if ( !headerText ) + return 0; + + text = headerText; + while ( (text=nextline(&buf, &nbuf, text)) ) + { + hline = sam_header_line_parse(buf); + if ( hline && sam_header_line_validate(hline) ) + // With too many (~250,000) reference sequences the header parsing was too slow with list_append. + hlines = list_append_to_end(hlines, hline); + else + { + if (hline) sam_header_line_free(hline); + sam_header_free(hlines); + if ( buf ) free(buf); + return NULL; + } + } + if ( buf ) free(buf); + + return hlines; +} + +void *sam_header2tbl(const void *_dict, char type[2], char key_tag[2], char value_tag[2]) +{ + 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 + while (l) + { + HeaderLine *hline = l->data; + 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); + 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); + k = kh_put(str, tbl, key->value, &ret); + kh_value(tbl, k) = value->value; + + l = l->next; + } + return tbl; +} + +char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n) +{ + const HeaderDict *dict = (const HeaderDict*)_dict; + const list_t *l = dict; + int max, n; + char **ret; + + ret = 0; *_n = max = n = 0; + while (l) + { + HeaderLine *hline = l->data; + 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 ) + { + l = l->next; + continue; + } + + if (n == max) { + max = max? max<<1 : 4; + ret = realloc(ret, max * sizeof(void*)); + } + ret[n++] = key->value; + + l = l->next; + } + *_n = n; + return ret; +} + +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); +} + +int sam_tbl_size(void *h) +{ + 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); +} + +void *sam_header_merge(int n, const void **_dicts) +{ + const HeaderDict **dicts = (const HeaderDict**)_dicts; + HeaderDict *out_dict; + int idict, status; + + if ( n<2 ) return NULL; + + out_dict = sam_header_clone(dicts[0]); + + for (idict=1; idictdata, out_hlines->data); + if ( status==0 ) + { + out_hlines = out_hlines->next; + continue; + } + + 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; + } + if ( status==3 ) + sam_header_line_merge_with(out_hlines->data, tmpl_hlines->data); + + inserted = 1; + break; + } + if ( !inserted ) + out_dict = list_append(out_dict, sam_header_line_clone(tmpl_hlines->data)); + + tmpl_hlines = tmpl_hlines->next; + } + } + + return out_dict; +} + + diff --git a/samtools/sam_view.c b/samtools/sam_view.c deleted file mode 100644 index 3b10e2e..0000000 --- a/samtools/sam_view.c +++ /dev/null @@ -1,255 +0,0 @@ -#include -#include -#include -#include -#include -#include "sam_header.h" -#include "sam.h" -#include "faidx.h" -#include "khash.h" -KHASH_SET_INIT_STR(rg) - -typedef khash_t(rg) *rghash_t; - -rghash_t g_rghash = 0; -static int g_min_mapQ = 0, g_flag_on = 0, g_flag_off = 0; -static char *g_library, *g_rg; -static int g_sol2sanger_tbl[128]; - -static void sol2sanger(bam1_t *b) -{ - int l; - uint8_t *qual = bam1_qual(b); - if (g_sol2sanger_tbl[30] == 0) { - for (l = 0; l != 128; ++l) { - g_sol2sanger_tbl[l] = (int)(10.0 * log(1.0 + pow(10.0, (l - 64 + 33) / 10.0)) / log(10.0) + .499); - if (g_sol2sanger_tbl[l] >= 93) g_sol2sanger_tbl[l] = 93; - } - } - for (l = 0; l < b->core.l_qseq; ++l) { - int q = qual[l]; - if (q > 127) q = 127; - qual[l] = g_sol2sanger_tbl[q]; - } -} - -static inline int __g_skip_aln(const bam_header_t *h, const bam1_t *b) -{ - 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_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; -} - -// callback function for bam_fetch() -static int view_func(const bam1_t *b, void *data) -{ - if (!__g_skip_aln(((samfile_t*)data)->header, b)) - samwrite((samfile_t*)data, b); - return 0; -} - -static int usage(int is_long_help); - -int main_samview(int argc, char *argv[]) -{ - int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, is_uncompressed = 0, is_bamout = 0, slx2sngr = 0; - int of_type = BAM_OFDEC, is_long_help = 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; - - /* parse command-line options */ - strcpy(in_mode, "r"); strcpy(out_mode, "w"); - while ((c = getopt(argc, argv, "Sbt:hHo:q:f:F:ul:r:xX?T:CR:")) >= 0) { - switch (c) { - case 'C': slx2sngr = 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': is_uncompressed = 1; break; - case 'l': g_library = strdup(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; - default: return usage(is_long_help); - } - } - if (is_uncompressed) 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 (is_uncompressed) strcat(out_mode, "u"); - 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(stderr, "[main_samview] fail to open file for reading.\n"); - goto view_end; - } - if (in->header == 0) { - fprintf(stderr, "[main_samview] fail to read the header.\n"); - goto view_end; - } - if ((out = samopen(fn_out? fn_out : "-", out_mode, in->header)) == 0) { - fprintf(stderr, "[main_samview] fail to open file for writing.\n"); - goto view_end; - } - 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 (!__g_skip_aln(in->header, b)) { - if (slx2sngr) sol2sanger(b); - samwrite(out, b); // write the alignment to `out' - } - } - if (r < -1) fprintf(stderr, "[main_samview] truncated file.\n"); - 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(stderr, "[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; - 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(stderr, "[main_samview] fail to get the reference name. Continue anyway.\n"); - continue; - } - bam_fetch(in->x.bam, idx, tid, beg, end, out, view_func); // fetch alignments - } - bam_index_destroy(idx); // destroy the BAM index - } - -view_end: - // 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_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); - samclose(out); - return ret; -} - -static int usage(int is_long_help) -{ - fprintf(stderr, "\n"); - fprintf(stderr, "Usage: samtools view [options] | [region1 [...]]\n\n"); - fprintf(stderr, "Options: -b output BAM\n"); - fprintf(stderr, " -h print header for the SAM output\n"); - fprintf(stderr, " -H print header only (no alignments)\n"); - fprintf(stderr, " -S input is SAM\n"); - fprintf(stderr, " -u uncompressed BAM output (force -b)\n"); - fprintf(stderr, " -x output FLAG in HEX (samtools-C specific)\n"); - fprintf(stderr, " -X output FLAG in string (samtools-C specific)\n"); - fprintf(stderr, " -t FILE list of reference names and lengths (force -S) [null]\n"); - fprintf(stderr, " -T FILE reference sequence file (force -S) [null]\n"); - fprintf(stderr, " -o FILE output file name [stdout]\n"); - fprintf(stderr, " -R FILE list of read groups to be outputted [null]\n"); - fprintf(stderr, " -f INT required flag, 0 for unset [0]\n"); - fprintf(stderr, " -F INT filtering flag, 0 for unset [0]\n"); - fprintf(stderr, " -q INT minimum mapping quality [0]\n"); - fprintf(stderr, " -l STR only output reads in library STR [null]\n"); - fprintf(stderr, " -r STR only output reads in read group STR [null]\n"); - fprintf(stderr, " -? longer help\n"); - fprintf(stderr, "\n"); - if (is_long_help) - fprintf(stderr, "Notes:\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\ -\n\ - 2. SAM->BAM conversion: `samtools view -bT ref.fa in.sam.gz'.\n\ -\n\ - 3. BAM->SAM conversion: `samtools view in.bam'.\n\ -\n\ - 4. 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\ -\n\ - 5. 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; -} - -int main_import(int argc, char *argv[]) -{ - int argc2, ret; - char **argv2; - if (argc != 4) { - fprintf(stderr, "Usage: bamtk import \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; -} diff --git a/samtools/sam_view.c.pysam.c b/samtools/sam_view.c.pysam.c new file mode 100644 index 0000000..0a6cbb0 --- /dev/null +++ b/samtools/sam_view.c.pysam.c @@ -0,0 +1,297 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#include "sam_header.h" +#include "sam.h" +#include "faidx.h" +#include "khash.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; + int *count; +} count_func_data_t; + +typedef khash_t(rg) *rghash_t; + +rghash_t g_rghash = 0; +static int g_min_mapQ = 0, g_flag_on = 0, g_flag_off = 0; +static char *g_library, *g_rg; +static int g_sol2sanger_tbl[128]; + +static void sol2sanger(bam1_t *b) +{ + int l; + uint8_t *qual = bam1_qual(b); + if (g_sol2sanger_tbl[30] == 0) { + for (l = 0; l != 128; ++l) { + g_sol2sanger_tbl[l] = (int)(10.0 * log(1.0 + pow(10.0, (l - 64 + 33) / 10.0)) / log(10.0) + .499); + if (g_sol2sanger_tbl[l] >= 93) g_sol2sanger_tbl[l] = 93; + } + } + for (l = 0; l < b->core.l_qseq; ++l) { + int q = qual[l]; + if (q > 127) q = 127; + qual[l] = g_sol2sanger_tbl[q]; + } +} + +static inline int __g_skip_aln(const bam_header_t *h, const bam1_t *b) +{ + 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_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; +} + +// callback function for bam_fetch() that prints nonskipped records +static int view_func(const bam1_t *b, void *data) +{ + if (!__g_skip_aln(((samfile_t*)data)->header, b)) + samwrite((samfile_t*)data, b); + return 0; +} + +// callback function for bam_fetch() that counts nonskipped records +static int count_func(const bam1_t *b, void *data) +{ + if (!__g_skip_aln(((count_func_data_t*)data)->header, b)) { + (*((count_func_data_t*)data)->count)++; + } + return 0; +} + +static int usage(int is_long_help); + +int main_samview(int argc, char *argv[]) +{ + int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, is_uncompressed = 0, is_bamout = 0, slx2sngr = 0, is_count = 0; + int of_type = BAM_OFDEC, is_long_help = 0; + int 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; + + /* parse command-line options */ + strcpy(in_mode, "r"); strcpy(out_mode, "w"); + while ((c = getopt(argc, argv, "Sbct:hHo:q:f:F:ul:r:xX?T:CR:")) >= 0) { + switch (c) { + case 'c': is_count = 1; break; + case 'C': slx2sngr = 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': is_uncompressed = 1; break; + case 'l': g_library = strdup(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; + default: return usage(is_long_help); + } + } + if (is_uncompressed) 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 (is_uncompressed) strcat(out_mode, "u"); + 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 (!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 (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 (!__g_skip_aln(in->header, b)) { + if (slx2sngr) sol2sanger(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 + } + +view_end: + if (is_count && ret == 0) { + printf("%d\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_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; +} + +static int usage(int is_long_help) +{ + fprintf(pysamerr, "\n"); + fprintf(pysamerr, "Usage: samtools view [options] | [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, " -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, " -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, " -? longer help\n"); + fprintf(pysamerr, "\n"); + if (is_long_help) + fprintf(pysamerr, "Notes:\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\ +\n\ + 2. SAM->BAM conversion: `samtools view -bT ref.fa in.sam.gz'.\n\ +\n\ + 3. BAM->SAM conversion: `samtools view in.bam'.\n\ +\n\ + 4. 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\ +\n\ + 5. 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; +} + +int main_import(int argc, char *argv[]) +{ + int argc2, ret; + char **argv2; + if (argc != 4) { + fprintf(pysamerr, "Usage: bamtk import \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; +} diff --git a/samtools/sample.c.pysam.c b/samtools/sample.c.pysam.c new file mode 100644 index 0000000..4b984a2 --- /dev/null +++ b/samtools/sample.c.pysam.c @@ -0,0 +1,94 @@ +#include "pysam.h" + +#include +#include +#include "sample.h" +#include "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; +} + +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); +} + +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); +} + +int bam_smpl_add(bam_sample_t *sm, const char *fn, const char *txt) +{ + const char *p = txt, *q, *r; + kstring_t buf; + int n = 0; + khash_t(sm) *sm2id = (khash_t(sm)*)sm->sm2id; + memset(&buf, 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); + *u = oq; *v = or; + } else break; + p = q > r? q : r; + ++n; + } + if (n == 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); +} diff --git a/samtools/sample.h b/samtools/sample.h new file mode 100644 index 0000000..85fe499 --- /dev/null +++ b/samtools/sample.h @@ -0,0 +1,17 @@ +#ifndef BAM_SAMPLE_H +#define BAM_SAMPLE_H + +#include "kstring.h" + +typedef struct { + int n, m; + char **smpl; + void *rg2smid, *sm2id; +} bam_sample_t; + +bam_sample_t *bam_smpl_init(void); +int bam_smpl_add(bam_sample_t *sm, const char *abs, const char *txt); +int bam_smpl_rg2smid(const bam_sample_t *sm, const char *fn, const char *rg, kstring_t *str); +void bam_smpl_destroy(bam_sample_t *sm); + +#endif diff --git a/samtools/win32/xcurses.h b/samtools/win32/xcurses.h new file mode 100644 index 0000000..6f3ce19 --- /dev/null +++ b/samtools/win32/xcurses.h @@ -0,0 +1,1377 @@ +/* Public Domain Curses */ + +/* $Id: curses.h,v 1.295 2008/07/15 17:13:25 wmcbrine Exp $ */ + +/*----------------------------------------------------------------------* + * PDCurses * + *----------------------------------------------------------------------*/ + +#ifndef __PDCURSES__ +#define __PDCURSES__ 1 + +/*man-start************************************************************** + +PDCurses definitions list: (Only define those needed) + + XCURSES True if compiling for X11. + PDC_RGB True if you want to use RGB color definitions + (Red = 1, Green = 2, Blue = 4) instead of BGR. + PDC_WIDE True if building wide-character support. + PDC_DLL_BUILD True if building a Win32 DLL. + NCURSES_MOUSE_VERSION Use the ncurses mouse API instead + of PDCurses' traditional mouse API. + +PDCurses portable platform definitions list: + + PDC_BUILD Defines API build version. + PDCURSES Enables access to PDCurses-only routines. + XOPEN Always true. + SYSVcurses True if you are compiling for SYSV portability. + BSDcurses True if you are compiling for BSD portability. + +**man-end****************************************************************/ + +#define PDC_BUILD 3401 +#define PDCURSES 1 /* PDCurses-only routines */ +#define XOPEN 1 /* X/Open Curses routines */ +#define SYSVcurses 1 /* System V Curses routines */ +#define BSDcurses 1 /* BSD Curses routines */ +#define CHTYPE_LONG 1 /* size of chtype; long */ + +/*----------------------------------------------------------------------*/ + +#include +#include +#include /* Required by X/Open usage below */ + +#ifdef PDC_WIDE +# include +#endif + +#if defined(__cplusplus) || defined(__cplusplus__) || defined(__CPLUSPLUS) +extern "C" +{ +# define bool _bool +#endif + +/*---------------------------------------------------------------------- + * + * PDCurses Manifest Constants + * + */ + +#ifndef FALSE +# define FALSE 0 +#endif +#ifndef TRUE +# define TRUE 1 +#endif +#ifndef NULL +# define NULL (void *)0 +#endif +#ifndef ERR +# define ERR (-1) +#endif +#ifndef OK +# define OK 0 +#endif + +/*---------------------------------------------------------------------- + * + * PDCurses Type Declarations + * + */ + +typedef unsigned char bool; /* PDCurses Boolean type */ + +#ifdef CHTYPE_LONG +# if _LP64 +typedef unsigned int chtype; +# else +typedef unsigned long chtype; /* 16-bit attr + 16-bit char */ +# endif +#else +typedef unsigned short chtype; /* 8-bit attr + 8-bit char */ +#endif + +#ifdef PDC_WIDE +typedef chtype cchar_t; +#endif + +typedef chtype attr_t; + +/*---------------------------------------------------------------------- + * + * PDCurses Mouse Interface -- SYSVR4, with extensions + * + */ + +typedef struct +{ + int x; /* absolute column, 0 based, measured in characters */ + int y; /* absolute row, 0 based, measured in characters */ + short button[3]; /* state of each button */ + int changes; /* flags indicating what has changed with the mouse */ +} MOUSE_STATUS; + +#define BUTTON_RELEASED 0x0000 +#define BUTTON_PRESSED 0x0001 +#define BUTTON_CLICKED 0x0002 +#define BUTTON_DOUBLE_CLICKED 0x0003 +#define BUTTON_TRIPLE_CLICKED 0x0004 +#define BUTTON_MOVED 0x0005 /* PDCurses */ +#define WHEEL_SCROLLED 0x0006 /* PDCurses */ +#define BUTTON_ACTION_MASK 0x0007 /* PDCurses */ + +#define PDC_BUTTON_SHIFT 0x0008 /* PDCurses */ +#define PDC_BUTTON_CONTROL 0x0010 /* PDCurses */ +#define PDC_BUTTON_ALT 0x0020 /* PDCurses */ +#define BUTTON_MODIFIER_MASK 0x0038 /* PDCurses */ + +#define MOUSE_X_POS (Mouse_status.x) +#define MOUSE_Y_POS (Mouse_status.y) + +/* + * Bits associated with the .changes field: + * 3 2 1 0 + * 210987654321098765432109876543210 + * 1 <- button 1 has changed + * 10 <- button 2 has changed + * 100 <- button 3 has changed + * 1000 <- mouse has moved + * 10000 <- mouse position report + * 100000 <- mouse wheel up + * 1000000 <- mouse wheel down + */ + +#define PDC_MOUSE_MOVED 0x0008 +#define PDC_MOUSE_POSITION 0x0010 +#define PDC_MOUSE_WHEEL_UP 0x0020 +#define PDC_MOUSE_WHEEL_DOWN 0x0040 + +#define A_BUTTON_CHANGED (Mouse_status.changes & 7) +#define MOUSE_MOVED (Mouse_status.changes & PDC_MOUSE_MOVED) +#define MOUSE_POS_REPORT (Mouse_status.changes & PDC_MOUSE_POSITION) +#define BUTTON_CHANGED(x) (Mouse_status.changes & (1 << ((x) - 1))) +#define BUTTON_STATUS(x) (Mouse_status.button[(x) - 1]) +#define MOUSE_WHEEL_UP (Mouse_status.changes & PDC_MOUSE_WHEEL_UP) +#define MOUSE_WHEEL_DOWN (Mouse_status.changes & PDC_MOUSE_WHEEL_DOWN) + +/* mouse bit-masks */ + +#define BUTTON1_RELEASED 0x00000001L +#define BUTTON1_PRESSED 0x00000002L +#define BUTTON1_CLICKED 0x00000004L +#define BUTTON1_DOUBLE_CLICKED 0x00000008L +#define BUTTON1_TRIPLE_CLICKED 0x00000010L +#define BUTTON1_MOVED 0x00000010L /* PDCurses */ + +#define BUTTON2_RELEASED 0x00000020L +#define BUTTON2_PRESSED 0x00000040L +#define BUTTON2_CLICKED 0x00000080L +#define BUTTON2_DOUBLE_CLICKED 0x00000100L +#define BUTTON2_TRIPLE_CLICKED 0x00000200L +#define BUTTON2_MOVED 0x00000200L /* PDCurses */ + +#define BUTTON3_RELEASED 0x00000400L +#define BUTTON3_PRESSED 0x00000800L +#define BUTTON3_CLICKED 0x00001000L +#define BUTTON3_DOUBLE_CLICKED 0x00002000L +#define BUTTON3_TRIPLE_CLICKED 0x00004000L +#define BUTTON3_MOVED 0x00004000L /* PDCurses */ + +/* For the ncurses-compatible functions only, BUTTON4_PRESSED and + BUTTON5_PRESSED are returned for mouse scroll wheel up and down; + otherwise PDCurses doesn't support buttons 4 and 5 */ + +#define BUTTON4_RELEASED 0x00008000L +#define BUTTON4_PRESSED 0x00010000L +#define BUTTON4_CLICKED 0x00020000L +#define BUTTON4_DOUBLE_CLICKED 0x00040000L +#define BUTTON4_TRIPLE_CLICKED 0x00080000L + +#define BUTTON5_RELEASED 0x00100000L +#define BUTTON5_PRESSED 0x00200000L +#define BUTTON5_CLICKED 0x00400000L +#define BUTTON5_DOUBLE_CLICKED 0x00800000L +#define BUTTON5_TRIPLE_CLICKED 0x01000000L + +#define MOUSE_WHEEL_SCROLL 0x02000000L /* PDCurses */ +#define BUTTON_MODIFIER_SHIFT 0x04000000L /* PDCurses */ +#define BUTTON_MODIFIER_CONTROL 0x08000000L /* PDCurses */ +#define BUTTON_MODIFIER_ALT 0x10000000L /* PDCurses */ + +#define ALL_MOUSE_EVENTS 0x1fffffffL +#define REPORT_MOUSE_POSITION 0x20000000L + +/* ncurses mouse interface */ + +typedef unsigned long mmask_t; + +typedef struct +{ + short id; /* unused, always 0 */ + int x, y, z; /* x, y same as MOUSE_STATUS; z unused */ + mmask_t bstate; /* equivalent to changes + button[], but + in the same format as used for mousemask() */ +} MEVENT; + +#ifdef NCURSES_MOUSE_VERSION +# define BUTTON_SHIFT BUTTON_MODIFIER_SHIFT +# define BUTTON_CONTROL BUTTON_MODIFIER_CONTROL +# define BUTTON_CTRL BUTTON_MODIFIER_CONTROL +# define BUTTON_ALT BUTTON_MODIFIER_ALT +#else +# define BUTTON_SHIFT PDC_BUTTON_SHIFT +# define BUTTON_CONTROL PDC_BUTTON_CONTROL +# define BUTTON_ALT PDC_BUTTON_ALT +#endif + +/*---------------------------------------------------------------------- + * + * PDCurses Structure Definitions + * + */ + +typedef struct _win /* definition of a window */ +{ + int _cury; /* current pseudo-cursor */ + int _curx; + int _maxy; /* max window coordinates */ + int _maxx; + int _begy; /* origin on screen */ + int _begx; + int _flags; /* window properties */ + chtype _attrs; /* standard attributes and colors */ + chtype _bkgd; /* background, normally blank */ + bool _clear; /* causes clear at next refresh */ + bool _leaveit; /* leaves cursor where it is */ + bool _scroll; /* allows window scrolling */ + bool _nodelay; /* input character wait flag */ + bool _immed; /* immediate update flag */ + bool _sync; /* synchronise window ancestors */ + bool _use_keypad; /* flags keypad key mode active */ + chtype **_y; /* pointer to line pointer array */ + int *_firstch; /* first changed character in line */ + int *_lastch; /* last changed character in line */ + int _tmarg; /* top of scrolling region */ + int _bmarg; /* bottom of scrolling region */ + int _delayms; /* milliseconds of delay for getch() */ + int _parx, _pary; /* coords relative to parent (0,0) */ + struct _win *_parent; /* subwin's pointer to parent win */ +} WINDOW; + +/* Avoid using the SCREEN struct directly -- use the corresponding + functions if possible. This struct may eventually be made private. */ + +typedef struct +{ + bool alive; /* if initscr() called, and not endwin() */ + bool autocr; /* if cr -> lf */ + bool cbreak; /* if terminal unbuffered */ + bool echo; /* if terminal echo */ + bool raw_inp; /* raw input mode (v. cooked input) */ + bool raw_out; /* raw output mode (7 v. 8 bits) */ + bool audible; /* FALSE if the bell is visual */ + bool mono; /* TRUE if current screen is mono */ + bool resized; /* TRUE if TERM has been resized */ + bool orig_attr; /* TRUE if we have the original colors */ + short orig_fore; /* original screen foreground color */ + short orig_back; /* original screen foreground color */ + int cursrow; /* position of physical cursor */ + int curscol; /* position of physical cursor */ + int visibility; /* visibility of cursor */ + int orig_cursor; /* original cursor size */ + int lines; /* new value for LINES */ + int cols; /* new value for COLS */ + unsigned long _trap_mbe; /* trap these mouse button events */ + unsigned long _map_mbe_to_key; /* map mouse buttons to slk */ + int mouse_wait; /* time to wait (in ms) for a + button release after a press, in + order to count it as a click */ + int slklines; /* lines in use by slk_init() */ + WINDOW *slk_winptr; /* window for slk */ + int linesrippedoff; /* lines ripped off via ripoffline() */ + int linesrippedoffontop; /* lines ripped off on + top via ripoffline() */ + int delaytenths; /* 1/10ths second to wait block + getch() for */ + bool _preserve; /* TRUE if screen background + to be preserved */ + int _restore; /* specifies if screen background + to be restored, and how */ + bool save_key_modifiers; /* TRUE if each key modifiers saved + with each key press */ + bool return_key_modifiers; /* TRUE if modifier keys are + returned as "real" keys */ + bool key_code; /* TRUE if last key is a special key; + used internally by get_wch() */ +#ifdef XCURSES + int XcurscrSize; /* size of Xcurscr shared memory block */ + bool sb_on; + int sb_viewport_y; + int sb_viewport_x; + int sb_total_y; + int sb_total_x; + int sb_cur_y; + int sb_cur_x; +#endif + short line_color; /* color of line attributes - default -1 */ +} SCREEN; + +/*---------------------------------------------------------------------- + * + * PDCurses External Variables + * + */ + +#ifdef PDC_DLL_BUILD +# ifdef CURSES_LIBRARY +# define PDCEX __declspec(dllexport) extern +# else +# define PDCEX __declspec(dllimport) +# endif +#else +# define PDCEX extern +#endif + +PDCEX int LINES; /* terminal height */ +PDCEX int COLS; /* terminal width */ +PDCEX WINDOW *stdscr; /* the default screen window */ +PDCEX WINDOW *curscr; /* the current screen image */ +PDCEX SCREEN *SP; /* curses variables */ +PDCEX MOUSE_STATUS Mouse_status; +PDCEX int COLORS; +PDCEX int COLOR_PAIRS; +PDCEX int TABSIZE; +PDCEX chtype acs_map[]; /* alternate character set map */ +PDCEX char ttytype[]; /* terminal name/description */ + +/*man-start************************************************************** + +PDCurses Text Attributes +======================== + +Originally, PDCurses used a short (16 bits) for its chtype. To include +color, a number of things had to be sacrificed from the strict Unix and +System V support. The main problem was fitting all character attributes +and color into an unsigned char (all 8 bits!). + +Today, PDCurses by default uses a long (32 bits) for its chtype, as in +System V. The short chtype is still available, by undefining CHTYPE_LONG +and rebuilding the library. + +The following is the structure of a win->_attrs chtype: + +short form: + +------------------------------------------------- +|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0| +------------------------------------------------- + color number | attrs | character eg 'a' + +The available non-color attributes are bold, reverse and blink. Others +have no effect. The high order char is an index into an array of +physical colors (defined in color.c) -- 32 foreground/background color +pairs (5 bits) plus 3 bits for other attributes. + +long form: + +---------------------------------------------------------------------------- +|31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|..| 3| 2| 1| 0| +---------------------------------------------------------------------------- + color number | modifiers | character eg 'a' + +The available non-color attributes are bold, underline, invisible, +right-line, left-line, protect, reverse and blink. 256 color pairs (8 +bits), 8 bits for other attributes, and 16 bits for character data. + +**man-end****************************************************************/ + +/*** Video attribute macros ***/ + +#define A_NORMAL (chtype)0 + +#ifdef CHTYPE_LONG +# define A_ALTCHARSET (chtype)0x00010000 +# define A_RIGHTLINE (chtype)0x00020000 +# define A_LEFTLINE (chtype)0x00040000 +# define A_INVIS (chtype)0x00080000 +# define A_UNDERLINE (chtype)0x00100000 +# define A_REVERSE (chtype)0x00200000 +# define A_BLINK (chtype)0x00400000 +# define A_BOLD (chtype)0x00800000 + +# define A_ATTRIBUTES (chtype)0xffff0000 +# define A_CHARTEXT (chtype)0x0000ffff +# define A_COLOR (chtype)0xff000000 + +# define A_ITALIC A_INVIS +# define A_PROTECT (A_UNDERLINE | A_LEFTLINE | A_RIGHTLINE) + +# define PDC_ATTR_SHIFT 19 +# define PDC_COLOR_SHIFT 24 +#else +# define A_BOLD (chtype)0x0100 /* X/Open */ +# define A_REVERSE (chtype)0x0200 /* X/Open */ +# define A_BLINK (chtype)0x0400 /* X/Open */ + +# define A_ATTRIBUTES (chtype)0xff00 /* X/Open */ +# define A_CHARTEXT (chtype)0x00ff /* X/Open */ +# define A_COLOR (chtype)0xf800 /* System V */ + +# define A_ALTCHARSET A_NORMAL /* X/Open */ +# define A_PROTECT A_NORMAL /* X/Open */ +# define A_UNDERLINE A_NORMAL /* X/Open */ + +# define A_LEFTLINE A_NORMAL +# define A_RIGHTLINE A_NORMAL +# define A_ITALIC A_NORMAL +# define A_INVIS A_NORMAL + +# define PDC_ATTR_SHIFT 8 +# define PDC_COLOR_SHIFT 11 +#endif + +#define A_STANDOUT (A_REVERSE | A_BOLD) /* X/Open */ +#define A_DIM A_NORMAL + +#define CHR_MSK A_CHARTEXT /* Obsolete */ +#define ATR_MSK A_ATTRIBUTES /* Obsolete */ +#define ATR_NRM A_NORMAL /* Obsolete */ + +/* For use with attr_t -- X/Open says, "these shall be distinct", so + this is a non-conforming implementation. */ + +#define WA_ALTCHARSET A_ALTCHARSET +#define WA_BLINK A_BLINK +#define WA_BOLD A_BOLD +#define WA_DIM A_DIM +#define WA_INVIS A_INVIS +#define WA_LEFT A_LEFTLINE +#define WA_PROTECT A_PROTECT +#define WA_REVERSE A_REVERSE +#define WA_RIGHT A_RIGHTLINE +#define WA_STANDOUT A_STANDOUT +#define WA_UNDERLINE A_UNDERLINE + +#define WA_HORIZONTAL A_NORMAL +#define WA_LOW A_NORMAL +#define WA_TOP A_NORMAL +#define WA_VERTICAL A_NORMAL + +/*** Alternate character set macros ***/ + +/* 'w' = 32-bit chtype; acs_map[] index | A_ALTCHARSET + 'n' = 16-bit chtype; it gets the fallback set because no bit is + available for A_ALTCHARSET */ + +#ifdef CHTYPE_LONG +# define ACS_PICK(w, n) ((chtype)w | A_ALTCHARSET) +#else +# define ACS_PICK(w, n) ((chtype)n) +#endif + +/* VT100-compatible symbols -- box chars */ + +#define ACS_ULCORNER ACS_PICK('l', '+') +#define ACS_LLCORNER ACS_PICK('m', '+') +#define ACS_URCORNER ACS_PICK('k', '+') +#define ACS_LRCORNER ACS_PICK('j', '+') +#define ACS_RTEE ACS_PICK('u', '+') +#define ACS_LTEE ACS_PICK('t', '+') +#define ACS_BTEE ACS_PICK('v', '+') +#define ACS_TTEE ACS_PICK('w', '+') +#define ACS_HLINE ACS_PICK('q', '-') +#define ACS_VLINE ACS_PICK('x', '|') +#define ACS_PLUS ACS_PICK('n', '+') + +/* VT100-compatible symbols -- other */ + +#define ACS_S1 ACS_PICK('o', '-') +#define ACS_S9 ACS_PICK('s', '_') +#define ACS_DIAMOND ACS_PICK('`', '+') +#define ACS_CKBOARD ACS_PICK('a', ':') +#define ACS_DEGREE ACS_PICK('f', '\'') +#define ACS_PLMINUS ACS_PICK('g', '#') +#define ACS_BULLET ACS_PICK('~', 'o') + +/* Teletype 5410v1 symbols -- these are defined in SysV curses, but + are not well-supported by most terminals. Stick to VT100 characters + for optimum portability. */ + +#define ACS_LARROW ACS_PICK(',', '<') +#define ACS_RARROW ACS_PICK('+', '>') +#define ACS_DARROW ACS_PICK('.', 'v') +#define ACS_UARROW ACS_PICK('-', '^') +#define ACS_BOARD ACS_PICK('h', '#') +#define ACS_LANTERN ACS_PICK('i', '*') +#define ACS_BLOCK ACS_PICK('0', '#') + +/* That goes double for these -- undocumented SysV symbols. Don't use + them. */ + +#define ACS_S3 ACS_PICK('p', '-') +#define ACS_S7 ACS_PICK('r', '-') +#define ACS_LEQUAL ACS_PICK('y', '<') +#define ACS_GEQUAL ACS_PICK('z', '>') +#define ACS_PI ACS_PICK('{', 'n') +#define ACS_NEQUAL ACS_PICK('|', '+') +#define ACS_STERLING ACS_PICK('}', 'L') + +/* Box char aliases */ + +#define ACS_BSSB ACS_ULCORNER +#define ACS_SSBB ACS_LLCORNER +#define ACS_BBSS ACS_URCORNER +#define ACS_SBBS ACS_LRCORNER +#define ACS_SBSS ACS_RTEE +#define ACS_SSSB ACS_LTEE +#define ACS_SSBS ACS_BTEE +#define ACS_BSSS ACS_TTEE +#define ACS_BSBS ACS_HLINE +#define ACS_SBSB ACS_VLINE +#define ACS_SSSS ACS_PLUS + +/* cchar_t aliases */ + +#ifdef PDC_WIDE +# define WACS_ULCORNER (&(acs_map['l'])) +# define WACS_LLCORNER (&(acs_map['m'])) +# define WACS_URCORNER (&(acs_map['k'])) +# define WACS_LRCORNER (&(acs_map['j'])) +# define WACS_RTEE (&(acs_map['u'])) +# define WACS_LTEE (&(acs_map['t'])) +# define WACS_BTEE (&(acs_map['v'])) +# define WACS_TTEE (&(acs_map['w'])) +# define WACS_HLINE (&(acs_map['q'])) +# define WACS_VLINE (&(acs_map['x'])) +# define WACS_PLUS (&(acs_map['n'])) + +# define WACS_S1 (&(acs_map['o'])) +# define WACS_S9 (&(acs_map['s'])) +# define WACS_DIAMOND (&(acs_map['`'])) +# define WACS_CKBOARD (&(acs_map['a'])) +# define WACS_DEGREE (&(acs_map['f'])) +# define WACS_PLMINUS (&(acs_map['g'])) +# define WACS_BULLET (&(acs_map['~'])) + +# define WACS_LARROW (&(acs_map[','])) +# define WACS_RARROW (&(acs_map['+'])) +# define WACS_DARROW (&(acs_map['.'])) +# define WACS_UARROW (&(acs_map['-'])) +# define WACS_BOARD (&(acs_map['h'])) +# define WACS_LANTERN (&(acs_map['i'])) +# define WACS_BLOCK (&(acs_map['0'])) + +# define WACS_S3 (&(acs_map['p'])) +# define WACS_S7 (&(acs_map['r'])) +# define WACS_LEQUAL (&(acs_map['y'])) +# define WACS_GEQUAL (&(acs_map['z'])) +# define WACS_PI (&(acs_map['{'])) +# define WACS_NEQUAL (&(acs_map['|'])) +# define WACS_STERLING (&(acs_map['}'])) + +# define WACS_BSSB WACS_ULCORNER +# define WACS_SSBB WACS_LLCORNER +# define WACS_BBSS WACS_URCORNER +# define WACS_SBBS WACS_LRCORNER +# define WACS_SBSS WACS_RTEE +# define WACS_SSSB WACS_LTEE +# define WACS_SSBS WACS_BTEE +# define WACS_BSSS WACS_TTEE +# define WACS_BSBS WACS_HLINE +# define WACS_SBSB WACS_VLINE +# define WACS_SSSS WACS_PLUS +#endif + +/*** Color macros ***/ + +#define COLOR_BLACK 0 + +#ifdef PDC_RGB /* RGB */ +# define COLOR_RED 1 +# define COLOR_GREEN 2 +# define COLOR_BLUE 4 +#else /* BGR */ +# define COLOR_BLUE 1 +# define COLOR_GREEN 2 +# define COLOR_RED 4 +#endif + +#define COLOR_CYAN (COLOR_BLUE | COLOR_GREEN) +#define COLOR_MAGENTA (COLOR_RED | COLOR_BLUE) +#define COLOR_YELLOW (COLOR_RED | COLOR_GREEN) + +#define COLOR_WHITE 7 + +/*---------------------------------------------------------------------- + * + * Function and Keypad Key Definitions. + * Many are just for compatibility. + * + */ + +#define KEY_CODE_YES 0x100 /* If get_wch() gives a key code */ + +#define KEY_BREAK 0x101 /* Not on PC KBD */ +#define KEY_DOWN 0x102 /* Down arrow key */ +#define KEY_UP 0x103 /* Up arrow key */ +#define KEY_LEFT 0x104 /* Left arrow key */ +#define KEY_RIGHT 0x105 /* Right arrow key */ +#define KEY_HOME 0x106 /* home key */ +#define KEY_BACKSPACE 0x107 /* not on pc */ +#define KEY_F0 0x108 /* function keys; 64 reserved */ + +#define KEY_DL 0x148 /* delete line */ +#define KEY_IL 0x149 /* insert line */ +#define KEY_DC 0x14a /* delete character */ +#define KEY_IC 0x14b /* insert char or enter ins mode */ +#define KEY_EIC 0x14c /* exit insert char mode */ +#define KEY_CLEAR 0x14d /* clear screen */ +#define KEY_EOS 0x14e /* clear to end of screen */ +#define KEY_EOL 0x14f /* clear to end of line */ +#define KEY_SF 0x150 /* scroll 1 line forward */ +#define KEY_SR 0x151 /* scroll 1 line back (reverse) */ +#define KEY_NPAGE 0x152 /* next page */ +#define KEY_PPAGE 0x153 /* previous page */ +#define KEY_STAB 0x154 /* set tab */ +#define KEY_CTAB 0x155 /* clear tab */ +#define KEY_CATAB 0x156 /* clear all tabs */ +#define KEY_ENTER 0x157 /* enter or send (unreliable) */ +#define KEY_SRESET 0x158 /* soft/reset (partial/unreliable) */ +#define KEY_RESET 0x159 /* reset/hard reset (unreliable) */ +#define KEY_PRINT 0x15a /* print/copy */ +#define KEY_LL 0x15b /* home down/bottom (lower left) */ +#define KEY_ABORT 0x15c /* abort/terminate key (any) */ +#define KEY_SHELP 0x15d /* short help */ +#define KEY_LHELP 0x15e /* long help */ +#define KEY_BTAB 0x15f /* Back tab key */ +#define KEY_BEG 0x160 /* beg(inning) key */ +#define KEY_CANCEL 0x161 /* cancel key */ +#define KEY_CLOSE 0x162 /* close key */ +#define KEY_COMMAND 0x163 /* cmd (command) key */ +#define KEY_COPY 0x164 /* copy key */ +#define KEY_CREATE 0x165 /* create key */ +#define KEY_END 0x166 /* end key */ +#define KEY_EXIT 0x167 /* exit key */ +#define KEY_FIND 0x168 /* find key */ +#define KEY_HELP 0x169 /* help key */ +#define KEY_MARK 0x16a /* mark key */ +#define KEY_MESSAGE 0x16b /* message key */ +#define KEY_MOVE 0x16c /* move key */ +#define KEY_NEXT 0x16d /* next object key */ +#define KEY_OPEN 0x16e /* open key */ +#define KEY_OPTIONS 0x16f /* options key */ +#define KEY_PREVIOUS 0x170 /* previous object key */ +#define KEY_REDO 0x171 /* redo key */ +#define KEY_REFERENCE 0x172 /* ref(erence) key */ +#define KEY_REFRESH 0x173 /* refresh key */ +#define KEY_REPLACE 0x174 /* replace key */ +#define KEY_RESTART 0x175 /* restart key */ +#define KEY_RESUME 0x176 /* resume key */ +#define KEY_SAVE 0x177 /* save key */ +#define KEY_SBEG 0x178 /* shifted beginning key */ +#define KEY_SCANCEL 0x179 /* shifted cancel key */ +#define KEY_SCOMMAND 0x17a /* shifted command key */ +#define KEY_SCOPY 0x17b /* shifted copy key */ +#define KEY_SCREATE 0x17c /* shifted create key */ +#define KEY_SDC 0x17d /* shifted delete char key */ +#define KEY_SDL 0x17e /* shifted delete line key */ +#define KEY_SELECT 0x17f /* select key */ +#define KEY_SEND 0x180 /* shifted end key */ +#define KEY_SEOL 0x181 /* shifted clear line key */ +#define KEY_SEXIT 0x182 /* shifted exit key */ +#define KEY_SFIND 0x183 /* shifted find key */ +#define KEY_SHOME 0x184 /* shifted home key */ +#define KEY_SIC 0x185 /* shifted input key */ + +#define KEY_SLEFT 0x187 /* shifted left arrow key */ +#define KEY_SMESSAGE 0x188 /* shifted message key */ +#define KEY_SMOVE 0x189 /* shifted move key */ +#define KEY_SNEXT 0x18a /* shifted next key */ +#define KEY_SOPTIONS 0x18b /* shifted options key */ +#define KEY_SPREVIOUS 0x18c /* shifted prev key */ +#define KEY_SPRINT 0x18d /* shifted print key */ +#define KEY_SREDO 0x18e /* shifted redo key */ +#define KEY_SREPLACE 0x18f /* shifted replace key */ +#define KEY_SRIGHT 0x190 /* shifted right arrow */ +#define KEY_SRSUME 0x191 /* shifted resume key */ +#define KEY_SSAVE 0x192 /* shifted save key */ +#define KEY_SSUSPEND 0x193 /* shifted suspend key */ +#define KEY_SUNDO 0x194 /* shifted undo key */ +#define KEY_SUSPEND 0x195 /* suspend key */ +#define KEY_UNDO 0x196 /* undo key */ + +/* PDCurses-specific key definitions -- PC only */ + +#define ALT_0 0x197 +#define ALT_1 0x198 +#define ALT_2 0x199 +#define ALT_3 0x19a +#define ALT_4 0x19b +#define ALT_5 0x19c +#define ALT_6 0x19d +#define ALT_7 0x19e +#define ALT_8 0x19f +#define ALT_9 0x1a0 +#define ALT_A 0x1a1 +#define ALT_B 0x1a2 +#define ALT_C 0x1a3 +#define ALT_D 0x1a4 +#define ALT_E 0x1a5 +#define ALT_F 0x1a6 +#define ALT_G 0x1a7 +#define ALT_H 0x1a8 +#define ALT_I 0x1a9 +#define ALT_J 0x1aa +#define ALT_K 0x1ab +#define ALT_L 0x1ac +#define ALT_M 0x1ad +#define ALT_N 0x1ae +#define ALT_O 0x1af +#define ALT_P 0x1b0 +#define ALT_Q 0x1b1 +#define ALT_R 0x1b2 +#define ALT_S 0x1b3 +#define ALT_T 0x1b4 +#define ALT_U 0x1b5 +#define ALT_V 0x1b6 +#define ALT_W 0x1b7 +#define ALT_X 0x1b8 +#define ALT_Y 0x1b9 +#define ALT_Z 0x1ba + +#define CTL_LEFT 0x1bb /* Control-Left-Arrow */ +#define CTL_RIGHT 0x1bc +#define CTL_PGUP 0x1bd +#define CTL_PGDN 0x1be +#define CTL_HOME 0x1bf +#define CTL_END 0x1c0 + +#define KEY_A1 0x1c1 /* upper left on Virtual keypad */ +#define KEY_A2 0x1c2 /* upper middle on Virt. keypad */ +#define KEY_A3 0x1c3 /* upper right on Vir. keypad */ +#define KEY_B1 0x1c4 /* middle left on Virt. keypad */ +#define KEY_B2 0x1c5 /* center on Virt. keypad */ +#define KEY_B3 0x1c6 /* middle right on Vir. keypad */ +#define KEY_C1 0x1c7 /* lower left on Virt. keypad */ +#define KEY_C2 0x1c8 /* lower middle on Virt. keypad */ +#define KEY_C3 0x1c9 /* lower right on Vir. keypad */ + +#define PADSLASH 0x1ca /* slash on keypad */ +#define PADENTER 0x1cb /* enter on keypad */ +#define CTL_PADENTER 0x1cc /* ctl-enter on keypad */ +#define ALT_PADENTER 0x1cd /* alt-enter on keypad */ +#define PADSTOP 0x1ce /* stop on keypad */ +#define PADSTAR 0x1cf /* star on keypad */ +#define PADMINUS 0x1d0 /* minus on keypad */ +#define PADPLUS 0x1d1 /* plus on keypad */ +#define CTL_PADSTOP 0x1d2 /* ctl-stop on keypad */ +#define CTL_PADCENTER 0x1d3 /* ctl-enter on keypad */ +#define CTL_PADPLUS 0x1d4 /* ctl-plus on keypad */ +#define CTL_PADMINUS 0x1d5 /* ctl-minus on keypad */ +#define CTL_PADSLASH 0x1d6 /* ctl-slash on keypad */ +#define CTL_PADSTAR 0x1d7 /* ctl-star on keypad */ +#define ALT_PADPLUS 0x1d8 /* alt-plus on keypad */ +#define ALT_PADMINUS 0x1d9 /* alt-minus on keypad */ +#define ALT_PADSLASH 0x1da /* alt-slash on keypad */ +#define ALT_PADSTAR 0x1db /* alt-star on keypad */ +#define ALT_PADSTOP 0x1dc /* alt-stop on keypad */ +#define CTL_INS 0x1dd /* ctl-insert */ +#define ALT_DEL 0x1de /* alt-delete */ +#define ALT_INS 0x1df /* alt-insert */ +#define CTL_UP 0x1e0 /* ctl-up arrow */ +#define CTL_DOWN 0x1e1 /* ctl-down arrow */ +#define CTL_TAB 0x1e2 /* ctl-tab */ +#define ALT_TAB 0x1e3 +#define ALT_MINUS 0x1e4 +#define ALT_EQUAL 0x1e5 +#define ALT_HOME 0x1e6 +#define ALT_PGUP 0x1e7 +#define ALT_PGDN 0x1e8 +#define ALT_END 0x1e9 +#define ALT_UP 0x1ea /* alt-up arrow */ +#define ALT_DOWN 0x1eb /* alt-down arrow */ +#define ALT_RIGHT 0x1ec /* alt-right arrow */ +#define ALT_LEFT 0x1ed /* alt-left arrow */ +#define ALT_ENTER 0x1ee /* alt-enter */ +#define ALT_ESC 0x1ef /* alt-escape */ +#define ALT_BQUOTE 0x1f0 /* alt-back quote */ +#define ALT_LBRACKET 0x1f1 /* alt-left bracket */ +#define ALT_RBRACKET 0x1f2 /* alt-right bracket */ +#define ALT_SEMICOLON 0x1f3 /* alt-semi-colon */ +#define ALT_FQUOTE 0x1f4 /* alt-forward quote */ +#define ALT_COMMA 0x1f5 /* alt-comma */ +#define ALT_STOP 0x1f6 /* alt-stop */ +#define ALT_FSLASH 0x1f7 /* alt-forward slash */ +#define ALT_BKSP 0x1f8 /* alt-backspace */ +#define CTL_BKSP 0x1f9 /* ctl-backspace */ +#define PAD0 0x1fa /* keypad 0 */ + +#define CTL_PAD0 0x1fb /* ctl-keypad 0 */ +#define CTL_PAD1 0x1fc +#define CTL_PAD2 0x1fd +#define CTL_PAD3 0x1fe +#define CTL_PAD4 0x1ff +#define CTL_PAD5 0x200 +#define CTL_PAD6 0x201 +#define CTL_PAD7 0x202 +#define CTL_PAD8 0x203 +#define CTL_PAD9 0x204 + +#define ALT_PAD0 0x205 /* alt-keypad 0 */ +#define ALT_PAD1 0x206 +#define ALT_PAD2 0x207 +#define ALT_PAD3 0x208 +#define ALT_PAD4 0x209 +#define ALT_PAD5 0x20a +#define ALT_PAD6 0x20b +#define ALT_PAD7 0x20c +#define ALT_PAD8 0x20d +#define ALT_PAD9 0x20e + +#define CTL_DEL 0x20f /* clt-delete */ +#define ALT_BSLASH 0x210 /* alt-back slash */ +#define CTL_ENTER 0x211 /* ctl-enter */ + +#define SHF_PADENTER 0x212 /* shift-enter on keypad */ +#define SHF_PADSLASH 0x213 /* shift-slash on keypad */ +#define SHF_PADSTAR 0x214 /* shift-star on keypad */ +#define SHF_PADPLUS 0x215 /* shift-plus on keypad */ +#define SHF_PADMINUS 0x216 /* shift-minus on keypad */ +#define SHF_UP 0x217 /* shift-up on keypad */ +#define SHF_DOWN 0x218 /* shift-down on keypad */ +#define SHF_IC 0x219 /* shift-insert on keypad */ +#define SHF_DC 0x21a /* shift-delete on keypad */ + +#define KEY_MOUSE 0x21b /* "mouse" key */ +#define KEY_SHIFT_L 0x21c /* Left-shift */ +#define KEY_SHIFT_R 0x21d /* Right-shift */ +#define KEY_CONTROL_L 0x21e /* Left-control */ +#define KEY_CONTROL_R 0x21f /* Right-control */ +#define KEY_ALT_L 0x220 /* Left-alt */ +#define KEY_ALT_R 0x221 /* Right-alt */ +#define KEY_RESIZE 0x222 /* Window resize */ +#define KEY_SUP 0x223 /* Shifted up arrow */ +#define KEY_SDOWN 0x224 /* Shifted down arrow */ + +#define KEY_MIN KEY_BREAK /* Minimum curses key value */ +#define KEY_MAX KEY_SDOWN /* Maximum curses key */ + +#define KEY_F(n) (KEY_F0 + (n)) + +/*---------------------------------------------------------------------- + * + * PDCurses Function Declarations + * + */ + +/* Standard */ + +int addch(const chtype); +int addchnstr(const chtype *, int); +int addchstr(const chtype *); +int addnstr(const char *, int); +int addstr(const char *); +int attroff(chtype); +int attron(chtype); +int attrset(chtype); +int attr_get(attr_t *, short *, void *); +int attr_off(attr_t, void *); +int attr_on(attr_t, void *); +int attr_set(attr_t, short, void *); +int baudrate(void); +int beep(void); +int bkgd(chtype); +void bkgdset(chtype); +int border(chtype, chtype, chtype, chtype, chtype, chtype, chtype, chtype); +int box(WINDOW *, chtype, chtype); +bool can_change_color(void); +int cbreak(void); +int chgat(int, attr_t, short, const void *); +int clearok(WINDOW *, bool); +int clear(void); +int clrtobot(void); +int clrtoeol(void); +int color_content(short, short *, short *, short *); +int color_set(short, void *); +int copywin(const WINDOW *, WINDOW *, int, int, int, int, int, int, int); +int curs_set(int); +int def_prog_mode(void); +int def_shell_mode(void); +int delay_output(int); +int delch(void); +int deleteln(void); +void delscreen(SCREEN *); +int delwin(WINDOW *); +WINDOW *derwin(WINDOW *, int, int, int, int); +int doupdate(void); +WINDOW *dupwin(WINDOW *); +int echochar(const chtype); +int echo(void); +int endwin(void); +char erasechar(void); +int erase(void); +void filter(void); +int flash(void); +int flushinp(void); +chtype getbkgd(WINDOW *); +int getnstr(char *, int); +int getstr(char *); +WINDOW *getwin(FILE *); +int halfdelay(int); +bool has_colors(void); +bool has_ic(void); +bool has_il(void); +int hline(chtype, int); +void idcok(WINDOW *, bool); +int idlok(WINDOW *, bool); +void immedok(WINDOW *, bool); +int inchnstr(chtype *, int); +int inchstr(chtype *); +chtype inch(void); +int init_color(short, short, short, short); +int init_pair(short, short, short); +WINDOW *initscr(void); +int innstr(char *, int); +int insch(chtype); +int insdelln(int); +int insertln(void); +int insnstr(const char *, int); +int insstr(const char *); +int instr(char *); +int intrflush(WINDOW *, bool); +bool isendwin(void); +bool is_linetouched(WINDOW *, int); +bool is_wintouched(WINDOW *); +char *keyname(int); +int keypad(WINDOW *, bool); +char killchar(void); +int leaveok(WINDOW *, bool); +char *longname(void); +int meta(WINDOW *, bool); +int move(int, int); +int mvaddch(int, int, const chtype); +int mvaddchnstr(int, int, const chtype *, int); +int mvaddchstr(int, int, const chtype *); +int mvaddnstr(int, int, const char *, int); +int mvaddstr(int, int, const char *); +int mvchgat(int, int, int, attr_t, short, const void *); +int mvcur(int, int, int, int); +int mvdelch(int, int); +int mvderwin(WINDOW *, int, int); +int mvgetch(int, int); +int mvgetnstr(int, int, char *, int); +int mvgetstr(int, int, char *); +int mvhline(int, int, chtype, int); +chtype mvinch(int, int); +int mvinchnstr(int, int, chtype *, int); +int mvinchstr(int, int, chtype *); +int mvinnstr(int, int, char *, int); +int mvinsch(int, int, chtype); +int mvinsnstr(int, int, const char *, int); +int mvinsstr(int, int, const char *); +int mvinstr(int, int, char *); +int mvprintw(int, int, const char *, ...); +int mvscanw(int, int, const char *, ...); +int mvvline(int, int, chtype, int); +int mvwaddchnstr(WINDOW *, int, int, const chtype *, int); +int mvwaddchstr(WINDOW *, int, int, const chtype *); +int mvwaddch(WINDOW *, int, int, const chtype); +int mvwaddnstr(WINDOW *, int, int, const char *, int); +int mvwaddstr(WINDOW *, int, int, const char *); +int mvwchgat(WINDOW *, int, int, int, attr_t, short, const void *); +int mvwdelch(WINDOW *, int, int); +int mvwgetch(WINDOW *, int, int); +int mvwgetnstr(WINDOW *, int, int, char *, int); +int mvwgetstr(WINDOW *, int, int, char *); +int mvwhline(WINDOW *, int, int, chtype, int); +int mvwinchnstr(WINDOW *, int, int, chtype *, int); +int mvwinchstr(WINDOW *, int, int, chtype *); +chtype mvwinch(WINDOW *, int, int); +int mvwinnstr(WINDOW *, int, int, char *, int); +int mvwinsch(WINDOW *, int, int, chtype); +int mvwinsnstr(WINDOW *, int, int, const char *, int); +int mvwinsstr(WINDOW *, int, int, const char *); +int mvwinstr(WINDOW *, int, int, char *); +int mvwin(WINDOW *, int, int); +int mvwprintw(WINDOW *, int, int, const char *, ...); +int mvwscanw(WINDOW *, int, int, const char *, ...); +int mvwvline(WINDOW *, int, int, chtype, int); +int napms(int); +WINDOW *newpad(int, int); +SCREEN *newterm(const char *, FILE *, FILE *); +WINDOW *newwin(int, int, int, int); +int nl(void); +int nocbreak(void); +int nodelay(WINDOW *, bool); +int noecho(void); +int nonl(void); +void noqiflush(void); +int noraw(void); +int notimeout(WINDOW *, bool); +int overlay(const WINDOW *, WINDOW *); +int overwrite(const WINDOW *, WINDOW *); +int pair_content(short, short *, short *); +int pechochar(WINDOW *, chtype); +int pnoutrefresh(WINDOW *, int, int, int, int, int, int); +int prefresh(WINDOW *, int, int, int, int, int, int); +int printw(const char *, ...); +int putwin(WINDOW *, FILE *); +void qiflush(void); +int raw(void); +int redrawwin(WINDOW *); +int refresh(void); +int reset_prog_mode(void); +int reset_shell_mode(void); +int resetty(void); +int ripoffline(int, int (*)(WINDOW *, int)); +int savetty(void); +int scanw(const char *, ...); +int scr_dump(const char *); +int scr_init(const char *); +int scr_restore(const char *); +int scr_set(const char *); +int scrl(int); +int scroll(WINDOW *); +int scrollok(WINDOW *, bool); +SCREEN *set_term(SCREEN *); +int setscrreg(int, int); +int slk_attroff(const chtype); +int slk_attr_off(const attr_t, void *); +int slk_attron(const chtype); +int slk_attr_on(const attr_t, void *); +int slk_attrset(const chtype); +int slk_attr_set(const attr_t, short, void *); +int slk_clear(void); +int slk_color(short); +int slk_init(int); +char *slk_label(int); +int slk_noutrefresh(void); +int slk_refresh(void); +int slk_restore(void); +int slk_set(int, const char *, int); +int slk_touch(void); +int standend(void); +int standout(void); +int start_color(void); +WINDOW *subpad(WINDOW *, int, int, int, int); +WINDOW *subwin(WINDOW *, int, int, int, int); +int syncok(WINDOW *, bool); +chtype termattrs(void); +attr_t term_attrs(void); +char *termname(void); +void timeout(int); +int touchline(WINDOW *, int, int); +int touchwin(WINDOW *); +int typeahead(int); +int untouchwin(WINDOW *); +void use_env(bool); +int vidattr(chtype); +int vid_attr(attr_t, short, void *); +int vidputs(chtype, int (*)(int)); +int vid_puts(attr_t, short, void *, int (*)(int)); +int vline(chtype, int); +int vw_printw(WINDOW *, const char *, va_list); +int vwprintw(WINDOW *, const char *, va_list); +int vw_scanw(WINDOW *, const char *, va_list); +int vwscanw(WINDOW *, const char *, va_list); +int waddchnstr(WINDOW *, const chtype *, int); +int waddchstr(WINDOW *, const chtype *); +int waddch(WINDOW *, const chtype); +int waddnstr(WINDOW *, const char *, int); +int waddstr(WINDOW *, const char *); +int wattroff(WINDOW *, chtype); +int wattron(WINDOW *, chtype); +int wattrset(WINDOW *, chtype); +int wattr_get(WINDOW *, attr_t *, short *, void *); +int wattr_off(WINDOW *, attr_t, void *); +int wattr_on(WINDOW *, attr_t, void *); +int wattr_set(WINDOW *, attr_t, short, void *); +void wbkgdset(WINDOW *, chtype); +int wbkgd(WINDOW *, chtype); +int wborder(WINDOW *, chtype, chtype, chtype, chtype, + chtype, chtype, chtype, chtype); +int wchgat(WINDOW *, int, attr_t, short, const void *); +int wclear(WINDOW *); +int wclrtobot(WINDOW *); +int wclrtoeol(WINDOW *); +int wcolor_set(WINDOW *, short, void *); +void wcursyncup(WINDOW *); +int wdelch(WINDOW *); +int wdeleteln(WINDOW *); +int wechochar(WINDOW *, const chtype); +int werase(WINDOW *); +int wgetch(WINDOW *); +int wgetnstr(WINDOW *, char *, int); +int wgetstr(WINDOW *, char *); +int whline(WINDOW *, chtype, int); +int winchnstr(WINDOW *, chtype *, int); +int winchstr(WINDOW *, chtype *); +chtype winch(WINDOW *); +int winnstr(WINDOW *, char *, int); +int winsch(WINDOW *, chtype); +int winsdelln(WINDOW *, int); +int winsertln(WINDOW *); +int winsnstr(WINDOW *, const char *, int); +int winsstr(WINDOW *, const char *); +int winstr(WINDOW *, char *); +int wmove(WINDOW *, int, int); +int wnoutrefresh(WINDOW *); +int wprintw(WINDOW *, const char *, ...); +int wredrawln(WINDOW *, int, int); +int wrefresh(WINDOW *); +int wscanw(WINDOW *, const char *, ...); +int wscrl(WINDOW *, int); +int wsetscrreg(WINDOW *, int, int); +int wstandend(WINDOW *); +int wstandout(WINDOW *); +void wsyncdown(WINDOW *); +void wsyncup(WINDOW *); +void wtimeout(WINDOW *, int); +int wtouchln(WINDOW *, int, int, int); +int wvline(WINDOW *, chtype, int); + +/* Wide-character functions */ + +#ifdef PDC_WIDE +int addnwstr(const wchar_t *, int); +int addwstr(const wchar_t *); +int add_wch(const cchar_t *); +int add_wchnstr(const cchar_t *, int); +int add_wchstr(const cchar_t *); +int border_set(const cchar_t *, const cchar_t *, const cchar_t *, + const cchar_t *, const cchar_t *, const cchar_t *, + const cchar_t *, const cchar_t *); +int box_set(WINDOW *, const cchar_t *, const cchar_t *); +int echo_wchar(const cchar_t *); +int erasewchar(wchar_t *); +int getbkgrnd(cchar_t *); +int getcchar(const cchar_t *, wchar_t *, attr_t *, short *, void *); +int getn_wstr(wint_t *, int); +int get_wch(wint_t *); +int get_wstr(wint_t *); +int hline_set(const cchar_t *, int); +int innwstr(wchar_t *, int); +int ins_nwstr(const wchar_t *, int); +int ins_wch(const cchar_t *); +int ins_wstr(const wchar_t *); +int inwstr(wchar_t *); +int in_wch(cchar_t *); +int in_wchnstr(cchar_t *, int); +int in_wchstr(cchar_t *); +char *key_name(wchar_t); +int killwchar(wchar_t *); +int mvaddnwstr(int, int, const wchar_t *, int); +int mvaddwstr(int, int, const wchar_t *); +int mvadd_wch(int, int, const cchar_t *); +int mvadd_wchnstr(int, int, const cchar_t *, int); +int mvadd_wchstr(int, int, const cchar_t *); +int mvgetn_wstr(int, int, wint_t *, int); +int mvget_wch(int, int, wint_t *); +int mvget_wstr(int, int, wint_t *); +int mvhline_set(int, int, const cchar_t *, int); +int mvinnwstr(int, int, wchar_t *, int); +int mvins_nwstr(int, int, const wchar_t *, int); +int mvins_wch(int, int, const cchar_t *); +int mvins_wstr(int, int, const wchar_t *); +int mvinwstr(int, int, wchar_t *); +int mvin_wch(int, int, cchar_t *); +int mvin_wchnstr(int, int, cchar_t *, int); +int mvin_wchstr(int, int, cchar_t *); +int mvvline_set(int, int, const cchar_t *, int); +int mvwaddnwstr(WINDOW *, int, int, const wchar_t *, int); +int mvwaddwstr(WINDOW *, int, int, const wchar_t *); +int mvwadd_wch(WINDOW *, int, int, const cchar_t *); +int mvwadd_wchnstr(WINDOW *, int, int, const cchar_t *, int); +int mvwadd_wchstr(WINDOW *, int, int, const cchar_t *); +int mvwgetn_wstr(WINDOW *, int, int, wint_t *, int); +int mvwget_wch(WINDOW *, int, int, wint_t *); +int mvwget_wstr(WINDOW *, int, int, wint_t *); +int mvwhline_set(WINDOW *, int, int, const cchar_t *, int); +int mvwinnwstr(WINDOW *, int, int, wchar_t *, int); +int mvwins_nwstr(WINDOW *, int, int, const wchar_t *, int); +int mvwins_wch(WINDOW *, int, int, const cchar_t *); +int mvwins_wstr(WINDOW *, int, int, const wchar_t *); +int mvwin_wch(WINDOW *, int, int, cchar_t *); +int mvwin_wchnstr(WINDOW *, int, int, cchar_t *, int); +int mvwin_wchstr(WINDOW *, int, int, cchar_t *); +int mvwinwstr(WINDOW *, int, int, wchar_t *); +int mvwvline_set(WINDOW *, int, int, const cchar_t *, int); +int pecho_wchar(WINDOW *, const cchar_t*); +int setcchar(cchar_t*, const wchar_t*, const attr_t, short, const void*); +int slk_wset(int, const wchar_t *, int); +int unget_wch(const wchar_t); +int vline_set(const cchar_t *, int); +int waddnwstr(WINDOW *, const wchar_t *, int); +int waddwstr(WINDOW *, const wchar_t *); +int wadd_wch(WINDOW *, const cchar_t *); +int wadd_wchnstr(WINDOW *, const cchar_t *, int); +int wadd_wchstr(WINDOW *, const cchar_t *); +int wbkgrnd(WINDOW *, const cchar_t *); +void wbkgrndset(WINDOW *, const cchar_t *); +int wborder_set(WINDOW *, const cchar_t *, const cchar_t *, + const cchar_t *, const cchar_t *, const cchar_t *, + const cchar_t *, const cchar_t *, const cchar_t *); +int wecho_wchar(WINDOW *, const cchar_t *); +int wgetbkgrnd(WINDOW *, cchar_t *); +int wgetn_wstr(WINDOW *, wint_t *, int); +int wget_wch(WINDOW *, wint_t *); +int wget_wstr(WINDOW *, wint_t *); +int whline_set(WINDOW *, const cchar_t *, int); +int winnwstr(WINDOW *, wchar_t *, int); +int wins_nwstr(WINDOW *, const wchar_t *, int); +int wins_wch(WINDOW *, const cchar_t *); +int wins_wstr(WINDOW *, const wchar_t *); +int winwstr(WINDOW *, wchar_t *); +int win_wch(WINDOW *, cchar_t *); +int win_wchnstr(WINDOW *, cchar_t *, int); +int win_wchstr(WINDOW *, cchar_t *); +wchar_t *wunctrl(cchar_t *); +int wvline_set(WINDOW *, const cchar_t *, int); +#endif + +/* Quasi-standard */ + +chtype getattrs(WINDOW *); +int getbegx(WINDOW *); +int getbegy(WINDOW *); +int getmaxx(WINDOW *); +int getmaxy(WINDOW *); +int getparx(WINDOW *); +int getpary(WINDOW *); +int getcurx(WINDOW *); +int getcury(WINDOW *); +void traceoff(void); +void traceon(void); +char *unctrl(chtype); + +int crmode(void); +int nocrmode(void); +int draino(int); +int resetterm(void); +int fixterm(void); +int saveterm(void); +int setsyx(int, int); + +int mouse_set(unsigned long); +int mouse_on(unsigned long); +int mouse_off(unsigned long); +int request_mouse_pos(void); +int map_button(unsigned long); +void wmouse_position(WINDOW *, int *, int *); +unsigned long getmouse(void); +unsigned long getbmap(void); + +/* ncurses */ + +int assume_default_colors(int, int); +const char *curses_version(void); +bool has_key(int); +int use_default_colors(void); +int wresize(WINDOW *, int, int); + +int mouseinterval(int); +mmask_t mousemask(mmask_t, mmask_t *); +bool mouse_trafo(int *, int *, bool); +int nc_getmouse(MEVENT *); +int ungetmouse(MEVENT *); +bool wenclose(const WINDOW *, int, int); +bool wmouse_trafo(const WINDOW *, int *, int *, bool); + +/* PDCurses */ + +int addrawch(chtype); +int insrawch(chtype); +bool is_termresized(void); +int mvaddrawch(int, int, chtype); +int mvdeleteln(int, int); +int mvinsertln(int, int); +int mvinsrawch(int, int, chtype); +int mvwaddrawch(WINDOW *, int, int, chtype); +int mvwdeleteln(WINDOW *, int, int); +int mvwinsertln(WINDOW *, int, int); +int mvwinsrawch(WINDOW *, int, int, chtype); +int raw_output(bool); +int resize_term(int, int); +WINDOW *resize_window(WINDOW *, int, int); +int waddrawch(WINDOW *, chtype); +int winsrawch(WINDOW *, chtype); +char wordchar(void); + +#ifdef PDC_WIDE +wchar_t *slk_wlabel(int); +#endif + +void PDC_debug(const char *, ...); +int PDC_ungetch(int); +int PDC_set_blink(bool); +int PDC_set_line_color(short); +void PDC_set_title(const char *); + +int PDC_clearclipboard(void); +int PDC_freeclipboard(char *); +int PDC_getclipboard(char **, long *); +int PDC_setclipboard(const char *, long); + +unsigned long PDC_get_input_fd(void); +unsigned long PDC_get_key_modifiers(void); +int PDC_return_key_modifiers(bool); +int PDC_save_key_modifiers(bool); + +#ifdef XCURSES +WINDOW *Xinitscr(int, char **); +void XCursesExit(void); +int sb_init(void); +int sb_set_horz(int, int, int); +int sb_set_vert(int, int, int); +int sb_get_horz(int *, int *, int *); +int sb_get_vert(int *, int *, int *); +int sb_refresh(void); +#endif + +/*** Functions defined as macros ***/ + +/* getch() and ungetch() conflict with some DOS libraries */ + +#define getch() wgetch(stdscr) +#define ungetch(ch) PDC_ungetch(ch) + +#define COLOR_PAIR(n) (((chtype)(n) << PDC_COLOR_SHIFT) & A_COLOR) +#define PAIR_NUMBER(n) (((n) & A_COLOR) >> PDC_COLOR_SHIFT) + +/* These will _only_ work as macros */ + +#define getbegyx(w, y, x) (y = getbegy(w), x = getbegx(w)) +#define getmaxyx(w, y, x) (y = getmaxy(w), x = getmaxx(w)) +#define getparyx(w, y, x) (y = getpary(w), x = getparx(w)) +#define getyx(w, y, x) (y = getcury(w), x = getcurx(w)) + +#define getsyx(y, x) { if (curscr->_leaveit) (y)=(x)=-1; \ + else getyx(curscr,(y),(x)); } + +#ifdef NCURSES_MOUSE_VERSION +# define getmouse(x) nc_getmouse(x) +#endif + +/* return codes from PDC_getclipboard() and PDC_setclipboard() calls */ + +#define PDC_CLIP_SUCCESS 0 +#define PDC_CLIP_ACCESS_ERROR 1 +#define PDC_CLIP_EMPTY 2 +#define PDC_CLIP_MEMORY_ERROR 3 + +/* PDCurses key modifier masks */ + +#define PDC_KEY_MODIFIER_SHIFT 1 +#define PDC_KEY_MODIFIER_CONTROL 2 +#define PDC_KEY_MODIFIER_ALT 4 +#define PDC_KEY_MODIFIER_NUMLOCK 8 + +#if defined(__cplusplus) || defined(__cplusplus__) || defined(__CPLUSPLUS) +# undef bool +} +#endif + +#endif /* __PDCURSES__ */ diff --git a/samtools/win32/zconf.h b/samtools/win32/zconf.h new file mode 100644 index 0000000..03a9431 --- /dev/null +++ b/samtools/win32/zconf.h @@ -0,0 +1,332 @@ +/* zconf.h -- configuration of the zlib compression library + * Copyright (C) 1995-2005 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#ifndef ZCONF_H +#define ZCONF_H + +/* + * If you *really* need a unique prefix for all types and library functions, + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. + */ +#ifdef Z_PREFIX +# define deflateInit_ z_deflateInit_ +# define deflate z_deflate +# define deflateEnd z_deflateEnd +# define inflateInit_ z_inflateInit_ +# define inflate z_inflate +# define inflateEnd z_inflateEnd +# define deflateInit2_ z_deflateInit2_ +# define deflateSetDictionary z_deflateSetDictionary +# define deflateCopy z_deflateCopy +# define deflateReset z_deflateReset +# define deflateParams z_deflateParams +# define deflateBound z_deflateBound +# define deflatePrime z_deflatePrime +# define inflateInit2_ z_inflateInit2_ +# define inflateSetDictionary z_inflateSetDictionary +# define inflateSync z_inflateSync +# define inflateSyncPoint z_inflateSyncPoint +# define inflateCopy z_inflateCopy +# define inflateReset z_inflateReset +# define inflateBack z_inflateBack +# define inflateBackEnd z_inflateBackEnd +# define compress z_compress +# define compress2 z_compress2 +# define compressBound z_compressBound +# define uncompress z_uncompress +# define adler32 z_adler32 +# define crc32 z_crc32 +# define get_crc_table z_get_crc_table +# define zError z_zError + +# define alloc_func z_alloc_func +# define free_func z_free_func +# define in_func z_in_func +# define out_func z_out_func +# define Byte z_Byte +# define uInt z_uInt +# define uLong z_uLong +# define Bytef z_Bytef +# define charf z_charf +# define intf z_intf +# define uIntf z_uIntf +# define uLongf z_uLongf +# define voidpf z_voidpf +# define voidp z_voidp +#endif + +#if defined(__MSDOS__) && !defined(MSDOS) +# define MSDOS +#endif +#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) +# define OS2 +#endif +#if defined(_WINDOWS) && !defined(WINDOWS) +# define WINDOWS +#endif +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) +# ifndef WIN32 +# define WIN32 +# endif +#endif +#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) +# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) +# ifndef SYS16BIT +# define SYS16BIT +# endif +# endif +#endif + +/* + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more + * than 64k bytes at a time (needed on systems with 16-bit int). + */ +#ifdef SYS16BIT +# define MAXSEG_64K +#endif +#ifdef MSDOS +# define UNALIGNED_OK +#endif + +#ifdef __STDC_VERSION__ +# ifndef STDC +# define STDC +# endif +# if __STDC_VERSION__ >= 199901L +# ifndef STDC99 +# define STDC99 +# endif +# endif +#endif +#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) +# define STDC +#endif +#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) +# define STDC +#endif +#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) +# define STDC +#endif +#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) +# define STDC +#endif + +#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ +# define STDC +#endif + +#ifndef STDC +# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ +# define const /* note: need a more gentle solution here */ +# endif +#endif + +/* Some Mac compilers merge all .h files incorrectly: */ +#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) +# define NO_DUMMY_DECL +#endif + +/* Maximum value for memLevel in deflateInit2 */ +#ifndef MAX_MEM_LEVEL +# ifdef MAXSEG_64K +# define MAX_MEM_LEVEL 8 +# else +# define MAX_MEM_LEVEL 9 +# endif +#endif + +/* Maximum value for windowBits in deflateInit2 and inflateInit2. + * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files + * created by gzip. (Files created by minigzip can still be extracted by + * gzip.) + */ +#ifndef MAX_WBITS +# define MAX_WBITS 15 /* 32K LZ77 window */ +#endif + +/* The memory requirements for deflate are (in bytes): + (1 << (windowBits+2)) + (1 << (memLevel+9)) + that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) + plus a few kilobytes for small objects. For example, if you want to reduce + the default memory requirements from 256K to 128K, compile with + make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" + Of course this will generally degrade compression (there's no free lunch). + + The memory requirements for inflate are (in bytes) 1 << windowBits + that is, 32K for windowBits=15 (default value) plus a few kilobytes + for small objects. +*/ + + /* Type declarations */ + +#ifndef OF /* function prototypes */ +# ifdef STDC +# define OF(args) args +# else +# define OF(args) () +# endif +#endif + +/* The following definitions for FAR are needed only for MSDOS mixed + * model programming (small or medium model with some far allocations). + * This was tested only with MSC; for other MSDOS compilers you may have + * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, + * just define FAR to be empty. + */ +#ifdef SYS16BIT +# if defined(M_I86SM) || defined(M_I86MM) + /* MSC small or medium model */ +# define SMALL_MEDIUM +# ifdef _MSC_VER +# define FAR _far +# else +# define FAR far +# endif +# endif +# if (defined(__SMALL__) || defined(__MEDIUM__)) + /* Turbo C small or medium model */ +# define SMALL_MEDIUM +# ifdef __BORLANDC__ +# define FAR _far +# else +# define FAR far +# endif +# endif +#endif + +#if defined(WINDOWS) || defined(WIN32) + /* If building or using zlib as a DLL, define ZLIB_DLL. + * This is not mandatory, but it offers a little performance increase. + */ +# ifdef ZLIB_DLL +# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) +# ifdef ZLIB_INTERNAL +# define ZEXTERN extern __declspec(dllexport) +# else +# define ZEXTERN extern __declspec(dllimport) +# endif +# endif +# endif /* ZLIB_DLL */ + /* If building or using zlib with the WINAPI/WINAPIV calling convention, + * define ZLIB_WINAPI. + * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. + */ +# ifdef ZLIB_WINAPI +# ifdef FAR +# undef FAR +# endif +# include + /* No need for _export, use ZLIB.DEF instead. */ + /* For complete Windows compatibility, use WINAPI, not __stdcall. */ +# define ZEXPORT WINAPI +# ifdef WIN32 +# define ZEXPORTVA WINAPIV +# else +# define ZEXPORTVA FAR CDECL +# endif +# endif +#endif + +#if defined (__BEOS__) +# ifdef ZLIB_DLL +# ifdef ZLIB_INTERNAL +# define ZEXPORT __declspec(dllexport) +# define ZEXPORTVA __declspec(dllexport) +# else +# define ZEXPORT __declspec(dllimport) +# define ZEXPORTVA __declspec(dllimport) +# endif +# endif +#endif + +#ifndef ZEXTERN +# define ZEXTERN extern +#endif +#ifndef ZEXPORT +# define ZEXPORT +#endif +#ifndef ZEXPORTVA +# define ZEXPORTVA +#endif + +#ifndef FAR +# define FAR +#endif + +#if !defined(__MACTYPES__) +typedef unsigned char Byte; /* 8 bits */ +#endif +typedef unsigned int uInt; /* 16 bits or more */ +typedef unsigned long uLong; /* 32 bits or more */ + +#ifdef SMALL_MEDIUM + /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ +# define Bytef Byte FAR +#else + typedef Byte FAR Bytef; +#endif +typedef char FAR charf; +typedef int FAR intf; +typedef uInt FAR uIntf; +typedef uLong FAR uLongf; + +#ifdef STDC + typedef void const *voidpc; + typedef void FAR *voidpf; + typedef void *voidp; +#else + typedef Byte const *voidpc; + typedef Byte FAR *voidpf; + typedef Byte *voidp; +#endif + +#if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */ +# include /* for off_t */ +# include /* for SEEK_* and off_t */ +# ifdef VMS +# include /* for off_t */ +# endif +# define z_off_t off_t +#endif +#ifndef SEEK_SET +# define SEEK_SET 0 /* Seek from beginning of file. */ +# define SEEK_CUR 1 /* Seek from current position. */ +# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ +#endif +#ifndef z_off_t +# define z_off_t long +#endif + +#if defined(__OS400__) +# define NO_vsnprintf +#endif + +#if defined(__MVS__) +# define NO_vsnprintf +# ifdef FAR +# undef FAR +# endif +#endif + +/* MVS linker does not support external names larger than 8 bytes */ +#if defined(__MVS__) +# pragma map(deflateInit_,"DEIN") +# pragma map(deflateInit2_,"DEIN2") +# pragma map(deflateEnd,"DEEND") +# pragma map(deflateBound,"DEBND") +# pragma map(inflateInit_,"ININ") +# pragma map(inflateInit2_,"ININ2") +# pragma map(inflateEnd,"INEND") +# pragma map(inflateSync,"INSY") +# pragma map(inflateSetDictionary,"INSEDI") +# pragma map(compressBound,"CMBND") +# pragma map(inflate_table,"INTABL") +# pragma map(inflate_fast,"INFA") +# pragma map(inflate_copyright,"INCOPY") +#endif + +#endif /* ZCONF_H */ diff --git a/samtools/win32/zlib.h b/samtools/win32/zlib.h new file mode 100644 index 0000000..0228179 --- /dev/null +++ b/samtools/win32/zlib.h @@ -0,0 +1,1357 @@ +/* zlib.h -- interface of the 'zlib' general purpose compression library + version 1.2.3, July 18th, 2005 + + Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler + + 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. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + + + The data format used by the zlib library is described by RFCs (Request for + Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt + (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). +*/ + +#ifndef ZLIB_H +#define ZLIB_H + +#include "zconf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZLIB_VERSION "1.2.3" +#define ZLIB_VERNUM 0x1230 + +/* + The 'zlib' compression library provides in-memory compression and + decompression functions, including integrity checks of the uncompressed + data. This version of the library supports only one compression method + (deflation) but other algorithms will be added later and will have the same + stream interface. + + Compression can be done in a single step if the buffers are large + enough (for example if an input file is mmap'ed), or can be done by + repeated calls of the compression function. In the latter case, the + application must provide more input and/or consume the output + (providing more output space) before each call. + + The compressed data format used by default by the in-memory functions is + the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped + around a deflate stream, which is itself documented in RFC 1951. + + The library also supports reading and writing files in gzip (.gz) format + with an interface similar to that of stdio using the functions that start + with "gz". The gzip format is different from the zlib format. gzip is a + gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. + + This library can optionally read and write gzip streams in memory as well. + + The zlib format was designed to be compact and fast for use in memory + and on communications channels. The gzip format was designed for single- + file compression on file systems, has a larger header than zlib to maintain + directory information, and uses a different, slower check method than zlib. + + The library does not install any signal handler. The decoder checks + the consistency of the compressed data, so the library should never + crash even in case of corrupted input. +*/ + +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); +typedef void (*free_func) OF((voidpf opaque, voidpf address)); + +struct internal_state; + +typedef struct z_stream_s { + Bytef *next_in; /* next input byte */ + uInt avail_in; /* number of bytes available at next_in */ + uLong total_in; /* total nb of input bytes read so far */ + + Bytef *next_out; /* next output byte should be put there */ + uInt avail_out; /* remaining free space at next_out */ + uLong total_out; /* total nb of bytes output so far */ + + char *msg; /* last error message, NULL if no error */ + struct internal_state FAR *state; /* not visible by applications */ + + alloc_func zalloc; /* used to allocate the internal state */ + free_func zfree; /* used to free the internal state */ + voidpf opaque; /* private data object passed to zalloc and zfree */ + + int data_type; /* best guess about the data type: binary or text */ + uLong adler; /* adler32 value of the uncompressed data */ + uLong reserved; /* reserved for future use */ +} z_stream; + +typedef z_stream FAR *z_streamp; + +/* + gzip header information passed to and from zlib routines. See RFC 1952 + for more details on the meanings of these fields. +*/ +typedef struct gz_header_s { + int text; /* true if compressed data believed to be text */ + uLong time; /* modification time */ + int xflags; /* extra flags (not used when writing a gzip file) */ + int os; /* operating system */ + Bytef *extra; /* pointer to extra field or Z_NULL if none */ + uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ + uInt extra_max; /* space at extra (only when reading header) */ + Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ + uInt name_max; /* space at name (only when reading header) */ + Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ + uInt comm_max; /* space at comment (only when reading header) */ + int hcrc; /* true if there was or will be a header crc */ + int done; /* true when done reading gzip header (not used + when writing a gzip file) */ +} gz_header; + +typedef gz_header FAR *gz_headerp; + +/* + The application must update next_in and avail_in when avail_in has + dropped to zero. It must update next_out and avail_out when avail_out + has dropped to zero. The application must initialize zalloc, zfree and + opaque before calling the init function. All other fields are set by the + compression library and must not be updated by the application. + + The opaque value provided by the application will be passed as the first + parameter for calls of zalloc and zfree. This can be useful for custom + memory management. The compression library attaches no meaning to the + opaque value. + + zalloc must return Z_NULL if there is not enough memory for the object. + If zlib is used in a multi-threaded application, zalloc and zfree must be + thread safe. + + On 16-bit systems, the functions zalloc and zfree must be able to allocate + exactly 65536 bytes, but will not be required to allocate more than this + if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, + pointers returned by zalloc for objects of exactly 65536 bytes *must* + have their offset normalized to zero. The default allocation function + provided by this library ensures this (see zutil.c). To reduce memory + requirements and avoid any allocation of 64K objects, at the expense of + compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). + + The fields total_in and total_out can be used for statistics or + progress reports. After compression, total_in holds the total size of + the uncompressed data and may be saved for use in the decompressor + (particularly if the decompressor wants to decompress everything in + a single step). +*/ + + /* constants */ + +#define Z_NO_FLUSH 0 +#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */ +#define Z_SYNC_FLUSH 2 +#define Z_FULL_FLUSH 3 +#define Z_FINISH 4 +#define Z_BLOCK 5 +/* Allowed flush values; see deflate() and inflate() below for details */ + +#define Z_OK 0 +#define Z_STREAM_END 1 +#define Z_NEED_DICT 2 +#define Z_ERRNO (-1) +#define Z_STREAM_ERROR (-2) +#define Z_DATA_ERROR (-3) +#define Z_MEM_ERROR (-4) +#define Z_BUF_ERROR (-5) +#define Z_VERSION_ERROR (-6) +/* Return codes for the compression/decompression functions. Negative + * values are errors, positive values are used for special but normal events. + */ + +#define Z_NO_COMPRESSION 0 +#define Z_BEST_SPEED 1 +#define Z_BEST_COMPRESSION 9 +#define Z_DEFAULT_COMPRESSION (-1) +/* compression levels */ + +#define Z_FILTERED 1 +#define Z_HUFFMAN_ONLY 2 +#define Z_RLE 3 +#define Z_FIXED 4 +#define Z_DEFAULT_STRATEGY 0 +/* compression strategy; see deflateInit2() below for details */ + +#define Z_BINARY 0 +#define Z_TEXT 1 +#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ +#define Z_UNKNOWN 2 +/* Possible values of the data_type field (though see inflate()) */ + +#define Z_DEFLATED 8 +/* The deflate compression method (the only one supported in this version) */ + +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ + +#define zlib_version zlibVersion() +/* for compatibility with versions < 1.0.2 */ + + /* basic functions */ + +ZEXTERN const char * ZEXPORT zlibVersion OF((void)); +/* The application can compare zlibVersion and ZLIB_VERSION for consistency. + If the first character differs, the library code actually used is + not compatible with the zlib.h header file used by the application. + This check is automatically made by deflateInit and inflateInit. + */ + +/* +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); + + Initializes the internal stream state for compression. The fields + zalloc, zfree and opaque must be initialized before by the caller. + If zalloc and zfree are set to Z_NULL, deflateInit updates them to + use default allocation functions. + + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: + 1 gives best speed, 9 gives best compression, 0 gives no compression at + all (the input data is simply copied a block at a time). + Z_DEFAULT_COMPRESSION requests a default compromise between speed and + compression (currently equivalent to level 6). + + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if level is not a valid compression level, + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible + with the version assumed by the caller (ZLIB_VERSION). + msg is set to null if there is no error message. deflateInit does not + perform any compression: this will be done by deflate(). +*/ + + +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); +/* + deflate compresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce some + output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. deflate performs one or both of the + following actions: + + - Compress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in and avail_in are updated and + processing will resume at this point for the next call of deflate(). + + - Provide more output starting at next_out and update next_out and avail_out + accordingly. This action is forced if the parameter flush is non zero. + Forcing flush frequently degrades the compression ratio, so this parameter + should be set only when necessary (in interactive applications). + Some output may be provided even if flush is not set. + + Before the call of deflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming + more output, and updating avail_in or avail_out accordingly; avail_out + should never be zero before the call. The application can consume the + compressed output when it wants, for example when the output buffer is full + (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK + and with zero avail_out, it must be called again after making room in the + output buffer because there might be more output pending. + + Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to + decide how much data to accumualte before producing output, in order to + maximize compression. + + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is + flushed to the output buffer and the output is aligned on a byte boundary, so + that the decompressor can get all input data available so far. (In particular + avail_in is zero after the call if enough output space has been provided + before the call.) Flushing may degrade compression for some compression + algorithms and so it should be used only when necessary. + + If flush is set to Z_FULL_FLUSH, all output is flushed as with + Z_SYNC_FLUSH, and the compression state is reset so that decompression can + restart from this point if previous compressed data has been damaged or if + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade + compression. + + If deflate returns with avail_out == 0, this function must be called again + with the same value of the flush parameter and more output space (updated + avail_out), until the flush is complete (deflate returns with non-zero + avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that + avail_out is greater than six to avoid repeated flush markers due to + avail_out == 0 on return. + + If the parameter flush is set to Z_FINISH, pending input is processed, + pending output is flushed and deflate returns with Z_STREAM_END if there + was enough output space; if deflate returns with Z_OK, this function must be + called again with Z_FINISH and more output space (updated avail_out) but no + more input data, until it returns with Z_STREAM_END or an error. After + deflate has returned Z_STREAM_END, the only possible operations on the + stream are deflateReset or deflateEnd. + + Z_FINISH can be used immediately after deflateInit if all the compression + is to be done in a single step. In this case, avail_out must be at least + the value returned by deflateBound (see below). If deflate does not return + Z_STREAM_END, then it must be called again as described above. + + deflate() sets strm->adler to the adler32 checksum of all input read + so far (that is, total_in bytes). + + deflate() may update strm->data_type if it can make a good guess about + the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered + binary. This field is only for information purposes and does not affect + the compression algorithm in any manner. + + deflate() returns Z_OK if some progress has been made (more input + processed or more output produced), Z_STREAM_END if all input has been + consumed and all output has been produced (only when flush is set to + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example + if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible + (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not + fatal, and deflate() can be called again with more input and more output + space to continue compressing. +*/ + + +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any + pending output. + + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the + stream state was inconsistent, Z_DATA_ERROR if the stream was freed + prematurely (some input or output was discarded). In the error case, + msg may be set but then points to a static string (which must not be + deallocated). +*/ + + +/* +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); + + Initializes the internal stream state for decompression. The fields + next_in, avail_in, zalloc, zfree and opaque must be initialized before by + the caller. If next_in is not Z_NULL and avail_in is large enough (the exact + value depends on the compression method), inflateInit determines the + compression method from the zlib header and allocates all data structures + accordingly; otherwise the allocation will be deferred to the first call of + inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to + use default allocation functions. + + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller. msg is set to null if there is no error + message. inflateInit does not perform any decompression apart from reading + the zlib header if present: this will be done by inflate(). (So next_in and + avail_in may be modified, but next_out and avail_out are unchanged.) +*/ + + +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); +/* + inflate decompresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. inflate performs one or both of the + following actions: + + - Decompress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in is updated and processing + will resume at this point for the next call of inflate(). + + - Provide more output starting at next_out and update next_out and avail_out + accordingly. inflate() provides as much output as possible, until there + is no more input data or no more space in the output buffer (see below + about the flush parameter). + + Before the call of inflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming + more output, and updating the next_* and avail_* values accordingly. + The application can consume the uncompressed output when it wants, for + example when the output buffer is full (avail_out == 0), or after each + call of inflate(). If inflate returns Z_OK and with zero avail_out, it + must be called again after making room in the output buffer because there + might be more output pending. + + The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, + Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much + output as possible to the output buffer. Z_BLOCK requests that inflate() stop + if and when it gets to the next deflate block boundary. When decoding the + zlib or gzip format, this will cause inflate() to return immediately after + the header and before the first block. When doing a raw inflate, inflate() + will go ahead and process the first block, and will return when it gets to + the end of that block, or when it runs out of data. + + The Z_BLOCK option assists in appending to or combining deflate streams. + Also to assist in this, on return inflate() will set strm->data_type to the + number of unused bits in the last byte taken from strm->next_in, plus 64 + if inflate() is currently decoding the last block in the deflate stream, + plus 128 if inflate() returned immediately after decoding an end-of-block + code or decoding the complete header up to just before the first byte of the + deflate stream. The end-of-block will not be indicated until all of the + uncompressed data from that block has been written to strm->next_out. The + number of unused bits may in general be greater than seven, except when + bit 7 of data_type is set, in which case the number of unused bits will be + less than eight. + + inflate() should normally be called until it returns Z_STREAM_END or an + error. However if all decompression is to be performed in a single step + (a single call of inflate), the parameter flush should be set to + Z_FINISH. In this case all pending input is processed and all pending + output is flushed; avail_out must be large enough to hold all the + uncompressed data. (The size of the uncompressed data may have been saved + by the compressor for this purpose.) The next operation on this stream must + be inflateEnd to deallocate the decompression state. The use of Z_FINISH + is never required, but can be used to inform inflate that a faster approach + may be used for the single inflate() call. + + In this implementation, inflate() always flushes as much output as + possible to the output buffer, and always uses the faster approach on the + first call. So the only effect of the flush parameter in this implementation + is on the return value of inflate(), as noted below, or when it returns early + because Z_BLOCK is used. + + If a preset dictionary is needed after this call (see inflateSetDictionary + below), inflate sets strm->adler to the adler32 checksum of the dictionary + chosen by the compressor and returns Z_NEED_DICT; otherwise it sets + strm->adler to the adler32 checksum of all output produced so far (that is, + total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described + below. At the end of the stream, inflate() checks that its computed adler32 + checksum is equal to that saved by the compressor and returns Z_STREAM_END + only if the checksum is correct. + + inflate() will decompress and check either zlib-wrapped or gzip-wrapped + deflate data. The header type is detected automatically. Any information + contained in the gzip header is not retained, so applications that need that + information should instead use raw inflate, see inflateInit2() below, or + inflateBack() and perform their own processing of the gzip header and + trailer. + + inflate() returns Z_OK if some progress has been made (more input processed + or more output produced), Z_STREAM_END if the end of the compressed data has + been reached and all uncompressed output has been produced, Z_NEED_DICT if a + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was + corrupted (input stream not conforming to the zlib format or incorrect check + value), Z_STREAM_ERROR if the stream structure was inconsistent (for example + if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory, + Z_BUF_ERROR if no progress is possible or if there was not enough room in the + output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and + inflate() can be called again with more input and more output space to + continue decompressing. If Z_DATA_ERROR is returned, the application may then + call inflateSync() to look for a good compression block if a partial recovery + of the data is desired. +*/ + + +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any + pending output. + + inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state + was inconsistent. In the error case, msg may be set but then points to a + static string (which must not be deallocated). +*/ + + /* Advanced functions */ + +/* + The following functions are needed only in some special applications. +*/ + +/* +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy)); + + This is another version of deflateInit with more compression options. The + fields next_in, zalloc, zfree and opaque must be initialized before by + the caller. + + The method parameter is the compression method. It must be Z_DEFLATED in + this version of the library. + + The windowBits parameter is the base two logarithm of the window size + (the size of the history buffer). It should be in the range 8..15 for this + version of the library. Larger values of this parameter result in better + compression at the expense of memory usage. The default value is 15 if + deflateInit is used instead. + + windowBits can also be -8..-15 for raw deflate. In this case, -windowBits + determines the window size. deflate() will then generate raw deflate data + with no zlib header or trailer, and will not compute an adler32 check value. + + windowBits can also be greater than 15 for optional gzip encoding. Add + 16 to windowBits to write a simple gzip header and trailer around the + compressed data instead of a zlib wrapper. The gzip header will have no + file name, no extra data, no comment, no modification time (set to zero), + no header crc, and the operating system will be set to 255 (unknown). If a + gzip stream is being written, strm->adler is a crc32 instead of an adler32. + + The memLevel parameter specifies how much memory should be allocated + for the internal compression state. memLevel=1 uses minimum memory but + is slow and reduces compression ratio; memLevel=9 uses maximum memory + for optimal speed. The default value is 8. See zconf.h for total memory + usage as a function of windowBits and memLevel. + + The strategy parameter is used to tune the compression algorithm. Use the + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a + filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no + string match), or Z_RLE to limit match distances to one (run-length + encoding). Filtered data consists mostly of small values with a somewhat + random distribution. In this case, the compression algorithm is tuned to + compress them better. The effect of Z_FILTERED is to force more Huffman + coding and less string matching; it is somewhat intermediate between + Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as + Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy + parameter only affects the compression ratio but not the correctness of the + compressed output even if it is not set appropriately. Z_FIXED prevents the + use of dynamic Huffman codes, allowing for a simpler decoder for special + applications. + + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid + method). msg is set to null if there is no error message. deflateInit2 does + not perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the compression dictionary from the given byte sequence + without producing any compressed output. This function must be called + immediately after deflateInit, deflateInit2 or deflateReset, before any + call of deflate. The compressor and decompressor must use exactly the same + dictionary (see inflateSetDictionary). + + The dictionary should consist of strings (byte sequences) that are likely + to be encountered later in the data to be compressed, with the most commonly + used strings preferably put towards the end of the dictionary. Using a + dictionary is most useful when the data to be compressed is short and can be + predicted with good accuracy; the data can then be compressed better than + with the default empty dictionary. + + Depending on the size of the compression data structures selected by + deflateInit or deflateInit2, a part of the dictionary may in effect be + discarded, for example if the dictionary is larger than the window size in + deflate or deflate2. Thus the strings most likely to be useful should be + put at the end of the dictionary, not at the front. In addition, the + current implementation of deflate will use at most the window size minus + 262 bytes of the provided dictionary. + + Upon return of this function, strm->adler is set to the adler32 value + of the dictionary; the decompressor may later use this value to determine + which dictionary has been used by the compressor. (The adler32 value + applies to the whole dictionary even if only a subset of the dictionary is + actually used by the compressor.) If a raw deflate was requested, then the + adler32 value is not computed and strm->adler is not set. + + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a + parameter is invalid (such as NULL dictionary) or the stream state is + inconsistent (for example if deflate has already been called for this stream + or if the compression method is bsort). deflateSetDictionary does not + perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when several compression strategies will be + tried, for example when there are several ways of pre-processing the input + data with a filter. The streams that will be discarded should then be freed + by calling deflateEnd. Note that deflateCopy duplicates the internal + compression state which can be quite large, so this strategy is slow and + can consume lots of memory. + + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); +/* + This function is equivalent to deflateEnd followed by deflateInit, + but does not free and reallocate all the internal compression state. + The stream will keep the same compression level and any other attributes + that may have been set by deflateInit2. + + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being NULL). +*/ + +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, + int level, + int strategy)); +/* + Dynamically update the compression level and compression strategy. The + interpretation of level and strategy is as in deflateInit2. This can be + used to switch between compression and straight copy of the input data, or + to switch to a different kind of input data requiring a different + strategy. If the compression level is changed, the input available so far + is compressed with the old level (and may be flushed); the new level will + take effect only at the next call of deflate(). + + Before the call of deflateParams, the stream state must be set as for + a call of deflate(), since the currently available input may have to + be compressed and flushed. In particular, strm->avail_out must be non-zero. + + deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source + stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR + if strm->avail_out was zero. +*/ + +ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, + int good_length, + int max_lazy, + int nice_length, + int max_chain)); +/* + Fine tune deflate's internal compression parameters. This should only be + used by someone who understands the algorithm used by zlib's deflate for + searching for the best matching string, and even then only by the most + fanatic optimizer trying to squeeze out the last compressed bit for their + specific input data. Read the deflate.c source code for the meaning of the + max_lazy, good_length, nice_length, and max_chain parameters. + + deflateTune() can be called after deflateInit() or deflateInit2(), and + returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. + */ + +ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, + uLong sourceLen)); +/* + deflateBound() returns an upper bound on the compressed size after + deflation of sourceLen bytes. It must be called after deflateInit() + or deflateInit2(). This would be used to allocate an output buffer + for deflation in a single pass, and so would be called before deflate(). +*/ + +ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + deflatePrime() inserts bits in the deflate output stream. The intent + is that this function is used to start off the deflate output with the + bits leftover from a previous deflate stream when appending to it. As such, + this function can only be used for raw deflate, and must be used before the + first deflate() call after a deflateInit2() or deflateReset(). bits must be + less than or equal to 16, and that many of the least significant bits of + value will be inserted in the output. + + deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, + gz_headerp head)); +/* + deflateSetHeader() provides gzip header information for when a gzip + stream is requested by deflateInit2(). deflateSetHeader() may be called + after deflateInit2() or deflateReset() and before the first call of + deflate(). The text, time, os, extra field, name, and comment information + in the provided gz_header structure are written to the gzip header (xflag is + ignored -- the extra flags are set according to the compression level). The + caller must assure that, if not Z_NULL, name and comment are terminated with + a zero byte, and that if extra is not Z_NULL, that extra_len bytes are + available there. If hcrc is true, a gzip header crc is included. Note that + the current versions of the command-line version of gzip (up through version + 1.3.x) do not support header crc's, and will report that it is a "multi-part + gzip file" and give up. + + If deflateSetHeader is not used, the default gzip header has text false, + the time set to zero, and os set to 255, with no extra, name, or comment + fields. The gzip header is returned to the default state by deflateReset(). + + deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, + int windowBits)); + + This is another version of inflateInit with an extra parameter. The + fields next_in, avail_in, zalloc, zfree and opaque must be initialized + before by the caller. + + The windowBits parameter is the base two logarithm of the maximum window + size (the size of the history buffer). It should be in the range 8..15 for + this version of the library. The default value is 15 if inflateInit is used + instead. windowBits must be greater than or equal to the windowBits value + provided to deflateInit2() while compressing, or it must be equal to 15 if + deflateInit2() was not used. If a compressed stream with a larger window + size is given as input, inflate() will return with the error code + Z_DATA_ERROR instead of trying to allocate a larger window. + + windowBits can also be -8..-15 for raw inflate. In this case, -windowBits + determines the window size. inflate() will then process raw deflate data, + not looking for a zlib or gzip header, not generating a check value, and not + looking for any check values for comparison at the end of the stream. This + is for use with other formats that use the deflate compressed data format + such as zip. Those formats provide their own check values. If a custom + format is developed using the raw deflate format for compressed data, it is + recommended that a check value such as an adler32 or a crc32 be applied to + the uncompressed data as is done in the zlib, gzip, and zip formats. For + most applications, the zlib format should be used as is. Note that comments + above on the use in deflateInit2() applies to the magnitude of windowBits. + + windowBits can also be greater than 15 for optional gzip decoding. Add + 32 to windowBits to enable zlib and gzip decoding with automatic header + detection, or add 16 to decode only the gzip format (the zlib format will + return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is + a crc32 instead of an adler32. + + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg + is set to null if there is no error message. inflateInit2 does not perform + any decompression apart from reading the zlib header if present: this will + be done by inflate(). (So next_in and avail_in may be modified, but next_out + and avail_out are unchanged.) +*/ + +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the decompression dictionary from the given uncompressed byte + sequence. This function must be called immediately after a call of inflate, + if that call returned Z_NEED_DICT. The dictionary chosen by the compressor + can be determined from the adler32 value returned by that call of inflate. + The compressor and decompressor must use exactly the same dictionary (see + deflateSetDictionary). For raw inflate, this function can be called + immediately after inflateInit2() or inflateReset() and before any call of + inflate() to set the dictionary. The application must insure that the + dictionary that was used for compression is provided. + + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a + parameter is invalid (such as NULL dictionary) or the stream state is + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the + expected one (incorrect adler32 value). inflateSetDictionary does not + perform any decompression: this will be done by subsequent calls of + inflate(). +*/ + +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); +/* + Skips invalid compressed data until a full flush point (see above the + description of deflate with Z_FULL_FLUSH) can be found, or until all + available input is skipped. No output is provided. + + inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR + if no more input was provided, Z_DATA_ERROR if no flush point has been found, + or Z_STREAM_ERROR if the stream structure was inconsistent. In the success + case, the application may save the current current value of total_in which + indicates where valid compressed data was found. In the error case, the + application may repeatedly call inflateSync, providing more input each time, + until success or end of the input data. +*/ + +ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when randomly accessing a large stream. The + first pass through the stream can periodically record the inflate state, + allowing restarting inflate at those points when randomly accessing the + stream. + + inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); +/* + This function is equivalent to inflateEnd followed by inflateInit, + but does not free and reallocate all the internal decompression state. + The stream will keep attributes that may have been set by inflateInit2. + + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being NULL). +*/ + +ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + This function inserts bits in the inflate input stream. The intent is + that this function is used to start inflating at a bit position in the + middle of a byte. The provided bits will be used before any bytes are used + from next_in. This function should only be used with raw inflate, and + should be used before the first inflate() call after inflateInit2() or + inflateReset(). bits must be less than or equal to 16, and that many of the + least significant bits of value will be inserted in the input. + + inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, + gz_headerp head)); +/* + inflateGetHeader() requests that gzip header information be stored in the + provided gz_header structure. inflateGetHeader() may be called after + inflateInit2() or inflateReset(), and before the first call of inflate(). + As inflate() processes the gzip stream, head->done is zero until the header + is completed, at which time head->done is set to one. If a zlib stream is + being decoded, then head->done is set to -1 to indicate that there will be + no gzip header information forthcoming. Note that Z_BLOCK can be used to + force inflate() to return immediately after header processing is complete + and before any actual data is decompressed. + + The text, time, xflags, and os fields are filled in with the gzip header + contents. hcrc is set to true if there is a header CRC. (The header CRC + was valid if done is set to one.) If extra is not Z_NULL, then extra_max + contains the maximum number of bytes to write to extra. Once done is true, + extra_len contains the actual extra field length, and extra contains the + extra field, or that field truncated if extra_max is less than extra_len. + If name is not Z_NULL, then up to name_max characters are written there, + terminated with a zero unless the length is greater than name_max. If + comment is not Z_NULL, then up to comm_max characters are written there, + terminated with a zero unless the length is greater than comm_max. When + any of extra, name, or comment are not Z_NULL and the respective field is + not present in the header, then that field is set to Z_NULL to signal its + absence. This allows the use of deflateSetHeader() with the returned + structure to duplicate the header. However if those fields are set to + allocated memory, then the application will need to save those pointers + elsewhere so that they can be eventually freed. + + If inflateGetHeader is not used, then the header information is simply + discarded. The header is always checked for validity, including the header + CRC if present. inflateReset() will reset the process to discard the header + information. The application would need to call inflateGetHeader() again to + retrieve the header from the next gzip stream. + + inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, + unsigned char FAR *window)); + + Initialize the internal stream state for decompression using inflateBack() + calls. The fields zalloc, zfree and opaque in strm must be initialized + before the call. If zalloc and zfree are Z_NULL, then the default library- + derived memory allocation routines are used. windowBits is the base two + logarithm of the window size, in the range 8..15. window is a caller + supplied buffer of that size. Except for special applications where it is + assured that deflate was used with small window sizes, windowBits must be 15 + and a 32K byte window must be supplied to be able to decompress general + deflate streams. + + See inflateBack() for the usage of these routines. + + inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of + the paramaters are invalid, Z_MEM_ERROR if the internal state could not + be allocated, or Z_VERSION_ERROR if the version of the library does not + match the version of the header file. +*/ + +typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *)); +typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); + +ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, + in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc)); +/* + inflateBack() does a raw inflate with a single call using a call-back + interface for input and output. This is more efficient than inflate() for + file i/o applications in that it avoids copying between the output and the + sliding window by simply making the window itself the output buffer. This + function trusts the application to not change the output buffer passed by + the output function, at least until inflateBack() returns. + + inflateBackInit() must be called first to allocate the internal state + and to initialize the state with the user-provided window buffer. + inflateBack() may then be used multiple times to inflate a complete, raw + deflate stream with each call. inflateBackEnd() is then called to free + the allocated state. + + A raw deflate stream is one with no zlib or gzip header or trailer. + This routine would normally be used in a utility that reads zip or gzip + files and writes out uncompressed files. The utility would decode the + header and process the trailer on its own, hence this routine expects + only the raw deflate stream to decompress. This is different from the + normal behavior of inflate(), which expects either a zlib or gzip header and + trailer around the deflate stream. + + inflateBack() uses two subroutines supplied by the caller that are then + called by inflateBack() for input and output. inflateBack() calls those + routines until it reads a complete deflate stream and writes out all of the + uncompressed data, or until it encounters an error. The function's + parameters and return types are defined above in the in_func and out_func + typedefs. inflateBack() will call in(in_desc, &buf) which should return the + number of bytes of provided input, and a pointer to that input in buf. If + there is no input available, in() must return zero--buf is ignored in that + case--and inflateBack() will return a buffer error. inflateBack() will call + out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() + should return zero on success, or non-zero on failure. If out() returns + non-zero, inflateBack() will return with an error. Neither in() nor out() + are permitted to change the contents of the window provided to + inflateBackInit(), which is also the buffer that out() uses to write from. + The length written by out() will be at most the window size. Any non-zero + amount of input may be provided by in(). + + For convenience, inflateBack() can be provided input on the first call by + setting strm->next_in and strm->avail_in. If that input is exhausted, then + in() will be called. Therefore strm->next_in must be initialized before + calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called + immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in + must also be initialized, and then if strm->avail_in is not zero, input will + initially be taken from strm->next_in[0 .. strm->avail_in - 1]. + + The in_desc and out_desc parameters of inflateBack() is passed as the + first parameter of in() and out() respectively when they are called. These + descriptors can be optionally used to pass any information that the caller- + supplied in() and out() functions need to do their job. + + On return, inflateBack() will set strm->next_in and strm->avail_in to + pass back any unused input that was provided by the last in() call. The + return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR + if in() or out() returned an error, Z_DATA_ERROR if there was a format + error in the deflate stream (in which case strm->msg is set to indicate the + nature of the error), or Z_STREAM_ERROR if the stream was not properly + initialized. In the case of Z_BUF_ERROR, an input or output error can be + distinguished using strm->next_in which will be Z_NULL only if in() returned + an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to + out() returning non-zero. (in() will always be called before out(), so + strm->next_in is assured to be defined if out() returns non-zero.) Note + that inflateBack() cannot return Z_OK. +*/ + +ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); +/* + All memory allocated by inflateBackInit() is freed. + + inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream + state was inconsistent. +*/ + +ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); +/* Return flags indicating compile-time options. + + Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: + 1.0: size of uInt + 3.2: size of uLong + 5.4: size of voidpf (pointer) + 7.6: size of z_off_t + + Compiler, assembler, and debug options: + 8: DEBUG + 9: ASMV or ASMINF -- use ASM code + 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention + 11: 0 (reserved) + + One-time table building (smaller code, but not thread-safe if true): + 12: BUILDFIXED -- build static block decoding tables when needed + 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed + 14,15: 0 (reserved) + + Library content (indicates missing functionality): + 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking + deflate code when not needed) + 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect + and decode gzip streams (to avoid linking crc code) + 18-19: 0 (reserved) + + Operation variations (changes in library functionality): + 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate + 21: FASTEST -- deflate algorithm with only one, lowest compression level + 22,23: 0 (reserved) + + The sprintf variant used by gzprintf (zero is best): + 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format + 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! + 26: 0 = returns value, 1 = void -- 1 means inferred string length returned + + Remainder: + 27-31: 0 (reserved) + */ + + + /* utility functions */ + +/* + The following utility functions are implemented on top of the + basic stream-oriented functions. To simplify the interface, some + default options are assumed (compression level and memory usage, + standard memory allocation functions). The source code of these + utility functions can easily be modified if you need special options. +*/ + +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Compresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total + size of the destination buffer, which must be at least the value returned + by compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed buffer. + This function can be used to compress a whole file at once if the + input file is mmap'ed. + compress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer. +*/ + +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level)); +/* + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed buffer. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ + +ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); +/* + compressBound() returns an upper bound on the compressed size after + compress() or compress2() on sourceLen bytes. It would be used before + a compress() or compress2() call to allocate the destination buffer. +*/ + +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Decompresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total + size of the destination buffer, which must be large enough to hold the + entire uncompressed data. (The size of the uncompressed data must have + been saved previously by the compressor and transmitted to the decompressor + by some mechanism outside the scope of this compression library.) + Upon exit, destLen is the actual size of the compressed buffer. + This function can be used to decompress a whole file at once if the + input file is mmap'ed. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. +*/ + + +typedef voidp gzFile; + +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); +/* + Opens a gzip (.gz) file for reading or writing. The mode parameter + is as in fopen ("rb" or "wb") but can also include a compression level + ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for + Huffman only compression as in "wb1h", or 'R' for run-length encoding + as in "wb1R". (See the description of deflateInit2 for more information + about the strategy parameter.) + + 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. + + gzopen returns NULL if the file could not be opened or if there was + insufficient memory to allocate the (de)compression state; errno + can be checked to distinguish the two cases (if errno is zero, the + zlib error is Z_MEM_ERROR). */ + +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); +/* + gzdopen() associates a gzFile with the file descriptor fd. File + descriptors are obtained from calls like open, dup, creat, pipe or + fileno (in the file has been previously opened with fopen). + The mode parameter is as in gzopen. + The next call of gzclose on the returned gzFile will also close the + file descriptor fd, just like fclose(fdopen(fd), mode) closes the file + descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode). + gzdopen returns NULL if there was insufficient memory to allocate + the (de)compression state. +*/ + +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); +/* + Dynamically update the compression level or strategy. See the description + of deflateInit2 for the meaning of these parameters. + gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not + opened for writing. +*/ + +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); +/* + Reads the given number of uncompressed bytes from the compressed file. + If the input file was not in gzip format, gzread copies the given number + of bytes into the buffer. + gzread returns the number of uncompressed bytes actually read (0 for + end of file, -1 for error). */ + +ZEXTERN int ZEXPORT gzwrite OF((gzFile file, + voidpc buf, unsigned len)); +/* + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of uncompressed bytes actually written + (0 in case of error). +*/ + +ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...)); +/* + Converts, formats, and writes the args to the compressed file under + control of the format string, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written (0 in case of error). The number of + uncompressed bytes written is limited to 4095. The caller should assure that + this limit is not exceeded. If it is exceeded, then gzprintf() will return + return an error (0) with nothing written. In this case, there may also be a + buffer overflow with unpredictable consequences, which is possible only if + zlib was compiled with the insecure functions sprintf() or vsprintf() + because the secure snprintf() or vsnprintf() functions were not available. +*/ + +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); +/* + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + gzputs returns the number of characters written, or -1 in case of error. +*/ + +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); +/* + Reads bytes from the compressed file until len-1 characters are read, or + a newline character is read and transferred to buf, or an end-of-file + condition is encountered. The string is then terminated with a null + character. + gzgets returns buf, or Z_NULL in case of error. +*/ + +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); +/* + Writes c, converted to an unsigned char, into the compressed file. + gzputc returns the value that was written, or -1 in case of error. +*/ + +ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); +/* + Reads one byte from the compressed file. gzgetc returns this byte + or -1 in case of end of file or error. +*/ + +ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); +/* + Push one character back onto the stream to be read again later. + Only one character of push-back is allowed. gzungetc() returns the + character pushed, or -1 on failure. gzungetc() will fail if a + character has been pushed but not read yet, or if c is -1. The pushed + character will be discarded if the stream is repositioned with gzseek() + or gzrewind(). +*/ + +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); +/* + Flushes all pending output into the compressed file. The parameter + flush is as in the deflate() function. The return value is the zlib + error number (see function gzerror below). gzflush returns Z_OK if + the flush parameter is Z_FINISH and all output could be flushed. + gzflush should be called only when strictly necessary because it can + degrade compression. +*/ + +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, + z_off_t offset, int whence)); +/* + Sets the starting position for the next gzread or gzwrite on the + given compressed file. The offset represents a number of bytes in the + uncompressed data stream. The whence parameter is defined as in lseek(2); + the value SEEK_END is not supported. + If the file is opened for reading, this function is emulated but can be + extremely slow. If the file is opened for writing, only forward seeks are + supported; gzseek then compresses a sequence of zeroes up to the new + starting position. + + gzseek returns the resulting offset location as measured in bytes from + the beginning of the uncompressed stream, or -1 in case of error, in + particular if the file is opened for writing and the new starting position + would be before the current position. +*/ + +ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); +/* + Rewinds the given file. This function is supported only for reading. + + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) +*/ + +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); +/* + Returns the starting position for the next gzread or gzwrite on the + given compressed file. This position represents a number of bytes in the + uncompressed data stream. + + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) +*/ + +ZEXTERN int ZEXPORT gzeof OF((gzFile file)); +/* + Returns 1 when EOF has previously been detected reading the given + input stream, otherwise zero. +*/ + +ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); +/* + Returns 1 if file is being read directly without decompression, otherwise + zero. +*/ + +ZEXTERN int ZEXPORT gzclose OF((gzFile file)); +/* + Flushes all pending output if necessary, closes the compressed file + and deallocates all the (de)compression state. The return value is the zlib + error number (see function gzerror below). +*/ + +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); +/* + Returns the error message for the last error which occurred on the + given compressed file. errnum is set to zlib error number. If an + error occurred in the file system and not in the compression library, + errnum is set to Z_ERRNO and the application may consult errno + to get the exact error code. +*/ + +ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); +/* + Clears the error and end-of-file flags for file. This is analogous to the + clearerr() function in stdio. This is useful for continuing to read a gzip + file that is being written concurrently. +*/ + + /* checksum functions */ + +/* + These functions are not related to compression but are exported + anyway because they might be useful in applications using the + compression library. +*/ + +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); +/* + Update a running Adler-32 checksum with the bytes buf[0..len-1] and + return the updated checksum. If buf is NULL, this function returns + the required initial value for the checksum. + An Adler-32 checksum is almost as reliable as a CRC32 but can be computed + much faster. Usage example: + + uLong adler = adler32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + adler = adler32(adler, buffer, length); + } + if (adler != original_adler) error(); +*/ + +ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, + z_off_t len2)); +/* + Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 + and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for + each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of + seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. +*/ + +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); +/* + Update a running CRC-32 with the bytes buf[0..len-1] and return the + updated CRC-32. If buf is NULL, this function returns the required initial + value for the for the crc. Pre- and post-conditioning (one's complement) is + performed within this function so it shouldn't be done by the application. + Usage example: + + uLong crc = crc32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + crc = crc32(crc, buffer, length); + } + if (crc != original_crc) error(); +*/ + +ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); + +/* + Combine two CRC-32 check values into one. For two sequences of bytes, + seq1 and seq2 with lengths len1 and len2, CRC-32 check values were + calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 + check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and + len2. +*/ + + + /* various hacks, don't look :) */ + +/* deflateInit and inflateInit are macros to allow checking the zlib version + * and the compiler's view of z_stream: + */ +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, + int windowBits, int memLevel, + int strategy, const char *version, + int stream_size)); +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, + unsigned char FAR *window, + const char *version, + int stream_size)); +#define deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) +#define inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream)) +#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, sizeof(z_stream)) +#define inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) +#define inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, sizeof(z_stream)) + + +#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) + struct internal_state {int dummy;}; /* hack for buggy compilers */ +#endif + +ZEXTERN const char * ZEXPORT zError OF((int)); +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z)); +ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); + +#ifdef __cplusplus +} +#endif + +#endif /* ZLIB_H */ diff --git a/setup.cfg b/setup.cfg index 652736c..b9711b8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,3 +4,9 @@ vendor = TDB packager = TDB distribution-name = Red Hat Linux requires = python + +[egg_info] +tag_build = +tag_date = 0 +tag_svn_revision = 0 + diff --git a/setup.py b/setup.py index ef3d29f..5d3b51a 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ pysam ''' -import os, sys, glob, shutil, hashlib +import os, sys, glob, shutil, hashlib, re, fnmatch name = "pysam" @@ -16,11 +16,22 @@ import version version = version.__version__ -samtools_exclude = ( "bamtk.c", "razip.c", "bgzip.c", "errmod.c", "bam_reheader.c", "bam2bcf.c" ) +samtools_exclude = ( "bamtk.c", "razip.c", "bgzip.c", + "bam_reheader.c", + "main.c", "calDepth.c", "bam2bed.c", + "wgsim.c", "md5fa.c", "maq2sam.c",) samtools_dest = os.path.abspath( "samtools" ) tabix_exclude = ( "main.c", ) tabix_dest = os.path.abspath( "tabix" ) +def locate(pattern, root=os.curdir): + '''Locate all files matching supplied filename pattern in and below + supplied root directory.''' + for path, dirs, files in os.walk(os.path.abspath(root)): + for filename in fnmatch.filter(files, pattern): + yield os.path.join(path, filename) + + # copy samtools source if len(sys.argv) >= 2 and sys.argv[1] == "import": if len(sys.argv) < 3: raise ValueError("missing PATH to samtools source directory") @@ -34,26 +45,73 @@ if len(sys.argv) >= 2 and sys.argv[1] == "import": srcdir = os.path.abspath( srcdir ) if not os.path.exists( srcdir ): raise IOError( "samtools src dir `%s` does not exist." % srcdir ) - cfiles = glob.glob( os.path.join( srcdir, "*.c" ) ) - hfiles = glob.glob( os.path.join( srcdir, "*.h" ) ) + cfiles = locate( "*.c", srcdir ) + hfiles = locate( "*.h", srcdir ) ncopied = 0 - for new_file in cfiles + hfiles: - f = os.path.basename(new_file) - if f in exclude: continue - old_file = os.path.join( destdir, f ) + + def _compareAndCopy( src, srcdir, destdir, exclude ): + + d, f = os.path.split(src) + if f in exclude: return None + common_prefix = os.path.commonprefix( (d, srcdir ) ) + subdir = re.sub( common_prefix, "", d )[1:] + targetdir = os.path.join( destdir, subdir ) + if not os.path.exists( targetdir ): + os.makedirs( targetdir ) + old_file = os.path.join( targetdir, f ) if os.path.exists( old_file ): md5_old = hashlib.md5("".join(open(old_file,"r").readlines())).digest() - md5_new = hashlib.md5("".join(open(new_file,"r").readlines())).digest() - if md5_old == md5_new: continue - raise ValueError( "incompatible files for %s and %s" % (old_file, new_file )) + md5_new = hashlib.md5("".join(open(src,"r").readlines())).digest() + if md5_old != md5_new: + raise ValueError( "incompatible files for %s and %s" % (old_file, src )) + + shutil.copy( src, targetdir ) + return old_file + + for src_file in hfiles: + _compareAndCopy( src_file, srcdir,destdir, exclude ) + ncopied += 1 - shutil.copy( new_file, destdir ) + cf = [] + for src_file in cfiles: + cf.append( _compareAndCopy( src_file, srcdir, destdir, exclude ) ) ncopied += 1 + print "installed latest source code from %s: %i files copied" % (srcdir, ncopied) + # redirect stderr to pysamerr and replace bam.h with a stub. + print "applying stderr redirection" + + for filename in cf: + if not filename: continue + dest = filename + ".pysam.c" + with open( filename ) as infile: + with open( dest, "w" ) as outfile: + outfile.write( '#include "pysam.h"\n\n' ) + outfile.write( re.sub( "stderr", "pysamerr", "".join(infile.readlines()) ) ) + + with open( os.path.join( destdir, "pysam.h" ), "w" )as outfile: + outfile.write ("""#ifndef PYSAM_H +#define PYSAM_H +#include "stdio.h" +extern FILE * pysamerr; +#endif +""") + sys.exit(0) -from distutils.core import setup, Extension -from Cython.Distutils import build_ext +from ez_setup import use_setuptools +use_setuptools() + +from setuptools import Extension, setup + +## note that for automatic cythoning, +## both pyrex and cython need to be installed. +## see http://mail.python.org/pipermail/distutils-sig/2007-September/008204.html + +try: + from Cython.Distutils import build_ext +except: + from setuptools.command.build_ext import build_ext classifiers = """ Development Status :: 2 - Alpha @@ -71,22 +129,23 @@ Topic :: Scientific/Engineering :: Bioinformatics samtools = Extension( "csamtools", # name of extension [ "pysam/csamtools.pyx" ] +\ - [ "pysam/%s" % x for x in ( - "pysam_util.c", )] +\ - glob.glob( os.path.join( "samtools", "*.c" ) ), + [ "pysam/%s" % x for x in ( + "pysam_util.c", )] +\ + glob.glob( os.path.join( "samtools", "*.pysam.c" )) +\ + glob.glob( os.path.join( "samtools", "*", "*.pysam.c" ) ), library_dirs=[], include_dirs=[ "samtools", "pysam" ], libraries=[ "z", ], language="c", - define_macros = [('FILE_OFFSET_BITS','64'), + define_macros = [('_FILE_OFFSET_BITS','64'), ('_USE_KNETFILE','')], ) tabix = Extension( "ctabix", # name of extension [ "pysam/ctabix.pyx" ] +\ - [ "pysam/%s" % x for x in ()] +\ - glob.glob( os.path.join( "tabix", "*.c" ) ), + [ "pysam/%s" % x for x in ( "tabix_util.c", )] +\ + glob.glob( os.path.join( "tabix", "*.pysam.c" ) ), library_dirs=[], include_dirs=[ "tabix", "pysam" ], libraries=[ "z", ], @@ -108,8 +167,10 @@ metadata = { "pysam/Pileup", "pysam/namedtuple", "pysam/version" ], + 'requires' : ['cython (>=0.12)'], 'ext_modules': [samtools, tabix], 'cmdclass' : {'build_ext': build_ext}, + 'install_requires' : ['cython>=0.12.1',], } if __name__=='__main__': diff --git a/tabix/bgzf.c b/tabix/bgzf.c deleted file mode 100644 index 7a936a8..0000000 --- a/tabix/bgzf.c +++ /dev/null @@ -1,676 +0,0 @@ -/* The MIT License - - Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology - - 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. -*/ - -/* - 2009-06-29 by lh3: cache recent uncompressed blocks. - 2009-06-25 by lh3: optionally use my knetfile library to access file on a FTP. - 2009-06-12 by lh3: support a mode string like "wu" where 'u' for uncompressed output */ - -#include -#include -#include -#include -#include -#include -#include -#include "bgzf.h" - -#include "khash.h" -typedef struct { - int size; - uint8_t *block; - int64_t end_offset; -} cache_t; -KHASH_MAP_INIT_INT64(cache, cache_t) - -#if defined(_WIN32) || defined(_MSC_VER) -#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 - -typedef int8_t bgzf_byte_t; - -static const int DEFAULT_BLOCK_SIZE = 64 * 1024; -static const int MAX_BLOCK_SIZE = 64 * 1024; - -static const int BLOCK_HEADER_LENGTH = 18; -static const int BLOCK_FOOTER_LENGTH = 8; - -static const int GZIP_ID1 = 31; -static const int GZIP_ID2 = 139; -static const int CM_DEFLATE = 8; -static const int FLG_FEXTRA = 4; -static const int OS_UNKNOWN = 255; -static const int BGZF_ID1 = 66; // 'B' -static const int BGZF_ID2 = 67; // 'C' -static const int BGZF_LEN = 2; -static const int BGZF_XLEN = 6; // BGZF_LEN+4 - -static const int GZIP_WINDOW_BITS = -15; // no zlib header -static const int Z_DEFAULT_MEM_LEVEL = 8; - - -inline -void -packInt16(uint8_t* buffer, uint16_t value) -{ - buffer[0] = value; - buffer[1] = value >> 8; -} - -inline -int -unpackInt16(const uint8_t* buffer) -{ - return (buffer[0] | (buffer[1] << 8)); -} - -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 inline -int -bgzf_min(int x, int y) -{ - return (x < y) ? x : y; -} - -static -void -report_error(BGZF* fp, const char* message) { - fp->error = message; -} - -static BGZF *bgzf_read_init() -{ - BGZF *fp; - fp = calloc(1, sizeof(BGZF)); - fp->uncompressed_block_size = MAX_BLOCK_SIZE; - fp->uncompressed_block = malloc(MAX_BLOCK_SIZE); - fp->compressed_block_size = MAX_BLOCK_SIZE; - fp->compressed_block = malloc(MAX_BLOCK_SIZE); - fp->cache_size = 0; - fp->cache = kh_init(cache); - return fp; -} - -static -BGZF* -open_read(int fd) -{ -#ifdef _USE_KNETFILE - knetFile *file = knet_dopen(fd, "r"); -#else - FILE* file = fdopen(fd, "r"); -#endif - BGZF* fp; - if (file == 0) return 0; - fp = bgzf_read_init(); - fp->file_descriptor = fd; - fp->open_mode = 'r'; -#ifdef _USE_KNETFILE - fp->x.fpr = file; -#else - fp->file = file; -#endif - return fp; -} - -static -BGZF* -open_write(int fd, bool is_uncompressed) -{ - FILE* file = fdopen(fd, "w"); - BGZF* fp; - if (file == 0) return 0; - fp = malloc(sizeof(BGZF)); - fp->file_descriptor = fd; - fp->open_mode = 'w'; - fp->owned_file = 0; fp->is_uncompressed = is_uncompressed; -#ifdef _USE_KNETFILE - fp->x.fpw = file; -#else - fp->file = file; -#endif - fp->uncompressed_block_size = DEFAULT_BLOCK_SIZE; - fp->uncompressed_block = NULL; - fp->compressed_block_size = MAX_BLOCK_SIZE; - fp->compressed_block = malloc(MAX_BLOCK_SIZE); - fp->block_address = 0; - fp->block_offset = 0; - fp->block_length = 0; - fp->error = NULL; - return fp; -} - -BGZF* -bgzf_open(const char* __restrict path, const char* __restrict mode) -{ - BGZF* fp = NULL; - if (mode[0] == 'r' || mode[0] == 'R') { /* The reading mode is preferred. */ -#ifdef _USE_KNETFILE - knetFile *file = knet_open(path, mode); - if (file == 0) return 0; - fp = bgzf_read_init(); - fp->file_descriptor = -1; - fp->open_mode = 'r'; - fp->x.fpr = file; -#else - int fd, oflag = O_RDONLY; -#ifdef _WIN32 - oflag |= O_BINARY; -#endif - fd = open(path, oflag); - if (fd == -1) return 0; - fp = open_read(fd); -#endif - } else if (mode[0] == 'w' || mode[0] == 'W') { - int fd, oflag = O_WRONLY | O_CREAT | O_TRUNC; -#ifdef _WIN32 - oflag |= O_BINARY; -#endif - fd = open(path, oflag, 0666); - if (fd == -1) return 0; - fp = open_write(fd, strstr(mode, "u")? 1 : 0); - } - if (fp != NULL) { - fp->owned_file = 1; - } - return fp; -} - -BGZF* -bgzf_fdopen(int fd, const char * __restrict mode) -{ - if (fd == -1) return 0; - if (mode[0] == 'r' || mode[0] == 'R') { - return open_read(fd); - } else if (mode[0] == 'w' || mode[0] == 'W') { - return open_write(fd, strstr(mode, "u")? 1 : 0); - } else { - return NULL; - } -} - -static -int -deflate_block(BGZF* fp, int block_length) -{ - // Deflate the block in fp->uncompressed_block into fp->compressed_block. - // Also adds an extra field that stores the compressed block length. - - bgzf_byte_t* buffer = fp->compressed_block; - int buffer_size = fp->compressed_block_size; - - // Init gzip header - buffer[0] = GZIP_ID1; - buffer[1] = GZIP_ID2; - buffer[2] = CM_DEFLATE; - buffer[3] = FLG_FEXTRA; - buffer[4] = 0; // mtime - buffer[5] = 0; - buffer[6] = 0; - buffer[7] = 0; - buffer[8] = 0; - buffer[9] = OS_UNKNOWN; - buffer[10] = BGZF_XLEN; - buffer[11] = 0; - buffer[12] = BGZF_ID1; - buffer[13] = BGZF_ID2; - buffer[14] = BGZF_LEN; - buffer[15] = 0; - buffer[16] = 0; // placeholder for block length - buffer[17] = 0; - - // loop to retry for blocks that do not compress enough - int input_length = block_length; - int compressed_length = 0; - while (1) { - int compress_level = fp->is_uncompressed? 0 : Z_DEFAULT_COMPRESSION; - z_stream zs; - zs.zalloc = NULL; - zs.zfree = NULL; - zs.next_in = fp->uncompressed_block; - zs.avail_in = input_length; - zs.next_out = (void*)&buffer[BLOCK_HEADER_LENGTH]; - zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH; - - int status = deflateInit2(&zs, compress_level, Z_DEFLATED, - GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY); - if (status != Z_OK) { - report_error(fp, "deflate init failed"); - return -1; - } - status = deflate(&zs, Z_FINISH); - if (status != Z_STREAM_END) { - deflateEnd(&zs); - if (status == Z_OK) { - // Not enough space in buffer. - // Can happen in the rare case the input doesn't compress enough. - // Reduce the amount of input until it fits. - input_length -= 1024; - if (input_length <= 0) { - // should never happen - report_error(fp, "input reduction failed"); - return -1; - } - continue; - } - report_error(fp, "deflate failed"); - return -1; - } - status = deflateEnd(&zs); - if (status != Z_OK) { - report_error(fp, "deflate end failed"); - return -1; - } - compressed_length = zs.total_out; - compressed_length += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH; - if (compressed_length > MAX_BLOCK_SIZE) { - // should never happen - report_error(fp, "deflate overflow"); - return -1; - } - break; - } - - packInt16((uint8_t*)&buffer[16], compressed_length-1); - uint32_t crc = crc32(0L, NULL, 0L); - crc = crc32(crc, fp->uncompressed_block, input_length); - packInt32((uint8_t*)&buffer[compressed_length-8], crc); - packInt32((uint8_t*)&buffer[compressed_length-4], input_length); - - int remaining = block_length - input_length; - if (remaining > 0) { - if (remaining > input_length) { - // should never happen (check so we can use memcpy) - report_error(fp, "remainder too large"); - return -1; - } - memcpy(fp->uncompressed_block, - fp->uncompressed_block + input_length, - remaining); - } - fp->block_offset = remaining; - return compressed_length; -} - -static -int -inflate_block(BGZF* fp, int block_length) -{ - // Inflate the block in fp->compressed_block into fp->uncompressed_block - - 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 = fp->uncompressed_block_size; - - int status = inflateInit2(&zs, GZIP_WINDOW_BITS); - if (status != Z_OK) { - report_error(fp, "inflate init failed"); - return -1; - } - status = inflate(&zs, Z_FINISH); - if (status != Z_STREAM_END) { - inflateEnd(&zs); - report_error(fp, "inflate failed"); - return -1; - } - status = inflateEnd(&zs); - if (status != Z_OK) { - report_error(fp, "inflate failed"); - return -1; - } - return zs.total_out; -} - -static -int -check_header(const bgzf_byte_t* header) -{ - return (header[0] == GZIP_ID1 && - header[1] == (bgzf_byte_t) GZIP_ID2 && - header[2] == Z_DEFLATED && - (header[3] & FLG_FEXTRA) != 0 && - unpackInt16((uint8_t*)&header[10]) == BGZF_XLEN && - header[12] == BGZF_ID1 && - header[13] == BGZF_ID2 && - unpackInt16((uint8_t*)&header[14]) == BGZF_LEN); -} - -static void free_cache(BGZF *fp) -{ - khint_t k; - khash_t(cache) *h = (khash_t(cache)*)fp->cache; - if (fp->open_mode != 'r') 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, MAX_BLOCK_SIZE); -#ifdef _USE_KNETFILE - knet_seek(fp->x.fpr, p->end_offset, SEEK_SET); -#else - fseeko(fp->file, p->end_offset, SEEK_SET); -#endif - 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 (MAX_BLOCK_SIZE >= fp->cache_size) return; - if ((kh_size(h) + 1) * 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(MAX_BLOCK_SIZE); - memcpy(kh_val(h, k).block, fp->uncompressed_block, MAX_BLOCK_SIZE); -} - -int -bgzf_read_block(BGZF* fp) -{ - bgzf_byte_t header[BLOCK_HEADER_LENGTH]; - int size = 0; -#ifdef _USE_KNETFILE - int64_t block_address = knet_tell(fp->x.fpr); - if (load_block_from_cache(fp, block_address)) return 0; - int count = knet_read(fp->x.fpr, header, sizeof(header)); -#else - int64_t block_address = ftello(fp->file); - if (load_block_from_cache(fp, block_address)) return 0; - int count = fread(header, 1, sizeof(header), fp->file); -#endif - if (count == 0) { - fp->block_length = 0; - return 0; - } - size = count; - if (count != sizeof(header)) { - report_error(fp, "read failed"); - return -1; - } - if (!check_header(header)) { - report_error(fp, "invalid block header"); - return -1; - } - int block_length = unpackInt16((uint8_t*)&header[16]) + 1; - bgzf_byte_t* compressed_block = (bgzf_byte_t*) fp->compressed_block; - memcpy(compressed_block, header, BLOCK_HEADER_LENGTH); - int remaining = block_length - BLOCK_HEADER_LENGTH; -#ifdef _USE_KNETFILE - count = knet_read(fp->x.fpr, &compressed_block[BLOCK_HEADER_LENGTH], remaining); -#else - count = fread(&compressed_block[BLOCK_HEADER_LENGTH], 1, remaining, fp->file); -#endif - if (count != remaining) { - report_error(fp, "read failed"); - return -1; - } - size += count; - count = inflate_block(fp, block_length); - if (count < 0) { - return -1; - } - if (fp->block_length != 0) { - // Do not reset offset if this read follows a seek. - fp->block_offset = 0; - } - fp->block_address = block_address; - fp->block_length = count; - cache_block(fp, size); - return 0; -} - -int -bgzf_read(BGZF* fp, void* data, int length) -{ - if (length <= 0) { - return 0; - } - if (fp->open_mode != 'r') { - report_error(fp, "file not open for reading"); - return -1; - } - - int bytes_read = 0; - bgzf_byte_t* output = data; - while (bytes_read < length) { - int available = fp->block_length - fp->block_offset; - if (available <= 0) { - if (bgzf_read_block(fp) != 0) { - return -1; - } - available = fp->block_length - fp->block_offset; - if (available <= 0) { - break; - } - } - int copy_length = bgzf_min(length-bytes_read, available); - bgzf_byte_t* 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) { -#ifdef _USE_KNETFILE - fp->block_address = knet_tell(fp->x.fpr); -#else - fp->block_address = ftello(fp->file); -#endif - fp->block_offset = 0; - fp->block_length = 0; - } - return bytes_read; -} - -static -int -flush_block(BGZF* fp) -{ - while (fp->block_offset > 0) { - int block_length = deflate_block(fp, fp->block_offset); - if (block_length < 0) { - return -1; - } -#ifdef _USE_KNETFILE - int count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw); -#else - int count = fwrite(fp->compressed_block, 1, block_length, fp->file); -#endif - if (count != block_length) { - report_error(fp, "write failed"); - return -1; - } - fp->block_address += block_length; - } - return 0; -} - -int -bgzf_write(BGZF* fp, const void* data, int length) -{ - if (fp->open_mode != 'w') { - report_error(fp, "file not open for writing"); - return -1; - } - - if (fp->uncompressed_block == NULL) { - fp->uncompressed_block = malloc(fp->uncompressed_block_size); - } - - const bgzf_byte_t* input = data; - int block_length = fp->uncompressed_block_size; - int bytes_written = 0; - while (bytes_written < length) { - int copy_length = bgzf_min(block_length - fp->block_offset, length - bytes_written); - bgzf_byte_t* buffer = fp->uncompressed_block; - 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) { - if (flush_block(fp) != 0) { - break; - } - } - } - return bytes_written; -} - -int -bgzf_close(BGZF* fp) -{ - if (fp->open_mode == 'w') { - if (flush_block(fp) != 0) { - return -1; - } - { // add an empty block - int count, block_length = deflate_block(fp, 0); -#ifdef _USE_KNETFILE - count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw); -#else - count = fwrite(fp->compressed_block, 1, block_length, fp->file); -#endif - } -#ifdef _USE_KNETFILE - if (fflush(fp->x.fpw) != 0) { -#else - if (fflush(fp->file) != 0) { -#endif - report_error(fp, "flush failed"); - return -1; - } - } - if (fp->owned_file) { -#ifdef _USE_KNETFILE - int ret; - if (fp->open_mode == 'w') ret = fclose(fp->x.fpw); - else ret = knet_close(fp->x.fpr); - if (ret != 0) return -1; -#else - if (fclose(fp->file) != 0) { - return -1; - } -#endif - } - 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; -#ifdef _USE_KNETFILE - offset = knet_tell(fp->x.fpr); - if (knet_seek(fp->x.fpr, -28, SEEK_END) != 0) return -1; - knet_read(fp->x.fpr, buf, 28); - knet_seek(fp->x.fpr, offset, SEEK_SET); -#else - offset = ftello(fp->file); - if (fseeko(fp->file, -28, SEEK_END) != 0) return -1; - fread(buf, 1, 28, fp->file); - fseeko(fp->file, offset, SEEK_SET); -#endif - return (memcmp(magic, buf, 28) == 0)? 1 : 0; -} - -int64_t -bgzf_seek(BGZF* fp, int64_t pos, int where) -{ - if (fp->open_mode != 'r') { - report_error(fp, "file not open for read"); - return -1; - } - if (where != SEEK_SET) { - report_error(fp, "unimplemented seek option"); - return -1; - } - int block_offset = pos & 0xFFFF; - int64_t block_address = (pos >> 16) & 0xFFFFFFFFFFFFLL; -#ifdef _USE_KNETFILE - if (knet_seek(fp->x.fpr, block_address, SEEK_SET) != 0) { -#else - if (fseeko(fp->file, block_address, SEEK_SET) != 0) { -#endif - report_error(fp, "seek failed"); - return -1; - } - fp->block_length = 0; // indicates current block is not loaded - fp->block_address = block_address; - fp->block_offset = block_offset; - return 0; -} diff --git a/tabix/bgzf.c.pysam.c b/tabix/bgzf.c.pysam.c new file mode 100644 index 0000000..6d5995f --- /dev/null +++ b/tabix/bgzf.c.pysam.c @@ -0,0 +1,704 @@ +#include "pysam.h" + +/* The MIT License + + Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology + + 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. +*/ + +/* + 2009-06-29 by lh3: cache recent uncompressed blocks. + 2009-06-25 by lh3: optionally use my knetfile library to access file on a FTP. + 2009-06-12 by lh3: support a mode string like "wu" where 'u' for uncompressed output */ + +#include +#include +#include +#include +#include +#include +#include +#include "bgzf.h" + +#include "khash.h" +typedef struct { + int size; + uint8_t *block; + int64_t end_offset; +} cache_t; +KHASH_MAP_INIT_INT64(cache, cache_t) + +#if defined(_WIN32) || defined(_MSC_VER) +#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 + +typedef int8_t bgzf_byte_t; + +static const int DEFAULT_BLOCK_SIZE = 64 * 1024; +static const int MAX_BLOCK_SIZE = 64 * 1024; + +static const int BLOCK_HEADER_LENGTH = 18; +static const int BLOCK_FOOTER_LENGTH = 8; + +static const int GZIP_ID1 = 31; +static const int GZIP_ID2 = 139; +static const int CM_DEFLATE = 8; +static const int FLG_FEXTRA = 4; +static const int OS_UNKNOWN = 255; +static const int BGZF_ID1 = 66; // 'B' +static const int BGZF_ID2 = 67; // 'C' +static const int BGZF_LEN = 2; +static const int BGZF_XLEN = 6; // BGZF_LEN+4 + +static const int GZIP_WINDOW_BITS = -15; // no zlib header +static const int Z_DEFAULT_MEM_LEVEL = 8; + + +inline +void +packInt16(uint8_t* buffer, uint16_t value) +{ + buffer[0] = value; + buffer[1] = value >> 8; +} + +inline +int +unpackInt16(const uint8_t* buffer) +{ + return (buffer[0] | (buffer[1] << 8)); +} + +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 inline +int +bgzf_min(int x, int y) +{ + return (x < y) ? x : y; +} + +static +void +report_error(BGZF* fp, const char* message) { + fp->error = message; +} + +int is_bgzipped(const char *fn) +{ + BGZF *fp; + uint8_t buf[10],magic[10]="\037\213\010\4\0\0\0\0\0\377"; + int n; + + if ((fp = bgzf_open(fn, "r")) == 0) + { + fprintf(pysamerr, "[is_bgzipped] failed to open the file: %s\n",fn); + return -1; + } + +#ifdef _USE_KNETFILE + n = knet_read(fp->x.fpr, buf, 10); +#else + n = fread(buf, 1, 10, fp->file); +#endif + bgzf_close(fp); + + if ( n!=10 ) + return -1; + + if ( !memcmp(magic, buf, 10) ) return 1; + return 0; +} + +static BGZF *bgzf_read_init() +{ + BGZF *fp; + fp = calloc(1, sizeof(BGZF)); + fp->uncompressed_block_size = MAX_BLOCK_SIZE; + fp->uncompressed_block = malloc(MAX_BLOCK_SIZE); + fp->compressed_block_size = MAX_BLOCK_SIZE; + fp->compressed_block = malloc(MAX_BLOCK_SIZE); + fp->cache_size = 0; + fp->cache = kh_init(cache); + return fp; +} + +static +BGZF* +open_read(int fd) +{ +#ifdef _USE_KNETFILE + knetFile *file = knet_dopen(fd, "r"); +#else + FILE* file = fdopen(fd, "r"); +#endif + BGZF* fp; + if (file == 0) return 0; + fp = bgzf_read_init(); + fp->file_descriptor = fd; + fp->open_mode = 'r'; +#ifdef _USE_KNETFILE + fp->x.fpr = file; +#else + fp->file = file; +#endif + return fp; +} + +static +BGZF* +open_write(int fd, bool is_uncompressed) +{ + FILE* file = fdopen(fd, "w"); + BGZF* fp; + if (file == 0) return 0; + fp = malloc(sizeof(BGZF)); + fp->file_descriptor = fd; + fp->open_mode = 'w'; + fp->owned_file = 0; fp->is_uncompressed = is_uncompressed; +#ifdef _USE_KNETFILE + fp->x.fpw = file; +#else + fp->file = file; +#endif + fp->uncompressed_block_size = DEFAULT_BLOCK_SIZE; + fp->uncompressed_block = NULL; + fp->compressed_block_size = MAX_BLOCK_SIZE; + fp->compressed_block = malloc(MAX_BLOCK_SIZE); + fp->block_address = 0; + fp->block_offset = 0; + fp->block_length = 0; + fp->error = NULL; + return fp; +} + +BGZF* +bgzf_open(const char* __restrict path, const char* __restrict mode) +{ + BGZF* fp = NULL; + if (mode[0] == 'r' || mode[0] == 'R') { /* The reading mode is preferred. */ +#ifdef _USE_KNETFILE + knetFile *file = knet_open(path, mode); + if (file == 0) return 0; + fp = bgzf_read_init(); + fp->file_descriptor = -1; + fp->open_mode = 'r'; + fp->x.fpr = file; +#else + int fd, oflag = O_RDONLY; +#ifdef _WIN32 + oflag |= O_BINARY; +#endif + fd = open(path, oflag); + if (fd == -1) return 0; + fp = open_read(fd); +#endif + } else if (mode[0] == 'w' || mode[0] == 'W') { + int fd, oflag = O_WRONLY | O_CREAT | O_TRUNC; +#ifdef _WIN32 + oflag |= O_BINARY; +#endif + fd = open(path, oflag, 0666); + if (fd == -1) return 0; + fp = open_write(fd, strstr(mode, "u")? 1 : 0); + } + if (fp != NULL) { + fp->owned_file = 1; + } + return fp; +} + +BGZF* +bgzf_fdopen(int fd, const char * __restrict mode) +{ + if (fd == -1) return 0; + if (mode[0] == 'r' || mode[0] == 'R') { + return open_read(fd); + } else if (mode[0] == 'w' || mode[0] == 'W') { + return open_write(fd, strstr(mode, "u")? 1 : 0); + } else { + return NULL; + } +} + +static +int +deflate_block(BGZF* fp, int block_length) +{ + // Deflate the block in fp->uncompressed_block into fp->compressed_block. + // Also adds an extra field that stores the compressed block length. + + bgzf_byte_t* buffer = fp->compressed_block; + int buffer_size = fp->compressed_block_size; + + // Init gzip header + buffer[0] = GZIP_ID1; + buffer[1] = GZIP_ID2; + buffer[2] = CM_DEFLATE; + buffer[3] = FLG_FEXTRA; + buffer[4] = 0; // mtime + buffer[5] = 0; + buffer[6] = 0; + buffer[7] = 0; + buffer[8] = 0; + buffer[9] = OS_UNKNOWN; + buffer[10] = BGZF_XLEN; + buffer[11] = 0; + buffer[12] = BGZF_ID1; + buffer[13] = BGZF_ID2; + buffer[14] = BGZF_LEN; + buffer[15] = 0; + buffer[16] = 0; // placeholder for block length + buffer[17] = 0; + + // loop to retry for blocks that do not compress enough + int input_length = block_length; + int compressed_length = 0; + while (1) { + int compress_level = fp->is_uncompressed? 0 : Z_DEFAULT_COMPRESSION; + z_stream zs; + zs.zalloc = NULL; + zs.zfree = NULL; + zs.next_in = fp->uncompressed_block; + zs.avail_in = input_length; + zs.next_out = (void*)&buffer[BLOCK_HEADER_LENGTH]; + zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH; + + int status = deflateInit2(&zs, compress_level, Z_DEFLATED, + GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY); + if (status != Z_OK) { + report_error(fp, "deflate init failed"); + return -1; + } + status = deflate(&zs, Z_FINISH); + if (status != Z_STREAM_END) { + deflateEnd(&zs); + if (status == Z_OK) { + // Not enough space in buffer. + // Can happen in the rare case the input doesn't compress enough. + // Reduce the amount of input until it fits. + input_length -= 1024; + if (input_length <= 0) { + // should never happen + report_error(fp, "input reduction failed"); + return -1; + } + continue; + } + report_error(fp, "deflate failed"); + return -1; + } + status = deflateEnd(&zs); + if (status != Z_OK) { + report_error(fp, "deflate end failed"); + return -1; + } + compressed_length = zs.total_out; + compressed_length += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH; + if (compressed_length > MAX_BLOCK_SIZE) { + // should never happen + report_error(fp, "deflate overflow"); + return -1; + } + break; + } + + packInt16((uint8_t*)&buffer[16], compressed_length-1); + uint32_t crc = crc32(0L, NULL, 0L); + crc = crc32(crc, fp->uncompressed_block, input_length); + packInt32((uint8_t*)&buffer[compressed_length-8], crc); + packInt32((uint8_t*)&buffer[compressed_length-4], input_length); + + int remaining = block_length - input_length; + if (remaining > 0) { + if (remaining > input_length) { + // should never happen (check so we can use memcpy) + report_error(fp, "remainder too large"); + return -1; + } + memcpy(fp->uncompressed_block, + fp->uncompressed_block + input_length, + remaining); + } + fp->block_offset = remaining; + return compressed_length; +} + +static +int +inflate_block(BGZF* fp, int block_length) +{ + // Inflate the block in fp->compressed_block into fp->uncompressed_block + + 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 = fp->uncompressed_block_size; + + int status = inflateInit2(&zs, GZIP_WINDOW_BITS); + if (status != Z_OK) { + report_error(fp, "inflate init failed"); + return -1; + } + status = inflate(&zs, Z_FINISH); + if (status != Z_STREAM_END) { + inflateEnd(&zs); + report_error(fp, "inflate failed"); + return -1; + } + status = inflateEnd(&zs); + if (status != Z_OK) { + report_error(fp, "inflate failed"); + return -1; + } + return zs.total_out; +} + +static +int +check_header(const bgzf_byte_t* header) +{ + return (header[0] == GZIP_ID1 && + header[1] == (bgzf_byte_t) GZIP_ID2 && + header[2] == Z_DEFLATED && + (header[3] & FLG_FEXTRA) != 0 && + unpackInt16((uint8_t*)&header[10]) == BGZF_XLEN && + header[12] == BGZF_ID1 && + header[13] == BGZF_ID2 && + unpackInt16((uint8_t*)&header[14]) == BGZF_LEN); +} + +static void free_cache(BGZF *fp) +{ + khint_t k; + khash_t(cache) *h = (khash_t(cache)*)fp->cache; + if (fp->open_mode != 'r') 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, MAX_BLOCK_SIZE); +#ifdef _USE_KNETFILE + knet_seek(fp->x.fpr, p->end_offset, SEEK_SET); +#else + fseeko(fp->file, p->end_offset, SEEK_SET); +#endif + 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 (MAX_BLOCK_SIZE >= fp->cache_size) return; + if ((kh_size(h) + 1) * 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(MAX_BLOCK_SIZE); + memcpy(kh_val(h, k).block, fp->uncompressed_block, MAX_BLOCK_SIZE); +} + +int +bgzf_read_block(BGZF* fp) +{ + bgzf_byte_t header[BLOCK_HEADER_LENGTH]; + int size = 0; +#ifdef _USE_KNETFILE + int64_t block_address = knet_tell(fp->x.fpr); + if (load_block_from_cache(fp, block_address)) return 0; + int count = knet_read(fp->x.fpr, header, sizeof(header)); +#else + int64_t block_address = ftello(fp->file); + if (load_block_from_cache(fp, block_address)) return 0; + int count = fread(header, 1, sizeof(header), fp->file); +#endif + if (count == 0) { + fp->block_length = 0; + return 0; + } + size = count; + if (count != sizeof(header)) { + report_error(fp, "read failed"); + return -1; + } + if (!check_header(header)) { + report_error(fp, "invalid block header"); + return -1; + } + int block_length = unpackInt16((uint8_t*)&header[16]) + 1; + bgzf_byte_t* compressed_block = (bgzf_byte_t*) fp->compressed_block; + memcpy(compressed_block, header, BLOCK_HEADER_LENGTH); + int remaining = block_length - BLOCK_HEADER_LENGTH; +#ifdef _USE_KNETFILE + count = knet_read(fp->x.fpr, &compressed_block[BLOCK_HEADER_LENGTH], remaining); +#else + count = fread(&compressed_block[BLOCK_HEADER_LENGTH], 1, remaining, fp->file); +#endif + if (count != remaining) { + report_error(fp, "read failed"); + return -1; + } + size += count; + count = inflate_block(fp, block_length); + if (count < 0) { + return -1; + } + if (fp->block_length != 0) { + // Do not reset offset if this read follows a seek. + fp->block_offset = 0; + } + fp->block_address = block_address; + fp->block_length = count; + cache_block(fp, size); + return 0; +} + +int +bgzf_read(BGZF* fp, void* data, int length) +{ + if (length <= 0) { + return 0; + } + if (fp->open_mode != 'r') { + report_error(fp, "file not open for reading"); + return -1; + } + + int bytes_read = 0; + bgzf_byte_t* output = data; + while (bytes_read < length) { + int available = fp->block_length - fp->block_offset; + if (available <= 0) { + if (bgzf_read_block(fp) != 0) { + return -1; + } + available = fp->block_length - fp->block_offset; + if (available <= 0) { + break; + } + } + int copy_length = bgzf_min(length-bytes_read, available); + bgzf_byte_t* 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) { +#ifdef _USE_KNETFILE + fp->block_address = knet_tell(fp->x.fpr); +#else + fp->block_address = ftello(fp->file); +#endif + fp->block_offset = 0; + fp->block_length = 0; + } + return bytes_read; +} + +static +int +flush_block(BGZF* fp) +{ + while (fp->block_offset > 0) { + int block_length = deflate_block(fp, fp->block_offset); + if (block_length < 0) { + return -1; + } +#ifdef _USE_KNETFILE + int count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw); +#else + int count = fwrite(fp->compressed_block, 1, block_length, fp->file); +#endif + if (count != block_length) { + report_error(fp, "write failed"); + return -1; + } + fp->block_address += block_length; + } + return 0; +} + +int +bgzf_write(BGZF* fp, const void* data, int length) +{ + if (fp->open_mode != 'w') { + report_error(fp, "file not open for writing"); + return -1; + } + + if (fp->uncompressed_block == NULL) { + fp->uncompressed_block = malloc(fp->uncompressed_block_size); + } + + const bgzf_byte_t* input = data; + int block_length = fp->uncompressed_block_size; + int bytes_written = 0; + while (bytes_written < length) { + int copy_length = bgzf_min(block_length - fp->block_offset, length - bytes_written); + bgzf_byte_t* buffer = fp->uncompressed_block; + 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) { + if (flush_block(fp) != 0) { + break; + } + } + } + return bytes_written; +} + +int +bgzf_close(BGZF* fp) +{ + if (fp->open_mode == 'w') { + if (flush_block(fp) != 0) { + return -1; + } + { // add an empty block + int count, block_length = deflate_block(fp, 0); +#ifdef _USE_KNETFILE + count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw); +#else + count = fwrite(fp->compressed_block, 1, block_length, fp->file); +#endif + } +#ifdef _USE_KNETFILE + if (fflush(fp->x.fpw) != 0) { +#else + if (fflush(fp->file) != 0) { +#endif + report_error(fp, "flush failed"); + return -1; + } + } + if (fp->owned_file) { +#ifdef _USE_KNETFILE + int ret; + if (fp->open_mode == 'w') ret = fclose(fp->x.fpw); + else ret = knet_close(fp->x.fpr); + if (ret != 0) return -1; +#else + if (fclose(fp->file) != 0) { + return -1; + } +#endif + } + 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; +#ifdef _USE_KNETFILE + offset = knet_tell(fp->x.fpr); + if (knet_seek(fp->x.fpr, -28, SEEK_END) != 0) return -1; + knet_read(fp->x.fpr, buf, 28); + knet_seek(fp->x.fpr, offset, SEEK_SET); +#else + offset = ftello(fp->file); + if (fseeko(fp->file, -28, SEEK_END) != 0) return -1; + fread(buf, 1, 28, fp->file); + fseeko(fp->file, offset, SEEK_SET); +#endif + return (memcmp(magic, buf, 28) == 0)? 1 : 0; +} + +int64_t +bgzf_seek(BGZF* fp, int64_t pos, int where) +{ + if (fp->open_mode != 'r') { + report_error(fp, "file not open for read"); + return -1; + } + if (where != SEEK_SET) { + report_error(fp, "unimplemented seek option"); + return -1; + } + int block_offset = pos & 0xFFFF; + int64_t block_address = (pos >> 16) & 0xFFFFFFFFFFFFLL; +#ifdef _USE_KNETFILE + if (knet_seek(fp->x.fpr, block_address, SEEK_SET) != 0) { +#else + if (fseeko(fp->file, block_address, SEEK_SET) != 0) { +#endif + report_error(fp, "seek failed"); + return -1; + } + fp->block_length = 0; // indicates current block is not loaded + fp->block_address = block_address; + fp->block_offset = block_offset; + return 0; +} diff --git a/tabix/bgzf.h b/tabix/bgzf.h index f544a67..70f497e 100644 --- a/tabix/bgzf.h +++ b/tabix/bgzf.h @@ -62,6 +62,13 @@ typedef struct { extern "C" { #endif +/* + * Checks the magic string of the file. Returns 1 + * for bgzipped files, -1 on errors and 0 for files + * without the bgzip magic string. + */ +int is_bgzipped(const char *path); + /* * Open an existing file descriptor for reading or writing. * Mode must be either "r" or "w". diff --git a/tabix/bgzip.c b/tabix/bgzip.c deleted file mode 100644 index d144632..0000000 --- a/tabix/bgzip.c +++ /dev/null @@ -1,201 +0,0 @@ -/* The MIT License - - Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology - - 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 -#include -#include -#include -#include -#include -#include -#include -#include "bgzf.h" - -static const int WINDOW_SIZE = 64 * 1024; - -static int bgzip_main_usage() -{ - fprintf(stderr, "\n"); - fprintf(stderr, "Usage: bgzip [options] [file] ...\n\n"); - fprintf(stderr, "Options: -c write on standard output, keep original files unchanged\n"); - fprintf(stderr, " -d decompress\n"); - fprintf(stderr, " -f overwrite files without asking\n"); - fprintf(stderr, " -b INT decompress at virtual file pointer INT\n"); - fprintf(stderr, " -s INT decompress INT bytes in the uncompressed file\n"); - fprintf(stderr, " -h give this help\n"); - fprintf(stderr, "\n"); - return 1; -} - -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); - scanf("%c", &c); - if (c != 'Y' && c != 'y') { - fprintf(stderr, "[bgzip] not overwritten\n"); - exit(1); - } - } - } - 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(1); - } - } - return fd; -} - -static void fail(BGZF* fp) -{ - fprintf(stderr, "Error: %s\n", fp->error); - exit(1); -} - -int main(int argc, char **argv) -{ - int c, compress, pstdout, is_forced; - BGZF *fp; - void *buffer; - long start, end, size; - - compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0; - while((c = getopt(argc, argv, "cdhfb:s:")) >= 0){ - switch(c){ - case 'h': return bgzip_main_usage(); - case 'd': compress = 0; break; - case 'c': pstdout = 1; break; - case 'b': start = atol(optarg); break; - case 's': size = atol(optarg); break; - case 'f': is_forced = 1; break; - } - } - 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 ((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(); - - fp = bgzf_fdopen(f_dst, "w"); - buffer = malloc(WINDOW_SIZE); - while ((c = read(f_src, buffer, WINDOW_SIZE)) > 0) - if (bgzf_write(fp, buffer, c) < 0) fail(fp); - // f_dst will be closed here - if (bgzf_close(fp) < 0) fail(fp); - if (argc > optind) unlink(argv[optind]); - free(buffer); - close(f_src); - return 0; - } else { - 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; - } - - 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 (bgzf_seek(fp, start, SEEK_SET) < 0) fail(fp); - while (1) { - if (end < 0) c = bgzf_read(fp, buffer, WINDOW_SIZE); - else c = bgzf_read(fp, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start)); - if (c == 0) break; - if (c < 0) fail(fp); - start += c; - write(f_dst, buffer, c); - if (end >= 0 && start >= end) break; - } - free(buffer); - if (bgzf_close(fp) < 0) fail(fp); - if (!pstdout) unlink(argv[optind]); - return 0; - } -} diff --git a/tabix/bgzip.c.pysam.c b/tabix/bgzip.c.pysam.c new file mode 100644 index 0000000..55e7029 --- /dev/null +++ b/tabix/bgzip.c.pysam.c @@ -0,0 +1,208 @@ +#include "pysam.h" + +/* The MIT License + + Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology + + 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 +#include +#include +#include +#include +#include +#include +#include +#include "bgzf.h" + +static const int WINDOW_SIZE = 64 * 1024; + +static int bgzip_main_usage() +{ + fprintf(pysamerr, "\n"); + fprintf(pysamerr, "Usage: bgzip [options] [file] ...\n\n"); + fprintf(pysamerr, "Options: -c write on standard output, keep original files unchanged\n"); + fprintf(pysamerr, " -d decompress\n"); + fprintf(pysamerr, " -f overwrite files without asking\n"); + fprintf(pysamerr, " -b INT decompress at virtual file pointer INT\n"); + fprintf(pysamerr, " -s INT decompress INT bytes in the uncompressed file\n"); + fprintf(pysamerr, " -h give this help\n"); + fprintf(pysamerr, "\n"); + return 1; +} + +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(pysamerr, "[bgzip] %s already exists; do you wish to overwrite (y or n)? ", fn); + scanf("%c", &c); + if (c != 'Y' && c != 'y') { + fprintf(pysamerr, "[bgzip] not overwritten\n"); + exit(1); + } + } + } + if (fd < 0) { + if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { + fprintf(pysamerr, "[bgzip] %s: Fail to write\n", fn); + exit(1); + } + } + return fd; +} + +static void fail(BGZF* fp) +{ + fprintf(pysamerr, "Error: %s\n", fp->error); + exit(1); +} + +int main(int argc, char **argv) +{ + int c, compress, pstdout, is_forced; + BGZF *fp; + void *buffer; + long start, end, size; + + compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0; + while((c = getopt(argc, argv, "cdhfb:s:")) >= 0){ + switch(c){ + case 'h': return bgzip_main_usage(); + case 'd': compress = 0; break; + case 'c': pstdout = 1; break; + case 'b': start = atol(optarg); break; + case 's': size = atol(optarg); break; + case 'f': is_forced = 1; break; + } + } + if (size >= 0) end = start + size; + if (end >= 0 && end < start) { + fprintf(pysamerr, "[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(pysamerr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]); + return 1; + } + + if ((f_src = open(argv[optind], O_RDONLY)) < 0) { + fprintf(pysamerr, "[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(); + + fp = bgzf_fdopen(f_dst, "w"); + buffer = malloc(WINDOW_SIZE); + while ((c = read(f_src, buffer, WINDOW_SIZE)) > 0) + if (bgzf_write(fp, buffer, c) < 0) fail(fp); + // f_dst will be closed here + if (bgzf_close(fp) < 0) fail(fp); + if (argc > optind && !pstdout) unlink(argv[optind]); + free(buffer); + close(f_src); + return 0; + } else { + struct stat sbuf; + int f_dst; + + if ( argc>optind ) + { + if ( stat(argv[optind],&sbuf)<0 ) + { + fprintf(pysamerr, "[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(pysamerr, "[bgzip] %s: unknown suffix -- ignored\n", argv[optind]); + return 1; + } + fp = bgzf_open(argv[optind], "r"); + if (fp == NULL) { + fprintf(pysamerr, "[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(pysamerr, "[bgzip] Could not read from stdin: %s\n", strerror(errno)); + return 1; + } + } + buffer = malloc(WINDOW_SIZE); + if (bgzf_seek(fp, start, SEEK_SET) < 0) fail(fp); + while (1) { + if (end < 0) c = bgzf_read(fp, buffer, WINDOW_SIZE); + else c = bgzf_read(fp, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start)); + if (c == 0) break; + if (c < 0) fail(fp); + start += c; + write(f_dst, buffer, c); + if (end >= 0 && start >= end) break; + } + free(buffer); + if (bgzf_close(fp) < 0) fail(fp); + if (!pstdout) unlink(argv[optind]); + return 0; + } +} diff --git a/tabix/index.c b/tabix/index.c deleted file mode 100644 index e5b227c..0000000 --- a/tabix/index.c +++ /dev/null @@ -1,954 +0,0 @@ -#include -#include -#include -#include "khash.h" -#include "ksort.h" -#include "kstring.h" -#include "bam_endian.h" -#ifdef _USE_KNETFILE -#include "knetfile.h" -#endif -#include "tabix.h" - -#define TAD_MIN_CHUNK_GAP 32768 -// 1<<14 is the size of minimum bin. -#define TAD_LIDX_SHIFT 14 - -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; -} ti_binlist_t; - -typedef struct { - int32_t n, m; - uint64_t *offset; -} ti_lidx_t; - -KHASH_MAP_INIT_INT(i, ti_binlist_t) -KHASH_MAP_INIT_STR(s, int) - -struct __ti_index_t { - ti_conf_t conf; - int32_t n, max; - khash_t(s) *tname; - khash_t(i) **index; - ti_lidx_t *index2; -}; - -struct __ti_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; - kstring_t str; - const ti_index_t *idx; - pair64_t *off; -}; - -typedef struct { - int tid, beg, end, bin; -} ti_intv_t; - -ti_conf_t ti_conf_gff = { 0, 1, 4, 5, '#', 0 }; -ti_conf_t ti_conf_bed = { TI_FLAG_UCSC, 1, 2, 3, '#', 0 }; -ti_conf_t ti_conf_psltbl = { TI_FLAG_UCSC, 15, 17, 18, '#', 0 }; -ti_conf_t ti_conf_sam = { TI_PRESET_SAM, 3, 4, 0, '@', 0 }; -ti_conf_t ti_conf_vcf = { TI_PRESET_VCF, 1, 2, 0, '#', 0 }; - -/*************** - * read a line * - ***************/ - -/* -int ti_readline(BGZF *fp, kstring_t *str) -{ - int c, l = 0; - str->l = 0; - while ((c = bgzf_getc(fp)) >= 0 && c != '\n') { - ++l; - if (c != '\r') kputc(c, str); - } - if (c < 0 && l == 0) return -1; // end of file - return str->l; -} -*/ - -/* Below is a faster implementation largely equivalent to the one - * commented out above. */ -int ti_readline(BGZF *fp, 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] != '\n'; ++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) { -#ifdef _USE_KNETFILE - fp->block_address = knet_tell(fp->x.fpr); -#else - fp->block_address = ftello(fp->file); -#endif - 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; -} - -/************************************* - * get the interval from a data line * - *************************************/ - -static inline int ti_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; -} - -static int get_tid(ti_index_t *idx, const char *ss) -{ - khint_t k; - int tid; - k = kh_get(s, idx->tname, ss); - if (k == kh_end(idx->tname)) { // a new target sequence - int ret, size; - // update idx->n, ->max, ->index and ->index2 - if (idx->n == idx->max) { - idx->max = idx->max? idx->max<<1 : 8; - idx->index = realloc(idx->index, idx->max * sizeof(void*)); - idx->index2 = realloc(idx->index2, idx->max * sizeof(ti_lidx_t)); - } - memset(&idx->index2[idx->n], 0, sizeof(ti_lidx_t)); - idx->index[idx->n++] = kh_init(i); - // update ->tname - tid = size = kh_size(idx->tname); - k = kh_put(s, idx->tname, strdup(ss), &ret); - kh_value(idx->tname, k) = size; - assert(idx->n == kh_size(idx->tname)); - } else tid = kh_value(idx->tname, k); - return tid; -} - -static int get_intv(ti_index_t *idx, kstring_t *str, ti_intv_t *intv) -{ - int i, b = 0, id = 1; - char *s; - intv->tid = intv->beg = intv->end = intv->bin = -1; - for (i = 0; i <= str->l; ++i) { - if (str->s[i] == '\t' || str->s[i] == 0) { - if (id == idx->conf.sc) { - str->s[i] = 0; - intv->tid = get_tid(idx, str->s + b); - if (i != str->l) str->s[i] = '\t'; - } else if (id == idx->conf.bc) { - // here ->beg is 0-based. - intv->beg = intv->end = strtol(str->s + b, &s, 0); - if (!(idx->conf.preset&TI_FLAG_UCSC)) --intv->beg; - else ++intv->end; - if (intv->beg < 0) intv->beg = 0; - if (intv->end < 1) intv->end = 1; - } else { - if ((idx->conf.preset&0xffff) == TI_PRESET_GENERIC) { - if (id == idx->conf.ec) intv->end = strtol(str->s + b, &s, 0); - } else if ((idx->conf.preset&0xffff) == TI_PRESET_SAM) { - if (id == 6) { // CIGAR - int l = 0, op; - char *t; - for (s = str->s + b; s < str->s + 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 ((idx->conf.preset&0xffff) == TI_PRESET_VCF) { - // FIXME: the following is NOT tested and is likely to be buggy - if (id == 5) { // ALT - char *t; - int max = 1; - for (s = str->s + b; s < str->s + i;) { - if (s[i] == 'D') { - long x = strtol(s + 1, &t, 10); - if (x > max) max = x; - s = t + 1; - } else ++s; - } - intv->end = intv->beg + max; - } - } - } - b = i + 1; - ++id; - } - } - if (intv->tid < 0 || intv->beg < 0 || intv->end < 0) return -1; - intv->bin = ti_reg2bin(intv->beg, intv->end); - return 0; -} - -/************ - * indexing * - ************/ - -// requirement: len <= LEN_MASK -static inline void insert_offset(khash_t(i) *h, int bin, uint64_t beg, uint64_t end) -{ - khint_t k; - ti_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(ti_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; -} - -static void merge_chunks(ti_index_t *idx) -{ - 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) { - ti_binlist_t *p; - if (!kh_exist(index, k)) continue; - p = &kh_value(index, k); - m = 0; - for (l = 1; l < p->n; ++l) { - if (p->list[m].v>>16 == p->list[l].u>>16) p->list[m].v = p->list[l].v; - else p->list[++m] = p->list[l]; - } // ~for(l) - p->n = m + 1; - } // ~for(k) - } // ~for(i) -} - -static void fill_missing(ti_index_t *idx) -{ - int i, j; - for (i = 0; i < idx->n; ++i) { - ti_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]; - } -} - -ti_index_t *ti_index_core(BGZF *fp, const ti_conf_t *conf) -{ - int ret; - ti_index_t *idx; - uint32_t last_bin, save_bin; - int32_t last_coor, last_tid, save_tid; - uint64_t save_off, last_off, lineno = 0; - kstring_t *str; - - str = calloc(1, sizeof(kstring_t)); - - idx = (ti_index_t*)calloc(1, sizeof(ti_index_t)); - idx->conf = *conf; - idx->n = idx->max = 0; - idx->tname = kh_init(s); - idx->index = 0; - idx->index2 = 0; - - save_bin = save_tid = last_tid = last_bin = 0xffffffffu; - save_off = last_off = bgzf_tell(fp); last_coor = 0xffffffffu; - while ((ret = ti_readline(fp, str)) >= 0) { - ti_intv_t intv; - ++lineno; - if (lineno <= idx->conf.line_skip || str->s[0] == idx->conf.meta_char) { - last_off = bgzf_tell(fp); - continue; - } - get_intv(idx, str, &intv); - if (last_tid != intv.tid) { // change of chromosomes - last_tid = intv.tid; - last_bin = 0xffffffffu; - } else if (last_coor > intv.beg) { - fprintf(stderr, "[ti_index_core] the file out of order at line %llu\n", (unsigned long long)lineno); - exit(1); - } - insert_offset2(&idx->index2[intv.tid], intv.beg, intv.end, last_off); - if (intv.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); - save_off = last_off; - save_bin = last_bin = intv.bin; - save_tid = intv.tid; - if (save_tid < 0) break; - } - if (bgzf_tell(fp) <= last_off) { - fprintf(stderr, "[ti_index_core] bug in BGZF: %llx < %llx\n", - (unsigned long long)bgzf_tell(fp), (unsigned long long)last_off); - exit(1); - } - last_off = bgzf_tell(fp); - last_coor = intv.beg; - } - if (save_tid >= 0) insert_offset(idx->index[save_tid], save_bin, save_off, bgzf_tell(fp)); - merge_chunks(idx); - fill_missing(idx); - - free(str->s); free(str); - return idx; -} - -void ti_index_destroy(ti_index_t *idx) -{ - khint_t k; - int i; - if (idx == 0) return; - // destroy the name hash table - for (k = kh_begin(idx->tname); k != kh_end(idx->tname); ++k) { - if (kh_exist(idx->tname, k)) - free((char*)kh_key(idx->tname, k)); - } - kh_destroy(s, idx->tname); - // destroy the binning index - for (i = 0; i < idx->n; ++i) { - khash_t(i) *index = idx->index[i]; - ti_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); - // destroy the linear index - free(idx->index2); - free(idx); -} - -/****************** - * index file I/O * - ******************/ - -void ti_index_save(const ti_index_t *idx, BGZF *fp) -{ - int32_t i, size, ti_is_be; - khint_t k; - ti_is_be = bam_is_big_endian(); - bgzf_write(fp, "TBI\1", 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); - assert(sizeof(ti_conf_t) == 24); - if (ti_is_be) { // write ti_conf_t; - uint32_t x[6]; - memcpy(x, &idx->conf, 24); - for (i = 0; i < 6; ++i) bgzf_write(fp, bam_swap_endian_4p(&x[i]), 4); - } else bgzf_write(fp, &idx->conf, sizeof(ti_conf_t)); - { // write target names - char **name; - int32_t l = 0; - name = calloc(kh_size(idx->tname), sizeof(void*)); - for (k = kh_begin(idx->tname); k != kh_end(idx->tname); ++k) - if (kh_exist(idx->tname, k)) - name[kh_value(idx->tname, k)] = (char*)kh_key(idx->tname, k); - for (i = 0; i < kh_size(idx->tname); ++i) - l += strlen(name[i]) + 1; - if (ti_is_be) bgzf_write(fp, bam_swap_endian_4p(&l), 4); - else bgzf_write(fp, &l, 4); - for (i = 0; i < kh_size(idx->tname); ++i) - bgzf_write(fp, name[i], strlen(name[i]) + 1); - free(name); - } - for (i = 0; i < idx->n; ++i) { - khash_t(i) *index = idx->index[i]; - ti_lidx_t *index2 = idx->index2 + i; - // write binning index - size = kh_size(index); - if (ti_is_be) { // big endian - uint32_t x = size; - bgzf_write(fp, bam_swap_endian_4p(&x), 4); - } else bgzf_write(fp, &size, 4); - for (k = kh_begin(index); k != kh_end(index); ++k) { - if (kh_exist(index, k)) { - ti_binlist_t *p = &kh_value(index, k); - if (ti_is_be) { // big endian - uint32_t x; - x = kh_key(index, k); bgzf_write(fp, bam_swap_endian_4p(&x), 4); - x = p->n; bgzf_write(fp, bam_swap_endian_4p(&x), 4); - for (x = 0; (int)x < p->n; ++x) { - bam_swap_endian_8p(&p->list[x].u); - bam_swap_endian_8p(&p->list[x].v); - } - bgzf_write(fp, p->list, 16 * p->n); - 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 { - bgzf_write(fp, &kh_key(index, k), 4); - bgzf_write(fp, &p->n, 4); - bgzf_write(fp, p->list, 16 * p->n); - } - } - } - // 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 ti_index_t *ti_index_load_core(BGZF *fp) -{ - int i, ti_is_be; - char magic[4]; - ti_index_t *idx; - ti_is_be = bam_is_big_endian(); - if (fp == 0) { - fprintf(stderr, "[ti_index_load_core] fail to load index.\n"); - return 0; - } - bgzf_read(fp, magic, 4); - if (strncmp(magic, "TBI\1", 4)) { - fprintf(stderr, "[ti_index_load] wrong magic number.\n"); - return 0; - } - idx = (ti_index_t*)calloc(1, sizeof(ti_index_t)); - bgzf_read(fp, &idx->n, 4); - if (ti_is_be) bam_swap_endian_4p(&idx->n); - idx->tname = kh_init(s); - idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*)); - idx->index2 = (ti_lidx_t*)calloc(idx->n, sizeof(ti_lidx_t)); - // read idx->conf - bgzf_read(fp, &idx->conf, sizeof(ti_conf_t)); - if (ti_is_be) { - bam_swap_endian_4p(&idx->conf.preset); - bam_swap_endian_4p(&idx->conf.sc); - bam_swap_endian_4p(&idx->conf.bc); - bam_swap_endian_4p(&idx->conf.ec); - bam_swap_endian_4p(&idx->conf.meta_char); - bam_swap_endian_4p(&idx->conf.line_skip); - } - { // read target names - int j, ret; - kstring_t *str; - int32_t l; - uint8_t *buf; - bgzf_read(fp, &l, 4); - if (ti_is_be) bam_swap_endian_4p(&l); - buf = calloc(l, 1); - bgzf_read(fp, buf, l); - str = calloc(1, sizeof(kstring_t)); - for (i = j = 0; i < l; ++i) { - if (buf[i] == 0) { - khint_t k = kh_put(s, idx->tname, strdup(str->s), &ret); - kh_value(idx->tname, k) = j++; - str->l = 0; - } else kputc(buf[i], str); - } - free(str->s); free(str); free(buf); - } - for (i = 0; i < idx->n; ++i) { - khash_t(i) *index; - ti_lidx_t *index2 = idx->index2 + i; - uint32_t key, size; - khint_t k; - int j, ret; - ti_binlist_t *p; - index = idx->index[i] = kh_init(i); - // load binning index - bgzf_read(fp, &size, 4); - if (ti_is_be) bam_swap_endian_4p(&size); - for (j = 0; j < (int)size; ++j) { - bgzf_read(fp, &key, 4); - if (ti_is_be) bam_swap_endian_4p(&key); - k = kh_put(i, index, key, &ret); - p = &kh_value(index, k); - bgzf_read(fp, &p->n, 4); - if (ti_is_be) bam_swap_endian_4p(&p->n); - p->m = p->n; - p->list = (pair64_t*)malloc(p->m * 16); - bgzf_read(fp, p->list, 16 * p->n); - if (ti_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 - 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; -} - -ti_index_t *ti_index_load_local(const char *fnidx) -{ - BGZF *fp; - fp = bgzf_open(fnidx, "r"); - if (fp) { - ti_index_t *idx = ti_index_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(stderr, "[download_from_remote] fail to open remote file.\n"); - return; - } - if ((fp = fopen(fn, "w")) == 0) { - fprintf(stderr, "[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), ".tbi"); - 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(stderr, "[%s] downloading the index file...\n", __func__); - download_from_remote(url); - free(url); - } - if (stat(fnidx, &sbuf) == 0) return fnidx; - free(fnidx); return 0; -} - -const char **ti_seqname(const ti_index_t *idx, int *n) -{ - const char **names; - khint_t k; - *n = idx->n; - names = calloc(idx->n, sizeof(void*)); - for (k = kh_begin(idx->tname); k < kh_end(idx->tname); ++k) - if (kh_exist(idx->tname, k)) - names[kh_val(idx->tname, k)] = kh_key(idx->tname, k); - return names; -} - -ti_index_t *ti_index_load(const char *fn) -{ - ti_index_t *idx; - char *fname = get_local_version(fn); - if (fname == 0) return 0; - idx = ti_index_load_local(fname); - free(fname); - if (idx == 0) fprintf(stderr, "[ti_index_load] fail to load BAM index.\n"); - return idx; -} - -int ti_index_build2(const char *fn, const ti_conf_t *conf, const char *_fnidx) -{ - char *fnidx; - BGZF *fp, *fpidx; - ti_index_t *idx; - if ((fp = bgzf_open(fn, "r")) == 0) { - fprintf(stderr, "[ti_index_build2] fail to open the BAM file.\n"); - return -1; - } - idx = ti_index_core(fp, conf); - bgzf_close(fp); - if (_fnidx == 0) { - fnidx = (char*)calloc(strlen(fn) + 5, 1); - strcpy(fnidx, fn); strcat(fnidx, ".tbi"); - } else fnidx = strdup(_fnidx); - fpidx = bgzf_open(fnidx, "w"); - if (fpidx == 0) { - fprintf(stderr, "[ti_index_build2] fail to create the index file.\n"); - free(fnidx); - return -1; - } - ti_index_save(idx, fpidx); - ti_index_destroy(idx); - bgzf_close(fpidx); - free(fnidx); - return 0; -} - -int ti_index_build(const char *fn, const ti_conf_t *conf) -{ - return ti_index_build2(fn, conf, 0); -} - -/******************************************** - * parse a region in the format chr:beg-end * - ********************************************/ - -int ti_get_tid(const ti_index_t *idx, const char *name) -{ - khiter_t iter; - const khash_t(s) *h = idx->tname; - iter = kh_get(s, h, name); /* get the tid */ - if (iter == kh_end(h)) return -1; - return kh_value(h, iter); -} - -int ti_parse_region(const ti_index_t *idx, 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 = ti_get_tid(idx, 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 * - *******************************/ - -#define MAX_BIN 37450 // =(8^6-1)/7+1 - -static inline int reg2bins(uint32_t beg, uint32_t end, uint16_t list[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; -} - -ti_iter_t ti_iter_first() -{ - ti_iter_t iter; - iter = calloc(1, sizeof(struct __ti_iter_t)); - iter->from_first = 1; - return iter; -} - -ti_iter_t ti_iter_query(const ti_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; - ti_iter_t iter = 0; - - if (beg < 0) beg = 0; - if (end < beg) return 0; - // initialize the iterator - iter = calloc(1, sizeof(struct __ti_iter_t)); - iter->idx = idx; iter->tid = tid; iter->beg = beg; iter->end = end; iter->i = -1; - // random access - bins = (uint16_t*)calloc(MAX_BIN, 2); - n_bins = reg2bins(beg, end, bins); - index = idx->index[tid]; - if (idx->index2[tid].n > 0) { - min_off = (beg>>TAD_LIDX_SHIFT >= idx->index2[tid].n)? idx->index2[tid].offset[idx->index2[tid].n-1] - : idx->index2[tid].offset[beg>>TAD_LIDX_SHIFT]; - if (min_off == 0) { // improvement for index files built by tabix prior to 0.1.4 - int n = beg>>TAD_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; - ti_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); - { - 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 - 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; -} - -const char *ti_iter_read(BGZF *fp, ti_iter_t iter, int *len) -{ - if (iter->finished) return 0; - if (iter->from_first) { - int ret; - if ((ret = ti_readline(fp, &iter->str)) < 0) { - iter->finished = 1; - return 0; - } else { - if (len) *len = iter->str.l; - return iter->str.s; - } - } - if (iter->n_off == 0) return 0; - while (1) { - int ret; - 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) 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 - bgzf_seek(fp, iter->off[iter->i+1].u, SEEK_SET); - iter->curr_off = bgzf_tell(fp); - } - ++iter->i; - } - if ((ret = ti_readline(fp, &iter->str)) >= 0) { - ti_intv_t intv; - iter->curr_off = bgzf_tell(fp); - if (iter->str.s[0] == iter->idx->conf.meta_char) continue; - get_intv((ti_index_t*)iter->idx, &iter->str, &intv); - if (intv.tid != iter->tid || intv.beg >= iter->end) break; // no need to proceed - else if (intv.end > iter->beg && iter->end > intv.beg) { - if (len) *len = iter->str.l; - return iter->str.s; - } - } else break; // end of file - } - iter->finished = 1; - return 0; -} - -void ti_iter_destroy(ti_iter_t iter) -{ - if (iter) { - free(iter->str.s); free(iter->off); - free(iter); - } -} - -int ti_fetch(BGZF *fp, const ti_index_t *idx, int tid, int beg, int end, void *data, ti_fetch_f func) -{ - ti_iter_t iter; - const char *s; - int len; - iter = ti_iter_query(idx, tid, beg, end); - while ((s = ti_iter_read(fp, iter, &len)) != 0) - func(len, s, data); - ti_iter_destroy(iter); - return 0; -} - -/******************* - * High-level APIs * - *******************/ - -tabix_t *ti_open(const char *fn, const char *fnidx) -{ - tabix_t *t; - BGZF *fp; - if ((fp = bgzf_open(fn, "r")) == 0) return 0; - t = calloc(1, sizeof(tabix_t)); - t->fn = strdup(fn); - if (fnidx) t->fnidx = strdup(fnidx); - t->fp = fp; - return t; -} - -void ti_close(tabix_t *t) -{ - if (t) { - bgzf_close(t->fp); - if (t->idx) ti_index_destroy(t->idx); - free(t->fn); free(t->fnidx); - free(t); - } -} - -int ti_lazy_index_load(tabix_t *t) -{ - if (t->idx == 0) { // load index - if (t->fnidx) t->idx = ti_index_load_local(t->fnidx); - else t->idx = ti_index_load(t->fn); - if (t->idx == 0) return -1; // fail to load index - } - return 0; -} - -ti_iter_t ti_queryi(tabix_t *t, int tid, int beg, int end) -{ - if (tid < 0) return ti_iter_first(); - if (ti_lazy_index_load(t) != 0) return 0; - return ti_iter_query(t->idx, tid, beg, end); -} - -ti_iter_t ti_querys(tabix_t *t, const char *reg) -{ - int tid, beg, end; - if (reg == 0) return ti_iter_first(); - if (ti_lazy_index_load(t) != 0) return 0; - if (ti_parse_region(t->idx, reg, &tid, &beg, &end) < 0) return 0; - return ti_iter_query(t->idx, tid, beg, end); -} - -ti_iter_t ti_query(tabix_t *t, const char *name, int beg, int end) -{ - int tid; - if (name == 0) return ti_iter_first(); - // then need to load the index - if (ti_lazy_index_load(t) != 0) return 0; - if ((tid = ti_get_tid(t->idx, name)) < 0) return 0; - return ti_iter_query(t->idx, tid, beg, end); -} - -const char *ti_read(tabix_t *t, ti_iter_t iter, int *len) -{ - return ti_iter_read(t->fp, iter, len); -} diff --git a/tabix/index.c.pysam.c b/tabix/index.c.pysam.c new file mode 100644 index 0000000..d0965ec --- /dev/null +++ b/tabix/index.c.pysam.c @@ -0,0 +1,972 @@ +#include "pysam.h" + +#include +#include +#include +#include "khash.h" +#include "ksort.h" +#include "kstring.h" +#include "bam_endian.h" +#ifdef _USE_KNETFILE +#include "knetfile.h" +#endif +#include "tabix.h" + +#define TAD_MIN_CHUNK_GAP 32768 +// 1<<14 is the size of minimum bin. +#define TAD_LIDX_SHIFT 14 + +typedef struct { + uint64_t u, v; +} pair64_t; + +#define pair64_lt(a,b) ((a).u < (b).u) +KSORT_INIT(offt, pair64_t, pair64_lt) + +typedef struct { + uint32_t m, n; + pair64_t *list; +} ti_binlist_t; + +typedef struct { + int32_t n, m; + uint64_t *offset; +} ti_lidx_t; + +KHASH_MAP_INIT_INT(i, ti_binlist_t) +KHASH_MAP_INIT_STR(s, int) + +struct __ti_index_t { + ti_conf_t conf; + int32_t n, max; + khash_t(s) *tname; + khash_t(i) **index; + ti_lidx_t *index2; +}; + +struct __ti_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; + kstring_t str; + const ti_index_t *idx; + pair64_t *off; +}; + +typedef struct { + int tid, beg, end, bin; +} ti_intv_t; + +ti_conf_t ti_conf_gff = { 0, 1, 4, 5, '#', 0 }; +ti_conf_t ti_conf_bed = { TI_FLAG_UCSC, 1, 2, 3, '#', 0 }; +ti_conf_t ti_conf_psltbl = { TI_FLAG_UCSC, 15, 17, 18, '#', 0 }; +ti_conf_t ti_conf_sam = { TI_PRESET_SAM, 3, 4, 0, '@', 0 }; +ti_conf_t ti_conf_vcf = { TI_PRESET_VCF, 1, 2, 0, '#', 0 }; + +/*************** + * read a line * + ***************/ + +/* +int ti_readline(BGZF *fp, kstring_t *str) +{ + int c, l = 0; + str->l = 0; + while ((c = bgzf_getc(fp)) >= 0 && c != '\n') { + ++l; + if (c != '\r') kputc(c, str); + } + if (c < 0 && l == 0) return -1; // end of file + return str->l; +} +*/ + +/* Below is a faster implementation largely equivalent to the one + * commented out above. */ +int ti_readline(BGZF *fp, 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] != '\n'; ++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) { +#ifdef _USE_KNETFILE + fp->block_address = knet_tell(fp->x.fpr); +#else + fp->block_address = ftello(fp->file); +#endif + 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; +} + +/************************************* + * get the interval from a data line * + *************************************/ + +static inline int ti_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; +} + +static int get_tid(ti_index_t *idx, const char *ss) +{ + khint_t k; + int tid; + k = kh_get(s, idx->tname, ss); + if (k == kh_end(idx->tname)) { // a new target sequence + int ret, size; + // update idx->n, ->max, ->index and ->index2 + if (idx->n == idx->max) { + idx->max = idx->max? idx->max<<1 : 8; + idx->index = realloc(idx->index, idx->max * sizeof(void*)); + idx->index2 = realloc(idx->index2, idx->max * sizeof(ti_lidx_t)); + } + memset(&idx->index2[idx->n], 0, sizeof(ti_lidx_t)); + idx->index[idx->n++] = kh_init(i); + // update ->tname + tid = size = kh_size(idx->tname); + k = kh_put(s, idx->tname, strdup(ss), &ret); + kh_value(idx->tname, k) = size; + assert(idx->n == kh_size(idx->tname)); + } else tid = kh_value(idx->tname, k); + return tid; +} + +static int get_intv(ti_index_t *idx, kstring_t *str, ti_intv_t *intv) +{ + int i, b = 0, id = 1; + char *s; + intv->tid = intv->beg = intv->end = intv->bin = -1; + for (i = 0; i <= str->l; ++i) { + if (str->s[i] == '\t' || str->s[i] == 0) { + if (id == idx->conf.sc) { + str->s[i] = 0; + intv->tid = get_tid(idx, str->s + b); + if (i != str->l) str->s[i] = '\t'; + } else if (id == idx->conf.bc) { + // here ->beg is 0-based. + intv->beg = intv->end = strtol(str->s + b, &s, 0); + if (!(idx->conf.preset&TI_FLAG_UCSC)) --intv->beg; + else ++intv->end; + if (intv->beg < 0) intv->beg = 0; + if (intv->end < 1) intv->end = 1; + } else { + if ((idx->conf.preset&0xffff) == TI_PRESET_GENERIC) { + if (id == idx->conf.ec) intv->end = strtol(str->s + b, &s, 0); + } else if ((idx->conf.preset&0xffff) == TI_PRESET_SAM) { + if (id == 6) { // CIGAR + int l = 0, op; + char *t; + for (s = str->s + b; s < str->s + 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 ((idx->conf.preset&0xffff) == TI_PRESET_VCF) { + // FIXME: the following is NOT tested and is likely to be buggy + if (id == 4) { + if (b < i) intv->end = intv->beg + (i - b); + } else if (id == 8) { // look for "END=" + int c = str->s[i]; + str->s[i] = 0; + s = strstr(str->s + b, "END="); + if (s == str->s + b) s += 4; + else if (s) { + s = strstr(str->s + b, ";END="); + if (s) s += 5; + } + if (s) intv->end = strtol(s, &s, 0); + str->s[i] = c; + } + } + } + b = i + 1; + ++id; + } + } + if (intv->tid < 0 || intv->beg < 0 || intv->end < 0) return -1; + intv->bin = ti_reg2bin(intv->beg, intv->end); + return 0; +} + +/************ + * indexing * + ************/ + +// requirement: len <= LEN_MASK +static inline void insert_offset(khash_t(i) *h, int bin, uint64_t beg, uint64_t end) +{ + khint_t k; + ti_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 uint64_t insert_offset2(ti_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; + return (uint64_t)beg<<32 | end; +} + +static void merge_chunks(ti_index_t *idx) +{ + 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) { + ti_binlist_t *p; + if (!kh_exist(index, k)) continue; + p = &kh_value(index, k); + m = 0; + for (l = 1; l < p->n; ++l) { + if (p->list[m].v>>16 == p->list[l].u>>16) p->list[m].v = p->list[l].v; + else p->list[++m] = p->list[l]; + } // ~for(l) + p->n = m + 1; + } // ~for(k) + } // ~for(i) +} + +static void fill_missing(ti_index_t *idx) +{ + int i, j; + for (i = 0; i < idx->n; ++i) { + ti_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]; + } +} + +ti_index_t *ti_index_core(BGZF *fp, const ti_conf_t *conf) +{ + int ret; + ti_index_t *idx; + uint32_t last_bin, save_bin; + int32_t last_coor, last_tid, save_tid; + uint64_t save_off, last_off, lineno = 0, offset0 = (uint64_t)-1, tmp; + kstring_t *str; + + str = calloc(1, sizeof(kstring_t)); + + idx = (ti_index_t*)calloc(1, sizeof(ti_index_t)); + idx->conf = *conf; + idx->n = idx->max = 0; + idx->tname = kh_init(s); + idx->index = 0; + idx->index2 = 0; + + save_bin = save_tid = last_tid = last_bin = 0xffffffffu; + save_off = last_off = bgzf_tell(fp); last_coor = 0xffffffffu; + while ((ret = ti_readline(fp, str)) >= 0) { + ti_intv_t intv; + ++lineno; + if (lineno <= idx->conf.line_skip || str->s[0] == idx->conf.meta_char) { + last_off = bgzf_tell(fp); + continue; + } + get_intv(idx, str, &intv); + if (last_tid != intv.tid) { // change of chromosomes + if (last_tid>intv.tid ) + { + fprintf(pysamerr,"[ti_index_core] the chromosome blocks not continuous at line %llu, is the file sorted?\n",(unsigned long long)lineno); + exit(1); + } + last_tid = intv.tid; + last_bin = 0xffffffffu; + } else if (last_coor > intv.beg) { + fprintf(pysamerr, "[ti_index_core] the file out of order at line %llu\n", (unsigned long long)lineno); + exit(1); + } + tmp = insert_offset2(&idx->index2[intv.tid], intv.beg, intv.end, last_off); + if (last_off == 0) offset0 = tmp; + if (intv.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); + save_off = last_off; + save_bin = last_bin = intv.bin; + save_tid = intv.tid; + if (save_tid < 0) break; + } + if (bgzf_tell(fp) <= last_off) { + fprintf(pysamerr, "[ti_index_core] bug in BGZF: %llx < %llx\n", + (unsigned long long)bgzf_tell(fp), (unsigned long long)last_off); + exit(1); + } + last_off = bgzf_tell(fp); + last_coor = intv.beg; + } + if (save_tid >= 0) insert_offset(idx->index[save_tid], save_bin, save_off, bgzf_tell(fp)); + merge_chunks(idx); + fill_missing(idx); + if (offset0 != (uint64_t)-1 && idx->n && idx->index2[0].offset) { + int i, beg = offset0>>32, end = offset0&0xffffffffu; + for (i = beg; i <= end; ++i) idx->index2[0].offset[i] = 0; + } + + free(str->s); free(str); + return idx; +} + +void ti_index_destroy(ti_index_t *idx) +{ + khint_t k; + int i; + if (idx == 0) return; + // destroy the name hash table + for (k = kh_begin(idx->tname); k != kh_end(idx->tname); ++k) { + if (kh_exist(idx->tname, k)) + free((char*)kh_key(idx->tname, k)); + } + kh_destroy(s, idx->tname); + // destroy the binning index + for (i = 0; i < idx->n; ++i) { + khash_t(i) *index = idx->index[i]; + ti_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); + // destroy the linear index + free(idx->index2); + free(idx); +} + +/****************** + * index file I/O * + ******************/ + +void ti_index_save(const ti_index_t *idx, BGZF *fp) +{ + int32_t i, size, ti_is_be; + khint_t k; + ti_is_be = bam_is_big_endian(); + bgzf_write(fp, "TBI\1", 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); + assert(sizeof(ti_conf_t) == 24); + if (ti_is_be) { // write ti_conf_t; + uint32_t x[6]; + memcpy(x, &idx->conf, 24); + for (i = 0; i < 6; ++i) bgzf_write(fp, bam_swap_endian_4p(&x[i]), 4); + } else bgzf_write(fp, &idx->conf, sizeof(ti_conf_t)); + { // write target names + char **name; + int32_t l = 0; + name = calloc(kh_size(idx->tname), sizeof(void*)); + for (k = kh_begin(idx->tname); k != kh_end(idx->tname); ++k) + if (kh_exist(idx->tname, k)) + name[kh_value(idx->tname, k)] = (char*)kh_key(idx->tname, k); + for (i = 0; i < kh_size(idx->tname); ++i) + l += strlen(name[i]) + 1; + if (ti_is_be) bgzf_write(fp, bam_swap_endian_4p(&l), 4); + else bgzf_write(fp, &l, 4); + for (i = 0; i < kh_size(idx->tname); ++i) + bgzf_write(fp, name[i], strlen(name[i]) + 1); + free(name); + } + for (i = 0; i < idx->n; ++i) { + khash_t(i) *index = idx->index[i]; + ti_lidx_t *index2 = idx->index2 + i; + // write binning index + size = kh_size(index); + if (ti_is_be) { // big endian + uint32_t x = size; + bgzf_write(fp, bam_swap_endian_4p(&x), 4); + } else bgzf_write(fp, &size, 4); + for (k = kh_begin(index); k != kh_end(index); ++k) { + if (kh_exist(index, k)) { + ti_binlist_t *p = &kh_value(index, k); + if (ti_is_be) { // big endian + uint32_t x; + x = kh_key(index, k); bgzf_write(fp, bam_swap_endian_4p(&x), 4); + x = p->n; bgzf_write(fp, bam_swap_endian_4p(&x), 4); + for (x = 0; (int)x < p->n; ++x) { + bam_swap_endian_8p(&p->list[x].u); + bam_swap_endian_8p(&p->list[x].v); + } + bgzf_write(fp, p->list, 16 * p->n); + 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 { + bgzf_write(fp, &kh_key(index, k), 4); + bgzf_write(fp, &p->n, 4); + bgzf_write(fp, p->list, 16 * p->n); + } + } + } + // 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 ti_index_t *ti_index_load_core(BGZF *fp) +{ + int i, ti_is_be; + char magic[4]; + ti_index_t *idx; + ti_is_be = bam_is_big_endian(); + if (fp == 0) { + fprintf(pysamerr, "[ti_index_load_core] fail to load index.\n"); + return 0; + } + bgzf_read(fp, magic, 4); + if (strncmp(magic, "TBI\1", 4)) { + fprintf(pysamerr, "[ti_index_load] wrong magic number.\n"); + return 0; + } + idx = (ti_index_t*)calloc(1, sizeof(ti_index_t)); + bgzf_read(fp, &idx->n, 4); + if (ti_is_be) bam_swap_endian_4p(&idx->n); + idx->tname = kh_init(s); + idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*)); + idx->index2 = (ti_lidx_t*)calloc(idx->n, sizeof(ti_lidx_t)); + // read idx->conf + bgzf_read(fp, &idx->conf, sizeof(ti_conf_t)); + if (ti_is_be) { + bam_swap_endian_4p(&idx->conf.preset); + bam_swap_endian_4p(&idx->conf.sc); + bam_swap_endian_4p(&idx->conf.bc); + bam_swap_endian_4p(&idx->conf.ec); + bam_swap_endian_4p(&idx->conf.meta_char); + bam_swap_endian_4p(&idx->conf.line_skip); + } + { // read target names + int j, ret; + kstring_t *str; + int32_t l; + uint8_t *buf; + bgzf_read(fp, &l, 4); + if (ti_is_be) bam_swap_endian_4p(&l); + buf = calloc(l, 1); + bgzf_read(fp, buf, l); + str = calloc(1, sizeof(kstring_t)); + for (i = j = 0; i < l; ++i) { + if (buf[i] == 0) { + khint_t k = kh_put(s, idx->tname, strdup(str->s), &ret); + kh_value(idx->tname, k) = j++; + str->l = 0; + } else kputc(buf[i], str); + } + free(str->s); free(str); free(buf); + } + for (i = 0; i < idx->n; ++i) { + khash_t(i) *index; + ti_lidx_t *index2 = idx->index2 + i; + uint32_t key, size; + khint_t k; + int j, ret; + ti_binlist_t *p; + index = idx->index[i] = kh_init(i); + // load binning index + bgzf_read(fp, &size, 4); + if (ti_is_be) bam_swap_endian_4p(&size); + for (j = 0; j < (int)size; ++j) { + bgzf_read(fp, &key, 4); + if (ti_is_be) bam_swap_endian_4p(&key); + k = kh_put(i, index, key, &ret); + p = &kh_value(index, k); + bgzf_read(fp, &p->n, 4); + if (ti_is_be) bam_swap_endian_4p(&p->n); + p->m = p->n; + p->list = (pair64_t*)malloc(p->m * 16); + bgzf_read(fp, p->list, 16 * p->n); + if (ti_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 + 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; +} + +ti_index_t *ti_index_load_local(const char *fnidx) +{ + BGZF *fp; + fp = bgzf_open(fnidx, "r"); + if (fp) { + ti_index_t *idx = ti_index_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), ".tbi"); + 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; +} + +const char **ti_seqname(const ti_index_t *idx, int *n) +{ + const char **names; + khint_t k; + *n = idx->n; + names = calloc(idx->n, sizeof(void*)); + for (k = kh_begin(idx->tname); k < kh_end(idx->tname); ++k) + if (kh_exist(idx->tname, k)) + names[kh_val(idx->tname, k)] = kh_key(idx->tname, k); + return names; +} + +ti_index_t *ti_index_load(const char *fn) +{ + ti_index_t *idx; + char *fname = get_local_version(fn); + if (fname == 0) return 0; + idx = ti_index_load_local(fname); + if (idx == 0) fprintf(pysamerr, "[ti_index_load] fail to load the index: %s\n", fname); + free(fname); + return idx; +} + +int ti_index_build2(const char *fn, const ti_conf_t *conf, const char *_fnidx) +{ + char *fnidx; + BGZF *fp, *fpidx; + ti_index_t *idx; + if ((fp = bgzf_open(fn, "r")) == 0) { + fprintf(pysamerr, "[ti_index_build2] fail to open the file: %s\n", fn); + return -1; + } + idx = ti_index_core(fp, conf); + bgzf_close(fp); + if (_fnidx == 0) { + fnidx = (char*)calloc(strlen(fn) + 5, 1); + strcpy(fnidx, fn); strcat(fnidx, ".tbi"); + } else fnidx = strdup(_fnidx); + fpidx = bgzf_open(fnidx, "w"); + if (fpidx == 0) { + fprintf(pysamerr, "[ti_index_build2] fail to create the index file.\n"); + free(fnidx); + return -1; + } + ti_index_save(idx, fpidx); + ti_index_destroy(idx); + bgzf_close(fpidx); + free(fnidx); + return 0; +} + +int ti_index_build(const char *fn, const ti_conf_t *conf) +{ + return ti_index_build2(fn, conf, 0); +} + +/******************************************** + * parse a region in the format chr:beg-end * + ********************************************/ + +int ti_get_tid(const ti_index_t *idx, const char *name) +{ + khiter_t iter; + const khash_t(s) *h = idx->tname; + iter = kh_get(s, h, name); /* get the tid */ + if (iter == kh_end(h)) return -1; + return kh_value(h, iter); +} + +int ti_parse_region(const ti_index_t *idx, 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 = ti_get_tid(idx, 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 * + *******************************/ + +#define MAX_BIN 37450 // =(8^6-1)/7+1 + +static inline int reg2bins(uint32_t beg, uint32_t end, uint16_t list[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; +} + +ti_iter_t ti_iter_first() +{ + ti_iter_t iter; + iter = calloc(1, sizeof(struct __ti_iter_t)); + iter->from_first = 1; + return iter; +} + +ti_iter_t ti_iter_query(const ti_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; + ti_iter_t iter = 0; + + if (beg < 0) beg = 0; + if (end < beg) return 0; + // initialize the iterator + iter = calloc(1, sizeof(struct __ti_iter_t)); + iter->idx = idx; iter->tid = tid; iter->beg = beg; iter->end = end; iter->i = -1; + // random access + bins = (uint16_t*)calloc(MAX_BIN, 2); + n_bins = reg2bins(beg, end, bins); + index = idx->index[tid]; + if (idx->index2[tid].n > 0) { + min_off = (beg>>TAD_LIDX_SHIFT >= idx->index2[tid].n)? idx->index2[tid].offset[idx->index2[tid].n-1] + : idx->index2[tid].offset[beg>>TAD_LIDX_SHIFT]; + if (min_off == 0) { // improvement for index files built by tabix prior to 0.1.4 + int n = beg>>TAD_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; + ti_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]; + } + } + if (n_off == 0) { + free(bins); free(off); return iter; + } + free(bins); + { + int l; + ks_introsort(offt, 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; +} + +const char *ti_iter_read(BGZF *fp, ti_iter_t iter, int *len) +{ + if (iter->finished) return 0; + if (iter->from_first) { + int ret; + if ((ret = ti_readline(fp, &iter->str)) < 0) { + iter->finished = 1; + return 0; + } else { + if (len) *len = iter->str.l; + return iter->str.s; + } + } + if (iter->n_off == 0) return 0; + while (1) { + int ret; + 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) 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 + bgzf_seek(fp, iter->off[iter->i+1].u, SEEK_SET); + iter->curr_off = bgzf_tell(fp); + } + ++iter->i; + } + if ((ret = ti_readline(fp, &iter->str)) >= 0) { + ti_intv_t intv; + iter->curr_off = bgzf_tell(fp); + if (iter->str.s[0] == iter->idx->conf.meta_char) continue; + get_intv((ti_index_t*)iter->idx, &iter->str, &intv); + if (intv.tid != iter->tid || intv.beg >= iter->end) break; // no need to proceed + else if (intv.end > iter->beg && iter->end > intv.beg) { + if (len) *len = iter->str.l; + return iter->str.s; + } + } else break; // end of file + } + iter->finished = 1; + return 0; +} + +void ti_iter_destroy(ti_iter_t iter) +{ + if (iter) { + free(iter->str.s); free(iter->off); + free(iter); + } +} + +int ti_fetch(BGZF *fp, const ti_index_t *idx, int tid, int beg, int end, void *data, ti_fetch_f func) +{ + ti_iter_t iter; + const char *s; + int len; + iter = ti_iter_query(idx, tid, beg, end); + while ((s = ti_iter_read(fp, iter, &len)) != 0) + func(len, s, data); + ti_iter_destroy(iter); + return 0; +} + +/******************* + * High-level APIs * + *******************/ + +tabix_t *ti_open(const char *fn, const char *fnidx) +{ + tabix_t *t; + BGZF *fp; + if ((fp = bgzf_open(fn, "r")) == 0) return 0; + t = calloc(1, sizeof(tabix_t)); + t->fn = strdup(fn); + if (fnidx) t->fnidx = strdup(fnidx); + t->fp = fp; + return t; +} + +void ti_close(tabix_t *t) +{ + if (t) { + bgzf_close(t->fp); + if (t->idx) ti_index_destroy(t->idx); + free(t->fn); free(t->fnidx); + free(t); + } +} + +int ti_lazy_index_load(tabix_t *t) +{ + if (t->idx == 0) { // load index + if (t->fnidx) t->idx = ti_index_load_local(t->fnidx); + else t->idx = ti_index_load(t->fn); + if (t->idx == 0) return -1; // fail to load index + } + return 0; +} + +ti_iter_t ti_queryi(tabix_t *t, int tid, int beg, int end) +{ + if (tid < 0) return ti_iter_first(); + if (ti_lazy_index_load(t) != 0) return 0; + return ti_iter_query(t->idx, tid, beg, end); +} + +ti_iter_t ti_querys(tabix_t *t, const char *reg) +{ + int tid, beg, end; + if (reg == 0) return ti_iter_first(); + if (ti_lazy_index_load(t) != 0) return 0; + if (ti_parse_region(t->idx, reg, &tid, &beg, &end) < 0) return 0; + return ti_iter_query(t->idx, tid, beg, end); +} + +ti_iter_t ti_query(tabix_t *t, const char *name, int beg, int end) +{ + int tid; + if (name == 0) return ti_iter_first(); + // then need to load the index + if (ti_lazy_index_load(t) != 0) return 0; + if ((tid = ti_get_tid(t->idx, name)) < 0) return 0; + return ti_iter_query(t->idx, tid, beg, end); +} + +const char *ti_read(tabix_t *t, ti_iter_t iter, int *len) +{ + return ti_iter_read(t->fp, iter, len); +} diff --git a/tabix/knetfile.c b/tabix/knetfile.c deleted file mode 100644 index 7c96a3e..0000000 --- a/tabix/knetfile.c +++ /dev/null @@ -1,632 +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 */ - -/* 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. -lh3 */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#include -#else -#include -#include -#include -#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(stderr, "select time-out\n"); - else if (ret == SOCKET_ERROR) - fprintf(stderr, "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; - 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(stderr, "%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, stderr); - 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(stderr, "[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(stderr,"[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(stderr, "[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(stderr, "[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(stderr, "[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) { - curr = read(fp->fd, buf + l, rest); - 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(stderr,"[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(stderr, "[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 fp->offset; - } - errno = EINVAL; - fprintf(stderr,"[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); free(fp->size_cmd); // 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/tabix/knetfile.c.pysam.c b/tabix/knetfile.c.pysam.c new file mode 100644 index 0000000..88d92f5 --- /dev/null +++ b/tabix/knetfile.c.pysam.c @@ -0,0 +1,634 @@ +#include "pysam.h" + +/* 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 */ + +/* 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. -lh3 */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#else +#include +#include +#include +#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; + 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) { + curr = read(fp->fd, buf + l, rest); + 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 fp->offset; + } + 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); free(fp->size_cmd); // 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/tabix/kstring.c b/tabix/kstring.c deleted file mode 100644 index e0203fa..0000000 --- a/tabix/kstring.c +++ /dev/null @@ -1,165 +0,0 @@ -#include -#include -#include -#include -#include -#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; -} - -// 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 * - **********************/ - -// reference: http://www-igm.univ-mlv.fr/~lecroq/string/node14.html -int *ksBM_prep(const uint8_t *pat, int m) -{ - int i, *suff, *prep, *bmGs, *bmBc; - prep = calloc(m + 256, 1); - 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 = 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; -} - -int *ksBM_search(const uint8_t *str, int n, const uint8_t *pat, int m, int *_prep, int *n_matches) -{ - int i, j, *prep, *bmGs, *bmBc; - int *matches = 0, mm = 0, nm = 0; - prep = _prep? _prep : ksBM_prep(pat, m); - 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) { - if (nm == mm) { - mm = mm? mm<<1 : 1; - matches = realloc(matches, mm * sizeof(int)); - } - matches[nm++] = j; - j += bmGs[0]; - } else { - int max = bmBc[str[i+j]] - m + 1 + i; - if (max < bmGs[i]) max = bmGs[i]; - j += max; - } - } - *n_matches = nm; - if (_prep == 0) free(prep); - return matches; -} - -#ifdef KSTRING_MAIN -#include -int main() -{ - kstring_t *s; - int *fields, n, i; - 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]); - free(s); - - { - static char *str = "abcdefgcdg"; - static char *pat = "cd"; - int n, *matches; - matches = ksBM_search(str, strlen(str), pat, strlen(pat), 0, &n); - printf("%d: \n", n); - for (i = 0; i < n; ++i) - printf("- %d\n", matches[i]); - free(matches); - } - return 0; -} -#endif diff --git a/tabix/kstring.c.pysam.c b/tabix/kstring.c.pysam.c new file mode 100644 index 0000000..3114a23 --- /dev/null +++ b/tabix/kstring.c.pysam.c @@ -0,0 +1,167 @@ +#include "pysam.h" + +#include +#include +#include +#include +#include +#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; +} + +// 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 * + **********************/ + +// reference: http://www-igm.univ-mlv.fr/~lecroq/string/node14.html +int *ksBM_prep(const uint8_t *pat, int m) +{ + int i, *suff, *prep, *bmGs, *bmBc; + prep = calloc(m + 256, 1); + 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 = 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; +} + +int *ksBM_search(const uint8_t *str, int n, const uint8_t *pat, int m, int *_prep, int *n_matches) +{ + int i, j, *prep, *bmGs, *bmBc; + int *matches = 0, mm = 0, nm = 0; + prep = _prep? _prep : ksBM_prep(pat, m); + 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) { + if (nm == mm) { + mm = mm? mm<<1 : 1; + matches = realloc(matches, mm * sizeof(int)); + } + matches[nm++] = j; + j += bmGs[0]; + } else { + int max = bmBc[str[i+j]] - m + 1 + i; + if (max < bmGs[i]) max = bmGs[i]; + j += max; + } + } + *n_matches = nm; + if (_prep == 0) free(prep); + return matches; +} + +#ifdef KSTRING_MAIN +#include +int main() +{ + kstring_t *s; + int *fields, n, i; + 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]); + free(s); + + { + static char *str = "abcdefgcdg"; + static char *pat = "cd"; + int n, *matches; + matches = ksBM_search(str, strlen(str), pat, strlen(pat), 0, &n); + printf("%d: \n", n); + for (i = 0; i < n; ++i) + printf("- %d\n", matches[i]); + free(matches); + } + return 0; +} +#endif diff --git a/tabix/pysam.h b/tabix/pysam.h new file mode 100644 index 0000000..008cbbd --- /dev/null +++ b/tabix/pysam.h @@ -0,0 +1,5 @@ +#ifndef PYSAM_H +#define PYSAM_H +#include "stdio.h" +extern FILE * pysamerr; +#endif diff --git a/tests/Makefile b/tests/Makefile index 38d30f0..3482829 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,11 +1,14 @@ -all: ex1.glf ex1.pileup.gz ex1.bam.bai ex1.glfview.gz \ +all: ex1.pileup.gz ex1.bam.bai \ ex2.sam.gz ex2.sam ex1.sam \ ex2.bam \ ex3.bam ex3.bam.bai \ ex4.bam ex4.bam.bai \ ex5.bam ex5.bam.bai \ ex6.bam \ - ex8.bam + ex8.bam ex8.bam.bai \ + ex9_nofail.bam.bai \ + ex9_fail.bam.bai \ + ex10.bam ex10.bam.bai ex2.sam.gz: ex1.bam ex1.bam.bai samtools view -h ex1.bam | gzip > ex2.sam.gz @@ -24,10 +27,8 @@ ex1.bam:ex1.sam.gz ex1.fa.fai samtools index $< ex1.pileup.gz:ex1.bam ex1.fa samtools pileup -cf ex1.fa ex1.bam | gzip > ex1.pileup.gz -ex1.glf:ex1.bam ex1.fa - samtools pileup -gf ex1.fa ex1.bam > ex1.glf -ex1.glfview.gz:ex1.glf - samtools glfview ex1.glf | gzip > ex1.glfview.gz clean: - rm -fr *.bam *.bai *.glf* *.fai *.pileup* *~ calDepth *.dSYM pysam_*.sam ex2.sam ex2.sam.gz ex1.sam + rm -fr *.bam *.bai *.fai *.pileup* \ + *~ calDepth *.dSYM pysam_*.sam \ + ex2.sam ex2.sam.gz ex1.sam diff --git a/tests/ex10.sam b/tests/ex10.sam new file mode 100644 index 0000000..45f08c5 --- /dev/null +++ b/tests/ex10.sam @@ -0,0 +1,19 @@ +@HD VN:1.0 +@SQ SN:1 LN:249250621 +read1 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:i:65536 ZL:i:25 +read2 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:i:214748 ZL:i:25 +read3 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:i:2147484 ZL:i:25 +read4 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:i:2147483647 ZL:i:25 +read5 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:i:-65536 ZL:i:25 +read6 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:i:-214748 ZL:i:25 +read7 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:i:-2147484 ZL:i:25 +read8 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:i:-2147483647 ZL:i:25 +read1 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:f:65536 ZL:i:25 +read2 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:f:214748 ZL:i:25 +read3 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:f:2147484 ZL:i:25 +read5 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:f:-65536 ZL:i:25 +read6 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:f:-214748 ZL:i:25 +read7 115 1 142618765 255 25M = 142618765 25 CGACCCACTCCGCCATTTTCATCCG IIGIIIHIGIIFIIIIIIIGIGIII NM:i:0 ZP:f:-2147484 ZL:i:25 + + + diff --git a/tests/ex9_fail.bam b/tests/ex9_fail.bam new file mode 100644 index 0000000..c30a7a7 Binary files /dev/null and b/tests/ex9_fail.bam differ diff --git a/tests/ex9_nofail.bam b/tests/ex9_nofail.bam new file mode 100644 index 0000000..1f90cda Binary files /dev/null and b/tests/ex9_nofail.bam differ diff --git a/tests/pysam_test.py b/tests/pysam_test.py index aefcbb9..5444537 100755 --- a/tests/pysam_test.py +++ b/tests/pysam_test.py @@ -7,11 +7,12 @@ and data files located there. import pysam import unittest -import os, re -import itertools +import os, re, sys +import itertools, collections import subprocess import shutil - +import logging +SAMTOOLS="samtools" def checkBinaryEqual( filename1, filename2 ): '''return true if the two files are binary equal.''' @@ -50,7 +51,7 @@ def runSamtools( cmd ): def getSamtoolsVersion(): '''return samtools version''' - pipe = subprocess.Popen("samtools", shell=True, stderr=subprocess.PIPE).stderr + pipe = subprocess.Popen(SAMTOOLS, shell=True, stderr=subprocess.PIPE).stderr lines = "".join(pipe.readlines()) return re.search( "Version:\s+(\S+)", lines).groups()[0] @@ -67,42 +68,42 @@ class BinaryTest(unittest.TestCase): mCommands = \ { "faidx" : \ ( - ("ex1.fa.fai", "samtools faidx ex1.fa"), + ("ex1.fa.fai", "faidx ex1.fa"), ("pysam_ex1.fa.fai", (pysam.faidx, "ex1.fa") ), ), "import" : ( - ("ex1.bam", "samtools import ex1.fa.fai ex1.sam.gz ex1.bam" ), + ("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") ), ), "index": ( - ("ex1.bam.bai", "samtools index ex1.bam" ), + ("ex1.bam.bai", "index ex1.bam" ), ("pysam_ex1.bam.bai", (pysam.index, "pysam_ex1.bam" ) ), ), "pileup1" : ( - ("ex1.pileup", "samtools pileup -cf ex1.fa ex1.bam > ex1.pileup" ), + ("ex1.pileup", "pileup -cf ex1.fa ex1.bam > ex1.pileup" ), ("pysam_ex1.pileup", (pysam.pileup, "-c -f ex1.fa ex1.bam" ) ) ), "pileup2" : ( - ("ex1.glf", "samtools pileup -gf ex1.fa ex1.bam > ex1.glf" ), + ("ex1.glf", "pileup -gf ex1.fa ex1.bam > ex1.glf" ), ("pysam_ex1.glf", (pysam.pileup, "-g -f ex1.fa ex1.bam" ) ) ), "glfview" : ( - ("ex1.glfview", "samtools glfview ex1.glf > ex1.glfview"), + ("ex1.glfview", "glfview ex1.glf > ex1.glfview"), ("pysam_ex1.glfview", (pysam.glfview, "ex1.glf" ) ), ), "view" : ( - ("ex1.view", "samtools view ex1.bam > ex1.view"), + ("ex1.view", "view ex1.bam > ex1.view"), ("pysam_ex1.view", (pysam.view, "ex1.bam" ) ), ), "view2" : ( - ("ex1.view", "samtools view -bT ex1.fa -o ex1.view2 ex1.sam"), + ("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" ) ), ), @@ -110,7 +111,10 @@ class BinaryTest(unittest.TestCase): # some tests depend on others. The order specifies in which order # the samtools commands are executed. - mOrder = ('faidx', 'import', 'index', 'pileup1', 'pileup2', 'glfview', 'view', 'view2' ) + mOrder = ('faidx', 'import', 'index', + 'pileup1', 'pileup2', + # 'glfview', deprecated + 'view', 'view2' ) def setUp( self ): '''setup tests. @@ -127,7 +131,7 @@ class BinaryTest(unittest.TestCase): command = self.mCommands[label] samtools_target, samtools_command = command[0] pysam_target, pysam_command = command[1] - runSamtools( samtools_command ) + runSamtools( " ".join( (SAMTOOLS, samtools_command ))) pysam_method, pysam_options = pysam_command output = pysam_method( *pysam_options.split(" "), raw=True) if ">" in samtools_command: @@ -138,13 +142,17 @@ class BinaryTest(unittest.TestCase): BinaryTest.first_time = False samtools_version = getSamtoolsVersion() - if samtools_version != pysam.__samtools_version__: + + def _r( s ): + # patch - remove any of the alpha/beta suffixes, i.e., 0.1.12a -> 0.1.12 + 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.mCommands[command][0][0], self.mCommands[command][1][0] self.assertTrue( checkBinaryEqual( samtools_target, pysam_target ), @@ -162,14 +170,15 @@ class BinaryTest(unittest.TestCase): def testPileup2( self ): self.checkCommand( "pileup2" ) - def testGLFView( self ): - self.checkCommand( "glfview" ) + # deprecated + # def testGLFView( self ): + # self.checkCommand( "glfview" ) def testView( self ): self.checkCommand( "view" ) def testEmptyIndex( self ): - self.assertRaises( pysam.SamtoolsError, pysam.index, "exdoesntexist.bam" ) + self.assertRaises( IOError, pysam.index, "exdoesntexist.bam" ) def __del__(self): return @@ -292,12 +301,19 @@ class TestIteratorRow(unittest.TestCase): 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) ) + 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, pair in enumerate( 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]) ) + a,b = pair + 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) ) ) + self.assertEqual( a.qual, d[10], "line %i: quality mismatch: %s != %s, \n%s\n%s\n" % \ + (line, a.qual, d[10], + str(a), str(d) ) ) def testIteratePerContig(self): '''check random access per contig''' @@ -313,7 +329,6 @@ class TestIteratorRow(unittest.TestCase): def tearDown(self): self.samfile.close() - class TestIteratorRowAll(unittest.TestCase): def setUp(self): @@ -381,7 +396,9 @@ class TestIteratorColumn(unittest.TestCase): # 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 tearDown(self): self.samfile.close() @@ -516,7 +533,17 @@ class TestHeaderSam(unittest.TestCase): 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() @@ -599,19 +626,30 @@ class TestExceptions(unittest.TestCase): 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( OverflowError, self.samfile.fetch, "chr1", 9999999999, 99999999999 ) + self.assertRaises( OverflowError, self.samfile.count, "chr1", 9999999999, 99999999999 ) def testOutOfRangeLargeNewFormat(self): - self.assertRaises( ValueError, self.samfile.fetch, "chr1", 9999999999999999999999999999999, 9999999999999999999999999999999999999999 ) + self.assertRaises( OverflowError, self.samfile.fetch, "chr1", 9999999999999999999999999999999, 9999999999999999999999999999999999999999 ) + self.assertRaises( OverflowError, 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''' @@ -661,6 +699,14 @@ class TestFastaFile(unittest.TestCase): # unknown sequence returns "" self.assertEqual( "", 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.iteritems(): + self.assertEqual( self.file.fetch( contig, len(s), len(s)+1), "" ) + + self.assertEqual( self.file.fetch( "chr3", 0 , 100), "" ) + def testFetchErrors( self ): self.assertRaises( ValueError, self.file.fetch ) self.assertRaises( ValueError, self.file.fetch, "chr1", -1, 10 ) @@ -958,10 +1004,12 @@ class TestRemoteFileFTP(unittest.TestCase): 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 ) @@ -973,7 +1021,11 @@ class TestRemoteFileHTTP( unittest.TestCase): local = "ex1.bam" def testView( self ): - self.assertRaises( pysam.SamtoolsError, pysam.view, self.url, self.region ) + 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") @@ -995,10 +1047,237 @@ class TestRemoteFileHTTP( unittest.TestCase): 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 = i.next() + 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 = i.next() + 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 = i.next() + 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 = i.next() + 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.assert_( 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 xrange( 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.''' + + readnames = [ x.split("\t")[0] for x in open( "ex1.sam", "r" ).readlines() ] + 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 ) + +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" ) if __name__ == "__main__": # build data files