Debian Codegraph @

src/utils/tabix.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 filename = file;
12 const char* cfilename = file.c_str();
13 struct stat stat_tbi,stat_vcf;
14 char *fnidx = (char*) calloc(strlen(cfilename) + 5, 1);
15 strcat(strcpy(fnidx, cfilename), ".tbi");
16 if ( bgzf_is_bgzf(cfilename)!=1 )
17 {
18 cerr << "[tabix++] was bgzip used to compress this file? " << file << endl;
19 free(fnidx);
20 exit(1);
21 }
22 // Common source of errors: new VCF is used with an old index
23 stat(fnidx, &stat_tbi);
24 stat(cfilename, &stat_vcf);
25 if ( stat_vcf.st_mtime > stat_tbi.st_mtime )
26 {
27 cerr << "[tabix++] the index file is older than the vcf file. Please use '-f' to overwrite or reindex." << endl;
28 free(fnidx);
29 exit(1);
30 }
31 free(fnidx);
32
33 if ((t = ti_open(cfilename, 0)) == 0) {
34 cerr << "[tabix++] fail to open the data file." << endl;
35 exit(1);
36 }
37
38 if (ti_lazy_index_load(t) < 0) {
39 cerr << "[tabix++] failed to load the index file." << endl;
40 exit(1);
41 }
42
43 idxconf = ti_get_conf(t->idx);
44
45 // set up the iterator, defaults to the beginning
46 iter = ti_query(t, 0, 0, 0);
47
48 }
49
50 Tabix::~Tabix(void) {
51 ti_iter_destroy(iter);
52 ti_close(t);
53 }
54
55 void Tabix::getHeader(string& header) {
56 header.clear();
57 ti_iter_destroy(iter);
58 iter = ti_query(t, 0, 0, 0);
59 const char* s;
60 int len;
61 while ((s = ti_read(t, iter, &len)) != 0) {
62 if ((int)(*s) != idxconf->meta_char) {
63 firstline = string(s); // stash this line
64 break;
65 } else {
66 header += string(s);
67 header += "\n";
68 }
69 }
70 }
71
72 void Tabix::getLastHeader(string & header) {
73 header.clear();
74 ti_iter_destroy(iter);
75 iter = ti_query(t, 0, 0, 0);
76 const char* s;
77 int len;
78 while ((s = ti_read(t, iter, &len)) != 0) {
79 if ((int)(*s) != idxconf->meta_char) {
80 firstline = string(s); // stash this line
81 break;
82 } else header = string(s);
83 }
84 }
85
86 bool Tabix::setRegion(string region) {
87 if (ti_parse_region(t->idx, region.c_str(), &tid, &beg, &end) == 0) {
88 firstline.clear();
89 ti_iter_destroy(iter);
90 iter = ti_queryi(t, tid, beg, end);
91 return true;
92 } else return false;
93 }
94
95 bool Tabix::getNextLine(string & line) {
96 const char* s;
97 int len;
98 if (!firstline.empty()) {
99 line = firstline; // recovers line read if header is parsed
100 firstline.clear();
101 return true;
102 }
103 if ((s = ti_read(t, iter, &len)) != 0) {
104 line = string(s);
105 return true;
106 } else return false;
107 }