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