Imported Upstream version 0.1.5
[tabix.git] / main.c
diff --git a/main.c b/main.c
index e0417b9d8b3fd2f5d293e4094de799561f8b83c8..520d6630679216b0eea8fd9fd91183e2ac779b70 100644 (file)
--- a/main.c
+++ b/main.c
@@ -2,10 +2,11 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <sys/stat.h>
 #include "bgzf.h"
 #include "tabix.h"
 
-#define PACKAGE_VERSION "0.1.2 (r543)"
+#define PACKAGE_VERSION "0.1.5 (r560)"
 
 static int fetch_func(int l, const char *s, void *data)
 {
@@ -15,9 +16,9 @@ static int fetch_func(int l, const char *s, void *data)
 
 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;
@@ -36,6 +37,8 @@ int main(int argc, char *argv[])
                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;
@@ -52,11 +55,27 @@ int main(int argc, char *argv[])
                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)
+        return ti_list_chromosomes(argv[optind]);
+       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");