6 #include "bam_endian.h"
8 #include "pysam_util.h"
9 #include "errmod.h" // for pysam_dump
12 #define inline __inline
15 // Definition of pysamerr
17 FILE * pysamerr = NULL;
19 FILE * pysam_set_stderr(int fd)
23 pysamerr = fdopen(fd, "w");
27 void pysam_unset_stderr()
31 pysamerr = fopen("/dev/null", "w");
34 // #######################################################
35 // utility routines to avoid using callbacks in bam_fetch
36 // taken from bam_index.c
37 // The order of the following declarations is important.
38 // #######################################################
39 #define BAM_MAX_BIN 37450 // =(8^6-1)/7+1
47 #define pair64_lt(a,b) ((a).u < (b).u)
49 KSORT_INIT(myoff, pair64_t, pair64_lt);
62 // initialize hashes ('i' and 's' are idenditifiers)
63 KHASH_MAP_INIT_INT(i, bam_binlist_t);
64 KHASH_MAP_INIT_STR(s, int)
69 uint64_t n_no_coor; // unmapped reads without coordinate
74 typedef struct __linkbuf_t {
77 struct __linkbuf_t *next;
85 struct __bam_plbuf_t {
87 lbnode_t *head, *tail, *dummy;
90 int32_t tid, pos, max_tid, max_pos;
96 static mempool_t *mp_init()
99 mp = (mempool_t*)calloc(1, sizeof(mempool_t));
102 static void mp_destroy(mempool_t *mp)
105 for (k = 0; k < mp->n; ++k) {
106 free(mp->buf[k]->b.data);
112 static inline lbnode_t *mp_alloc(mempool_t *mp)
115 if (mp->n == 0) return (lbnode_t*)calloc(1, sizeof(lbnode_t));
116 else return mp->buf[--mp->n];
118 static inline void mp_free(mempool_t *mp, lbnode_t *p)
120 --mp->cnt; p->next = 0; // clear lbnode_t::next here
121 if (mp->n == mp->max) {
122 mp->max = mp->max? mp->max<<1 : 256;
123 mp->buf = (lbnode_t**)realloc(mp->buf, sizeof(lbnode_t*) * mp->max);
125 mp->buf[mp->n++] = p;
128 static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos)
132 bam1_core_t *c = &b->core;
133 uint32_t x = c->pos, y = 0;
134 int ret = 1, is_restart = 1;
136 if (c->flag&BAM_FUNMAP) return 0; // unmapped read
137 assert(x <= pos); // otherwise a bug
138 p->qpos = -1; p->indel = 0; p->is_del = p->is_head = p->is_tail = 0;
139 for (k = 0; k < c->n_cigar; ++k) {
140 int op = bam1_cigar(b)[k] & BAM_CIGAR_MASK; // operation
141 int l = bam1_cigar(b)[k] >> BAM_CIGAR_SHIFT; // length
142 if (op == BAM_CMATCH) { // NOTE: this assumes the first and the last operation MUST BE a match or a clip
143 if (x + l > pos) { // overlap with pos
144 p->indel = p->is_del = 0;
145 p->qpos = y + (pos - x);
146 if (x == pos && is_restart) p->is_head = 1;
147 if (x + l - 1 == pos) { // come to the end of a match
148 if (k < c->n_cigar - 1) { // there are additional operation(s)
149 uint32_t cigar = bam1_cigar(b)[k+1]; // next CIGAR
150 int op_next = cigar&BAM_CIGAR_MASK; // next CIGAR operation
151 if (op_next == BAM_CDEL) p->indel = -(int32_t)(cigar>>BAM_CIGAR_SHIFT); // del
152 else if (op_next == BAM_CINS) p->indel = cigar>>BAM_CIGAR_SHIFT; // ins
153 if (op_next == BAM_CSOFT_CLIP || op_next == BAM_CREF_SKIP || op_next == BAM_CHARD_CLIP)
154 p->is_tail = 1; // tail
155 } else p->is_tail = 1; // this is the last operation; set tail
159 } else if (op == BAM_CDEL) { // then set ->is_del
161 p->indel = 0; p->is_del = 1;
162 p->qpos = y + (pos - x);
165 } else if (op == BAM_CREF_SKIP) x += l;
166 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
167 is_restart = (op == BAM_CREF_SKIP || op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP);
169 if (op == BAM_CREF_SKIP) ret = 0; // then do not put it into pileup at all
173 assert(x > pos); // otherwise a bug
177 // the following code has been taken from bam_plbuf_push
178 // and modified such that instead of a function call
179 // the function returns and will continue (if cont is true).
180 // from where it left off.
183 // 1: if buf is full and can be emitted
184 // 0: if b has been added
185 // -1: if there was an error
186 int pysam_pileup_next(const bam1_t *b,
188 bam_pileup1_t ** plp,
193 *plp = bam_plp_next(buf->iter, tid, pos, n_plp);
194 if (plp == NULL) return 0;
198 typedef struct __bmc_aux_t {
205 // Return number of mapped reads on tid.
206 // If tid < 0, return mapped reads without a coordinate (0)
207 uint32_t pysam_get_mapped( const bam_index_t *idx, const int tid )
213 khash_t(i) *h = idx->index[tid];
214 k = kh_get(i, h, BAM_MAX_BIN);
217 return kh_val(h, k).list[1].u;
225 uint32_t pysam_get_unmapped( const bam_index_t *idx, const int tid )
231 khash_t(i) *h = idx->index[tid];
232 k = kh_get(i, h, BAM_MAX_BIN);
235 return kh_val(h, k).list[1].v;
240 return idx->n_no_coor;
243 /* uint32_t pysam_glf_depth( glf1_t * g ) */
245 /* return g->depth; */
249 /* void pysam_dump_glf( glf1_t * g, bam_maqcns_t * c ) */
252 /* fprintf(stderr, */
253 /* "glf: ref_base=%i, max_mapQ=%i, min_lk=%i, depth=%i", */
259 /* for (x = 0; x < 10; ++x) */
260 /* fprintf(stderr, ", lk%x=%i, ", x, g->lk[x]); */
262 /* fprintf(stderr, */
263 /* "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", */
274 /* for (x = 0; x < c->aux->max; ++x) */
276 /* fprintf(stderr, ", info-%i=%i ", x, c->aux->info[x]); */
277 /* if (c->aux->info[x] == 0) break; */
280 /* for (x = 0; x < c->aux->max; ++x) */
282 /* fprintf(stderr, ", info16-%i=%i ", x, c->aux->info16[x]); */
283 /* if (c->aux->info16[x] == 0) break; */
290 // pysam dispatch function to emulate the samtools
291 // command line within python.
292 // taken from the main function in bamtk.c
293 // added code to reset getopt
294 int bam_taf2baf(int argc, char *argv[]);
295 int bam_mpileup(int argc, char *argv[]);
296 int bam_merge(int argc, char *argv[]);
297 int bam_index(int argc, char *argv[]);
298 int bam_sort(int argc, char *argv[]);
299 int bam_tview_main(int argc, char *argv[]);
300 int bam_mating(int argc, char *argv[]);
301 int bam_rmdup(int argc, char *argv[]);
302 int bam_flagstat(int argc, char *argv[]);
303 int bam_fillmd(int argc, char *argv[]);
304 int bam_idxstats(int argc, char *argv[]);
305 int main_samview(int argc, char *argv[]);
306 int main_import(int argc, char *argv[]);
307 int main_reheader(int argc, char *argv[]);
308 int main_cut_target(int argc, char *argv[]);
309 int main_phase(int argc, char *argv[]);
310 int main_cat(int argc, char *argv[]);
311 int main_depth(int argc, char *argv[]);
312 int main_bam2fq(int argc, char *argv[]);
313 int faidx_main(int argc, char *argv[]);
315 int pysam_dispatch(int argc, char *argv[] )
319 setmode(fileno(stdout), O_BINARY);
320 setmode(fileno(stdin), O_BINARY);
329 if (argc < 2) return 1;
332 if (strcmp(argv[1], "view") == 0) retval = main_samview(argc-1, argv+1);
333 else if (strcmp(argv[1], "import") == 0) retval = main_import(argc-1, argv+1);
334 else if (strcmp(argv[1], "mpileup") == 0) retval = bam_mpileup(argc-1, argv+1);
335 else if (strcmp(argv[1], "merge") == 0) retval = bam_merge(argc-1, argv+1);
336 else if (strcmp(argv[1], "sort") == 0) retval = bam_sort(argc-1, argv+1);
337 else if (strcmp(argv[1], "index") == 0) retval = bam_index(argc-1, argv+1);
338 else if (strcmp(argv[1], "faidx") == 0) retval = faidx_main(argc-1, argv+1);
339 else if (strcmp(argv[1], "idxstats") == 0) retval = bam_idxstats(argc-1, argv+1);
340 else if (strcmp(argv[1], "fixmate") == 0) retval = bam_mating(argc-1, argv+1);
341 else if (strcmp(argv[1], "rmdup") == 0) retval = bam_rmdup(argc-1, argv+1);
342 else if (strcmp(argv[1], "flagstat") == 0) retval = bam_flagstat(argc-1, argv+1);
343 else if (strcmp(argv[1], "calmd") == 0) retval = bam_fillmd(argc-1, argv+1);
344 else if (strcmp(argv[1], "fillmd") == 0) retval = bam_fillmd(argc-1, argv+1);
345 else if (strcmp(argv[1], "reheader") == 0) retval = main_reheader(argc-1, argv+1);
346 else if (strcmp(argv[1], "cat") == 0) retval = main_cat(argc-1, argv+1);
347 else if (strcmp(argv[1], "targetcut") == 0) retval = main_cut_target(argc-1, argv+1);
348 else if (strcmp(argv[1], "phase") == 0) retval = main_phase(argc-1, argv+1);
349 else if (strcmp(argv[1], "depth") == 0)
351 retval = main_depth(argc-1, argv+1);
354 else if (strcmp(argv[1], "bam2fq") == 0) retval = main_bam2fq(argc-1, argv+1);
357 else if (strcmp(argv[1], "tview") == 0) retval = bam_tview_main(argc-1, argv+1);
361 fprintf(stderr, "[main] unrecognized command '%s'\n", argv[1]);
369 // taken from samtools/bam_import.c
370 static inline uint8_t *alloc_data(bam1_t *b, size_t size)
372 if (b->m_data < size)
375 kroundup32(b->m_data);
376 b->data = (uint8_t*)realloc(b->data, b->m_data);
381 // update the variable length data within a bam1_t entry.
382 // Adds *nbytes_new* - *nbytes_old* into the variable length data of *src* at *pos*.
383 // Data within the bam1_t entry is moved so that it is
384 // consistent with the data field lengths.
385 bam1_t * pysam_bam_update( bam1_t * b,
386 const size_t nbytes_old,
387 const size_t nbytes_new,
390 int d = nbytes_new-nbytes_old;
395 if (d == 0) return b;
397 new_size = d + b->data_len;
398 offset = pos - b->data;
400 //printf("d=%i, old=%i, new=%i, old_size=%i, new_size=%i\n",
401 // d, nbytes_old, nbytes_new, b->data_len, new_size);
403 // increase memory if required
406 alloc_data( b, new_size );
407 pos = b->data + offset;
410 if (b->data_len != 0)
412 if (offset < 0 || offset > b->data_len)
413 fprintf(stderr, "[pysam_bam_insert] illegal offset: '%i'\n", (int)offset);
416 // printf("dest=%p, src=%p, n=%i\n", pos+nbytes_new, pos + nbytes_old, b->data_len - (offset+nbytes_old));
417 memmove( pos + nbytes_new,
419 b->data_len - (offset + nbytes_old));
421 b->data_len = new_size;
426 // translate a nucleotide character to binary code
427 unsigned char pysam_translate_sequence( const unsigned char s )
429 return bam_nt16_table[s];
433 void bam_init_header_hash(bam_header_t *header);
435 // translate a reference string *s* to a tid
436 // code taken from bam_parse_region
437 int pysam_reference2tid( bam_header_t *header, const char * s )
443 bam_init_header_hash(header);
444 h = (khash_t(s)*)header->hash;
446 iter = kh_get(s, h, s); /* get the ref_id */
447 if (iter == kh_end(h)) { // name not found
451 return kh_value(h, iter);
454 // Auxiliary functions for B support
455 void bam_aux_appendB(bam1_t *b, const char tag[2], char type, char subtype, int len, uint8_t *data)
462 // check that type is 'B'
463 if('B' != type) return;
465 ori_len = b->data_len;
467 data_len = len * bam_aux_type2size(subtype);
468 // infer the data length from the sub-type
469 b->data_len += 8 + data_len;
471 b->l_aux += 8 + data_len;
473 if (b->m_data < b->data_len)
476 b->m_data = b->data_len;
478 kroundup32(b->m_data);
480 b->data = (uint8_t*)realloc(b->data, b->m_data);
484 b->data[ori_len] = tag[0];
485 b->data[ori_len + 1] = tag[1];
487 b->data[ori_len + 2] = type;
489 b->data[ori_len + 3] = subtype;
491 (*(int32_t*)(b->data + ori_len + 4)) = len;
493 memcpy(b->data + ori_len + 8, data, data_len);
498 // return size of auxiliary type
499 int bam_aux_type2size(int x)
501 if (x == 'C' || x == 'c' || x == 'A') return 1;
502 else if (x == 'S' || x == 's') return 2;
503 else if (x == 'I' || x == 'i' || x == 'f') return 4;