| 1 |
/* |
| 2 |
* This is a C++ wrapper around tabix project which abstracts some of the details of opening and jumping in tabix-indexed files. |
| 3 |
* Author: Erik Garrison [email protected] |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "tabix.hpp" |
| 7 |
|
| 8 |
Tabix::Tabix(void) { } |
| 9 |
|
| 10 |
Tabix::Tabix(string& file) { |
| •11 |
has_jumped = false; |
| 12 |
filename = file; |
| 13 |
const char* cfilename = file.c_str(); |
| 14 |
struct stat stat_tbi,stat_vcf; |
| 15 |
char *fnidx = (char*) calloc(strlen(cfilename) + 5, 1); |
| 16 |
strcat(strcpy(fnidx, cfilename), ".tbi"); |
| 17 |
if ( bgzf_is_bgzf(cfilename)!=1 ) |
| 18 |
{ |
| 19 |
cerr << "[tabix++] was bgzip used to compress this file? " << file << endl; |
| 20 |
free(fnidx); |
| 21 |
exit(1); |
| 22 |
} |
| 23 |
// Common source of errors: new VCF is used with an old index |
| 24 |
stat(fnidx, &stat_tbi); |
| 25 |
stat(cfilename, &stat_vcf); |
| 26 |
if ( stat_vcf.st_mtime > stat_tbi.st_mtime ) |
| 27 |
{ |
| 28 |
cerr << "[tabix++] the index file is older than the vcf file. Please use '-f' to overwrite or reindex." << endl; |
| 29 |
free(fnidx); |
| 30 |
exit(1); |
| 31 |
} |
| 32 |
free(fnidx); |
| 33 |
|
| •34 |
if ((fn = hts_open(cfilename, "r")) == 0) { |
| 35 |
cerr << "[tabix++] fail to open the data file." << endl; |
| 36 |
exit(1); |
| 37 |
} |
| 38 |
|
| •39 |
if ((tbx = tbx_index_load(cfilename)) == NULL) { |
| 40 |
cerr << "[tabix++] failed to load the index file." << endl; |
| 41 |
exit(1); |
| 42 |
} |
| 43 |
|
| •44 |
int nseq; |
| 45 |
const char** seq = tbx_seqnames(tbx, &nseq); |
| 46 |
for (int i=0; i<nseq; i++) { |
| 47 |
chroms.push_back(seq[i]); |
| 48 |
} |
| 49 |
free(seq); |
| 50 |
|
| 51 |
idxconf = &tbx_conf_vcf; |
| 52 |
|
| 53 |
// set up the iterator, defaults to the beginning |
| •54 |
current_chrom = chroms.begin(); |
| 55 |
iter = tbx_itr_querys(tbx, current_chrom->c_str()); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
Tabix::~Tabix(void) { |
| •60 |
tbx_itr_destroy(iter); |
| 61 |
tbx_destroy(tbx); |
| 62 |
} |
| 63 |
|
| 64 |
void Tabix::getHeader(string& header) { |
| 65 |
header.clear(); |
| •66 |
kstring_t str = {0,0,0}; |
| 67 |
while ( hts_getline(fn, KS_SEP_LINE, &str) >= 0 ) { |
| 68 |
if ( !str.l || str.s[0]!=tbx->conf.meta_char ) { |
| 69 |
break; |
| 70 |
} else { |
| •71 |
header += string(str.s); |
| 72 |
header += "\n"; |
| 73 |
} |
| 74 |
} |
| •75 |
// set back to start |
| 76 |
current_chrom = chroms.begin(); |
| 77 |
if (iter) tbx_itr_destroy(iter); |
| 78 |
iter = tbx_itr_querys(tbx, current_chrom->c_str()); |
| 79 |
} |
| 80 |
|
| 81 |
void Tabix::getLastHeader(string & header) { |
| •82 |
header.clear(); |
| 83 |
kstring_t str = {0,0,0}; |
| 84 |
while ( hts_getline(fn, KS_SEP_LINE, &str) >= 0 ) { |
| 85 |
if ( !str.l || str.s[0]!=tbx->conf.meta_char ) { |
| 86 |
break; |
| 87 |
} else header = string(str.s); |
| 88 |
} |
| 89 |
// set back to start |
| 90 |
current_chrom = chroms.begin(); |
| 91 |
if (iter) tbx_itr_destroy(iter); |
| 92 |
iter = tbx_itr_querys(tbx, current_chrom->c_str()); |
| 93 |
} |
| 94 |
|
| 95 |
bool Tabix::setRegion(string region) { |
| •96 |
tbx_itr_destroy(iter); |
| 97 |
iter = tbx_itr_querys(tbx, region.c_str()); |
| 98 |
has_jumped = true; |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
bool Tabix::getNextLine(string & line) { |
| •103 |
kstring_t str = {0,0,0}; |
| 104 |
if (has_jumped) { |
| 105 |
if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) { |
| 106 |
line = string(str.s); |
| 107 |
return true; |
| 108 |
} else return false; |
| 109 |
} else { // step through all sequences in the file |
| 110 |
// we've never jumped, so read everything |
| 111 |
if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) { |
| 112 |
line = string(str.s); |
| 113 |
return true; |
| 114 |
} else { |
| 115 |
++current_chrom; |
| 116 |
while (current_chrom != chroms.end()) { |
| 117 |
tbx_itr_destroy(iter); |
| 118 |
iter = tbx_itr_querys(tbx, current_chrom->c_str()); |
| 119 |
if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) { |
| 120 |
line = string(str.s); |
| 121 |
return true; |
| 122 |
} else { |
| 123 |
++current_chrom; |
| 124 |
} |
| 125 |
} |
| 126 |
return false; |
| 127 |
} |
| 128 |
} |
| 129 |
} |