| 1 | #include <iostream> |
| 2 | |
| 3 | #include "Common.hpp" |
| 4 | #include "CompressedSequence.hpp" |
| 5 | #include "Kmer.hpp" |
| 6 | |
| 7 | CompressedSequence::CompressedSequence() { |
| 8 | |
| 9 | initShort(); |
| 10 | } |
| 11 | |
| 12 | CompressedSequence::~CompressedSequence() { |
| 13 | |
| 14 | clear(); |
| 15 | } |
| 16 | |
| 17 | // use: _cs = CompressedSequence(cs); |
| 18 | // pre: |
| 19 | // post: the DNA string in _cs and is the same as in cs |
| 20 | CompressedSequence::CompressedSequence(const CompressedSequence& o) { |
| 21 | |
| 22 | if (o.isShort()) { |
| 23 | |
| 24 | asBits._size = o.asBits._size; |
| 25 | memcpy(asBits._arr, o.asBits._arr, 31); |
| 26 | } |
| 27 | else setSequence(o, 0, o.size()); // copy sequence and pointers etc. |
| 28 | } |
| 29 | |
| 30 | CompressedSequence::CompressedSequence(CompressedSequence&& o) { |
| 31 | |
| 32 | if (o.isShort()) { |
| 33 | |
| 34 | asBits._size = o.asBits._size; |
| 35 | memcpy(asBits._arr, o.asBits._arr, 31); // plain vanilla copy |
| 36 | } |
| 37 | else { |
| 38 | |
| 39 | asPointer._length = o.asPointer._length; |
| 40 | asPointer._capacity = o.asPointer._capacity; |
| 41 | asPointer._data = o.asPointer._data; |
| 42 | |
| 43 | o.initShort(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // use: _cs = cs; |
| 48 | // pre: |
| 49 | // post: the DNA string in _cs is the same as in cs |
| 50 | CompressedSequence& CompressedSequence::operator=(const CompressedSequence& o) { |
| 51 | |
| 52 | if (this != &o){ |
| 53 | |
| 54 | if (o.isShort()) { |
| 55 | |
| 56 | asBits._size = o.asBits._size; |
| 57 | memcpy(asBits._arr, o.asBits._arr,31); // plain vanilla copy |
| 58 | } |
| 59 | else setSequence(o, 0, o.size()); // copy sequence and pointers etc. |
| 60 | } |
| 61 | |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | CompressedSequence& CompressedSequence::operator=(CompressedSequence&& o) { |
| 66 | |
| 67 | if (this != &o) { |
| 68 | |
| 69 | if (o.isShort()) { |
| 70 | |
| 71 | asBits._size = o.asBits._size; |
| 72 | memcpy(asBits._arr, o.asBits._arr, 31); // plain vanilla copy |
| 73 | } |
| 74 | else { |
| 75 | |
| 76 | clear(); |
| 77 | |
| 78 | asPointer._length = o.asPointer._length; |
| 79 | asPointer._capacity = o.asPointer._capacity; |
| 80 | asPointer._data = o.asPointer._data; |
| 81 | |
| 82 | o.initShort(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | // use: s |
| 90 | // pre: s has only the characters 'A','C','G' and 'T' and can have any length |
| 91 | // post: the DNA string in cs is now the same as s |
| 92 | CompressedSequence::CompressedSequence(const char *s) { |
| 93 | |
| 94 | initShort(); |
| 95 | |
| 96 | if (s != NULL) setSequence(s, 0, strlen(s)); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | // same as with char *s but with string |
| 101 | CompressedSequence::CompressedSequence(const string& s) { |
| 102 | |
| 103 | initShort(); |
| 104 | |
| 105 | setSequence(s.c_str(), 0, s.length()); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | // use: cs = CompressedSequence(km); |
| 110 | // pre: |
| 111 | // post: the DNA string in cs is now the same as the DNA string in km |
| 112 | CompressedSequence::CompressedSequence(const Kmer& km) { |
| 113 | |
| 114 | initShort(); |
| 115 | |
| 116 | setSequence(km, 0, Kmer::k); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | // use: cs._resize_and_copy(new_cap, copy_limit); |
| 121 | // pre: |
| 122 | // post: The DNA string in cs has space for at least new_length bases |
| 123 | // the first copy_limit characters of cs are the same as before this method |
| 124 | void CompressedSequence::_resize_and_copy(const size_t new_cap, const size_t copy_limit) { |
| 125 | |
| 126 | if (new_cap <= capacity()) return; |
| 127 | |
| 128 | unsigned char* new_data = new unsigned char[new_cap](); // allocate new storage |
| 129 | size_t bytes = round_to_bytes(copy_limit); |
| 130 | |
| 131 | memcpy(new_data, getPointer(), bytes); // copy old data |
| 132 | |
| 133 | if (isShort()) { |
| 134 | |
| 135 | size_t sz = size(); |
| 136 | |
| 137 | asBits._size = 0; // this is now a long sequence. |
| 138 | |
| 139 | setSize(sz); |
| 140 | |
| 141 | asPointer._data = new_data; |
| 142 | asPointer._capacity = new_cap; |
| 143 | } |
| 144 | else { |
| 145 | |
| 146 | delete[] asPointer._data; |
| 147 | |
| 148 | asPointer._data = new_data; |
| 149 | asPointer._capacity = new_cap; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void CompressedSequence::setSequence(const CompressedSequence& o, const size_t offset_o, const size_t length_o, const size_t offset, const bool reversed) { |
| 154 | |
| 155 | const size_t nb_bytes_o = round_to_bytes(length_o + offset); |
| 156 | |
| 157 | if (nb_bytes_o > capacity()) _resize_and_copy(nb_bytes_o, size()); |
| 158 | |
| 159 | unsigned char* data = const_cast<unsigned char*>(getPointer()); |
| 160 | const unsigned char* data_o = o.getPointer(); |
| 161 | |
| 162 | size_t w_index = offset; |
| 163 | size_t wi, wj, index_o; |
| 164 | |
| 165 | if (reversed){ |
| 166 | |
| 167 | index_o = offset_o + length_o - 1; |
| 168 | |
| 169 | for (size_t i = 0; i < length_o; ++i, ++w_index, --index_o) { |
| 170 | |
| 171 | wi = w_index >> 2; |
| 172 | wj = (w_index & 0x3) << 1; |
| 173 | |
| 174 | data[wi] &= ~(0x3 << wj); // clear bits |
| 175 | data[wi] |= (3 - ((data_o[index_o >> 2] >> ((index_o & 0x3) << 1)) & 0x3)) << wj; |
| 176 | } |
| 177 | } |
| 178 | else { |
| 179 | |
| 180 | index_o = offset_o; |
| 181 | |
| 182 | for (size_t i = 0; i < length_o; ++i, ++w_index, ++index_o) { |
| 183 | |
| 184 | wi = w_index >> 2; |
| 185 | wj = (w_index & 0x3) << 1; |
| 186 | |
| 187 | data[wi] &= ~(0x3 << wj); // clear bits |
| 188 | data[wi] |= ((data_o[index_o >> 2] >> ((index_o & 0x3) << 1)) & 0x3) << wj; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // new length? |
| 193 | if (length_o + offset > size()) setSize(length_o + offset); |
| 194 | } |
| 195 | |
| 196 | void CompressedSequence::setSequence(const char *s, const size_t offset, const size_t length, const bool reversed) { |
| 197 | |
| 198 | const size_t rounded_length = round_to_bytes(length); |
| 199 | |
| 200 | const char* s_offset = s + offset; |
| 201 | |
| 202 | if (rounded_length > capacity()) _resize_and_copy(rounded_length, size()); |
| 203 | |
| 204 | unsigned char* data = const_cast<unsigned char*>(getPointer()); |
| 205 | |
| 206 | if (reversed) { |
| 207 | |
| 208 | for (size_t index = 0; index < length; ++index) { |
| 209 | |
| 210 | const size_t i = index >> 2; |
| 211 | const size_t j = (index & 0x3) << 1; |
| 212 | const uint8_t c = bases[0x03-bits[static_cast<uint8_t>(s_offset[length-index-1])]]; |
| 213 | |
| 214 | data[i] &= ~(0x03 << j); // set bits to 0, default |
| 215 | data[i] |= (bits[c] << j); |
| 216 | } |
| 217 | } |
| 218 | else { |
| 219 | |
| 220 | for (size_t index = 0; index < length; ++index) { |
| 221 | |
| 222 | const size_t i = index >> 2; |
| 223 | const size_t j = (index & 0x3) << 1; |
| 224 | const uint8_t c = static_cast<uint8_t>(s_offset[index]); |
| 225 | |
| 226 | data[i] &= ~(0x03 << j); // set bits to 0, default |
| 227 | data[i] |= (bits[c] << j); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (length > size()) setSize(length); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | // use: s = cs.toString(offset, length); |
| 236 | // pre: offset + length <= cs.size(), |
| 237 | // post: s is the DNA string from c[offset,...,offset+length-1] |
| 238 | string CompressedSequence::toString(const size_t offset, const size_t length) const { |
| 239 | |
| 240 | const unsigned char* data = getPointer(); |
| 241 | |
| 242 | string s(length, 0); |
| 243 | |
| 244 | for (size_t index = offset; index < offset+length; ++index) { |
| 245 | |
| 246 | s[index-offset] = bases[(data[index >> 2] >> ((index & 0x3) << 1)) & 0x03]; |
| 247 | } |
| 248 | |
| 249 | return s; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | // use: s = cs.toString(offset, length); |
| 254 | // pre: offset + length <= cs.size() |
| 255 | // s has space for length characters |
| 256 | // post: s is the same as cs[offset,...,offset+length-1] |
| 257 | void CompressedSequence::toString(char *s, const size_t offset, const size_t length) const { |
| 258 | |
| 259 | const unsigned char* data = getPointer(); |
| 260 | |
| 261 | for (size_t index = offset; index < offset+length; ++index) { |
| 262 | |
| 263 | s[index-offset] = bases[(data[index >> 2] >> ((index & 0x3) << 1)) & 0x03]; |
| 264 | } |
| 265 | |
| 266 | s[length] = 0; // 0-terminated string |
| 267 | } |
| 268 | |
| 269 | bool CompressedSequence::write(std::ostream& stream_out) const { |
| 270 | |
| 271 | if (stream_out.fail()) return false; |
| 272 | |
| 273 | const unsigned char* data = getPointer(); |
| 274 | const size_t len = size(); |
| 275 | const size_t data_sz = round_to_bytes(len); |
| 276 | |
| 277 | stream_out.write(reinterpret_cast<const char*>(&len), sizeof(size_t)); |
| 278 | stream_out.write(reinterpret_cast<const char*>(data), data_sz * sizeof(unsigned char)); |
| 279 | |
| 280 | return !stream_out.fail(); |
| 281 | } |
| 282 | |
| 283 | bool CompressedSequence::read(std::istream& stream_in) { |
| 284 | |
| 285 | if (!stream_in.fail()) { |
| 286 | |
| 287 | clear(); |
| 288 | |
| 289 | size_t len = 0; |
| 290 | |
| 291 | stream_in.read(reinterpret_cast<char*>(&len), sizeof(size_t)); |
| 292 | |
| 293 | if (len != 0) { |
| 294 | |
| 295 | const size_t data_sz = round_to_bytes(len); |
| 296 | |
| 297 | _resize_and_copy(data_sz, size()); |
| 298 | setSize(len); |
| 299 | |
| 300 | unsigned char* data = const_cast<unsigned char*>(getPointer()); |
| 301 | |
| 302 | stream_in.read(reinterpret_cast<char*>(data), data_sz * sizeof(unsigned char)); |
| 303 | } |
| 304 | |
| 305 | return !stream_in.fail(); |
| 306 | } |
| 307 | |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | char CompressedSequence::getChar(const size_t offset) const { |
| 312 | |
| 313 | return bases[(getPointer()[offset >> 2] >> ((offset & 0x3) << 1)) & 0x03]; |
| 314 | } |
| 315 | |
| 316 | Kmer CompressedSequence::getKmer(const size_t offset) const { |
| 317 | |
| 318 | Kmer km; |
| 319 | |
| 320 | const unsigned char* data = getPointer(); |
| 321 | |
| 322 | const size_t len = offset + Kmer::k; |
| 323 | |
| 324 | if ((offset & 0x3) == 0){ // Extraction byte to byte is possible |
| 325 | |
| 326 | const size_t nbytes = (Kmer::k + 3) / 4; |
| 327 | |
| 328 | size_t i = offset >> 2, j = 0; |
| 329 | |
| 330 | for (; j < nbytes - 1; ++i, ++j) km.bytes[(7 - (j & 0x7)) + ((j >> 3) << 3)] = revBits[data[i]]; |
| 331 | |
| 332 | unsigned char tmp_km = 0; |
| 333 | unsigned char tmp_data = data[i]; |
| 334 | |
| 335 | for (i <<= 2; i < len; ++i, tmp_data >>= 2) tmp_km = (tmp_km << 2) | (tmp_data & 0x3); |
| 336 | |
| 337 | tmp_km <<= ((4 - (Kmer::k & 0x3)) << 1) & 0x7; |
| 338 | km.bytes[(7 - (j & 0x7)) + ((j >> 3) << 3)] = tmp_km; |
| 339 | } |
| 340 | else { // Extraction 2 bits per 2 bits |
| 341 | |
| 342 | const size_t nlongs = (Kmer::k + 31) / 32; |
| 343 | |
| 344 | size_t j = 0; |
| 345 | |
| 346 | for (size_t i = offset; j < nlongs; ++j){ |
| 347 | |
| 348 | uint64_t tmp_km = 0; |
| 349 | const size_t end = len < i + 32 ? len : i + 32; |
| 350 | |
| 351 | for (; i != end; ++i) tmp_km = (tmp_km << 2) | ((data[i >> 2] >> ((i & 0x3) << 1)) & 0x3); |
| 352 | |
| 353 | km.longs[j] = tmp_km; |
| 354 | } |
| 355 | |
| 356 | km.longs[j-1] <<= (32 - (Kmer::k & 0x1f)) << 1; |
| 357 | } |
| 358 | |
| 359 | return km; |
| 360 | } |
| 361 | |
| 362 | Minimizer CompressedSequence::getMinimizer(const size_t offset) const { |
| 363 | |
| 364 | Minimizer minz; |
| 365 | |
| 366 | const unsigned char* data = getPointer(); |
| 367 | |
| 368 | const size_t len = offset + Minimizer::g; |
| 369 | |
| 370 | if ((offset & 0x3) == 0){ // Extraction byte to byte is possible |
| 371 | |
| 372 | const size_t nbytes = (Minimizer::g + 3) / 4; |
| 373 | |
| 374 | size_t i = offset >> 2, j = 0; |
| 375 | |
| 376 | for (; j < nbytes - 1; ++i, ++j) minz.bytes[(7 - (j & 0x7)) + ((j >> 3) << 3)] = revBits[data[i]]; |
| 377 | |
| 378 | unsigned char tmp_km = 0; |
| 379 | unsigned char tmp_data = data[i]; |
| 380 | |
| 381 | for (i <<= 2; i < len; ++i, tmp_data >>= 2) tmp_km = (tmp_km << 2) | (tmp_data & 0x3); |
| 382 | |
| 383 | tmp_km <<= ((4 - (Minimizer::g & 0x3)) << 1) & 0x7; |
| 384 | minz.bytes[(7 - (j & 0x7)) + ((j >> 3) << 3)] = tmp_km; |
| 385 | } |
| 386 | else { // Extraction 2 bits per 2 bits |
| 387 | |
| 388 | const size_t nlongs = (Minimizer::g + 31) / 32; |
| 389 | |
| 390 | size_t j = 0; |
| 391 | |
| 392 | for (size_t i = offset; j < nlongs; ++j){ |
| 393 | |
| 394 | uint64_t tmp_km = 0; |
| 395 | const size_t end = len < i + 32 ? len : i + 32; |
| 396 | |
| 397 | for (; i != end; ++i) tmp_km = (tmp_km << 2) | ((data[i >> 2] >> ((i & 0x3) << 1)) & 0x3); |
| 398 | |
| 399 | minz.longs[j] = tmp_km; |
| 400 | } |
| 401 | |
| 402 | minz.longs[j-1] <<= (32 - (Minimizer::g & 0x1f)) << 1; |
| 403 | } |
| 404 | |
| 405 | return minz; |
| 406 | } |
| 407 | |
| 408 | bool CompressedSequence::compareKmer(const size_t offset, const size_t length, const Kmer& km) const { |
| 409 | |
| 410 | const unsigned char* data = getPointer(); |
| 411 | |
| 412 | if ((length > Kmer::k) || ((offset + length) > size()) || km.isEmpty() || km.isDeleted()) return false; |
| 413 | |
| 414 | if ((offset & 0x3) == 0){ // Comparison byte to byte is possible |
| 415 | |
| 416 | const size_t nbytes = (length + 3) / 4; |
| 417 | |
| 418 | size_t i = offset >> 2, j = 0; |
| 419 | |
| 420 | while (j < nbytes - 1){ // Check full bytes (bytes containing 4 characters to compare) |
| 421 | |
| 422 | j += ((static_cast<size_t>(data[i] == revBits[km.bytes[(7 - (j & 0x7)) + ((j >> 3) << 3)]]) - 1) & nbytes) + 1; |
| 423 | ++i; |
| 424 | } |
| 425 | |
| 426 | if (j != nbytes - 1) return false; |
| 427 | if ((length & 0x3) == 0) return (data[i] == revBits[km.bytes[(7 - (j & 0x7)) + ((j >> 3) << 3)]]); // Last byte is also a full byte |
| 428 | |
| 429 | const char mask = (0x1ULL << ((length & 0x3) << 1)) - 1; |
| 430 | |
| 431 | return (data[i] & mask) == (revBits[km.bytes[(7 - (j & 0x7)) + ((j >> 3) << 3)]] & mask); |
| 432 | } |
| 433 | else { //Comparison 2 bits per 2 bits |
| 434 | |
| 435 | size_t iu = offset, ik = 0; |
| 436 | |
| 437 | while (ik < length){ |
| 438 | |
| 439 | const size_t cu = (data[iu >> 2] >> ((iu & 0x3) << 1)) & 0x3; |
| 440 | const size_t ck = (km.longs[ik >> 5] >> (62 - ((ik & 0x1F) << 1))) & 0x3; |
| 441 | |
| 442 | ik += ((static_cast<size_t>(cu == ck) - 1) & length) + 1; |
| 443 | ++iu; |
| 444 | } |
| 445 | |
| 446 | return (ik == length); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | int64_t CompressedSequence::findKmer(const Kmer& km) const { |
| 451 | |
| 452 | const int k = Kmer::k; |
| 453 | |
| 454 | const size_t sz = size(); |
| 455 | |
| 456 | if (sz >= k){ |
| 457 | |
| 458 | Kmer km_cs = getKmer(0); |
| 459 | if (km_cs == km) return 0; |
| 460 | |
| 461 | if (sz > k){ |
| 462 | |
| 463 | size_t i = k; |
| 464 | const unsigned char* data = getPointer(); |
| 465 | unsigned char tmp = data[i >> 2] >> ((i & 0x3) << 1); |
| 466 | |
| 467 | for (; i < sz; ++i, tmp >>= 2){ |
| 468 | |
| 469 | if ((i & 0x3) == 0) tmp = data[i >> 2]; |
| 470 | |
| 471 | km_cs.selfForwardBase(bases[tmp & 0x3]); |
| 472 | |
| 473 | if (km_cs == km) return i-k+1; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | // use: j = cs.jump(s,i,pos,reversed) |
| 483 | // pre: 0 <= i < s.length, -1 <= pos < cs._length if reversed true, 0 <= pos <= cs._length if reversed false |
| 484 | // post: if reversed == false |
| 485 | // s[i...i+j-1] == cs._data[pos...pos+j-1], 0 <= j <= min(s.length-i, cs._length-pos) |
| 486 | // else |
| 487 | // reverse_complement(s[i...i+j-1]) == cs._data[pos-j+1...pos], 0 <= j <= min(s.length-i, pos+1) |
| 488 | /*size_t CompressedSequence::jump(const char *s, const size_t i, int pos, const bool reversed) const { |
| 489 | |
| 490 | assert(i >= 0); |
| 491 | assert(i < strlen(s)); |
| 492 | assert(pos >= -1); |
| 493 | assert(0 <= size() - pos); // this prevents -1 <= _length from giving false |
| 494 | |
| 495 | const unsigned char* data = getPointer(); |
| 496 | |
| 497 | size_t i_cpy = i; |
| 498 | |
| 499 | if (reversed){ |
| 500 | |
| 501 | if (pos == -1) return 0; |
| 502 | |
| 503 | int idx_div = pos >> 2; |
| 504 | int idx_mod = (pos & 0x3) << 1; |
| 505 | |
| 506 | for (; (s[i_cpy] != '\0') && (pos != -1); --pos, ++i_cpy, idx_mod -= 2) { |
| 507 | |
| 508 | if (idx_mod == -2){ |
| 509 | --idx_div; |
| 510 | idx_mod = 6; |
| 511 | } |
| 512 | |
| 513 | if (s[i_cpy] != bases[3-((data[idx_div] >> idx_mod) & 0x03)]) break; |
| 514 | } |
| 515 | } |
| 516 | else { |
| 517 | |
| 518 | size_t cs_size = size(); |
| 519 | |
| 520 | if (pos == cs_size) return 0; |
| 521 | |
| 522 | unsigned char tmp = data[pos >> 2] >> ((pos & 0x3) << 1); |
| 523 | |
| 524 | for (; (s[i_cpy] != '\0') && (pos != cs_size); ++pos, ++i_cpy, tmp >>= 2) { |
| 525 | |
| 526 | if ((pos & 0x3) == 0) tmp = data[pos >> 2]; |
| 527 | if (s[i_cpy] != bases[tmp & 0x3]) break; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | return i_cpy - i; |
| 532 | }*/ |
| 533 | |
| 534 | size_t CompressedSequence::jump(const char *s, const size_t i, int pos, const bool reversed) const { |
| 535 | |
| 536 | const unsigned char* data = getPointer(); |
| 537 | const char* s_tmp = &s[i]; |
| 538 | |
| 539 | if (reversed){ |
| 540 | |
| 541 | for (; (*s_tmp != '\0') && (pos != -1); --pos, ++s_tmp) { |
| 542 | |
| 543 | if (*s_tmp != bases[3 - ((data[pos >> 2] >> ((pos & 0x3) << 1)) & 0x03)]) break; |
| 544 | } |
| 545 | } |
| 546 | else { |
| 547 | |
| 548 | const size_t cs_size = size(); |
| 549 | |
| 550 | for (; (*s_tmp != '\0') && (pos < cs_size); ++pos, ++s_tmp) { |
| 551 | |
| 552 | if (*s_tmp != bases[(data[pos >> 2] >> ((pos & 0x3) << 1)) & 0x3]) break; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | return s_tmp - &s[i]; |
| 557 | } |
| 558 | |
| 559 | /*size_t CompressedSequence::bw_jump(const char *s, const size_t i, int pos, const bool reversed) const { |
| 560 | |
| 561 | assert(i >= 0); |
| 562 | assert(i < strlen(s)); |
| 563 | assert(pos >= -1); |
| 564 | assert(0 <= size() - pos); // this prevents -1 <= _length from giving false |
| 565 | |
| 566 | const unsigned char* data = getPointer(); |
| 567 | |
| 568 | size_t i_cpy = i; |
| 569 | |
| 570 | if (reversed){ |
| 571 | |
| 572 | size_t cs_size = size(); |
| 573 | |
| 574 | if (pos == cs_size) return 0; |
| 575 | |
| 576 | unsigned char tmp = data[pos/4] >> (2 * (pos % 4)); |
| 577 | |
| 578 | for (; (i_cpy != -1) && (pos != cs_size); ++pos, --i_cpy, tmp >>= 2) { |
| 579 | |
| 580 | if (pos%4 == 0) tmp = data[pos/4]; |
| 581 | if (s[i_cpy] != bases[3 - (tmp & 0x3)]) break; |
| 582 | } |
| 583 | } |
| 584 | else { |
| 585 | |
| 586 | if (pos == -1) return 0; |
| 587 | |
| 588 | int idx_div = pos / 4; |
| 589 | int idx_mod = 2 * (pos % 4); |
| 590 | |
| 591 | for (; (i_cpy != -1) && (pos != -1); --pos, --i_cpy, idx_mod -= 2) { |
| 592 | |
| 593 | if (idx_mod == -2){ |
| 594 | idx_div--; |
| 595 | idx_mod = 6; |
| 596 | } |
| 597 | |
| 598 | if (s[i_cpy] != bases[(data[idx_div] >> idx_mod) & 0x03]) break; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | return i - i_cpy; |
| 603 | }*/ |
| 604 | |
| 605 | void CompressedSequence::clear() { |
| 606 | |
| 607 | if (!isShort() && (asPointer._capacity > 0) && (asPointer._data != NULL)) delete[] asPointer._data; |
| 608 | |
| 609 | initShort(); |
| 610 | } |
| 611 | |
| 612 | const char CompressedSequence::bases[256] = { |
| 613 | 'A','C','G','T','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 614 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 615 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 616 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 617 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 618 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 619 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 620 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 621 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 622 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 623 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 624 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 625 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 626 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 627 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N', |
| 628 | 'N','N','N','N','N','N','N','N', 'N','N','N','N','N','N','N','N' |
| 629 | }; |
| 630 | |
| 631 | const uint8_t CompressedSequence::bits[256] = { |
| 632 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 633 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 634 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 635 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 636 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 637 | 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 638 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 639 | 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 640 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 641 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 642 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 643 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 644 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 645 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 646 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 647 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 648 | }; |
| 649 | |
| 650 | const uint8_t CompressedSequence::revBits[256] = |
| 651 | { |
| 652 | 0x0, 0x40, 0x80, 0xc0, 0x10, 0x50, 0x90, 0xd0, 0x20, 0x60, 0xa0, 0xe0, 0x30, 0x70, 0xb0, 0xf0, |
| 653 | 0x4, 0x44, 0x84, 0xc4, 0x14, 0x54, 0x94, 0xd4, 0x24, 0x64, 0xa4, 0xe4, 0x34, 0x74, 0xb4, 0xf4, |
| 654 | 0x8, 0x48, 0x88, 0xc8, 0x18, 0x58, 0x98, 0xd8, 0x28, 0x68, 0xa8, 0xe8, 0x38, 0x78, 0xb8, 0xf8, |
| 655 | 0xc, 0x4c, 0x8c, 0xcc, 0x1c, 0x5c, 0x9c, 0xdc, 0x2c, 0x6c, 0xac, 0xec, 0x3c, 0x7c, 0xbc, 0xfc, |
| 656 | 0x1, 0x41, 0x81, 0xc1, 0x11, 0x51, 0x91, 0xd1, 0x21, 0x61, 0xa1, 0xe1, 0x31, 0x71, 0xb1, 0xf1, |
| 657 | 0x5, 0x45, 0x85, 0xc5, 0x15, 0x55, 0x95, 0xd5, 0x25, 0x65, 0xa5, 0xe5, 0x35, 0x75, 0xb5, 0xf5, |
| 658 | 0x9, 0x49, 0x89, 0xc9, 0x19, 0x59, 0x99, 0xd9, 0x29, 0x69, 0xa9, 0xe9, 0x39, 0x79, 0xb9, 0xf9, |
| 659 | 0xd, 0x4d, 0x8d, 0xcd, 0x1d, 0x5d, 0x9d, 0xdd, 0x2d, 0x6d, 0xad, 0xed, 0x3d, 0x7d, 0xbd, 0xfd, |
| 660 | 0x2, 0x42, 0x82, 0xc2, 0x12, 0x52, 0x92, 0xd2, 0x22, 0x62, 0xa2, 0xe2, 0x32, 0x72, 0xb2, 0xf2, |
| 661 | 0x6, 0x46, 0x86, 0xc6, 0x16, 0x56, 0x96, 0xd6, 0x26, 0x66, 0xa6, 0xe6, 0x36, 0x76, 0xb6, 0xf6, |
| 662 | 0xa, 0x4a, 0x8a, 0xca, 0x1a, 0x5a, 0x9a, 0xda, 0x2a, 0x6a, 0xaa, 0xea, 0x3a, 0x7a, 0xba, 0xfa, |
| 663 | 0xe, 0x4e, 0x8e, 0xce, 0x1e, 0x5e, 0x9e, 0xde, 0x2e, 0x6e, 0xae, 0xee, 0x3e, 0x7e, 0xbe, 0xfe, |
| 664 | 0x3, 0x43, 0x83, 0xc3, 0x13, 0x53, 0x93, 0xd3, 0x23, 0x63, 0xa3, 0xe3, 0x33, 0x73, 0xb3, 0xf3, |
| 665 | 0x7, 0x47, 0x87, 0xc7, 0x17, 0x57, 0x97, 0xd7, 0x27, 0x67, 0xa7, 0xe7, 0x37, 0x77, 0xb7, 0xf7, |
| 666 | 0xb, 0x4b, 0x8b, 0xcb, 0x1b, 0x5b, 0x9b, 0xdb, 0x2b, 0x6b, 0xab, 0xeb, 0x3b, 0x7b, 0xbb, 0xfb, |
| 667 | 0xf, 0x4f, 0x8f, 0xcf, 0x1f, 0x5f, 0x9f, 0xdf, 0x2f, 0x6f, 0xaf, 0xef, 0x3f, 0x7f, 0xbf, 0xff |
| 668 | }; |
| 669 |