Imported Upstream version 0.1.6
authorCharles Plessy <plessy@debian.org>
Thu, 19 Aug 2010 06:49:40 +0000 (15:49 +0900)
committerCharles Plessy <plessy@debian.org>
Thu, 19 Aug 2010 06:49:40 +0000 (15:49 +0900)
ChangeLog [new file with mode: 0644]
NEWS
index.c
main.c
tabix.h

diff --git a/ChangeLog b/ChangeLog
new file mode 100644 (file)
index 0000000..19d4f3e
--- /dev/null
+++ b/ChangeLog
@@ -0,0 +1,253 @@
+------------------------------------------------------------------------
+r562 | lh3lh3 | 2010-05-09 19:43:56 -0400 (Sun, 09 May 2010) | 2 lines
+Changed paths:
+   M /trunk/tabix/index.c
+
+Fixed a bug
+
+------------------------------------------------------------------------
+r560 | lh3lh3 | 2010-05-05 10:59:09 -0400 (Wed, 05 May 2010) | 3 lines
+Changed paths:
+   A /trunk/tabix/NEWS
+   M /trunk/tabix/TabixReader.java
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+   M /trunk/tabix/tabix.1
+   M /trunk/tabix/tabix.h
+
+ * Release tabix-0.1.5 (r560)
+ * Improve seeking efficiency. Index file needs to be rebuilt.
+
+------------------------------------------------------------------------
+r559 | lh3lh3 | 2010-05-04 23:11:42 -0400 (Tue, 04 May 2010) | 2 lines
+Changed paths:
+   M /trunk/tabix/main.c
+
+Release tabix-0.1.4 (r559)
+
+------------------------------------------------------------------------
+r558 | lh3lh3 | 2010-05-01 12:48:01 -0400 (Sat, 01 May 2010) | 2 lines
+Changed paths:
+   M /trunk/tabix/TabixReader.java
+
+implement SAM/VCF support; NOT tested yet
+
+------------------------------------------------------------------------
+r557 | lh3lh3 | 2010-05-01 00:42:34 -0400 (Sat, 01 May 2010) | 2 lines
+Changed paths:
+   A /trunk/tabix/TabixReader.java
+
+The Java implementation of tabix.
+
+------------------------------------------------------------------------
+r556 | lh3lh3 | 2010-04-30 22:34:07 -0400 (Fri, 30 Apr 2010) | 4 lines
+Changed paths:
+   M /trunk/tabix/index.c
+   M /trunk/tabix/knetfile.c
+   M /trunk/tabix/main.c
+
+ * tabix-0.1.3-3 (r556)
+ * fixed a small memory leak in knetfile
+ * fixed a minor bug for remote downloading
+
+------------------------------------------------------------------------
+r555 | lh3lh3 | 2010-04-30 22:15:12 -0400 (Fri, 30 Apr 2010) | 4 lines
+Changed paths:
+   M /trunk/tabix/Makefile
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+
+ * tabix-0.1.3-2 (r555)
+ * do not overwrite index file by default
+ * a little code cleanup
+
+------------------------------------------------------------------------
+r554 | lh3lh3 | 2010-04-30 21:44:31 -0400 (Fri, 30 Apr 2010) | 2 lines
+Changed paths:
+   M /trunk/tabix/index.c
+
+fixed a potential bug for UCSC-like coordinate
+
+------------------------------------------------------------------------
+r553 | lh3lh3 | 2010-04-28 17:43:41 -0400 (Wed, 28 Apr 2010) | 2 lines
+Changed paths:
+   M /trunk/tabix/tabix.tex
+
+minor clarification to the format spec
+
+------------------------------------------------------------------------
+r552 | lh3lh3 | 2010-04-28 16:33:07 -0400 (Wed, 28 Apr 2010) | 3 lines
+Changed paths:
+   M /trunk/tabix/Makefile
+   M /trunk/tabix/bgzip.c
+   A /trunk/tabix/tabix.tex
+
+ * added the format specification
+ * fixed a typo in bgzip
+
+------------------------------------------------------------------------
+r550 | petulda | 2010-04-22 11:03:24 -0400 (Thu, 22 Apr 2010) | 1 line
+Changed paths:
+   M /trunk/tabix/bgzip.c
+
+The behaviour changed slightly to mimic gzip. Detect if std descriptors are connected to the terminal.
+------------------------------------------------------------------------
+r549 | petulda | 2010-04-22 09:46:10 -0400 (Thu, 22 Apr 2010) | 1 line
+Changed paths:
+   M /trunk/tabix/bgzip.c
+
+Fix in src/dst file detection and slight change of behaviour
+------------------------------------------------------------------------
+r548 | petulda | 2010-04-19 04:39:46 -0400 (Mon, 19 Apr 2010) | 1 line
+Changed paths:
+   M /trunk/tabix/index.c
+
+Close file descriptor in ti_list_chromosomes
+------------------------------------------------------------------------
+r547 | petulda | 2010-04-16 09:27:11 -0400 (Fri, 16 Apr 2010) | 1 line
+Changed paths:
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+   M /trunk/tabix/tabix.h
+
+Added the -l option for listing chromosomes
+------------------------------------------------------------------------
+r544 | lh3lh3 | 2010-03-29 10:58:48 -0400 (Mon, 29 Mar 2010) | 2 lines
+Changed paths:
+   M /trunk/tabix/main.c
+
+removed a line of debugging code
+
+------------------------------------------------------------------------
+r543 | lh3lh3 | 2010-03-19 12:29:16 -0400 (Fri, 19 Mar 2010) | 3 lines
+Changed paths:
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+   M /trunk/tabix/tabix.1
+
+ * tabix-0.1.3 (r543)
+ * fixed another off-by-one bug
+
+------------------------------------------------------------------------
+r542 | lh3lh3 | 2010-03-16 22:35:52 -0400 (Tue, 16 Mar 2010) | 2 lines
+Changed paths:
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+   M /trunk/tabix/tabix.1
+
+Release tabix-0.1.1
+
+------------------------------------------------------------------------
+r506 | lh3lh3 | 2009-11-02 23:20:12 -0500 (Mon, 02 Nov 2009) | 2 lines
+Changed paths:
+   M /trunk/tabix/main.c
+
+Release tabix-0.1.0
+
+------------------------------------------------------------------------
+r505 | lh3lh3 | 2009-11-02 23:15:49 -0500 (Mon, 02 Nov 2009) | 2 lines
+Changed paths:
+   A /trunk/tabix/tabix.1
+
+documentation
+
+------------------------------------------------------------------------
+r504 | lh3lh3 | 2009-11-02 11:08:18 -0500 (Mon, 02 Nov 2009) | 5 lines
+Changed paths:
+   M /trunk/tabix/Makefile
+   M /trunk/tabix/bgzip.c
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+   M /trunk/tabix/tabix.h
+
+ * tabix-0.0.0-5 (r504)
+ * fixed a critical bug in fetching data (a typo in fact)
+ * support SAM (tested on ex1.sam) and VCF (not tested)
+ * improve the command-line interface
+
+------------------------------------------------------------------------
+r503 | lh3lh3 | 2009-11-02 10:04:43 -0500 (Mon, 02 Nov 2009) | 3 lines
+Changed paths:
+   M /trunk/tabix/Makefile
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+
+ * tabix-0.0.0-4 (r503)
+ * index files are bgzf compressed
+
+------------------------------------------------------------------------
+r502 | lh3lh3 | 2009-11-02 09:47:25 -0500 (Mon, 02 Nov 2009) | 4 lines
+Changed paths:
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+   M /trunk/tabix/tabix.h
+
+ * tabix-0.0.0-3 (r502)
+ * support meta lines (not tested)
+ * I am going to make the index file in the BGZF format
+
+------------------------------------------------------------------------
+r501 | lh3lh3 | 2009-11-01 22:03:07 -0500 (Sun, 01 Nov 2009) | 3 lines
+Changed paths:
+   M /trunk/tabix/Makefile
+   M /trunk/tabix/bgzf.h
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+
+ * tabix-0.0.0-2 (r501)
+ * accelerate ti_readline()
+
+------------------------------------------------------------------------
+r500 | lh3lh3 | 2009-11-01 20:49:52 -0500 (Sun, 01 Nov 2009) | 3 lines
+Changed paths:
+   M /trunk/tabix/Makefile
+   M /trunk/tabix/bgzip.c
+   M /trunk/tabix/index.c
+   M /trunk/tabix/main.c
+
+ * tabix-0.0.0-1 (r500)
+ * apparently working
+
+------------------------------------------------------------------------
+r499 | lh3lh3 | 2009-11-01 14:04:52 -0500 (Sun, 01 Nov 2009) | 2 lines
+Changed paths:
+   D /trunk/tabix/parser.c
+
+obsolete file
+
+------------------------------------------------------------------------
+r498 | lh3lh3 | 2009-11-01 14:04:08 -0500 (Sun, 01 Nov 2009) | 2 lines
+Changed paths:
+   M /trunk/tabix/bgzip.c
+
+bgzip is more like gzip in its command-line interface
+
+------------------------------------------------------------------------
+r497 | lh3lh3 | 2009-11-01 13:43:35 -0500 (Sun, 01 Nov 2009) | 2 lines
+Changed paths:
+   A /trunk/tabix/Makefile
+   A /trunk/tabix/bam_endian.h
+   A /trunk/tabix/bgzf.c
+   A /trunk/tabix/bgzf.h
+   A /trunk/tabix/bgzip.c
+   A /trunk/tabix/index.c
+   A /trunk/tabix/khash.h
+   A /trunk/tabix/knetfile.c
+   A /trunk/tabix/knetfile.h
+   A /trunk/tabix/ksort.h
+   A /trunk/tabix/kstring.c
+   A /trunk/tabix/kstring.h
+   A /trunk/tabix/main.c
+   A /trunk/tabix/parser.c
+   A /trunk/tabix/tabix.h
+
+initial source code. It is BUGGY!
+
+------------------------------------------------------------------------
+r496 | lh3lh3 | 2009-11-01 13:42:39 -0500 (Sun, 01 Nov 2009) | 2 lines
+Changed paths:
+   A /trunk/tabix
+
+A generic indexer for TAB-delimited genome position files
+
+------------------------------------------------------------------------
diff --git a/NEWS b/NEWS
index aaa95e07e45eb4575c3db5f9b17bee5517ea0303..8cb7f0dd2d1de74a717fb535056412d04a8fddff 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,14 +1,38 @@
+Beta Release 0.1.6 (9 May, 2010)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Notable changes:
+
+ * Improved backward compatibility. Release 0.1.5 does not work with the
+   buggy index file generated by 0.1.2.
+
+ * Fixed a bug in building linear index. The bug does not affect the
+   results, only affects efficiency in rare cases.
+
+ * Reduced the number of seek calls given an index generated by old
+   version of tabix.
+
+ * Added new APIs for retrieving data via an iterator. The old callback
+   APIs are not changed, although internally it uses iterator to
+   retrieve data.
+
+I am trying to freeze tabix. I just hope I am committing new bugs.
+
+(0.1.6: 9 May 2010, r563)
+
+
+
 Beta Release 0.1.5 (5 May, 2010)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 Notable changes:
 
