/usr/include/kmer/bio/kmerhuge.H is in libkmer-dev 0~20150903+r2013-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #define MERWORD(N) _md[N]
class kMerHuge {
public:
kMerHuge(uint32 ms=uint32ZERO) {
setMerSize(ms);
clear();
};
~kMerHuge() {
};
void setMerSize(uint32 ms);
uint32 getMerSize(void) const { return(_merSize); };
void setMerSpan(uint32 ms) { _merSpan = ms; };
uint32 getMerSpan(void) const { return(_merSpan); };
kMerHuge &reverseComplement(void) {
for (uint32 i=0, j=KMER_WORDS-1; i<KMER_WORDS/2; i++, j--) {
uint64 t = MERWORD(i);
MERWORD(i) = MERWORD(j);
MERWORD(j) = t;
}
for (uint32 i=0; i<KMER_WORDS; i++) {
MERWORD(i) = ((MERWORD(i) >> 2) & 0x3333333333333333llu) | ((MERWORD(i) << 2) & 0xccccccccccccccccllu);
MERWORD(i) = ((MERWORD(i) >> 4) & 0x0f0f0f0f0f0f0f0fllu) | ((MERWORD(i) << 4) & 0xf0f0f0f0f0f0f0f0llu);
MERWORD(i) = ((MERWORD(i) >> 8) & 0x00ff00ff00ff00ffllu) | ((MERWORD(i) << 8) & 0xff00ff00ff00ff00llu);
MERWORD(i) = ((MERWORD(i) >> 16) & 0x0000ffff0000ffffllu) | ((MERWORD(i) << 16) & 0xffff0000ffff0000llu);
MERWORD(i) = ((MERWORD(i) >> 32) & 0x00000000ffffffffllu) | ((MERWORD(i) << 32) & 0xffffffff00000000llu);
MERWORD(i) ^= 0xffffffffffffffffllu;
}
*this >>= KMER_WORDS * 64 - 2 * _merSize;
return(*this);
};
void clear(void) {
for (uint32 i=0; i<KMER_WORDS; i++)
MERWORD(i) = uint64ZERO;
};
void smallest(void) {
clear();
};
void largest(void) {
clear();
reverseComplement();
};
private:
void operator>>=(uint32 x) {
// thisWord, the word we shift bits into
// thatWord, the word we shift bits out of
// shift, the number of bits we shift
//
uint32 thisWord = 0;
uint32 thatWord = x >> 6;
uint32 shift = x & uint32MASK(6);
// Do an initial word-size shift, to reduce the shift amount to
// be less than wordsize. Fill any shifted-out words with zero.
//
if (thatWord) {
while (thatWord < KMER_WORDS)
MERWORD(thisWord++) = MERWORD(thatWord++);
while (thisWord < KMER_WORDS)
MERWORD(thisWord++) = 0;
}
// Do bit-size shift, of adjacent words
//
thisWord = 0;
thatWord = 1;
MERWORD(thisWord) >>= shift;
while (thatWord < KMER_WORDS) {
MERWORD(thisWord++) |= MERWORD(thatWord) << (64 - shift);
MERWORD(thatWord++) >>= shift;
}
};
void operator<<=(uint32 x) {
uint32 thisWord = KMER_WORDS;
uint32 thatWord = KMER_WORDS - (x >> 6);
uint32 shift = x & uint32MASK(6);
if (thatWord != KMER_WORDS) {
while (thatWord > 0)
MERWORD(--thisWord) = MERWORD(--thatWord);
while (thisWord > 0)
MERWORD(--thisWord) = 0;
}
thisWord = KMER_WORDS;
thatWord = KMER_WORDS - 1;
MERWORD(thisWord-1) <<= shift;
while (thatWord > 0) {
--thisWord;
--thatWord;
MERWORD(thisWord) |= MERWORD(thatWord) >> (64 - shift);
MERWORD(thatWord) <<= shift;
}
};
public:
void operator+=(uint64 x) {
*this <<= 2;
assert((x & 0xfc) == 0);
MERWORD(0) |= x & uint64NUMBER(0x3);
};
void operator-=(uint64 x) {
*this >>= 2;
assert((x & 0xfc) == 0);
MERWORD(_lastWord) |= (x & uint64NUMBER(0x3)) << _lastShift;
};
void mask(bool full) {
MERWORD(_maskWord) &= _mask;
if (full)
for (uint32 x=_maskWord+1; x<KMER_WORDS; x++)
MERWORD(x) = uint64ZERO;
};
public:
bool operator!=(kMerHuge const &r) const {
uint64 res = uint64ZERO;
for (uint32 i=KMER_WORDS; i--; )
res |= MERWORD(i) ^ r.MERWORD(i);
return(res != uint64ZERO);
};
bool operator==(kMerHuge const &r) const {
uint64 res = uint64ZERO;
for (uint32 i=KMER_WORDS; i--; )
res |= MERWORD(i) ^ r.MERWORD(i);
return(res == uint64ZERO);
};
bool operator<(kMerHuge const &r) const {
for (uint32 i=KMER_WORDS; i--; ) {
if (MERWORD(i) < r.MERWORD(i)) return(true);
if (MERWORD(i) > r.MERWORD(i)) return(false);
}
return(false);
};
bool operator>(kMerHuge const &r) const {
for (uint32 i=KMER_WORDS; i--; ) {
if (MERWORD(i) > r.MERWORD(i)) return(true);
if (MERWORD(i) < r.MERWORD(i)) return(false);
}
return(false);
};
bool operator<=(kMerHuge const &r) const {
for (uint32 i=KMER_WORDS; i--; ) {
if (MERWORD(i) < r.MERWORD(i)) return(true);
if (MERWORD(i) > r.MERWORD(i)) return(false);
}
return(true);
};
bool operator>=(kMerHuge const &r) const {
for (uint32 i=KMER_WORDS; i--; ) {
if (MERWORD(i) > r.MERWORD(i)) return(true);
if (MERWORD(i) < r.MERWORD(i)) return(false);
}
return(true);
};
int qsort_less(kMerHuge const &r) const {
for (uint32 i=KMER_WORDS; i--; ) {
if (MERWORD(i) < r.MERWORD(i)) return(-1);
if (MERWORD(i) > r.MERWORD(i)) return(1);
}
return(0);
};
public:
operator uint64 () const {return(MERWORD(0));};
public:
// these should work generically for both big and small
void writeToBitPackedFile(bitPackedFile *BPF, uint32 numBits=0) const {
if (numBits == 0)
numBits = _merSize << 1;
uint32 lastWord = numBits >> 6;
if ((numBits & uint32MASK(6)) == 0)
lastWord++;
if (numBits & uint32MASK(6))
BPF->putBits(MERWORD(lastWord), numBits & uint32MASK(6));
while (lastWord > 0) {
lastWord--;
BPF->putBits(MERWORD(lastWord), 64);
}
};
void readFromBitPackedFile(bitPackedFile *BPF, uint32 numBits=0) {
if (numBits == 0)
numBits = _merSize << 1;
uint32 lastWord = numBits >> 6;
if ((numBits & uint32MASK(6)) == 0)
lastWord++;
if (numBits & uint32MASK(6))
MERWORD(lastWord) = BPF->getBits(numBits & uint32MASK(6));
while (lastWord > 0) {
lastWord--;
MERWORD(lastWord) = BPF->getBits(64);
}
};
public:
// these should work generically for both big and small
void setBits(uint32 pos, uint32 numbits, uint64 val) {
uint32 wrd = pos >> 6;
uint32 bit = pos & 0x3f;
val &= uint64MASK(numbits);
if (wrd >= KMER_WORDS) {
fprintf(stderr, "kMer::setBits()-- ERROR: tried to set pos="uint32FMT" numbits="uint32FMT" larger than KMER_WORDS=%d\n",
pos, numbits, KMER_WORDS), exit(1);
}
// If we have enough space in the word for the bits, replace
// those bits in the word. Otherwise we need to split the value
// into two pieces, and add to the end of the first word and the
// start of the second.
if (64 - bit >= numbits) {
MERWORD(wrd) &= ~(uint64MASK(numbits) << bit);
MERWORD(wrd) |= val << bit;
} else {
if (wrd+1 >= KMER_WORDS) {
fprintf(stderr, "kMer::setBits()-- ERROR: tried to set pos="uint32FMT" numbits="uint32FMT" larger than KMER_WORDS=%d\n",
pos, numbits, KMER_WORDS), exit(1);
}
uint32 b1 = 64 - bit; // bits in the first word
uint32 b2 = numbits - b1; // bits in the second word
MERWORD(wrd) &= ~(uint64MASK(b1) << bit);
MERWORD(wrd) |= (val & uint64MASK(b1)) << bit;
MERWORD(wrd+1) &= ~(uint64MASK(b2));
MERWORD(wrd+1) |= (val >> b1) & uint64MASK(b2);
}
};
uint64 getBits(uint32 pos, uint32 numbits) const {
uint64 val = uint64ZERO;
uint32 wrd = pos >> 6;
uint32 bit = pos & 0x3f;
if (wrd >= KMER_WORDS) {
fprintf(stderr, "kMer::getBits()-- ERROR: tried to get pos="uint32FMT" numbits="uint32FMT" larger than KMER_WORDS=%d\n",
pos, numbits, KMER_WORDS), exit(1);
}
if (64 - bit >= numbits) {
val = MERWORD(wrd) >> bit;
} else {
if (wrd+1 >= KMER_WORDS) {
fprintf(stderr, "kMer::getBits()-- ERROR: tried to get pos="uint32FMT" numbits="uint32FMT" larger than KMER_WORDS=%d\n",
pos, numbits, KMER_WORDS), exit(1);
}
uint32 b1 = 64 - bit; // bits in the first word
uint32 b2 = numbits - b1; // bits in the second word
val = MERWORD(wrd) >> (64-b1);
val |= (MERWORD(wrd+1) & uint64MASK(b2)) << b1;
}
val &= uint64MASK(numbits);
return(val);
};
public:
// these should work generically for both big and small
uint64 startOfMer(uint32 bits) const {
return(getBits((_merSize << 1) - bits, bits));
};
uint64 endOfMer(uint32 bits) const {
return(MERWORD(0) & uint64MASK(bits));
};
public:
// these should work generically for both big and small
uint64 getWord(uint32 wrd) const { return(MERWORD(wrd)); };
void setWord(uint32 wrd, uint64 val) { MERWORD(wrd) = val; };
public:
char *merToString(char *instr) const;
private:
uint64 _md[KMER_WORDS];
// The _merSize is always the number of letters in the mer -- if we
// are a spaced seed, it is the weight.
//
uint32 _merSize;
uint32 _merSpan;
// The mask is used to make sure the mer has only _merSize bases
// set -- we can get more than that if we shift to the left. The
// _maskWord is the word that we want to mask:
//
uint64 _mask;
uint32 _maskWord;
// For operator-=() (add a base to the left end) we need to know
// what the last word is, and how far to shift the bits.
//
// _lastWord -- the last word that contains bases
// _lastShift -- the amount we need to shift left to put bits 0 and 1
// into the last base
uint32 _lastWord;
uint32 _lastShift;
};
inline
void
kMerHuge::setMerSize(uint32 ms) {
_merSize = ms;
_merSpan = ms;
_lastWord = (2 * ms - 2) / 64;
_lastShift = (2 * ms - 2) % 64;
_mask = uint64ZERO;
_maskWord = _merSize / 32;
// Filled whole words with the mer, the mask is special-cased
// to clear the whole next word, unless there is no whole next
// word, then it does nothing on the last word.
//
// Otherwise, we can construct the mask as usual.
//
if ((_merSize % 32) == 0) {
if (_maskWord >= KMER_WORDS) {
_maskWord = KMER_WORDS - 1;
_mask = ~uint64ZERO;
} else {
_maskWord = _merSize / 32;
_mask = uint64ZERO;
}
} else {
_mask = uint64MASK((_merSize % 32) << 1);
}
if (_maskWord >= KMER_WORDS) {
fprintf(stderr, "kMer::setMerSize()-- ERROR! Desired merSize of "uint32FMT" larger than\n", _merSize);
fprintf(stderr, " available storage space (KMER_WORDS=%d, max merSize %d).\n", KMER_WORDS, KMER_WORDS*32);
exit(1);
}
}
inline
char *
kMerHuge::merToString(char *instr) const {
uint32 lastWord = _merSize >> 5;
char *str = instr;
if ((_merSize & uint32MASK(6)) == 0)
lastWord++;
if (_merSize & uint32MASK(5)) {
uint64ToMerString(_merSize & uint32MASK(5), MERWORD(lastWord), str);
str += _merSize & uint32MASK(5);
}
while (lastWord > 0) {
lastWord--;
uint64ToMerString(32, MERWORD(lastWord), str);
str += 32;
}
return(instr);
};
|