- 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;
- kstring_t *str = calloc(1, sizeof(kstring_t));
- n_seeks = 0; i = -1; curr_off = 0;
- 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
- bgzf_seek(fp, off[i+1].u, SEEK_SET);
- curr_off = bgzf_tell(fp);
- ++n_seeks;
- }
- ++i;
+ if (iter->finished) return 0;
+ if (iter->from_first) {
+ int ret;
+ if ((ret = ti_readline(iter->fp, &iter->str)) < 0) {
+ iter->finished = 1;
+ return 0;
+ } else {
+ if (len) *len = iter->str.l;
+ return iter->str.s;
+ }
+ }
+ if (iter->n_off == 0) return 0;
+ while (1) {
+ 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
+ bgzf_seek(iter->fp, iter->off[iter->i+1].u, SEEK_SET);
+ iter->curr_off = bgzf_tell(iter->fp);