1 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#include "bcftools.pysam.h"
/* vcfindex.c -- Index bgzip compressed VCF/BCF files for random access.
Copyright (C) 2014-2016 Genome Research Ltd.
Author: Shane McCarthy <sm15@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <htslib/vcf.h>
#include <htslib/tbx.h>
#include <sys/stat.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <htslib/kstring.h>
#include <htslib/bgzf.h>
#include "bcftools.h"
#define BCF_LIDX_SHIFT 14
static void usage(void)
{
fprintf(bcftools_stderr, "\n");
fprintf(bcftools_stderr, "About: Index bgzip compressed VCF/BCF files for random access.\n");
fprintf(bcftools_stderr, "Usage: bcftools index [options] <in.bcf>|<in.vcf.gz>\n");
fprintf(bcftools_stderr, "\n");
fprintf(bcftools_stderr, "Indexing options:\n");
fprintf(bcftools_stderr, " -c, --csi generate CSI-format index for VCF/BCF files [default]\n");
fprintf(bcftools_stderr, " -f, --force overwrite index if it already exists\n");
fprintf(bcftools_stderr, " -m, --min-shift INT set minimal interval size for CSI indices to 2^INT [14]\n");
fprintf(bcftools_stderr, " -o, --output-file FILE optional output index file name\n");
fprintf(bcftools_stderr, " -t, --tbi generate TBI-format index for VCF files\n");
fprintf(bcftools_stderr, " --threads INT use multithreading with INT worker threads [0]\n");
fprintf(bcftools_stderr, "\n");
fprintf(bcftools_stderr, "Stats options:\n");
fprintf(bcftools_stderr, " -n, --nrecords print number of records based on existing index file\n");
fprintf(bcftools_stderr, " -s, --stats print per contig stats based on existing index file\n");
fprintf(bcftools_stderr, "\n");
exit(1);
}
int vcf_index_stats(char *fname, int stats)
{
const char **seq;
int i, nseq;
tbx_t *tbx = NULL;
hts_idx_t *idx = NULL;
htsFile *fp = hts_open(fname,"r");
if ( !fp ) { fprintf(bcftools_stderr,"Could not read %s\n", fname); return 1; }
bcf_hdr_t *hdr = bcf_hdr_read(fp);
if ( !hdr ) { fprintf(bcftools_stderr,"Could not read the header: %s\n", fname); return 1; }
if ( hts_get_format(fp)->format==vcf )
{
tbx = tbx_index_load(fname);
if ( !tbx ) { fprintf(bcftools_stderr,"Could not load index for VCF: %s\n", fname); return 1; }
}
else if ( hts_get_format(fp)->format==bcf )
{
idx = bcf_index_load(fname);
if ( !idx ) { fprintf(bcftools_stderr,"Could not load index for BCF file: %s\n", fname); return 1; }
}
else
{
fprintf(bcftools_stderr,"Could not detect the file type as VCF or BCF: %s\n", fname);
return 1;
}
seq = tbx ? tbx_seqnames(tbx, &nseq) : bcf_index_seqnames(idx, hdr, &nseq);
uint64_t sum = 0;
for (i=0; i<nseq; i++)
{
uint64_t records, v;
hts_idx_get_stat(tbx ? tbx->idx : idx, i, &records, &v);
sum+=records;
if (stats&2 || !records) continue;
bcf_hrec_t *hrec = bcf_hdr_get_hrec(hdr, BCF_HL_CTG, "ID", seq[i], NULL);
int hkey = hrec ? bcf_hrec_find_key(hrec, "length") : -1;
fprintf(bcftools_stdout, "%s\t%s\t%" PRIu64 "\n", seq[i], hkey<0?".":hrec->vals[hkey], records);
}
if (!sum)
{
// No counts found.
// Is this because index version has no stored count data, or no records?
bcf1_t *rec = bcf_init1();
if (bcf_read1(fp, hdr, rec) >= 0)
{
fprintf(bcftools_stderr,"index of %s does not contain any count metadata. Please re-index with a newer version of bcftools or tabix.\n", fname);
return 1;
}
bcf_destroy1(rec);
}
if (stats&2) fprintf(bcftools_stdout, "%" PRIu64 "\n", sum);
free(seq);
if ( hts_close(fp)!=0 ) error("[%s] Error: close failed\n", __func__);
bcf_hdr_destroy(hdr);
if (tbx)
tbx_destroy(tbx);
if (idx)
hts_idx_destroy(idx);
return 0;
}
int main_vcfindex(int argc, char *argv[])
{
int c, force = 0, tbi = 0, stats = 0, n_threads = 0;
int min_shift = BCF_LIDX_SHIFT;
char *outfn = NULL;
static struct option loptions[] =
{
{"csi",no_argument,NULL,'c'},
{"tbi",no_argument,NULL,'t'},
{"force",no_argument,NULL,'f'},
{"min-shift",required_argument,NULL,'m'},
{"stats",no_argument,NULL,'s'},
{"nrecords",no_argument,NULL,'n'},
{"threads",required_argument,NULL,9},
{"output-file",required_argument,NULL,'o'},
{NULL, 0, NULL, 0}
};
char *tmp;
while ((c = getopt_long(argc, argv, "ctfm:sno:", loptions, NULL)) >= 0)
{
switch (c)
{
case 'c': tbi = 0; break;
case 't': tbi = 1; min_shift = 0; break;
case 'f': force = 1; break;
case 'm':
min_shift = strtol(optarg,&tmp,10);
if ( *tmp ) error("Could not parse argument: --min-shift %s\n", optarg);
break;
case 's': stats |= 1; break;
case 'n': stats |= 2; break;
case 9:
n_threads = strtol(optarg,&tmp,10);
if ( *tmp ) error("Could not parse argument: --threads %s\n", optarg);
break;
case 'o': outfn = optarg; break;
default: usage();
}
}
if (stats>2)
{
fprintf(bcftools_stderr, "[E::%s] expected only one of --stats or --nrecords options\n", __func__);
return 1;
}
if (tbi && min_shift>0)
{
fprintf(bcftools_stderr, "[E::%s] min-shift option only expected for CSI indices \n", __func__);
return 1;
}
if (min_shift < 0 || min_shift > 30)
{
fprintf(bcftools_stderr, "[E::%s] expected min_shift in range [0,30] (%d)\n", __func__, min_shift);
return 1;
}
char *fname = NULL;
if ( optind>=argc )
{
if ( !isatty(fileno((FILE *)stdin)) ) fname = "-"; // reading from stdin
else usage();
}
else fname = argv[optind];
if (stats) return vcf_index_stats(fname, stats);
kstring_t idx_fname = {0,0,0};
if (outfn)
kputs(outfn,&idx_fname);
else
{
if (!strcmp(fname, "-")) { fprintf(bcftools_stderr, "[E::%s] must specify an output path for index file when reading VCF/BCF from stdin\n", __func__); return 1; }
ksprintf(&idx_fname, "%s.%s", fname, tbi ? "tbi" : "csi");
}
if (!force)
{
// Before complaining about existing index, check if the VCF file isn't newer.
struct stat stat_tbi, stat_file;
if ( stat(idx_fname.s, &stat_tbi)==0 )
{
stat(fname, &stat_file);
if ( stat_file.st_mtime <= stat_tbi.st_mtime )
{
fprintf(bcftools_stderr,"[E::%s] the index file exists. Please use '-f' to overwrite %s\n", __func__, idx_fname.s);
free(idx_fname.s);
return 1;
}
}
// check for truncated files, allow only with -f
BGZF *fp = bgzf_open(fname, "r");
if ( !fp ) error("index: failed to open %s\n", fname);
if ( bgzf_compression(fp)!=2 ) error("index: the file is not BGZF compressed, cannot index: %s\n", fname);
if ( bgzf_check_EOF(fp)!=1 ) error("index: the input is probably truncated, use -f to index anyway: %s\n", fname);
if ( bgzf_close(fp)!=0 ) error("index: close failed: %s\n", fname);
}
int ret = bcf_index_build3(fname, idx_fname.s, min_shift, n_threads);
free(idx_fname.s);
if (ret != 0) {
if (ret == -2)
error("index: failed to open \"%s\"\n", fname);
else if (ret == -3)
error("index: \"%s\" is in a format that cannot be usefully indexed\n", fname);
else
error("index: failed to create index for \"%s\"\n", fname);
}
return 0;
}
|