Imported Upstream version 0.2.0
[tabix.git] / main.c
diff --git a/main.c b/main.c
index 520d6630679216b0eea8fd9fd91183e2ac779b70..a2a456566fb9f9a68e3692827cc8fd99943f3f2c 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;
@@ -77,36 +84,38 @@ int main(int argc, char *argv[])
                return ti_index_build(argv[optind], &conf);
        }
        { // retrieve
-               BGZF *fp;
-               fp = bgzf_open(argv[optind], "r");
-               if (fp == 0) {
+               tabix_t *t;
+               if ((t = ti_open(argv[optind], 0)) == 0) {
                        fprintf(stderr, "[main] fail to open the data file.\n");
                        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_query(t, 0, 0, 0);
+                       while ((s = ti_read(t, 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;
-                       idx = ti_index_load(argv[optind]);
-                       if (idx == 0) {
-                               bgzf_close(fp);
-                               fprintf(stderr, "[main] fail to load the index.\n");
-                               return 1;
-                       }
+                       ti_lazy_index_load(t);
                        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);
+                               if (ti_parse_region(t->idx, argv[i], &tid, &beg, &end) == 0) {
+                                       ti_iter_t iter;
+                                       const char *s;
+                                       int len;
+                                       iter = ti_queryi(t, tid, beg, end);
+                                       while ((s = ti_read(t, 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);
                }
-               bgzf_close(fp);
+               ti_close(t);
        }
        return 0;
 }