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