| 1 |
#include "bcftools.pysam.h" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <stdint.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <stdbool.h> |
| 12 |
|
| 13 |
#include "edlib.h" |
| 14 |
|
| 15 |
typedef uint64_t Word; |
| 16 |
static const int WORD_SIZE = 64; |
| 17 |
static const Word WORD_1 = (Word)1; |
| 18 |
static const Word HIGH_BIT_MASK = 1LL << 63; |
| 19 |
|
| 20 |
#define MAX_UCHAR 7 // better cache usage for our data |
| 21 |
|
| 22 |
#ifndef MAX |
| 23 |
#define MAX(a,b) ((a)>(b)?(a):(b)) |
| 24 |
#endif |
| 25 |
|
| 26 |
typedef struct Block { |
| 27 |
Word P; |
| 28 |
Word M; |
| 29 |
int score; |
| 30 |
} Block; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
typedef struct EqualityDefinition { |
| 38 |
bool matrix[MAX_UCHAR + 1][MAX_UCHAR + 1]; |
| 39 |
} EqualityDefinition; |
| 40 |
|
| 41 |
static EqualityDefinition * |
| 42 |
CreateEqualityDefinition(const char *alphabet, int alphabet_size, |
| 43 |
const EdlibEqualityPair* additionalEqualities, |
| 44 |
const int additionalEqualitiesLength) { |
| 45 |
EqualityDefinition *ed = malloc(sizeof(*ed)); |
| 46 |
|
| 47 |
for (size_t i = 0; i < alphabet_size; i++) { |
| 48 |
for (size_t j = 0; j < alphabet_size; j++) { |
| 49 |
ed->matrix[i][j] = (i == j); |
| 50 |
} |
| 51 |
} |
| 52 |
if (additionalEqualities != NULL) { |
| 53 |
for (int i = 0; i < additionalEqualitiesLength; i++) { |
| 54 |
const char *firstTransformed = strchr(alphabet, additionalEqualities[i].first); |
| 55 |
const char *secondTransformed = strchr(alphabet, additionalEqualities[i].second); |
| 56 |
if (firstTransformed && alphabet_size) { |
| 57 |
ed->matrix[firstTransformed - alphabet][secondTransformed - alphabet] = |
| 58 |
ed->matrix[secondTransformed - alphabet][firstTransformed - alphabet] |
| 59 |
= true; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return ed; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
static inline const |
| 73 |
bool equalityDefinition_areEqual(const EqualityDefinition *ed, unsigned char a, unsigned char b) { |
| 74 |
return ed->matrix[a][b]; |
| 75 |
} |
| 76 |
|
| 77 |
static int myersCalcEditDistanceSemiGlobal(const Word* Peq, int W, int maxNumBlocks, |
| 78 |
int queryLength, |
| 79 |
const unsigned char* target, int targetLength, |
| 80 |
int k, EdlibAlignMode mode, |
| 81 |
int* bestScore_, int** positions_, int* numPositions_); |
| 82 |
|
| 83 |
static char *transformSequences(const char* queryOriginal, int queryLength, |
| 84 |
const char* targetOriginal, int targetLength, |
| 85 |
unsigned char** queryTransformed, |
| 86 |
unsigned char** targetTransformed, |
| 87 |
int *alphabet_size); |
| 88 |
|
| 89 |
static inline int ceilDiv(int x, int y); |
| 90 |
|
| 91 |
static inline unsigned char* createReverseCopy(const unsigned char* seq, int length); |
| 92 |
|
| 93 |
static inline Word* buildPeq(const int alphabetLength, |
| 94 |
const unsigned char* query, |
| 95 |
const int queryLength, |
| 96 |
const EqualityDefinition* equalityDefinition); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
EdlibAlignResult edlibAlign(const char* const queryOriginal, const int queryLength, |
| 103 |
const char* const targetOriginal, const int targetLength, |
| 104 |
const EdlibAlignConfig config) { |
| 105 |
EdlibAlignResult result; |
| 106 |
result.status = EDLIB_STATUS_OK; |
| 107 |
result.editDistance = -1; |
| 108 |
result.endLocations = result.startLocations = NULL; |
| 109 |
result.numLocations = 0; |
| 110 |
result.alignment = NULL; |
| 111 |
result.alignmentLength = 0; |
| 112 |
result.alphabetLength = 0; |
| 113 |
|
| 114 |
|
| 115 |
unsigned char* query, * target; |
| 116 |
int alphabet_size; |
| 117 |
char *alphabet = transformSequences(queryOriginal, queryLength, targetOriginal, targetLength, |
| 118 |
&query, &target, &alphabet_size); |
| 119 |
result.alphabetLength = alphabet_size; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if (queryLength == 0 || targetLength == 0) { |
| 124 |
if (config.mode == EDLIB_MODE_NW) { |
| 125 |
result.editDistance = MAX(queryLength, targetLength); |
| 126 |
result.endLocations = malloc(sizeof(int) * 1); |
| 127 |
result.endLocations[0] = targetLength - 1; |
| 128 |
result.numLocations = 1; |
| 129 |
} else if (config.mode == EDLIB_MODE_SHW || config.mode == EDLIB_MODE_HW) { |
| 130 |
result.editDistance = queryLength; |
| 131 |
result.endLocations = malloc(sizeof(int) * 1); |
| 132 |
result.endLocations[0] = -1; |
| 133 |
result.numLocations = 1; |
| 134 |
} else { |
| 135 |
result.status = EDLIB_STATUS_ERROR; |
| 136 |
} |
| 137 |
|
| 138 |
free(query); |
| 139 |
free(target); |
| 140 |
free(alphabet); |
| 141 |
return result; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
int maxNumBlocks = ceilDiv(queryLength, WORD_SIZE); |
| 146 |
int W = maxNumBlocks * WORD_SIZE - queryLength; |
| 147 |
EqualityDefinition *equalityDefinition = |
| 148 |
CreateEqualityDefinition(alphabet, alphabet_size, config.additionalEqualities, config.additionalEqualitiesLength); |
| 149 |
Word* Peq = buildPeq(alphabet_size, query, queryLength, equalityDefinition); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
bool dynamicK = false; |
| 157 |
int k = config.k; |
| 158 |
if (k < 0) { |
| 159 |
dynamicK = true; |
| 160 |
k = WORD_SIZE; |
| 161 |
} |
| 162 |
|
| 163 |
do { |
| 164 |
if (config.mode == EDLIB_MODE_HW || config.mode == EDLIB_MODE_SHW) { |
| 165 |
myersCalcEditDistanceSemiGlobal(Peq, W, maxNumBlocks, |
| 166 |
queryLength, target, targetLength, |
| 167 |
k, config.mode, &(result.editDistance), |
| 168 |
&(result.endLocations), &(result.numLocations)); |
| 169 |
} else { |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
} |
| 175 |
k *= 2; |
| 176 |
} while(dynamicK && result.editDistance == -1); |
| 177 |
|
| 178 |
if (result.editDistance >= 0) { |
| 179 |
|
| 180 |
if (config.mode == EDLIB_MODE_NW) { |
| 181 |
result.endLocations = malloc(sizeof(int) * 1); |
| 182 |
result.endLocations[0] = targetLength - 1; |
| 183 |
result.numLocations = 1; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
if (config.task == EDLIB_TASK_LOC || config.task == EDLIB_TASK_PATH) { |
| 188 |
result.startLocations = malloc(result.numLocations * sizeof(int)); |
| 189 |
if (config.mode == EDLIB_MODE_HW) { |
| 190 |
const unsigned char* rTarget = createReverseCopy(target, targetLength); |
| 191 |
const unsigned char* rQuery = createReverseCopy(query, queryLength); |
| 192 |
|
| 193 |
Word* rPeq = buildPeq(alphabet_size, rQuery, queryLength, equalityDefinition); |
| 194 |
for (int i = 0; i < result.numLocations; i++) { |
| 195 |
int endLocation = result.endLocations[i]; |
| 196 |
if (endLocation == -1) { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
result.startLocations[i] = 0; |
| 209 |
} else { |
| 210 |
int bestScoreSHW, numPositionsSHW; |
| 211 |
int* positionsSHW; |
| 212 |
myersCalcEditDistanceSemiGlobal( |
| 213 |
rPeq, W, maxNumBlocks, |
| 214 |
queryLength, rTarget + targetLength - endLocation - 1, endLocation + 1, |
| 215 |
result.editDistance, EDLIB_MODE_SHW, |
| 216 |
&bestScoreSHW, &positionsSHW, &numPositionsSHW); |
| 217 |
|
| 218 |
|
| 219 |
result.startLocations[i] = endLocation - positionsSHW[numPositionsSHW - 1]; |
| 220 |
free(positionsSHW); |
| 221 |
} |
| 222 |
} |
| 223 |
free((void *)rTarget); |
| 224 |
free((void *)rQuery); |
| 225 |
free(rPeq); |
| 226 |
} else { |
| 227 |
for (int i = 0; i < result.numLocations; i++) { |
| 228 |
result.startLocations[i] = 0; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
free(Peq); |
| 237 |
free(query); |
| 238 |
free(target); |
| 239 |
free(alphabet); |
| 240 |
free(equalityDefinition); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
return result; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
static inline Word* buildPeq(const int alphabetLength, |
| 254 |
const unsigned char* const query, |
| 255 |
const int queryLength, |
| 256 |
const EqualityDefinition* equalityDefinition) { |
| 257 |
int maxNumBlocks = ceilDiv(queryLength, WORD_SIZE); |
| 258 |
|
| 259 |
Word* Peq = malloc((alphabetLength + 1) * maxNumBlocks * sizeof(*Peq)); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
for (int symbol = 0; symbol < alphabetLength; symbol++) { |
| 264 |
for (int b = 0; b < maxNumBlocks; b++) { |
| 265 |
Word PeqW = 0; |
| 266 |
for (int r = (b+1) * WORD_SIZE - 1; r >= b * WORD_SIZE; r--) { |
| 267 |
PeqW = (PeqW<<1) |
| 268 |
+ (r >= queryLength |
| 269 |
|| equalityDefinition_areEqual(equalityDefinition, |
| 270 |
query[r], symbol)); |
| 271 |
} |
| 272 |
Peq[symbol * maxNumBlocks + b] = PeqW; |
| 273 |
} |
| 274 |
} |
| 275 |
{ |
| 276 |
int symbol = alphabetLength; |
| 277 |
for (int b = 0; b < maxNumBlocks; b++) { |
| 278 |
|
| 279 |
Peq[symbol * maxNumBlocks + b] = (Word)-1; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return Peq; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
static inline unsigned char* createReverseCopy(const unsigned char* const seq, const int length) { |
| 292 |
unsigned char* rSeq = malloc(length); |
| 293 |
for (int i = 0; i < length; i++) { |
| 294 |
rSeq[i] = seq[length - i - 1]; |
| 295 |
} |
| 296 |
return rSeq; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static inline int calculateBlock(Word Pv, Word Mv, Word Eq, const int hin, |
| 313 |
Word *PvOut, Word *MvOut) { |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
Word hinIsNeg = (Word)(hin >> 2) & WORD_1; |
| 320 |
|
| 321 |
Word Xv = Eq | Mv; |
| 322 |
|
| 323 |
Eq |= hinIsNeg; |
| 324 |
Word Xh = (((Eq & Pv) + Pv) ^ Pv) | Eq; |
| 325 |
|
| 326 |
Word Ph = Mv | ~(Xh | Pv); |
| 327 |
Word Mh = Pv & Xh; |
| 328 |
|
| 329 |
int hout = 0; |
| 330 |
|
| 331 |
hout = (Ph & HIGH_BIT_MASK) >> (WORD_SIZE - 1); |
| 332 |
|
| 333 |
hout -= (Mh & HIGH_BIT_MASK) >> (WORD_SIZE - 1); |
| 334 |
|
| 335 |
Ph <<= 1; |
| 336 |
Mh <<= 1; |
| 337 |
|
| 338 |
|
| 339 |
Mh |= hinIsNeg; |
| 340 |
|
| 341 |
Ph |= (Word)((hin + 1) >> 1); |
| 342 |
|
| 343 |
*PvOut = Mh | ~(Xv | Ph); |
| 344 |
*MvOut = Ph & Xv; |
| 345 |
|
| 346 |
return hout; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
static inline int ceilDiv(const int x, const int y) { |
| 354 |
return x % y ? x / y + 1 : x / y; |
| 355 |
} |
| 356 |
|
| 357 |
static inline int min(const int x, const int y) { |
| 358 |
return x < y ? x : y; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
static inline int *getBlockCellValues(const Block block) { |
| 367 |
int *scores = malloc(WORD_SIZE * sizeof(*scores)); |
| 368 |
int score = block.score; |
| 369 |
Word mask = HIGH_BIT_MASK; |
| 370 |
for (int i = 0; i < WORD_SIZE - 1; i++) { |
| 371 |
scores[i] = score; |
| 372 |
if (block.P & mask) score--; |
| 373 |
if (block.M & mask) score++; |
| 374 |
mask >>= 1; |
| 375 |
} |
| 376 |
scores[WORD_SIZE - 1] = score; |
| 377 |
return scores; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
static inline bool allBlockCellsLarger(const Block block, const int k) { |
| 386 |
int *scores = getBlockCellValues(block); |
| 387 |
for (int i = 0; i < WORD_SIZE; i++) { |
| 388 |
if (scores[i] <= k) { |
| 389 |
free(scores); |
| 390 |
return false; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
free(scores); |
| 395 |
return true; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
static int myersCalcEditDistanceSemiGlobal( |
| 418 |
const Word* const Peq, const int W, const int maxNumBlocks, |
| 419 |
const int queryLength, |
| 420 |
const unsigned char* const target, const int targetLength, |
| 421 |
int k, const EdlibAlignMode mode, |
| 422 |
int* const bestScore_, int** const positions_, int* const numPositions_) { |
| 423 |
*positions_ = NULL; |
| 424 |
*numPositions_ = 0; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
int firstBlock = 0; |
| 429 |
int lastBlock = min(ceilDiv(k + 1, WORD_SIZE), maxNumBlocks) - 1; |
| 430 |
Block *bl; |
| 431 |
|
| 432 |
Block* blocks = malloc(maxNumBlocks * sizeof(*blocks)); |
| 433 |
|
| 434 |
|
| 435 |
if (mode == EDLIB_MODE_HW) { |
| 436 |
k = min(queryLength, k); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
const int STRONG_REDUCE_NUM = 2048; |
| 442 |
|
| 443 |
|
| 444 |
bl = blocks; |
| 445 |
for (int b = 0; b <= lastBlock; b++) { |
| 446 |
bl->score = (b + 1) * WORD_SIZE; |
| 447 |
bl->P = (Word)(-1); |
| 448 |
bl->M = (Word)(0); |
| 449 |
bl++; |
| 450 |
} |
| 451 |
|
| 452 |
int bestScore = -1; |
| 453 |
#define MAX_POS 100 // maximum number of positions returned. |
| 454 |
int positions[MAX_POS]; |
| 455 |
int npositions = 0; |
| 456 |
const int startHout = mode == EDLIB_MODE_HW ? 0 : 1; |
| 457 |
const unsigned char* targetChar = target; |
| 458 |
for (int c = 0; c < targetLength; c++) { |
| 459 |
const Word* Peq_c = Peq + (*targetChar) * maxNumBlocks; |
| 460 |
|
| 461 |
|
| 462 |
int hout = startHout; |
| 463 |
bl = blocks + firstBlock; |
| 464 |
Peq_c += firstBlock; |
| 465 |
for (int b = firstBlock; b <= lastBlock; b++) { |
| 466 |
hout = calculateBlock(bl->P, bl->M, *Peq_c, hout, &bl->P, &bl->M); |
| 467 |
bl->score += hout; |
| 468 |
bl++; Peq_c++; |
| 469 |
} |
| 470 |
bl--; Peq_c--; |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
if ((lastBlock < maxNumBlocks - 1) && (bl->score - hout <= k) |
| 475 |
&& ((*(Peq_c + 1) & WORD_1) || hout < 0)) { |
| 476 |
|
| 477 |
lastBlock++; bl++; Peq_c++; |
| 478 |
bl->P = (Word)(-1); |
| 479 |
bl->M = (Word)(0); |
| 480 |
bl->score = (bl - 1)->score - hout + WORD_SIZE + calculateBlock(bl->P, bl->M, *Peq_c, hout, &bl->P, &bl->M); |
| 481 |
} else { |
| 482 |
while (lastBlock >= firstBlock && bl->score >= k + WORD_SIZE) { |
| 483 |
lastBlock--; bl--; Peq_c--; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
if (c % STRONG_REDUCE_NUM == 0) { |
| 492 |
while (lastBlock >= 0 && lastBlock >= firstBlock && allBlockCellsLarger(*bl, k)) { |
| 493 |
lastBlock--; bl--; Peq_c--; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
if (mode == EDLIB_MODE_HW && lastBlock == -1) { |
| 501 |
lastBlock++; bl++; Peq_c++; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
if (mode != EDLIB_MODE_HW) { |
| 506 |
while (firstBlock <= lastBlock && blocks[firstBlock].score >= k + WORD_SIZE) { |
| 507 |
firstBlock++; |
| 508 |
} |
| 509 |
if (c % STRONG_REDUCE_NUM == 0) { |
| 510 |
while (firstBlock <= lastBlock && allBlockCellsLarger(blocks[firstBlock], k)) { |
| 511 |
firstBlock++; |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
if (lastBlock < firstBlock) { |
| 518 |
*bestScore_ = bestScore; |
| 519 |
if (bestScore != -1) { |
| 520 |
*positions_ = malloc(npositions * sizeof(int)); |
| 521 |
*numPositions_ = npositions; |
| 522 |
memcpy(*positions_, positions, npositions * sizeof(int)); |
| 523 |
} |
| 524 |
free(blocks); |
| 525 |
return EDLIB_STATUS_OK; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
if (lastBlock == maxNumBlocks - 1) { |
| 531 |
int colScore = bl->score; |
| 532 |
if (colScore <= k) { |
| 533 |
|
| 534 |
if (bestScore == -1 || colScore <= bestScore) { |
| 535 |
if (colScore != bestScore) { |
| 536 |
npositions = 0; |
| 537 |
bestScore = colScore; |
| 538 |
|
| 539 |
|
| 540 |
k = bestScore; |
| 541 |
} |
| 542 |
if (npositions < MAX_POS) |
| 543 |
positions[npositions++] = c - W; |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
targetChar++; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
if (lastBlock == maxNumBlocks - 1) { |
| 555 |
int *blockScores = getBlockCellValues(*bl); |
| 556 |
for (int i = 0; i < W; i++) { |
| 557 |
int colScore = blockScores[i + 1]; |
| 558 |
if (colScore <= k && (bestScore == -1 || colScore <= bestScore)) { |
| 559 |
if (colScore != bestScore) { |
| 560 |
npositions = 0; |
| 561 |
k = bestScore = colScore; |
| 562 |
} |
| 563 |
if (npositions < MAX_POS) |
| 564 |
positions[npositions++] = targetLength - W + i; |
| 565 |
} |
| 566 |
} |
| 567 |
free(blockScores); |
| 568 |
} |
| 569 |
|
| 570 |
*bestScore_ = bestScore; |
| 571 |
if (bestScore != -1) { |
| 572 |
*positions_ = malloc(npositions * sizeof(int)); |
| 573 |
*numPositions_ = npositions; |
| 574 |
memcpy(*positions_, positions, npositions * sizeof(int)); |
| 575 |
} |
| 576 |
|
| 577 |
free(blocks); |
| 578 |
return EDLIB_STATUS_OK; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
static char *transformSequences(const char* const queryOriginal, const int queryLength, |
| 601 |
const char* const targetOriginal, const int targetLength, |
| 602 |
unsigned char** const queryTransformed, |
| 603 |
unsigned char** const targetTransformed, |
| 604 |
int *alphabet_size) { |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
*queryTransformed = malloc(sizeof(unsigned char) * queryLength); |
| 610 |
*targetTransformed = malloc(sizeof(unsigned char) * targetLength); |
| 611 |
|
| 612 |
char *alphabet = malloc(MAX_UCHAR+1), *alphabet_cp = alphabet; |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
unsigned char letterIdx[MAX_UCHAR + 1]; |
| 617 |
bool inAlphabet[MAX_UCHAR + 1]; |
| 618 |
for (int i = 0; i < MAX_UCHAR + 1; i++) inAlphabet[i] = false; |
| 619 |
|
| 620 |
for (int i = 0; i < queryLength; i++) { |
| 621 |
unsigned char c = queryOriginal[i]; |
| 622 |
if (!inAlphabet[c]) { |
| 623 |
inAlphabet[c] = true; |
| 624 |
letterIdx[c] = alphabet_cp - alphabet; |
| 625 |
*alphabet_cp++ = queryOriginal[i]; |
| 626 |
} |
| 627 |
(*queryTransformed)[i] = letterIdx[c]; |
| 628 |
} |
| 629 |
for (int i = 0; i < targetLength; i++) { |
| 630 |
unsigned char c = targetOriginal[i]; |
| 631 |
if (!inAlphabet[c]) { |
| 632 |
inAlphabet[c] = true; |
| 633 |
letterIdx[c] = alphabet_cp - alphabet; |
| 634 |
*alphabet_cp++ = targetOriginal[i]; |
| 635 |
} |
| 636 |
(*targetTransformed)[i] = letterIdx[c]; |
| 637 |
} |
| 638 |
|
| 639 |
*alphabet_size = alphabet_cp - alphabet; |
| 640 |
return alphabet; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
EdlibAlignConfig edlibNewAlignConfig(int k, EdlibAlignMode mode, EdlibAlignTask task, |
| 645 |
const EdlibEqualityPair* additionalEqualities, |
| 646 |
int additionalEqualitiesLength) { |
| 647 |
EdlibAlignConfig config; |
| 648 |
config.k = k; |
| 649 |
config.mode = mode; |
| 650 |
config.task = task; |
| 651 |
config.additionalEqualities = additionalEqualities; |
| 652 |
config.additionalEqualitiesLength = additionalEqualitiesLength; |
| 653 |
return config; |
| 654 |
} |
| 655 |
|
| 656 |
EdlibAlignConfig edlibDefaultAlignConfig(void) { |
| 657 |
return edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_DISTANCE, NULL, 0); |
| 658 |
} |
| 659 |
|
| 660 |
void edlibFreeAlignResult(EdlibAlignResult result) { |
| 661 |
if (result.endLocations) free(result.endLocations); |
| 662 |
if (result.startLocations) free(result.startLocations); |
| 663 |
if (result.alignment) free(result.alignment); |
| 664 |
} |