- * Clarify that tabix is released under MIT/X11.
+ * Clarified that tabix is released under MIT/X11.
 
- * Improve the robustness of indexing and retrieval.
+ * Improved the robustness of indexing and retrieval.
 
- * Reduce the number of seek calls when the specified region starts from
-   a 16kb block with no data. The index format is the same, but the
+ * Reduced the number of seek calls when the specified region starts
+   from a 16kb block with no data. The index format is the same, but the
    content is changed a little.
 
 (0.1.5: 5 May 2010, r560)
diff --git a/index.c b/index.c
index cfb2938ee4ee9d490cd70f5635608b8f21abe291..61d7d6d6fdc370d09d9b4b916e629cf48e1aa8c2 100644 (file)
--- a/index.c
+++ b/index.c
@@ -42,6 +42,16 @@ struct __ti_index_t {
        ti_lidx_t *index2;
 };
 
+struct __ti_iter_t {
+       int from_first; // read from the first record; no random access
+       int tid, beg, end, n_off, i, finished;
+       uint64_t curr_off;
+       kstring_t str;
+       BGZF *fp;
+       const ti_index_t *idx;
+       pair64_t *off;
+};
+
 typedef struct {
        int tid, beg, end, bin;
 } ti_intv_t;
@@ -241,9 +251,13 @@ static inline void insert_offset2(ti_lidx_t *index2, int _beg, int _end, uint64_
                index2->offset = (uint64_t*)realloc(index2->offset, index2->m * 8);
                memset(index2->offset + old_m, 0, 8 * (index2->m - old_m));
        }
