#include "bio++.H" #include "positionDB.H" void positionDB::reallocateSpace(uint64*& posn, uint64& posnMax, uint64& posnLen, uint64 len) { if (posnMax < posnLen + len) { uint64 *pp; posnMax = posnLen + len + (len >> 2); if (posnMax == 0) posnMax = 16384; try { pp = new uint64 [posnMax]; } catch (...) { fprintf(stderr, "positionDB::get()-- Can't allocate space for more positions, requested "uint64FMT" uint64's.\n", posnMax); abort(); } memcpy(pp, posn, sizeof(uint64) * posnLen); delete [] posn; posn = pp; } } void positionDB::loadPositions(uint64 J, uint64*& posn, uint64& posnMax, uint64& posnLen, uint64& count) { uint64 sizs[3] = {_pptrWidth, 1, _sizeWidth}; uint64 vals[3] = {0, 0, 1}; getDecodedValues(_buckets, J + _chckWidth, (_sizeWidth == 0) ? 2 : 3, sizs, vals); // If the size is stored, the count is updated to the correct // thing. If it's not stored, the count is set to 1 by the default // value of vals[2], and reset after we get the number of positions // stored. // count = vals[2]; if (vals[1]) { reallocateSpace(posn, posnMax, posnLen, 64); posn[posnLen++] = vals[0]; } else { uint64 ptr = vals[0] * _posnWidth; uint64 len = getDecodedValue(_positions, ptr, _posnWidth); if (_sizeWidth == 0) count = len; reallocateSpace(posn, posnMax, posnLen, len + 64); for (ptr += _posnWidth; len > 0; ptr += _posnWidth, len--) posn[posnLen++] = getDecodedValue(_positions, ptr, _posnWidth); } } bool positionDB::getExact(uint64 mer, uint64*& posn, uint64& posnMax, uint64& posnLen, uint64& count) { uint64 h = HASH(mer); uint64 c = CHECK(mer); uint64 st, ed; if (_hashTable_BP) { st = getDecodedValue(_hashTable_BP, h * _hashWidth, _hashWidth); ed = getDecodedValue(_hashTable_BP, h * _hashWidth + _hashWidth, _hashWidth); } else { st = _hashTable_FW[h]; ed = _hashTable_FW[h+1]; } posnLen = 0; if (st == ed) return(false); for (uint64 i=st, J=st * _wFin; i 0) return(vals[2]); if (vals[1]) return(1); return(getDecodedValue(_positions, vals[0] * _posnWidth, _posnWidth)); } } return(0); } uint64 positionDB::setCount(uint64 mer, uint64 count) { uint64 h = HASH(mer); uint64 c = CHECK(mer); uint64 st, ed; if (_hashTable_BP) { st = getDecodedValue(_hashTable_BP, h * _hashWidth, _hashWidth); ed = getDecodedValue(_hashTable_BP, h * _hashWidth + _hashWidth, _hashWidth); } else { st = _hashTable_FW[h]; ed = _hashTable_FW[h+1]; } if (st == ed) return(0); for (uint64 i=st, J=st * _wFin; i 0) count = vals[3]; // What happened here: By default, the count is 1. If it is // NOT a unique mer in the table, we reset the count to the // number of entries in the table. Then, if there is a count // stored in the table, we reset the count again. // Move on to copying the data, if in the correct range. if (vals[2] == 1) { // Is a single mer in our table. Copy if the actual count is // acceptable. if ((lo <= count) && (count < hi)) { okCount++; setDecodedValues(_buckets, nb, (_sizeWidth == 0) ? 3 : 4, sizs, vals); nb += _wFin; } else { _numberOfDistinct--; _numberOfMers--; loCount++; } } else { // Mer has more than one location in the table. Copy all // locations if the count is acceptable. if ((lo <= count) && (count < hi)) { okCount++; // Copy the bucket vals[1] = np / _posnWidth; setDecodedValues(_buckets, nb, (_sizeWidth == 0) ? 3 : 4, sizs, vals); nb += _wFin; // Copy length of the positions if (cp != np) setDecodedValue(_positions, np, _posnWidth, len); np += _posnWidth; cp += _posnWidth; // Copy positions while (len > 0) { if (cp != np) setDecodedValue(_positions, np, _posnWidth, getDecodedValue(_positions, cp, _posnWidth)); np += _posnWidth; cp += _posnWidth; len--; } } else { // Not acceptable count _numberOfDistinct--; _numberOfEntries -= len; if (count < lo) loCount++; if (count > hi) hiCount++; } } // Move to the next entry st++; cb += _wFin; } // Over all entries in the bucket // Update the end position of this bucket if (_hashTable_BP) setDecodedValue(_hashTable_BP, h * _hashWidth + _hashWidth, _hashWidth, nb / _wFin); else _hashTable_FW[h+1] = nb / _wFin; } // Over all buckets fprintf(stderr, "positionDB::filter()-- Filtered "uint64FMT" kmers less than "uint64FMT"\n", loCount, lo); fprintf(stderr, "positionDB::filter()-- Filtered "uint64FMT" kmers more than "uint64FMT"\n", hiCount, hi); fprintf(stderr, "positionDB::filter()-- Saved "uint64FMT" kmers with acceptable count\n", okCount); //dump("posDB.after"); }