| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <esl_config.h> |
| 17 |
#include <p7_config.h> |
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
#include <math.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include "easel.h" |
| 26 |
#include "esl_msa.h" |
| 27 |
#include "esl_random.h" |
| 28 |
#include "esl_stats.h" |
| 29 |
#include "esl_vectorops.h" |
| 30 |
|
| 31 |
#include "hmmer.h" |
| 32 |
|
| 33 |
#include "infernal.h" |
| 34 |
|
| 35 |
static float cm2hmm_emit_prob(CM_t *cm, CP9Map_t *cp9map, int x, int i, int k); |
| 36 |
static void cm2hmm_special_trans_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, double *psi, char ***tmap); |
| 37 |
static void cm2hmm_trans_probs_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, int k, double *psi, char ***tmap); |
| 38 |
static void hmm_add_single_trans_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, int a, int b, int k, int hmm_trans_idx, double *psi, char ***tmap); |
| 39 |
static float cm_sum_subpaths_cp9(CM_t *cm, CP9Map_t *cp9map, int start, int end, char ***tmap, int k, double *psi); |
| 40 |
static int check_psi_vs_phi_cp9(CM_t *cm, char *errbuf, CP9Map_t *cp9map, double *psi, double **phi, double threshold, int debug_level); |
| 41 |
static int CP9_node_chi_squared(CP9_t *ahmm, CP9_t *shmm, int nd, float threshold, int print_flag); |
| 42 |
static int check_cm_adj_bp(CM_t *cm, CP9Map_t *cp9map); |
| 43 |
static float FChiSquareFit(float *f1, float *f2, int N); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
CP9Map_t * |
| 59 |
AllocCP9Map(CM_t *cm) |
| 60 |
{ |
| 61 |
int status; |
| 62 |
CP9Map_t *cp9map; |
| 63 |
int v,ks,i; |
| 64 |
|
| 65 |
ESL_ALLOC(cp9map, sizeof(struct cp9map_s)); |
| 66 |
|
| 67 |
|
| 68 |
cp9map->hmm_M = 0; |
| 69 |
for(v = 0; v <= cm->M; v++) |
| 70 |
{ |
| 71 |
if(cm->stid[v] == MATP_MP) |
| 72 |
cp9map->hmm_M += 2; |
| 73 |
else if(cm->stid[v] == MATL_ML || cm->stid[v] == MATR_MR) |
| 74 |
cp9map->hmm_M++; |
| 75 |
} |
| 76 |
cp9map->cm_M = cm->M; |
| 77 |
cp9map->cm_nodes = cm->nodes; |
| 78 |
|
| 79 |
|
| 80 |
ESL_ALLOC(cp9map->nd2lpos, sizeof(int) * cp9map->cm_nodes); |
| 81 |
ESL_ALLOC(cp9map->nd2rpos, sizeof(int) * cp9map->cm_nodes); |
| 82 |
for(i = 0; i < cp9map->cm_nodes; i++) |
| 83 |
cp9map->nd2lpos[i] = cp9map->nd2rpos[i] = -1; |
| 84 |
|
| 85 |
ESL_ALLOC(cp9map->pos2nd, sizeof(int) * (cp9map->hmm_M+1)); |
| 86 |
ESL_ALLOC(cp9map->hns2cs, sizeof(int **) * (cp9map->hmm_M+1)); |
| 87 |
for(i = 0; i <= cp9map->hmm_M; i++) |
| 88 |
{ |
| 89 |
cp9map->pos2nd[i] = -1; |
| 90 |
ESL_ALLOC(cp9map->hns2cs[i], sizeof(int *) * 3); |
| 91 |
for(ks = 0; ks < 3; ks++) |
| 92 |
{ |
| 93 |
ESL_ALLOC(cp9map->hns2cs[i][ks], sizeof(int) * 2); |
| 94 |
cp9map->hns2cs[i][ks][0] = cp9map->hns2cs[i][ks][1] = -1; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
ESL_ALLOC(cp9map->cs2hn, sizeof(int *) * (cp9map->cm_M+1)); |
| 99 |
ESL_ALLOC(cp9map->cs2hs, sizeof(int *) * (cp9map->cm_M+1)); |
| 100 |
for(v = 0; v <= cp9map->cm_M; v++) |
| 101 |
{ |
| 102 |
ESL_ALLOC(cp9map->cs2hn[v], sizeof(int) * 2); |
| 103 |
ESL_ALLOC(cp9map->cs2hs[v], sizeof(int) * 2); |
| 104 |
for(i = 0; i <= 1; i++) |
| 105 |
cp9map->cs2hn[v][i] = cp9map->cs2hs[v][i] = -1; |
| 106 |
} |
| 107 |
|
| 108 |
return cp9map; |
| 109 |
|
| 110 |
ERROR: |
| 111 |
cm_Fail("Memory allocation error."); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
float |
| 121 |
SizeofCP9Map(CP9Map_t *cp9map) |
| 122 |
{ |
| 123 |
float bytes = 0.; |
| 124 |
|
| 125 |
bytes = sizeof(CP9Map_t); |
| 126 |
|
| 127 |
|
| 128 |
bytes += sizeof(int) * cp9map->cm_nodes; |
| 129 |
bytes += sizeof(int) * cp9map->cm_nodes; |
| 130 |
bytes += sizeof(int) * (cp9map->hmm_M+1); |
| 131 |
bytes += sizeof(int **) * (cp9map->hmm_M+1); |
| 132 |
bytes += sizeof(int *) * (cp9map->hmm_M+1) * 3; |
| 133 |
bytes += sizeof(int) * (cp9map->hmm_M+1) * 3 * 2; |
| 134 |
bytes += sizeof(int *) * (cp9map->cm_M+1); |
| 135 |
bytes += sizeof(int *) * (cp9map->cm_M+1); |
| 136 |
bytes += sizeof(int *) * (cp9map->cm_M+1) * 2; |
| 137 |
bytes += sizeof(int *) * (cp9map->cm_M+1) * 2; |
| 138 |
|
| 139 |
return (bytes / 1000000.); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
void |
| 147 |
FreeCP9Map(CP9Map_t *cp9map) |
| 148 |
{ |
| 149 |
int v,k,ks; |
| 150 |
for(v = 0; v <= cp9map->cm_M; v++) |
| 151 |
{ |
| 152 |
free(cp9map->cs2hn[v]); |
| 153 |
free(cp9map->cs2hs[v]); |
| 154 |
} |
| 155 |
free(cp9map->cs2hn); |
| 156 |
free(cp9map->cs2hs); |
| 157 |
|
| 158 |
for(k = 0; k <= cp9map->hmm_M; k++) |
| 159 |
{ |
| 160 |
for(ks = 0; ks < 3; ks++) |
| 161 |
free(cp9map->hns2cs[k][ks]); |
| 162 |
free(cp9map->hns2cs[k]); |
| 163 |
} |
| 164 |
free(cp9map->hns2cs); |
| 165 |
|
| 166 |
free(cp9map->nd2lpos); |
| 167 |
free(cp9map->nd2rpos); |
| 168 |
free(cp9map->pos2nd); |
| 169 |
free(cp9map); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
int |
| 196 |
build_cp9_hmm(CM_t *cm, char *errbuf, int do_psi_test, float thresh, int debug_level, |
| 197 |
CP9_t **ret_hmm, CP9Map_t **ret_cp9map) |
| 198 |
{ |
| 199 |
int status; |
| 200 |
int k; |
| 201 |
int i; |
| 202 |
double *psi; |
| 203 |
double **phi; |
| 204 |
char ***tmap; |
| 205 |
int *ap; |
| 206 |
int k_state; |
| 207 |
CP9Map_t *cp9map; |
| 208 |
CP9_t *hmm; |
| 209 |
|
| 210 |
|
| 211 |
if(cm->flags & CMH_LOCAL_BEGIN) ESL_FAIL(eslEINCOMPAT, errbuf, "build_cp9_hmm(): CMH_LOCAL_BEGIN flag is up.\n"); |
| 212 |
if(cm->flags & CMH_LOCAL_END) ESL_FAIL(eslEINCOMPAT, errbuf, "build_cp9_hmm(): CMH_LOCAL_END flag is up.\n"); |
| 213 |
|
| 214 |
|
| 215 |
cp9map = AllocCP9Map(cm); |
| 216 |
|
| 217 |
CP9_map_cm2hmm(cm, cp9map, debug_level); |
| 218 |
|
| 219 |
hmm = AllocCPlan9(cp9map->hmm_M, cm->abc); |
| 220 |
ZeroCPlan9(hmm); |
| 221 |
CPlan9SetNullModel(hmm, cm->null, 1.0); |
| 222 |
CPlan9InitEL(hmm, cm); |
| 223 |
|
| 224 |
hmm->null2_omega = cm->null2_omega; |
| 225 |
hmm->null3_omega = cm->null3_omega; |
| 226 |
|
| 227 |
ESL_ALLOC(ap, sizeof(int) * 2); |
| 228 |
if(debug_level > 1) |
| 229 |
{ |
| 230 |
printf("-------------------------------------------------\n"); |
| 231 |
printf("In build_CP9_hmm()\n"); |
| 232 |
} |
| 233 |
|
| 234 |
tmap = cm_CreateTransitionMap(); |
| 235 |
psi = cm_ExpectedStateOccupancy(cm); |
| 236 |
|
| 237 |
|
| 238 |
for(i = 0; i < cm->abc->K; i++) |
| 239 |
{ |
| 240 |
hmm->ins[0][i] = cm->e[1][i]; |
| 241 |
} |
| 242 |
for(k = 1; k <= hmm->M; k++) |
| 243 |
{ |
| 244 |
for(i = 0; i < cm->abc->K; i++) |
| 245 |
{ |
| 246 |
hmm->mat[k][i] = 0.0; |
| 247 |
hmm->ins[k][i] = 0.0; |
| 248 |
} |
| 249 |
|
| 250 |
k_state = HMMMATCH; |
| 251 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 252 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
for(i = 0; i < cm->abc->K; i++) |
| 259 |
{ |
| 260 |
hmm->mat[k][i] += psi[ap[0]] * |
| 261 |
cm2hmm_emit_prob(cm, cp9map, ap[0], i, k); |
| 262 |
if(ap[1] != -1) |
| 263 |
hmm->mat[k][i] += psi[ap[1]] * |
| 264 |
cm2hmm_emit_prob(cm, cp9map, ap[1], i, k); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
k_state = HMMINSERT; |
| 269 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 270 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
for(i = 0; i < cm->abc->K; i++) |
| 275 |
{ |
| 276 |
hmm->ins[k][i] += psi[ap[0]] * |
| 277 |
cm2hmm_emit_prob(cm, cp9map, ap[0], i, k); |
| 278 |
if(ap[1] != -1) |
| 279 |
hmm->ins[k][i] += psi[ap[1]] * |
| 280 |
cm2hmm_emit_prob(cm, cp9map, ap[1], i, k); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
cm2hmm_special_trans_cp9(cm, hmm, cp9map, psi, tmap); |
| 290 |
|
| 291 |
for(k = 1; k < hmm->M; k++) |
| 292 |
{ |
| 293 |
cm2hmm_trans_probs_cp9(cm, hmm, cp9map, k, psi, tmap); |
| 294 |
} |
| 295 |
|
| 296 |
CPlan9Renormalize(hmm); |
| 297 |
|
| 298 |
if(debug_level > 1) |
| 299 |
debug_print_cp9_params(stdout, hmm, TRUE); |
| 300 |
if(do_psi_test) { |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
fill_phi_cp9(hmm, &phi, 1, FALSE); |
| 308 |
if((status = check_psi_vs_phi_cp9(cm, errbuf, cp9map, psi, phi, (double) thresh, debug_level)) != eslOK) return status; |
| 309 |
for(k = 0; k <= hmm->M; k++) free(phi[k]); |
| 310 |
free(phi); |
| 311 |
} |
| 312 |
|
| 313 |
free(ap); |
| 314 |
free(psi); |
| 315 |
cm_FreeTransitionMap(tmap); |
| 316 |
|
| 317 |
*ret_hmm = hmm; |
| 318 |
*ret_cp9map = cp9map; |
| 319 |
return eslOK; |
| 320 |
|
| 321 |
ERROR: |
| 322 |
ESL_FAIL(eslEMEM, errbuf, "memory allocation error"); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
void |
| 343 |
CP9_map_cm2hmm(CM_t *cm, CP9Map_t *cp9map, int debug_level) |
| 344 |
{ |
| 345 |
int k; |
| 346 |
int ks; |
| 347 |
int n; |
| 348 |
int nn; |
| 349 |
int n_begr; |
| 350 |
int is_left; |
| 351 |
int is_right; |
| 352 |
int v; |
| 353 |
int v1, v2; |
| 354 |
CMEmitMap_t *emap; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
emap = CreateEmitMap(cm); |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
for(n = 0; n < cm->nodes; n++) |
| 369 |
{ |
| 370 |
if(cm->ndtype[n] == MATP_nd || |
| 371 |
cm->ndtype[n] == MATL_nd) |
| 372 |
{ |
| 373 |
cp9map->nd2lpos[n] = emap->lpos[n]; |
| 374 |
cp9map->pos2nd[cp9map->nd2lpos[n]] = n; |
| 375 |
} |
| 376 |
if(cm->ndtype[n] == MATP_nd || |
| 377 |
cm->ndtype[n] == MATR_nd) |
| 378 |
{ |
| 379 |
cp9map->nd2rpos[n] = emap->rpos[n]; |
| 380 |
cp9map->pos2nd[cp9map->nd2rpos[n]] = n; |
| 381 |
} |
| 382 |
} |
| 383 |
FreeEmitMap(emap); |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
k = 0; |
| 388 |
ks = 0; |
| 389 |
v = 0; |
| 390 |
map_helper(cm, cp9map, k, ks, v); |
| 391 |
|
| 392 |
|
| 393 |
ks = 1; |
| 394 |
v = 1; |
| 395 |
map_helper(cm, cp9map, k, ks, v); |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
for(k = 1; k <= cp9map->hmm_M; k++) |
| 401 |
{ |
| 402 |
n = cp9map->pos2nd[k]; |
| 403 |
if(cp9map->nd2lpos[n] == k) |
| 404 |
{ |
| 405 |
is_left = TRUE; |
| 406 |
is_right = FALSE; |
| 407 |
} |
| 408 |
else if(cp9map->nd2rpos[n] == k) |
| 409 |
{ |
| 410 |
is_left = FALSE; |
| 411 |
is_right = TRUE; |
| 412 |
} |
| 413 |
switch(cm->ndtype[n]) |
| 414 |
{ |
| 415 |
case ROOT_nd: |
| 416 |
case BIF_nd: |
| 417 |
case BEGL_nd: |
| 418 |
case BEGR_nd: |
| 419 |
case END_nd: |
| 420 |
printf("ERROR: HMM node k doesn't map to MATP, MATR or MATL\n"); |
| 421 |
exit(1); |
| 422 |
break; |
| 423 |
|
| 424 |
case MATP_nd: |
| 425 |
if(is_left) |
| 426 |
{ |
| 427 |
ks = 0; |
| 428 |
v = cm->nodemap[n]; |
| 429 |
map_helper(cm, cp9map, k, ks, v); |
| 430 |
v = cm->nodemap[n] + 1; |
| 431 |
map_helper(cm, cp9map, k, ks, v); |
| 432 |
|
| 433 |
ks = 1; |
| 434 |
v = cm->nodemap[n] + 4; |
| 435 |
map_helper(cm, cp9map, k, ks, v); |
| 436 |
|
| 437 |
ks = 2; |
| 438 |
v = cm->nodemap[n] + 2; |
| 439 |
map_helper(cm, cp9map, k, ks, v); |
| 440 |
v = cm->nodemap[n] + 3; |
| 441 |
map_helper(cm, cp9map, k, ks, v); |
| 442 |
} |
| 443 |
else if(is_right) |
| 444 |
{ |
| 445 |
ks = 0; |
| 446 |
v = cm->nodemap[n]; |
| 447 |
map_helper(cm, cp9map, k, ks, v); |
| 448 |
v = cm->nodemap[n] + 2; |
| 449 |
map_helper(cm, cp9map, k, ks, v); |
| 450 |
|
| 451 |
ks = 1; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
if(k != cp9map->hmm_M) |
| 460 |
|
| 461 |
{ |
| 462 |
nn = cp9map->pos2nd[k+1]; |
| 463 |
if(cp9map->nd2lpos[nn] == (k+1)) |
| 464 |
{ |
| 465 |
|
| 466 |
n_begr = nn; |
| 467 |
while(n_begr >= 0 && (cm->ndtype[n_begr] != BEGR_nd)) |
| 468 |
n_begr--; |
| 469 |
if(n_begr == -1) |
| 470 |
{ |
| 471 |
printf("ERROR: can't find BEGR node above node %d\n", nn); |
| 472 |
printf("k is %d\n", k); |
| 473 |
exit(1); |
| 474 |
} |
| 475 |
v = cm->nodemap[n_begr] + 1; |
| 476 |
map_helper(cm, cp9map, k, ks, v); |
| 477 |
} |
| 478 |
else if(cp9map->nd2rpos[nn] == (k+1)) |
| 479 |
{ |
| 480 |
|
| 481 |
if(cm->ndtype[nn] == MATP_nd) |
| 482 |
{ |
| 483 |
v = cm->nodemap[nn] + 5; |
| 484 |
map_helper(cm, cp9map, k, ks, v); |
| 485 |
} |
| 486 |
else if(cm->ndtype[nn] == MATR_nd) |
| 487 |
{ |
| 488 |
v = cm->nodemap[nn] + 2; |
| 489 |
map_helper(cm, cp9map, k, ks, v); |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
else |
| 494 |
{ |
| 495 |
v = 2; |
| 496 |
map_helper(cm, cp9map, k, ks, v); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
if(cp9map->nd2lpos[cp9map->pos2nd[k-1]] == (k-1)) |
| 507 |
{ |
| 508 |
if(cm->ndtype[cp9map->pos2nd[k-1]] != MATL_nd) |
| 509 |
{ |
| 510 |
if(cm->ndtype[cp9map->pos2nd[k-1]] == MATP_nd) |
| 511 |
{ |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
} |
| 518 |
else |
| 519 |
{ |
| 520 |
printf("ERROR, full understanding of the CM architecture remains elusive (0)...\n"); |
| 521 |
exit(1); |
| 522 |
} |
| 523 |
} |
| 524 |
v = cm->nodemap[n] + 5; |
| 525 |
map_helper(cm, cp9map, (k-1), ks, v); |
| 526 |
} |
| 527 |
|
| 528 |
ks = 2; |
| 529 |
v = cm->nodemap[n] + 1; |
| 530 |
map_helper(cm, cp9map, k, ks, v); |
| 531 |
|
| 532 |
v = cm->nodemap[n] + 3; |
| 533 |
map_helper(cm, cp9map, k, ks, v); |
| 534 |
} |
| 535 |
break; |
| 536 |
|
| 537 |
case MATL_nd: |
| 538 |
ks = 0; |
| 539 |
v = cm->nodemap[n]; |
| 540 |
map_helper(cm, cp9map, k, ks, v); |
| 541 |
|
| 542 |
ks = 1; |
| 543 |
v = cm->nodemap[n] + 2; |
| 544 |
map_helper(cm, cp9map, k, ks, v); |
| 545 |
|
| 546 |
ks = 2; |
| 547 |
v = cm->nodemap[n] + 1; |
| 548 |
map_helper(cm, cp9map, k, ks, v); |
| 549 |
|
| 550 |
if(k == cp9map->hmm_M) |
| 551 |
{ |
| 552 |
ks = 1; |
| 553 |
v = 2; |
| 554 |
map_helper(cm, cp9map, k, ks, v); |
| 555 |
} |
| 556 |
|
| 557 |
break; |
| 558 |
|
| 559 |
case MATR_nd: |
| 560 |
ks = 0; |
| 561 |
v = cm->nodemap[n]; |
| 562 |
map_helper(cm, cp9map, k, ks, v); |
| 563 |
|
| 564 |
ks = 1; |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
if(k != cp9map->hmm_M) |
| 574 |
{ |
| 575 |
nn = cp9map->pos2nd[k+1]; |
| 576 |
if(cp9map->nd2lpos[nn] == (k+1)) |
| 577 |
{ |
| 578 |
|
| 579 |
n_begr = nn; |
| 580 |
while((cm->ndtype[n_begr] != BEGR_nd) && n_begr >= 0) |
| 581 |
n_begr--; |
| 582 |
if(n_begr == -1) |
| 583 |
{ |
| 584 |
printf("ERROR: can't find BEGR node above node %d\n", nn); |
| 585 |
exit(1); |
| 586 |
} |
| 587 |
v = cm->nodemap[n_begr] + 1; |
| 588 |
map_helper(cm, cp9map, k, ks, v); |
| 589 |
} |
| 590 |
else if(cp9map->nd2rpos[nn] == (k+1)) |
| 591 |
{ |
| 592 |
|
| 593 |
if(cm->ndtype[nn] == MATP_nd) |
| 594 |
{ |
| 595 |
v = cm->nodemap[nn] + 5; |
| 596 |
map_helper(cm, cp9map, k, ks, v); |
| 597 |
} |
| 598 |
else if(cm->ndtype[nn] == MATR_nd) |
| 599 |
{ |
| 600 |
v = cm->nodemap[nn] + 2; |
| 601 |
map_helper(cm, cp9map, k, ks, v); |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
else |
| 606 |
{ |
| 607 |
v = 2; |
| 608 |
map_helper(cm, cp9map, k, ks, v); |
| 609 |
} |
| 610 |
if(cp9map->nd2lpos[cp9map->pos2nd[k-1]] == (k-1)) |
| 611 |
{ |
| 612 |
printf("ERROR, full understanding of the CM architecture remains elusive (1)...\n"); |
| 613 |
exit(1); |
| 614 |
} |
| 615 |
|
| 616 |
ks = 2; |
| 617 |
v = cm->nodemap[n] + 1; |
| 618 |
map_helper(cm, cp9map, k, ks, v); |
| 619 |
break; |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
for(v = 0; v <= cm->M; v++) |
| 626 |
{ |
| 627 |
if((cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) && |
| 628 |
((cp9map->cs2hn[v][0] == -1) || cp9map->cs2hn[v][1] != -1)) |
| 629 |
{ |
| 630 |
if(cm->sttype[(v+1)] != E_st) |
| 631 |
{ |
| 632 |
printf("ERROR during cp9map->cs2hn construction\ncp9map->cs2hn[%d][0]: %d | cp9map->cs2hn[%d][1]: %d AND v is an INSERT state\n", v, cp9map->cs2hn[v][0], v, cp9map->cs2hn[v][1]); |
| 633 |
exit(1); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
for(k = 0; k <= cp9map->hmm_M; k++) |
| 642 |
{ |
| 643 |
for(ks = 0; ks < 3; ks++) |
| 644 |
{ |
| 645 |
v1 = cp9map->hns2cs[k][ks][0]; |
| 646 |
v2 = cp9map->hns2cs[k][ks][1]; |
| 647 |
if(ks == 1 && v2 != -1) |
| 648 |
{ |
| 649 |
printf("ERROR in CP9_map_cm2hmm: HMM insert state of node: %d\n\tmaps to 2 CM states (%d and %d)\n", k, v1, v2); |
| 650 |
exit(1); |
| 651 |
} |
| 652 |
|
| 653 |
if(debug_level > 1) |
| 654 |
printf("hns2cs[%3d][%3d][0]: %3d | hns2cs[%3d][%3d[1]: %3d\n", k, ks, v1, k, ks, v2); |
| 655 |
if(v1 != -1 && (cp9map->cs2hn[v1][0] == k && cp9map->cs2hs[v1][0] == ks)) |
| 656 |
{ |
| 657 |
|
| 658 |
} |
| 659 |
else if(v1 != -1 && (cp9map->cs2hn[v1][1] == k && cp9map->cs2hs[v1][1] == ks)) |
| 660 |
{ |
| 661 |
|
| 662 |
} |
| 663 |
else if(v2 != -1 && (cp9map->cs2hn[v2][0] == k && cp9map->cs2hs[v2][0] == ks)) |
| 664 |
{ |
| 665 |
|
| 666 |
} |
| 667 |
else if(v2 != -1 && (cp9map->cs2hn[v2][1] == k && cp9map->cs2hs[v2][1] == ks)) |
| 668 |
{ |
| 669 |
|
| 670 |
} |
| 671 |
else if(v1 == -1 && v2 == -1 && (k == 0 && ks == 2)) |
| 672 |
{ |
| 673 |
|
| 674 |
} |
| 675 |
else if(v1 == -1 && v2 == -1 && (k != 0 || ks != 2)) |
| 676 |
|
| 677 |
{ |
| 678 |
|
| 679 |
printf("maps inconsistent case 1, HMM node state (non D_0) maps to no CM state, v1: %d | v2: %d k: %d | ks: %d\n", v1, v2, k, ks); |
| 680 |
exit(1); |
| 681 |
} |
| 682 |
else |
| 683 |
{ |
| 684 |
|
| 685 |
printf("maps inconsistent case 2 v1: %d | v2: %d k: %d | ks: %d\n", v1, v2, k, ks); |
| 686 |
exit(1); |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
return; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
void |
| 714 |
map_helper(CM_t *cm, CP9Map_t *cp9map, int k, int ks, int v) |
| 715 |
{ |
| 716 |
if(ks == 1 && cm->sttype[(v+1)] == E_st) |
| 717 |
{ |
| 718 |
return; |
| 719 |
} |
| 720 |
if(cp9map->cs2hn[v][0] == -1) |
| 721 |
{ |
| 722 |
cp9map->cs2hn[v][0] = k; |
| 723 |
if(cp9map->cs2hs[v][0] != -1) |
| 724 |
cm_Fail("ERROR in map_helper, cp9map->cs2hn[%d][0] is -1 but cp9map->cs2hs[%d][0] is not, this shouldn't happen.\n", v, v); |
| 725 |
cp9map->cs2hs[v][0] = ks; |
| 726 |
} |
| 727 |
else if (cp9map->cs2hn[v][1] == -1) |
| 728 |
{ |
| 729 |
cp9map->cs2hn[v][1] = k; |
| 730 |
if(cp9map->cs2hs[v][1] != -1) |
| 731 |
cm_Fail("ERROR in map_helper, cp9map->cs2hn[%d][0] is -1 but cp9map->cs2hs[%d][0] is not, this shouldn't happen.\n", v, v); |
| 732 |
cp9map->cs2hs[v][1] = ks; |
| 733 |
} |
| 734 |
else |
| 735 |
cm_Fail("ERROR in map_helper, cp9map->cs2hn[%d][1] is not -1, and we're trying to add to it, this shouldn't happen.\n", v); |
| 736 |
|
| 737 |
if(cp9map->hns2cs[k][ks][0] == -1) |
| 738 |
cp9map->hns2cs[k][ks][0] = v; |
| 739 |
else if(cp9map->hns2cs[k][ks][1] == -1) |
| 740 |
cp9map->hns2cs[k][ks][1] = v; |
| 741 |
else |
| 742 |
cm_Fail("ERROR in map_helper, cp9map->hns2cs[%d][%d][1] is not -1, and we're trying to add to it, this shouldn't happen.\n", k, ks); |
| 743 |
return; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
static float |
| 764 |
cm2hmm_emit_prob(CM_t *cm, CP9Map_t *cp9map, int x, int i, int k) |
| 765 |
{ |
| 766 |
float ret_eprob; |
| 767 |
int is_left; |
| 768 |
int j; |
| 769 |
|
| 770 |
if(cp9map->nd2lpos[cp9map->pos2nd[k]] == k) |
| 771 |
is_left = TRUE; |
| 772 |
else |
| 773 |
is_left = FALSE; |
| 774 |
|
| 775 |
ret_eprob = 0.; |
| 776 |
|
| 777 |
|
| 778 |
if(cm->stid[x] != MATP_MP) |
| 779 |
ret_eprob = cm->e[x][i]; |
| 780 |
|
| 781 |
else if(cm->stid[x] == MATP_MP) |
| 782 |
{ |
| 783 |
|
| 784 |
if(is_left) |
| 785 |
for(j = (i*cm->abc->K); j < ((i+1)*cm->abc->K); j++) |
| 786 |
ret_eprob += cm->e[x][j]; |
| 787 |
else |
| 788 |
for(j = i; j < (cm->abc->K*cm->abc->K); j+=cm->abc->K) |
| 789 |
ret_eprob += cm->e[x][j]; |
| 790 |
} |
| 791 |
return ret_eprob; |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
static void |
| 814 |
cm2hmm_special_trans_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, double *psi, |
| 815 |
char ***tmap) |
| 816 |
{ |
| 817 |
int status; |
| 818 |
int *ap; |
| 819 |
|
| 820 |
int *bp; |
| 821 |
|
| 822 |
int k_state; |
| 823 |
int k; |
| 824 |
int hmm_trans_idx; |
| 825 |
float d; |
| 826 |
|
| 827 |
ESL_ALLOC(ap, sizeof (int) * 2); |
| 828 |
ESL_ALLOC(bp, sizeof (int) * 2); |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 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 |
k = 0; |
| 888 |
k_state = HMMMATCH; |
| 889 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 890 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 891 |
|
| 892 |
k_state = HMMMATCH; |
| 893 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 894 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 895 |
|
| 896 |
hmm_trans_idx = CTMM; |
| 897 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 898 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 899 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 900 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 901 |
|
| 902 |
|
| 903 |
hmm->begin[1] = hmm->t[0][CTMM]; |
| 904 |
hmm->t[0][CTMM] = 0.; |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
k = 0; |
| 909 |
k_state = HMMMATCH; |
| 910 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 911 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 912 |
|
| 913 |
k_state = HMMINSERT; |
| 914 |
bp[0] = cp9map->hns2cs[k][k_state][0]; |
| 915 |
bp[1] = cp9map->hns2cs[k][k_state][1]; |
| 916 |
hmm_trans_idx = CTMI; |
| 917 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 918 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 919 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 920 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 921 |
|
| 922 |
|
| 923 |
k = 0; |
| 924 |
k_state = HMMMATCH; |
| 925 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 926 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 927 |
|
| 928 |
k_state = HMMDELETE; |
| 929 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 930 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 931 |
hmm_trans_idx = CTMD; |
| 932 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 933 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 934 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 935 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 936 |
|
| 937 |
|
| 938 |
k = 0; |
| 939 |
k_state = HMMINSERT; |
| 940 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 941 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 942 |
|
| 943 |
k_state = HMMMATCH; |
| 944 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 945 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 946 |
hmm_trans_idx = CTIM; |
| 947 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 948 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 949 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 950 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 951 |
|
| 952 |
|
| 953 |
k = 0; |
| 954 |
k_state = HMMINSERT; |
| 955 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 956 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 957 |
|
| 958 |
k_state = HMMINSERT; |
| 959 |
bp[0] = cp9map->hns2cs[k][k_state][0]; |
| 960 |
bp[1] = cp9map->hns2cs[k][k_state][1]; |
| 961 |
hmm_trans_idx = CTII; |
| 962 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 963 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 964 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 965 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 966 |
|
| 967 |
|
| 968 |
k = 0; |
| 969 |
k_state = HMMINSERT; |
| 970 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 971 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 972 |
|
| 973 |
k_state = HMMDELETE; |
| 974 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 975 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 976 |
hmm_trans_idx = CTID; |
| 977 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 978 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 979 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 980 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 981 |
|
| 982 |
|
| 983 |
hmm->t[0][CTDM] = 0.; |
| 984 |
hmm->t[0][CTDI] = 0.; |
| 985 |
hmm->t[0][CTDD] = 0.; |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
d =esl_vec_FSum(hmm->begin+1, hmm->M) + hmm->t[0][CTMI] + hmm->t[0][CTMD]; |
| 991 |
esl_vec_FScale(hmm->begin+1, hmm->M, 1./d); |
| 992 |
hmm->t[0][CTMI] /= d; |
| 993 |
hmm->t[0][CTMD] /= d; |
| 994 |
|
| 995 |
esl_vec_FNorm(hmm->t[0]+cp9_TRANS_INSERT_OFFSET, 3); |
| 996 |
|
| 997 |
k = 0; |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
k = hmm->M; |
| 1019 |
k_state = HMMMATCH; |
| 1020 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1021 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1022 |
|
| 1023 |
bp[0] = (cm->M) - 1; |
| 1024 |
bp[1] = -1; |
| 1025 |
hmm_trans_idx = CTMM; |
| 1026 |
|
| 1027 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1028 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1029 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1030 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1031 |
|
| 1032 |
hmm->end[hmm->M] = hmm->t[hmm->M][CTMM]; |
| 1033 |
hmm->t[hmm->M][CTMM] = 0.; |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
k = hmm->M; |
| 1040 |
k_state = HMMMATCH; |
| 1041 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1042 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1043 |
|
| 1044 |
k_state = HMMINSERT; |
| 1045 |
bp[0] = cp9map->hns2cs[k][k_state][0]; |
| 1046 |
bp[1] = cp9map->hns2cs[k][k_state][1]; |
| 1047 |
|
| 1048 |
hmm_trans_idx = CTMI; |
| 1049 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1050 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1051 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1052 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
hmm->t[hmm->M][CTMD] = 0.; |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
k = hmm->M; |
| 1064 |
k_state = HMMINSERT; |
| 1065 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1066 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1067 |
|
| 1068 |
bp[0] = (cm->M) - 1; |
| 1069 |
bp[1] = -1; |
| 1070 |
hmm_trans_idx = CTIM; |
| 1071 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1072 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1073 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1074 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
k = hmm->M; |
| 1081 |
k_state = HMMINSERT; |
| 1082 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1083 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1084 |
|
| 1085 |
k_state = HMMINSERT; |
| 1086 |
bp[0] = cp9map->hns2cs[k][k_state][0]; |
| 1087 |
bp[1] = cp9map->hns2cs[k][k_state][1]; |
| 1088 |
|
| 1089 |
hmm_trans_idx = CTII; |
| 1090 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1091 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1092 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1093 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
hmm->t[hmm->M][CTID] = 0.; |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
k = hmm->M; |
| 1105 |
k_state = HMMDELETE; |
| 1106 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1107 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1108 |
|
| 1109 |
bp[0] = (cm->M) - 1; |
| 1110 |
bp[1] = -1; |
| 1111 |
hmm_trans_idx = CTDM; |
| 1112 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1113 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1114 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1115 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
k = hmm->M; |
| 1122 |
k_state = HMMDELETE; |
| 1123 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1124 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1125 |
|
| 1126 |
k_state = HMMINSERT; |
| 1127 |
bp[0] = cp9map->hns2cs[k][k_state][0]; |
| 1128 |
bp[1] = cp9map->hns2cs[k][k_state][1]; |
| 1129 |
|
| 1130 |
hmm_trans_idx = CTDI; |
| 1131 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1132 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1133 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1134 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
hmm->t[hmm->M][CTDD] = 0.; |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
k = hmm->M; |
| 1144 |
d =esl_vec_FSum(hmm->t[k], 4) + hmm->end[k]; |
| 1145 |
esl_vec_FScale(hmm->t[k], 4, 1./d); |
| 1146 |
hmm->end[k] /= d; |
| 1147 |
esl_vec_FNorm(hmm->t[k]+cp9_TRANS_INSERT_OFFSET, 3); |
| 1148 |
esl_vec_FNorm(hmm->t[k]+cp9_TRANS_DELETE_OFFSET, 3); |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
free(ap); |
| 1165 |
free(bp); |
| 1166 |
return; |
| 1167 |
|
| 1168 |
ERROR: |
| 1169 |
cm_Fail("Memory allocation error."); |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
static void |
| 1194 |
cm2hmm_trans_probs_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, int k, double *psi, char ***tmap) |
| 1195 |
{ |
| 1196 |
int status; |
| 1197 |
int *ap; |
| 1198 |
|
| 1199 |
int *bp; |
| 1200 |
|
| 1201 |
|
| 1202 |
int k_state; |
| 1203 |
|
| 1204 |
int hmm_trans_idx; |
| 1205 |
float d; |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
ESL_ALLOC(ap, sizeof (int) * 2); |
| 1210 |
ESL_ALLOC(bp, sizeof (int) * 2); |
| 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 |
k_state = HMMMATCH; |
| 1245 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1246 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1247 |
|
| 1248 |
k_state = HMMMATCH; |
| 1249 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 1250 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 1251 |
hmm_trans_idx = CTMM; |
| 1252 |
|
| 1253 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1254 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1255 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1256 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
k_state = HMMMATCH; |
| 1264 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1265 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1266 |
|
| 1267 |
k_state = HMMINSERT; |
| 1268 |
bp[0] = cp9map->hns2cs[k][k_state][0]; |
| 1269 |
bp[1] = cp9map->hns2cs[k][k_state][1]; |
| 1270 |
|
| 1271 |
hmm_trans_idx = CTMI; |
| 1272 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1273 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1274 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1275 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
k_state = HMMMATCH; |
| 1282 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1283 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1284 |
|
| 1285 |
k_state = HMMDELETE; |
| 1286 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 1287 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 1288 |
|
| 1289 |
hmm_trans_idx = CTMD; |
| 1290 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1291 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1292 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1293 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
k_state = HMMINSERT; |
| 1300 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1301 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1302 |
|
| 1303 |
k_state = HMMMATCH; |
| 1304 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 1305 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 1306 |
|
| 1307 |
hmm_trans_idx = CTIM; |
| 1308 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1309 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1310 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1311 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
k_state = HMMINSERT; |
| 1318 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1319 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1320 |
|
| 1321 |
k_state = HMMINSERT; |
| 1322 |
bp[0] = cp9map->hns2cs[k][k_state][0]; |
| 1323 |
bp[1] = cp9map->hns2cs[k][k_state][1]; |
| 1324 |
|
| 1325 |
|
| 1326 |
hmm_trans_idx = CTII; |
| 1327 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1328 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1329 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1330 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
k_state = HMMINSERT; |
| 1337 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1338 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1339 |
|
| 1340 |
k_state = HMMDELETE; |
| 1341 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 1342 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 1343 |
|
| 1344 |
hmm_trans_idx = CTID; |
| 1345 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1346 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1347 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1348 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
k_state = HMMDELETE; |
| 1355 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1356 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1357 |
|
| 1358 |
k_state = HMMMATCH; |
| 1359 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 1360 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 1361 |
|
| 1362 |
hmm_trans_idx = CTDM; |
| 1363 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1364 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1365 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1366 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
k_state = HMMDELETE; |
| 1373 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1374 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1375 |
|
| 1376 |
k_state = HMMINSERT; |
| 1377 |
bp[0] = cp9map->hns2cs[k][k_state][0]; |
| 1378 |
bp[1] = cp9map->hns2cs[k][k_state][1]; |
| 1379 |
|
| 1380 |
hmm_trans_idx = CTDI; |
| 1381 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1382 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1383 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1384 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
k_state = HMMDELETE; |
| 1391 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1392 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1393 |
|
| 1394 |
k_state = HMMDELETE; |
| 1395 |
bp[0] = cp9map->hns2cs[k+1][k_state][0]; |
| 1396 |
bp[1] = cp9map->hns2cs[k+1][k_state][1]; |
| 1397 |
|
| 1398 |
hmm_trans_idx = CTDD; |
| 1399 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1400 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1401 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap); |
| 1402 |
hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap); |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
d = esl_vec_FSum(hmm->t[k], 4) + hmm->end[k]; |
| 1407 |
esl_vec_FScale(hmm->t[k], 4, 1./d); |
| 1408 |
hmm->end[k] /= d; |
| 1409 |
|
| 1410 |
esl_vec_FNorm(hmm->t[k]+cp9_TRANS_INSERT_OFFSET, 3); |
| 1411 |
esl_vec_FNorm(hmm->t[k]+cp9_TRANS_DELETE_OFFSET, 3); |
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
free(ap); |
| 1427 |
free(bp); |
| 1428 |
return; |
| 1429 |
|
| 1430 |
ERROR: |
| 1431 |
cm_Fail("Memory allocation error."); |
| 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 |
|
| 1457 |
|
| 1458 |
void |
| 1459 |
fill_phi_cp9(CP9_t *hmm, double ***ret_phi, int spos, int entered_only) |
| 1460 |
{ |
| 1461 |
int status; |
| 1462 |
int k; |
| 1463 |
double **phi; |
| 1464 |
double *phi_ins = NULL; |
| 1465 |
|
| 1466 |
ESL_ALLOC(phi, sizeof(double *) * (hmm->M+1)); |
| 1467 |
for(k = 0; k <= hmm->M; k++) |
| 1468 |
ESL_ALLOC(phi[k], sizeof(double) * 3); |
| 1469 |
|
| 1470 |
if(entered_only) ESL_ALLOC(phi_ins, sizeof(double) * (hmm->M+1)); |
| 1471 |
|
| 1472 |
|
| 1473 |
for (k = 0; k <= hmm->M; k++) |
| 1474 |
phi[k][0] = phi[k][1] = phi[k][2] = 0.; |
| 1475 |
|
| 1476 |
|
| 1477 |
phi[spos-1][HMMMATCH] = 1.0; |
| 1478 |
phi[spos-1][HMMINSERT] = phi[spos-1][HMMMATCH] * hmm->t[spos-1][CTMI]; |
| 1479 |
if(entered_only) phi_ins[spos-1] = phi[spos-1][HMMINSERT]; |
| 1480 |
phi[spos-1][HMMINSERT] += phi[spos-1][HMMINSERT] * (hmm->t[spos-1][CTII] / |
| 1481 |
(1. - hmm->t[spos-1][CTII])); |
| 1482 |
phi[spos-1][HMMDELETE] = 0.; |
| 1483 |
|
| 1484 |
|
| 1485 |
for (k = 1; k <= hmm->M; k++) |
| 1486 |
{ |
| 1487 |
if(k == (spos-1)) |
| 1488 |
continue; |
| 1489 |
|
| 1490 |
|
| 1491 |
phi[k][HMMMATCH] += phi[k-1][HMMMATCH] * hmm->t[k-1][CTMM]; |
| 1492 |
phi[k][HMMMATCH] += phi[k-1][HMMDELETE] * hmm->t[k-1][CTDM]; |
| 1493 |
phi[k][HMMMATCH] += phi[k-1][HMMINSERT] * hmm->t[k-1][CTIM]; |
| 1494 |
phi[k][HMMMATCH] += hmm->begin[k]; |
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
phi[k][HMMDELETE] += phi[k-1][HMMMATCH] * hmm->t[k-1][CTMD]; |
| 1499 |
phi[k][HMMDELETE] += phi[k-1][HMMINSERT] * hmm->t[k-1][CTID]; |
| 1500 |
phi[k][HMMDELETE] += phi[k-1][HMMDELETE] * hmm->t[k-1][CTDD]; |
| 1501 |
|
| 1502 |
|
| 1503 |
phi[k][HMMINSERT] += phi[k][HMMMATCH] * hmm->t[k][CTMI]; |
| 1504 |
phi[k][HMMINSERT] += phi[k][HMMDELETE] * hmm->t[k][CTDI]; |
| 1505 |
|
| 1506 |
if(entered_only) phi_ins[k] = phi[k][HMMINSERT]; |
| 1507 |
phi[k][HMMINSERT] += (phi[k][HMMINSERT] * (hmm->t[k][CTII] / (1-hmm->t[k][CTII]))); |
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
} |
| 1514 |
if(entered_only) { |
| 1515 |
for(k = 0; k <= hmm->M; k++) phi[k][HMMINSERT] = phi_ins[k]; |
| 1516 |
free(phi_ins); |
| 1517 |
} |
| 1518 |
*ret_phi = phi; |
| 1519 |
return; |
| 1520 |
|
| 1521 |
ERROR: |
| 1522 |
cm_Fail("Memory allocation error."); |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
static void |
| 1546 |
hmm_add_single_trans_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, int a, int b, int k, int hmm_trans_idx, |
| 1547 |
double *psi, char ***tmap) |
| 1548 |
{ |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
if(a == -1 || b == -1) |
| 1555 |
return; |
| 1556 |
|
| 1557 |
if(a <= b) |
| 1558 |
hmm->t[k][hmm_trans_idx] += psi[a] * cm_sum_subpaths_cp9(cm, cp9map, a, b, tmap, k, psi); |
| 1559 |
else if (a > b) |
| 1560 |
hmm->t[k][hmm_trans_idx] += psi[b] * cm_sum_subpaths_cp9(cm, cp9map, b, a, tmap, k, psi); |
| 1561 |
|
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
static float |
| 1597 |
cm_sum_subpaths_cp9(CM_t *cm, CP9Map_t *cp9map, int start, int end, char ***tmap, |
| 1598 |
int k, double *psi) |
| 1599 |
{ |
| 1600 |
int status; |
| 1601 |
int v; |
| 1602 |
|
| 1603 |
double *sub_psi; |
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
float to_return; |
| 1609 |
int y; |
| 1610 |
int x; |
| 1611 |
char tmap_val; |
| 1612 |
int is_insert; |
| 1613 |
float insert_to_start; |
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
if(start > end) |
| 1622 |
{ |
| 1623 |
printf("ERROR in cm_sum_subpaths_cp9: start: %d > end: %d\n", start, end); |
| 1624 |
exit(1); |
| 1625 |
} |
| 1626 |
if(start == end) |
| 1627 |
{ |
| 1628 |
if(cm->sttype[start] != IL_st && cm->sttype[start] != IR_st) |
| 1629 |
{ |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
if((cm->stid[start] != MATP_MP && cm->stid[start] != MATP_D) && |
| 1641 |
(cm->stid[start] != MATP_ML && cm->stid[start] != MATP_MR)) |
| 1642 |
{ |
| 1643 |
printf("ERROR asking for self transition of non-insert, non-MATP state: %d\n", start); |
| 1644 |
exit(1); |
| 1645 |
} |
| 1646 |
return 1.0; |
| 1647 |
} |
| 1648 |
|
| 1649 |
|
| 1650 |
return cm->t[start][0]; |
| 1651 |
} |
| 1652 |
to_return = 0.; |
| 1653 |
|
| 1654 |
ESL_ALLOC(sub_psi, sizeof(double) * (end - start + 1)); |
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
sub_psi[0] = 1.; |
| 1662 |
|
| 1663 |
if((cm->sttype[start] != IL_st && cm->sttype[start] != IR_st) && |
| 1664 |
(cm->sttype[end] != IL_st && cm->sttype[end] != IR_st)) |
| 1665 |
{ |
| 1666 |
insert_to_start = 0.; |
| 1667 |
if(cp9map->hns2cs[k][1][0] < start) |
| 1668 |
insert_to_start = psi[cp9map->hns2cs[k][1][0]] * cm_sum_subpaths_cp9(cm, cp9map, cp9map->hns2cs[k][1][0], start, tmap, k, psi); |
| 1669 |
if((cp9map->hns2cs[k][1][1] != -1) && (cp9map->hns2cs[k][1][1] < start)) |
| 1670 |
insert_to_start += psi[cp9map->hns2cs[k][1][1]] * cm_sum_subpaths_cp9(cm, cp9map, cp9map->hns2cs[k][1][1], start, tmap, k, psi); |
| 1671 |
sub_psi[0] -= insert_to_start / psi[start]; |
| 1672 |
|
| 1673 |
} |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
for (v = (start+1); v <= end; v++) |
| 1680 |
{ |
| 1681 |
|
| 1682 |
sub_psi[v-start] = 0.; |
| 1683 |
if(cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) |
| 1684 |
is_insert = 1; |
| 1685 |
else |
| 1686 |
is_insert = 0; |
| 1687 |
|
| 1688 |
if(cm->sttype[v] == S_st) |
| 1689 |
{ |
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
sub_psi[v-start] = sub_psi[(v-1)-start] * 1.; |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
if((v != end && is_insert) && ((cp9map->cs2hn[v][0] == k) || cp9map->cs2hn[v][1] == k)) |
| 1700 |
{ |
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
} |
| 1710 |
else |
| 1711 |
{ |
| 1712 |
for (y = cm->pnum[v]-1; y >= is_insert; y--) |
| 1713 |
{ |
| 1714 |
x = cm->plast[v] - y; |
| 1715 |
|
| 1716 |
|
| 1717 |
tmap_val = tmap[(int) cm->stid[x]][(int) cm->ndtype[cm->ndidx[v]+is_insert]][(int) cm->stid[v]]; |
| 1718 |
#if eslDEBUGLEVEL >= 1 |
| 1719 |
if(tmap_val == -1) |
| 1720 |
{ |
| 1721 |
printf("#DEBUG: tmap ERROR 1\n"); |
| 1722 |
printf("#DEBUG: v: %d | pnum[v]: %d | plast[v]: %d | y: %d | x: %d | d1: %d | d2: %d | d3: %d\n", v, cm->pnum[v], cm->plast[v], y, x, ((int) cm->stid[x]), ((int) (cm->ndtype[cm->ndidx[v]+is_insert])), ((int) cm->stid[v])); |
| 1723 |
exit(1); |
| 1724 |
} |
| 1725 |
#endif |
| 1726 |
if((x - start) < 0) |
| 1727 |
sub_psi[v-start] += 0.; |
| 1728 |
else |
| 1729 |
sub_psi[v-start] += sub_psi[x-start] * cm->t[x][(int) tmap_val]; |
| 1730 |
} |
| 1731 |
if(v != end && is_insert) |
| 1732 |
|
| 1733 |
{ |
| 1734 |
sub_psi[v-start] += sub_psi[v-start] * (cm->t[v][0] / (1-cm->t[v][0])); |
| 1735 |
|
| 1736 |
} |
| 1737 |
} |
| 1738 |
} |
| 1739 |
to_return = (float) sub_psi[end-start]; |
| 1740 |
free(sub_psi); |
| 1741 |
return to_return; |
| 1742 |
|
| 1743 |
ERROR: |
| 1744 |
cm_Fail("Memory allocation error."); |
| 1745 |
return 0.; |
| 1746 |
} |
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
static int |
| 1772 |
check_psi_vs_phi_cp9(CM_t *cm, char *errbuf,CP9Map_t *cp9map, double *psi, double **phi, double threshold, |
| 1773 |
int debug_level) |
| 1774 |
{ |
| 1775 |
int status; |
| 1776 |
int v; |
| 1777 |
int k; |
| 1778 |
int y; |
| 1779 |
double summed_psi; |
| 1780 |
int *ap; |
| 1781 |
int k_state; |
| 1782 |
int violation; |
| 1783 |
int v_ct; |
| 1784 |
double diff; |
| 1785 |
int ret_val; |
| 1786 |
int adj_bp_flag; |
| 1787 |
|
| 1788 |
if (cm->flags & CMH_LOCAL_BEGIN) ESL_FAIL(eslEINCOMPAT, errbuf, "internal error: we're in CM local mode while trying to build a CP9 HMM"); |
| 1789 |
|
| 1790 |
adj_bp_flag = FALSE; |
| 1791 |
if(check_cm_adj_bp(cm, cp9map)) |
| 1792 |
{ |
| 1793 |
adj_bp_flag = TRUE; |
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
} |
| 1802 |
ret_val = TRUE; |
| 1803 |
v_ct = 0; |
| 1804 |
ESL_ALLOC(ap, sizeof(int) * 2); |
| 1805 |
|
| 1806 |
for(v = 0; v < cm->M; v++) |
| 1807 |
if(cm->stid[v] != BIF_B) |
| 1808 |
{ |
| 1809 |
for(y = 0; y < cm->cnum[v]; y++) |
| 1810 |
if(debug_level > 1) printf("cm->t[%d][%d]: %f\n", v, y, cm->t[v][y]); |
| 1811 |
if(debug_level > 1) printf("\n"); |
| 1812 |
} |
| 1813 |
|
| 1814 |
for (k = 0; k <= cp9map->hmm_M; k++) |
| 1815 |
{ |
| 1816 |
k_state = HMMMATCH; |
| 1817 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1818 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1819 |
summed_psi = psi[ap[0]]; |
| 1820 |
if(ap[1] != -1) |
| 1821 |
summed_psi += psi[ap[1]]; |
| 1822 |
violation = FALSE; |
| 1823 |
diff = phi[k][0] - summed_psi; |
| 1824 |
if((diff > threshold) || ((-1. * diff) > threshold)) |
| 1825 |
{ |
| 1826 |
violation = TRUE; |
| 1827 |
v_ct++; |
| 1828 |
} |
| 1829 |
if(violation) |
| 1830 |
printf("M k: %4d | phi: %f | psi: %f VIOLATION (%f) (cm v1: %d cm v2: %d)\n", k, phi[k][0], summed_psi, diff, ap[0], ap[1]); |
| 1831 |
else if(debug_level > 1) |
| 1832 |
printf("M k: %4d | phi: %f | psi: %f\n", k, phi[k][0], summed_psi); |
| 1833 |
|
| 1834 |
k_state = HMMINSERT; |
| 1835 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1836 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1837 |
if(ap[1] != -1) |
| 1838 |
cm_Fail("ERROR, HMM insert state of node %d maps to 2 CM insert states: %d and %d\n", k, ap[0], ap[1]); |
| 1839 |
summed_psi = psi[ap[0]]; |
| 1840 |
violation = FALSE; |
| 1841 |
diff = phi[k][1] - summed_psi; |
| 1842 |
if((diff > threshold) || ((-1. * diff) > threshold)) |
| 1843 |
{ |
| 1844 |
violation = TRUE; |
| 1845 |
v_ct++; |
| 1846 |
} |
| 1847 |
if(violation) |
| 1848 |
printf("I k: %4d | phi: %f | psi: %f VIOLATION (%f) (cm v1: %d cm v2: %d)\n", k, phi[k][1], summed_psi, diff, ap[0], ap[1]); |
| 1849 |
else if(debug_level > 1) |
| 1850 |
printf("I k: %4d | phi: %f | psi: %f\n", k, phi[k][1], summed_psi); |
| 1851 |
|
| 1852 |
k_state = HMMDELETE; |
| 1853 |
ap[0] = cp9map->hns2cs[k][k_state][0]; |
| 1854 |
ap[1] = cp9map->hns2cs[k][k_state][1]; |
| 1855 |
if(k == 0) |
| 1856 |
summed_psi = 0.; |
| 1857 |
else |
| 1858 |
{ |
| 1859 |
summed_psi = psi[ap[0]]; |
| 1860 |
if(ap[1] != -1) |
| 1861 |
summed_psi += psi[ap[1]]; |
| 1862 |
} |
| 1863 |
violation = FALSE; |
| 1864 |
diff = phi[k][2] - summed_psi; |
| 1865 |
if((diff > threshold) || ((-1. * diff) > threshold)) |
| 1866 |
{ |
| 1867 |
violation = TRUE; |
| 1868 |
v_ct++; |
| 1869 |
} |
| 1870 |
if(violation) |
| 1871 |
printf("D k: %4d | phi: %f | psi: %f VIOLATION (%f) (cm v1: %d cm v2: %d)\n", k, phi[k][2], summed_psi, diff, ap[0], ap[1]); |
| 1872 |
else if(debug_level > 1) |
| 1873 |
{ |
| 1874 |
printf("D k: %4d | phi: %f | psi: %f\n\n", k, phi[k][2], summed_psi); |
| 1875 |
} |
| 1876 |
} |
| 1877 |
free(ap); |
| 1878 |
|
| 1879 |
if(v_ct > 0) |
| 1880 |
{ |
| 1881 |
printf("ERROR, %d HMM states violate the %f threshold b/t psi and phi.\n", v_ct, threshold); |
| 1882 |
ret_val = FALSE; |
| 1883 |
} |
| 1884 |
if(adj_bp_flag == TRUE) return eslOK; |
| 1885 |
|
| 1886 |
if(ret_val == FALSE) ESL_FAIL(eslFAIL, errbuf, "cp9 hmm fails psi/phi test"); |
| 1887 |
|
| 1888 |
return eslOK; |
| 1889 |
|
| 1890 |
ERROR: |
| 1891 |
cm_Fail("Memory allocation error."); |
| 1892 |
return 0.; |
| 1893 |
} |
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
void |
| 1909 |
debug_print_cp9_params(FILE *fp, CP9_t *hmm, int print_scores) |
| 1910 |
{ |
| 1911 |
int k, i; |
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
fprintf(fp, "Consensus Length: %d\n", hmm->M); |
| 1916 |
fprintf(fp, "Node: 0\n"); |
| 1917 |
for(i = 0; i < hmm->abc->K; i++) |
| 1918 |
{ |
| 1919 |
if(print_scores) fprintf(fp, "ins[%3d][%3d] = %.3f | %10d\n", 0, i, hmm->ins[0][i], hmm->isc[i][0]); |
| 1920 |
else fprintf(fp, "ins[%3d][%3d] = %.3f\n", 0, i, hmm->ins[0][i]); |
| 1921 |
} |
| 1922 |
fprintf(fp, "\n"); |
| 1923 |
|
| 1924 |
k=0; |
| 1925 |
if(print_scores) { |
| 1926 |
fprintf(fp, "\tCTMM[%d] = %f | %d\n", k, hmm->t[0][CTMM], hmm->tsc[CTMM][0]); |
| 1927 |
fprintf(fp, "\tCTMI[%d] = %f | %d\n", k, hmm->t[0][CTMI], hmm->tsc[CTMI][0]); |
| 1928 |
fprintf(fp, "\tCTMD[%d] = %f | %d\n", k, hmm->t[0][CTMD], hmm->tsc[CTMD][0]); |
| 1929 |
fprintf(fp, "\tCTMEL[%d] = %f | %d\n", k, hmm->t[0][CTMEL], hmm->tsc[CTMEL][0]); |
| 1930 |
fprintf(fp, "\tCTIM[%d] = %f | %d\n", k, hmm->t[0][CTIM], hmm->tsc[CTIM][0]); |
| 1931 |
fprintf(fp, "\tCTII[%d] = %f | %d\n", k, hmm->t[0][CTII], hmm->tsc[CTII][0]); |
| 1932 |
fprintf(fp, "\tCTID[%d] = %f | %d\n", k, hmm->t[0][CTID], hmm->tsc[CTID][0]); |
| 1933 |
fprintf(fp, "\tCTDM[%d] = %f | %d\n", k, hmm->t[0][CTDM], hmm->tsc[CTDM][0]); |
| 1934 |
fprintf(fp, "\tCTDI[%d] = %f | %d\n", k, hmm->t[0][CTDI], hmm->tsc[CTDI][0]); |
| 1935 |
fprintf(fp, "\tCTDD[%d] = %f | %d\n", k, hmm->t[0][CTDD], hmm->tsc[CTDD][0]); |
| 1936 |
} |
| 1937 |
else { |
| 1938 |
fprintf(fp, "\tCTMM[%d] = %f\n", k, hmm->t[0][CTMM]); |
| 1939 |
fprintf(fp, "\tCTMI[%d] = %f\n", k, hmm->t[0][CTMI]); |
| 1940 |
fprintf(fp, "\tCTMD[%d] = %f\n", k, hmm->t[0][CTMD]); |
| 1941 |
fprintf(fp, "\tCTMEL[%d] = %f\n", k, hmm->t[0][CTMEL]); |
| 1942 |
fprintf(fp, "\tCTIM[%d] = %f\n", k, hmm->t[0][CTIM]); |
| 1943 |
fprintf(fp, "\tCTII[%d] = %f\n", k, hmm->t[0][CTII]); |
| 1944 |
fprintf(fp, "\tCTID[%d] = %f\n", k, hmm->t[0][CTID]); |
| 1945 |
fprintf(fp, "\tCTDM[%d] = %f\n", k, hmm->t[0][CTDM]); |
| 1946 |
fprintf(fp, "\tCTDI[%d] = %f\n", k, hmm->t[0][CTDI]); |
| 1947 |
fprintf(fp, "\tCTDD[%d] = %f\n", k, hmm->t[0][CTDD]); |
| 1948 |
} |
| 1949 |
for(k = 1; k <= hmm->M; k++) |
| 1950 |
{ |
| 1951 |
fprintf(fp, "Node: %d\n", k); |
| 1952 |
for(i = 0; i < hmm->abc->Kp; i++) |
| 1953 |
{ |
| 1954 |
if(print_scores) |
| 1955 |
fprintf(fp, "mat[%3d][%3d] = %.3f | %10d\n", k, i, hmm->mat[k][i], hmm->msc[i][k]); |
| 1956 |
else |
| 1957 |
fprintf(fp, "mat[%3d][%3d] = %.3f\n", k, i, hmm->mat[k][i]); |
| 1958 |
} |
| 1959 |
for(i = 0; i < hmm->abc->Kp; i++) |
| 1960 |
{ |
| 1961 |
if(print_scores) |
| 1962 |
fprintf(fp, "ins[%3d][%3d] = %.3f | %10d\n", k, i, hmm->ins[k][i], hmm->isc[i][k]); |
| 1963 |
else |
| 1964 |
fprintf(fp, "ins[%3d][%3d] = %.3f\n", k, i, hmm->ins[k][i]); |
| 1965 |
} |
| 1966 |
fprintf(fp, "\n"); |
| 1967 |
if(print_scores) { |
| 1968 |
fprintf(fp, "\tCTMM[%d] = %f | %10d\n", k, hmm->t[k][CTMM], hmm->tsc[CTMM][k]); |
| 1969 |
fprintf(fp, "\tCTMI[%d] = %f | %10d\n", k, hmm->t[k][CTMI], hmm->tsc[CTMI][k]); |
| 1970 |
fprintf(fp, "\tCTMD[%d] = %f | %10d\n", k, hmm->t[k][CTMD], hmm->tsc[CTMD][k]); |
| 1971 |
fprintf(fp, "\tCTMEL[%d] = %f | %10d\n", k, hmm->t[k][CTMEL], hmm->tsc[CTMEL][k]); |
| 1972 |
fprintf(fp, "\tCTIM[%d] = %f | %10d\n", k, hmm->t[k][CTIM], hmm->tsc[CTIM][k]); |
| 1973 |
fprintf(fp, "\tCTII[%d] = %f | %10d\n", k, hmm->t[k][CTII], hmm->tsc[CTII][k]); |
| 1974 |
fprintf(fp, "\tCTID[%d] = %f | %10d\n", k, hmm->t[k][CTID], hmm->tsc[CTID][k]); |
| 1975 |
fprintf(fp, "\tCTDM[%d] = %f | %10d\n", k, hmm->t[k][CTDM], hmm->tsc[CTDM][k]); |
| 1976 |
fprintf(fp, "\tCTDI[%d] = %f | %10d\n", k, hmm->t[k][CTDI], hmm->tsc[CTDI][k]); |
| 1977 |
fprintf(fp, "\tCTDD[%d] = %f | %10d\n", k, hmm->t[k][CTDD], hmm->tsc[CTDD][k]); |
| 1978 |
fprintf(fp, "\t beg[%d] = %f | %10d\n", k, hmm->begin[k], hmm->bsc[k]); |
| 1979 |
fprintf(fp, "\t end[%d] = %f | %10d\n", k, hmm->end[k], hmm->esc[k]); |
| 1980 |
} |
| 1981 |
else { |
| 1982 |
fprintf(fp, "\tCTMM[%d] = %f\n", k, hmm->t[k][CTMM]); |
| 1983 |
fprintf(fp, "\tCTMI[%d] = %f\n", k, hmm->t[k][CTMI]); |
| 1984 |
fprintf(fp, "\tCTMD[%d] = %f\n", k, hmm->t[k][CTMD]); |
| 1985 |
fprintf(fp, "\tCTMEL[%d] = %f\n", k, hmm->t[k][CTMEL]); |
| 1986 |
fprintf(fp, "\tCTIM[%d] = %f\n", k, hmm->t[k][CTIM]); |
| 1987 |
fprintf(fp, "\tCTII[%d] = %f\n", k, hmm->t[k][CTII]); |
| 1988 |
fprintf(fp, "\tCTID[%d] = %f\n", k, hmm->t[k][CTID]); |
| 1989 |
fprintf(fp, "\tCTDM[%d] = %f\n", k, hmm->t[k][CTDM]); |
| 1990 |
fprintf(fp, "\tCTDI[%d] = %f\n", k, hmm->t[k][CTDI]); |
| 1991 |
fprintf(fp, "\tCTDD[%d] = %f\n", k, hmm->t[k][CTDD]); |
| 1992 |
fprintf(fp, "\t beg[%d] = %f\n", k, hmm->begin[k]); |
| 1993 |
fprintf(fp, "\t end[%d] = %f\n", k, hmm->end[k]); |
| 1994 |
} |
| 1995 |
fprintf(fp, "\n"); |
| 1996 |
} |
| 1997 |
} |
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
int |
| 2030 |
CP9_check_by_sampling(CM_t *cm, CP9_t *hmm, char *errbuf, ESL_RANDOMNESS *r, CMSubInfo_t *subinfo, |
| 2031 |
int spos, int epos, float chi_thresh, int nsamples, int print_flag) |
| 2032 |
{ |
| 2033 |
int status; |
| 2034 |
Parsetree_t **tr; |
| 2035 |
ESL_SQ **sq; |
| 2036 |
ESL_MSA *msa; |
| 2037 |
float *wgt; |
| 2038 |
char *name; |
| 2039 |
int i, nd; |
| 2040 |
int L; |
| 2041 |
int apos; |
| 2042 |
int *matassign; |
| 2043 |
int *useme; |
| 2044 |
CP9trace_t **cp9_tr; |
| 2045 |
CP9_t *shmm; |
| 2046 |
int msa_nseq; |
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
int nsampled; |
| 2052 |
int cc; |
| 2053 |
int v_ct; |
| 2054 |
int spredict_total_ct; |
| 2055 |
int swrong_total_ct; |
| 2056 |
int namelen; |
| 2057 |
char *tmp_name; |
| 2058 |
|
| 2059 |
spredict_total_ct = 0; |
| 2060 |
swrong_total_ct = 0; |
| 2061 |
v_ct = 0; |
| 2062 |
|
| 2063 |
msa_nseq = 1000; |
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
shmm = AllocCPlan9(hmm->M, hmm->abc); |
| 2069 |
ZeroCPlan9(shmm); |
| 2070 |
CPlan9SetNullModel(shmm, hmm->null, 1.0); |
| 2071 |
|
| 2072 |
CPlan9Renormalize(hmm); |
| 2073 |
CMRenormalize(cm); |
| 2074 |
|
| 2075 |
|
| 2076 |
nsampled = 0; |
| 2077 |
ESL_ALLOC(sq, sizeof(ESL_SQ *) * msa_nseq); |
| 2078 |
ESL_ALLOC(tr, (sizeof(Parsetree_t *) * msa_nseq)); |
| 2079 |
ESL_ALLOC(wgt,(sizeof(float) * msa_nseq)); |
| 2080 |
esl_vec_FSet(wgt, msa_nseq, 1.0); |
| 2081 |
namelen = 3 + IntMaxDigits() + 1; |
| 2082 |
ESL_ALLOC(tmp_name, sizeof(char) * namelen); |
| 2083 |
|
| 2084 |
while(nsampled < nsamples) |
| 2085 |
{ |
| 2086 |
if(nsampled != 0) { |
| 2087 |
|
| 2088 |
free(matassign); |
| 2089 |
free(useme); |
| 2090 |
for (i = 0; i < msa_nseq; i++) { |
| 2091 |
CP9FreeTrace(cp9_tr[i]); |
| 2092 |
FreeParsetree(tr[i]); |
| 2093 |
esl_sq_Destroy(sq[i]); |
| 2094 |
} |
| 2095 |
free(cp9_tr); |
| 2096 |
esl_msa_Destroy(msa); |
| 2097 |
} |
| 2098 |
|
| 2099 |
if(nsampled + msa_nseq > nsamples) |
| 2100 |
msa_nseq = nsamples - nsampled; |
| 2101 |
for (i = 0; i < msa_nseq; i++) { |
| 2102 |
ESL_ALLOC(name, sizeof(char) * namelen); |
| 2103 |
snprintf(name, namelen, "seq%d", i+1); |
| 2104 |
if((status = EmitParsetree(cm, errbuf, r, name, TRUE, &(tr[i]), &(sq[i]), &L)) != eslOK) return status; |
| 2105 |
free(name); |
| 2106 |
} |
| 2107 |
|
| 2108 |
Parsetrees2Alignment(cm, errbuf, cm->abc, sq, NULL, tr, NULL, msa_nseq, NULL, NULL, |
| 2109 |
TRUE, |
| 2110 |
FALSE, |
| 2111 |
FALSE, |
| 2112 |
&msa); |
| 2113 |
|
| 2114 |
if(msa->flags & eslMSA_DIGITAL) ESL_FAIL(eslEINCOMPAT, errbuf, "CP9_check_by_sampling(): sampled MSA should NOT be digitized.\n"); |
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
ESL_ALLOC(useme, sizeof(int) * (msa->alen+1)); |
| 2119 |
for (apos = 0, cc = 0; apos < msa->alen; apos++) { |
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
useme[apos] = (cc < (spos-1) || cc > epos) ? 0 : 1; |
| 2125 |
if (!esl_abc_CIsGap(cm->abc, msa->rf[apos])) { |
| 2126 |
cc++; |
| 2127 |
if(cc == (epos+1)) useme[apos] = 0; |
| 2128 |
|
| 2129 |
} |
| 2130 |
} |
| 2131 |
if((status = esl_msa_ColumnSubset(msa, errbuf, useme)) != eslOK) return status; |
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
ESL_ALLOC(matassign, sizeof(int) * (msa->alen+1)); |
| 2136 |
matassign[0] = 0; |
| 2137 |
for (apos = 0; apos < msa->alen; apos++) { |
| 2138 |
matassign[apos+1] = 0; |
| 2139 |
if (!esl_abc_CIsGap(cm->abc, msa->rf[apos])) |
| 2140 |
matassign[apos+1] = 1; |
| 2141 |
} |
| 2142 |
|
| 2143 |
if((status = esl_msa_Digitize(cm->abc, msa, NULL)) == eslEINVAL) ESL_FAIL(status, errbuf, "CP9_check_by_sampling(): esl_msa_Digitize() returned eslEINVAL, some characters must be invalid in msa."); |
| 2144 |
CP9_fake_tracebacks(msa, matassign, &cp9_tr); |
| 2145 |
|
| 2146 |
|
| 2147 |
for (i = 0; i < msa->nseq; i++) |
| 2148 |
CP9TraceCount(shmm, sq[i]->dsq, wgt[i], cp9_tr[i]); |
| 2149 |
|
| 2150 |
nsampled += msa_nseq; |
| 2151 |
} |
| 2152 |
|
| 2153 |
|
| 2154 |
free(matassign); |
| 2155 |
free(useme); |
| 2156 |
free(tmp_name); |
| 2157 |
for (i = 0; i < msa_nseq; i++) { |
| 2158 |
CP9FreeTrace(cp9_tr[i]); |
| 2159 |
FreeParsetree(tr[i]); |
| 2160 |
esl_sq_Destroy(sq[i]); |
| 2161 |
} |
| 2162 |
esl_msa_Destroy(msa); |
| 2163 |
free(cp9_tr); |
| 2164 |
free(tr); |
| 2165 |
free(sq); |
| 2166 |
free(wgt); |
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
if(print_flag) |
| 2176 |
{ |
| 2177 |
printf("PRINTING BUILT HMM PARAMS:\n"); |
| 2178 |
debug_print_cp9_params(stdout, hmm, TRUE); |
| 2179 |
printf("DONE PRINTING BUILT HMM PARAMS:\n"); |
| 2180 |
|
| 2181 |
printf("PRINTING SAMPLED HMM PARAMS:\n"); |
| 2182 |
debug_print_cp9_params(stdout, shmm, TRUE); |
| 2183 |
printf("DONE PRINTING SAMPLED HMM PARAMS:\n"); |
| 2184 |
} |
| 2185 |
for(nd = 0; nd <= shmm->M; nd++) { |
| 2186 |
if(print_flag) printf("nd:%d\n", nd); |
| 2187 |
if(!(CP9_node_chi_squared(hmm, shmm, nd, chi_thresh, print_flag))) { |
| 2188 |
if(subinfo == NULL) { |
| 2189 |
v_ct++; |
| 2190 |
printf("SAMPLING VIOLATION[%3d]: TRUE | spos: %3d | epos: %3d\n", nd, spos, epos); |
| 2191 |
} |
| 2192 |
else if(subinfo != NULL && subinfo->imp_cc[nd] == 0) { |
| 2193 |
v_ct++; |
| 2194 |
printf("SAMPLING VIOLATION[%3d]: TRUE | spos: %3d | epos: %3d | subinfo->imp_cc: %d\n", nd, spos, epos, subinfo->imp_cc[nd]); |
| 2195 |
} |
| 2196 |
else if(subinfo != NULL && subinfo->imp_cc[nd] != 0) { |
| 2197 |
spredict_total_ct++; |
| 2198 |
subinfo->spredict_ct[subinfo->imp_cc[nd]]++; |
| 2199 |
if(print_flag) printf("PREDICTED SAMPLING VIOLATION[%3d]: TRUE | spos: %3d | epos: %3d | subinfo->imp_cc: %d (spredict_total_ct: %d)\n", nd, spos, epos, subinfo->imp_cc[nd], spredict_total_ct); |
| 2200 |
} |
| 2201 |
} |
| 2202 |
else if(subinfo != NULL && subinfo->imp_cc[nd] != 0) { |
| 2203 |
|
| 2204 |
spredict_total_ct++; |
| 2205 |
subinfo->spredict_ct[subinfo->imp_cc[nd]]++; |
| 2206 |
swrong_total_ct++; |
| 2207 |
subinfo->swrong_ct[subinfo->imp_cc[nd]]++; |
| 2208 |
if(print_flag) printf("NON-VIOLATION[%3d] %3d : spos: %3d | epos: %3d | non-subinfo->imp_cc: %d\n", nd, swrong_total_ct, spos, epos, subinfo->imp_cc[nd]); |
| 2209 |
} |
| 2210 |
} |
| 2211 |
|
| 2212 |
|
| 2213 |
CPlan9Renormalize(shmm); |
| 2214 |
CP9Logoddsify(shmm); |
| 2215 |
|
| 2216 |
if(print_flag) |
| 2217 |
{ |
| 2218 |
printf("PRINTING BUILT HMM PARAMS:\n"); |
| 2219 |
debug_print_cp9_params(stdout, hmm, TRUE); |
| 2220 |
printf("DONE PRINTING BUILT HMM PARAMS:\n"); |
| 2221 |
|
| 2222 |
|
| 2223 |
printf("PRINTING SAMPLED HMM PARAMS:\n"); |
| 2224 |
debug_print_cp9_params(stdout, shmm, TRUE); |
| 2225 |
printf("DONE PRINTING SAMPLED HMM PARAMS:\n"); |
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
} |
| 2230 |
FreeCPlan9(shmm); |
| 2231 |
|
| 2232 |
if(v_ct > 0) ESL_FAIL(eslFAIL, errbuf, "CP9_check_by_sampling(): check failed"); |
| 2233 |
return eslOK; |
| 2234 |
|
| 2235 |
ERROR: |
| 2236 |
ESL_FAIL(status, errbuf, "memory allocation error"); |
| 2237 |
return FALSE; |
| 2238 |
} |
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
int |
| 2261 |
CP9_node_chi_squared(CP9_t *ahmm, CP9_t *shmm, int nd, float threshold, int print_flag) |
| 2262 |
{ |
| 2263 |
int status; |
| 2264 |
double p; |
| 2265 |
int x; |
| 2266 |
float m_nseq, i_nseq, d_nseq; |
| 2267 |
float check_m_nseq, check_i_nseq; |
| 2268 |
float *temp_ahmm_trans; |
| 2269 |
float *temp_shmm_trans; |
| 2270 |
int ret_val; |
| 2271 |
|
| 2272 |
ret_val = TRUE; |
| 2273 |
|
| 2274 |
if(nd > shmm->M || nd > ahmm->M) |
| 2275 |
cm_Fail("ERROR CP9_node_chi_squared() is being grossly misused.\n"); |
| 2276 |
|
| 2277 |
CPlan9Renormalize(ahmm); |
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
CP9Logoddsify(ahmm); |
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
if(nd == 0) |
| 2312 |
m_nseq = shmm->begin[1]; |
| 2313 |
else if(nd == shmm->M) |
| 2314 |
m_nseq = shmm->end[nd]; |
| 2315 |
else |
| 2316 |
m_nseq = shmm->t[nd][CTMM]; |
| 2317 |
m_nseq += shmm->t[nd][CTMI]; |
| 2318 |
m_nseq += shmm->t[nd][CTMD]; |
| 2319 |
|
| 2320 |
i_nseq = shmm->t[nd][CTIM]; |
| 2321 |
i_nseq += shmm->t[nd][CTII]; |
| 2322 |
i_nseq += shmm->t[nd][CTID]; |
| 2323 |
|
| 2324 |
if(nd != 0) |
| 2325 |
{ |
| 2326 |
d_nseq = shmm->t[nd][CTDM]; |
| 2327 |
d_nseq += shmm->t[nd][CTDI]; |
| 2328 |
d_nseq += shmm->t[nd][CTDD]; |
| 2329 |
} |
| 2330 |
|
| 2331 |
if(nd != 0) { |
| 2332 |
check_m_nseq = 0.; |
| 2333 |
for (x = 0; x < ahmm->abc->K; x++) check_m_nseq += shmm->mat[nd][x]; |
| 2334 |
if(fabs(check_m_nseq - m_nseq) > 0.0001) cm_Fail("ERROR: node: %d has different number of sampled match emissions and transitions.\n"); |
| 2335 |
} |
| 2336 |
check_i_nseq = 0.; |
| 2337 |
for (x = 0; x < ahmm->abc->K; x++) check_i_nseq += shmm->ins[nd][x]; |
| 2338 |
if((check_i_nseq >= i_nseq && ((check_i_nseq - i_nseq) > 0.0001)) || |
| 2339 |
(check_i_nseq < i_nseq && ((i_nseq - check_i_nseq) > 0.0001))) |
| 2340 |
cm_Fail("ERROR: node: %d has different number of sampled insert emissions and transitions.\n"); |
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
if(nd != 0) { |
| 2346 |
esl_vec_FScale(ahmm->mat[nd], ahmm->abc->K, esl_vec_FSum(shmm->mat[nd], ahmm->abc->K)); |
| 2347 |
p = FChiSquareFit(ahmm->mat[nd], shmm->mat[nd], ahmm->abc->K); |
| 2348 |
if (p < threshold) { |
| 2349 |
printf("Rejected match emission distribution for CP9 node %d: chi-squared p = %f\n", nd, p); |
| 2350 |
ret_val = FALSE; |
| 2351 |
} |
| 2352 |
} |
| 2353 |
|
| 2354 |
esl_vec_FScale(ahmm->ins[nd], ahmm->abc->K, esl_vec_FSum(shmm->ins[nd], ahmm->abc->K)); |
| 2355 |
p = FChiSquareFit(ahmm->ins[nd], shmm->ins[nd], ahmm->abc->K); |
| 2356 |
if (p < threshold) { |
| 2357 |
printf("Rejected insert emission distribution for CP9 node %d: chi-squared p = %f\n", nd, p); |
| 2358 |
ret_val = FALSE; |
| 2359 |
} |
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
ESL_ALLOC(temp_ahmm_trans, sizeof(float) * cp9_NTRANS); |
| 2365 |
ESL_ALLOC(temp_shmm_trans, sizeof(float) * cp9_NTRANS); |
| 2366 |
if(nd == 0 || nd == shmm->M) { |
| 2367 |
if(nd == 0) { |
| 2368 |
temp_ahmm_trans[0] = ahmm->begin[1]; |
| 2369 |
temp_shmm_trans[0] = shmm->begin[1]; |
| 2370 |
for(x = 1; x < cp9_TRANS_NMATCH; x++) { |
| 2371 |
temp_ahmm_trans[x] = ahmm->t[0][x]; |
| 2372 |
temp_shmm_trans[x] = shmm->t[0][x]; |
| 2373 |
} |
| 2374 |
} |
| 2375 |
if(nd == shmm->M) { |
| 2376 |
temp_ahmm_trans[0] = ahmm->end[ahmm->M]; |
| 2377 |
temp_shmm_trans[0] = shmm->end[shmm->M]; |
| 2378 |
for(x = 1; x < cp9_TRANS_NMATCH; x++) { |
| 2379 |
temp_ahmm_trans[x] = ahmm->t[ahmm->M][x]; |
| 2380 |
temp_shmm_trans[x] = shmm->t[shmm->M][x]; |
| 2381 |
} |
| 2382 |
} |
| 2383 |
esl_vec_FScale(temp_ahmm_trans, cp9_TRANS_NMATCH, esl_vec_FSum(temp_shmm_trans, cp9_TRANS_NMATCH)); |
| 2384 |
p = FChiSquareFit(temp_ahmm_trans, temp_shmm_trans, cp9_TRANS_NMATCH); |
| 2385 |
if (p < threshold) { |
| 2386 |
printf("Rejected match transition distribution for CP9 node %d: chi-squared p = %f\n", nd, p); |
| 2387 |
ret_val = FALSE; |
| 2388 |
} |
| 2389 |
} |
| 2390 |
else { |
| 2391 |
esl_vec_FScale(ahmm->t[nd], cp9_TRANS_NMATCH, esl_vec_FSum(shmm->t[nd], cp9_TRANS_NMATCH)); |
| 2392 |
p = FChiSquareFit(ahmm->t[nd], shmm->t[nd], cp9_TRANS_NMATCH); |
| 2393 |
if (p < threshold) { |
| 2394 |
printf("Rejected match transition distribution for CP9 node %d: chi-squared p = %f\n", nd, p); |
| 2395 |
ret_val = FALSE; |
| 2396 |
} |
| 2397 |
} |
| 2398 |
|
| 2399 |
esl_vec_FScale (ahmm->t[nd] + cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT, esl_vec_FSum(shmm->t[nd]+cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT)); |
| 2400 |
p = FChiSquareFit(ahmm->t[nd] + cp9_TRANS_INSERT_OFFSET, shmm->t[nd] + cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT); |
| 2401 |
if (p < threshold) { |
| 2402 |
printf("Rejected insert transition distribution for CP9 node %d: chi-squared p = %f\n", nd, p); |
| 2403 |
ret_val = FALSE; |
| 2404 |
} |
| 2405 |
|
| 2406 |
|
| 2407 |
if(nd != 0) { |
| 2408 |
esl_vec_FScale (ahmm->t[nd] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE, esl_vec_FSum(shmm->t[nd] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE)); |
| 2409 |
p = FChiSquareFit(ahmm->t[nd] + cp9_TRANS_DELETE_OFFSET, shmm->t[nd] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE); |
| 2410 |
if (p < threshold) { |
| 2411 |
printf("Rejected delete transition distribution for CP9 node %d: chi-squared p = %f\n", nd, p); |
| 2412 |
ret_val = FALSE; |
| 2413 |
} |
| 2414 |
} |
| 2415 |
else if(print_flag) printf("d_nseq: %.3f", d_nseq); |
| 2416 |
|
| 2417 |
free(temp_ahmm_trans); |
| 2418 |
free(temp_shmm_trans); |
| 2419 |
|
| 2420 |
|
| 2421 |
CPlan9Renormalize(ahmm); |
| 2422 |
|
| 2423 |
return ret_val; |
| 2424 |
|
| 2425 |
ERROR: |
| 2426 |
cm_Fail("Memory allocation error."); |
| 2427 |
return 0.; |
| 2428 |
} |
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
void |
| 2445 |
debug_print_phi_cp9(CP9_t *hmm, double **phi) |
| 2446 |
{ |
| 2447 |
int k; |
| 2448 |
|
| 2449 |
for(k = 0; k <= hmm->M; k++) |
| 2450 |
{ |
| 2451 |
printf("phi[%4d][M]: %f\n", k, phi[k][0]); |
| 2452 |
printf("phi[%4d][I]: %f\n", k, phi[k][1]); |
| 2453 |
if(k != 0) |
| 2454 |
printf("phi[%4d][D]: %f\n\n", k, phi[k][2]); |
| 2455 |
else |
| 2456 |
printf("\n"); |
| 2457 |
} |
| 2458 |
return; |
| 2459 |
} |
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
static float |
| 2468 |
FChiSquareFit(float *f1, float *f2, int N) |
| 2469 |
{ |
| 2470 |
int i; |
| 2471 |
float diff; |
| 2472 |
float chisq = 0.0; |
| 2473 |
int n; |
| 2474 |
double qax; |
| 2475 |
|
| 2476 |
n = 0; |
| 2477 |
for (i = 0; i < N; i++) |
| 2478 |
{ |
| 2479 |
if (f1[i] == 0. && f2[i] == 0.) continue; |
| 2480 |
diff = f1[i] - f2[i]; |
| 2481 |
chisq += diff * diff / (f1[i]+f2[i]); |
| 2482 |
n++; |
| 2483 |
} |
| 2484 |
|
| 2485 |
if (n > 1) |
| 2486 |
{ |
| 2487 |
if(esl_stats_IncompleteGamma(((float) n-1.)/2., chisq/2., NULL, &qax) != eslOK) |
| 2488 |
cm_Fail("ERROR in FChiSquareFit() call to esl_stats_IncompleteGamma()"); |
| 2489 |
return (float) qax; |
| 2490 |
} |
| 2491 |
else |
| 2492 |
return -1.; |
| 2493 |
} |
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
|
| 2506 |
|
| 2507 |
|
| 2508 |
static int |
| 2509 |
check_cm_adj_bp(CM_t *cm, CP9Map_t *cp9map) |
| 2510 |
{ |
| 2511 |
int k, prev_k; |
| 2512 |
prev_k = cp9map->pos2nd[1]; |
| 2513 |
for(k = 2; k <= cp9map->hmm_M; k++) |
| 2514 |
{ |
| 2515 |
if(cp9map->pos2nd[k] == prev_k) |
| 2516 |
return TRUE; |
| 2517 |
prev_k = cp9map->pos2nd[k]; |
| 2518 |
} |
| 2519 |
return FALSE; |
| 2520 |
} |
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
|
| 2534 |
|
| 2535 |
|
| 2536 |
|
| 2537 |
|
| 2538 |
|
| 2539 |
|
| 2540 |
int |
| 2541 |
MakeDealignedString(const ESL_ALPHABET *abc, char *aseq, int alen, char *ss, char **ret_s) |
| 2542 |
{ |
| 2543 |
int status; |
| 2544 |
char *new; |
| 2545 |
int apos, rpos; |
| 2546 |
|
| 2547 |
ESL_ALLOC(new, (alen+1) * sizeof(char)); |
| 2548 |
for (apos = rpos = 0; apos < alen; apos++) |
| 2549 |
if (! esl_abc_CIsGap(abc, aseq[apos])) |
| 2550 |
{ |
| 2551 |
new[rpos] = ss[apos]; |
| 2552 |
rpos++; |
| 2553 |
} |
| 2554 |
new[rpos] = '\0'; |
| 2555 |
if (alen != strlen(ss)) |
| 2556 |
{ cm_Fail("ERROR dealigning sequence."); } |
| 2557 |
*ret_s = new; |
| 2558 |
return eslOK; |
| 2559 |
|
| 2560 |
ERROR: |
| 2561 |
cm_Fail("Memory allocation error."); |
| 2562 |
return 0; |
| 2563 |
} |
| 2564 |
|
| 2565 |
|
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
|
| 2571 |
|
| 2572 |
|
| 2573 |
|
| 2574 |
|
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
int |
| 2596 |
sub_build_cp9_hmm_from_mother(CM_t *cm, char *errbuf, CM_t *mother_cm, CMSubMap_t *mother_map, CP9_t **ret_hmm, CP9Map_t **ret_cp9map, int do_psi_test, |
| 2597 |
float psi_vs_phi_threshold, int debug_level) |
| 2598 |
{ |
| 2599 |
int status; |
| 2600 |
int k; |
| 2601 |
int mk,x; |
| 2602 |
double *psi; |
| 2603 |
double **phi; |
| 2604 |
CP9Map_t *cp9map; |
| 2605 |
CP9_t *hmm; |
| 2606 |
float d; |
| 2607 |
char ***tmap = NULL; |
| 2608 |
|
| 2609 |
|
| 2610 |
if(cm->flags & CMH_LOCAL_BEGIN) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), CMH_LOCAL_BEGIN flag is up"); |
| 2611 |
if(cm->flags & CMH_LOCAL_END) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), CMH_LOCAL_END flag is up"); |
| 2612 |
if(mother_cm->cp9->flags & CPLAN9_EL) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), mother_cm's cp9 has EL local ends on"); |
| 2613 |
if(!(mother_cm->cp9->flags & CPLAN9_LOCAL_BEGIN)) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), mother_cm's cp9 does not have local begins on"); |
| 2614 |
if(!(mother_cm->cp9->flags & CPLAN9_LOCAL_END)) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), mother_cm's cp9 does not have local ends on."); |
| 2615 |
|
| 2616 |
|
| 2617 |
cp9map = AllocCP9Map(cm); |
| 2618 |
|
| 2619 |
CP9_map_cm2hmm(cm, cp9map, debug_level); |
| 2620 |
|
| 2621 |
hmm = AllocCPlan9(cp9map->hmm_M, cm->abc); |
| 2622 |
ZeroCPlan9(hmm); |
| 2623 |
CPlan9SetNullModel(hmm, cm->null, 1.0); |
| 2624 |
CPlan9InitEL(hmm, cm); |
| 2625 |
|
| 2626 |
hmm->null2_omega = cm->null2_omega; |
| 2627 |
hmm->null3_omega = cm->null3_omega; |
| 2628 |
|
| 2629 |
|
| 2630 |
if((psi = cm_ExpectedStateOccupancy(cm)) == NULL) goto ERROR; |
| 2631 |
tmap = cm_CreateTransitionMap(); |
| 2632 |
|
| 2633 |
|
| 2634 |
cm2hmm_special_trans_cp9(cm, hmm, cp9map, psi, tmap); |
| 2635 |
|
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
d = hmm->begin[1] + hmm->t[0][CTMI] + hmm->t[0][CTMD] + hmm->t[0][CTMEL]; |
| 2640 |
hmm->begin[1] /= d; |
| 2641 |
hmm->t[0][CTMI] /= d; |
| 2642 |
hmm->t[0][CTMD] /= d; |
| 2643 |
hmm->t[0][CTMEL] /= d; |
| 2644 |
|
| 2645 |
esl_vec_FNorm(hmm->t[0] + cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT); |
| 2646 |
|
| 2647 |
esl_vec_FSet (hmm->t[0] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE, 0.); |
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
d = esl_vec_FSum(hmm->t[hmm->M], cp9_TRANS_NMATCH) + hmm->end[hmm->M]; |
| 2652 |
esl_vec_FScale(hmm->t[hmm->M], cp9_TRANS_NMATCH, 1./d); |
| 2653 |
hmm->end[hmm->M] /= d; |
| 2654 |
|
| 2655 |
esl_vec_FNorm(hmm->t[hmm->M] + cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT); |
| 2656 |
|
| 2657 |
esl_vec_FNorm(hmm->t[hmm->M] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE); |
| 2658 |
|
| 2659 |
|
| 2660 |
for(x = 0; x < cp9_NTRANS; x++) hmm->tsc[x][0] = Prob2Score(hmm->t[0][x], 1.0); |
| 2661 |
for(x = 0; x < cp9_NTRANS; x++) hmm->tsc[x][hmm->M] = Prob2Score(hmm->t[hmm->M][x], 1.0); |
| 2662 |
hmm->bsc[1] = Prob2Score(hmm->begin[1], 1.0); |
| 2663 |
hmm->esc[hmm->M] = Prob2Score(hmm->end[hmm->M], 1.0); |
| 2664 |
|
| 2665 |
|
| 2666 |
for(k = 0; k <= (mother_map->epos-mother_map->spos+1); k++) { |
| 2667 |
mk = k + mother_map->spos - 1; |
| 2668 |
if(k > 0) { |
| 2669 |
esl_vec_FCopy(mother_cm->cp9->mat[mk], mother_cm->cp9->abc->K, hmm->mat[k]); |
| 2670 |
for(x = 0; x < mother_cm->cp9->abc->Kp; x++) { |
| 2671 |
hmm->msc[x][k] = mother_cm->cp9->msc[x][mk]; |
| 2672 |
} |
| 2673 |
} |
| 2674 |
esl_vec_FCopy(mother_cm->cp9->ins[mk], mother_cm->cp9->abc->K, hmm->ins[k]); |
| 2675 |
for(x = 0; x < mother_cm->cp9->abc->Kp; x++) { |
| 2676 |
hmm->isc[x][k] = mother_cm->cp9->isc[x][mk]; |
| 2677 |
} |
| 2678 |
|
| 2679 |
|
| 2680 |
if(k > 1) { |
| 2681 |
hmm->begin[k] = 0.; |
| 2682 |
hmm->bsc[k] = -INFTY; |
| 2683 |
} |
| 2684 |
if(k > 0 && k < hmm->M) { |
| 2685 |
|
| 2686 |
|
| 2687 |
for(x = cp9_TRANS_MATCH_OFFSET; x < cp9_TRANS_NMATCH; x++) { |
| 2688 |
hmm->t[k][x] = mother_cm->cp9->t[mk][x] / (1. - mother_cm->cp9->end[mk]); |
| 2689 |
hmm->tsc[x][k] = Prob2Score(hmm->t[k][x], 1.0); |
| 2690 |
} |
| 2691 |
|
| 2692 |
|
| 2693 |
esl_vec_FCopy(mother_cm->cp9->t[mk] + cp9_TRANS_INSERT_OFFSET, cp9_NTRANS - cp9_TRANS_INSERT_OFFSET, hmm->t[k] + cp9_TRANS_INSERT_OFFSET); |
| 2694 |
for(x = cp9_TRANS_INSERT_OFFSET; x < cp9_NTRANS; x++) { |
| 2695 |
hmm->tsc[x][k] = mother_cm->cp9->tsc[x][mk]; |
| 2696 |
} |
| 2697 |
hmm->end[k] = 0.; |
| 2698 |
hmm->esc[k] = -INFTY; |
| 2699 |
} |
| 2700 |
} |
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
for (k = 0 ; k <= hmm->M; k++) { |
| 2705 |
int *otsc_k = hmm->otsc + k*cp9O_NTRANS; |
| 2706 |
otsc_k[cp9O_MM] = hmm->tsc[CTMM][k]; |
| 2707 |
otsc_k[cp9O_MI] = hmm->tsc[CTMI][k]; |
| 2708 |
otsc_k[cp9O_MD] = hmm->tsc[CTMD][k]; |
| 2709 |
otsc_k[cp9O_IM] = hmm->tsc[CTIM][k]; |
| 2710 |
otsc_k[cp9O_II] = hmm->tsc[CTII][k]; |
| 2711 |
otsc_k[cp9O_DM] = hmm->tsc[CTDM][k]; |
| 2712 |
otsc_k[cp9O_DD] = hmm->tsc[CTDD][k]; |
| 2713 |
otsc_k[cp9O_ID] = hmm->tsc[CTID][k]; |
| 2714 |
otsc_k[cp9O_DI] = hmm->tsc[CTDI][k]; |
| 2715 |
otsc_k[cp9O_BM] = hmm->bsc[k]; |
| 2716 |
otsc_k[cp9O_MEL]= hmm->tsc[CTMEL][k]; |
| 2717 |
otsc_k[cp9O_ME] = hmm->esc[k]; |
| 2718 |
} |
| 2719 |
|
| 2720 |
hmm->flags |= CPLAN9_HASBITS; |
| 2721 |
|
| 2722 |
if(debug_level > 1) debug_print_cp9_params(stdout, hmm, TRUE); |
| 2723 |
if(do_psi_test) { |
| 2724 |
|
| 2725 |
|
| 2726 |
|
| 2727 |
|
| 2728 |
|
| 2729 |
|
| 2730 |
fill_phi_cp9(hmm, &phi, 1, FALSE); |
| 2731 |
if((status = check_psi_vs_phi_cp9(cm, errbuf, cp9map, psi, phi, (double) psi_vs_phi_threshold, debug_level)) != eslOK) return status; |
| 2732 |
for(k = 0; k <= hmm->M; k++) free(phi[k]); |
| 2733 |
free(phi); |
| 2734 |
} |
| 2735 |
|
| 2736 |
*ret_hmm = hmm; |
| 2737 |
*ret_cp9map = cp9map; |
| 2738 |
|
| 2739 |
cm_FreeTransitionMap(tmap); |
| 2740 |
free(psi); |
| 2741 |
|
| 2742 |
return eslOK; |
| 2743 |
|
| 2744 |
ERROR: |
| 2745 |
ESL_FAIL(eslEMEM, errbuf, "memory allocation error."); |
| 2746 |
return 0; |
| 2747 |
} |
| 2748 |
|
| 2749 |
|
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
void |
| 2763 |
CPlan9InitEL(CP9_t *cp9, CM_t *cm) |
| 2764 |
{ |
| 2765 |
int status; |
| 2766 |
int k; |
| 2767 |
int nd; |
| 2768 |
int *tmp_el_from_ct; |
| 2769 |
|
| 2770 |
if(cm->emap == NULL) cm_Fail("CPlan9InitEL() cm->emap is NULL"); |
| 2771 |
|
| 2772 |
|
| 2773 |
cp9->el_self = sreEXP2(cm->el_selfsc); |
| 2774 |
cp9->el_selfsc = Prob2Score(cp9->el_self, 1.0); |
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
for(k = 0; k <= cp9->M; k++) |
| 2786 |
{ |
| 2787 |
cp9->el_from_ct[k] = 0; |
| 2788 |
cp9->has_el[k] = FALSE; |
| 2789 |
} |
| 2790 |
cp9->el_from_ct[(cp9->M+1)] = 0; |
| 2791 |
|
| 2792 |
|
| 2793 |
for(nd = 0; nd < cm->nodes; nd++) |
| 2794 |
{ |
| 2795 |
if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd || |
| 2796 |
cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BEGL_nd || |
| 2797 |
cm->ndtype[nd] == BEGR_nd) && |
| 2798 |
cm->ndtype[nd+1] != END_nd) |
| 2799 |
{ |
| 2800 |
|
| 2801 |
cp9->el_from_ct[cm->emap->rpos[nd]]++; |
| 2802 |
cp9->has_el[cm->emap->lpos[nd]] = TRUE; |
| 2803 |
} |
| 2804 |
} |
| 2805 |
|
| 2806 |
|
| 2807 |
for(k = 0; k <= (cp9->M+1); k++) |
| 2808 |
{ |
| 2809 |
if(cp9->el_from_idx[k] != NULL) |
| 2810 |
cm_Fail("ERROR in CPlan9InitEL() el_from_idx has already been initialized\n"); |
| 2811 |
if(cp9->el_from_ct[k] > 0) |
| 2812 |
{ |
| 2813 |
ESL_ALLOC(cp9->el_from_idx[k], sizeof(int) * cp9->el_from_ct[k]); |
| 2814 |
ESL_ALLOC(cp9->el_from_cmnd[k],sizeof(int) * cp9->el_from_ct[k]); |
| 2815 |
} |
| 2816 |
|
| 2817 |
} |
| 2818 |
|
| 2819 |
|
| 2820 |
ESL_ALLOC(tmp_el_from_ct, sizeof(int) * (cp9->M+2)); |
| 2821 |
for(k = 0; k <= (cp9->M+1); k++) |
| 2822 |
tmp_el_from_ct[k] = 0; |
| 2823 |
for(nd = 0; nd < cm->nodes; nd++) |
| 2824 |
{ |
| 2825 |
if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd || |
| 2826 |
cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BEGL_nd || |
| 2827 |
cm->ndtype[nd] == BEGR_nd) && |
| 2828 |
cm->ndtype[nd+1] != END_nd) |
| 2829 |
{ |
| 2830 |
k = cm->emap->rpos[nd]; |
| 2831 |
cp9->el_from_idx[k][tmp_el_from_ct[k]] = cm->emap->lpos[nd]; |
| 2832 |
cp9->el_from_cmnd[k][tmp_el_from_ct[k]] = nd; |
| 2833 |
tmp_el_from_ct[k]++; |
| 2834 |
} |
| 2835 |
} |
| 2836 |
|
| 2837 |
|
| 2838 |
|
| 2839 |
|
| 2840 |
|
| 2841 |
|
| 2842 |
|
| 2843 |
|
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
free(tmp_el_from_ct); |
| 2848 |
return; |
| 2849 |
|
| 2850 |
ERROR: |
| 2851 |
cm_Fail("Memory allocation error."); |
| 2852 |
} |