Imported Upstream version 0.7
[pysam.git] / pysam / csamtools.pxd
1 cdef extern from "string.h":
2   ctypedef int size_t
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)
6
7 cdef extern from "stdlib.h":
8   void free(void *)
9   void *malloc(size_t)
10   void *calloc(size_t,size_t)
11   void *realloc(void *,size_t)
12   int c_abs "abs" (int)
13   void qsort(void *base, size_t nmemb, size_t size,
14              int (*compar)(void *,void *))
15
16 cdef extern from "math.h":
17    double sqrt(double x)
18
19 cdef extern from "stdio.h":
20   ctypedef struct FILE:
21     pass
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)
27
28   FILE * stderr
29   FILE * stdout
30   int fclose(FILE *)
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)
36
37 cdef extern from "ctype.h":
38   int toupper(int c)
39   int tolower(int c)
40   
41 cdef extern from "unistd.h":
42   char *ttyname(int fd)
43   int isatty(int fd)  
44
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)
50   char *strdup(char *)
51   char *strcat(char *,char *)
52   size_t strlen(char *s)
53   int memcmp( void * s1, void *s2, size_t len )
54
55 cdef extern from "Python.h":
56    long _Py_HashPointer(void*)
57    FILE* PyFile_AsFile(object)
58
59 cdef extern from "razf.h":
60   pass
61
62 cdef extern from "stdint.h":
63   ctypedef int int8_t
64   ctypedef int int16_t
65   ctypedef int int32_t
66   ctypedef int int64_t
67   ctypedef int uint8_t
68   ctypedef int uint16_t
69   ctypedef int uint32_t
70   ctypedef int uint64_t
71
72 cdef extern from "bam.h":
73
74   # constants
75   int BAM_DEF_MASK
76   # IF _IOLIB=2, bamFile = BGZF, see bgzf.h
77   # samtools uses KNETFILE, check how this works
78
79   ctypedef struct tamFile:
80       pass
81
82   ctypedef struct bamFile:
83       pass
84
85   ctypedef struct bam1_core_t:
86       int32_t tid 
87       int32_t pos
88       uint32_t bin
89       uint32_t qual
90       uint32_t l_qname
91       uint32_t flag
92       uint32_t n_cigar
93       int32_t l_qseq
94       int32_t mtid 
95       int32_t mpos 
96       int32_t isize
97
98   ctypedef struct bam1_t:
99     bam1_core_t core
100     int l_aux
101     int data_len
102     int m_data
103     uint8_t *data
104
105   ctypedef struct bam_pileup1_t:
106       bam1_t *b 
107       int32_t qpos 
108       int indel
109       int level
110       uint32_t is_del
111       uint32_t is_head
112       uint32_t is_tail
113
114   ctypedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, bam_pileup1_t *pl, void *data)
115
116   ctypedef int (*bam_fetch_f)(bam1_t *b, void *data)
117
118   ctypedef struct bam_header_t:
119      int32_t n_targets
120      char **target_name
121      uint32_t *target_len
122      void *hash
123      void *rg2lib
124      int l_text
125      char *text
126
127   ctypedef struct bam_index_t:
128       int32_t n
129       uint64_t n_no_coor
130
131   ctypedef struct bam_plbuf_t:
132       pass
133
134   ctypedef struct pair64_t:
135       uint64_t u, v
136       
137   ctypedef struct bam_iter_t:
138       int from_first
139       int tid, beg, end, n_off, i, finished
140       uint64_t curr_off
141       pair64_t *off
142
143   # ctypedef __bam_iter_t * bam_iter_t
144
145   bam1_t * bam_init1()
146   void bam_destroy1(bam1_t *)
147
148   bamFile razf_dopen(int data_fd, char *mode)
149
150   int64_t bam_seek( bamFile fp, uint64_t voffset, int where)
151   int64_t bam_tell( bamFile fp )
152
153   # void bam_init_header_hash(bam_header_t *header)
154
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)
162
163   ###############################################
164   # bam iterator interface
165   bam_iter_t bam_iter_query( bam_index_t *idx, int tid, int beg, int end)
166
167   int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b)
168
169   void bam_iter_destroy(bam_iter_t iter)
170
171   ###############################################
172
173   bam1_t * bam_dup1( bam1_t *src ) 
174   
175   bam1_t * bam_copy1(bam1_t *bdst, bam1_t *bsrc)
176   bam_index_t *bam_index_load(char *f )
177
178   void bam_index_destroy(bam_index_t *idx)
179
180   int bam_parse_region(bam_header_t *header, char *str, int *ref_id, int *begin, int *end)
181
182   ###############################################
183   bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data)
184
185   int bam_fetch(bamFile fp, bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func)
186
187   int bam_plbuf_push(bam1_t *b, bam_plbuf_t *buf)
188
189   void bam_plbuf_destroy(bam_plbuf_t *buf)
190   ########################################
191   # pileup iterator interface
192   ctypedef struct bam_plp_t:
193       pass
194
195   ctypedef bam_pileup1_t * const_bam_pileup1_t_ptr "const bam_pileup1_t *"
196
197   ctypedef int (*bam_plp_auto_f)(void *data, bam1_t *b)
198
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)
207
208   ##################################################
209
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)
213
214   bam_header_t *bam_header_init()
215
216   int bam_header_write( bamFile fp, bam_header_t *header)
217
218   bam_header_t *bam_header_read( bamFile fp )
219
220   void bam_header_destroy(bam_header_t *header)
221
222   bam1_t * bam_dup1( bam1_t *src ) 
223   
224   bam1_t * bam_copy1(bam1_t *bdst, bam1_t *bsrc)
225
226   uint8_t *bam_aux_get(bam1_t *b,  char tag[2])
227
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)
233   
234   int bam_reg2bin(uint32_t beg, uint32_t end)
235
236   uint32_t bam_calend(bam1_core_t *c, uint32_t *cigar)
237
238   int bam_aux_type2size( int )
239     
240
241 cdef extern from *:
242     ctypedef char* const_char_ptr "const char*"
243
244 cdef extern from "sam.h":
245
246   ctypedef struct samfile_t_un:
247     tamFile tamr
248     bamFile bam
249     FILE *tamw
250     
251   ctypedef struct samfile_t:
252      int type
253      samfile_t_un x
254      bam_header_t *header
255
256   samfile_t *samopen( const_char_ptr fn, char * mode, void *aux)
257
258   int sampileup( samfile_t *fp, int mask, bam_pileup_f func, void *data)
259
260   void samclose(samfile_t *fp)
261
262   int samread(samfile_t *fp, bam1_t *b)
263
264   int samwrite(samfile_t *fp, bam1_t *b)
265
266   int bam_prob_realn(bam1_t *b, char *ref)
267   int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
268
269
270 #cdef extern from "glf.h":
271 #   ctypedef struct glf1_t:
272 #      pass
273
274 #cdef extern from "bam_maqcns.h":
275 #
276 #  ctypedef struct bam_maqcns_t:
277 #     float het_rate, theta
278 #     int n_hap, cap_mapQ, errmod, min_baseQ
279 #     float eta, q_r
280 #     double *fk, *coef
281 #     double *lhet
282 #     void *aux
283
284 #  glf1_t *bam_maqcns_glfgen(int n, 
285 #                            bam_pileup1_t *pl, 
286 #                            uint8_t ref_base, 
287 #                            bam_maqcns_t *bm)
288
289 #  ctypedef struct bam_maqindel_opt_t:
290 #      int q_indel
291 #      float r_indel
292 #      float r_snp
293 #      int mm_penalty, indel_err, ambi_thres
294      
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)
299   
300 #  uint32_t glf2cns(glf1_t *g, int q_r)
301
302 #  int BAM_ERRMOD_MAQ2
303 #  int BAM_ERRMOD_MAQ
304 #  int BAM_ERRMOD_SOAP
305
306 #  ctypedef struct bam_maqindel_ret_t: 
307 #    int indel1
308 #    int indel2        
309 #    int cnt1
310 #    int cnt2
311 #    int cnt_anti
312 #    int cnt_ref
313 #    int cnt_ambi
314 #    char *s[2]
315 #    int gt
316 #    int gl[2]
317 #    int q_cns
318 #    int q_ref
319     
320 #  void bam_maqindel_ret_destroy( bam_maqindel_ret_t * )
321
322 #  bam_maqindel_opt_t *bam_maqindel_opt_init()
323
324 #  bam_maqindel_ret_t * bam_maqindel(int n, 
325 #                    int pos, 
326 #                    bam_maqindel_opt_t * mi, 
327 #                    bam_pileup1_t * pl, 
328 #                    char *ref,
329 #                    int _n_types, 
330 #                    int * _types )
331                                                                
332
333 cdef extern from "faidx.h":
334
335    ctypedef struct faidx_t:
336       pass
337
338    int fai_build(char *fn)
339
340    void fai_destroy(faidx_t *fai)
341
342    faidx_t *fai_load(char *fn)
343
344    char *fai_fetch(faidx_t *fai, char *reg, int *len)
345
346    int faidx_fetch_nseq(faidx_t *fai)
347
348    char *faidx_fetch_seq(faidx_t *fai, char *c_name, 
349                          int p_beg_i, int p_end_i, int *len)
350
351
352 cdef extern from "pysam_util.h":
353
354     int pysam_pileup_next(bam1_t *b, 
355                           bam_plbuf_t *buf, 
356                           bam_pileup1_t ** plp,
357                           int * tid,
358                           int * pos,
359                           int * n_plp )
360
361
362     int pysam_dispatch(int argc, char *argv[] )
363
364     # stand-in functions for samtools macros
365     void pysam_bam_destroy1( bam1_t * b) 
366
367     # add *nbytes* into the variable length data of *src* at *pos*
368     bam1_t * pysam_bam_update( bam1_t * b, 
369                                size_t nbytes_old,
370                                size_t nbytes_new,
371                                uint8_t * pos )
372
373     # translate char to unsigned char
374     unsigned char pysam_translate_sequence( char s )
375
376     unsigned char * bam_nt16_table
377
378     int pysam_reference2tid( bam_header_t *header, char * s )
379
380     void pysam_set_stderr( int fd )
381     void pysam_unset_stderr()
382
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 )
386
387 #    uint32_t pysam_glf_depth( glf1_t * g )
388
389 #    void pysam_dump_glf( glf1_t * g, bam_maqcns_t * c )
390
391 ####################################################################
392 # Utility types
393
394 ctypedef struct __iterdata:
395     samfile_t * samfile
396     bam_iter_t iter
397     faidx_t * fastafile
398     int tid
399     char * seq
400     int seq_len
401
402 ####################################################################
403 #
404 # Exposing pysam extension classes
405 #
406 # Note: need to declare all C fields and methods here
407 #
408 cdef class Fastafile:
409     cdef char * _filename
410     # pointer to fastafile
411     cdef faidx_t * fastafile
412
413     cdef char * _fetch( self, char * reference, int start, int end, int * length )
414
415 cdef class AlignedRead:
416
417     # object that this AlignedRead represents
418     cdef bam1_t * _delegate
419
420 cdef class Samfile:
421     cdef char * _filename
422     # pointer to samfile
423     cdef samfile_t * samfile
424     # pointer to index
425     cdef bam_index_t *index
426     # true if file is a bam file
427     cdef int isbam
428     # true if not a file but a stream
429     cdef int isstream
430     # true if file is not on the local filesystem
431     cdef int isremote
432     # current read within iteration
433     cdef bam1_t * b
434     # file opening mode
435     cdef char * mode
436
437     # beginning of read section
438     cdef int64_t start_offset 
439
440     cdef bam_header_t * _buildHeader( self, new_header )
441     cdef bam1_t * getCurrent( self )
442     cdef int cnext(self)
443
444     # write an aligned read
445     cpdef int write( self, AlignedRead read )
446
447     cdef char * _getrname( self, int tid )
448
449 cdef class PileupProxy:
450     cdef bam_pileup1_t ** plp
451     cdef int tid
452     cdef int pos
453     cdef int n_pu
454
455 cdef class PileupRead:
456     cdef AlignedRead _alignment
457     cdef int32_t  _qpos
458     cdef int _indel
459     cdef int _level
460     cdef uint32_t _is_del
461     cdef uint32_t _is_head
462     cdef uint32_t _is_tail
463
464 cdef class IteratorRow:
465     pass
466
467 cdef class IteratorRowRegion(IteratorRow):
468     cdef bam_iter_t             iter # iterator state object
469     cdef bam1_t *               b
470     cdef int                    retval
471     cdef Samfile                samfile
472     cdef samfile_t              * fp
473     # true if samfile belongs to this object
474     cdef int owns_samfile
475
476     cdef bam1_t * getCurrent( self )
477
478     cdef int cnext(self)
479
480 cdef class IteratorRowAll(IteratorRow):
481     cdef bam1_t * b
482     cdef samfile_t * fp
483     cdef int owns_samfile
484     cdef bam1_t * getCurrent( self )
485     cdef int cnext(self)
486
487 cdef class IteratorRowAllRefs(IteratorRow):
488     cdef Samfile     samfile
489     cdef int         tid
490     cdef IteratorRowRegion rowiter
491
492 cdef class IteratorRowSelection(IteratorRow):
493     cdef bam1_t * b
494     cdef int current_pos
495     cdef samfile_t * fp
496     cdef positions
497     # true if samfile belongs to this object
498     cdef int owns_samfile
499
500     cdef bam1_t * getCurrent( self )
501
502     cdef int cnext(self)
503
504 cdef class IteratorColumn:
505
506     # result of the last plbuf_push
507     cdef IteratorRowRegion iter
508     cdef int tid
509     cdef int pos
510     cdef int n_plp
511     cdef int mask
512     cdef const_bam_pileup1_t_ptr plp
513     cdef bam_plp_t pileup_iter
514     cdef __iterdata iterdata
515     cdef Samfile samfile
516     cdef Fastafile fastafile
517     cdef stepper
518     cdef int max_depth
519
520     cdef int cnext(self)
521     cdef char * getSequence( self )
522     cdef setMask( self, mask )
523     cdef setupIteratorData( self,
524                             int tid,
525                             int start,
526                             int end,
527                             int reopen = ? )
528
529     cdef reset( self, tid, start, end )
530
531 cdef class IteratorColumnRegion(IteratorColumn):
532     cdef int start
533     cdef int end
534     cdef int truncate
535
536 cdef class IteratorColumnAllRefs(IteratorColumn):
537     pass
538
539 cdef class IndexedReads:
540     cdef Samfile samfile
541     cdef samfile_t * fp
542     cdef index
543     # true if samfile belongs to this object
544     cdef int owns_samfile
545
546