]> git.donarmstrong.com Git - samtools.git/blobdiff - bcftools/index.c
Fix memory leaks:
[samtools.git] / bcftools / index.c
index b2663d40838b5144936871b9bff22ce460e01d24..a7db24f671d6761dbc1558ec0a8af775814a2c98 100644 (file)
@@ -45,18 +45,7 @@ static inline void insert_offset2(bcf_lidx_t *index2, int _beg, int _end, uint64
        if (index2->n < end + 1) index2->n = end + 1;
 }
 
-static void fill_missing(bcf_idx_t *idx)
-{
-       int i, j;
-       for (i = 0; i < idx->n; ++i) {
-               bcf_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];
-       }
-}
-
-bcf_idx_t *bcf_idx_core(bcf_t *bp)
+bcf_idx_t *bcf_idx_core(bcf_t *bp, bcf_hdr_t *h)
 {
        bcf_idx_t *idx;
        int32_t last_coor, last_tid;
@@ -69,12 +58,12 @@ bcf_idx_t *bcf_idx_core(bcf_t *bp)
        b = calloc(1, sizeof(bcf1_t));
        str = calloc(1, sizeof(kstring_t));
        idx = (bcf_idx_t*)calloc(1, sizeof(bcf_idx_t));
-       idx->n = bp->h.n_ref;
-       idx->index2 = calloc(bp->h.n_ref, sizeof(bcf_lidx_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, b)) > 0) {
+       while ((ret = bcf_read(bp, h, b)) > 0) {
                int end, tmp;
                if (last_tid != b->tid) { // change of chromosomes
                        last_tid = b->tid;
@@ -89,7 +78,6 @@ bcf_idx_t *bcf_idx_core(bcf_t *bp)
                last_off = bgzf_tell(fp);
                last_coor = b->pos;
        }
-       fill_missing(idx);
        free(str->s); free(str); bcf_destroy(b);
        return idx;
 }
@@ -255,11 +243,13 @@ int bcf_idx_build2(const char *fn, const char *_fnidx)
        BGZF *fpidx;
        bcf_t *bp;
        bcf_idx_t *idx;
+       bcf_hdr_t *h;
        if ((bp = bcf_open(fn, "r")) == 0) {
                fprintf(stderr, "[bcf_idx_build2] fail to open the BAM file.\n");
                return -1;
        }
-       idx = bcf_idx_core(bp);
+       h = bcf_hdr_read(bp);
+       idx = bcf_idx_core(bp, h);
        bcf_close(bp);
        if (_fnidx == 0) {
                fnidx = (char*)calloc(strlen(fn) + 5, 1);
@@ -269,6 +259,7 @@ int bcf_idx_build2(const char *fn, const char *_fnidx)
        if (fpidx == 0) {
                fprintf(stderr, "[bcf_idx_build2] fail to create the index file.\n");
                free(fnidx);
+        bcf_idx_destroy(idx);
                return -1;
        }
        bcf_idx_save(idx, fpidx);
@@ -323,14 +314,14 @@ int bcf_parse_region(void *str2id, const char *str, int *tid, int *begin, int *e
  * retrieve a specified region *
  *******************************/
 
-uint64_t bcf_idx_query(const bcf_idx_t *idx, int tid, int beg, int end)
+uint64_t bcf_idx_query(const bcf_idx_t *idx, int tid, int beg)
 {
-       uint64_t min_off;
+       uint64_t min_off, *offset;
+       int i;
        if (beg < 0) beg = 0;
-       if (end < beg) return 0xffffffffffffffffull;
-       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];
-       assert(min_off);
+       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;
 }