| 1 |
#include "bcftools.pysam.h" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <string.h> |
| 32 |
#include <strings.h> |
| 33 |
#include <htslib/bgzf.h> |
| 34 |
#include <htslib/tbx.h> |
| 35 |
|
| 36 |
int main_tabix(int argc, char *argv[]) |
| 37 |
{ |
| 38 |
int c, min_shift = -1, is_force = 0, is_all = 0, detect = 1; |
| 39 |
tbx_conf_t conf = tbx_conf_gff; |
| 40 |
while ((c = getopt(argc, argv, "0fap:s:b:e:S:c:m:")) >= 0) |
| 41 |
if (c == '0') conf.preset |= TBX_UCSC; |
| 42 |
else if (c == 'f') is_force = 1; |
| 43 |
else if (c == 'a') is_all = 1; |
| 44 |
else if (c == 'm') min_shift = atoi(optarg); |
| 45 |
else if (c == 's') conf.sc = atoi(optarg); |
| 46 |
else if (c == 'b') conf.bc = atoi(optarg); |
| 47 |
else if (c == 'e') conf.ec = atoi(optarg); |
| 48 |
else if (c == 'c') conf.meta_char = *optarg; |
| 49 |
else if (c == 'S') conf.line_skip = atoi(optarg); |
| 50 |
else if (c == 'p') { |
| 51 |
if (strcmp(optarg, "gff") == 0) conf = tbx_conf_gff; |
| 52 |
else if (strcmp(optarg, "bed") == 0) conf = tbx_conf_bed; |
| 53 |
else if (strcmp(optarg, "sam") == 0) conf = tbx_conf_sam; |
| 54 |
else if (strcmp(optarg, "vcf") == 0) conf = tbx_conf_vcf; |
| 55 |
else { |
| 56 |
fprintf(bcftools_stderr, "The type '%s' not recognised\n", optarg); |
| 57 |
return 1; |
| 58 |
detect = 0; |
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
if (optind == argc) { |
| 63 |
fprintf(bcftools_stderr, "\nUsage: bcftools tabix [options] <in.gz> [reg1 [...]]\n\n"); |
| 64 |
fprintf(bcftools_stderr, "Options: -p STR preset: gff, bed, sam or vcf [gff]\n"); |
| 65 |
fprintf(bcftools_stderr, " -s INT column number for sequence names (suppressed by -p) [1]\n"); |
| 66 |
fprintf(bcftools_stderr, " -b INT column number for region start [4]\n"); |
| 67 |
fprintf(bcftools_stderr, " -e INT column number for region end (if no end, set INT to -b) [5]\n"); |
| 68 |
fprintf(bcftools_stderr, " -0 specify coordinates are zero-based\n"); |
| 69 |
fprintf(bcftools_stderr, " -S INT skip first INT lines [0]\n"); |
| 70 |
fprintf(bcftools_stderr, " -c CHAR skip lines starting with CHAR [null]\n"); |
| 71 |
fprintf(bcftools_stderr, " -a print all records\n"); |
| 72 |
fprintf(bcftools_stderr, " -f force to overwrite existing index\n"); |
| 73 |
fprintf(bcftools_stderr, " -m INT set the minimal interval size to 1<<INT; 0 for the old tabix index [0]\n"); |
| 74 |
fprintf(bcftools_stderr, "\n"); |
| 75 |
return 1; |
| 76 |
} |
| 77 |
if (is_all) { |
| 78 |
kstring_t s; |
| 79 |
BGZF *fp; |
| 80 |
s.l = s.m = 0; s.s = 0; |
| 81 |
fp = bgzf_open(argv[optind], "r"); |
| 82 |
while (bgzf_getline(fp, '\n', &s) >= 0) bcftools_puts(s.s); |
| 83 |
bgzf_close(fp); |
| 84 |
free(s.s); |
| 85 |
} else if (optind + 2 > argc) { |
| 86 |
if ( detect ) |
| 87 |
{ |
| 88 |
|
| 89 |
int l = strlen(argv[optind]); |
| 90 |
if (l>=7 && strcasecmp(argv[optind]+l-7, ".gff.gz") == 0) conf = tbx_conf_gff; |
| 91 |
else if (l>=7 && strcasecmp(argv[optind]+l-7, ".bed.gz") == 0) conf = tbx_conf_bed; |
| 92 |
else if (l>=7 && strcasecmp(argv[optind]+l-7, ".sam.gz") == 0) conf = tbx_conf_sam; |
| 93 |
else if (l>=7 && strcasecmp(argv[optind]+l-7, ".vcf.gz") == 0) conf = tbx_conf_vcf; |
| 94 |
} |
| 95 |
|
| 96 |
if (!is_force) { |
| 97 |
char *fn; |
| 98 |
FILE *fp; |
| 99 |
fn = (char*)malloc(strlen(argv[optind]) + 5); |
| 100 |
strcat(strcpy(fn, argv[optind]), min_shift <= 0? ".tbi" : ".csi"); |
| 101 |
if ((fp = fopen(fn, "rb")) != 0) { |
| 102 |
fclose(fp); |
| 103 |
free(fn); |
| 104 |
fprintf(bcftools_stderr, "[E::%s] the index file exists; use option '-f' to overwrite\n", __func__); |
| 105 |
return 1; |
| 106 |
} |
| 107 |
free(fn); |
| 108 |
} |
| 109 |
if ( tbx_index_build(argv[optind], min_shift, &conf) ) |
| 110 |
{ |
| 111 |
fprintf(bcftools_stderr,"tbx_index_build failed: Is the file bgzip-compressed? Was wrong -p [type] option used?\n"); |
| 112 |
return 1; |
| 113 |
} |
| 114 |
} else { |
| 115 |
tbx_t *tbx; |
| 116 |
BGZF *fp; |
| 117 |
kstring_t s; |
| 118 |
int i; |
| 119 |
if ((tbx = tbx_index_load(argv[optind])) == 0) return 1; |
| 120 |
if ((fp = bgzf_open(argv[optind], "r")) == 0) return 1; |
| 121 |
s.s = 0; s.l = s.m = 0; |
| 122 |
for (i = optind + 1; i < argc; ++i) { |
| 123 |
hts_itr_t *itr; |
| 124 |
if ((itr = tbx_itr_querys(tbx, argv[i])) == 0) continue; |
| 125 |
while (tbx_bgzf_itr_next(fp, tbx, itr, &s) >= 0) bcftools_puts(s.s); |
| 126 |
tbx_itr_destroy(itr); |
| 127 |
} |
| 128 |
free(s.s); |
| 129 |
bgzf_close(fp); |
| 130 |
tbx_destroy(tbx); |
| 131 |
} |
| 132 |
return 0; |
| 133 |
} |