forked from lh3/ropebwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgkquery.cpp
356 lines (317 loc) · 11.7 KB
/
cgkquery.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <sstream>
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
#include <string>
using std::string;
#include <ctime>
#include <cstring>
#include <getopt.h>
#include "CGkArray.h"
void print_usage(char const *name)
{
cerr << "usage: " << name << " [options] <index>" << endl
<< "Sample program to test out CGkArrays. Check README for more information." << endl;
}
// FIXME Clean up. Use for debugging only.
bool equalVectors(CGkArray::position_vector const & vector1, CGkArray::position_vector const &vector2)
{
if ( vector1.size() != vector2.size() )
return false;
return std::equal ( vector1.begin(), vector1.end(), vector2.begin() );
}
// FIXME Clean up. Use for debugging only.
void revstr(uchar *t, ulong n)
{
uchar c;
for (ulong i = 0; i < n / 2; ++i) {
c = t[i];
t[i] = t[n - i - 1];
t[n - i - 1] = c;
}
}
// FIXME Clean up. Use for debugging only.
void complstr(uchar *t, ulong n)
{
for (ulong i = 0; i < n; ++i) {
switch (t[i])
{
case 'A': t[i] = 'T'; break;
case 'T': t[i] = 'A'; break;
case 'C': t[i] = 'G'; break;
case 'G': t[i] = 'C'; break;
default: t[i] = 'N'; break;
}
}
}
int main(int argc, char **argv)
{
/**
* Parse command line parameters
*/
if (argc <= 1)
{
print_usage(argv[0]);
return 0;
}
bool verbose = false;
bool debug = false;
unsigned nqueries = 0;
#ifdef PARALLEL_SUPPORT
unsigned parallel = 1; /* Disabled for this simple example */
#endif
static struct option long_options[] =
{
{"nqueries", required_argument, 0, 'q'},
{"debug", no_argument, 0, 'D'},
{"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
int option_index = 0;
int c;
while ((c = getopt_long(argc, argv, "q:Dv", long_options, &option_index)) != -1)
{
switch(c)
{
case 'q':
nqueries = atoi(optarg); break;
case 'v':
verbose = true; break;
case 'D':
debug = true; break;
case '?':
case 'h':
print_usage(argv[0]);
return 1;
default:
print_usage(argv[0]);
std::abort ();
}
}
// Parse filenames
if (argc - optind != 1)
{
cerr << argv[0] << ": index filename is required" << endl;
print_usage(argv[0]);
return 1;
}
string indexfile = string(argv[optind++]);
if (nqueries < 1)
{
cerr << argv[0] << ": parameter -q,--nqueries <int> is mandatory, where <int> is greater than 0." << endl;
print_usage(argv[0]);
return 1;
}
/**
* Initialize shared data structures
*/
if (verbose) cerr << "Loading index " << indexfile << endl;
CGkArray *tc = new CGkArray(indexfile);
// Sanity checks
if (!tc) {
cerr << "readaligner: could not read index file " << indexfile << endl;
return 1;
}
/**
* Initialize shared input reader(s)
*/
/* TODO */
/**
* Initialize shared output writer
*/
/* TODO */
// Shared counters
unsigned total_found = 0;
ulong total_occs = 0;
time_t wctime = time(NULL);
#ifdef PARALLEL_SUPPORT
if (parallel != 0)
{
if (verbose && parallel == 1)
cerr << "Using only one core (default setting for this sample script)" << endl;
if (verbose && parallel != 1)
cerr << "Using " << parallel << " cores." << endl;
omp_set_num_threads(parallel);
}
else
if (verbose) cerr << "Using all " << omp_get_max_threads() << " cores available." << endl;
/* #pragma omp parallel */
#endif
/**
* Example on how to compute a so called read-coverage profile
*
* Input: Read number (here the first read, n:o 0).
* Output: k-mer coverage at each position of the read (in backwards order)
*/
if (debug)
{
unsigned readno = 0;
ulong readpos = tc->getLength(readno) - tc->getGkSize() - 1;
ulong tmp = tc->initMoveLeft(readno); // Initialize <internal value> for the given read n:o
CGkArray::sa_range sar = tc->moveLeft(tmp); // Get the SA range of the last k-mer
while (sar.first <= sar.second) // End of the traversal is signaled by an empty SA range
{
cout << "At read " << readno << " position " << readpos << " coverage is " << tc->countReads(sar) << endl;
// Take one step left (using <internal value>)
sar = tc->moveLeft(tmp);
--readpos;
}
assert(readpos == 0); // Arrived at the first position of the read
}
/**
* Read-coverage profile for the reverse complemented strand
*
* Input: Read number (here the first read, n:o 0).
* Output: k-mer coverage for the reverse complemented read
*/
if (debug)
{
// Read number is used here only to retrieve the read sequence
unsigned readno = 0;
// Retrieve the read sequence from the index, user must delete [] the buffer
uchar *read = tc->getRead(readno);
unsigned l = tc->getLength(readno) - 1;
ulong readpos = l - tc->getGkSize() + 1;
// Rev. compl. the given read
revstr(read, l);
complstr(read, l);
// Initialize <internal pointer> for an arbitrary pattern
CGkArray::internal_pointer tmp = tc->initMoveLeft(read);
CGkArray::sa_range sar = tc->moveLeft(tmp, read); // Get the SA range of the last k-mer
while (readpos)
{
--readpos;
cout << "At reverse complemented read " << readno << " position " << readpos
<< " coverage is " << tc->countReads(sar) << endl;
// Take one step left (using <internal pointer>)
sar = tc->moveLeft(tmp, read);
}
assert(readpos == 0); // Arrived at the first position of the read
delete [] read;
}
/**
* Test random positions
*/
srand(543262346);
wctime = time(NULL);
cerr << "Testing " << nqueries << " random positions for Q1..." << endl;
for (unsigned i = 0; i < nqueries; ++i)
{
ulong pos = rand() % tc->getLength();
if (!tc->isValidTextPos(pos))
pos -= tc->getGkSize(); // Make it a valid position (at least k nucleotides from the end of the read)
CGkArray::position_vector occs = tc->reportReads(pos);
if (debug)
{
uchar const *suffix = tc->getSuffix(tc->inverseSA(pos), tc->getGkSize());
for (CGkArray::position_vector::iterator it = occs.begin(); it != occs.end(); ++it)
cout << "Pattern " << suffix << " = " << it->first << "," << it->second << endl;
// Search with the uchar *
CGkArray::sa_range sar = tc->kmerToSARange(suffix);
CGkArray::position_vector occs2 = tc->reportReads(sar); // query with SA range
if (!equalVectors(occs,occs2))
{ cerr << "Q1 assert failed: vectors were not equal at i = " << i << endl; abort(); }
delete [] suffix;
}
if (!occs.empty())
++total_found;
total_occs += occs.size();
}
cerr << "Number of reported alignments: " << total_occs << endl
<< "Number of reads found: " << total_found << endl;
cerr << "Wall-clock time: " << std::difftime(time(NULL), wctime) << " seconds ("
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << endl;
srand(543262346);
cerr << "Testing " << nqueries << " random positions for Q2..." << endl;
total_found = 0;
wctime = time(NULL);
total_occs = 0;
for (unsigned i = 0; i < nqueries; ++i)
{
ulong pos = rand() % tc->getLength();
if (!tc->isValidTextPos(pos))
pos -= tc->getGkSize(); // Make it a valid position (at least k nucleotides from the end of the read)
ulong occs = tc->countReads(pos);
if (debug)
{
uchar const *suffix = tc->getSuffix(tc->inverseSA(pos), tc->getGkSize());
cout << "Pattern " << suffix << " = " << occs << endl;
// Search with the uchar *
CGkArray::sa_range sar = tc->kmerToSARange(suffix);
unsigned occs2 = tc->countReads(sar); // query with SA range
if (occs != occs2)
{ cerr << "Q2 assert failed: counts were not equal at i = " << i << endl; abort(); }
delete [] suffix;
}
if (occs)
++total_found;
total_occs += occs;
}
cerr << "Number of reported alignments: " << total_occs << endl
<< "Number of reads found: " << total_found << endl;
cerr << "Wall-clock time: " << std::difftime(time(NULL), wctime) << " seconds ("
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << endl;
srand(543262346);
cerr << "Testing " << nqueries << " random positions for Q3..." << endl;
total_found = 0;
wctime = time(NULL);
total_occs = 0;
for (unsigned i = 0; i < nqueries; ++i)
{
ulong pos = rand() % tc->getLength();
if (!tc->isValidTextPos(pos))
pos -= tc->getGkSize(); // Make it a valid position (at least k nucleotides from the end of the read)
CGkArray::position_vector occs = tc->reportOccs(pos);
if (debug)
{
uchar const *suffix = tc->getSuffix(tc->inverseSA(pos), tc->getGkSize());
for (CGkArray::position_vector::iterator it = occs.begin(); it != occs.end(); ++it)
cout << "Pattern " << suffix << " = " << it->first << "," << it->second << endl;
// Search with the uchar *
CGkArray::sa_range sar = tc->kmerToSARange(suffix);
CGkArray::position_vector occs2 = tc->reportOccs(sar); // query with SA range
if (!equalVectors(occs,occs2))
{ cerr << "Q3 assert failed: vectors were not equal at i = " << i << endl; abort(); }
delete [] suffix;
}
if (!occs.empty())
++total_found;
total_occs += occs.size();
}
cerr << "Number of reported alignments: " << total_occs << endl
<< "Number of reads found: " << total_found << endl;
cerr << "Wall-clock time: " << std::difftime(time(NULL), wctime) << " seconds ("
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << endl;
srand(543262346);
cerr << "Testing " << nqueries << " random positions for Q4..." << endl;
total_found = 0;
wctime = time(NULL);
total_occs = 0;
for (unsigned i = 0; i < nqueries; ++i)
{
ulong pos = rand() % tc->getLength();
if (!tc->isValidTextPos(pos))
pos -= tc->getGkSize(); // Make it a valid position (at least k nucleotides from the end of the read)
ulong occs = tc->countOccs(pos);
if (debug)
{
uchar const * suffix = tc->getSuffix(tc->inverseSA(pos), tc->getGkSize());
cout << "Testing " << suffix << " = " << occs << endl;
// Search with the uchar *
CGkArray::sa_range sar = tc->kmerToSARange(suffix);
unsigned occs2 = tc->countOccs(sar); // query with SA range
if (occs != occs2)
{ cerr << "Q4 assert failed: counts were not equal at i = " << i << endl; abort(); }
delete [] suffix;
}
if (occs)
++total_found;
total_occs += occs;
}
cerr << "Number of reported alignments: " << total_occs << endl
<< "Number of reads found: " << total_found << endl;
cerr << "Wall-clock time: " << std::difftime(time(NULL), wctime) << " seconds ("
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << endl;
delete tc;
}