-       for (i = beg + 1; i <= end; ++i)
-               if (index2->offset[i] == 0) index2->offset[i] = offset;
-       index2->n = end + 1;
+       if (beg == end) {
+               if (index2->offset[beg] == 0) index2->offset[beg] = offset;
+       } else {
+               for (i = beg; i <= end; ++i)
+                       if (index2->offset[i] == 0) index2->offset[i] = offset;
+       }
+       if (index2->n < end + 1) index2->n = end + 1;
 }
 
 static void merge_chunks(ti_index_t *idx)
@@ -310,10 +324,10 @@ ti_index_t *ti_index_core(BGZF *fp, const ti_conf_t *conf)
                        last_tid = intv.tid;
                        last_bin = 0xffffffffu;
                } else if (last_coor > intv.beg) {
-                       fprintf(stderr, "[ti_index_core] the file is not sorted at line %llu\n", (unsigned long long)lineno);
+                       fprintf(stderr, "[ti_index_core] the file out of order at line %llu\n", (unsigned long long)lineno);
                        exit(1);
                }
-               if (intv.bin < 4681) insert_offset2(&idx->index2[intv.tid], intv.beg, intv.end, last_off);
+               insert_offset2(&idx->index2[intv.tid], intv.beg, intv.end, last_off);
                if (intv.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);
