- int n_off;
- pair64_t *off = get_chunk_coordinates(idx, tid, beg, end, &n_off);
- if (off == 0) return 0;
- {
- // retrive alignments
- uint64_t curr_off;
- int i, ret, n_seeks;
- n_seeks = 0; i = -1; curr_off = 0;
- bam1_t *b = (bam1_t*)calloc(1, sizeof(bam1_t));
- for (;;) {
- if (curr_off == 0 || curr_off >= off[i].v) { // then jump to the next chunk
- if (i == n_off - 1) break; // no more chunks
- if (i >= 0) assert(curr_off == off[i].v); // otherwise bug
- if (i < 0 || off[i].v != off[i+1].u) { // not adjacent chunks; then seek
- bam_seek(fp, off[i+1].u, SEEK_SET);
- curr_off = bam_tell(fp);
- ++n_seeks;
- }
- ++i;
+ if (iter) { free(iter->off); free(iter); }
+}
+
+int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b)
+{
+ if (iter->finished) return -1;
+ if (iter->from_first) {
+ int ret = bam_read1(fp, b);
+ if (ret < 0) iter->finished = 1;
+ return ret;
+ }
+ if (iter->off == 0) return -1;
+ for (;;) {
+ int ret;
+ if (iter->curr_off == 0 || iter->curr_off >= iter->off[iter->i].v) { // then jump to the next chunk
+ if (iter->i == iter->n_off - 1) break; // no more chunks
+ if (iter->i >= 0) assert(iter->curr_off == iter->off[iter->i].v); // otherwise bug
+ if (iter->i < 0 || iter->off[iter->i].v != iter->off[iter->i+1].u) { // not adjacent chunks; then seek
+ bam_seek(fp, iter->off[iter->i+1].u, SEEK_SET);
+ iter->curr_off = bam_tell(fp);