]> git.donarmstrong.com Git - samtools.git/commitdiff
Merge pull request #44 from peterjc/check_bins master
authorpd3 <pd3@sanger.ac.uk>
Mon, 29 Apr 2013 11:30:05 +0000 (04:30 -0700)
committerpd3 <pd3@sanger.ac.uk>
Mon, 29 Apr 2013 11:30:05 +0000 (04:30 -0700)
Update 'samtools index' BIN verification code

bam_index.c

index 2fac746c8a6f375ff16b745793d74521ea8352d8..8c0d85414ba36475b68e9c98f79e02af7678377c 100644 (file)
@@ -170,7 +170,6 @@ bam_index_t *bam_index_core(bamFile 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));
@@ -194,10 +193,14 @@ bam_index_t *bam_index_core(bamFile fp)
                        return NULL;
                }
                if (c->tid >= 0) {
-                       recalculated_bin = bam_reg2bin(c->pos, bam_calend(c, bam1_cigar(b)));
+                       if (c->n_cigar) {
+                               recalculated_bin = bam_reg2bin(c->pos, bam_calend(c, bam1_cigar(b)));
+                       } else {
+                               recalculated_bin = bam_reg2bin(c->pos, c->pos + 1);
+                       }
                        if (c->bin != recalculated_bin) {
-                               fprintf(stderr, "[bam_index_core] read '%s' mapped at POS %d to %d has BIN %d but should be %d\n",
-                                       bam1_qname(b), c->pos + 1, bam_calend(c, bam1_cigar(b)), 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;
                        }
@@ -228,6 +231,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));