X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=bam_index.c;h=8dc7ce38171fcc830b22f3e642a13a165ee2f4a7;hb=5d2c0d15df64246aa55b086fa85f9c68ae27e402;hp=51c27014da0f9f010b563db92112f71d7ad6bdd6;hpb=1539c893f863596db71ab2af75a802e935496940;p=samtools.git diff --git a/bam_index.c b/bam_index.c index 51c2701..8dc7ce3 100644 --- a/bam_index.c +++ b/bam_index.c @@ -172,19 +172,23 @@ bam_index_t *bam_index_core(bamFile fp) save_bin = save_tid = last_tid = last_bin = 0xffffffffu; save_off = last_off = bam_tell(fp); last_coor = 0xffffffffu; - n_mapped = n_unmapped = n_no_coor = off_end = 0; + n_mapped = n_unmapped = n_no_coor = off_end = 0; 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_coor > c->pos) { + } 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 ((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); - exit(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); @@ -203,7 +207,7 @@ bam_index_t *bam_index_core(bamFile fp) if (bam_tell(fp) <= last_off) { fprintf(stderr, "[bam_index_core] bug in BGZF/RAZF: %llx < %llx\n", (unsigned long long)bam_tell(fp), (unsigned long long)last_off); - exit(1); + return NULL; } if (c->flag & BAM_FUNMAP) ++n_unmapped; else ++n_mapped; @@ -222,7 +226,7 @@ bam_index_t *bam_index_core(bamFile fp) ++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); + return NULL; } } } @@ -255,11 +259,23 @@ void bam_index_save(const bam_index_t *idx, FILE *fp) { int32_t i, size; khint_t k; - fwrite("BAI\1", 1, 4, fp); + if (fwrite("BAI\1", 1, 4, fp) < 4) { + fprintf(stderr, "[%s] failed to write magic number.\n", __func__); + return; + } if (bam_is_be) { uint32_t x = idx->n; - fwrite(bam_swap_endian_4p(&x), 4, 1, fp); - } else fwrite(&idx->n, 4, 1, fp); + if (fwrite(bam_swap_endian_4p(&x), 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_ref.\n", __func__); + return; + } + } + else { + if (fwrite(&idx->n, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_ref.\n", __func__); + return; + } + } for (i = 0; i < idx->n; ++i) { khash_t(i) *index = idx->index[i]; bam_lidx_t *index2 = idx->index2 + i; @@ -267,49 +283,100 @@ void bam_index_save(const bam_index_t *idx, FILE *fp) size = kh_size(index); if (bam_is_be) { // big endian uint32_t x = size; - fwrite(bam_swap_endian_4p(&x), 4, 1, fp); - } else fwrite(&size, 4, 1, fp); + if (fwrite(bam_swap_endian_4p(&x), 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_bin.\n", __func__); + return; + } + } + else { + if (fwrite(&size, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_bin.\n", __func__); + return; + } + } for (k = kh_begin(index); k != kh_end(index); ++k) { if (kh_exist(index, k)) { bam_binlist_t *p = &kh_value(index, k); if (bam_is_be) { // big endian uint32_t x; - x = kh_key(index, k); fwrite(bam_swap_endian_4p(&x), 4, 1, fp); - x = p->n; fwrite(bam_swap_endian_4p(&x), 4, 1, fp); + x = kh_key(index, k); + if (fwrite(bam_swap_endian_4p(&x), 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write bin.\n", __func__); + return; + } + x = p->n; + if (fwrite(bam_swap_endian_4p(&x), 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_chunk.\n", __func__); + return; + } for (x = 0; (int)x < p->n; ++x) { bam_swap_endian_8p(&p->list[x].u); bam_swap_endian_8p(&p->list[x].v); } - fwrite(p->list, 16, p->n, fp); + if (fwrite(p->list, 16, p->n, fp) < p->n) { + fprintf(stderr, "[%s] failed to write %d chunks.\n", __func__, p->n); + return; + } + /* for (x = 0; (int)x < p->n; ++x) { bam_swap_endian_8p(&p->list[x].u); bam_swap_endian_8p(&p->list[x].v); + } */ + } + else { + if (fwrite(&kh_key(index, k), 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write bin.\n", __func__); + return; + } + if (fwrite(&p->n, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_chunk.\n", __func__); + return; + } + if (fwrite(p->list, 16, p->n, fp) < p->n) { + fprintf(stderr, "[%s] failed to write %d chunks.\n", __func__, p->n); + return; } - } else { - fwrite(&kh_key(index, k), 4, 1, fp); - fwrite(&p->n, 4, 1, fp); - fwrite(p->list, 16, p->n, fp); } } } // write linear index (index2) if (bam_is_be) { int x = index2->n; - fwrite(bam_swap_endian_4p(&x), 4, 1, fp); - } else fwrite(&index2->n, 4, 1, fp); + if (fwrite(bam_swap_endian_4p(&x), 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_intv.\n", __func__); + return; + } + } + else { + if (fwrite(&index2->n, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_intv.\n", __func__); + return; + } + } if (bam_is_be) { // big endian int x; for (x = 0; (int)x < index2->n; ++x) bam_swap_endian_8p(&index2->offset[x]); - fwrite(index2->offset, 8, index2->n, fp); + if (fwrite(index2->offset, 8, index2->n, fp) < index2->n) { + fprintf(stderr, "[%s] failed to write ioffset.\n", __func__); + return; + } for (x = 0; (int)x < index2->n; ++x) bam_swap_endian_8p(&index2->offset[x]); - } else fwrite(index2->offset, 8, index2->n, fp); + } + else { + if (fwrite(index2->offset, 8, index2->n, fp) < index2->n) { + fprintf(stderr, "[%s] failed to write ioffset.\n", __func__); + return; + } + } } { // write the number of reads coor-less records. uint64_t x = idx->n_no_coor; if (bam_is_be) bam_swap_endian_8p(&x); - fwrite(&x, 8, 1, fp); + if (fwrite(&x, 8, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to write n_no_coor.\n", __func__); + } } fflush(fp); } @@ -323,14 +390,22 @@ static bam_index_t *bam_index_load_core(FILE *fp) fprintf(stderr, "[bam_index_load_core] fail to load index.\n"); return 0; } - fread(magic, 1, 4, fp); + if (fread(magic, 1, 4, fp) < 4) { + fprintf(stderr, "[%s] failed to read magic number.\n", __func__); + fclose(fp); + return 0; + } if (strncmp(magic, "BAI\1", 4)) { fprintf(stderr, "[bam_index_load] wrong magic number.\n"); fclose(fp); return 0; } idx = (bam_index_t*)calloc(1, sizeof(bam_index_t)); - fread(&idx->n, 4, 1, fp); + if (fread(&idx->n, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to read n_ref.\n", __func__); + fclose(fp); + return 0; + } if (bam_is_be) bam_swap_endian_4p(&idx->n); idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*)); idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t)); @@ -343,18 +418,34 @@ static bam_index_t *bam_index_load_core(FILE *fp) bam_binlist_t *p; index = idx->index[i] = kh_init(i); // load binning index - fread(&size, 4, 1, fp); + if (fread(&size, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to read n_bin.\n", __func__); + fclose(fp); + return 0; + } if (bam_is_be) bam_swap_endian_4p(&size); for (j = 0; j < (int)size; ++j) { - fread(&key, 4, 1, fp); + if (fread(&key, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to read bin.\n", __func__); + fclose(fp); + return 0; + } if (bam_is_be) bam_swap_endian_4p(&key); k = kh_put(i, index, key, &ret); p = &kh_value(index, k); - fread(&p->n, 4, 1, fp); + if (fread(&p->n, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to read n_chunk.\n", __func__); + fclose(fp); + return 0; + } if (bam_is_be) bam_swap_endian_4p(&p->n); p->m = p->n; p->list = (pair64_t*)malloc(p->m * 16); - fread(p->list, 16, p->n, fp); + if (fread(p->list, 16, p->n, fp) < p->n) { + fprintf(stderr, "[%s] failed to read %d chunks.\n", __func__, p->n); + fclose(fp); + return 0; + } if (bam_is_be) { int x; for (x = 0; x < p->n; ++x) { @@ -364,13 +455,26 @@ static bam_index_t *bam_index_load_core(FILE *fp) } } // load linear index - fread(&index2->n, 4, 1, fp); + if (fread(&index2->n, 4, 1, fp) < 1) { + fprintf(stderr, "[%s] failed to read n_intv", __func__); + fclose(fp); + return 0; + } if (bam_is_be) bam_swap_endian_4p(&index2->n); - index2->m = index2->n; - index2->offset = (uint64_t*)calloc(index2->m, 8); - fread(index2->offset, index2->n, 8, fp); - if (bam_is_be) - for (j = 0; j < index2->n; ++j) bam_swap_endian_8p(&index2->offset[j]); + if (index2->n > 0) { + index2->m = index2->n; + index2->offset = (uint64_t*)calloc(index2->m, 8); + if (fread(index2->offset, index2->n, 8, fp) < index2->n) { + fprintf(stderr, "[%s] failed to read %d intervals.", __func__, index2->n); + fclose(fp); + return 0; + } + if (bam_is_be) { + for (j = 0; j < index2->n; ++j) { + bam_swap_endian_8p(&index2->offset[j]); + } + } + } } if (fread(&idx->n_no_coor, 8, 1, fp) == 0) idx->n_no_coor = 0; if (bam_is_be) bam_swap_endian_8p(&idx->n_no_coor); @@ -433,8 +537,12 @@ static void download_from_remote(const char *url) return; } buf = (uint8_t*)calloc(buf_size, 1); - while ((l = knet_read(fp_remote, buf, buf_size)) != 0) - fwrite(buf, 1, l, fp); + while ((l = knet_read(fp_remote, buf, buf_size)) != 0) { + if (fwrite(buf, 1, l, fp) < l) { + fprintf(stderr, "[%s] fail to write to destination file.\n", __func__); + break; + } + } free(buf); fclose(fp); knet_close(fp_remote); @@ -473,6 +581,10 @@ int bam_index_build2(const char *fn, const char *_fnidx) } idx = bam_index_core(fp); bam_close(fp); + if(idx == 0) { + fprintf(stderr, "[bam_index_build2] fail to index the BAM file.\n"); + return -1; + } if (_fnidx == 0) { fnidx = (char*)calloc(strlen(fn) + 5, 1); strcpy(fnidx, fn); strcat(fnidx, ".bai");