-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChunkIndexer.cpp~
430 lines (355 loc) · 12.8 KB
/
ChunkIndexer.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
#include "ChunkIndexer.h"
#include "InputReader.h"
#include "DNALineReader.h"
#include "divsufsort.h"
#include "BitPrefix.h"
using namespace std;
ChunkIndexer::~ChunkIndexer()
{
//free memory of arrays used - TBD test what really happens
vector<unsigned char>().swap(this->chunk);
vector<int>().swap(this->chunkSA);
}
bool ChunkIndexer::init(IndexConfig *cfg, InputReader *reader)
{
this->longestInputLine=0;
this->reader = reader;
//check that we have all the required information
string fofilenames;
string fokeys;
if(cfg->keyExists("fofilenames"))
{
fofilenames = cfg->getValue<string>("fofilenames");
}
else
{
displayWarning("ChunkIndexer: Init failed. The name of the file (fofilenames) containing input file names is missing in db config");
return false;
}
if(cfg->keyExists("fokeys"))
{
fokeys = cfg->getValue<string>("fokeys");
}
else
{
displayWarning("ChunkIndexer: Init failed. The name of the file (fokeys) containing sample names is missing in db config");
return false;
}
//now actually read the above files into string arrays
uint64 readLines = readLinesIntoArray(&fofilenames,&(this->inputFileNames));
if(readLines <= 0)
{
displayWarning("ChunkIndexer: Init failed. Error reading file "+fofilenames+": probably does not contain any lines");
return false;
}
readLines = readLinesIntoArray(&fokeys,&(this->sampleKeys));
if(readLines <= 0)
{
displayWarning("ChunkIndexer: Init failed. The file with sample keys "+fokeys+" does not contain any lines");
return false;
}
//get config parameters and fill default values if params are missing
this->chunkMaxCharacters = 250000000;
if(cfg->keyExists("chunkMaxCharacters"))
{
this->chunkMaxCharacters = cfg->getValue<uint64>("chunkMaxCharacters");
}
this->maxChunks = 32000;
if(cfg->keyExists("maxChunks"))
{
this->maxChunks = cfg->getValue<short>("maxChunks");
}
this->maxFileSize = 4096000 ;//4MB default
if(cfg->keyExists("maxFileSize"))
{
this->maxFileSize = cfg->getValue<uint32>("maxFileSize");
}
string fileName = "mapChunkToSample.csv";
if(cfg->keyExists("mappingFileName"))
{
fileName = cfg->getValue<string>("mappingFileName");
}
this->dir = ".";
if(cfg->keyExists("metaDataDir"))
{
this->dir = cfg->getValue<string>("metaDataDir");
}
this->mappingFileName = this->dir + separator() + fileName;
this->tempDir = ".";
if(cfg->keyExists("tempDir"))
{
this->tempDir = cfg->getValue<string>("tempDir");
}
this->bwtFileExt =".bwt";
if(cfg->keyExists("bwtFileExt"))
{
this->bwtFileExt = cfg->getValue<string>("bwtFileExt");
}
this->saFileExt = ".sa";
if(cfg->keyExists("saFileExt"))
{
this->saFileExt = cfg->getValue<string>("saFileExt");
}
this->chunkFileExt=".chunk";
if(cfg->keyExists("chunkFileExt"))
{
this->chunkFileExt = cfg->getValue<string>("chunkFileExt");
}
//we need to allocate a char * and an int * buffer to use with Mori suffix sorting algo
this->chunk.resize((size_t)(this->chunkMaxCharacters+2)); //for sentinel and char termination character
this->chunkSA.resize((size_t)this->chunkMaxCharacters);
//all values are set, can start indexing
return true;
}
bool ChunkIndexer::indexChunks ()
{
//go in a loop through input files, read an appropriate chunk from this file into array of lines
//build BWT using in-mem suffix sorting algo and write it to disk
//also add to mapping array: index in this array corresponds to - chunk id, value - to sample id
this->sentinel = this->reader->getSentinel();
this->currentChunkID=0;
short sampleID=0;
for(vector<string>::const_iterator it = this->inputFileNames.begin(); it != this->inputFileNames.end(); ++it,++sampleID)
{
string fname=*it;
this->reader->init(fname,0);
this->currentInputPos=0;
this->totalAddedCharacters=0;
uint64 currentPosInFile=0L;
if(this->currentChunkID >= this->maxChunks)
{
displayWarning("ChunkIndexer: Number of chunks "+ T_to_string<short>(this->currentChunkID)+ " exceeded predefined max " +T_to_string<short>(this->maxChunks));
//write mappings from chunk ID to sample into a file
this->recordChunkMapping();
//stop processing the rest of files
return true;
}
string line= reader->nextValidToken(¤tPosInFile);
while(line!="")
{
if(line!="skip") //valid line
{
if(line.size() > this->longestInputLine)
this->longestInputLine=line.size();
//did not reach max characters yet - add current line to chunk
if ( (uint64)(this->totalAddedCharacters+line.size() +2)>= (uint64)this->chunkMaxCharacters)
{
if(!this->finalizeCurrentChunk(sampleID))
return false;
//start over a new chunk
this->currentInputPos=0;
this->totalAddedCharacters=0;
this->currentChunkID++;
}
//transfer characters in inified format to the current input chunk
unsigned int s=0;
for(; s< line.size(); s++)
{
this->chunk[(size_t)this->currentInputPos] = this->reader->getUnifiedCharacter(line.at(s));
this->chunkSA[(size_t)this->currentInputPos]=0;
this->currentInputPos++;
this->totalAddedCharacters++;
}
//add sentinel character at the end of the line
this->chunk[(size_t)this->currentInputPos] = this->sentinel;
this->currentInputPos++;
this->totalAddedCharacters++;
this->chunkSA[(size_t)this->currentInputPos]=0;
}
line= reader->nextValidToken(¤tPosInFile);
}
//reading lines from the file is done
this->reader->closeFile(); //finished adding characters from this file
//check if something is still in memory and flush it as the current chunk
if(this->totalAddedCharacters>0)
{
if(!this->finalizeCurrentChunk(sampleID))
return false;
this->currentChunkID++;
//start over a new chunk
this->currentInputPos=0;
this->totalAddedCharacters=0;
}
printf("Finished processing file %s (file id = %d), total chunks so far=%d\n",fname.c_str(),sampleID,this->currentChunkID);
}
//record mappings from chunk IDs to samples into a file
if(!this->recordChunkMapping())
return false;
return true;
}
bool ChunkIndexer::finalizeCurrentChunk(short fileID)
{
this->chunkToSampleMap.push_back(fileID); //finished chunk, record the file it belongs to
//add final sentinel character
this->chunk[(size_t)this->currentInputPos] = this->sentinel;
this->currentInputPos++;
this->totalAddedCharacters++;
this->chunk[(size_t)this->currentInputPos]='\0'; //terminate input char array - just in case TBD check
//build SA from already collected characters
if(!this->buildSAMori())
{
displayWarning("Building suffix array for file id = "+T_to_string<short>(fileID) +" failed.");
return false;
}
//record BWT simple, SA simple and entire chunk as it was indexed
if(!this->recordSA(this->currentChunkID)) return false;
if(!this->recordBWT(this->currentChunkID)) return false;
if(!this->recordChunk(this->currentChunkID)) return false;
return true;
}
bool ChunkIndexer::buildSAMori()
{
if(divsufsort(&(this->chunk[0]),&(this->chunkSA[0]),(int)this->totalAddedCharacters) != 0)
{
displayWarning("ChunkIndexer: Can not index chunk of data. Errpr occured in building suffix array by Mori algorithm");
return false;
}
return true;
}
bool ChunkIndexer::recordSA(short chunkID)
{
string saFileName = this->dir + separator() + T_to_string<short>(chunkID)+this->saFileExt;
uint64 written = flushData<int>(this->chunkSA,this->totalAddedCharacters, saFileName);
if(written != this->totalAddedCharacters)
return false;
return true;
}
bool ChunkIndexer::recordChunk(short chunkID) //tbd test if binary is a good idea
{
string chunkFileName = this->dir + separator() + T_to_string<short>(chunkID)+this->chunkFileExt;
uint64 written = flushData<unsigned char>(this->chunk,this->totalAddedCharacters, chunkFileName);
if(written != this->totalAddedCharacters)
return false;
return true;
}
int ChunkIndexer::getBitEncoding(int suffixStart, uint32 *bitArray, unsigned char *prefix)
{
//reset bit array to all zeroes
for (int i=0; i<10; i++)
bitArray[i] = 0;
int suffixLength=0;
bool endOfSuffix = false;
int i;
int currentPos=0;
for(i=suffixStart; i < (int)this->totalAddedCharacters && !endOfSuffix; i++)
{
unsigned char currentChar = this->chunk[i];
if (currentChar != this->sentinel)
{
suffixLength++;
BitPrefix::setCharacterAtPos (currentChar, currentPos++, bitArray);
}
else
{
endOfSuffix=true;
BitPrefix::setSuffixLength (suffixLength, bitArray);
BitPrefix::setPrefix (&this->chunk[suffixStart], suffixLength, prefix);
}
}
return ((suffixLength*2 + 8)/32 +1);
}
//this is a basic version that writes only BWT characters needed for merge
//change - it also records small chunks of BWTStateCell arrays - needed for efficient merge
//TBD - use different writers to write a searchable compressed BWT
bool ChunkIndexer::recordBWT(short chunkID)
{
//at most 150 chars (2 bits per char), 300 bits, + 8 bits to encode suffix length = 308 bits/8 = 40 bytes or 10 integers
uint32 prefixEncoding [10];
FILE *file = NULL;
unsigned char filePrefix = 0;
bool prefixedFileOpen = false;
string runFileNamePrefix = this->tempDir + separator() + "prefixrun_" + T_to_string<short>(chunkID)+"_";
string bwtFileName = this->dir + separator() + T_to_string<short>(chunkID)+this->bwtFileExt;
ofstream textWriter;
textWriter.open(bwtFileName.c_str());
if(!textWriter)
{
displayWarning("ChunkIndexer::recording BWT: cannot write to file " + bwtFileName);
return false;
}
for(unsigned int i=0;i<this->totalAddedCharacters;i++)
{
int pos = this->chunkSA[i];
if(pos==0)
{
textWriter<<this->sentinel;
}
else
{
textWriter << this->chunk[pos-1];
}
//record variable-length prefixes for merge
unsigned char currentPrefix=0;
int totalInts = getBitEncoding(pos, &prefixEncoding[0], ¤tPrefix);
if (prefixedFileOpen)
{
//compare with the current prefix file
if (filePrefix != currentPrefix)
{
//flush data to a file
//close previous file
fclose(file);
//open a new file
string fname = runFileNamePrefix + T_to_string<short>((short)currentPrefix);
filePrefix = currentPrefix;
BitPrefix:: printBitPrefix (& currentPrefix);
file=fopen((fname).c_str(), "wb");
if (!file)
{
displayWarning("Writing prefixed runs failed: Cannot write to file " + fname);
return false;
}
}
}
else
{
//open a new file
string fname = runFileNamePrefix + T_to_string<short>((short)currentPrefix);
filePrefix = currentPrefix;
file=fopen((fname).c_str(), "wb");
if (!file)
{
displayWarning("Writing prefixed runs failed: Cannot write to file " + fname);
return false;
}
prefixedFileOpen = true;
}
//write current prefix to file
size_t written = fwrite(&(prefixEncoding[0]), sizeof(uint32), (size_t)totalInts, file);
if(written != (size_t)totalInts)
{
std::string wantedSTR = T_to_string<uint64>(totalInts);
std::string writtenSTR = T_to_string<uint64>(written);
displayWarning("Writing prefixed runs failed: Not all has been written. wanted to write "+wantedSTR+ " but written only "+writtenSTR);
return false;
}
}
fclose(file);
textWriter.close();
return true;
}
bool ChunkIndexer::recordChunkMapping()
{
short totalChunks=this->currentChunkID;
ofstream textWriter;
textWriter.open(this->mappingFileName.c_str());
if(!textWriter)
{
displayWarning("ChunkIndexer::recording sample mapping: cannot write to file " + this->mappingFileName);
return false;
}
for(int i=0;i<totalChunks;i++)
{
string chunkID = T_to_string<short>(i);
string fileID = T_to_string<short>(this->chunkToSampleMap[i]);
string inputKey = this->sampleKeys[this->chunkToSampleMap[i]];
string inputFileName = this->inputFileNames[this->chunkToSampleMap[i]];
textWriter<<chunkID+","+ fileID +","+inputKey+","+ inputFileName<<endl ;
}
textWriter.close();
return true;
}
short ChunkIndexer::getTotalChunks()
{
return this->currentChunkID;
}