Debian Codegraph @

libmeryl/libmeryl.C

raw
1 #include "libmeryl.H"
2
3 #define LIBMERYL_HISTOGRAM_MAX 1048576
4
5 // 0123456789012345
6 static char *ImagicV = "merylStreamIv03\n";
7 static char *ImagicX = "merylStreamIvXX\n";
8 static char *DmagicV = "merylStreamDv03\n";
9 static char *DmagicX = "merylStreamDvXX\n";
10 static char *PmagicV = "merylStreamPv03\n";
11 static char *PmagicX = "merylStreamPvXX\n";
12
13 merylStreamReader::merylStreamReader(const char *fn, uint32 ms) {
14
15 if (fn == 0L) {
16 fprintf(stderr, "ERROR - no counted database file specified.\n");
17 exit(1);
18 }
19
20 // Open the files
21 //
22 char *inpath = new char [strlen(fn) + 8];
23
24 sprintf(inpath, "%s.mcidx", fn);
25 _IDX = new bitPackedFile(inpath);
26
27 sprintf(inpath, "%s.mcdat", fn);
28 _DAT = new bitPackedFile(inpath);
29
30 sprintf(inpath, "%s.mcpos", fn);
31 if (fileExists(inpath))
32 _POS = new bitPackedFile(inpath);
33 else
34 _POS = 0L;
35
36 delete [] inpath;
37
38 // Verify that they are what they should be, and read in the header
39 //
40 char Imagic[16] = {0};
41 char Dmagic[16] = {0};
42 char Pmagic[16] = {0};
43 bool fail = false;
44
45 for (uint32 i=0; i<16; i++) {
46 Imagic[i] = _IDX->getBits(8);
47 Dmagic[i] = _DAT->getBits(8);
48 if (_POS)
49 Pmagic[i] = _POS->getBits(8);
50 }
51 if (strncmp(Imagic, ImagicX, 16) == 0) {
52 fprintf(stderr, "merylStreamReader()-- ERROR: %s.mcidx is an INCOMPLETE merylStream index file!\n", fn);
53 fail = true;
54 }
55 if (strncmp(Imagic, ImagicX, 13) != 0) {
56 fprintf(stderr, "merylStreamReader()-- ERROR: %s.mcidx is not a merylStream index file!\n", fn);
57 fail = true;
58 }
59 if (strncmp(Dmagic, DmagicX, 16) == 0) {
60 fprintf(stderr, "merylStreamReader()-- ERROR: %s.mcdat is an INCOMPLETE merylStream data file!\n", fn);
61 fail = true;
62 }
63 if (strncmp(Dmagic, DmagicX, 13) != 0) {
64 fprintf(stderr, "merylStreamReader()-- ERROR: %s.mcdat is not a merylStream data file!\n", fn);
65 fail = true;
66 }
67 if ((Imagic[13] != Dmagic[13]) ||
68 (Imagic[14] != Dmagic[14])) {
69 fprintf(stderr, "merylStreamReader()-- ERROR: %s.mcidx and %s.mcdat are different versions!\n", fn, fn);
70 fail = true;
71 }
72 #warning not checking pmagic
73
74 if (fail)
75 exit(1);
76
77 _idxIsPacked = _IDX->getBits(32);
78 _datIsPacked = _IDX->getBits(32);
79 _posIsPacked = _IDX->getBits(32);
80
81 _merSizeInBits = _IDX->getBits(32) << 1;
82 _merCompression = _IDX->getBits(32);
83 _prefixSize = _IDX->getBits(32);
84 _merDataSize = _merSizeInBits - _prefixSize;
85
86 _numUnique = _IDX->getBits(64);
87 _numDistinct = _IDX->getBits(64);
88 _numTotal = _IDX->getBits(64);
89
90 _histogramHuge = 0;
91 _histogramLen = 0;
92 _histogramMaxValue = 0;
93 _histogram = 0L;
94
95 uint32 version = atoi(Imagic + 13);
96
97 if (version > 1) {
98 _histogramHuge = _IDX->getBits(64);
99 _histogramLen = _IDX->getBits(64);
100 _histogramMaxValue = _IDX->getBits(64);
101 _histogram = new uint64 [_histogramLen];
102
103 for (uint32 i=0; i<_histogramLen; i++)
104 _histogram[i] = _IDX->getBits(64);
105 }
106
107 _thisBucket = uint64ZERO;
108 _thisBucketSize = getIDXnumber();
109 _numBuckets = uint64ONE << _prefixSize;
110
111 _thisMer.setMerSize(_merSizeInBits >> 1);
112 _thisMer.clear();
113 _thisMerCount = uint64ZERO;
114
115 _thisMerPositionsMax = 0;
116 _thisMerPositions = 0L;
117
118 _validMer = true;
119
120 #ifdef SHOW_VARIABLES
121 fprintf(stderr, "_merSizeInBits = "uint32FMT"\n", _merSizeInBits);
122 fprintf(stderr, "_merCompression = "uint32FMT"\n", _merCompression);
123 fprintf(stderr, "_prefixSize = "uint32FMT"\n", _prefixSize);
124 fprintf(stderr, "_merDataSize = "uint32FMT"\n", _merDataSize);
125 fprintf(stderr, "_numUnique = "uint64FMT"\n", _numUnique);
126 fprintf(stderr, "_numDistinct = "uint64FMT"\n", _numDistinct);
127 fprintf(stderr, "_numTotal = "uint64FMT"\n", _numTotal);
128 fprintf(stderr, "_thisBucket = "uint64FMT"\n", _thisBucket);
129 fprintf(stderr, "_thisBucketSize = "uint64FMT"\n", _thisBucketSize);
130 fprintf(stderr, "_thisMerCount = "uint64FMT"\n", _thisMerCount);
131 #endif
132
133 if ((ms > 0) && (_merSizeInBits >> 1 != ms)) {
134 fprintf(stderr, "merylStreamReader()-- ERROR: User requested mersize "uint32FMT" but '%s' is mersize "uint32FMT"\n",
135 ms, fn, _merSizeInBits >> 1);
136 exit(1);
137 }
138 }
139
140
141 merylStreamReader::~merylStreamReader() {
142 delete _IDX;
143 delete _DAT;
144 delete _POS;
145 delete [] _thisMerPositions;
146 delete [] _histogram;
147 }
148
149
150
151 bool
152 merylStreamReader::nextMer(void) {
153
154 // Use a while here, so that we skip buckets that are empty
155 //
156 while ((_thisBucketSize == 0) && (_thisBucket < _numBuckets)) {
157 _thisBucketSize = getIDXnumber();
158 _thisBucket++;
159 }
160
161 if (_thisBucket >= _numBuckets)
162 return(_validMer = false);
163
164 // Before you get rid of the clear() -- if, say, the list of mers
165 // is sorted and we can shift the mer to make space for the new
166 // stuff -- make sure that nobody is calling reverseComplement()!
167 //
168 _thisMer.clear();
169 _thisMer.readFromBitPackedFile(_DAT, _merDataSize);
170 _thisMer.setBits(_merDataSize, _prefixSize, _thisBucket);
171
172 _thisMerCount = getDATnumber();
173
174 _thisBucketSize--;
175
176 if (_POS) {
177 if (_thisMerPositionsMax < _thisMerCount) {
178 delete [] _thisMerPositions;
179 _thisMerPositionsMax = _thisMerCount + 1024;
180 _thisMerPositions = new uint32 [_thisMerPositionsMax];
181 }
182 for (uint32 i=0; i<_thisMerCount; i++) {
183 _thisMerPositions[i] = _POS->getBits(32);
184 }
185 }
186
187 return(true);
188 }
189
190
191
192
193
194
195 merylStreamWriter::merylStreamWriter(const char *fn,
196 uint32 merSize,
197 uint32 merComp,
198 uint32 prefixSize,
199 bool positionsEnabled) {
200
201 char *outpath = new char [strlen(fn) + 17];
202
203 sprintf(outpath, "%s.mcidx", fn);
204 _IDX = new bitPackedFile(outpath, 0, true);
205
206 sprintf(outpath, "%s.mcdat", fn);
207 _DAT = new bitPackedFile(outpath, 0, true);
208
209 if (positionsEnabled) {
210 sprintf(outpath, "%s.mcpos", fn);
211 _POS = new bitPackedFile(outpath, 0, true);
212 } else {
213 _POS = 0L;
214 }
215
216 delete [] outpath;
217
218 // Save really important stuff
219
220 // unpacked --> write 0.42M mers/sec on 8 threads, merge 3.3M mers/sec
221 // packed --> write 0.77M mers/sec on 8 threads, merge 3.9M mers/sec
222 //
223 // This sucks.
224 //
225 _idxIsPacked = 1;
226 _datIsPacked = 1;
227 _posIsPacked = 0;
228
229 _merSizeInBits = merSize * 2;
230 _merCompression = merComp;
231 _prefixSize = prefixSize;
232 _merDataSize = _merSizeInBits - _prefixSize;
233
234 _thisBucket = uint64ZERO;
235 _thisBucketSize = uint64ZERO;
236 _numBuckets = uint64ONE << _prefixSize;
237
238 _numUnique = uint64ZERO;
239 _numDistinct = uint64ZERO;
240 _numTotal = uint64ZERO;
241
242 _thisMerIsBits = false;
243 _thisMerIskMer = false;
244
245 _thisMer.setMerSize(_merSizeInBits >> 1);
246 _thisMer.clear();
247
248 _thisMerPre = uint64ZERO;
249 _thisMerMer = uint64ZERO;
250
251 _thisMerPreSize = prefixSize;
252 _thisMerMerSize = 2 * merSize - prefixSize;
253
254 _thisMerCount = uint64ZERO;
255
256 for (uint32 i=0; i<16; i++)
257 _IDX->putBits(ImagicX[i], 8);
258
259 _IDX->putBits(_idxIsPacked, 32);
260 _IDX->putBits(_datIsPacked, 32);
261 _IDX->putBits(_posIsPacked, 32);
262
263 _IDX->putBits(_merSizeInBits >> 1, 32);
264 _IDX->putBits(_merCompression, 32);
265 _IDX->putBits(_prefixSize, 32);
266 _IDX->putBits(_numUnique, 64);
267 _IDX->putBits(_numDistinct, 64);
268 _IDX->putBits(_numTotal, 64);
269
270 _histogramHuge = 0;
271 _histogramLen = LIBMERYL_HISTOGRAM_MAX;
272 _histogramMaxValue = 0;
273 _histogram = new uint64 [_histogramLen];
274
275 for (uint32 i=0; i<_histogramLen; i++)
276 _histogram[i] = 0;
277
278 _IDX->putBits(_histogramHuge, 64);
279 _IDX->putBits(_histogramLen, 64);
280 _IDX->putBits(_histogramMaxValue, 64);
281 for (uint32 i=0; i<_histogramLen; i++)
282 _IDX->putBits(_histogram[i], 64);
283
284 for (uint32 i=0; i<16; i++)
285 _DAT->putBits(DmagicX[i], 8);
286
287 if (_POS)
288 for (uint32 i=0; i<16; i++)
289 _POS->putBits(PmagicX[i], 8);
290 }
291
292
293 merylStreamWriter::~merylStreamWriter() {
294
295 writeMer();
296
297 // Finish writing the buckets.
298 //
299 while (_thisBucket < _numBuckets + 2) {
300 setIDXnumber(_thisBucketSize);
301 _thisBucketSize = 0;
302 _thisBucket++;
303 }
304
305 // Seek back to the start and rewrite the magic numbers
306 //
307 _IDX->seek(0);
308 _DAT->seek(0);
309
310 for (uint32 i=0; i<16; i++)
311 _IDX->putBits(ImagicV[i], 8);
312
313 _IDX->putBits(_idxIsPacked, 32);
314 _IDX->putBits(_datIsPacked, 32);
315 _IDX->putBits(_posIsPacked, 32);
316
317 _IDX->putBits(_merSizeInBits >> 1, 32);
318 _IDX->putBits(_merCompression, 32);
319 _IDX->putBits(_prefixSize, 32);
320 _IDX->putBits(_numUnique, 64);
321 _IDX->putBits(_numDistinct, 64);
322 _IDX->putBits(_numTotal, 64);
323
324 _IDX->putBits(_histogramHuge, 64);
325 _IDX->putBits(_histogramLen, 64);
326 _IDX->putBits(_histogramMaxValue, 64);
327 for (uint32 i=0; i<_histogramLen; i++)
328 _IDX->putBits(_histogram[i], 64);
329 delete _IDX;
330
331 delete [] _histogram;
332
333 for (uint32 i=0; i<16; i++)
334 _DAT->putBits(DmagicV[i], 8);
335 delete _DAT;
336
337 if (_POS) {
338 for (uint32 i=0; i<16; i++)
339 _POS->putBits(PmagicV[i], 8);
340 delete _POS;
341 }
342 }
343
344
345 void
346 merylStreamWriter::writeMer(void) {
347
348 if (_thisMerCount == 0)
349 return;
350
351 _numTotal += _thisMerCount;
352 _numDistinct++;
353
354 if (_thisMerCount < LIBMERYL_HISTOGRAM_MAX)
355 _histogram[_thisMerCount]++;
356 else
357 _histogramHuge++;
358 if (_histogramMaxValue < _thisMerCount)
359 _histogramMaxValue = _thisMerCount;
360
361 assert((_thisMerIsBits == false) || (_thisMerIskMer == false));
362
363 if (_thisMerIsBits) {
364 if (_thisMerCount == 1) {
365 _DAT->putBits(_thisMerMer, _thisMerMerSize);
366 setDATnumber(1);
367 _thisBucketSize++;
368 _numUnique++;
369 } else {
370 _DAT->putBits(_thisMerMer, _thisMerMerSize);
371 setDATnumber(_thisMerCount);
372 _thisBucketSize++;
373 }
374
375 } else {
376 if (_thisMerCount == 1) {
377 _thisMer.writeToBitPackedFile(_DAT, _merDataSize);
378 setDATnumber(1);
379 _thisBucketSize++;
380 _numUnique++;
381 } else if (_thisMerCount > 1) {
382 _thisMer.writeToBitPackedFile(_DAT, _merDataSize);
383 setDATnumber(_thisMerCount);
384 _thisBucketSize++;
385 }
386 }
387 }
388
389
390
391 void
392 merylStreamWriter::addMer(kMer &mer, uint32 count, uint32 *positions) {
393 uint64 val;
394
395 if (_thisMerIskMer == false) {
396 _thisMerIskMer = true;
397 assert(_thisMerIsBits == false);
398 }
399
400 // Fail if we see a smaller mer than last time.
401 //
402 if (mer < _thisMer) {
403 char str[1024];
404 fprintf(stderr, "merylStreamWriter::addMer()-- ERROR: your mer stream isn't sorted increasingly!\n");
405 fprintf(stderr, "merylStreamWriter::addMer()-- last: %s\n", _thisMer.merToString(str));
406 fprintf(stderr, "merylStreamWriter::addMer()-- this: %s\n", mer.merToString(str));
407 exit(1);
408 }
409
410 // If there was a position given, write it.
411 //
412 if (positions && _POS)
413 for (uint32 i=0; i<count; i++)
414 _POS->putBits(positions[i], 32);
415
416 // If the new mer is the same as the last one just increase the
417 // count.
418 //
419 if (mer == _thisMer) {
420 _thisMerCount += count;
421 return;
422 }
423
424 // Write thisMer to disk. If the count is zero, we don't write
425 // anything. The count is zero for the first mer (all A) unless we
426 // add that mer, and if the silly user gives us a mer with zero
427 // count.
428 //
429 writeMer();
430
431 // If the new mer is in a different bucket from the last mer, write
432 // out some bucket counts. We need a while loop (opposed to just
433 // writing one bucket) because we aren't guaranteed that the mers
434 // are in adjacent buckets.
435 //
436 val = mer.startOfMer(_prefixSize);
437
438 while (_thisBucket < val) {
439 setIDXnumber(_thisBucketSize);
440 _thisBucketSize = 0;
441 _thisBucket++;
442 }
443
444 // Remember the new mer for the next time
445 //
446 _thisMer = mer;
447 _thisMerCount = count;
448 }
449
450
451
452 void
453 merylStreamWriter::addMer(uint64 prefix, uint32 prefixBits,
454 uint64 mer, uint32 merBits,
455 uint32 count,
456 uint32 *positions) {
457
458 if (_thisMerIsBits == false) {
459 _thisMerIsBits = true;
460 assert(_thisMerIskMer == false);
461 }
462
463 assert(prefixBits == _prefixSize);
464 assert(prefixBits == _thisMerPreSize);
465 assert(merBits == _thisMerMerSize);
466 assert(prefixBits + merBits == _merSizeInBits);
467
468 if ((prefix < _thisMerPre) ||
469 (prefix <= _thisMerPre) && (mer < _thisMerMer)) {
470 assert(0);
471 }
472
473 if ((prefix == _thisMerPre) &&
474 (mer == _thisMerMer)) {
475 _thisMerCount += count;
476 return;
477 }
478
479 writeMer();
480
481 while (_thisBucket < prefix) {
482 setIDXnumber(_thisBucketSize);
483 _thisBucketSize = 0;
484 _thisBucket++;
485 }
486
487 _thisMerPre = prefix;
488 _thisMerMer = mer;
489 _thisMerCount = count;
490 }