-
Notifications
You must be signed in to change notification settings - Fork 4
/
qtabix.cpp
174 lines (137 loc) · 3.4 KB
/
qtabix.cpp
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
/*
This file is part of CuteVCF.
Foobar is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
@author : Sacha Schutz <sacha@labsquare.org>
*/
#include "qtabix.h"
QTabix::QTabix()
{
}
QTabix::QTabix(const QString &filename)
{
setFilename(filename);
}
QTabix::~QTabix()
{
}
void QTabix::setRegion(const QString ®ion)
{
mRegions = region;
}
const QString &QTabix::region() const
{
return mRegions;
}
const QString &QTabix::filename() const
{
return mFilename;
}
void QTabix::setFilename(const QString &filename)
{
mFilename = filename;
if (QFile::exists(mFilename))
readInfo();
}
const QByteArray &QTabix::header() const
{
return mHeaders;
}
const QStringList &QTabix::chromosoms() const
{
return mChromosoms;
}
bool QTabix::readLineInto(QByteArray &line)
{
if (tbx_itr_next(fp, tbx, iter,&str) >= 0)
{
line = QByteArray(str.s);
return true;
}
tbx_itr_destroy(iter);
tbx_destroy(tbx);
return false;
}
void QTabix::buildIndex(const QString &filename)
{
// build vcf.gz.tbi
tbx_conf_t conf = tbx_conf_vcf;
int min_shift = 0;
tbx_index_build(filename.toStdString().c_str(), min_shift, &conf);
}
bool QTabix::open()
{
fp = NULL;
tbx = NULL;
iter = NULL;
// open file
fp = hts_open(mFilename.toStdString().c_str(),"r");
if (!fp){
qCritical()<<Q_FUNC_INFO<<"Could not read "<<mFilename;
return false;
}
// open index
tbx = tbx_index_load(mFilename.toStdString().c_str());
if (!tbx){
qCritical()<<Q_FUNC_INFO<<"Could not read .tbi index of "<<mFilename;
return false;
}
tbx->conf = tbx_conf_vcf;
str= {0,0,0};
// set region
iter = tbx_itr_querys(tbx, mRegions.toStdString().c_str());
if (!iter){
qCritical()<<"could not query the region "<<mRegions;
return false;
}
return true;
}
bool QTabix::readInfo()
{
fp = NULL;
tbx = NULL;
iter = NULL;
// open file
fp = hts_open(mFilename.toStdString().c_str(),"r");
if (!fp){
qCritical()<<Q_FUNC_INFO<<"Could not read "<<mFilename;
return false;
}
// open index
tbx = tbx_index_load(mFilename.toStdString().c_str());
if (!tbx){
qCritical()<<Q_FUNC_INFO<<"Could not read .tbi index of "<<mFilename;
return false;
}
// reads chromosomes
int nseq;
const char **seq = NULL;
seq = tbx_seqnames(tbx, &nseq);
mChromosoms.clear();
for (int i=0; i<nseq; ++i)
mChromosoms.append(seq[i]);
delete seq;
// reads headers
str= {0,0,0};
while ( hts_getline(fp, KS_SEP_LINE, &str) >= 0 )
{
if ( !str.l || str.s[0]!=tbx->conf.meta_char ) {
break;
}
else {
mHeaders.append(str.s);
mHeaders.append('\n');
}
}
tbx_itr_destroy(iter);
tbx_destroy(tbx);
return true;
}