@@ -612,21 +626,16 @@ static char *get_local_version(const char *fn)
        free(fnidx); return 0;
 }
 
-int ti_list_chromosomes(const char *fn)
+const char **ti_seqname(const ti_index_t *idx, int *n)
 {
-       ti_index_t *idx;
-       char **names;
-       int i;
+       const char **names;
        khint_t k;
-       idx = ti_index_load(fn);
+       *n = idx->n;
        names = calloc(idx->n, sizeof(void*));
        for (k = kh_begin(idx->tname); k < kh_end(idx->tname); ++k)
                if (kh_exist(idx->tname, k))
-                       names[kh_val(idx->tname, k)] = (char*)kh_key(idx->tname, k);
-       for (i = 0; i < idx->n; ++i) printf("%s\n", names[i]);
-       free(names);
-       ti_index_destroy(idx);
-       return 0;
+                       names[kh_val(idx->tname, k)] = kh_key(idx->tname, k);
+       return names;
 }
 
 ti_index_t *ti_index_load(const char *fn)
@@ -732,13 +741,15 @@ static inline int reg2bins(uint32_t beg, uint32_t end, uint16_t list[MAX_BIN])
        return i;
 }
 
-static inline int is_overlap(int32_t beg, int32_t end, int rbeg, int rend)
+ti_iter_t ti_first(BGZF *fp)
 {
-       return (rend > beg && rbeg < end);
+       ti_iter_t iter;
+       iter = calloc(1, sizeof(struct __ti_iter_t));
+       iter->fp = fp; iter->from_first = 1;
+       return iter;
 }
 
