-
Notifications
You must be signed in to change notification settings - Fork 5
/
builder.cpp
472 lines (416 loc) · 12.9 KB
/
builder.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "TextCollectionBuilder.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstring>
#include <cassert>
#include <getopt.h>
using namespace std;
/**
* Flags set based on command line parameters
*/
bool verbose = false;
bool color = false; // Convert DNA to color codes?
bool rotation = false; // Build rotation index?
unsigned patlen = 0; // pattern length for rotation index
void revstr(std::string &t)
{
char c;
std::size_t n = t.size();
for (std::size_t i = 0; i < n / 2; ++i) {
c = t[i];
t[i] = t[n - i - 1];
t[n - i - 1] = c;
}
}
void complement(std::string &t)
{
for (std::string::iterator it = t.begin(); it != t.end(); ++it)
{
switch (*it)
{
case('T'):
*it = 'A';
break;
case('G'):
*it = 'C';
break;
case('C'):
*it = 'G';
break;
case('A'):
*it = 'T';
break;
}
}
}
/**
* Normalize to upper-case
*/
void normalize(std::string &t, string const &name)
{
string invalidSymbols = "";
for (std::string::iterator it = t.begin(); it != t.end(); ++it)
{
switch (*it)
{
case('a'):
*it = 'A';
break;
case('c'):
*it = 'C';
break;
case('g'):
*it = 'G';
break;
case('t'):
*it = 'T';
break;
case('n'):
*it = 'N';
break;
case('A'):
case('C'):
case('G'):
case('T'):
case('N'):
break;
case('0'):
case('1'):
case('2'):
case('3'):
case('.'):
break;
default:
size_t pos = invalidSymbols.find_first_of(*it);
if (pos == string::npos)
invalidSymbols += *it;
*it = 'N';
break;
}
}
if (invalidSymbols != "")
std::cerr << "Warning: sequence " << name << " contains invalid symbol(s): " << invalidSymbols << std::endl;
}
bool is_color_bases(string const &text)
{
string::const_iterator it = text.begin();
++it; // Skip first (possibly an adaptor symbol)
for (;it != text.end(); ++it)
if (*it != '0' && *it != '1' && *it != '2' && *it != '3' && *it != '.')
return false;
return true;
}
bool is_dna_bases(string const &text)
{
for (string::const_iterator it = text.begin();it != text.end(); ++it)
if (*it != 'A' && *it != 'C' && *it != 'G' && *it != 'T' && *it != 'N')
return false;
return true;
}
void dna_to_color(string &text, string const &name)
{
string output = "";
string::const_iterator it = text.begin();
if (is_color_bases(text))
{
if (verbose)
cerr << "sequence " << name << " already in color bases, skipping conversion" << endl;
return;
}
// We have to assume DNA alphabet from here on (no mixed solid/dna symbols)
if (!is_dna_bases(text))
{
cerr << "error: sequence " << name << " contains color (0123.) and DNA bases (ACGTN) mixed together!" << endl;
abort();
}
char code[5][5] = {{'0', '1', '2', '3', '.'},
{'1', '0', '3', '2', '.'},
{'2', '3', '0', '1', '.'},
{'3', '2', '1', '0', '.'},
{'.', '.', '.', '.', '.'}};
int map[256];
for (unsigned i = 0; i < 256; ++i)
map[i] = -1;
map[(int)'A'] = 0;
map[(int)'C'] = 1;
map[(int)'G'] = 2;
map[(int)'T'] = 3;
map[(int)'N'] = 4;
int prev = *(it++);
prev = map[prev];
assert(prev != -1);
if (it == text.end())
{
cerr << "Warning: sequence " << name << " was shorter than 2 bp!" << endl;
text = "";
return;
}
while (it != text.end())
{
int c = *(it++);
c = map[c];
assert(c != -1);
output += code[prev][c];
prev = c;
}
text = output;
}
void transform(string &text, string const &name, bool reverse)
{
assert(!(reverse && rotation));
normalize(text, name);
// Color transform first (deprecated)
if (color)
dna_to_color(text, name);
// Appending a reverse complement
string revcmpl(text);
revstr(revcmpl);
complement(revcmpl);
text.append(1, '-');
text.append(revcmpl);
// Store everything reversed
revstr(text);
}
void build(istream *input, string const &outputfile, unsigned samplerate, bool reverse, ulong estimatedLength, time_t wctime)
{
TextCollectionBuilder *tcb = new TextCollectionBuilder(samplerate, estimatedLength);
ulong j = 0;
unsigned i = 0;
string text = "";
string name = "undef";
string row;
while (getline(*input, row).good())
{
if (row[0] == '>')
{
row = row.substr(row.find_first_not_of(" \t", 1));
row = row.substr(0, row.find_first_of(" \t"));
i ++;
j += text.size();
// If no title given, generate simple id numbers
if (row.empty())
{
stringstream ss;
ss << i-2;
row = ss.str();
}
if (verbose && i % 1000000 == 0)
{
cerr << "Inserting: " << row << " ("
<< j/(1024*1024) << " MB, ";
// if (estimatedLength)
// cerr << (100*j/estimatedLength) << "%, ";
cerr << "elapsed " << std::difftime(time(NULL), wctime) << " s, "
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << endl;
}
if (text.size() > 0)
{
assert(name != "undef");
transform(text, name, reverse);
if (reverse)
tcb->InsertText((uchar const *)text.c_str());
else
tcb->InsertText((uchar const *)text.c_str(), name);
}
text.clear();
name = row; // Cache the fasta title
}
else
text.append(row);
}
// Flush the text buffer
j += text.size();
if (text.size() > 0)
{
transform(text, name, reverse);
if (reverse)
tcb->InsertText((uchar const *)text.c_str());
else
tcb->InsertText((uchar const *)text.c_str(), name);
}
text.clear();
row.clear();
std::cerr << "Warning: not thread-safe" << std::endl;
if (verbose)
std::cerr << "Creating new index with " << i << " sequences, total " << j << " bytes, " << j/1024 << " kb "
<< "(elapsed " << std::difftime(time(NULL), wctime) << " s, "
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << std::endl;
// TODO currently never uses plain text
bool storePlainText = false;
if (reverse || rotation)
storePlainText = false;
TextCollection* tc = tcb->InitTextCollection(storePlainText, color, patlen);
delete tcb; tcb = 0;
if (verbose) std::cerr << "Saving to file " << outputfile << endl
<< "(total wall-clock time " << std::difftime(time(NULL), wctime) << " s, "
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << endl;
tc->save(outputfile);
delete tc;
}
void print_usage(char const *name)
{
cerr << "usage: " << name << " [options] <input> [output]" << endl
<< "Check README or `" << name << " --help' for more information." << endl;
}
void print_help(char const *name)
{
cerr << "usage: " << name << " [options] <input> [output]" << endl << endl
<< "<input> is the input filename. "
<< "If no output filename is given, the index is stored as <input>.fmi" << endl
<< "or as <input>.rlcsa." << endl << endl
<< "Options:" << endl
<< " -s <int>, --sample-rate <int> Sampling rate for the index, a smaller number " << endl
<< " yields a bigger index but can decrease search " << endl
<< " time (default: " << TEXTCOLLECTION_DEFAULT_SAMPLERATE << ")." << endl
<< " -h, --help Display command line options." << endl
<< " -v, --verbose Print progress information." << endl;
}
int atoi_min(char const *value, int min, char const *parameter, char const *name)
{
std::istringstream iss(value);
int i;
char c;
if (!(iss >> i) || iss.get(c))
{
cerr << "readaligner: argument of " << parameter << " must be of type <int>, and greater than or equal to " << min << endl
<< "Check README or `" << name << " --help' for more information." << endl;
std::exit(1);
}
if (i < min)
{
cerr << "readaligner: argument of " << parameter << " must be greater than or equal to " << min << endl
<< "Check README or `" << name << " --help' for more information." << endl;
std::exit(1);
}
return i;
}
int main(int argc, char **argv)
{
std::cerr << "Warning: Reversing the string by default" << std::endl;
/**
* Parse command line parameters
*/
if (argc == 1)
{
print_usage(argv[0]);
return 1;
}
unsigned samplerate = 0;
bool reverse = false;
static struct option long_options[] =
{
{"sample-rate", required_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
int option_index = 0;
int c;
while ((c = getopt_long(argc, argv, "cR:s:F:hv",
long_options, &option_index)) != -1)
{
switch(c)
{
case 's':
samplerate = atoi_min(optarg, 1, "-s, --sample-rate", argv[0]);
break;
case 'h':
print_help(argv[0]);
return 0;
case 'v':
verbose = true; break;
case '?':
print_usage(argv[0]);
return 1;
default:
print_usage(argv[0]);
std::abort();
}
}
cerr << "Warning: sampling is disabled!" << endl;
if (samplerate && samplerate <= 3)
cerr << "Warning: small samplerates (-s, --sample-rate) may yield infeasible index sizes" << endl;
if (argc - optind < 1)
{
cerr << "readaligner: no input filename given!" << endl;
print_usage(argv[0]);
return 1;
}
if (argc - optind > 2)
cerr << "Warning: too many filenames given! Ignoring all but first two." << endl;
string inputfile = string(argv[optind++]);
string outputfile = "";
if (optind != argc)
outputfile = string(argv[optind++]);
istream *fp;
if (inputfile == "-")
fp = &std::cin;
else
fp = new std::ifstream(inputfile.c_str());
if (!fp->good())
{
cerr << "builder: unable to read input file " << inputfile << endl;
exit(1);
}
cerr << std::fixed;
cerr.precision(2);
time_t wctime = time(NULL);
// estimate the total input sequence length
// fp->seekg(0, ios::end);
long estLength = 1; //fp->tellg();
if (estLength == -1)
{
cerr << "Warning: unable to estimate input file size" << endl;
estLength = 0;
}
// fp->seekg(0);
/**
* Build forward/rotation index
*/
if (verbose)
cerr << "Building the forward index:" << endl;
if (outputfile == "")
outputfile = inputfile; // suffix will be added by TextCollection::save().
if (rotation)
outputfile += TextCollection::ROTATION_EXTENSION;
build(fp, outputfile, samplerate, false, estLength, wctime);
if (!reverse)
{
// No reverse index
if (verbose)
std::cerr << "Skipping reverse indexing. Save complete. "
<< "(total wall-clock time " << std::difftime(time(NULL), wctime) << " s, "
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << endl;
if (fp != &std::cin)
delete fp;
fp = 0;
return 0; // all done
}
/**
* Build reverse index
*/
if (verbose)
cerr << "Building the reverse index:" << endl;
assert(!rotation);
fp->clear(); // forget previous EOF
fp->seekg(0);
if (!fp->good())
{
cerr << "builder: unable to rewind the input file!" << endl;
return 1;
}
outputfile += TextCollection::REVERSE_EXTENSION;
build(fp, outputfile, samplerate, true, estLength, wctime);
if (fp != &std::cin)
delete fp;
fp = 0;
if (verbose)
std::cerr << "Save complete. "
<< "(total wall-clock time " << std::difftime(time(NULL), wctime) << " s, "
<< std::difftime(time(NULL), wctime) / 3600 << " hours)" << endl;
return 0;
}