Imported Upstream version 0.2.0
[tabix.git] / main.c
1 #include <string.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <sys/stat.h>
6 #include "bgzf.h"
7 #include "tabix.h"
8
9 #define PACKAGE_VERSION "0.1.6 (r565)"
10
11 int main(int argc, char *argv[])
12 {
13         int c, skip = -1, meta = -1, list_chrms = 0, force = 0;
14         ti_conf_t conf = ti_conf_gff;
15         while ((c = getopt(argc, argv, "p:s:b:e:0S:c:lf")) >= 0) {
16                 switch (c) {
17                 case '0': conf.preset |= TI_FLAG_UCSC; break;
18                 case 'S': skip = atoi(optarg); break;
19                 case 'c': meta = optarg[0]; break;
20                 case 'p':
21                         if (strcmp(optarg, "gff") == 0) conf = ti_conf_gff;
22                         else if (strcmp(optarg, "bed") == 0) conf = ti_conf_bed;
23                         else if (strcmp(optarg, "sam") == 0) conf = ti_conf_sam;
24                         else if (strcmp(optarg, "vcf") == 0) conf = ti_conf_vcf;
25                         else if (strcmp(optarg, "psltbl") == 0) conf = ti_conf_psltbl;
26                         else {
27                                 fprintf(stderr, "[main] unrecognized preset '%s'\n", optarg);
28                                 return 1;
29                         }
30                         break;
31                 case 's': conf.sc = atoi(optarg); break;
32                 case 'b': conf.bc = atoi(optarg); break;
33                 case 'e': conf.ec = atoi(optarg); break;
34         case 'l': list_chrms = 1; break;
35                 case 'f': force = 1; break;
36                 }
37         }
38         if (skip >= 0) conf.line_skip = skip;
39         if (meta >= 0) conf.meta_char = meta;
40         if (optind == argc) {
41                 fprintf(stderr, "\n");
42                 fprintf(stderr, "Program: tabix (TAB-delimited file InderXer)\n");
43                 fprintf(stderr, "Version: %s\n\n", PACKAGE_VERSION);
44                 fprintf(stderr, "Usage:   tabix <in.tab.bgz> [region1 [region2 [...]]]\n\n");
45                 fprintf(stderr, "Options: -p STR     preset: gff, bed, sam, vcf, psltbl [gff]\n");
46                 fprintf(stderr, "         -s INT     sequence name column [1]\n");
47                 fprintf(stderr, "         -b INT     start column [4]\n");
48                 fprintf(stderr, "         -e INT     end column [5]\n");
49                 fprintf(stderr, "         -S INT     skip first INT lines [0]\n");
50                 fprintf(stderr, "         -c CHAR    symbol for comment/meta lines [#]\n");
51                 fprintf(stderr, "         -0         zero-based coordinate\n");
52                 fprintf(stderr, "         -l         list chromosome names\n");
53                 fprintf(stderr, "         -f         force to overwrite the index\n");
54                 fprintf(stderr, "\n");
55                 return 1;
56         }
57     if (list_chrms) {
58                 ti_index_t *idx;
59                 int i, n;
60                 const char **names;
61                 idx = ti_index_load(argv[optind]);
62                 if (idx == 0) {
63                         fprintf(stderr, "[main] fail to load the index file.\n");
64                         return 1;
65                 }
66                 names = ti_seqname(idx, &n);
67                 for (i = 0; i < n; ++i) printf("%s\n", names[i]);
68                 free(names);
69                 ti_index_destroy(idx);
70                 return 0;
71         }
72         if (optind + 1 == argc) {
73                 if (force == 0) {
74                         struct stat buf;
75                         char *fnidx = calloc(strlen(argv[optind]) + 5, 1);
76                         strcat(strcpy(fnidx, argv[optind]), ".tbi");
77                         if (stat(fnidx, &buf) == 0) {
78                                 fprintf(stderr, "[tabix] the index file exists. Please use '-f' to overwrite.\n");
79                                 free(fnidx);
80                                 return 1;
81                         }
82                         free(fnidx);
83                 }
84                 return ti_index_build(argv[optind], &conf);
85         }
86         { // retrieve
87                 tabix_t *t;
88                 if ((t = ti_open(argv[optind], 0)) == 0) {
89                         fprintf(stderr, "[main] fail to open the data file.\n");
90                         return 1;
91                 }
92                 if (strcmp(argv[optind+1], ".") == 0) { // retrieve all
93                         ti_iter_t iter;
94                         const char *s;
95                         int len;
96                         iter = ti_query(t, 0, 0, 0);
97                         while ((s = ti_read(t, iter, &len)) != 0) {
98                                 fputs(s, stdout); fputc('\n', stdout);
99                         }
100                         ti_iter_destroy(iter);
101                 } else { // retrieve from specified regions
102                         int i;
103                         ti_lazy_index_load(t);
104                         for (i = optind + 1; i < argc; ++i) {
105                                 int tid, beg, end;
106                                 if (ti_parse_region(t->idx, argv[i], &tid, &beg, &end) == 0) {
107                                         ti_iter_t iter;
108                                         const char *s;
109                                         int len;
110                                         iter = ti_queryi(t, tid, beg, end);
111                                         while ((s = ti_read(t, iter, &len)) != 0) {
112                                                 fputs(s, stdout); fputc('\n', stdout);
113                                         }
114                                         ti_iter_destroy(iter);
115                                 } else fprintf(stderr, "[main] invalid region: unknown target name or minus interval.\n");
116                         }
117                 }
118                 ti_close(t);
119         }
120         return 0;
121 }