- // retrive alignments
- 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
- bam_seek(fp, off[i+1].u, SEEK_SET);
- curr_off = bam_tell(fp);
- ++n_seeks;
- }
- ++i;
+ bam_destroy1(b);
+ }
+ iter->n_off = n_off; iter->off = off;
+ return iter;
+}
+
+pair64_t *get_chunk_coordinates(const bam_index_t *idx, int tid, int beg, int end, int *cnt_off)
+{ // for pysam compatibility
+ bam_iter_t iter;
+ pair64_t *off;
+ iter = bam_iter_query(idx, tid, beg, end);
+ off = iter->off; *cnt_off = iter->n_off;
+ free(iter);
+ return off;
+}
+
+void bam_iter_destroy(bam_iter_t iter)
+{
+ if (iter) { free(iter->off); free(iter); }
+}
+
+int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b)
+{
+ int ret;
+ if (iter && iter->finished) return -1;
+ if (iter == 0 || iter->from_first) {
+ ret = bam_read1(fp, b);
+ if (ret < 0 && iter) iter->finished = 1;
+ return ret;
+ }
+ if (iter->off == 0) return -1;
+ for (;;) {
+ 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) { ret = -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);