X-Git-Url: https://git.donarmstrong.com/?p=samtools.git;a=blobdiff_plain;f=bam_index.c;h=d6b94e2413e1dab2d2dc4ce2c91e471854ddd35a;hp=c65d512320289a98c8bd6c75ecbe0ddebded2bc1;hb=307c147168f7154e3755712797078c513e0b242a;hpb=80658e4d152b53bd55927c432ceece7702ab49d7 diff --git a/bam_index.c b/bam_index.c index c65d512..d6b94e2 100644 --- a/bam_index.c +++ b/bam_index.c @@ -159,9 +159,14 @@ bam_index_t *bam_index_core(bamFile fp) 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; @@ -176,19 +181,19 @@ bam_index_t *bam_index_core(bamFile fp) off_beg = off_end = bam_tell(fp); while ((ret = bam_read1(fp, b)) >= 0) { if (c->tid < 0) ++n_no_coor; - if (last_tid < c->tid) { // change of chromosomes + if (last_tid < c->tid || (last_tid >= 0 && c->tid < 0)) { // change of chromosomes last_tid = c->tid; last_bin = 0xffffffffu; - } else if (last_tid > c->tid) { + } else if ((uint32_t)last_tid > (uint32_t)c->tid) { fprintf(stderr, "[bam_index_core] the alignment is not sorted (%s): %d-th chr > %d-th chr\n", bam1_qname(b), last_tid+1, c->tid+1); return NULL; - } else if (last_coor > c->pos) { + } else if ((int32_t)c->tid >= 0 && last_coor > c->pos) { fprintf(stderr, "[bam_index_core] the alignment is not sorted (%s): %u > %u in %d-th chr\n", bam1_qname(b), last_coor, c->pos, c->tid+1); return NULL; } - if (c->tid >= 0) insert_offset2(&idx->index2[b->core.tid], b, last_off); + 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 insert_offset(idx->index[save_tid], save_bin, save_off, last_off);