-// ti_fetch helper function retrieves 
-pair64_t *get_chunk_coordinates(const ti_index_t *idx, int tid, int beg, int end, int* cnt_off)
+ti_iter_t ti_query(BGZF *fp, const ti_index_t *idx, int tid, int beg, int end)
 {
        uint16_t *bins;
        int i, n_bins, n_off;
@@ -746,18 +757,33 @@ pair64_t *get_chunk_coordinates(const ti_index_t *idx, int tid, int beg, int end
        khint_t k;
        khash_t(i) *index;
        uint64_t min_off;
+       ti_iter_t iter = 0;
 
+       // initialize the iterator
+       iter = calloc(1, sizeof(struct __ti_iter_t));
+       iter->fp = fp; iter->idx = idx;
+       iter->tid = tid; iter->beg = beg; iter->end = end; iter->i = -1;
+       // random access
        bins = (uint16_t*)calloc(MAX_BIN, 2);
        n_bins = reg2bins(beg, end, bins);
        index = idx->index[tid];
-       min_off = (beg>>TAD_LIDX_SHIFT >= idx->index2[tid].n)? idx->index2[tid].offset[idx->index2[tid].n-1]
-               : idx->index2[tid].offset[beg>>TAD_LIDX_SHIFT];
+       if (idx->index2[tid].n > 0) {
+               min_off = (beg>>TAD_LIDX_SHIFT >= idx->index2[tid].n)? idx->index2[tid].offset[idx->index2[tid].n-1]
+                       : idx->index2[tid].offset[beg>>TAD_LIDX_SHIFT];
+               if (min_off == 0) { // improvement for index files built by tabix prior to 0.1.4
+                       int n = beg>>TAD_LIDX_SHIFT;
+                       if (n > idx->index2[tid].n) n = idx->index2[tid].n;
+                       for (i = n - 1; i >= 0; --i)
+                               if (idx->index2[tid].offset[i] != 0) break;
+                       if (i >= 0) min_off = idx->index2[tid].offset[i];
+               }
+       } else min_off = 0; // tabix 0.1.2 may produce such index files
        for (i = n_off = 0; i < n_bins; ++i) {
                if ((k = kh_get(i, index, bins[i])) != kh_end(index))
                        n_off += kh_value(index, k).n;
        }
        if (n_off == 0) {
-               free(bins); return 0;
+               free(bins); return iter;
        }
        off = (pair64_t*)calloc(n_off, 16);
        for (i = n_off = 0; i < n_bins; ++i) {
@@ -788,44 +814,67 @@ pair64_t *get_chunk_coordinates(const ti_index_t *idx, int tid, int beg, int end
                        n_off = l + 1;
                }
        }
-       *cnt_off = n_off;
-       return off;
+       iter->n_off = n_off; iter->off = off;
+       return iter;
 }
 
-int ti_fetch(BGZF *fp, const ti_index_t *idx, int tid, int beg, int end, void *data, ti_fetch_f func)
+const char *ti_iter_read(ti_iter_t iter, int *len)
 {
-       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);
                        }
-                       if ((ret = ti_readline(fp, str)) >= 0) {
-                               ti_intv_t intv;
-                               curr_off = bgzf_tell(fp);
-                               if (str->s[0] == idx->conf.meta_char) continue;
-                               get_intv((ti_index_t*)idx, str, &intv);
-                               if (intv.tid != tid || intv.beg >= end) break; // no need to proceed
-                               else if (is_overlap(beg, end, intv.beg, intv.end)) func(str->l, str->s, data);
-                       } else break; // end of file
+                       ++iter->i;
                }
