X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=main.c;h=8ae6b2e69d78c0b7251635041344e90e02782c85;hb=a3f2132b77c1d5d5b084aeb60bd6e56dded44f6c;hp=a05939dd96f306843d877859148246c30a44f90b;hpb=d1fcb739b58e050328dcfc47c3af235bc0e41455;p=tabix.git diff --git a/main.c b/main.c index a05939d..8ae6b2e 100644 --- a/main.c +++ b/main.c @@ -6,13 +6,13 @@ #include "bgzf.h" #include "tabix.h" -#define PACKAGE_VERSION "0.1.6 (r565)" +#define PACKAGE_VERSION "0.2.3 (r876)" int main(int argc, char *argv[]) { - int c, skip = -1, meta = -1, list_chrms = 0, force = 0; + int c, skip = -1, meta = -1, list_chrms = 0, force = 0, print_header = 0; ti_conf_t conf = ti_conf_gff; - while ((c = getopt(argc, argv, "p:s:b:e:0S:c:lf")) >= 0) { + while ((c = getopt(argc, argv, "p:s:b:e:0S:c:lhf")) >= 0) { switch (c) { case '0': conf.preset |= TI_FLAG_UCSC; break; case 'S': skip = atoi(optarg); break; @@ -21,7 +21,7 @@ int main(int argc, char *argv[]) if (strcmp(optarg, "gff") == 0) conf = ti_conf_gff; else if (strcmp(optarg, "bed") == 0) conf = ti_conf_bed; else if (strcmp(optarg, "sam") == 0) conf = ti_conf_sam; - else if (strcmp(optarg, "vcf") == 0) conf = ti_conf_vcf; + else if (strcmp(optarg, "vcf") == 0 || strcmp(optarg, "vcf4") == 0) conf = ti_conf_vcf; else if (strcmp(optarg, "psltbl") == 0) conf = ti_conf_psltbl; else { fprintf(stderr, "[main] unrecognized preset '%s'\n", optarg); @@ -32,6 +32,7 @@ int main(int argc, char *argv[]) case 'b': conf.bc = atoi(optarg); break; case 'e': conf.ec = atoi(optarg); break; case 'l': list_chrms = 1; break; + case 'h': print_header = 1; break; case 'f': force = 1; break; } } @@ -49,6 +50,7 @@ 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, " -h print the VCF header\n"); fprintf(stderr, " -l list chromosome names\n"); fprintf(stderr, " -f force to overwrite the index\n"); fprintf(stderr, "\n"); @@ -81,12 +83,16 @@ int main(int argc, char *argv[]) } free(fnidx); } + if ( is_bgzipped(argv[optind])!=1 ) + { + fprintf(stderr,"[tabix] was bgzip used to compress this file? %s\n", argv[optind]); + return 1; + } return ti_index_build(argv[optind], &conf); } { // retrieve - BGZF *fp; - fp = bgzf_open(argv[optind], "r"); - if (fp == 0) { + tabix_t *t; + if ((t = ti_open(argv[optind], 0)) == 0) { fprintf(stderr, "[main] fail to open the data file.\n"); return 1; } @@ -94,36 +100,45 @@ int main(int argc, char *argv[]) ti_iter_t iter; const char *s; int len; - iter = ti_first(fp); - while ((s = ti_iter_read(iter, &len)) != 0) { + iter = ti_query(t, 0, 0, 0); + while ((s = ti_read(t, iter, &len)) != 0) { fputs(s, stdout); fputc('\n', stdout); } ti_iter_destroy(iter); } else { // retrieve from specified regions - ti_index_t *idx; int i; - idx = ti_index_load(argv[optind]); - if (idx == 0) { - bgzf_close(fp); - fprintf(stderr, "[main] fail to load the index.\n"); - return 1; - } + if ( ti_lazy_index_load(t) ) + { + fprintf(stderr,"[tabix] failed to load the index file.\n"); + return 1; + } + + ti_iter_t iter; + const char *s; + int len; + if ( print_header ) + { + // If requested, print the header lines here + iter = ti_query(t, 0, 0, 0); + while ((s = ti_read(t, iter, &len)) != 0) { + if ( *s != '#' ) break; + fputs(s, stdout); fputc('\n', stdout); + } + ti_iter_destroy(iter); + } for (i = optind + 1; i < argc; ++i) { int tid, beg, end; - if (ti_parse_region(idx, argv[i], &tid, &beg, &end) == 0) { - 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) { + if (ti_parse_region(t->idx, argv[i], &tid, &beg, &end) == 0) { + iter = ti_queryi(t, tid, beg, end); + while ((s = ti_read(t, 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"); + } + // else fprintf(stderr, "[main] invalid region: unknown target name or minus interval.\n"); } - ti_index_destroy(idx); } - bgzf_close(fp); + ti_close(t); } return 0; }