From: Charles Plessy Date: Thu, 13 Jan 2011 09:09:34 +0000 (+0900) Subject: Imported Upstream version 0.1.2 X-Git-Tag: archive/raspbian/0.22.0+ds-1+rpi1~1^2^2~469 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=d91452807d2948772dd1a59390a6e03824e6736f;p=python-pysam.git Imported Upstream version 0.1.2 --- diff --git a/PKG-INFO b/PKG-INFO index 174c1a9..765534d 100644 --- a/PKG-INFO +++ b/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 1.0 Name: pysam -Version: 0.1.1 +Version: 0.1.2 Summary: pysam Home-page: http://code.google.com/p/pysam/ Author: Andreas Heger diff --git a/pysam/__init__.py b/pysam/__init__.py index 69edfe6..3062753 100644 --- a/pysam/__init__.py +++ b/pysam/__init__.py @@ -45,6 +45,14 @@ class SamtoolsDispatcher(object): retval, stderr, stdout = csamtools._samtools_dispatch( self.dispatch, args ) if retval: raise SamtoolsError( "\n".join( stderr ) ) self.stderr = stderr + # samtools commands do not propagate the return code correctly. + # I have thus added this patch to throw if there is output on stderr. + # Note that there is sometimes output on stderr that is not an error, + # 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]" ) ] + if stderr: raise SamtoolsError( "\n".join( stderr ) ) + # call parser for stdout: if not kwargs.get("raw") and stdout and self.parsers: for options, parser in self.parsers: diff --git a/pysam/csamtools.pxd b/pysam/csamtools.pxd index 63bb314..f8361b7 100644 --- a/pysam/csamtools.pxd +++ b/pysam/csamtools.pxd @@ -113,9 +113,12 @@ cdef extern from "bam.h": bamFile razf_dopen(int data_fd, char *mode) - int64_t bam_seek( bamFile fp, uint64_t voffset, int where) + # removed - macros not found - int64_t bam_tell( bamFile fp ) + # int64_t bam_seek( bamFile fp, uint64_t voffset, int where) + # int64_t bam_tell( bamFile fp ) + # void bam_destroy1( bam1_t * b) + # void bam_init_header_hash(bam_header_t *header) bam1_t * bam_dup1( bam1_t *src ) @@ -124,8 +127,6 @@ cdef extern from "bam.h": void bam_index_destroy(bam_index_t *idx) - void bam_destroy1( bam1_t * b) - int bam_parse_region(bam_header_t *header, char *str, int *ref_id, int *begin, int *end) bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data) @@ -139,10 +140,6 @@ cdef extern from "bam.h": void bam_cleanup_fetch_iterator(bam_fetch_iterator_t *iter) - bam_fetch_iterator_t* bam_init_fetchall_iterator(bamFile fp, bam_index_t *idx) - - bam1_t * bam_fetchall_iterate(bam_fetch_iterator_t *iter) - int bam_fetch(bamFile fp, bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func) int bam_plbuf_push(bam1_t *b, bam_plbuf_t *buf) @@ -171,8 +168,7 @@ cdef extern from "bam.h": double bam_aux2d(uint8_t *s) char bam_aux2A( uint8_t *s) char *bam_aux2Z( uint8_t *s) - - void bam_init_header_hash(bam_header_t *header) + cdef extern from "sam.h": @@ -202,10 +198,6 @@ cdef extern from "pysam_util.h": uint64_t u uint64_t v - int pysam_bam_fetch_init(bamFile fp, bam_index_t *idx, int tid, int beg, int end, pair64_t ** offp ) - - int pysam_bam_fetch_is_overlap(uint32_t beg, uint32_t end, bam1_t *b) - int pysam_bam_plbuf_push(bam1_t *b, bam_plbuf_t *buf, int cont) int pysam_get_pos( bam_plbuf_t *buf) @@ -216,4 +208,5 @@ cdef extern from "pysam_util.h": int pysam_dispatch(int argc, char *argv[] ) - + # stand-in functions for samtools macros + void pysam_bam_destroy1( bam1_t * b) diff --git a/pysam/csamtools.pyx b/pysam/csamtools.pyx index a0bda89..826016f 100644 --- a/pysam/csamtools.pyx +++ b/pysam/csamtools.pyx @@ -48,7 +48,7 @@ cdef makeAlignedRead( bam1_t * src): dest = AlignedRead() # destroy dummy delegate created in constructor # to prevent memory leak. - bam_destroy1(dest._delegate) + pysam_bam_destroy1(dest._delegate) dest._delegate = bam_dup1(src) return dest @@ -711,7 +711,7 @@ cdef class IteratorRowAll: def __dealloc__(self): '''remember: dealloc cannot call other methods!''' - bam_destroy1(self.b) + pysam_bam_destroy1(self.b) cdef class IteratorColumn: '''iterates over columns. @@ -818,7 +818,7 @@ cdef class AlignedRead: def __dealloc__(self): """todo is this enough or do we need to free() each string? eg 'qual' etc""" - bam_destroy1(self._delegate) + pysam_bam_destroy1(self._delegate) def __str__(self): """todo""" diff --git a/pysam/pysam_util.c b/pysam/pysam_util.c index 8d28096..02d9db3 100644 --- a/pysam/pysam_util.c +++ b/pysam/pysam_util.c @@ -288,4 +288,10 @@ int pysam_dispatch(int argc, char *argv[] ) return 0; } +// standin for bam_destroy1 in bam.h +void pysam_bam_destroy1( bam1_t * b ) +{ + free((b)->data); + free(b); +} diff --git a/pysam/pysam_util.h b/pysam/pysam_util.h index e2a72ac..5334d70 100644 --- a/pysam/pysam_util.h +++ b/pysam/pysam_util.h @@ -37,15 +37,9 @@ int pysam_get_pos( const bam_plbuf_t *buf); int pysam_get_tid( const bam_plbuf_t *buf); bam_pileup1_t * pysam_get_pileup( const bam_plbuf_t *buf); -int pysam_bam_fetch_init(bamFile fp, - const bam_index_t *idx, - int tid, int beg, int end, - pair64_t ** offp); - -int pysam_bam_fetch_is_overlap(uint32_t beg, uint32_t end, const bam1_t *b); - int pysam_dispatch(int argc, char *argv[] ); - +// stand-in for macro - not wrappable in pyrex +void pysam_bam_destroy1( bam1_t * b ); #endif diff --git a/setup.py b/setup.py index 544e48f..57b19e7 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ pysam import os, sys, glob, shutil name = "pysam" -version = "0.1.1" +version = "0.1.2" samtools_exclude = ( "bamtk.c", "razip.c", "bgzip.c" ) samtools_dest = os.path.abspath( "samtools" ) diff --git a/tests/Makefile b/tests/Makefile index 7de46f5..5ce6e7e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,4 +1,4 @@ -all: ex1.glf ex1.pileup.gz ex1.bam.bai ex1.glfview.gz ex2.sam.gz ex2.sam ex1.sam ex3.bam ex3.bam.bai ex4.bam ex4.bam.bai +all: ex1.glf ex1.pileup.gz ex1.bam.bai ex1.glfview.gz ex2.sam.gz ex2.sam ex1.sam ex3.bam ex3.bam.bai ex4.bam ex4.bam.bai ex5.bam ex5.bam.bai @echo; echo \# You can now launch the viewer with: \'samtools tview ex1.bam ex1.fa\'; echo; ex2.sam.gz: ex1.bam ex1.bam.bai @@ -10,6 +10,9 @@ ex3.bam: ex3.sam ex1.fa.fai ex4.bam: ex4.sam ex1.fa.fai samtools import ex1.fa.fai ex4.sam ex4.bam +ex5.bam: ex5.sam ex1.fa.fai + samtools import ex1.fa.fai ex5.sam ex5.bam + %.sam: %.sam.gz gunzip < $< > $@ diff --git a/tests/pysam_test.py b/tests/pysam_test.py index 2bbe8a3..6d27cc6 100755 --- a/tests/pysam_test.py +++ b/tests/pysam_test.py @@ -101,7 +101,6 @@ class BinaryTest(unittest.TestCase): executed. Individual tests will then just compare the output files. ''' - if BinaryTest.first_time: # copy the source shutil.copy( "ex1.fa", "pysam_ex1.fa" ) @@ -144,6 +143,9 @@ class BinaryTest(unittest.TestCase): def testView( self ): self.checkCommand( "view" ) + def testEmptyIndex( self ): + self.assertRaises( pysam.SamtoolsError, pysam.index, "exdoesntexist.bam" ) + def __del__(self): for label, command in self.mCommands.iteritems(): @@ -318,7 +320,6 @@ class TestIteratorColumn(unittest.TestCase): def tearDown(self): self.samfile.close() - class TestAlignedReadBam(unittest.TestCase): @@ -472,6 +473,18 @@ class TestHeaderBam(unittest.TestCase): def tearDown(self): self.samfile.close() +class TestUnmappedReads(unittest.TestCase): + + def testSAM(self): + samfile=pysam.Samfile( "ex5.sam","r" ) + self.assertEqual( len(list(samfile.fetch( until_eof = True))), 2 ) + samfile.close() + + def testBAM(self): + samfile=pysam.Samfile( "ex5.bam","rb" ) + self.assertEqual( len(list(samfile.fetch( until_eof = True))), 2 ) + samfile.close() + class TestPileupObjects(unittest.TestCase): def setUp(self):