File: 02_Replace_libtabix_by_libhts.patch

package info (click to toggle)
fastqtl 2.184%2Bv7%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,428 kB
  • sloc: cpp: 3,102; python: 323; makefile: 82; sh: 19; javascript: 3
file content (216 lines) | stat: -rw-r--r-- 5,667 bytes parent folder | download
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
Author: Dylan Aïssi <bob.dybian@gmail.com>
Description: Merge src/utils/tabix.*pp with upstream (libtabixpp 1.0.0) in order to use libhts instead libtabix. 
Last-Update: 2016-04-29
Forwarded: no


--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,7 @@
   LIB_TABX=$(PATH_TABX)/libtabix.a
 else
   LIB_MATH=-lRmath
-  LIB_TABX=-ltabix
+  LIB_TABX=-lhts
 endif
 LIB_MACX=-L/usr/local/lib/
 
--- a/src/utils/tabix.cpp
+++ b/src/utils/tabix.cpp
@@ -8,6 +8,7 @@
 Tabix::Tabix(void) { }
 
 Tabix::Tabix(string& file) {
+    has_jumped = false;
     filename = file;
     const char* cfilename = file.c_str();
     struct stat stat_tbi,stat_vcf;
@@ -30,78 +31,99 @@
     }
     free(fnidx);
 
-    if ((t = ti_open(cfilename, 0)) == 0) {
+    if ((fn = hts_open(cfilename, "r")) == 0) {
         cerr << "[tabix++] fail to open the data file." << endl;
         exit(1);
     }
 
-    if (ti_lazy_index_load(t) < 0) {
+    if ((tbx = tbx_index_load(cfilename)) == NULL) {
         cerr << "[tabix++] failed to load the index file." << endl;
         exit(1);
     }
 
-    idxconf = ti_get_conf(t->idx);
+    int nseq;
+    const char** seq = tbx_seqnames(tbx, &nseq);
+    for (int i=0; i<nseq; i++) {
+        chroms.push_back(seq[i]);
+    }
+    free(seq);
+
+    idxconf = &tbx_conf_vcf;
 
     // set up the iterator, defaults to the beginning
-    iter = ti_query(t, 0, 0, 0);
+    current_chrom = chroms.begin();
+    iter = tbx_itr_querys(tbx, current_chrom->c_str());
 
 }
 
 Tabix::~Tabix(void) {
-    ti_iter_destroy(iter);
-    ti_close(t);
+    tbx_itr_destroy(iter);
+    tbx_destroy(tbx);
 }
 
 void Tabix::getHeader(string& header) {
     header.clear();
-    ti_iter_destroy(iter);
-    iter = ti_query(t, 0, 0, 0);
-    const char* s;
-    int len;
-    while ((s = ti_read(t, iter, &len)) != 0) {
-        if ((int)(*s) != idxconf->meta_char) {
-            firstline = string(s); // stash this line
+    kstring_t str = {0,0,0};
+    while ( hts_getline(fn, KS_SEP_LINE, &str) >= 0 ) {
+        if ( !str.l || str.s[0]!=tbx->conf.meta_char ) {
             break;
         } else {
-            header += string(s);
+            header += string(str.s);
             header += "\n";
         }
     }
+    // set back to start
+    current_chrom = chroms.begin();
+    if (iter) tbx_itr_destroy(iter);
+    iter = tbx_itr_querys(tbx, current_chrom->c_str());
 }
 
 void Tabix::getLastHeader(string & header) {
-	header.clear();
-	ti_iter_destroy(iter);
-	iter = ti_query(t, 0, 0, 0);
-	const char* s;
-	int len;
-	while ((s = ti_read(t, iter, &len)) != 0) {
-		if ((int)(*s) != idxconf->meta_char) {
-			firstline = string(s); // stash this line
-			break;
-		} else header = string(s);
-	}
+    header.clear();
+    kstring_t str = {0,0,0};
+    while ( hts_getline(fn, KS_SEP_LINE, &str) >= 0 ) {
+        if ( !str.l || str.s[0]!=tbx->conf.meta_char ) {
+            break;
+        } else header = string(str.s);
+    }
+    // set back to start
+    current_chrom = chroms.begin();
+    if (iter) tbx_itr_destroy(iter);
+    iter = tbx_itr_querys(tbx, current_chrom->c_str());
 }
 
 bool Tabix::setRegion(string region) {
-    if (ti_parse_region(t->idx, region.c_str(), &tid, &beg, &end) == 0) {
-        firstline.clear();
-        ti_iter_destroy(iter);
-        iter = ti_queryi(t, tid, beg, end);
-        return true;
-    } else return false;
+    tbx_itr_destroy(iter);
+    iter = tbx_itr_querys(tbx, region.c_str());
+    has_jumped = true;
+    return true;
 }
 
 bool Tabix::getNextLine(string & line) {
-    const char* s;
-    int len;
-    if (!firstline.empty()) {
-        line = firstline; // recovers line read if header is parsed
-        firstline.clear();
-        return true;
-    }
-    if ((s = ti_read(t, iter, &len)) != 0) {
-        line = string(s);
-        return true;
-    } else return false;
+    kstring_t str = {0,0,0};
+    if (has_jumped) {
+        if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) {
+            line = string(str.s);
+            return true;
+        } else return false;
+    } else { // step through all sequences in the file
+        // we've never jumped, so read everything
+        if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) {
+            line = string(str.s);
+            return true;
+        } else {
+            ++current_chrom;
+            while (current_chrom != chroms.end()) {
+                tbx_itr_destroy(iter);
+                iter = tbx_itr_querys(tbx, current_chrom->c_str());
+                if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) {
+                    line = string(str.s);
+                    return true;
+                } else {
+                    ++current_chrom;
+                }
+            }
+            return false;
+        }
+    }
 }
--- a/src/utils/tabix.hpp
+++ b/src/utils/tabix.hpp
@@ -9,24 +9,31 @@
 #include <string>
 #include <stdlib.h>
 #include <sys/stat.h>
-#include <bgzf.h>
-#include <tabix.h>
+#include <htslib/bgzf.h>
+#include <htslib/tbx.h>
+#include <htslib/kseq.h>
 #include <iostream>
+#include <cstring>
+#include <vector>
 
 
 using namespace std;
 
 class Tabix {
 
-    tabix_t *t;
-    ti_iter_t iter;
-    const ti_conf_t *idxconf;
+    htsFile* fn;
+    tbx_t* tbx;
+    hts_itr_t* iter;
+    const tbx_conf_t *idxconf;
     int tid, beg, end;
     string firstline;
+    bool has_jumped;
+    vector<string>::iterator current_chrom;
 
 public:
 
     string filename;
+    vector<string> chroms;
 
     Tabix(void);
     Tabix(string& file);
@@ -36,6 +43,7 @@
     void getLastHeader(string & header);
     bool setRegion(string region);
     bool getNextLine(string& line);
+
 };
 
 #endif