| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include <esl_config.h> |
| 35 |
|
| 36 |
#include <math.h> |
| 37 |
|
| 38 |
#include "easel.h" |
| 39 |
#include "esl_composition.h" |
| 40 |
#include "esl_dmatrix.h" |
| 41 |
#include "esl_vectorops.h" |
| 42 |
#include "esl_ratematrix.h" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
int |
| 80 |
esl_rmx_SetWAG(ESL_DMATRIX *Q, double *pi) |
| 81 |
{ |
| 82 |
static double wagE[190] = { |
| 83 |
1.027040, 0.738998, 0.030295, 1.582850, 0.021352, 6.174160, 0.210494, 0.398020, 0.046730, 0.081134, |
| 84 |
1.416720, 0.306674, 0.865584, 0.567717, 0.049931, 0.316954, 0.248972, 0.930676, 0.570025, 0.679371, |
| 85 |
0.249410, 0.193335, 0.170135, 0.039437, 0.127395, 1.059470, 0.030450, 0.138190, 0.906265, 0.074034, |
| 86 |
0.479855, 2.584430, 0.088836, 0.373558, 0.890432, 0.323832, 0.397915, 0.384287, 0.084805, 0.154263, |
| 87 |
2.115170, 0.061304, 0.499462, 3.170970, 0.257555, 0.893496, 0.390482, 0.103754, 0.315124, 1.190630, |
| 88 |
0.174100, 0.404141, 4.257460, 0.934276, 4.854020, 0.509848, 0.265256, 5.429420, 0.947198, 0.096162, |
| 89 |
1.125560, 3.956290, 0.554236, 3.012010, 0.131528, 0.198221, 1.438550, 0.109404, 0.423984, 0.682355, |
| 90 |
0.161444, 0.243570, 0.696198, 0.099929, 0.556896, 0.415844, 0.171329, 0.195081, 0.908598, 0.098818, |
| 91 |
0.616783, 5.469470, 0.099921, 0.330052, 4.294110, 0.113917, 3.894900, 0.869489, 1.545260, 1.543640, |
| 92 |
0.933372, 0.551571, 0.528191, 0.147304, 0.439157, 0.102711, 0.584665, 2.137150, 0.186979, 5.351420, |
| 93 |
0.497671, 0.683162, 0.635346, 0.679489, 3.035500, 3.370790, 1.407660, 1.071760, 0.704939, 0.545931, |
| 94 |
1.341820, 0.740169, 0.319440, 0.967130, 0.344739, 0.493905, 3.974230, 1.613280, 1.028870, 1.224190, |
| 95 |
2.121110, 0.512984, 0.374866, 0.822765, 0.171903, 0.225833, 0.473307, 1.458160, 1.386980, 0.326622, |
| 96 |
1.516120, 2.030060, 0.795384, 0.857928, 0.554413, 4.378020, 2.006010, 1.002140, 0.152335, 0.588731, |
| 97 |
0.649892, 0.187247, 0.118358, 7.821300, 0.305434, 1.800340, 2.058450, 0.196246, 0.314887, 0.301281, |
| 98 |
0.251849, 0.232739, 1.388230, 0.113133, 0.717070, 0.129767, 0.156557, 1.529640, 0.336983, 0.262569, |
| 99 |
0.212483, 0.137505, 0.665309, 0.515706, 0.071917, 0.139405, 0.215737, 1.163920, 0.523742, 0.110864, |
| 100 |
0.365369, 0.240735, 0.543833, 0.325711, 0.196303, 6.454280, 0.103604, 3.873440, 0.420170, 0.133264, |
| 101 |
0.398618, 0.428437, 1.086000, 0.216046, 0.227710, 0.381533, 0.786993, 0.291148, 0.314730, 2.485390}; |
| 102 |
static double wagpi[20]; |
| 103 |
int i,j,z; |
| 104 |
|
| 105 |
if (Q->m != 20 || Q->n != 20 || Q->type != eslGENERAL) |
| 106 |
ESL_EXCEPTION(eslEINVAL, "Q must be a 20x20 general matrix"); |
| 107 |
esl_composition_WAG(wagpi); |
| 108 |
|
| 109 |
|
| 110 |
z = 0; |
| 111 |
for (i = 0; i < 20; i++) |
| 112 |
{ |
| 113 |
Q->mx[i][i] = 0.; |
| 114 |
for (j = 0; j < i; j++) { |
| 115 |
Q->mx[i][j] = wagE[z++]; |
| 116 |
Q->mx[j][i] = Q->mx[i][j]; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
for (i = 0; i < 20; i++) |
| 122 |
for (j = 0; j < 20; j++) |
| 123 |
if (pi != NULL) Q->mx[i][j] *= pi[j]; |
| 124 |
else Q->mx[i][j] *= wagpi[j]; |
| 125 |
|
| 126 |
|
| 127 |
for (i = 0; i < 20; i++) |
| 128 |
Q->mx[i][i] = -1. * esl_vec_DSum(Q->mx[i], 20); |
| 129 |
|
| 130 |
|
| 131 |
if (pi != NULL) esl_rmx_ScaleTo(Q, pi, 1.0); |
| 132 |
else esl_rmx_ScaleTo(Q, wagpi, 1.0); |
| 133 |
|
| 134 |
return eslOK; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
int |
| 147 |
esl_rmx_SetJukesCantor(ESL_DMATRIX *Q) |
| 148 |
{ |
| 149 |
int i,j; |
| 150 |
double pi[4] = { 0.25, 0.25, 0.25, 0.25 }; |
| 151 |
|
| 152 |
if (Q->m != 4 || Q->n != 4 || Q->type != eslGENERAL) |
| 153 |
ESL_EXCEPTION(eslEINVAL, "Q must be a 4x4 general matrix"); |
| 154 |
|
| 155 |
for (i = 0; i < 4; i++) { |
| 156 |
for (j = 0; j < 4; j++) |
| 157 |
{ |
| 158 |
if (i != j) Q->mx[i][j] = 1.0; |
| 159 |
else Q->mx[i][j] = 0.0; |
| 160 |
} |
| 161 |
Q->mx[i][i] = -1. * esl_vec_DSum(Q->mx[i], 4); |
| 162 |
} |
| 163 |
esl_rmx_ScaleTo(Q, pi, 1.0); |
| 164 |
return eslOK; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
int |
| 179 |
esl_rmx_SetKimura(ESL_DMATRIX *Q, double alpha, double beta) |
| 180 |
{ |
| 181 |
int i,j; |
| 182 |
double pi[4] = { 0.25, 0.25, 0.25, 0.25 }; |
| 183 |
|
| 184 |
if (Q->m != 4 || Q->n != 4 || Q->type != eslGENERAL) |
| 185 |
ESL_EXCEPTION(eslEINVAL, "Q must be a 4x4 general matrix"); |
| 186 |
|
| 187 |
for (i = 0; i < 4; i++) { |
| 188 |
for (j = 0; j < 4; j++) |
| 189 |
{ |
| 190 |
if (i != j) Q->mx[i][j] = ((i+j)%2)? beta : alpha; |
| 191 |
else Q->mx[i][j] = 0.0; |
| 192 |
} |
| 193 |
Q->mx[i][i] = -1. * esl_vec_DSum(Q->mx[i], 4); |
| 194 |
} |
| 195 |
esl_rmx_ScaleTo(Q, pi, 1.0); |
| 196 |
return eslOK; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
int |
| 210 |
esl_rmx_SetF81(ESL_DMATRIX *Q, double *pi) |
| 211 |
{ |
| 212 |
int i,j; |
| 213 |
|
| 214 |
if (Q->m != 4 || Q->n != 4 || Q->type != eslGENERAL) |
| 215 |
ESL_EXCEPTION(eslEINVAL, "Q must be a 4x4 general matrix"); |
| 216 |
|
| 217 |
for (i = 0; i < 4; i++) { |
| 218 |
for (j = 0; j < 4; j++) |
| 219 |
{ |
| 220 |
if (i != j) Q->mx[i][j] = pi[j]; |
| 221 |
else Q->mx[i][j] = 0.0; |
| 222 |
} |
| 223 |
Q->mx[i][i] = -1. * esl_vec_DSum(Q->mx[i], 4); |
| 224 |
} |
| 225 |
esl_rmx_ScaleTo(Q, pi, 1.0); |
| 226 |
return eslOK; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
int |
| 250 |
esl_rmx_SetHKY( ESL_DMATRIX *Q, double *pi, double alpha, double beta) |
| 251 |
{ |
| 252 |
int i,j; |
| 253 |
|
| 254 |
if (Q->m != 4 || Q->n != 4 || Q->type != eslGENERAL) |
| 255 |
ESL_EXCEPTION(eslEINVAL, "Q must be a 4x4 general matrix"); |
| 256 |
|
| 257 |
for (i = 0; i < 4; i++) { |
| 258 |
for (j = 0; j < 4; j++) |
| 259 |
{ |
| 260 |
if (i != j) Q->mx[i][j] = ((i+j)%2)? pi[j]*beta : pi[j]*alpha; |
| 261 |
else Q->mx[i][j] = 0.; |
| 262 |
} |
| 263 |
Q->mx[i][i] = -1. * esl_vec_DSum(Q->mx[i], 4); |
| 264 |
} |
| 265 |
esl_rmx_ScaleTo(Q, pi, 1.0); |
| 266 |
return eslOK; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
int |
| 305 |
esl_rmx_ValidateP(ESL_DMATRIX *P, double tol, char *errbuf) |
| 306 |
{ |
| 307 |
int i,j; |
| 308 |
double sum; |
| 309 |
|
| 310 |
if (P->type != eslGENERAL) ESL_EXCEPTION(eslEINVAL, "P must be type eslGENERAL to be validated"); |
| 311 |
|
| 312 |
for (i = 0; i < P->n; i++) |
| 313 |
{ |
| 314 |
sum = esl_vec_DSum(P->mx[i], P->m); |
| 315 |
if (fabs(sum-1.0) > tol) ESL_FAIL(eslFAIL, errbuf, "row %d does not sum to 1.0", i); |
| 316 |
|
| 317 |
for (j = 0; j < P->m; j++) |
| 318 |
if (P->mx[i][j] < 0.0 || P->mx[i][j] > 1.0) |
| 319 |
ESL_FAIL(eslFAIL, errbuf, "element %d,%d is not a probability (%f)", i,j,P->mx[i][j]); |
| 320 |
} |
| 321 |
return eslOK; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
int |
| 356 |
esl_rmx_ValidateQ(ESL_DMATRIX *Q, double tol, char *errbuf) |
| 357 |
{ |
| 358 |
int i,j; |
| 359 |
double qi; |
| 360 |
|
| 361 |
if (Q->type != eslGENERAL) ESL_EXCEPTION(eslEINVAL, "Q must be type eslGENERAL to be validated"); |
| 362 |
if (Q->n != Q->m) ESL_EXCEPTION(eslEINVAL, "a rate matrix Q must be square"); |
| 363 |
|
| 364 |
for (i = 0; i < Q->n; i++) |
| 365 |
{ |
| 366 |
qi = 0.; |
| 367 |
for (j = 0; j < Q->m; j++) |
| 368 |
{ |
| 369 |
if (i != j) { |
| 370 |
if (Q->mx[i][j] < 0.) ESL_FAIL(eslFAIL, errbuf, "offdiag elem %d,%d < 0",i,j); |
| 371 |
qi += Q->mx[i][j]; |
| 372 |
} else { |
| 373 |
if (Q->mx[i][j] > 0.) ESL_FAIL(eslFAIL, errbuf, "diag elem %d,%d < 0", i,j); |
| 374 |
} |
| 375 |
} |
| 376 |
if (fabs(qi + Q->mx[i][i]) > tol) ESL_FAIL(eslFAIL, errbuf, "row %d does not sum to 0.0", i); |
| 377 |
} |
| 378 |
return eslOK; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
int |
| 409 |
esl_rmx_ScaleTo(ESL_DMATRIX *Q, double *pi, double unit) |
| 410 |
{ |
| 411 |
int i,j; |
| 412 |
double sum = 0.; |
| 413 |
|
| 414 |
if (Q->m != Q->n || Q->type != eslGENERAL) |
| 415 |
ESL_EXCEPTION(eslEINVAL, "Q must be a square general matrix"); |
| 416 |
|
| 417 |
for (i = 0; i < Q->m; i++) |
| 418 |
for (j = 0; j < Q->n; j++) |
| 419 |
if (i != j) sum += pi[i] * Q->mx[i][j]; |
| 420 |
|
| 421 |
for (i = 0; i < Q->m; i++) |
| 422 |
for (j = 0; j < Q->n; j++) |
| 423 |
Q->mx[i][j] *= (unit / sum); |
| 424 |
|
| 425 |
return eslOK; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
int |
| 455 |
esl_rmx_E2Q(ESL_DMATRIX *E, double *pi, ESL_DMATRIX *Q) |
| 456 |
{ |
| 457 |
int i,j; |
| 458 |
|
| 459 |
if (E->n != Q->n) ESL_EXCEPTION(eslEINVAL, "E and Q sizes differ"); |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
for (i = 0; i < E->n; i++) |
| 464 |
for (j = 0; j < i; j++) |
| 465 |
{ |
| 466 |
Q->mx[i][j] = pi[j] * E->mx[i][j]; |
| 467 |
Q->mx[j][i] = pi[i] * E->mx[i][j]; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
for (i = 0; i < Q->n; i++) |
| 473 |
{ |
| 474 |
Q->mx[i][i] = 0.; |
| 475 |
Q->mx[i][i] = -1. * esl_vec_DSum(Q->mx[i], Q->n); |
| 476 |
} |
| 477 |
return eslOK; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
double |
| 496 |
esl_rmx_RelativeEntropy(ESL_DMATRIX *P, double *pi) |
| 497 |
{ |
| 498 |
double H = 0.; |
| 499 |
int i,j; |
| 500 |
|
| 501 |
for (i = 0; i < P->m; i++) |
| 502 |
for (j = 0; j < P->n; j++) |
| 503 |
H += P->mx[i][j] * pi[i] * log(P->mx[i][j] / pi[j]); |
| 504 |
return H / eslCONST_LOG2; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
double |
| 522 |
esl_rmx_ExpectedScore(ESL_DMATRIX *P, double *pi) |
| 523 |
{ |
| 524 |
double S = 0.; |
| 525 |
int i,j; |
| 526 |
|
| 527 |
for (i = 0; i < P->m; i++) |
| 528 |
for (j = 0; j < P->n; j++) |
| 529 |
S += pi[j] * pi[i] * log(P->mx[i][j] / pi[j]); |
| 530 |
return S / eslCONST_LOG2; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
#ifdef eslRATEMATRIX_BENCHMARK |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
#ifdef HAVE_LIBGSL |
| 550 |
#include <gsl/gsl_matrix.h> |
| 551 |
#include <gsl/gsl_linalg.h> |
| 552 |
#endif |
| 553 |
|
| 554 |
#include "easel.h" |
| 555 |
#include "esl_stopwatch.h" |
| 556 |
#include "esl_dmatrix.h" |
| 557 |
#include "esl_ratematrix.h" |
| 558 |
|
| 559 |
int |
| 560 |
main(void) |
| 561 |
{ |
| 562 |
ESL_STOPWATCH *w = NULL; |
| 563 |
ESL_DMATRIX *Q = NULL; |
| 564 |
ESL_DMATRIX *P = NULL; |
| 565 |
double t = 5.0; |
| 566 |
int esl_iterations = 100; |
| 567 |
int i; |
| 568 |
#ifdef HAVE_LIBGSL |
| 569 |
gsl_matrix *Qg = NULL; |
| 570 |
gsl_matrix *Pg = NULL; |
| 571 |
int gsl_iterations = 100; |
| 572 |
#endif |
| 573 |
|
| 574 |
w = esl_stopwatch_Create(); |
| 575 |
Q = esl_dmatrix_Create(20, 20); |
| 576 |
P = esl_dmatrix_Create(20, 20); |
| 577 |
esl_rmx_SetWAG(Q, NULL); |
| 578 |
|
| 579 |
esl_stopwatch_Start(w); |
| 580 |
for (i = 0; i < esl_iterations; i++) |
| 581 |
esl_dmx_Exp(Q, t, P); |
| 582 |
esl_stopwatch_Stop(w); |
| 583 |
printf("Easel takes: %g sec\n", w->user / (double) esl_iterations); |
| 584 |
|
| 585 |
#ifdef HAVE_LIBGSL |
| 586 |
if (esl_dmx_MorphGSL(Q, &Qg) != eslOK) esl_fatal("morph to gsl_matrix failed"); |
| 587 |
if ((Pg = gsl_matrix_alloc(20, 20)) == NULL) esl_fatal("gsl alloc failed"); |
| 588 |
gsl_matrix_scale(Qg, t); |
| 589 |
|
| 590 |
esl_stopwatch_Start(w); |
| 591 |
for (i = 0; i < gsl_iterations; i++) |
| 592 |
gsl_linalg_exponential_ss(Qg, Pg, GSL_PREC_DOUBLE); |
| 593 |
esl_stopwatch_Stop(w); |
| 594 |
printf(" GSL takes: %g sec\n", w->user / (double) gsl_iterations); |
| 595 |
|
| 596 |
gsl_matrix_free(Qg); |
| 597 |
gsl_matrix_free(Pg); |
| 598 |
#endif |
| 599 |
|
| 600 |
esl_dmatrix_Destroy(Q); |
| 601 |
esl_dmatrix_Destroy(P); |
| 602 |
esl_stopwatch_Destroy(w); |
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
#endif |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
#ifdef eslRATEMATRIX_REGRESSION |
| 613 |
#ifdef HAVE_LIBGSL |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
#include <esl_config.h> |
| 623 |
|
| 624 |
#include <gsl/gsl_matrix.h> |
| 625 |
#include <gsl/gsl_linalg.h> |
| 626 |
|
| 627 |
#include "easel.h" |
| 628 |
#include "esl_dmatrix.h" |
| 629 |
#include "esl_ratematrix.h" |
| 630 |
|
| 631 |
int |
| 632 |
main(void) |
| 633 |
{ |
| 634 |
char errbuf[eslERRBUFSIZE]; |
| 635 |
char *alphabet = "ACDEFGHIKLMNPQRSTVWY"; |
| 636 |
ESL_DMATRIX *Q = NULL; |
| 637 |
ESL_DMATRIX *P = NULL; |
| 638 |
gsl_matrix *Qg = NULL; |
| 639 |
gsl_matrix *Pg = NULL; |
| 640 |
ESL_DMATRIX *Pge = NULL; |
| 641 |
double t = 15.0; |
| 642 |
|
| 643 |
if ((Q = esl_dmatrix_Create(20, 20)) == NULL) esl_fatal("malloc failed"); |
| 644 |
if ((P = esl_dmatrix_Create(20, 20)) == NULL) esl_fatal("malloc failed"); |
| 645 |
|
| 646 |
if (esl_rmx_SetWAG(Q, NULL) != eslOK) esl_fatal("_SetWAG() failed"); |
| 647 |
if (esl_rmx_ValidateQ(Q, 0.0001, errbuf) != eslOK) esl_fatal("Q validation failed: %s", errbuf); |
| 648 |
|
| 649 |
if (esl_dmx_Exp(Q, t, P) != eslOK) esl_fatal("matrix exponentiation failed"); |
| 650 |
if (esl_rmx_ValidateP(P, 0.0001, errbuf) != eslOK) esl_fatal("P validation failed: %s", errbuf); |
| 651 |
|
| 652 |
if (esl_dmx_MorphGSL(Q, &Qg) != eslOK) esl_fatal("morph to gsl_matrix failed"); |
| 653 |
if ((Pg = gsl_matrix_alloc(20, 20)) == NULL) esl_fatal("gsl alloc failed"); |
| 654 |
gsl_matrix_scale(Qg, t); |
| 655 |
if (gsl_linalg_exponential_ss(Qg, Pg, GSL_PREC_DOUBLE) != 0) esl_fatal("gsl's exponentiation failed"); |
| 656 |
if (esl_dmx_UnmorphGSL(Pg, &Pge) != eslOK) esl_fatal("morph from gsl_matrix failed"); |
| 657 |
|
| 658 |
esl_dmatrix_Dump(stdout, P, alphabet, alphabet); |
| 659 |
|
| 660 |
if (esl_dmatrix_Compare(Pge, P, 0.00001) != eslOK) esl_fatal("whoops, different answers."); |
| 661 |
|
| 662 |
esl_dmatrix_Destroy(Q); |
| 663 |
esl_dmatrix_Destroy(P); |
| 664 |
esl_dmatrix_Destroy(Pge); |
| 665 |
gsl_matrix_free(Qg); |
| 666 |
gsl_matrix_free(Pg); |
| 667 |
return 0; |
| 668 |
} |
| 669 |
#else |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
int main(void) { return 0; } |
| 674 |
#endif |
| 675 |
|
| 676 |
#endif |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
#ifdef eslRATEMATRIX_TESTDRIVE |
| 683 |
|
| 684 |
static void |
| 685 |
utest_SetWAG(void) |
| 686 |
{ |
| 687 |
char errbuf[eslERRBUFSIZE]; |
| 688 |
ESL_DMATRIX *Q = NULL; |
| 689 |
ESL_DMATRIX *P = NULL; |
| 690 |
double t = 50.0; |
| 691 |
double pi[20]; |
| 692 |
int i; |
| 693 |
|
| 694 |
if ((Q = esl_dmatrix_Create(20, 20)) == NULL) esl_fatal("malloc failed"); |
| 695 |
if ((P = esl_dmatrix_Create(20, 20)) == NULL) esl_fatal("malloc failed"); |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
if (esl_rmx_SetWAG(Q, NULL) != eslOK) esl_fatal("_SetWAG() failed"); |
| 702 |
if (esl_dmx_Exp(Q, t, P) != eslOK) esl_fatal("matrix exponentiation failed"); |
| 703 |
if (esl_rmx_ValidateP(P, 1e-7, errbuf) != eslOK) esl_fatal("P validation failed: %s", errbuf); |
| 704 |
if (esl_rmx_ValidateQ(Q, 1e-7, errbuf) != eslOK) esl_fatal("Q validation failed: %s", errbuf); |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
esl_vec_DSet(pi, 20, 0.05); |
| 710 |
if (esl_rmx_SetWAG(Q, pi) != eslOK) esl_fatal("_SetWAG() failed"); |
| 711 |
if (esl_dmx_Exp(Q, t, P) != eslOK) esl_fatal("matrix exponentiation failed"); |
| 712 |
if (esl_rmx_ValidateP(P, 1e-7, errbuf) != eslOK) esl_fatal("P validation failed: %s", errbuf); |
| 713 |
if (esl_rmx_ValidateQ(Q, 1e-7, errbuf) != eslOK) esl_fatal("Q validation failed: %s", errbuf); |
| 714 |
for (i = 0; i < 20; i++) |
| 715 |
if (esl_vec_DCompare(P->mx[i], pi, 20, 1e-7) != eslOK) esl_fatal("P didn't converge to right pi's"); |
| 716 |
|
| 717 |
esl_dmatrix_Destroy(Q); |
| 718 |
esl_dmatrix_Destroy(P); |
| 719 |
return; |
| 720 |
} |
| 721 |
|
| 722 |
#ifdef HAVE_LIBLAPACK |
| 723 |
static void |
| 724 |
utest_Diagonalization(void) |
| 725 |
{ |
| 726 |
ESL_DMATRIX *P = NULL; |
| 727 |
ESL_DMATRIX *P2 = NULL; |
| 728 |
ESL_DMATRIX *C = NULL; |
| 729 |
ESL_DMATRIX *D = NULL; |
| 730 |
double *lambda = NULL; |
| 731 |
ESL_DMATRIX *U = NULL; |
| 732 |
ESL_DMATRIX *Ui = NULL; |
| 733 |
int i,j; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
if ((P = esl_dmatrix_Create(4, 4)) == NULL) esl_fatal("malloc failed"); |
| 740 |
if ((C = esl_dmatrix_Create(4, 4)) == NULL) esl_fatal("malloc failed"); |
| 741 |
if ((Ui = esl_dmatrix_Create(4, 4)) == NULL) esl_fatal("malloc failed"); |
| 742 |
if ((D = esl_dmatrix_Create(4, 4)) == NULL) esl_fatal("malloc failed"); |
| 743 |
if ((P2 = esl_dmatrix_Create(4, 4)) == NULL) esl_fatal("malloc failed"); |
| 744 |
for (i = 0; i < 4; i++) |
| 745 |
for (j = 0; j < 4; j++) |
| 746 |
if (i == j) P->mx[i][j] = 0.25 + 0.75 * exp(-4./3.); |
| 747 |
else P->mx[i][j] = 0.25 - 0.25 * exp(-4./3.); |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
if (esl_dmx_Diagonalize(P, &lambda, NULL, &U, NULL) != eslOK) esl_fatal("diagonalization failed"); |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
esl_dmatrix_SetZero(D); |
| 756 |
for (i = 0; i < P->n; i++) D->mx[i][i] = lambda[i]; |
| 757 |
esl_dmx_Invert(U, Ui); |
| 758 |
esl_dmx_Multiply(U, D, C); |
| 759 |
esl_dmx_Multiply(C, Ui, P2); |
| 760 |
|
| 761 |
if (esl_dmatrix_Compare(P, P2, 1e-7) != eslOK) esl_fatal("diagonalization unit test failed"); |
| 762 |
|
| 763 |
free(lambda); |
| 764 |
esl_dmatrix_Destroy(P2); |
| 765 |
esl_dmatrix_Destroy(Ui); |
| 766 |
esl_dmatrix_Destroy(U); |
| 767 |
esl_dmatrix_Destroy(D); |
| 768 |
esl_dmatrix_Destroy(C); |
| 769 |
esl_dmatrix_Destroy(P); |
| 770 |
return; |
| 771 |
} |
| 772 |
#endif |
| 773 |
|
| 774 |
#endif |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
#ifdef eslRATEMATRIX_TESTDRIVE |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
#include <esl_config.h> |
| 787 |
|
| 788 |
#include "easel.h" |
| 789 |
#include "esl_ratematrix.h" |
| 790 |
|
| 791 |
int |
| 792 |
main(void) |
| 793 |
{ |
| 794 |
utest_SetWAG(); |
| 795 |
#ifdef HAVE_LIBLAPACK |
| 796 |
utest_Diagonalization(); |
| 797 |
#endif |
| 798 |
|
| 799 |
return 0; |
| 800 |
|
| 801 |
} |
| 802 |
#endif |
| 803 |
|