-//             fprintf(stderr, "[ti_fetch] # seek calls: %d\n", n_seeks);
-               free(str->s); free(str);
+               if ((ret = ti_readline(iter->fp, &iter->str)) >= 0) {
+                       ti_intv_t intv;
+                       iter->curr_off = bgzf_tell(iter->fp);
+                       if (iter->str.s[0] == iter->idx->conf.meta_char) continue;
+                       get_intv((ti_index_t*)iter->idx, &iter->str, &intv);
+                       if (intv.tid != iter->tid || intv.beg >= iter->end) break; // no need to proceed
+                       else if (intv.end > iter->beg && iter->end > intv.beg) {
+                               if (len) *len = iter->str.l;
+                               return iter->str.s;
+                       }
+               } else break; // end of file
+       }
+       iter->finished = 1;
+       return 0;
+}
+
+void ti_iter_destroy(ti_iter_t iter)
+{
+       if (iter) {
+               free(iter->str.s); free(iter->off);
+               free(iter);
        }
-       free(off);
+}
+
+int ti_fetch(BGZF *fp, const ti_index_t *idx, int tid, int beg, int end, void *data, ti_fetch_f func)
+{
+       ti_iter_t iter;
+       const char *s;
+       int len;
+       iter = ti_query(fp, idx, tid, beg, end);
+       while ((s = ti_iter_read(iter, &len)) != 0)
+               func(len, s, data);
+       ti_iter_destroy(iter);
        return 0;
 }
diff --git a/main.c b/main.c
index 520d6630679216b0eea8fd9fd91183e2ac779b70..a05939dd96f306843d877859148246c30a44f90b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,13 +6,7 @@
 #include "bgzf.h"
 #include "tabix.h"
 
-#define PACKAGE_VERSION "0.1.5 (r560)"
-
-static int fetch_func(int l, const char *s, void *data)
-{
-       printf("%s\n", s);
-       return 0;
-}
+#define PACKAGE_VERSION "0.1.6 (r565)"
 
 int main(int argc, char *argv[])
 {
@@ -60,8 +54,21 @@ int main(int argc, char *argv[])
                fprintf(stderr, "\n");
                return 1;
        }
