| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <esl_config.h> |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
#include <ctype.h> |
| 15 |
#include <math.h> |
| 16 |
#ifdef HAVE_STRINGS_H |
| 17 |
#include <strings.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "easel.h" |
| 21 |
#include "esl_mem.h" |
| 22 |
|
| 23 |
#include "esl_alphabet.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static ESL_ALPHABET *create_rna(void); |
| 32 |
static ESL_ALPHABET *create_dna(void); |
| 33 |
static ESL_ALPHABET *create_amino(void); |
| 34 |
static ESL_ALPHABET *create_coins(void); |
| 35 |
static ESL_ALPHABET *create_dice(void); |
| 36 |
|
| 37 |
static int set_complementarity(ESL_ALPHABET *a); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
ESL_ALPHABET * |
| 53 |
esl_alphabet_Create(int type) |
| 54 |
{ |
| 55 |
ESL_ALPHABET *a = NULL; |
| 56 |
|
| 57 |
switch(type) { |
| 58 |
case eslRNA: a = create_rna(); break; |
| 59 |
case eslDNA: a = create_dna(); break; |
| 60 |
case eslAMINO: a = create_amino(); break; |
| 61 |
case eslCOINS: a = create_coins(); break; |
| 62 |
case eslDICE: a = create_dice(); break; |
| 63 |
default: esl_fatal("bad alphabet type: unrecognized"); |
| 64 |
} |
| 65 |
return a; |
| 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 |
ESL_ALPHABET * |
| 99 |
esl_alphabet_CreateCustom(const char *alphabet, int K, int Kp) |
| 100 |
{ |
| 101 |
ESL_ALPHABET *a = NULL; |
| 102 |
int c,x,y; |
| 103 |
int status; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if (strlen(alphabet) != Kp) ESL_XEXCEPTION(eslEINVAL, "alphabet length != Kp"); |
| 108 |
if (Kp < K+4) ESL_XEXCEPTION(eslEINVAL, "Kp too small in alphabet"); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
ESL_ALLOC(a, sizeof(ESL_ALPHABET)); |
| 113 |
a->sym = NULL; |
| 114 |
a->degen = NULL; |
| 115 |
a->ndegen = NULL; |
| 116 |
a->complement = NULL; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
ESL_ALLOC(a->sym, sizeof(char) * (Kp+1)); |
| 121 |
ESL_ALLOC(a->ndegen, sizeof(int) * Kp); |
| 122 |
ESL_ALLOC(a->degen, sizeof(char *) * Kp); |
| 123 |
a->degen[0] = NULL; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
ESL_ALLOC(a->degen[0], sizeof(char) * (Kp*K)); |
| 128 |
for (x = 1; x < Kp; x++) |
| 129 |
a->degen[x] = a->degen[0]+(K*x); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
a->type = eslNONSTANDARD; |
| 134 |
a->K = K; |
| 135 |
a->Kp = Kp; |
| 136 |
strcpy(a->sym, alphabet); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
for (c = 0; c < 128; c++) a->inmap[c] = eslDSQ_ILLEGAL; |
| 142 |
for (x = 0; x < a->Kp; x++) a->inmap[(int) a->sym[x]] = x; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
for (x = 0; x < a->Kp; x++) |
| 151 |
{ |
| 152 |
a->ndegen[x] = 0; |
| 153 |
for (y = 0; y < a->K; y++) a->degen[x][y] = 0; |
| 154 |
} |
| 155 |
for (x = 0; x < a->K; x++) |
| 156 |
{ |
| 157 |
a->ndegen[x] = 1; |
| 158 |
a->degen[x][x] = 1; |
| 159 |
} |
| 160 |
|
| 161 |
a->ndegen[Kp-3] = K; |
| 162 |
for (x = 0; x < a->K; x++) a->degen[Kp-3][x] = 1; |
| 163 |
|
| 164 |
return a; |
| 165 |
|
| 166 |
ERROR: |
| 167 |
esl_alphabet_Destroy(a); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
static ESL_ALPHABET * |
| 176 |
create_rna(void) |
| 177 |
{ |
| 178 |
ESL_ALPHABET *a = NULL; |
| 179 |
int status; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if ((a = esl_alphabet_CreateCustom("ACGU-RYMKSWHBVDN*~", 4, 18)) == NULL) return NULL; |
| 184 |
a->type = eslRNA; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
esl_alphabet_SetEquiv(a, 'T', 'U'); |
| 189 |
esl_alphabet_SetEquiv(a, 'X', 'N'); |
| 190 |
esl_alphabet_SetEquiv(a, 'I', 'A'); |
| 191 |
esl_alphabet_SetEquiv(a, '_', '-'); |
| 192 |
esl_alphabet_SetEquiv(a, '.', '-'); |
| 193 |
esl_alphabet_SetCaseInsensitive(a); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
esl_alphabet_SetDegeneracy(a, 'R', "AG"); |
| 198 |
esl_alphabet_SetDegeneracy(a, 'Y', "CU"); |
| 199 |
esl_alphabet_SetDegeneracy(a, 'M', "AC"); |
| 200 |
esl_alphabet_SetDegeneracy(a, 'K', "GU"); |
| 201 |
esl_alphabet_SetDegeneracy(a, 'S', "CG"); |
| 202 |
esl_alphabet_SetDegeneracy(a, 'W', "AU"); |
| 203 |
esl_alphabet_SetDegeneracy(a, 'H', "ACU"); |
| 204 |
esl_alphabet_SetDegeneracy(a, 'B', "CGU"); |
| 205 |
esl_alphabet_SetDegeneracy(a, 'V', "ACG"); |
| 206 |
esl_alphabet_SetDegeneracy(a, 'D', "AGU"); |
| 207 |
|
| 208 |
if ( (status = set_complementarity(a)) != eslOK) goto ERROR; |
| 209 |
|
| 210 |
return a; |
| 211 |
|
| 212 |
ERROR: |
| 213 |
esl_alphabet_Destroy(a); |
| 214 |
return NULL; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
static ESL_ALPHABET * |
| 222 |
create_dna(void) |
| 223 |
{ |
| 224 |
ESL_ALPHABET *a = NULL; |
| 225 |
int status; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if ((a = esl_alphabet_CreateCustom("ACGT-RYMKSWHBVDN*~", 4, 18)) == NULL) return NULL; |
| 230 |
a->type = eslDNA; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
esl_alphabet_SetEquiv(a, 'U', 'T'); |
| 235 |
esl_alphabet_SetEquiv(a, 'X', 'N'); |
| 236 |
esl_alphabet_SetEquiv(a, 'I', 'A'); |
| 237 |
esl_alphabet_SetEquiv(a, '_', '-'); |
| 238 |
esl_alphabet_SetEquiv(a, '.', '-'); |
| 239 |
esl_alphabet_SetCaseInsensitive(a); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
esl_alphabet_SetDegeneracy(a, 'R', "AG"); |
| 244 |
esl_alphabet_SetDegeneracy(a, 'Y', "CT"); |
| 245 |
esl_alphabet_SetDegeneracy(a, 'M', "AC"); |
| 246 |
esl_alphabet_SetDegeneracy(a, 'K', "GT"); |
| 247 |
esl_alphabet_SetDegeneracy(a, 'S', "CG"); |
| 248 |
esl_alphabet_SetDegeneracy(a, 'W', "AT"); |
| 249 |
esl_alphabet_SetDegeneracy(a, 'H', "ACT"); |
| 250 |
esl_alphabet_SetDegeneracy(a, 'B', "CGT"); |
| 251 |
esl_alphabet_SetDegeneracy(a, 'V', "ACG"); |
| 252 |
esl_alphabet_SetDegeneracy(a, 'D', "AGT"); |
| 253 |
|
| 254 |
if ( (status = set_complementarity(a)) != eslOK) goto ERROR; |
| 255 |
return a; |
| 256 |
|
| 257 |
ERROR: |
| 258 |
esl_alphabet_Destroy(a); |
| 259 |
return NULL; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static ESL_ALPHABET * |
| 267 |
create_amino(void) |
| 268 |
{ |
| 269 |
ESL_ALPHABET *a = NULL; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if ((a = esl_alphabet_CreateCustom("ACDEFGHIKLMNPQRSTVWY-BJZOUX*~", 20, 29)) == NULL) return NULL; |
| 274 |
a->type = eslAMINO; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
esl_alphabet_SetEquiv(a, '_', '-'); |
| 279 |
esl_alphabet_SetEquiv(a, '.', '-'); |
| 280 |
esl_alphabet_SetCaseInsensitive(a); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
esl_alphabet_SetDegeneracy(a, 'B', "ND"); |
| 285 |
esl_alphabet_SetDegeneracy(a, 'J', "IL"); |
| 286 |
esl_alphabet_SetDegeneracy(a, 'Z', "QE"); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
esl_alphabet_SetDegeneracy(a, 'U', "C"); |
| 291 |
esl_alphabet_SetDegeneracy(a, 'O', "K"); |
| 292 |
|
| 293 |
return a; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
static ESL_ALPHABET * |
| 301 |
create_coins(void) |
| 302 |
{ |
| 303 |
ESL_ALPHABET *a = NULL; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if ((a = esl_alphabet_CreateCustom("HT-X*~", 2, 6)) == NULL) return NULL; |
| 308 |
a->type = eslCOINS; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
esl_alphabet_SetEquiv(a, '_', '-'); |
| 313 |
esl_alphabet_SetEquiv(a, '.', '-'); |
| 314 |
esl_alphabet_SetCaseInsensitive(a); |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
return a; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
static ESL_ALPHABET * |
| 325 |
create_dice(void) |
| 326 |
{ |
| 327 |
ESL_ALPHABET *a = NULL; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if ((a = esl_alphabet_CreateCustom("123456-X*~", 6, 10)) == NULL) return NULL; |
| 332 |
a->type = eslCOINS; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
esl_alphabet_SetEquiv(a, '_', '-'); |
| 337 |
esl_alphabet_SetEquiv(a, '.', '-'); |
| 338 |
esl_alphabet_SetCaseInsensitive(a); |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
return a; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
static int |
| 351 |
set_complementarity(ESL_ALPHABET *a) |
| 352 |
{ |
| 353 |
int status; |
| 354 |
|
| 355 |
if (a->type != eslRNA && a->type != eslDNA) |
| 356 |
ESL_EXCEPTION(eslEINVAL, "alphabet isn't nucleic: no complementarity to set"); |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
ESL_DASSERT1(( a->Kp == 18 )); |
| 363 |
ESL_DASSERT1(( a->sym[17] == '~' )); |
| 364 |
|
| 365 |
ESL_ALLOC(a->complement, sizeof(ESL_DSQ) * a->Kp); |
| 366 |
a->complement[0] = 3; |
| 367 |
a->complement[1] = 2; |
| 368 |
a->complement[2] = 1; |
| 369 |
a->complement[3] = 0; |
| 370 |
a->complement[4] = 4; |
| 371 |
a->complement[5] = 6; |
| 372 |
a->complement[6] = 5; |
| 373 |
a->complement[7] = 8; |
| 374 |
a->complement[8] = 7; |
| 375 |
a->complement[9] = 9; |
| 376 |
a->complement[10]= 10; |
| 377 |
a->complement[11]= 14; |
| 378 |
a->complement[12]= 13; |
| 379 |
a->complement[13]= 12; |
| 380 |
a->complement[14]= 11; |
| 381 |
a->complement[15]= 15; |
| 382 |
a->complement[16]= 16; |
| 383 |
a->complement[17]= 17; |
| 384 |
|
| 385 |
return eslOK; |
| 386 |
|
| 387 |
ERROR: |
| 388 |
return status; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
int |
| 408 |
esl_alphabet_SetEquiv(ESL_ALPHABET *a, char sym, char c) |
| 409 |
{ |
| 410 |
char *sp = NULL; |
| 411 |
ESL_DSQ x; |
| 412 |
|
| 413 |
|
| 414 |
if ((sp = strchr(a->sym, sym)) != NULL) |
| 415 |
ESL_EXCEPTION(eslEINVAL, "symbol %c is already in internal alphabet, can't equivalence it", sym); |
| 416 |
if ((sp = strchr(a->sym, c)) == NULL) |
| 417 |
ESL_EXCEPTION(eslEINVAL, "char %c not in the alphabet, can't map to it", c); |
| 418 |
|
| 419 |
x = sp - a->sym; |
| 420 |
a->inmap[(int) sym] = x; |
| 421 |
return eslOK; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
int |
| 443 |
esl_alphabet_SetCaseInsensitive(ESL_ALPHABET *a) |
| 444 |
{ |
| 445 |
int lc, uc; |
| 446 |
|
| 447 |
for (lc = 'a'; lc <= 'z'; lc++) |
| 448 |
{ |
| 449 |
uc = toupper(lc); |
| 450 |
|
| 451 |
if (esl_abc_CIsValid(a, lc) && ! esl_abc_CIsValid(a, uc)) a->inmap[uc] = a->inmap[lc]; |
| 452 |
else if (esl_abc_CIsValid(a, uc) && ! esl_abc_CIsValid(a, lc)) a->inmap[lc] = a->inmap[uc]; |
| 453 |
else if (esl_abc_CIsValid(a, lc) && esl_abc_CIsValid(a, uc) && a->inmap[uc] != a->inmap[lc]) |
| 454 |
ESL_EXCEPTION(eslECORRUPT, "symbols %c and %c map differently already (%c vs. %c)", |
| 455 |
lc, uc, a->inmap[lc], a->inmap[uc]); |
| 456 |
} |
| 457 |
return eslOK; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
int |
| 485 |
esl_alphabet_SetDegeneracy(ESL_ALPHABET *a, char c, char *ds) |
| 486 |
{ |
| 487 |
char *sp; |
| 488 |
ESL_DSQ x,y; |
| 489 |
|
| 490 |
if ((sp = strchr(a->sym, c)) == NULL) |
| 491 |
ESL_EXCEPTION(eslEINVAL, "no such degenerate character"); |
| 492 |
x = sp - a->sym; |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
if (x == a->Kp-3) |
| 499 |
ESL_EXCEPTION(eslEINVAL, "can't redefine all-degenerate char %c", c); |
| 500 |
if (x < a->K+1 || x >= a->Kp-2) |
| 501 |
ESL_EXCEPTION(eslEINVAL, "char %c isn't in expected position in alphabet", c); |
| 502 |
|
| 503 |
while (*ds != '\0') { |
| 504 |
if ((sp = strchr(a->sym, *ds)) == NULL) ESL_EXCEPTION(eslEINVAL, "no such base character"); |
| 505 |
y = sp - a->sym; |
| 506 |
if (! esl_abc_XIsCanonical(a, y)) ESL_EXCEPTION(eslEINVAL, "can't map degeneracy to noncanonical character"); |
| 507 |
|
| 508 |
a->degen[x][y] = 1; |
| 509 |
a->ndegen[x]++; |
| 510 |
ds++; |
| 511 |
} |
| 512 |
return eslOK; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
int |
| 537 |
esl_alphabet_SetIgnored(ESL_ALPHABET *a, const char *ignoredchars) |
| 538 |
{ |
| 539 |
int i; |
| 540 |
for (i = 0; ignoredchars[i] != '\0'; i++) a->inmap[(int)ignoredchars[i]] = eslDSQ_IGNORED; |
| 541 |
return eslOK; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
size_t |
| 551 |
esl_alphabet_Sizeof(ESL_ALPHABET *a) |
| 552 |
{ |
| 553 |
size_t n = 0; |
| 554 |
n += sizeof(ESL_ALPHABET); |
| 555 |
n += sizeof(char) * a->Kp; |
| 556 |
n += sizeof(char *) * a->Kp; |
| 557 |
n += sizeof(char) * (a->Kp * a->K); |
| 558 |
n += sizeof(int) * a->Kp; |
| 559 |
if (a->complement) n += sizeof(ESL_DSQ) * a->Kp; |
| 560 |
return n; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
void |
| 573 |
esl_alphabet_Destroy(ESL_ALPHABET *a) |
| 574 |
{ |
| 575 |
if (a) |
| 576 |
{ |
| 577 |
if (a->sym) free(a->sym); |
| 578 |
if (a->ndegen) free(a->ndegen); |
| 579 |
if (a->degen) |
| 580 |
{ |
| 581 |
if (a->degen[0]) free(a->degen[0]); |
| 582 |
free(a->degen); |
| 583 |
} |
| 584 |
if (a->complement) free(a->complement); |
| 585 |
free(a); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
int |
| 635 |
esl_abc_CreateDsq(const ESL_ALPHABET *a, const char *seq, ESL_DSQ **ret_dsq) |
| 636 |
{ |
| 637 |
ESL_DSQ *dsq = NULL; |
| 638 |
int status; |
| 639 |
int64_t L; |
| 640 |
|
| 641 |
L = strlen(seq); |
| 642 |
ESL_ALLOC(dsq, sizeof(ESL_DSQ) * (L+2)); |
| 643 |
status = esl_abc_Digitize(a, seq, dsq); |
| 644 |
|
| 645 |
if (ret_dsq != NULL) *ret_dsq = dsq; else free(dsq); |
| 646 |
return status; |
| 647 |
|
| 648 |
ERROR: |
| 649 |
if (dsq != NULL) free(dsq); |
| 650 |
if (ret_dsq != NULL) *ret_dsq = NULL; |
| 651 |
return status; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
int |
| 677 |
esl_abc_Digitize(const ESL_ALPHABET *a, const char *seq, ESL_DSQ *dsq) |
| 678 |
{ |
| 679 |
int status; |
| 680 |
int64_t i; |
| 681 |
int64_t j; |
| 682 |
ESL_DSQ x; |
| 683 |
|
| 684 |
status = eslOK; |
| 685 |
dsq[0] = eslDSQ_SENTINEL; |
| 686 |
for (i = 0, j = 1; seq[i] != '\0'; i++) |
| 687 |
{ |
| 688 |
x = a->inmap[(int) seq[i]]; |
| 689 |
if (esl_abc_XIsValid(a, x)) dsq[j] = x; |
| 690 |
else if (x == eslDSQ_IGNORED) continue; |
| 691 |
else { |
| 692 |
status = eslEINVAL; |
| 693 |
dsq[j] = esl_abc_XGetUnknown(a); |
| 694 |
} |
| 695 |
j++; |
| 696 |
} |
| 697 |
dsq[j] = eslDSQ_SENTINEL; |
| 698 |
return status; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
int |
| 720 |
esl_abc_Textize(const ESL_ALPHABET *a, const ESL_DSQ *dsq, int64_t L, char *seq) |
| 721 |
{ |
| 722 |
int64_t i; |
| 723 |
|
| 724 |
for (i = 0; i < L; i++) |
| 725 |
seq[i] = a->sym[dsq[i+1]]; |
| 726 |
seq[i] = '\0'; |
| 727 |
return eslOK; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
int |
| 763 |
esl_abc_TextizeN(const ESL_ALPHABET *a, const ESL_DSQ *dptr, int64_t L, char *buf) |
| 764 |
{ |
| 765 |
int64_t i; |
| 766 |
|
| 767 |
for (i = 0; i < L; i++) |
| 768 |
{ |
| 769 |
if (dptr[i] == eslDSQ_SENTINEL) |
| 770 |
{ |
| 771 |
buf[i] = '\0'; |
| 772 |
return eslOK; |
| 773 |
} |
| 774 |
buf[i] = a->sym[dptr[i]]; |
| 775 |
} |
| 776 |
return eslOK; |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
int |
| 790 |
esl_abc_dsqcpy(const ESL_DSQ *dsq, int64_t L, ESL_DSQ *dcopy) |
| 791 |
{ |
| 792 |
memcpy(dcopy, dsq, sizeof(ESL_DSQ) * (L+2)); |
| 793 |
return eslOK; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
int |
| 821 |
esl_abc_dsqdup(const ESL_DSQ *dsq, int64_t L, ESL_DSQ **ret_dup) |
| 822 |
{ |
| 823 |
int status; |
| 824 |
ESL_DSQ *new = NULL; |
| 825 |
|
| 826 |
if (ret_dup == NULL) return eslOK; |
| 827 |
|
| 828 |
*ret_dup = NULL; |
| 829 |
if (dsq == NULL) return eslOK; |
| 830 |
if (L < 0) L = esl_abc_dsqlen(dsq); |
| 831 |
|
| 832 |
ESL_ALLOC(new, sizeof(ESL_DSQ) * (L+2)); |
| 833 |
memcpy(new, dsq, sizeof(ESL_DSQ) * (L+2)); |
| 834 |
|
| 835 |
*ret_dup = new; |
| 836 |
return eslOK; |
| 837 |
|
| 838 |
ERROR: |
| 839 |
if (new != NULL) free(new); |
| 840 |
if (ret_dup != NULL) *ret_dup = NULL; |
| 841 |
return status; |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
int |
| 923 |
esl_abc_dsqcat(const ESL_DSQ *inmap, ESL_DSQ **dsq, int64_t *L, const char *s, esl_pos_t n) |
| 924 |
{ |
| 925 |
int status = eslOK; |
| 926 |
|
| 927 |
if (*L < 0) *L = ((*dsq) ? esl_abc_dsqlen(*dsq) : 0); |
| 928 |
if ( n < 0) n = ( (s) ? strlen(s) : 0); |
| 929 |
|
| 930 |
if (n == 0) { goto ERROR; } |
| 931 |
|
| 932 |
if (*dsq == NULL) { |
| 933 |
ESL_ALLOC(*dsq, sizeof(ESL_DSQ) * (n+2)); |
| 934 |
(*dsq)[0] = eslDSQ_SENTINEL; |
| 935 |
} else |
| 936 |
ESL_REALLOC(*dsq, sizeof(ESL_DSQ) * (*L+n+2)); |
| 937 |
|
| 938 |
return esl_abc_dsqcat_noalloc(inmap, *dsq, L, s, n); |
| 939 |
|
| 940 |
ERROR: |
| 941 |
return status; |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
int |
| 961 |
esl_abc_dsqcat_noalloc(const ESL_DSQ *inmap, ESL_DSQ *dsq, int64_t *L, const char *s, esl_pos_t n) |
| 962 |
{ |
| 963 |
int64_t xpos; |
| 964 |
esl_pos_t cpos; |
| 965 |
ESL_DSQ x; |
| 966 |
int status = eslOK; |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
for (xpos = *L+1, cpos = 0; cpos < n; cpos++) |
| 973 |
{ |
| 974 |
if (! isascii(s[cpos])) { dsq[xpos++] = inmap[0]; status = eslEINVAL; continue; } |
| 975 |
|
| 976 |
x = inmap[(int) s[cpos]]; |
| 977 |
|
| 978 |
if (x <= 127) dsq[xpos++] = x; |
| 979 |
else switch (x) { |
| 980 |
case eslDSQ_SENTINEL: ESL_EXCEPTION(eslEINCONCEIVABLE, "input char mapped to eslDSQ_SENTINEL"); break; |
| 981 |
case eslDSQ_ILLEGAL: dsq[xpos++] = inmap[0]; status = eslEINVAL; break; |
| 982 |
case eslDSQ_IGNORED: break; |
| 983 |
case eslDSQ_EOL: ESL_EXCEPTION(eslEINCONCEIVABLE, "input char mapped to eslDSQ_EOL"); break; |
| 984 |
case eslDSQ_EOD: ESL_EXCEPTION(eslEINCONCEIVABLE, "input char mapped to eslDSQ_EOD"); break; |
| 985 |
default: ESL_EXCEPTION(eslEINCONCEIVABLE, "bad inmap, no such ESL_DSQ code"); break; |
| 986 |
} |
| 987 |
} |
| 988 |
dsq[xpos] = eslDSQ_SENTINEL; |
| 989 |
*L = xpos-1; |
| 990 |
return status; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
int64_t |
| 1002 |
esl_abc_dsqlen(const ESL_DSQ *dsq) |
| 1003 |
{ |
| 1004 |
int64_t n = 0; |
| 1005 |
while (dsq[n+1] != eslDSQ_SENTINEL) n++; |
| 1006 |
return n; |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
int64_t |
| 1017 |
esl_abc_dsqrlen(const ESL_ALPHABET *abc, const ESL_DSQ *dsq) |
| 1018 |
{ |
| 1019 |
int64_t n = 0; |
| 1020 |
int64_t i; |
| 1021 |
|
| 1022 |
for (i = 1; dsq[i] != eslDSQ_SENTINEL; i++) |
| 1023 |
if (esl_abc_XIsResidue(abc, dsq[i])) n++; |
| 1024 |
return n; |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
int |
| 1053 |
esl_abc_CDealign(const ESL_ALPHABET *abc, char *s, const ESL_DSQ *ref_ax, int64_t *opt_rlen) |
| 1054 |
{ |
| 1055 |
int64_t apos; |
| 1056 |
int64_t n = 0; |
| 1057 |
|
| 1058 |
if (s == NULL) return eslOK; |
| 1059 |
|
| 1060 |
for (n=0, apos=1; ref_ax[apos] != eslDSQ_SENTINEL; apos++) |
| 1061 |
if (! esl_abc_XIsGap(abc, ref_ax[apos]) && ! esl_abc_XIsMissing(abc, ref_ax[apos]) ) |
| 1062 |
s[n++] = s[apos-1]; |
| 1063 |
s[n] = '\0'; |
| 1064 |
|
| 1065 |
if (opt_rlen != NULL) *opt_rlen = n; |
| 1066 |
return eslOK; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
int |
| 1083 |
esl_abc_XDealign(const ESL_ALPHABET *abc, ESL_DSQ *x, const ESL_DSQ *ref_ax, int64_t *opt_rlen) |
| 1084 |
{ |
| 1085 |
int64_t apos; |
| 1086 |
int64_t n = 0; |
| 1087 |
|
| 1088 |
if (x == NULL) return eslOK; |
| 1089 |
|
| 1090 |
x[0] = eslDSQ_SENTINEL; |
| 1091 |
for (n=1, apos=1; ref_ax[apos] != eslDSQ_SENTINEL; apos++) |
| 1092 |
if (! esl_abc_XIsGap(abc, ref_ax[apos]) && ! esl_abc_XIsMissing(abc, ref_ax[apos]) ) |
| 1093 |
x[n++] = x[apos]; |
| 1094 |
x[n] = eslDSQ_SENTINEL; |
| 1095 |
|
| 1096 |
if (opt_rlen != NULL) *opt_rlen = n-1; |
| 1097 |
return eslOK; |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
int |
| 1119 |
esl_abc_ConvertDegen2X(const ESL_ALPHABET *abc, ESL_DSQ *dsq) |
| 1120 |
{ |
| 1121 |
int64_t i; |
| 1122 |
|
| 1123 |
for (i = 1; dsq[i] != eslDSQ_SENTINEL; i++) |
| 1124 |
if (esl_abc_XIsDegenerate(abc, dsq[i])) |
| 1125 |
dsq[i] = esl_abc_XGetUnknown(abc); |
| 1126 |
return eslOK; |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
int |
| 1146 |
esl_abc_revcomp(const ESL_ALPHABET *abc, ESL_DSQ *dsq, int n) |
| 1147 |
{ |
| 1148 |
ESL_DSQ x; |
| 1149 |
int pos; |
| 1150 |
|
| 1151 |
if (abc->complement == NULL) |
| 1152 |
ESL_EXCEPTION(eslEINCOMPAT, "tried to reverse complement using an alphabet that doesn't have one"); |
| 1153 |
|
| 1154 |
for (pos = 1; pos <= n/2; pos++) |
| 1155 |
{ |
| 1156 |
x = abc->complement[dsq[n-pos+1]]; |
| 1157 |
dsq[n-pos+1] = abc->complement[dsq[pos]]; |
| 1158 |
dsq[pos] = x; |
| 1159 |
} |
| 1160 |
if (n%2) dsq[pos] = abc->complement[dsq[pos]]; |
| 1161 |
return eslOK; |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
int |
| 1188 |
esl_abc_ValidateType(int type) |
| 1189 |
{ |
| 1190 |
if (type <= 0 || type > eslNONSTANDARD) return eslFAIL; |
| 1191 |
else return eslOK; |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
int |
| 1251 |
esl_abc_GuessAlphabet(const int64_t *ct, int *ret_type) |
| 1252 |
{ |
| 1253 |
int type = eslUNKNOWN; |
| 1254 |
char aaonly[] = "EFIJLOPQZ"; |
| 1255 |
char allcanon[] = "ACG"; |
| 1256 |
char aacanon[] = "DHKMRSVWY"; |
| 1257 |
int64_t n1, n2, n3, nn, nt, nu, nx, n; |
| 1258 |
int x1, x2, x3, xn, xt, xu; |
| 1259 |
int i, x; |
| 1260 |
|
| 1261 |
x1 = x2 = x3 = xn = xt = xu = 0; |
| 1262 |
n1 = n2 = n3 = n = 0; |
| 1263 |
for (i = 0; i < 26; i++) n += ct[i]; |
| 1264 |
for (i = 0; aaonly[i] != '\0'; i++) { x = ct[aaonly[i] - 'A']; if (x > 0) { n1 += x; x1++; } } |
| 1265 |
for (i = 0; allcanon[i] != '\0'; i++) { x = ct[allcanon[i] - 'A']; if (x > 0) { n2 += x; x2++; } } |
| 1266 |
for (i = 0; aacanon[i] != '\0'; i++) { x = ct[aacanon[i] - 'A']; if (x > 0) { n3 += x; x3++; } } |
| 1267 |
nt = ct['T' - 'A']; xt = (nt ? 1 : 0); |
| 1268 |
nu = ct['U' - 'A']; xu = (nu ? 1 : 0); |
| 1269 |
nx = ct['X' - 'A']; |
| 1270 |
nn = ct['N' - 'A']; xn = (nn ? 1 : 0); |
| 1271 |
|
| 1272 |
if (n <= 10) type = eslUNKNOWN; |
| 1273 |
else if (n > 2000 && nn == n) type = eslDNA; |
| 1274 |
else if (n1 > 0) type = eslAMINO; |
| 1275 |
else if (n-(n2+nt+nn) <= 0.02*n && x2+xt == 4) type = eslDNA; |
| 1276 |
else if (n-(n2+nu+nn) <= 0.02*n && x2+xu == 4) type = eslRNA; |
| 1277 |
else if (n-(n1+n2+n3+nn+nt+nx) <= 0.02*n && n3>n2 && x1+x2+x3+xn+xt >= 15) type = eslAMINO; |
| 1278 |
|
| 1279 |
*ret_type = type; |
| 1280 |
if (type == eslUNKNOWN) return eslENOALPHABET; |
| 1281 |
else return eslOK; |
| 1282 |
} |
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
double |
| 1323 |
esl_abc_Match(const ESL_ALPHABET *abc, ESL_DSQ x, ESL_DSQ y, double *p) |
| 1324 |
{ |
| 1325 |
int i; |
| 1326 |
double prob; |
| 1327 |
double sx, sy; |
| 1328 |
|
| 1329 |
|
| 1330 |
if (esl_abc_XIsCanonical(abc, x) && esl_abc_XIsCanonical(abc, y)) |
| 1331 |
{ |
| 1332 |
if (x==y) return 1.0; else return 0.0; |
| 1333 |
} |
| 1334 |
if ( ! esl_abc_XIsResidue(abc, x) || ! esl_abc_XIsResidue(abc, x)) return 0.0; |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
if (p != NULL) |
| 1339 |
{ |
| 1340 |
prob = sx = sy = 0.; |
| 1341 |
for (i = 0; i < abc->K; i++) |
| 1342 |
{ |
| 1343 |
if (abc->degen[(int)x][i]) sx += p[i]; |
| 1344 |
if (abc->degen[(int)y][i]) sy += p[i]; |
| 1345 |
if (abc->degen[(int)x][i] && abc->degen[(int)y][i]) prob += p[i] * p[i]; |
| 1346 |
} |
| 1347 |
prob = prob / (sx*sy); |
| 1348 |
} |
| 1349 |
else |
| 1350 |
{ |
| 1351 |
double uniformp = 1. / (double) abc->K; |
| 1352 |
prob = sx = sy = 0.; |
| 1353 |
for (i = 0; i < abc->K; i++) |
| 1354 |
{ |
| 1355 |
if (abc->degen[(int)x][i]) sx += uniformp; |
| 1356 |
if (abc->degen[(int)y][i]) sy += uniformp; |
| 1357 |
if (abc->degen[(int)x][i] && abc->degen[(int)y][i]) prob += uniformp * uniformp; |
| 1358 |
} |
| 1359 |
prob = prob / (sx*sy); |
| 1360 |
} |
| 1361 |
return prob; |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
int |
| 1391 |
esl_abc_IAvgScore(const ESL_ALPHABET *a, ESL_DSQ x, const int *sc) |
| 1392 |
{ |
| 1393 |
float result = 0.; |
| 1394 |
int i; |
| 1395 |
|
| 1396 |
if (! esl_abc_XIsResidue(a, x)) return 0; |
| 1397 |
for (i = 0; i < a->K; i++) |
| 1398 |
if (a->degen[(int) x][i]) result += (float) sc[i]; |
| 1399 |
result /= (float) a->ndegen[(int) x]; |
| 1400 |
if (result < 0) return (int) (result - 0.5); |
| 1401 |
else return (int) (result + 0.5); |
| 1402 |
} |
| 1403 |
float |
| 1404 |
esl_abc_FAvgScore(const ESL_ALPHABET *a, ESL_DSQ x, const float *sc) |
| 1405 |
{ |
| 1406 |
float result = 0.; |
| 1407 |
int i; |
| 1408 |
|
| 1409 |
if (! esl_abc_XIsResidue(a, x)) return 0.; |
| 1410 |
for (i = 0; i < a->K; i++) |
| 1411 |
if (a->degen[(int) x][i]) result += sc[i]; |
| 1412 |
result /= (float) a->ndegen[(int) x]; |
| 1413 |
return result; |
| 1414 |
} |
| 1415 |
double |
| 1416 |
esl_abc_DAvgScore(const ESL_ALPHABET *a, ESL_DSQ x, const double *sc) |
| 1417 |
{ |
| 1418 |
double result = 0.; |
| 1419 |
int i; |
| 1420 |
|
| 1421 |
if (! esl_abc_XIsResidue(a, x)) return 0.; |
| 1422 |
for (i = 0; i < a->K; i++) |
| 1423 |
if (a->degen[(int) x][i]) result += sc[i]; |
| 1424 |
result /= (double) a->ndegen[(int) x]; |
| 1425 |
return result; |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
int |
| 1457 |
esl_abc_IExpectScore(const ESL_ALPHABET *a, ESL_DSQ x, const int *sc, const float *p) |
| 1458 |
{ |
| 1459 |
float result = 0.; |
| 1460 |
float denom = 0.; |
| 1461 |
int i; |
| 1462 |
|
| 1463 |
if (! esl_abc_XIsResidue(a, x)) return 0; |
| 1464 |
for (i = 0; i < a->K; i++) |
| 1465 |
if (a->degen[(int) x][i]) { |
| 1466 |
result += (float) sc[i] * p[i]; |
| 1467 |
denom += p[i]; |
| 1468 |
} |
| 1469 |
result /= denom; |
| 1470 |
if (result < 0) return (int) (result - 0.5); |
| 1471 |
else return (int) (result + 0.5); |
| 1472 |
} |
| 1473 |
float |
| 1474 |
esl_abc_FExpectScore(const ESL_ALPHABET *a, ESL_DSQ x, const float *sc, const float *p) |
| 1475 |
{ |
| 1476 |
float result = 0.; |
| 1477 |
float denom = 0.; |
| 1478 |
int i; |
| 1479 |
|
| 1480 |
if (! esl_abc_XIsResidue(a, x)) return 0.; |
| 1481 |
for (i = 0; i < a->K; i++) |
| 1482 |
if (a->degen[(int) x][i]) { |
| 1483 |
result += sc[i] * p[i]; |
| 1484 |
denom += p[i]; |
| 1485 |
} |
| 1486 |
result /= denom; |
| 1487 |
return result; |
| 1488 |
} |
| 1489 |
double |
| 1490 |
esl_abc_DExpectScore(const ESL_ALPHABET *a, ESL_DSQ x, const double *sc, const double *p) |
| 1491 |
{ |
| 1492 |
double result = 0.; |
| 1493 |
double denom = 0.; |
| 1494 |
int i; |
| 1495 |
|
| 1496 |
if (! esl_abc_XIsResidue(a, x)) return 0.; |
| 1497 |
for (i = 0; i < a->K; i++) |
| 1498 |
if (a->degen[(int) x][i]) { |
| 1499 |
result += sc[i] * p[i]; |
| 1500 |
denom += p[i]; |
| 1501 |
} |
| 1502 |
result /= denom; |
| 1503 |
return result; |
| 1504 |
} |
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
int |
| 1527 |
esl_abc_IAvgScVec(const ESL_ALPHABET *a, int *sc) |
| 1528 |
{ |
| 1529 |
ESL_DSQ x; |
| 1530 |
for (x = a->K+1; x <= a->Kp-3; x++) |
| 1531 |
sc[x] = esl_abc_IAvgScore(a, x, sc); |
| 1532 |
return eslOK; |
| 1533 |
} |
| 1534 |
int |
| 1535 |
esl_abc_FAvgScVec(const ESL_ALPHABET *a, float *sc) |
| 1536 |
{ |
| 1537 |
ESL_DSQ x; |
| 1538 |
for (x = a->K+1; x <= a->Kp-3; x++) |
| 1539 |
sc[x] = esl_abc_FAvgScore(a, x, sc); |
| 1540 |
return eslOK; |
| 1541 |
} |
| 1542 |
int |
| 1543 |
esl_abc_DAvgScVec(const ESL_ALPHABET *a, double *sc) |
| 1544 |
{ |
| 1545 |
ESL_DSQ x; |
| 1546 |
for (x = a->K+1; x <= a->Kp-3; x++) |
| 1547 |
sc[x] = esl_abc_DAvgScore(a, x, sc); |
| 1548 |
return eslOK; |
| 1549 |
} |
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
int |
| 1574 |
esl_abc_IExpectScVec(const ESL_ALPHABET *a, int *sc, const float *p) |
| 1575 |
{ |
| 1576 |
ESL_DSQ x; |
| 1577 |
for (x = a->K+1; x <= a->Kp-3; x++) |
| 1578 |
sc[x] = esl_abc_IExpectScore(a, x, sc, p); |
| 1579 |
return eslOK; |
| 1580 |
} |
| 1581 |
int |
| 1582 |
esl_abc_FExpectScVec(const ESL_ALPHABET *a, float *sc, const float *p) |
| 1583 |
{ |
| 1584 |
ESL_DSQ x; |
| 1585 |
for (x = a->K+1; x <= a->Kp-3; x++) |
| 1586 |
sc[x] = esl_abc_FExpectScore(a, x, sc, p); |
| 1587 |
return eslOK; |
| 1588 |
} |
| 1589 |
int |
| 1590 |
esl_abc_DExpectScVec(const ESL_ALPHABET *a, double *sc, const double *p) |
| 1591 |
{ |
| 1592 |
ESL_DSQ x; |
| 1593 |
for (x = a->K+1; x <= a->Kp-3; x++) |
| 1594 |
sc[x] = esl_abc_DExpectScore(a, x, sc, p); |
| 1595 |
return eslOK; |
| 1596 |
} |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
int |
| 1621 |
esl_abc_FCount(const ESL_ALPHABET *abc, float *ct, ESL_DSQ x, float wt) |
| 1622 |
{ |
| 1623 |
ESL_DSQ y; |
| 1624 |
|
| 1625 |
if (esl_abc_XIsCanonical(abc, x) || esl_abc_XIsGap(abc, x)) |
| 1626 |
ct[x] += wt; |
| 1627 |
else if (esl_abc_XIsMissing(abc, x) || esl_abc_XIsNonresidue(abc, x)) |
| 1628 |
return eslOK; |
| 1629 |
else |
| 1630 |
for (y = 0; y < abc->K; y++) { |
| 1631 |
if (abc->degen[x][y]) |
| 1632 |
ct[y] += wt / (float) abc->ndegen[x]; |
| 1633 |
} |
| 1634 |
return eslOK; |
| 1635 |
} |
| 1636 |
int |
| 1637 |
esl_abc_DCount(const ESL_ALPHABET *abc, double *ct, ESL_DSQ x, double wt) |
| 1638 |
{ |
| 1639 |
ESL_DSQ y; |
| 1640 |
|
| 1641 |
if (esl_abc_XIsCanonical(abc, x) || esl_abc_XIsGap(abc, x)) |
| 1642 |
ct[x] += wt; |
| 1643 |
else if (esl_abc_XIsMissing(abc, x) || esl_abc_XIsNonresidue(abc, x)) |
| 1644 |
return eslOK; |
| 1645 |
else |
| 1646 |
for (y = 0; y < abc->K; y++) { |
| 1647 |
if (abc->degen[x][y]) |
| 1648 |
ct[y] += wt / (double) abc->ndegen[x]; |
| 1649 |
} |
| 1650 |
return eslOK; |
| 1651 |
} |
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
int |
| 1664 |
esl_abc_EncodeType(char *type) |
| 1665 |
{ |
| 1666 |
if (strcasecmp(type, "amino") == 0) return eslAMINO; |
| 1667 |
else if (strcasecmp(type, "rna") == 0) return eslRNA; |
| 1668 |
else if (strcasecmp(type, "dna") == 0) return eslDNA; |
| 1669 |
else if (strcasecmp(type, "coins") == 0) return eslCOINS; |
| 1670 |
else if (strcasecmp(type, "dice") == 0) return eslDICE; |
| 1671 |
else if (strcasecmp(type, "custom")== 0) return eslNONSTANDARD; |
| 1672 |
else return eslUNKNOWN; |
| 1673 |
} |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
int |
| 1684 |
esl_abc_EncodeTypeMem(char *type, int n) |
| 1685 |
{ |
| 1686 |
if (esl_memstrcmp_case(type, n, "amino")) return eslAMINO; |
| 1687 |
else if (esl_memstrcmp_case(type, n, "rna")) return eslRNA; |
| 1688 |
else if (esl_memstrcmp_case(type, n, "dna")) return eslDNA; |
| 1689 |
else if (esl_memstrcmp_case(type, n, "coins")) return eslCOINS; |
| 1690 |
else if (esl_memstrcmp_case(type, n, "dice")) return eslDICE; |
| 1691 |
else if (esl_memstrcmp_case(type, n, "custom")) return eslNONSTANDARD; |
| 1692 |
else return eslUNKNOWN; |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
char * |
| 1704 |
esl_abc_DecodeType(int type) |
| 1705 |
{ |
| 1706 |
switch (type) { |
| 1707 |
case eslUNKNOWN: return "unknown"; |
| 1708 |
case eslRNA: return "RNA"; |
| 1709 |
case eslDNA: return "DNA"; |
| 1710 |
case eslAMINO: return "amino"; |
| 1711 |
case eslCOINS: return "coins"; |
| 1712 |
case eslDICE: return "dice"; |
| 1713 |
case eslNONSTANDARD: return "custom"; |
| 1714 |
default: break; |
| 1715 |
} |
| 1716 |
esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "no such alphabet type code %d\n", type); |
| 1717 |
return NULL; |
| 1718 |
} |
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
int |
| 1748 |
esl_abc_ValidateSeq(const ESL_ALPHABET *a, const char *seq, int64_t L, char *errbuf) |
| 1749 |
{ |
| 1750 |
int status; |
| 1751 |
int64_t i; |
| 1752 |
int64_t firstpos = -1; |
| 1753 |
int64_t nbad = 0; |
| 1754 |
|
| 1755 |
if (errbuf) *errbuf = 0; |
| 1756 |
|
| 1757 |
|
| 1758 |
if (a) |
| 1759 |
{ |
| 1760 |
for (i = 0; i < L; i++) { |
| 1761 |
if (! esl_abc_CIsValid(a, seq[i])) { |
| 1762 |
if (firstpos == -1) firstpos = i; |
| 1763 |
nbad++; |
| 1764 |
} |
| 1765 |
} |
| 1766 |
} |
| 1767 |
else |
| 1768 |
{ |
| 1769 |
for (i = 0; i < L; i++) { |
| 1770 |
if (! isascii(seq[i])) { |
| 1771 |
if (firstpos == -1) firstpos = i; |
| 1772 |
nbad++; |
| 1773 |
} |
| 1774 |
} |
| 1775 |
} |
| 1776 |
|
| 1777 |
if (nbad == 1) ESL_XFAIL(eslEINVAL, errbuf, "invalid char %c at pos %" PRId64, seq[firstpos], firstpos+1); |
| 1778 |
else if (nbad > 1) ESL_XFAIL(eslEINVAL, errbuf, "%" PRId64 " invalid chars (including %c at pos %" PRId64 ")", nbad, seq[firstpos], firstpos+1); |
| 1779 |
return eslOK; |
| 1780 |
|
| 1781 |
ERROR: |
| 1782 |
return status; |
| 1783 |
} |
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
#ifdef eslALPHABET_TESTDRIVE |
| 1792 |
#include "esl_vectorops.h" |
| 1793 |
|
| 1794 |
static int |
| 1795 |
utest_Create(void) |
| 1796 |
{ |
| 1797 |
char msg[] = "esl_alphabet_Create() unit test failed"; |
| 1798 |
int types[] = { eslDNA, eslRNA, eslAMINO, eslCOINS, eslDICE }; |
| 1799 |
int Karr[] = { 4, 4, 20, 2, 6 }; |
| 1800 |
int Kparr[] = { 18, 18, 29, 6, 10 }; |
| 1801 |
int i; |
| 1802 |
ESL_ALPHABET *a; |
| 1803 |
ESL_DSQ x; |
| 1804 |
|
| 1805 |
for (i = 0; i < 3; i++) |
| 1806 |
{ |
| 1807 |
if ((a = esl_alphabet_Create(types[i])) == NULL) esl_fatal(msg); |
| 1808 |
if (a->type != types[i]) esl_fatal(msg); |
| 1809 |
if (a->K != Karr[i]) esl_fatal(msg); |
| 1810 |
if (a->Kp != Kparr[i]) esl_fatal(msg); |
| 1811 |
if (strlen(a->sym) != a->Kp) esl_fatal(msg); |
| 1812 |
|
| 1813 |
x = esl_abc_XGetGap(a); |
| 1814 |
if (x != a->K) esl_fatal(msg); |
| 1815 |
if (a->ndegen[x] != 0) esl_fatal(msg); |
| 1816 |
|
| 1817 |
x = esl_abc_XGetUnknown(a); |
| 1818 |
if (x != a->Kp-3) esl_fatal(msg); |
| 1819 |
if (a->ndegen[x] != a->K) esl_fatal(msg); |
| 1820 |
|
| 1821 |
x = esl_abc_XGetNonresidue(a); |
| 1822 |
if (x != a->Kp-2) esl_fatal(msg); |
| 1823 |
if (a->ndegen[x] != 0) esl_fatal(msg); |
| 1824 |
|
| 1825 |
x = esl_abc_XGetMissing(a); |
| 1826 |
if (x != a->Kp-1) esl_fatal(msg); |
| 1827 |
if (a->ndegen[x] != 0) esl_fatal(msg); |
| 1828 |
|
| 1829 |
esl_alphabet_Destroy(a); |
| 1830 |
} |
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
#ifdef eslTEST_THROWING |
| 1835 |
if (esl_alphabet_Create(-1) != NULL) esl_fatal(msg); |
| 1836 |
if (esl_alphabet_Create(eslUNKNOWN) != NULL) esl_fatal(msg); |
| 1837 |
if (esl_alphabet_Create(eslNONSTANDARD) != NULL) esl_fatal(msg); |
| 1838 |
#endif |
| 1839 |
|
| 1840 |
return eslOK; |
| 1841 |
} |
| 1842 |
|
| 1843 |
static int |
| 1844 |
utest_CreateCustom(void) |
| 1845 |
{ |
| 1846 |
char msg[] = "esl_alphabet_CreateCustom() unit test failed"; |
| 1847 |
ESL_ALPHABET *a; |
| 1848 |
char *testseq = "AaU-~Z"; |
| 1849 |
ESL_DSQ expect[] = { eslDSQ_SENTINEL, 0, 0, 15, 20, 26, 23, eslDSQ_SENTINEL }; |
| 1850 |
ESL_DSQ *dsq; |
| 1851 |
|
| 1852 |
if ((a = esl_alphabet_CreateCustom("ACDEFGHIKLMNPQRSTVWY-BJZX*~", 20, 27)) == NULL) esl_fatal(msg); |
| 1853 |
if (esl_alphabet_SetEquiv(a, 'O', 'K') != eslOK) esl_fatal(msg); |
| 1854 |
if (esl_alphabet_SetEquiv(a, 'U', 'S') != eslOK) esl_fatal(msg); |
| 1855 |
if (esl_alphabet_SetCaseInsensitive(a) != eslOK) esl_fatal(msg); |
| 1856 |
if (esl_alphabet_SetDegeneracy(a, 'Z', "QE") != eslOK) esl_fatal(msg); |
| 1857 |
|
| 1858 |
if (esl_abc_CreateDsq(a, testseq, &dsq) != eslOK) esl_fatal(msg); |
| 1859 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * (strlen(testseq)+2)) != 0) esl_fatal(msg); |
| 1860 |
|
| 1861 |
free(dsq); |
| 1862 |
esl_alphabet_Destroy(a); |
| 1863 |
|
| 1864 |
#ifdef eslTEST_THROWING |
| 1865 |
if (esl_alphabet_CreateCustom("ACGT-RYMKSWHBVDN*~", 4, 21) != NULL) esl_fatal(msg); |
| 1866 |
#endif |
| 1867 |
|
| 1868 |
return eslOK; |
| 1869 |
} |
| 1870 |
|
| 1871 |
static int |
| 1872 |
utest_SetEquiv(void) |
| 1873 |
{ |
| 1874 |
char msg[] = "esl_alphabet_SetEquiv() unit test failed"; |
| 1875 |
ESL_ALPHABET *a; |
| 1876 |
char *testseq = "a1&"; |
| 1877 |
ESL_DSQ expect[] = { eslDSQ_SENTINEL, 0, 4, 7, eslDSQ_SENTINEL }; |
| 1878 |
ESL_DSQ *dsq; |
| 1879 |
|
| 1880 |
if ((a = esl_alphabet_CreateCustom("ACGT-N*~", 4, 8)) == NULL) esl_fatal(msg); |
| 1881 |
if (esl_alphabet_SetEquiv(a, 'a', 'A') != eslOK) esl_fatal(msg); |
| 1882 |
if (esl_alphabet_SetEquiv(a, '1', '-') != eslOK) esl_fatal(msg); |
| 1883 |
if (esl_alphabet_SetEquiv(a, '&', '~') != eslOK) esl_fatal(msg); |
| 1884 |
|
| 1885 |
#ifdef eslTEST_THROWING |
| 1886 |
if (esl_alphabet_SetEquiv(a, 'G', 'C') != eslEINVAL) esl_fatal(msg); |
| 1887 |
if (esl_alphabet_SetEquiv(a, '2', 'V') != eslEINVAL) esl_fatal(msg); |
| 1888 |
#endif |
| 1889 |
|
| 1890 |
if (esl_abc_CreateDsq(a, testseq, &dsq) != eslOK) esl_fatal(msg); |
| 1891 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * (strlen(testseq)+2)) != 0) esl_fatal(msg); |
| 1892 |
free(dsq); |
| 1893 |
esl_alphabet_Destroy(a); |
| 1894 |
return eslOK; |
| 1895 |
} |
| 1896 |
|
| 1897 |
static int |
| 1898 |
utest_SetCaseInsensitive(void) |
| 1899 |
{ |
| 1900 |
char msg[] = "esl_alphabet_SetCaseInsensitive() unit test failed"; |
| 1901 |
ESL_ALPHABET *a; |
| 1902 |
char *testseq = "ACGT"; |
| 1903 |
ESL_DSQ expect[] = { eslDSQ_SENTINEL, 0, 1, 2, 3, eslDSQ_SENTINEL }; |
| 1904 |
ESL_DSQ *dsq; |
| 1905 |
|
| 1906 |
if ((a = esl_alphabet_CreateCustom("acgt-n*~", 4, 8)) == NULL) esl_fatal(msg); |
| 1907 |
if (esl_alphabet_SetCaseInsensitive(a) != eslOK) esl_fatal(msg); |
| 1908 |
if (esl_abc_CreateDsq(a, testseq, &dsq) != eslOK) esl_fatal(msg); |
| 1909 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * (strlen(testseq)+2)) != 0) esl_fatal(msg); |
| 1910 |
free(dsq); |
| 1911 |
esl_alphabet_Destroy(a); |
| 1912 |
|
| 1913 |
#ifdef TEST_THROWING |
| 1914 |
if ((a = esl_alphabet_CreateCustom("acgt-n*~", 4, 8)) == NULL) esl_fatal(msg); |
| 1915 |
if (esl_alphabet_SetEquiv(a, 'A', 'c') != eslOK) esl_fatal(msg); |
| 1916 |
if (esl_alphabet_SetCaseInsensitive(a) != eslECORRUPT) esl_fatal(msg); |
| 1917 |
esl_alphabet_Destroy(a); |
| 1918 |
#endif |
| 1919 |
|
| 1920 |
return eslOK; |
| 1921 |
} |
| 1922 |
|
| 1923 |
static int |
| 1924 |
utest_SetDegeneracy(void) |
| 1925 |
{ |
| 1926 |
char msg[] = "esl_alphabet_SetDegeneracy() unit test failed"; |
| 1927 |
ESL_ALPHABET *a; |
| 1928 |
char *testseq = "yrn"; |
| 1929 |
ESL_DSQ expect[] = { eslDSQ_SENTINEL, 6, 5, 7, eslDSQ_SENTINEL }; |
| 1930 |
ESL_DSQ *dsq; |
| 1931 |
ESL_DSQ x; |
| 1932 |
|
| 1933 |
if ((a = esl_alphabet_CreateCustom("ACGT-RYN*~", 4, 10)) == NULL) esl_fatal(msg); |
| 1934 |
if (esl_alphabet_SetDegeneracy(a, 'R', "AG") != eslOK) esl_fatal(msg); |
| 1935 |
if (esl_alphabet_SetDegeneracy(a, 'Y', "CT") != eslOK) esl_fatal(msg); |
| 1936 |
if (esl_alphabet_SetCaseInsensitive(a) != eslOK) esl_fatal(msg); |
| 1937 |
|
| 1938 |
if (esl_abc_CreateDsq(a, testseq, &dsq) != eslOK) esl_fatal(msg); |
| 1939 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * (strlen(testseq)+2)) != 0) esl_fatal(msg); |
| 1940 |
|
| 1941 |
x = esl_abc_DigitizeSymbol(a, 'a'); if (a->ndegen[x] != 1) esl_fatal(msg); |
| 1942 |
x = esl_abc_DigitizeSymbol(a, 'r'); if (a->ndegen[x] != 2) esl_fatal(msg); |
| 1943 |
x = esl_abc_DigitizeSymbol(a, 'y'); if (a->ndegen[x] != 2) esl_fatal(msg); |
| 1944 |
x = esl_abc_DigitizeSymbol(a, 'n'); if (a->ndegen[x] != 4) esl_fatal(msg); |
| 1945 |
|
| 1946 |
free(dsq); |
| 1947 |
esl_alphabet_Destroy(a); |
| 1948 |
|
| 1949 |
#ifdef TEST_THROWING |
| 1950 |
if ((a = esl_alphabet_CreateCustom("ACGT-RYN*~", 4, 10)) == NULL) esl_fatal(msg); |
| 1951 |
if (esl_abc_SetDegeneracy(a, 'z', "AC") != eslEINVAL) esl_fatal(msg); |
| 1952 |
if (esl_abc_SetDegeneracy(a, 'N', "ACGT") != eslEINVAL) esl_fatal(msg); |
| 1953 |
if (esl_abc_SetDegeneracy(a, 'A', "GT") != eslEINVAL) esl_fatal(msg); |
| 1954 |
if (esl_abc_SetDegeneracy(a, '-', "GT") != eslEINVAL) esl_fatal(msg); |
| 1955 |
if (esl_abc_SetDegeneracy(a, '*', "GT") != eslEINVAL) esl_fatal(msg); |
| 1956 |
if (esl_abc_SetDegeneracy(a, '~', "GT") != eslEINVAL) esl_fatal(msg); |
| 1957 |
if (esl_abc_SetDegeneracy(a, 'R', "XY") != eslEINVAL) esl_fatal(msg); |
| 1958 |
if (esl_abc_SetDegeneracy(a, 'R', "YN") != eslEINVAL) esl_fatal(msg); |
| 1959 |
esl_alphabet_Destroy(a); |
| 1960 |
#endif |
| 1961 |
return eslOK; |
| 1962 |
} |
| 1963 |
|
| 1964 |
static int |
| 1965 |
utest_SetIgnored(void) |
| 1966 |
{ |
| 1967 |
char msg[] = "esl_alphabet_SetIgnored() unit test failed"; |
| 1968 |
ESL_ALPHABET *a; |
| 1969 |
char *testseq = "y \trn"; |
| 1970 |
ESL_DSQ expect[] = { eslDSQ_SENTINEL, 6, 5, 15, eslDSQ_SENTINEL }; |
| 1971 |
int L = 5; |
| 1972 |
ESL_DSQ *dsq; |
| 1973 |
|
| 1974 |
if ((a = esl_alphabet_Create(eslRNA)) == NULL) esl_fatal(msg); |
| 1975 |
if (esl_alphabet_SetIgnored(a, " \t") != eslOK) esl_fatal(msg); |
| 1976 |
|
| 1977 |
if (esl_abc_CreateDsq(a, testseq, &dsq) != eslOK) esl_fatal(msg); |
| 1978 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * L) != 0) esl_fatal(msg); |
| 1979 |
free(dsq); |
| 1980 |
esl_alphabet_Destroy(a); |
| 1981 |
return eslOK; |
| 1982 |
} |
| 1983 |
|
| 1984 |
|
| 1985 |
static int |
| 1986 |
utest_Destroy(void) |
| 1987 |
{ |
| 1988 |
char msg[] = "esl_alphabet_Destroy() unit test failed"; |
| 1989 |
ESL_ALPHABET *a; |
| 1990 |
|
| 1991 |
if ((a = esl_alphabet_CreateCustom("ACGT-RYN*~", 4, 10)) == NULL) esl_fatal(msg); |
| 1992 |
esl_alphabet_Destroy(a); |
| 1993 |
esl_alphabet_Destroy(NULL); |
| 1994 |
return eslOK; |
| 1995 |
} |
| 1996 |
|
| 1997 |
static int |
| 1998 |
utest_CreateDsq(void) |
| 1999 |
{ |
| 2000 |
char msg[] = "esl_abc_CreateDsq() unit test failed"; |
| 2001 |
ESL_ALPHABET *a; |
| 2002 |
char goodseq[] = "ACDEF"; |
| 2003 |
char badseq[] = "1@%34"; |
| 2004 |
ESL_DSQ *dsq; |
| 2005 |
ESL_DSQ x; |
| 2006 |
|
| 2007 |
if ((a = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal(msg); |
| 2008 |
|
| 2009 |
if (esl_abc_CreateDsq(a, goodseq, &dsq) != eslOK) esl_fatal(msg); |
| 2010 |
if (dsq[1] != 0 || dsq[2] != 1) esl_fatal(msg); |
| 2011 |
free(dsq); |
| 2012 |
|
| 2013 |
if (esl_abc_CreateDsq(a, badseq, &dsq) != eslEINVAL) esl_fatal(msg); |
| 2014 |
x = esl_abc_XGetUnknown(a); |
| 2015 |
if (dsq[1] != x || dsq[2] != x) esl_fatal(msg); |
| 2016 |
free(dsq); |
| 2017 |
|
| 2018 |
if (esl_abc_CreateDsq(a, goodseq, NULL) != eslOK) esl_fatal(msg); |
| 2019 |
|
| 2020 |
esl_alphabet_Destroy(a); |
| 2021 |
return eslOK; |
| 2022 |
} |
| 2023 |
|
| 2024 |
static int |
| 2025 |
utest_Digitize(void) |
| 2026 |
{ |
| 2027 |
char msg[] = "esl_abc_Digitize() unit test failed"; |
| 2028 |
ESL_ALPHABET *a; |
| 2029 |
char goodseq[] = "ACDEF"; |
| 2030 |
char badseq[] = "1@%34"; |
| 2031 |
ESL_DSQ *dsq; |
| 2032 |
ESL_DSQ x; |
| 2033 |
int status; |
| 2034 |
|
| 2035 |
ESL_ALLOC(dsq, sizeof(ESL_DSQ) * (strlen(goodseq)+2)); |
| 2036 |
|
| 2037 |
if ((a = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal(msg); |
| 2038 |
esl_abc_Digitize(a, goodseq, dsq); |
| 2039 |
if (dsq[1] != 0 || dsq[2] != 1) esl_fatal(msg); |
| 2040 |
|
| 2041 |
esl_abc_Digitize(a, badseq, dsq); |
| 2042 |
x = esl_abc_XGetUnknown(a); |
| 2043 |
if (dsq[1] != x || dsq[2] != x) esl_fatal(msg); |
| 2044 |
|
| 2045 |
free(dsq); |
| 2046 |
esl_alphabet_Destroy(a); |
| 2047 |
return eslOK; |
| 2048 |
|
| 2049 |
ERROR: |
| 2050 |
esl_fatal(msg); |
| 2051 |
return status; |
| 2052 |
} |
| 2053 |
|
| 2054 |
static int |
| 2055 |
utest_Textize(void) |
| 2056 |
{ |
| 2057 |
char msg[] = "esl_abc_Textize() unit test failed"; |
| 2058 |
ESL_ALPHABET *a; |
| 2059 |
char goodseq[] = "acdef"; |
| 2060 |
char *newseq; |
| 2061 |
ESL_DSQ *dsq; |
| 2062 |
int L; |
| 2063 |
int status; |
| 2064 |
|
| 2065 |
L = strlen(goodseq); |
| 2066 |
ESL_ALLOC(newseq, sizeof(char) * (L+1)); |
| 2067 |
if ((a = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal(msg); |
| 2068 |
if (esl_abc_CreateDsq(a, goodseq, &dsq) != eslOK) esl_fatal(msg); |
| 2069 |
if (esl_abc_Textize(a, dsq, L, newseq ) != eslOK) esl_fatal(msg); |
| 2070 |
if (strcmp(newseq, "ACDEF") != 0) esl_fatal(msg); |
| 2071 |
free(dsq); |
| 2072 |
free(newseq); |
| 2073 |
esl_alphabet_Destroy(a); |
| 2074 |
return eslOK; |
| 2075 |
|
| 2076 |
ERROR: |
| 2077 |
esl_fatal(msg); |
| 2078 |
return status; |
| 2079 |
} |
| 2080 |
|
| 2081 |
static int |
| 2082 |
utest_TextizeN(void) |
| 2083 |
{ |
| 2084 |
char msg[] = "esl_abc_TextizeN() unit test failed"; |
| 2085 |
ESL_ALPHABET *a; |
| 2086 |
char goodseq[] = "acdefrynacdef"; |
| 2087 |
ESL_DSQ *dsq; |
| 2088 |
ESL_DSQ *dptr; |
| 2089 |
int W; |
| 2090 |
|
| 2091 |
if ((a = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal(msg); |
| 2092 |
if (esl_abc_CreateDsq(a, goodseq, &dsq) != eslOK) esl_fatal(msg); |
| 2093 |
|
| 2094 |
dptr = dsq+6; |
| 2095 |
W = 5; |
| 2096 |
if (esl_abc_TextizeN(a, dptr, W, goodseq) != eslOK) esl_fatal(msg); |
| 2097 |
if (strcmp(goodseq, "RYNACrynacdef") != 0) esl_fatal(msg); |
| 2098 |
|
| 2099 |
|
| 2100 |
dptr = dsq+10; |
| 2101 |
W = 20; |
| 2102 |
if (esl_abc_TextizeN(a, dptr, W, goodseq) != eslOK) esl_fatal(msg); |
| 2103 |
if (strcmp(goodseq, "CDEF") != 0) esl_fatal(msg); |
| 2104 |
|
| 2105 |
free(dsq); |
| 2106 |
esl_alphabet_Destroy(a); |
| 2107 |
return eslOK; |
| 2108 |
} |
| 2109 |
|
| 2110 |
static int |
| 2111 |
utest_dsqdup(void) |
| 2112 |
{ |
| 2113 |
char msg[] = "esl_abc_dsqdup() unit test failed"; |
| 2114 |
ESL_ALPHABET *a; |
| 2115 |
char goodseq[] = "ACGt"; |
| 2116 |
ESL_DSQ expect[] = { eslDSQ_SENTINEL, 0, 1, 2, 3, eslDSQ_SENTINEL }; |
| 2117 |
ESL_DSQ *d1, *d2; |
| 2118 |
int L; |
| 2119 |
|
| 2120 |
L = strlen(goodseq); |
| 2121 |
if ((a = esl_alphabet_Create(eslRNA)) == NULL) esl_fatal(msg); |
| 2122 |
if (esl_abc_CreateDsq(a, goodseq, &d1) != eslOK) esl_fatal(msg); |
| 2123 |
|
| 2124 |
if (esl_abc_dsqdup(d1, -1, &d2) != eslOK) esl_fatal(msg); |
| 2125 |
if (memcmp(d2, expect, sizeof(ESL_DSQ) * (L+2)) != 0) esl_fatal(msg); |
| 2126 |
free(d2); |
| 2127 |
|
| 2128 |
if (esl_abc_dsqdup(d1, L, &d2) != eslOK) esl_fatal(msg); |
| 2129 |
if (memcmp(d2, expect, sizeof(ESL_DSQ) * (L+2)) != 0) esl_fatal(msg); |
| 2130 |
free(d2); |
| 2131 |
|
| 2132 |
free(d1); |
| 2133 |
esl_alphabet_Destroy(a); |
| 2134 |
return eslOK; |
| 2135 |
} |
| 2136 |
|
| 2137 |
static int |
| 2138 |
utest_dsqcat(void) |
| 2139 |
{ |
| 2140 |
char msg[] = "esl_abc_dsqcat() unit test failed"; |
| 2141 |
ESL_ALPHABET *a; |
| 2142 |
char goodseq[] = "ACGt"; |
| 2143 |
char addseq[] = "RYM KN"; |
| 2144 |
char badseq[] = "RYM K&"; |
| 2145 |
ESL_DSQ expect[] = { eslDSQ_SENTINEL, 0, 1, 2, 3, 5, 6, 7, 8, 15, eslDSQ_SENTINEL }; |
| 2146 |
ESL_DSQ *dsq; |
| 2147 |
int64_t L1; |
| 2148 |
esl_pos_t L2; |
| 2149 |
|
| 2150 |
|
| 2151 |
if ((a = esl_alphabet_Create(eslRNA)) == NULL) esl_fatal(msg); |
| 2152 |
a->inmap[0] = esl_abc_XGetUnknown(a); |
| 2153 |
a->inmap[' '] = eslDSQ_IGNORED; |
| 2154 |
|
| 2155 |
L1 = strlen(goodseq); |
| 2156 |
L2 = strlen(addseq); |
| 2157 |
if (esl_abc_CreateDsq(a, goodseq, &dsq) != eslOK) esl_fatal(msg); |
| 2158 |
if (esl_abc_dsqcat(a->inmap, &dsq, &L1, addseq, L2) != eslOK) esl_fatal(msg); |
| 2159 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * (L1+2)) != 0) esl_fatal(msg); |
| 2160 |
free(dsq); |
| 2161 |
|
| 2162 |
L1 = -1; |
| 2163 |
L2 = -1; |
| 2164 |
if (esl_abc_CreateDsq(a, goodseq, &dsq) != eslOK) esl_fatal(msg); |
| 2165 |
if (esl_abc_dsqcat(a->inmap, &dsq, &L1, addseq, L2) != eslOK) esl_fatal(msg); |
| 2166 |
if (L1 != esl_abc_dsqlen(dsq)) esl_fatal(msg); |
| 2167 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * (L1+2)) != 0) esl_fatal(msg); |
| 2168 |
free(dsq); |
| 2169 |
|
| 2170 |
L1 = 0; |
| 2171 |
dsq = NULL; |
| 2172 |
if (esl_abc_dsqcat(a->inmap, &dsq, &L1, goodseq, -1) != eslOK) esl_fatal(msg); |
| 2173 |
if (esl_abc_dsqcat(a->inmap, &dsq, &L1, addseq, -1) != eslOK) esl_fatal(msg); |
| 2174 |
if (L1 != esl_abc_dsqlen(dsq)) esl_fatal(msg); |
| 2175 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * (L1+2)) != 0) esl_fatal(msg); |
| 2176 |
free(dsq); |
| 2177 |
|
| 2178 |
L1 = -1; |
| 2179 |
L2 = strlen(badseq); |
| 2180 |
if (esl_abc_CreateDsq(a, goodseq, &dsq) != eslOK) esl_fatal(msg); |
| 2181 |
if (esl_abc_dsqcat(a->inmap, &dsq, &L1, badseq, L2) != eslEINVAL) esl_fatal(msg); |
| 2182 |
if (L1 != esl_abc_dsqlen(dsq)) esl_fatal(msg); |
| 2183 |
if (memcmp(dsq, expect, sizeof(ESL_DSQ) * (L1+2)) != 0) esl_fatal(msg); |
| 2184 |
free(dsq); |
| 2185 |
|
| 2186 |
esl_alphabet_Destroy(a); |
| 2187 |
return eslOK; |
| 2188 |
} |
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
static int |
| 2199 |
degeneracy_integer_scores(void) |
| 2200 |
{ |
| 2201 |
char *msg = "degeneracy_integer_scores unit test failed"; |
| 2202 |
ESL_ALPHABET *a; |
| 2203 |
ESL_DSQ x; |
| 2204 |
float p[] = {0.4, 0.1, 0.1, 0.4}; |
| 2205 |
int sc[] = { -1, -6, 6, 1}; |
| 2206 |
int val; |
| 2207 |
|
| 2208 |
a = esl_alphabet_Create(eslDNA); |
| 2209 |
|
| 2210 |
x = esl_abc_DigitizeSymbol(a, 'N'); |
| 2211 |
val = esl_abc_IAvgScore(a, x, sc); |
| 2212 |
|
| 2213 |
if (val != 0) esl_fatal(msg); |
| 2214 |
|
| 2215 |
x = esl_abc_DigitizeSymbol(a, 'M'); |
| 2216 |
val = esl_abc_IExpectScore(a, x, sc, p); |
| 2217 |
|
| 2218 |
if (val != -2) esl_fatal(msg); |
| 2219 |
|
| 2220 |
esl_alphabet_Destroy(a); |
| 2221 |
return eslOK; |
| 2222 |
} |
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
static int |
| 2229 |
degeneracy_float_scores(void) |
| 2230 |
{ |
| 2231 |
char *msg = "degeneracy_float_scores unit test failed"; |
| 2232 |
ESL_ALPHABET *a; |
| 2233 |
ESL_DSQ x; |
| 2234 |
float p[] = {0.4, 0.1, 0.1, 0.4}; |
| 2235 |
float sc[] = { -1., -6., 6., 1.}; |
| 2236 |
float val; |
| 2237 |
|
| 2238 |
a = esl_alphabet_Create(eslRNA); |
| 2239 |
|
| 2240 |
x = esl_abc_DigitizeSymbol(a, 'N'); |
| 2241 |
val = esl_abc_FAvgScore(a, x, sc); |
| 2242 |
|
| 2243 |
if (fabs(val - 0.) > 0.0001) esl_fatal(msg); |
| 2244 |
|
| 2245 |
x = esl_abc_DigitizeSymbol(a, 'M'); |
| 2246 |
val = esl_abc_FExpectScore(a, x, sc, p); |
| 2247 |
|
| 2248 |
if (fabs(val + 2.) > 0.0001) esl_fatal(msg); |
| 2249 |
|
| 2250 |
esl_alphabet_Destroy(a); |
| 2251 |
return eslOK; |
| 2252 |
} |
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
static int |
| 2260 |
degeneracy_double_scores(void) |
| 2261 |
{ |
| 2262 |
char *msg = "degeneracy_double_scores unit test failed"; |
| 2263 |
ESL_ALPHABET *a; |
| 2264 |
ESL_DSQ x; |
| 2265 |
double p[] = {0.4, 0.1, 0.1, 0.4}; |
| 2266 |
double sc[] = { -1., -6., 6., 1.}; |
| 2267 |
double val; |
| 2268 |
|
| 2269 |
a = esl_alphabet_Create(eslRNA); |
| 2270 |
|
| 2271 |
x = esl_abc_DigitizeSymbol(a, 'N'); |
| 2272 |
val = esl_abc_DAvgScore(a, x, sc); |
| 2273 |
|
| 2274 |
if (fabs(val - 0.) > 0.0001) esl_fatal(msg); |
| 2275 |
|
| 2276 |
x = esl_abc_DigitizeSymbol(a, 'M'); |
| 2277 |
val = esl_abc_DExpectScore(a, x, sc, p); |
| 2278 |
|
| 2279 |
if (fabs(val + 2.) > 0.0001) esl_fatal(msg); |
| 2280 |
|
| 2281 |
esl_alphabet_Destroy(a); |
| 2282 |
return eslOK; |
| 2283 |
} |
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
static int |
| 2293 |
utest_FCount(void) |
| 2294 |
{ |
| 2295 |
char *msg = "FCount unit test failure"; |
| 2296 |
ESL_ALPHABET *a = NULL; |
| 2297 |
ESL_DSQ x; |
| 2298 |
char *teststring = "X~-Z.UAX"; |
| 2299 |
char *s; |
| 2300 |
int status; |
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
float expect[21] = { 1.1, 1.1, 0.1, 0.6, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.6, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 2.0 }; |
| 2305 |
float *vec; |
| 2306 |
|
| 2307 |
a = esl_alphabet_Create(eslAMINO); |
| 2308 |
ESL_ALLOC(vec, sizeof(float) * (a->K+1)); |
| 2309 |
esl_vec_FSet(vec, a->K+1, 0.); |
| 2310 |
for (s = teststring; *s != '\0'; s++) |
| 2311 |
{ |
| 2312 |
x = esl_abc_DigitizeSymbol(a, *s); |
| 2313 |
if (esl_abc_FCount(a, vec, x, 1.0) != eslOK) esl_fatal(msg); |
| 2314 |
} |
| 2315 |
if (esl_vec_FCompare(vec, expect, a->K+1, 0.0001) != eslOK) esl_fatal(msg); |
| 2316 |
|
| 2317 |
esl_alphabet_Destroy(a); |
| 2318 |
free(vec); |
| 2319 |
return eslOK; |
| 2320 |
|
| 2321 |
ERROR: |
| 2322 |
esl_fatal("allocation failed"); |
| 2323 |
return status; |
| 2324 |
} |
| 2325 |
|
| 2326 |
static int |
| 2327 |
utest_DCount(void) |
| 2328 |
{ |
| 2329 |
char *msg = "DCount unit test failure"; |
| 2330 |
ESL_ALPHABET *a = NULL; |
| 2331 |
ESL_DSQ x; |
| 2332 |
char *teststring = "X~-Z.UAX"; |
| 2333 |
char *s; |
| 2334 |
int status; |
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
double expect[21] = { 1.1, 1.1, 0.1, 0.6, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.6, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 2.0 }; |
| 2339 |
double *vec; |
| 2340 |
|
| 2341 |
a = esl_alphabet_Create(eslAMINO); |
| 2342 |
ESL_ALLOC(vec, sizeof(double) * (a->K+1)); |
| 2343 |
esl_vec_DSet(vec, a->K+1, 0.); |
| 2344 |
for (s = teststring; *s != '\0'; s++) |
| 2345 |
{ |
| 2346 |
x = esl_abc_DigitizeSymbol(a, *s); |
| 2347 |
if (esl_abc_DCount(a, vec, x, 1.0) != eslOK) esl_fatal(msg); |
| 2348 |
} |
| 2349 |
if (esl_vec_DCompare(vec, expect, a->K+1, 0.0001) != eslOK) esl_fatal(msg); |
| 2350 |
|
| 2351 |
esl_alphabet_Destroy(a); |
| 2352 |
free(vec); |
| 2353 |
return eslOK; |
| 2354 |
|
| 2355 |
ERROR: |
| 2356 |
esl_fatal("allocation failed"); |
| 2357 |
return status; |
| 2358 |
} |
| 2359 |
#endif |
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
#ifdef eslALPHABET_TESTDRIVE |
| 2375 |
static int basic_examples(void); |
| 2376 |
|
| 2377 |
#include "easel.h" |
| 2378 |
#include "esl_alphabet.h" |
| 2379 |
|
| 2380 |
int |
| 2381 |
main(void) |
| 2382 |
{ |
| 2383 |
utest_Create(); |
| 2384 |
utest_CreateCustom(); |
| 2385 |
utest_SetEquiv(); |
| 2386 |
utest_SetCaseInsensitive(); |
| 2387 |
utest_SetDegeneracy(); |
| 2388 |
utest_SetIgnored(); |
| 2389 |
utest_Destroy(); |
| 2390 |
|
| 2391 |
utest_CreateDsq(); |
| 2392 |
utest_Digitize(); |
| 2393 |
utest_Textize(); |
| 2394 |
utest_TextizeN(); |
| 2395 |
utest_dsqdup(); |
| 2396 |
utest_dsqcat(); |
| 2397 |
|
| 2398 |
utest_FCount(); |
| 2399 |
utest_DCount(); |
| 2400 |
|
| 2401 |
basic_examples(); |
| 2402 |
degeneracy_integer_scores(); |
| 2403 |
degeneracy_float_scores(); |
| 2404 |
degeneracy_double_scores(); |
| 2405 |
|
| 2406 |
return eslOK; |
| 2407 |
} |
| 2408 |
|
| 2409 |
static int |
| 2410 |
basic_examples(void) |
| 2411 |
{ |
| 2412 |
char *msg = "basic alphabet example tests failed"; |
| 2413 |
ESL_ALPHABET *a1, *a2; |
| 2414 |
char dnaseq[] = "GARYtcN"; |
| 2415 |
char aaseq[] = "EFILqzU"; |
| 2416 |
int L; |
| 2417 |
ESL_DSQ *dsq, *dsq2; |
| 2418 |
int i; |
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
if ((a1 = esl_alphabet_Create(eslDNA)) == NULL) esl_fatal(msg); |
| 2424 |
L = strlen(dnaseq); |
| 2425 |
if ((dsq = malloc(sizeof(ESL_DSQ) * (L+2))) == NULL) esl_fatal(msg); |
| 2426 |
if (esl_abc_Digitize(a1, dnaseq, dsq) != eslOK) esl_fatal(msg); |
| 2427 |
if (esl_abc_dsqlen(dsq) != L) esl_fatal(msg); |
| 2428 |
esl_alphabet_Destroy(a1); |
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
if ((a2 = esl_alphabet_Create(eslRNA)) == NULL) esl_fatal(msg); |
| 2436 |
if ((dsq2 = malloc(sizeof(ESL_DSQ) * (L+2))) == NULL) esl_fatal(msg); |
| 2437 |
if (esl_abc_Digitize(a2, dnaseq, dsq2) != eslOK) esl_fatal(msg); |
| 2438 |
for (i = 1; i <= L; i++) |
| 2439 |
if (dsq[i] != dsq2[i]) esl_fatal(msg); |
| 2440 |
esl_alphabet_Destroy(a2); |
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
if ((a1 = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal(msg); |
| 2447 |
if (esl_abc_Digitize(a1, aaseq, dsq) != eslOK) esl_fatal(msg); |
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
if ((a2 = esl_alphabet_CreateCustom("ACDEFGHIKLMNPQRSTVWY-BJZOUX~", 20, 28)) == NULL) esl_fatal(msg); |
| 2455 |
if (esl_alphabet_SetCaseInsensitive(a2) != eslOK) esl_fatal(msg); |
| 2456 |
if (esl_alphabet_SetDegeneracy(a2, 'Z', "QE") != eslOK) esl_fatal(msg); |
| 2457 |
|
| 2458 |
if (esl_abc_Digitize(a2, aaseq, dsq2) != eslOK) esl_fatal(msg); |
| 2459 |
for (i = 1; i <= L; i++) |
| 2460 |
if (dsq[i] != dsq2[i]) esl_fatal(msg); |
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
esl_alphabet_Destroy(a1); |
| 2465 |
esl_alphabet_Destroy(a2); |
| 2466 |
free(dsq); |
| 2467 |
free(dsq2); |
| 2468 |
return eslOK; |
| 2469 |
} |
| 2470 |
|
| 2471 |
|
| 2472 |
#endif |
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
|
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
#ifdef eslALPHABET_EXAMPLE |
| 2481 |
|
| 2482 |
#include "easel.h" |
| 2483 |
#include "esl_alphabet.h" |
| 2484 |
int main(void) |
| 2485 |
{ |
| 2486 |
ESL_ALPHABET *a; |
| 2487 |
char dnaseq[] = "GARYTC"; |
| 2488 |
int L = 6; |
| 2489 |
ESL_DSQ *dsq; |
| 2490 |
|
| 2491 |
a = esl_alphabet_Create(eslDNA); |
| 2492 |
|
| 2493 |
if ((dsq = malloc(sizeof(ESL_DSQ) * (L+2))) == NULL) esl_fatal("malloc failed"); |
| 2494 |
if (esl_abc_Digitize(a, dnaseq, dsq) != eslOK) esl_fatal("failed to digitize the sequence"); |
| 2495 |
|
| 2496 |
free(dsq); |
| 2497 |
esl_alphabet_Destroy(a); |
| 2498 |
return 0; |
| 2499 |
} |
| 2500 |
|
| 2501 |
#endif |
| 2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
|
| 2506 |
#ifdef eslALPHABET_EXAMPLE2 |
| 2507 |
|
| 2508 |
#include "easel.h" |
| 2509 |
#include "esl_alphabet.h" |
| 2510 |
int main(void) |
| 2511 |
{ |
| 2512 |
ESL_ALPHABET *a; |
| 2513 |
|
| 2514 |
|
| 2515 |
a = esl_alphabet_CreateCustom("ACDEFGHIKLMNOPQRSTUVWY-BJZX~", 22, 28); |
| 2516 |
|
| 2517 |
|
| 2518 |
esl_alphabet_SetEquiv(a, '.', '-'); |
| 2519 |
|
| 2520 |
|
| 2521 |
esl_alphabet_SetCaseInsensitive(a); |
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
esl_alphabet_SetDegeneracy(a, 'B', "DN"); |
| 2526 |
esl_alphabet_SetDegeneracy(a, 'J', "IL"); |
| 2527 |
esl_alphabet_SetDegeneracy(a, 'Z', "QE"); |
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
esl_alphabet_Destroy(a); |
| 2533 |
return 0; |
| 2534 |
} |
| 2535 |
|
| 2536 |
#endif |
| 2537 |
|
| 2538 |
|
| 2539 |
|
| 2540 |
#ifdef eslALPHABET_EXAMPLE3 |
| 2541 |
#include "easel.h" |
| 2542 |
#include "esl_alphabet.h" |
| 2543 |
#include "esl_sq.h" |
| 2544 |
#include "esl_sqio.h" |
| 2545 |
|
| 2546 |
int |
| 2547 |
main(int argc, char **argv) |
| 2548 |
{ |
| 2549 |
ESL_SQ *sq = esl_sq_Create(); |
| 2550 |
ESL_SQFILE *sqfp; |
| 2551 |
int format = eslSQFILE_UNKNOWN; |
| 2552 |
char *seqfile = argv[1]; |
| 2553 |
int type; |
| 2554 |
int status; |
| 2555 |
|
| 2556 |
status = esl_sqfile_Open(seqfile, format, NULL, &sqfp); |
| 2557 |
if (status == eslENOTFOUND) esl_fatal("No such file."); |
| 2558 |
else if (status == eslEFORMAT) esl_fatal("Format couldn't be determined."); |
| 2559 |
else if (status != eslOK) esl_fatal("Open failed, code %d.", status); |
| 2560 |
|
| 2561 |
while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) |
| 2562 |
{ |
| 2563 |
esl_sq_GuessAlphabet(sq, &type); |
| 2564 |
printf("%-25s %s\n", sq->name, esl_abc_DecodeType(type)); |
| 2565 |
esl_sq_Reuse(sq); |
| 2566 |
} |
| 2567 |
if (status == eslEFORMAT) esl_fatal("Parse failed\n %s", esl_sqfile_GetErrorBuf(sqfp)); |
| 2568 |
else if (status != eslEOF) esl_fatal("Unexpected read error %d", status); |
| 2569 |
|
| 2570 |
esl_sq_Destroy(sq); |
| 2571 |
esl_sqfile_Close(sqfp); |
| 2572 |
return 0; |
| 2573 |
} |
| 2574 |
#endif |
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|