]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_index.c
* Added Brent's method for frequency estimate
[samtools.git] / bam_index.c
index 0638a2e826a1ceb10f86a144ca42c78355bcae35..51c27014da0f9f010b563db92112f71d7ad6bdd6 100644 (file)
@@ -212,13 +212,20 @@ bam_index_t *bam_index_core(bamFile fp)
        }
        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, off_end);
+               insert_offset(idx->index[save_tid], BAM_MAX_BIN, off_beg, bam_tell(fp));
                insert_offset(idx->index[save_tid], BAM_MAX_BIN, n_mapped, n_unmapped);
        }
        merge_chunks(idx);
        fill_missing(idx);
-       if (ret >= 0)
-               while ((ret = bam_read1(fp, b)) >= 0) ++n_no_coor;
+       if (ret >= 0) {
+               while ((ret = bam_read1(fp, b)) >= 0) {
+                       ++n_no_coor;
+                       if (c->tid >= 0 && n_no_coor) {
+                               fprintf(stderr, "[bam_index_core] the alignment is not sorted: reads without coordinates prior to reads with coordinates.\n");
+                               exit(1);
+                       }
+               }
+       }
        if (ret < -1) fprintf(stderr, "[bam_index_core] truncated file? Continue anyway. (%d)\n", ret);
        free(b->data); free(b);
        idx->n_no_coor = n_no_coor;