-    if (list_chrms)
-        return ti_list_chromosomes(argv[optind]);
+    if (list_chrms) {
+               ti_index_t *idx;
+               int i, n;
+               const char **names;
+               idx = ti_index_load(argv[optind]);
+               if (idx == 0) {
+                       fprintf(stderr, "[main] fail to load the index file.\n");
+                       return 1;
+               }
+               names = ti_seqname(idx, &n);
+               for (i = 0; i < n; ++i) printf("%s\n", names[i]);
+               free(names);
+               ti_index_destroy(idx);
+               return 0;
+       }
        if (optind + 1 == argc) {
                if (force == 0) {
                        struct stat buf;
@@ -84,11 +91,14 @@ int main(int argc, char *argv[])
                        return 1;
                }
                if (strcmp(argv[optind+1], ".") == 0) { // retrieve all
-                       kstring_t *str = calloc(1, sizeof(kstring_t));
-                       while (ti_readline(fp, str) >= 0) { // FIXME: check return code for error
-                               fputs(str->s, stdout); fputc('\n', stdout);
+                       ti_iter_t iter;
+                       const char *s;
+                       int len;
+                       iter = ti_first(fp);
+                       while ((s = ti_iter_read(iter, &len)) != 0) {
+                               fputs(s, stdout); fputc('\n', stdout);
                        }
-                       free(str->s); free(str);
+                       ti_iter_destroy(iter);
                } else { // retrieve from specified regions
                        ti_index_t *idx;
                        int i;
@@ -101,7 +111,14 @@ int main(int argc, char *argv[])
                        for (i = optind + 1; i < argc; ++i) {
                                int tid, beg, end;
                                if (ti_parse_region(idx, argv[i], &tid, &beg, &end) == 0) {
-                                       ti_fetch(fp, idx, tid, beg, end, 0, fetch_func);
+                                       ti_iter_t iter;
+                                       const char *s;
+                                       int len;
+                                       iter = ti_query(fp, idx, tid, beg, end);
+                                       while ((s = ti_iter_read(iter, &len)) != 0) {
+                                               fputs(s, stdout); fputc('\n', stdout);
+                                       }
+                                       ti_iter_destroy(iter);
                                } else fprintf(stderr, "[main] invalid region: unknown target name or minus interval.\n");
                        }
                        ti_index_destroy(idx);
diff --git a/tabix.h b/tabix.h
index 0df29a0f8ea889f03684eb540bd517cbb76d861c..a002d8a1e09a4da451449ab24431abade57c6c76 100644 (file)
--- a/tabix.h
+++ b/tabix.h
@@ -43,27 +43,63 @@ typedef int (*ti_fetch_f)(int l, const char *s, void *data);
 struct __ti_index_t;
 typedef struct __ti_index_t ti_index_t;
 
+struct __ti_iter_t;
+typedef struct __ti_iter_t *ti_iter_t;
+
 typedef struct {
        int32_t preset;
-       int32_t sc, bc, ec;
+       int32_t sc, bc, ec; // seq col., beg col. and end col.
        int32_t meta_char, line_skip;
 } ti_conf_t;
 
-extern ti_conf_t ti_conf_gff, ti_conf_bed, ti_conf_psltbl, ti_conf_vcf, ti_conf_sam;
+extern ti_conf_t ti_conf_gff, ti_conf_bed, ti_conf_psltbl, ti_conf_vcf, ti_conf_sam; // preset
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-       int ti_readline(BGZF *fp, kstring_t *str);
-
+       /* Build the index for file <fn>. File <fn>.tbi will be generated
+        * and overwrite the file of the same name. Return -1 on failure. */
        int ti_index_build(const char *fn, const ti_conf_t *conf);
+
+       /* Load the index from file <fn>.tbi. If <fn> is a URL and the index
+        * file is not in the working directory, <fn>.tbi will be
+        * downloaded. Return NULL on failure. */
        ti_index_t *ti_index_load(const char *fn);
-    int ti_list_chromosomes(const char *fn);
+
+       /* Destroy the index */
        void ti_index_destroy(ti_index_t *idx);
+
+       /* Get the list of sequence names. Each "char*" pointer points to a
+        * internal member of the index, so DO NOT modify the returned
+        * pointer; otherwise the index will be corrupted. The returned
+        * pointer should be freed by a single free() call by the routine
+        * calling this function. The number of sequences is returned at *n. */
+       const char **ti_seqname(const ti_index_t *idx, int *n);
+
+       /* Parse a region like: chr2, chr2:100, chr2:100-200. Return -1 on failure. */
        int ti_parse_region(ti_index_t *idx, const char *str, int *tid, int *begin, int *end);
+
+       /* Get the iterator pointing to the first record at the current file
+        * position. If the file is just openned, the iterator points to the
+        * first record in the file. */
+       ti_iter_t ti_first(BGZF *fp);
+
+       /* Get the iterator pointing to the first record in region tid:beg-end */
+       ti_iter_t ti_query(BGZF *fp, const ti_index_t *idx, int tid, int beg, int end);
+
+       /* Get the data line pointed by the iterator and iterate to the next record. */
+       const char *ti_iter_read(ti_iter_t iter, int *len);
+
+       /* Destroy the iterator */
+       void ti_iter_destroy(ti_iter_t iter);
+
+       /* The callback version for random access */
        int ti_fetch(BGZF *fp, const ti_index_t *idx, int tid, int beg, int end, void *data, ti_fetch_f func);
 
+       /* Read one line. */
+       int ti_readline(BGZF *fp, kstring_t *str);
+
 #ifdef __cplusplus
 }
 #endif