]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_index.c
works
[samtools.git] / bam_index.c
index 66d8eb8a896ab5a0a4decf612e65eac158e92cbf..d6b94e2413e1dab2d2dc4ce2c91e471854ddd35a 100644 (file)
@@ -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;
@@ -188,7 +193,7 @@ bam_index_t *bam_index_core(bamFile fp)
                                        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);