1 cdef extern from "string.h":
3 void *memcpy(void *dst,void *src,size_t len)
4 void *memmove(void *dst,void *src,size_t len)
5 void *memset(void *b,int c,size_t len)
7 cdef extern from "stdlib.h":
10 void *calloc(size_t,size_t)
11 void *realloc(void *,size_t)
13 void qsort(void *base, size_t nmemb, size_t size,
14 int (*compar)(void *,void *))
16 cdef extern from "math.h":
19 cdef extern from "stdio.h":
22 FILE *fopen(char *,char *)
23 FILE *freopen(char *path, char *mode, FILE *stream)
24 int fileno(FILE *stream)
25 int dup2(int oldfd, int newfd)
26 int fflush(FILE *stream)
31 int sscanf(char *str,char *fmt,...)
32 int printf(char *fmt,...)
33 int sprintf(char *str,char *fmt,...)
34 int fprintf(FILE *ifile,char *fmt,...)
35 char *fgets(char *str,int size,FILE *ifile)
37 cdef extern from "ctype.h":
41 cdef extern from "unistd.h":
45 cdef extern from "string.h":
46 int strcmp(char *s1, char *s2)
47 int strncmp(char *s1,char *s2,size_t len)
48 char *strcpy(char *dest,char *src)
49 char *strncpy(char *dest,char *src, size_t len)
51 char *strcat(char *,char *)
52 size_t strlen(char *s)
53 int memcmp( void * s1, void *s2, size_t len )
55 cdef extern from "Python.h":
56 long _Py_HashPointer(void*)
57 FILE* PyFile_AsFile(object)
59 cdef extern from "razf.h":
62 cdef extern from "stdint.h":
72 cdef extern from "bam.h":
76 # IF _IOLIB=2, bamFile = BGZF, see bgzf.h
77 # samtools uses KNETFILE, check how this works
79 ctypedef struct tamFile:
82 ctypedef struct bamFile:
85 ctypedef struct bam1_core_t:
98 ctypedef struct bam1_t:
105 ctypedef struct bam_pileup1_t:
114 ctypedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, bam_pileup1_t *pl, void *data)
116 ctypedef int (*bam_fetch_f)(bam1_t *b, void *data)
118 ctypedef struct bam_header_t:
127 ctypedef struct bam_index_t:
131 ctypedef struct bam_plbuf_t:
134 ctypedef struct pair64_t:
137 ctypedef struct bam_iter_t:
139 int tid, beg, end, n_off, i, finished
143 # ctypedef __bam_iter_t * bam_iter_t
146 void bam_destroy1(bam1_t *)
148 bamFile razf_dopen(int data_fd, char *mode)
150 int64_t bam_seek( bamFile fp, uint64_t voffset, int where)
151 int64_t bam_tell( bamFile fp )
153 # void bam_init_header_hash(bam_header_t *header)
155 ###############################################
156 # stand-ins for samtools macros
157 uint32_t * bam1_cigar( bam1_t * b)
158 char * bam1_qname( bam1_t * b)
159 uint8_t * bam1_seq( bam1_t * b)
160 uint8_t * bam1_qual( bam1_t * b)
161 uint8_t * bam1_aux( bam1_t * b)
163 ###############################################
164 # bam iterator interface
165 bam_iter_t bam_iter_query( bam_index_t *idx, int tid, int beg, int end)
167 int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b)
169 void bam_iter_destroy(bam_iter_t iter)
171 ###############################################
173 bam1_t * bam_dup1( bam1_t *src )
175 bam1_t * bam_copy1(bam1_t *bdst, bam1_t *bsrc)
176 bam_index_t *bam_index_load(char *f )
178 void bam_index_destroy(bam_index_t *idx)
180 int bam_parse_region(bam_header_t *header, char *str, int *ref_id, int *begin, int *end)
182 ###############################################
183 bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data)
185 int bam_fetch(bamFile fp, bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func)
187 int bam_plbuf_push(bam1_t *b, bam_plbuf_t *buf)
189 void bam_plbuf_destroy(bam_plbuf_t *buf)
190 ########################################
191 # pileup iterator interface
192 ctypedef struct bam_plp_t:
195 ctypedef bam_pileup1_t * const_bam_pileup1_t_ptr "const bam_pileup1_t *"
197 ctypedef int (*bam_plp_auto_f)(void *data, bam1_t *b)
199 bam_plp_t bam_plp_init( bam_plp_auto_f func, void *data)
200 int bam_plp_push( bam_plp_t iter, bam1_t *b)
201 bam_pileup1_t * bam_plp_next( bam_plp_t iter, int *_tid, int *_pos, int *_n_plp)
202 bam_pileup1_t * bam_plp_auto( bam_plp_t iter, int *_tid, int *_pos, int *_n_plp)
203 void bam_plp_set_mask(bam_plp_t iter, int mask)
204 void bam_plp_reset(bam_plp_t iter)
205 void bam_plp_destroy(bam_plp_t iter)
206 void bam_plp_set_maxcnt(bam_plp_t iter, int maxcnt)
208 ##################################################
210 int bam_read1( bamFile fp, bam1_t *b)
211 int bam_validate1( bam_header_t *header, bam1_t *b)
212 int bam_write1( bamFile fp, bam1_t *b)
214 bam_header_t *bam_header_init()
216 int bam_header_write( bamFile fp, bam_header_t *header)
218 bam_header_t *bam_header_read( bamFile fp )
220 void bam_header_destroy(bam_header_t *header)
222 bam1_t * bam_dup1( bam1_t *src )
224 bam1_t * bam_copy1(bam1_t *bdst, bam1_t *bsrc)
226 uint8_t *bam_aux_get(bam1_t *b, char tag[2])
228 int32_t bam_aux2i(uint8_t *s)
229 float bam_aux2f(uint8_t *s)
230 double bam_aux2d(uint8_t *s)
231 char bam_aux2A( uint8_t *s)
232 char *bam_aux2Z( uint8_t *s)
234 int bam_reg2bin(uint32_t beg, uint32_t end)
236 uint32_t bam_calend(bam1_core_t *c, uint32_t *cigar)
238 int bam_aux_type2size( int )
242 ctypedef char* const_char_ptr "const char*"
244 cdef extern from "sam.h":
246 ctypedef struct samfile_t_un:
251 ctypedef struct samfile_t:
256 samfile_t *samopen( const_char_ptr fn, char * mode, void *aux)
258 int sampileup( samfile_t *fp, int mask, bam_pileup_f func, void *data)
260 void samclose(samfile_t *fp)
262 int samread(samfile_t *fp, bam1_t *b)
264 int samwrite(samfile_t *fp, bam1_t *b)
266 int bam_prob_realn(bam1_t *b, char *ref)
267 int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
270 #cdef extern from "glf.h":
271 # ctypedef struct glf1_t:
274 #cdef extern from "bam_maqcns.h":
276 # ctypedef struct bam_maqcns_t:
277 # float het_rate, theta
278 # int n_hap, cap_mapQ, errmod, min_baseQ
284 # glf1_t *bam_maqcns_glfgen(int n,
289 # ctypedef struct bam_maqindel_opt_t:
293 # int mm_penalty, indel_err, ambi_thres
295 # uint32_t bam_maqcns_call(int n, bam_pileup1_t *pl, bam_maqcns_t *bm)
296 # bam_maqcns_t * bam_maqcns_init()
297 # void bam_maqcns_destroy(bam_maqcns_t *bm)
298 # void bam_maqcns_prepare(bam_maqcns_t *bm)
300 # uint32_t glf2cns(glf1_t *g, int q_r)
302 # int BAM_ERRMOD_MAQ2
304 # int BAM_ERRMOD_SOAP
306 # ctypedef struct bam_maqindel_ret_t:
320 # void bam_maqindel_ret_destroy( bam_maqindel_ret_t * )
322 # bam_maqindel_opt_t *bam_maqindel_opt_init()
324 # bam_maqindel_ret_t * bam_maqindel(int n,
326 # bam_maqindel_opt_t * mi,
327 # bam_pileup1_t * pl,
333 cdef extern from "faidx.h":
335 ctypedef struct faidx_t:
338 int fai_build(char *fn)
340 void fai_destroy(faidx_t *fai)
342 faidx_t *fai_load(char *fn)
344 char *fai_fetch(faidx_t *fai, char *reg, int *len)
346 int faidx_fetch_nseq(faidx_t *fai)
348 char *faidx_fetch_seq(faidx_t *fai, char *c_name,
349 int p_beg_i, int p_end_i, int *len)
352 cdef extern from "pysam_util.h":
354 int pysam_pileup_next(bam1_t *b,
356 bam_pileup1_t ** plp,
362 int pysam_dispatch(int argc, char *argv[] )
364 # stand-in functions for samtools macros
365 void pysam_bam_destroy1( bam1_t * b)
367 # add *nbytes* into the variable length data of *src* at *pos*
368 bam1_t * pysam_bam_update( bam1_t * b,
373 # translate char to unsigned char
374 unsigned char pysam_translate_sequence( char s )
376 unsigned char * bam_nt16_table
378 int pysam_reference2tid( bam_header_t *header, char * s )
380 void pysam_set_stderr( int fd )
381 void pysam_unset_stderr()
383 # return mapped/unmapped reads on tid
384 uint32_t pysam_get_mapped( bam_index_t *idx, int tid )
385 uint32_t pysam_get_unmapped( bam_index_t *idx, int tid )
387 # uint32_t pysam_glf_depth( glf1_t * g )
389 # void pysam_dump_glf( glf1_t * g, bam_maqcns_t * c )
391 ####################################################################
394 ctypedef struct __iterdata:
402 ####################################################################
404 # Exposing pysam extension classes
406 # Note: need to declare all C fields and methods here
408 cdef class Fastafile:
409 cdef char * _filename
410 # pointer to fastafile
411 cdef faidx_t * fastafile
413 cdef char * _fetch( self, char * reference, int start, int end, int * length )
415 cdef class AlignedRead:
417 # object that this AlignedRead represents
418 cdef bam1_t * _delegate
421 cdef char * _filename
423 cdef samfile_t * samfile
425 cdef bam_index_t *index
426 # true if file is a bam file
428 # true if not a file but a stream
430 # true if file is not on the local filesystem
432 # current read within iteration
437 # beginning of read section
438 cdef int64_t start_offset
440 cdef bam_header_t * _buildHeader( self, new_header )
441 cdef bam1_t * getCurrent( self )
444 # write an aligned read
445 cpdef int write( self, AlignedRead read )
447 cdef char * _getrname( self, int tid )
449 cdef class PileupProxy:
450 cdef bam_pileup1_t ** plp
455 cdef class PileupRead:
456 cdef AlignedRead _alignment
460 cdef uint32_t _is_del
461 cdef uint32_t _is_head
462 cdef uint32_t _is_tail
464 cdef class IteratorRow:
467 cdef class IteratorRowRegion(IteratorRow):
468 cdef bam_iter_t iter # iterator state object
473 # true if samfile belongs to this object
474 cdef int owns_samfile
476 cdef bam1_t * getCurrent( self )
480 cdef class IteratorRowAll(IteratorRow):
483 cdef int owns_samfile
484 cdef bam1_t * getCurrent( self )
487 cdef class IteratorRowAllRefs(IteratorRow):
490 cdef IteratorRowRegion rowiter
492 cdef class IteratorRowSelection(IteratorRow):
497 # true if samfile belongs to this object
498 cdef int owns_samfile
500 cdef bam1_t * getCurrent( self )
504 cdef class IteratorColumn:
506 # result of the last plbuf_push
507 cdef IteratorRowRegion iter
512 cdef const_bam_pileup1_t_ptr plp
513 cdef bam_plp_t pileup_iter
514 cdef __iterdata iterdata
516 cdef Fastafile fastafile
521 cdef char * getSequence( self )
522 cdef setMask( self, mask )
523 cdef setupIteratorData( self,
529 cdef reset( self, tid, start, end )
531 cdef class IteratorColumnRegion(IteratorColumn):
536 cdef class IteratorColumnAllRefs(IteratorColumn):
539 cdef class IndexedReads:
543 # true if samfile belongs to this object
544 cdef int owns_samfile