| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include "meryl.H" |
| 6 |
#include "libmeryl.H" |
| 7 |
|
| 8 |
|
| 9 |
using namespace std; |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
|
| 13 |
struct mMer { |
| 14 |
kMer _mer; |
| 15 |
uint32 _cnt; |
| 16 |
uint32 _off; |
| 17 |
}; |
| 18 |
|
| 19 |
static |
| 20 |
int |
| 21 |
mMerGreaterThan(void const *a, void const *b) { |
| 22 |
mMer const *A = (mMer const *)a; |
| 23 |
mMer const *B = (mMer const *)b; |
| 24 |
return(B->_mer.qsort_less(A->_mer)); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class mMerList { |
| 30 |
public: |
| 31 |
mMerList(uint32 maxSize) { |
| 32 |
_posLen = 0; |
| 33 |
_posMax = 2 * maxSize; |
| 34 |
_pos = new uint32 [_posMax]; |
| 35 |
|
| 36 |
_mmmLen = 0; |
| 37 |
_mmmMax = maxSize; |
| 38 |
_mmm = new mMer [_mmmMax]; |
| 39 |
}; |
| 40 |
~mMerList() { |
| 41 |
delete [] _pos; |
| 42 |
delete [] _mmm; |
| 43 |
}; |
| 44 |
|
| 45 |
uint32 length(void) { return(_mmmLen); }; |
| 46 |
|
| 47 |
// Until we sort, first() is the last thing loaded. |
| 48 |
// After we sort, first() is the lowest mer in the set. |
| 49 |
|
| 50 |
kMer &first(void) { return(_mmm[_mmmLen-1]._mer); }; |
| 51 |
//kMer &last(void) { return(_mmm[0]._mer); }; |
| 52 |
//kMer &get(uint32 i) { return(_mmm[i]._mer); }; |
| 53 |
|
| 54 |
// Return the first (sorted order) thing in the list -- it's the last on the list. |
| 55 |
kMer *pop(uint32 &cnt, uint32* &pos) { |
| 56 |
if (_mmmLen == 0) |
| 57 |
return(0L); |
| 58 |
|
| 59 |
_mmmLen--; |
| 60 |
|
| 61 |
assert(_sorted); |
| 62 |
|
| 63 |
cnt = _mmm[_mmmLen]._cnt; |
| 64 |
pos = 0L; |
| 65 |
|
| 66 |
if (_mmm[_mmmLen]._off != ~uint32ZERO) |
| 67 |
pos = _pos + _mmm[_mmmLen]._off; |
| 68 |
|
| 69 |
return(&_mmm[_mmmLen]._mer); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
// rebuild the position list, squeezes out empty items |
| 74 |
void rebuild(void) { |
| 75 |
if (_posLen > 0) { |
| 76 |
uint32 *np = new uint32 [_posMax]; |
| 77 |
|
| 78 |
_posLen = 0; |
| 79 |
|
| 80 |
for (uint32 i=0; i<_mmmLen; i++) { |
| 81 |
mMer *m = _mmm + i; |
| 82 |
|
| 83 |
if (m->_off != ~uint32ZERO) { |
| 84 |
_mmm[_mmmLen]._off = _posLen; |
| 85 |
|
| 86 |
for (uint32 p=0; p<m->_cnt; p++, _posLen++) |
| 87 |
np[_posLen] = _pos[p]; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
delete [] _pos; |
| 92 |
_pos = np; |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
// Read more mers from the file |
| 99 |
void read(merylStreamReader *R, uint32 num) { |
| 100 |
uint32 xxx = 0; |
| 101 |
|
| 102 |
if (_mmmLen + num >= _mmmMax) { |
| 103 |
fprintf(stderr, "Reallocate _mmm\n"); |
| 104 |
_mmmMax = _mmmMax + 2 * num; |
| 105 |
mMer *tmp = new mMer [_mmmMax]; |
| 106 |
memcpy(tmp, _mmm, sizeof(mMer) * _mmmLen); |
| 107 |
delete [] _mmm; |
| 108 |
_mmm = tmp; |
| 109 |
} |
| 110 |
|
| 111 |
_sorted = false; |
| 112 |
|
| 113 |
R->nextMer(); |
| 114 |
|
| 115 |
for (xxx=0; (xxx < num) && (R->validMer()); xxx++) { |
| 116 |
if (_mmmMax <= _mmmLen) { |
| 117 |
fprintf(stderr, "Reallocate _mmm\n"); |
| 118 |
_mmmMax *= 2; |
| 119 |
mMer *tmp = new mMer [_mmmMax]; |
| 120 |
memcpy(tmp, _mmm, sizeof(mMer) * _mmmLen); |
| 121 |
delete [] _mmm; |
| 122 |
_mmm = tmp; |
| 123 |
} |
| 124 |
|
| 125 |
_mmm[_mmmLen]._mer = R->theFMer(); |
| 126 |
_mmm[_mmmLen]._cnt = R->theCount(); |
| 127 |
_mmm[_mmmLen]._off = ~uint32ZERO; |
| 128 |
|
| 129 |
uint32 *pos = R->thePositions(); |
| 130 |
if (pos) { |
| 131 |
_mmm[_mmmLen]._off = _posLen; |
| 132 |
|
| 133 |
if (_posMax <= _posLen + _mmm[_mmmLen]._cnt) { |
| 134 |
fprintf(stderr, "Reallocate _pos\n"); |
| 135 |
_posMax *= 2; |
| 136 |
uint32 *tmp = new uint32 [_posMax]; |
| 137 |
memcpy(tmp, _pos, sizeof(uint32) * _posLen); |
| 138 |
delete [] _pos; |
| 139 |
_pos = tmp; |
| 140 |
} |
| 141 |
|
| 142 |
for (uint32 i=0; i<_mmm[_mmmLen]._cnt; i++, _posLen++) |
| 143 |
_pos[_posLen] = pos[i]; |
| 144 |
} |
| 145 |
|
| 146 |
_mmmLen++; |
| 147 |
|
| 148 |
R->nextMer(); |
| 149 |
} |
| 150 |
|
| 151 |
//fprintf(stderr, "read()-- now up to "uint32FMT" mers ("uint32FMT" pos); loaded "uint32FMT" out of "uint32FMT" requested.\n", _mmmLen, _posLen, xxx, num); |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
// Sort our list of mers |
| 156 |
void sort(void) { |
| 157 |
if (_sorted == false) { |
| 158 |
//fprintf(stderr, "SORT BEG\n"); |
| 159 |
qsort_mt(_mmm, _mmmLen, sizeof(mMer), mMerGreaterThan, 8, 32 * 1024); |
| 160 |
_sorted = true; |
| 161 |
//fprintf(stderr, "SORT END\n"); |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
private: |
| 167 |
bool _sorted; |
| 168 |
|
| 169 |
uint32 _posLen; |
| 170 |
uint32 _posMax; |
| 171 |
uint32 *_pos; |
| 172 |
|
| 173 |
uint32 _mmmLen; |
| 174 |
uint32 _mmmMax; |
| 175 |
mMer *_mmm; |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
void |
| 182 |
multipleOperations(merylArgs *args) { |
| 183 |
|
| 184 |
char debugstring[256]; |
| 185 |
char debugstring2[256]; |
| 186 |
|
| 187 |
if (args->mergeFilesLen < 2) { |
| 188 |
fprintf(stderr, "ERROR - must have at least two databases (you gave "uint32FMT")!\n", args->mergeFilesLen); |
| 189 |
exit(1); |
| 190 |
} |
| 191 |
if (args->outputFile == 0L) { |
| 192 |
fprintf(stderr, "ERROR - no output file specified.\n"); |
| 193 |
exit(1); |
| 194 |
} |
| 195 |
if ((args->personality != PERSONALITY_MERGE) && |
| 196 |
(args->personality != PERSONALITY_MIN) && |
| 197 |
(args->personality != PERSONALITY_MINEXIST) && |
| 198 |
(args->personality != PERSONALITY_MAX) && |
| 199 |
(args->personality != PERSONALITY_ADD) && |
| 200 |
(args->personality != PERSONALITY_AND) && |
| 201 |
(args->personality != PERSONALITY_NAND) && |
| 202 |
(args->personality != PERSONALITY_OR) && |
| 203 |
(args->personality != PERSONALITY_XOR)) { |
| 204 |
fprintf(stderr, "ERROR - only personalities min, minexist, max, add, and, nand, or, xor\n"); |
| 205 |
fprintf(stderr, "ERROR - are supported in multipleOperations().\n"); |
| 206 |
fprintf(stderr, "ERROR - this is a coding error, not a user error.\n"); |
| 207 |
exit(1); |
| 208 |
} |
| 209 |
|
| 210 |
merylStreamReader **R = new merylStreamReader* [args->mergeFilesLen]; |
| 211 |
merylStreamWriter *W = 0L; |
| 212 |
|
| 213 |
uint32 maxSize = 512 * 1024; |
| 214 |
|
| 215 |
mMerList *M = new mMerList(maxSize + maxSize / 4); |
| 216 |
|
| 217 |
|
| 218 |
// Open the input files and load some mers - we need to do this |
| 219 |
// just so we can check the mersizes/compression next. |
| 220 |
// |
| 221 |
for (uint32 i=0; i<args->mergeFilesLen; i++) { |
| 222 |
R[i] = new merylStreamReader(args->mergeFiles[i]); |
| 223 |
M->read(R[i], 1 + i); |
| 224 |
} |
| 225 |
|
| 226 |
// Verify that the mersizes are all the same |
| 227 |
// |
| 228 |
bool fail = false; |
| 229 |
uint32 merSize = R[0]->merSize(); |
| 230 |
uint32 merComp = R[0]->merCompression(); |
| 231 |
|
| 232 |
for (uint32 i=0; i<args->mergeFilesLen; i++) { |
| 233 |
fail |= (merSize != R[i]->merSize()); |
| 234 |
fail |= (merComp != R[i]->merCompression()); |
| 235 |
} |
| 236 |
|
| 237 |
if (fail) |
| 238 |
fprintf(stderr, "ERROR: mer sizes (or compression level) differ.\n"), exit(1); |
| 239 |
|
| 240 |
// Open the output file, using the largest prefix size found in the |
| 241 |
// input/mask files. |
| 242 |
// |
| 243 |
uint32 prefixSize = 0; |
| 244 |
for (uint32 i=0; i<args->mergeFilesLen; i++) |
| 245 |
if (prefixSize < R[i]->prefixSize()) |
| 246 |
prefixSize = R[i]->prefixSize(); |
| 247 |
|
| 248 |
W = new merylStreamWriter(args->outputFile, merSize, merComp, prefixSize); |
| 249 |
|
| 250 |
|
| 251 |
kMer lastLoaded; |
| 252 |
|
| 253 |
lastLoaded.setMerSize(merSize); |
| 254 |
lastLoaded.smallest(); |
| 255 |
|
| 256 |
// Load mers from all files, remember the largest mer we load. |
| 257 |
// |
| 258 |
for (uint32 i=0; i<args->mergeFilesLen; i++) { |
| 259 |
M->read(R[i], maxSize / args->mergeFilesLen); |
| 260 |
if (lastLoaded < M->first()) |
| 261 |
lastLoaded = M->first(); |
| 262 |
} |
| 263 |
|
| 264 |
// Make sure all files have at least that largest mer loaded. |
| 265 |
// |
| 266 |
for (uint32 i=0; i<args->mergeFilesLen; i++) |
| 267 |
while (R[i]->validMer() && (R[i]->theFMer() <= lastLoaded)) |
| 268 |
M->read(R[i], 2 * 1024); |
| 269 |
|
| 270 |
fprintf(stderr, "Initial load: length="uint32FMT" lastLoaded=%s\n", |
| 271 |
M->length(), lastLoaded.merToString(debugstring)); |
| 272 |
|
| 273 |
M->sort(); |
| 274 |
|
| 275 |
bool allLoaded = false; |
| 276 |
bool moreStuff = true; |
| 277 |
|
| 278 |
kMer currentMer; // The current mer we're operating on |
| 279 |
uint32 currentCount = uint32ZERO; // The count (operation dependent) of this mer |
| 280 |
uint32 currentTimes = uint32ZERO; // Number of files it's in |
| 281 |
|
| 282 |
uint32 currentPositionsMax = 0; |
| 283 |
uint32 *currentPositions = 0L; |
| 284 |
|
| 285 |
kMer *thisMer; // The mer we just read |
| 286 |
uint32 thisCount = uint32ZERO; // The count of the mer we just read |
| 287 |
uint32 *thisPositions = 0L; |
| 288 |
|
| 289 |
speedCounter *C = new speedCounter(" %7.2f Mmers -- %5.2f Mmers/second\r", 1000000.0, 0x1fffff, args->beVerbose); |
| 290 |
|
| 291 |
currentMer.setMerSize(merSize); |
| 292 |
|
| 293 |
while (moreStuff) { |
| 294 |
|
| 295 |
// Load more stuff if needed. M is sorted, so first() is the |
| 296 |
// smallest mer in the set - we're good up to and including |
| 297 |
// lastLoaded. |
| 298 |
// |
| 299 |
if ((allLoaded == false) && |
| 300 |
((M->length() == 0) || (lastLoaded < M->first()))) { |
| 301 |
|
| 302 |
#if 0 |
| 303 |
if (M->length() > 0) |
| 304 |
fprintf(stderr, "LOADMORE length="uint32FMT" lastLoaded=%s first=%s\n", |
| 305 |
M->length(), lastLoaded.merToString(debugstring2), M->first().merToString(debugstring)); |
| 306 |
else |
| 307 |
fprintf(stderr, "LOADMORE length="uint32FMT" lastLoaded=%s first=EMPTY\n", |
| 308 |
M->length(), lastLoaded.merToString(debugstring2)); |
| 309 |
#endif |
| 310 |
|
| 311 |
// We need to copy all the mers currently loaded into fresh |
| 312 |
// storage, so we can deallocate the position storage. Yucky. |
| 313 |
// |
| 314 |
M->rebuild(); |
| 315 |
|
| 316 |
allLoaded = true; |
| 317 |
|
| 318 |
// Load more stuff to give us a large collection of mers |
| 319 |
// |
| 320 |
uint32 additionalLoading = 8192; |
| 321 |
|
| 322 |
if (maxSize / args->mergeFilesLen > M->length()) |
| 323 |
additionalLoading = maxSize / args->mergeFilesLen - M->length(); |
| 324 |
|
| 325 |
//fprintf(stderr, "LOADMORE adding "uint32FMT" from each file\n", additionalLoading); |
| 326 |
|
| 327 |
lastLoaded.setMerSize(merSize); |
| 328 |
lastLoaded.smallest(); |
| 329 |
|
| 330 |
for (uint32 i=0; i<args->mergeFilesLen; i++) { |
| 331 |
if (R[i]->validMer()) { |
| 332 |
M->read(R[i], additionalLoading); |
| 333 |
if (lastLoaded < M->first()) |
| 334 |
lastLoaded = M->first(); |
| 335 |
allLoaded = false; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
// Make sure all files have at least that largest mer loaded. |
| 340 |
// |
| 341 |
for (uint32 i=0; i<args->mergeFilesLen; i++) |
| 342 |
while (R[i]->validMer() && (R[i]->theFMer() <= lastLoaded)) |
| 343 |
M->read(R[i], 2 * 1024); |
| 344 |
|
| 345 |
M->sort(); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
// All done? Exit. |
| 350 |
if (M->length() == 0) |
| 351 |
moreStuff = false; |
| 352 |
|
| 353 |
thisMer = M->pop(thisCount, thisPositions); |
| 354 |
|
| 355 |
// If we've hit a different mer, write out the last one |
| 356 |
if ((M->length() == 0) || (*thisMer != currentMer)) { |
| 357 |
switch (args->personality) { |
| 358 |
case PERSONALITY_MIN: |
| 359 |
if (currentTimes == args->mergeFilesLen) |
| 360 |
W->addMer(currentMer, currentCount); |
| 361 |
break; |
| 362 |
case PERSONALITY_MERGE: |
| 363 |
case PERSONALITY_MINEXIST: |
| 364 |
case PERSONALITY_MAX: |
| 365 |
case PERSONALITY_ADD: |
| 366 |
W->addMer(currentMer, currentCount, currentPositions); |
| 367 |
break; |
| 368 |
case PERSONALITY_AND: |
| 369 |
if (currentTimes == args->mergeFilesLen) |
| 370 |
W->addMer(currentMer, currentCount); |
| 371 |
break; |
| 372 |
case PERSONALITY_NAND: |
| 373 |
if (currentTimes != args->mergeFilesLen) |
| 374 |
W->addMer(currentMer, currentCount); |
| 375 |
break; |
| 376 |
case PERSONALITY_OR: |
| 377 |
W->addMer(currentMer, currentCount); |
| 378 |
break; |
| 379 |
case PERSONALITY_XOR: |
| 380 |
if ((currentTimes % 2) == 1) |
| 381 |
W->addMer(currentMer, currentCount); |
| 382 |
break; |
| 383 |
default: |
| 384 |
fprintf(stderr, "ERROR - invalid personality in multipleOperations::write\n"); |
| 385 |
fprintf(stderr, "ERROR - this is a coding error, not a user error.\n"); |
| 386 |
exit(1); |
| 387 |
break; |
| 388 |
} |
| 389 |
|
| 390 |
currentMer = *thisMer; |
| 391 |
|
| 392 |
currentCount = uint32ZERO; |
| 393 |
currentTimes = uint32ZERO; |
| 394 |
|
| 395 |
C->tick(); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
if (moreStuff == false) |
| 400 |
break; |
| 401 |
|
| 402 |
|
| 403 |
// Perform the operation |
| 404 |
switch (args->personality) { |
| 405 |
case PERSONALITY_MERGE: |
| 406 |
if (thisPositions) { |
| 407 |
|
| 408 |
if (currentPositionsMax == 0) { |
| 409 |
currentPositionsMax = 1048576; |
| 410 |
currentPositions = new uint32 [currentPositionsMax]; |
| 411 |
} |
| 412 |
|
| 413 |
if (currentPositionsMax < currentCount + thisCount) { |
| 414 |
while (currentPositionsMax < currentCount + thisCount) |
| 415 |
currentPositionsMax *= 2; |
| 416 |
|
| 417 |
uint32 *t = new uint32 [currentPositionsMax]; |
| 418 |
memcpy(t, currentPositions, sizeof(uint32) * currentCount); |
| 419 |
delete [] currentPositions; |
| 420 |
currentPositions = t; |
| 421 |
} |
| 422 |
|
| 423 |
if (thisCount < 16) { |
| 424 |
for (uint32 i=0; i<thisCount; i++) |
| 425 |
currentPositions[currentCount + i] = thisPositions[i]; |
| 426 |
} else { |
| 427 |
memcpy(currentPositions + currentCount, thisPositions, sizeof(uint32) * thisCount); |
| 428 |
} |
| 429 |
} |
| 430 |
// Otherwise, we're the same as ADD. |
| 431 |
currentCount += thisCount; |
| 432 |
break; |
| 433 |
case PERSONALITY_MIN: |
| 434 |
case PERSONALITY_MINEXIST: |
| 435 |
if (currentTimes == 0) { |
| 436 |
currentCount = thisCount; |
| 437 |
} else { |
| 438 |
if (currentCount > thisCount) |
| 439 |
currentCount = thisCount; |
| 440 |
} |
| 441 |
break; |
| 442 |
case PERSONALITY_MAX: |
| 443 |
if (currentCount < thisCount) |
| 444 |
currentCount = thisCount; |
| 445 |
break; |
| 446 |
case PERSONALITY_ADD: |
| 447 |
currentCount += thisCount; |
| 448 |
break; |
| 449 |
case PERSONALITY_AND: |
| 450 |
case PERSONALITY_NAND: |
| 451 |
case PERSONALITY_OR: |
| 452 |
case PERSONALITY_XOR: |
| 453 |
currentCount = 1; |
| 454 |
break; |
| 455 |
default: |
| 456 |
fprintf(stderr, "ERROR - invalid personality in multipleOperations::operate\n"); |
| 457 |
fprintf(stderr, "ERROR - this is a coding error, not a user error.\n"); |
| 458 |
exit(1); |
| 459 |
break; |
| 460 |
} |
| 461 |
|
| 462 |
currentTimes++; |
| 463 |
} |
| 464 |
|
| 465 |
for (uint32 i=0; i<args->mergeFilesLen; i++) |
| 466 |
delete R[i]; |
| 467 |
delete R; |
| 468 |
delete W; |
| 469 |
delete M; |
| 470 |
delete C; |
| 471 |
} |