X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam_index.c;h=f250907039156c5559d92bfff9254ef0d8c401c8;hb=be1abba58b415b449a7a9230f2221fd3960781db;hp=9610a2656031657b796abc68bf32c1b6f1564745;hpb=cb5f44947629a68d16a48d32eef6e42a66e482fe;p=samtools.git diff --git a/bam_index.c b/bam_index.c index 9610a26..f250907 100644 --- a/bam_index.c +++ b/bam_index.c @@ -154,18 +154,22 @@ bam_index_t *bam_index_core(bamFile fp) bam_header_t *h; int i, ret; bam_index_t *idx; - uint32_t last_bin, save_bin; + uint32_t last_bin, save_bin, recalculated_bin; int32_t last_coor, last_tid, save_tid; bam1_core_t *c; uint64_t save_off, last_off, n_mapped, n_unmapped, off_beg, off_end, n_no_coor; + h = bam_header_read(fp); + if(h == 0) { + fprintf(stderr, "[bam_index_core] Invalid BAM header."); + return NULL; + } + idx = (bam_index_t*)calloc(1, sizeof(bam_index_t)); b = (bam1_t*)calloc(1, sizeof(bam1_t)); - h = bam_header_read(fp); c = &b->core; idx->n = h->n_targets; - bam_header_destroy(h); idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*)); for (i = 0; i < idx->n; ++i) idx->index[i] = kh_init(i); idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t)); @@ -188,6 +192,15 @@ bam_index_t *bam_index_core(bamFile fp) bam1_qname(b), last_coor, c->pos, c->tid+1); return NULL; } + if (c->tid >= 0) { + recalculated_bin = bam_reg2bin(c->pos, bam_calend(c, bam1_cigar(b))); + if (c->bin != recalculated_bin) { + fprintf(stderr, "[bam_index_core] read '%s' mapped to '%s' at POS %d to %d has BIN %d but should be %d\n", + bam1_qname(b), h->target_name[c->tid], c->pos + 1, bam_calend(c, bam1_cigar(b)), c->bin, recalculated_bin); + fprintf(stderr, "[bam_index_core] Fix it by using BAM->SAM->BAM to force a recalculation of the BIN field\n"); + return NULL; + } + } if (c->tid >= 0 && !(c->flag & BAM_FUNMAP)) insert_offset2(&idx->index2[b->core.tid], b, last_off); if (c->bin != last_bin) { // then possibly write the binning index if (save_bin != 0xffffffffu) // save_bin==0xffffffffu only happens to the first record @@ -214,6 +227,7 @@ bam_index_t *bam_index_core(bamFile fp) last_off = bam_tell(fp); last_coor = b->core.pos; } + bam_header_destroy(h); if (save_tid >= 0) { insert_offset(idx->index[save_tid], save_bin, save_off, bam_tell(fp)); insert_offset(idx->index[save_tid], BAM_MAX_BIN, off_beg, bam_tell(fp)); @@ -459,6 +473,7 @@ bam_index_t *bam_index_load(const char *fn) strcat(strcpy(fnidx, fn), ".bai"); fprintf(stderr, "[bam_index_load] attempting to download the remote index file.\n"); download_from_remote(fnidx); + free(fnidx); idx = bam_index_load_local(fn); } if (idx == 0) fprintf(stderr, "[bam_index_load] fail to load BAM index.\n"); @@ -489,6 +504,7 @@ int bam_index_build2(const char *fn, const char *_fnidx) if (fpidx == 0) { fprintf(stderr, "[bam_index_build2] fail to create the index file.\n"); free(fnidx); + bam_index_destroy(idx); return -1; } bam_index_save(idx, fpidx);