#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
+#include <sys/stat.h>
#include "bgzf.h"
#include "tabix.h"
-#define PACKAGE_VERSION "0.1.1 (r542)"
-
-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[])
{
- int c, skip = -1, meta = -1;
+ int c, skip = -1, meta = -1, list_chrms = 0, force = 0;
ti_conf_t conf = ti_conf_gff;
- while ((c = getopt(argc, argv, "p:s:b:e:0S:c:")) >= 0) {
+ while ((c = getopt(argc, argv, "p:s:b:e:0S:c:lf")) >= 0) {
switch (c) {
case '0': conf.preset |= TI_FLAG_UCSC; break;
case 'S': skip = atoi(optarg); break;
case 's': conf.sc = atoi(optarg); break;
case 'b': conf.bc = atoi(optarg); break;
case 'e': conf.ec = atoi(optarg); break;
+ case 'l': list_chrms = 1; break;
+ case 'f': force = 1; break;
}
}
if (skip >= 0) conf.line_skip = skip;
fprintf(stderr, " -S INT skip first INT lines [0]\n");
fprintf(stderr, " -c CHAR symbol for comment/meta lines [#]\n");
fprintf(stderr, " -0 zero-based coordinate\n");
+ fprintf(stderr, " -l list chromosome names\n");
+ fprintf(stderr, " -f force to overwrite the index\n");
fprintf(stderr, "\n");
return 1;
}
- if (optind + 1 == argc)
+ 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;
+ char *fnidx = calloc(strlen(argv[optind]) + 5, 1);
+ strcat(strcpy(fnidx, argv[optind]), ".tbi");
+ if (stat(fnidx, &buf) == 0) {
+ fprintf(stderr, "[tabix] the index file exists. Please use '-f' to overwrite.\n");
+ free(fnidx);
+ return 1;
+ }
+ free(fnidx);
+ }
return ti_index_build(argv[optind], &conf);
+ }
{ // retrieve
BGZF *fp;
fp = bgzf_open(argv[optind], "r");
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;
for (i = optind + 1; i < argc; ++i) {
int tid, beg, end;
if (ti_parse_region(idx, argv[i], &tid, &beg, &end) == 0) {
- fprintf(stderr, "%d,%d\n", beg, end);
- 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);