| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <esl_config.h> |
| 12 |
#include <p7_config.h> |
| 13 |
#include "config.h" |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <string.h> |
| 17 |
#include <ctype.h> |
| 18 |
#include <time.h> |
| 19 |
#include <math.h> |
| 20 |
|
| 21 |
#include "easel.h" |
| 22 |
#include "esl_msa.h" |
| 23 |
#include "esl_vectorops.h" |
| 24 |
|
| 25 |
#include "hmmer.h" |
| 26 |
|
| 27 |
#include "infernal.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
void |
| 34 |
CP9AllocTrace(int tlen, CP9trace_t **ret_tr) |
| 35 |
{ |
| 36 |
int status; |
| 37 |
CP9trace_t *tr; |
| 38 |
|
| 39 |
ESL_ALLOC(tr, sizeof(CP9trace_t)); |
| 40 |
ESL_ALLOC(tr->statetype, sizeof(char) * tlen); |
| 41 |
ESL_ALLOC(tr->nodeidx, sizeof(int) * tlen); |
| 42 |
ESL_ALLOC(tr->pos, sizeof(int) * tlen); |
| 43 |
*ret_tr = tr; |
| 44 |
return; |
| 45 |
|
| 46 |
ERROR: |
| 47 |
cm_Fail("Memory allocation error."); |
| 48 |
} |
| 49 |
void |
| 50 |
CP9ReallocTrace(CP9trace_t *tr, int tlen) |
| 51 |
{ |
| 52 |
int status; |
| 53 |
void *tmp; |
| 54 |
|
| 55 |
ESL_RALLOC(tr->statetype, tmp, tlen * sizeof(char)); |
| 56 |
ESL_RALLOC(tr->nodeidx, tmp, tlen * sizeof(int)); |
| 57 |
ESL_RALLOC(tr->pos, tmp, tlen * sizeof(int)); |
| 58 |
return; |
| 59 |
|
| 60 |
ERROR: |
| 61 |
cm_Fail("Memory reallocation error."); |
| 62 |
} |
| 63 |
void |
| 64 |
CP9FreeTrace(CP9trace_t *tr) |
| 65 |
{ |
| 66 |
if (tr == NULL) return; |
| 67 |
free(tr->pos); |
| 68 |
free(tr->nodeidx); |
| 69 |
free(tr->statetype); |
| 70 |
free(tr); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void |
| 87 |
CP9_fake_tracebacks(ESL_MSA *msa, int *matassign, CP9trace_t ***ret_tr) |
| 88 |
{ |
| 89 |
if(! (msa->flags & eslMSA_DIGITAL)) |
| 90 |
cm_Fail("ERROR in CP9_fake_tracebacks(), msa should be digitized.\n"); |
| 91 |
|
| 92 |
int status; |
| 93 |
CP9trace_t **tr; |
| 94 |
int idx; |
| 95 |
int i; |
| 96 |
int k; |
| 97 |
int apos; |
| 98 |
int tpos; |
| 99 |
int first_match; |
| 100 |
|
| 101 |
|
| 102 |
ESL_ALLOC(tr, sizeof(CP9trace_t *) * msa->nseq); |
| 103 |
|
| 104 |
first_match = -1; |
| 105 |
|
| 106 |
for (apos = 0; apos < msa->alen; apos++) |
| 107 |
{ |
| 108 |
if(matassign[apos+1] && first_match == -1) first_match = apos; |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
for (idx = 0; idx < msa->nseq; idx++) |
| 113 |
{ |
| 114 |
CP9AllocTrace(msa->alen+2, &tr[idx]); |
| 115 |
|
| 116 |
|
| 117 |
tr[idx]->statetype[0] = CSTB; |
| 118 |
tr[idx]->nodeidx[0] = 0; |
| 119 |
tr[idx]->pos[0] = 0; |
| 120 |
|
| 121 |
i = 1; |
| 122 |
k = 0; |
| 123 |
tpos = 1; |
| 124 |
|
| 125 |
for (apos = 0; apos < msa->alen; apos++) |
| 126 |
{ |
| 127 |
tr[idx]->statetype[tpos] = CSTBOGUS; |
| 128 |
|
| 129 |
if (matassign[apos+1] && !(esl_abc_XIsGap(msa->abc, msa->ax[idx][(apos+1)]))) |
| 130 |
{ |
| 131 |
k++; |
| 132 |
tr[idx]->statetype[tpos] = CSTM; |
| 133 |
tr[idx]->nodeidx[tpos] = k; |
| 134 |
tr[idx]->pos[tpos] = i; |
| 135 |
i++; |
| 136 |
tpos++; |
| 137 |
} |
| 138 |
else if (matassign[apos+1]) |
| 139 |
{ |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
k++; |
| 151 |
tr[idx]->statetype[tpos] = CSTD; |
| 152 |
tr[idx]->nodeidx[tpos] = k; |
| 153 |
tr[idx]->pos[tpos] = 0; |
| 154 |
tpos++; |
| 155 |
} |
| 156 |
else if (! (esl_abc_XIsGap(msa->abc, msa->ax[idx][(apos+1)]))) |
| 157 |
{ |
| 158 |
tr[idx]->statetype[tpos] = CSTI; |
| 159 |
tr[idx]->nodeidx[tpos] = k; |
| 160 |
tr[idx]->pos[tpos] = i; |
| 161 |
i++; |
| 162 |
tpos++; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
tr[idx]->statetype[tpos] = CSTE; |
| 177 |
tr[idx]->nodeidx[tpos] = 0; |
| 178 |
tr[idx]->pos[tpos] = 0; |
| 179 |
tpos++; |
| 180 |
tr[idx]->tlen = tpos; |
| 181 |
} |
| 182 |
|
| 183 |
*ret_tr = tr; |
| 184 |
return; |
| 185 |
|
| 186 |
ERROR: |
| 187 |
cm_Fail("Memory allocation error."); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
void |
| 205 |
CP9TraceCount(CP9_t *hmm, ESL_DSQ *dsq, float wt, CP9trace_t *tr) |
| 206 |
{ |
| 207 |
|
| 208 |
if(dsq == NULL) cm_Fail("ERROR in CP9TraceCount(), dsq is NULL."); |
| 209 |
if(hmm->flags & CPLAN9_EL) cm_Fail("CP9TraceCount(), EL states are on, which this function is not setup for."); |
| 210 |
|
| 211 |
int tpos; |
| 212 |
int i; |
| 213 |
|
| 214 |
for (tpos = 0; tpos < tr->tlen; tpos++) |
| 215 |
{ |
| 216 |
i = tr->pos[tpos]; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if (tr->statetype[tpos] == CSTM) |
| 221 |
esl_abc_FCount(hmm->abc, hmm->mat[tr->nodeidx[tpos]], dsq[i], wt); |
| 222 |
else if (tr->statetype[tpos] == CSTI) |
| 223 |
esl_abc_FCount(hmm->abc, hmm->ins[tr->nodeidx[tpos]], dsq[i], wt); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
switch (tr->statetype[tpos]) { |
| 228 |
case CSTB: |
| 229 |
switch (tr->statetype[tpos+1]) { |
| 230 |
case CSTM: hmm->begin[tr->nodeidx[tpos+1]] += wt; break; |
| 231 |
case CSTI: hmm->t[0][CTMI] += wt; break; |
| 232 |
case CSTD: hmm->t[0][CTMD] += wt; break; |
| 233 |
default: |
| 234 |
cm_Fail("illegal state transition %s->%s in traceback", |
| 235 |
CP9Statetype(tr->statetype[tpos]), |
| 236 |
CP9Statetype(tr->statetype[tpos+1])); |
| 237 |
} |
| 238 |
break; |
| 239 |
case CSTM: |
| 240 |
switch (tr->statetype[tpos+1]) { |
| 241 |
case CSTM: hmm->t[tr->nodeidx[tpos]][CTMM] += wt; break; |
| 242 |
case CSTI: hmm->t[tr->nodeidx[tpos]][CTMI] += wt; break; |
| 243 |
case CSTD: hmm->t[tr->nodeidx[tpos]][CTMD] += wt; break; |
| 244 |
case CSTE: hmm->end[tr->nodeidx[tpos]] += wt; break; |
| 245 |
default: |
| 246 |
cm_Fail("illegal state transition %s->%s in traceback", |
| 247 |
CP9Statetype(tr->statetype[tpos]), |
| 248 |
CP9Statetype(tr->statetype[tpos+1])); |
| 249 |
} |
| 250 |
break; |
| 251 |
case CSTI: |
| 252 |
switch (tr->statetype[tpos+1]) { |
| 253 |
case CSTM: hmm->t[tr->nodeidx[tpos]][CTIM] += wt; break; |
| 254 |
case CSTI: hmm->t[tr->nodeidx[tpos]][CTII] += wt; break; |
| 255 |
case CSTD: hmm->t[tr->nodeidx[tpos]][CTID] += wt; break; |
| 256 |
case CSTE: |
| 257 |
|
| 258 |
if((tpos+1) != (tr->tlen-1)) |
| 259 |
cm_Fail("illegal state transition %s->%s (I is not final insert) in traceback", |
| 260 |
CP9Statetype(tr->statetype[tpos]), |
| 261 |
CP9Statetype(tr->statetype[tpos+1])); |
| 262 |
hmm->t[tr->nodeidx[tpos]][CTIM] += wt; break; |
| 263 |
break; |
| 264 |
default: |
| 265 |
cm_Fail("illegal state transition %s->%s in traceback", |
| 266 |
CP9Statetype(tr->statetype[tpos]), |
| 267 |
CP9Statetype(tr->statetype[tpos+1])); |
| 268 |
} |
| 269 |
break; |
| 270 |
case CSTD: |
| 271 |
switch (tr->statetype[tpos+1]) { |
| 272 |
case CSTM: hmm->t[tr->nodeidx[tpos]][CTDM] += wt; break; |
| 273 |
case CSTI: hmm->t[tr->nodeidx[tpos]][CTDI] += wt; break; |
| 274 |
case CSTD: hmm->t[tr->nodeidx[tpos]][CTDD] += wt; break; |
| 275 |
case CSTE: |
| 276 |
|
| 277 |
if((tpos+1) != (tr->tlen-1)) |
| 278 |
cm_Fail("illegal state transition %s->%s (D is not final delete) in traceback", |
| 279 |
CP9Statetype(tr->statetype[tpos]), |
| 280 |
CP9Statetype(tr->statetype[tpos+1])); |
| 281 |
hmm->t[tr->nodeidx[tpos]][CTDM] += wt; break; |
| 282 |
break; |
| 283 |
default: |
| 284 |
cm_Fail("illegal state transition %s->%s in traceback", |
| 285 |
CP9Statetype(tr->statetype[tpos]), |
| 286 |
CP9Statetype(tr->statetype[tpos+1])); |
| 287 |
} |
| 288 |
break; |
| 289 |
case CSTEL: |
| 290 |
cm_Fail("EL in traceback in CP9TraceCount(), this function is being abused."); |
| 291 |
break; |
| 292 |
case CSTE: |
| 293 |
break; |
| 294 |
|
| 295 |
default: |
| 296 |
cm_Fail("illegal state %s in traceback", |
| 297 |
CP9Statetype(tr->statetype[tpos])); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
float |
| 315 |
CP9TraceScore(CP9_t *hmm, ESL_DSQ *dsq, CP9trace_t *tr) |
| 316 |
{ |
| 317 |
int score; |
| 318 |
int tpos; |
| 319 |
char sym; |
| 320 |
|
| 321 |
|
| 322 |
if(dsq == NULL) |
| 323 |
cm_Fail("ERROR in CP9TraceScore, dsq is NULL."); |
| 324 |
|
| 325 |
|
| 326 |
score = 0; |
| 327 |
for (tpos = 0; tpos < tr->tlen-1; tpos++) |
| 328 |
{ |
| 329 |
sym = dsq[tr->pos[tpos]]; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
if (tr->statetype[tpos] == CSTM) |
| 334 |
score += hmm->msc[(int) sym][tr->nodeidx[tpos]]; |
| 335 |
else if (tr->statetype[tpos] == CSTI) |
| 336 |
score += hmm->isc[(int) sym][tr->nodeidx[tpos]]; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
score += CP9TransitionScoreLookup(hmm, |
| 341 |
tr->statetype[tpos], tr->nodeidx[tpos], |
| 342 |
tr->statetype[tpos+1], tr->nodeidx[tpos+1]); |
| 343 |
} |
| 344 |
return Scorify(score); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
char * |
| 353 |
CP9Statetype(char st) |
| 354 |
{ |
| 355 |
switch (st) { |
| 356 |
case CSTM: return "M"; |
| 357 |
case CSTD: return "D"; |
| 358 |
case CSTI: return "I"; |
| 359 |
case CSTB: return "B"; |
| 360 |
case CSTE: return "E"; |
| 361 |
case CSTEL: return "L"; |
| 362 |
default: return "BOGUS"; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
void |
| 379 |
CP9PrintTrace(FILE *fp, CP9trace_t *tr, CP9_t *hmm, ESL_DSQ *dsq) |
| 380 |
{ |
| 381 |
|
| 382 |
if((dsq != NULL) && (hmm == NULL)) |
| 383 |
cm_Fail("ERROR in CP9PrintTrace, dsq is non-NULL but HMM is NULL.\n"); |
| 384 |
|
| 385 |
int tpos; |
| 386 |
unsigned int sym; |
| 387 |
int sc; |
| 388 |
|
| 389 |
if (tr == NULL) { |
| 390 |
fprintf(fp, " [ trace is NULL ]\n"); |
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
if (hmm == NULL) { |
| 395 |
fprintf(fp, "st node rpos - traceback len %d\n", tr->tlen); |
| 396 |
fprintf(fp, "-- ---- ------\n"); |
| 397 |
for (tpos = 0; tpos < tr->tlen; tpos++) { |
| 398 |
fprintf(fp, "%1s %4d %6d\n", |
| 399 |
CP9Statetype(tr->statetype[tpos]), |
| 400 |
tr->nodeidx[tpos], |
| 401 |
tr->pos[tpos]); |
| 402 |
} |
| 403 |
} else { |
| 404 |
if (!(hmm->flags & CPLAN9_HASBITS)) |
| 405 |
cm_Fail("oi, you can't print scores from that hmm, it's not ready."); |
| 406 |
|
| 407 |
sc = 0; |
| 408 |
fprintf(fp, "st node rpos transit emission - traceback len %d\n", tr->tlen); |
| 409 |
fprintf(fp, "-- ---- ------ ------- --------\n"); |
| 410 |
for (tpos = 0; tpos < tr->tlen; tpos++) { |
| 411 |
if (dsq != NULL) sym = dsq[tr->pos[tpos]]; |
| 412 |
|
| 413 |
fprintf(fp, "%1s %4d %6d %7d", |
| 414 |
CP9Statetype(tr->statetype[tpos]), |
| 415 |
tr->nodeidx[tpos], |
| 416 |
tr->pos[tpos], |
| 417 |
(tpos < tr->tlen-1) ? |
| 418 |
CP9TransitionScoreLookup(hmm, tr->statetype[tpos], tr->nodeidx[tpos], |
| 419 |
tr->statetype[tpos+1], tr->nodeidx[tpos+1]) : 0); |
| 420 |
|
| 421 |
if (tpos < tr->tlen-1) |
| 422 |
sc += CP9TransitionScoreLookup(hmm, tr->statetype[tpos], tr->nodeidx[tpos], |
| 423 |
tr->statetype[tpos+1], tr->nodeidx[tpos+1]); |
| 424 |
|
| 425 |
if (dsq != NULL) { |
| 426 |
if (tr->statetype[tpos] == CSTM) |
| 427 |
{ |
| 428 |
fprintf(fp, " %8d %c", hmm->msc[(int) sym][tr->nodeidx[tpos]], |
| 429 |
hmm->abc->sym[(int) sym]); |
| 430 |
sc += hmm->msc[(int) sym][tr->nodeidx[tpos]]; |
| 431 |
} |
| 432 |
else if (tr->statetype[tpos] == CSTI) |
| 433 |
{ |
| 434 |
fprintf(fp, " %8d %c", hmm->isc[(int) sym][tr->nodeidx[tpos]], |
| 435 |
(char) tolower((int) hmm->abc->sym[(int) sym])); |
| 436 |
sc += hmm->isc[(int) sym][tr->nodeidx[tpos]]; |
| 437 |
} |
| 438 |
else if (tr->statetype[tpos] == CSTEL) |
| 439 |
{ |
| 440 |
if(tr->statetype[tpos-1] == CSTEL) |
| 441 |
{ |
| 442 |
fprintf(fp, " %8d %c", 0, |
| 443 |
(char) tolower((int) hmm->abc->sym[(int) sym])); |
| 444 |
} |
| 445 |
else |
| 446 |
{ |
| 447 |
fprintf(fp, " %8s %c", "-", '-'); |
| 448 |
} |
| 449 |
} |
| 450 |
} else { |
| 451 |
fprintf(fp, " %8s %c", "-", '-'); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
fputs("\n", fp); |
| 456 |
} |
| 457 |
fprintf(fp, " ------- --------\n"); |
| 458 |
fprintf(fp, " total: %6d\n\n", sc); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
int |
| 471 |
CP9TransitionScoreLookup(CP9_t *hmm, char st1, int k1, |
| 472 |
char st2, int k2) |
| 473 |
{ |
| 474 |
switch (st1) { |
| 475 |
case CSTB: |
| 476 |
switch (st2) { |
| 477 |
case CSTM: return hmm->bsc[k2]; |
| 478 |
case CSTI: return hmm->tsc[CTMI][0]; |
| 479 |
case CSTD: return hmm->tsc[CTMD][0]; |
| 480 |
default: cm_Fail("illegal %s->%s transition", CP9Statetype(st1), CP9Statetype(st2)); |
| 481 |
} |
| 482 |
break; |
| 483 |
case CSTM: |
| 484 |
switch (st2) { |
| 485 |
case CSTM: return hmm->tsc[CTMM][k1]; |
| 486 |
case CSTI: return hmm->tsc[CTMI][k1]; |
| 487 |
case CSTD: return hmm->tsc[CTMD][k1]; |
| 488 |
case CSTE: return hmm->esc[k1]; |
| 489 |
case CSTEL: return hmm->tsc[CTMEL][k1]; |
| 490 |
default: cm_Fail("illegal %s->%s transition", CP9Statetype(st1), CP9Statetype(st2)); |
| 491 |
} |
| 492 |
break; |
| 493 |
case CSTI: |
| 494 |
switch (st2) { |
| 495 |
case CSTM: return hmm->tsc[CTIM][k1]; |
| 496 |
case CSTI: return hmm->tsc[CTII][k1]; |
| 497 |
case CSTD: return hmm->tsc[CTID][k1]; |
| 498 |
case CSTE: return hmm->tsc[CTIM][k1]; |
| 499 |
default: cm_Fail("illegal %s->%s transition", CP9Statetype(st1), CP9Statetype(st2)); |
| 500 |
} |
| 501 |
break; |
| 502 |
case CSTD: |
| 503 |
switch (st2) { |
| 504 |
case CSTM: return hmm->tsc[CTDM][k1]; |
| 505 |
case CSTI: return hmm->tsc[CTDI][k1]; |
| 506 |
case CSTD: return hmm->tsc[CTDD][k1]; |
| 507 |
case CSTE: return hmm->tsc[CTDM][k1]; |
| 508 |
default: cm_Fail("illegal %s->%s transition", CP9Statetype(st1), CP9Statetype(st2)); |
| 509 |
} |
| 510 |
break; |
| 511 |
case CSTEL: |
| 512 |
switch (st2) { |
| 513 |
case CSTM: return 0; |
| 514 |
case CSTE: return 0; |
| 515 |
case CSTEL: return hmm->el_selfsc; |
| 516 |
default: cm_Fail("illegal %s->%s transition", CP9Statetype(st1), CP9Statetype(st2)); |
| 517 |
} |
| 518 |
break; |
| 519 |
case CSTE: |
| 520 |
|
| 521 |
cm_Fail("illegal %s->%s transition", CP9Statetype(st1), CP9Statetype(st2)); |
| 522 |
break; |
| 523 |
default: cm_Fail("illegal state %s in traceback", CP9Statetype(st1)); |
| 524 |
} |
| 525 |
|
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
void |
| 548 |
CP9ViterbiTrace(CP9_t *hmm, ESL_DSQ *dsq, int i0, int j0, |
| 549 |
CP9_MX *mx, CP9trace_t **ret_tr) |
| 550 |
{ |
| 551 |
|
| 552 |
if(dsq == NULL) |
| 553 |
cm_Fail("ERROR in CP9ViterbiTrace(), dsq is NULL."); |
| 554 |
|
| 555 |
CP9trace_t *tr; |
| 556 |
int curralloc; |
| 557 |
int tpos; |
| 558 |
int i; |
| 559 |
int k; |
| 560 |
int *erow, **mmx, **imx, **dmx, **elmx; |
| 561 |
int sc; |
| 562 |
int error_flag; |
| 563 |
int c; |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
curralloc = (j0-i0+1) * 2 + 2; |
| 570 |
CP9AllocTrace(curralloc, &tr); |
| 571 |
|
| 572 |
mmx = mx->mmx; |
| 573 |
imx = mx->imx; |
| 574 |
dmx = mx->dmx; |
| 575 |
elmx= mx->elmx; |
| 576 |
erow= mx->erow; |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
tr->statetype[0] = CSTE; |
| 582 |
tr->nodeidx[0] = 0; |
| 583 |
tr->pos[0] = 0; |
| 584 |
tpos = 1; |
| 585 |
i = j0; |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
while (tr->statetype[tpos-1] != CSTB) { |
| 590 |
error_flag = FALSE; |
| 591 |
switch (tr->statetype[tpos-1]) { |
| 592 |
case CSTM: |
| 593 |
|
| 594 |
sc = mmx[i+1][k+1] - hmm->msc[dsq[i+1]][k+1]; |
| 595 |
if (sc <= -INFTY) { CP9FreeTrace(tr); *ret_tr = NULL; return; } |
| 596 |
else if (sc == mmx[i][k] + hmm->tsc[CTMM][k]) |
| 597 |
{ |
| 598 |
tr->statetype[tpos] = CSTM; |
| 599 |
tr->nodeidx[tpos] = k--; |
| 600 |
tr->pos[tpos] = i--; |
| 601 |
} |
| 602 |
else if (sc == imx[i][k] + hmm->tsc[CTIM][k]) |
| 603 |
{ |
| 604 |
tr->statetype[tpos] = CSTI; |
| 605 |
tr->nodeidx[tpos] = k; |
| 606 |
tr->pos[tpos] = i--; |
| 607 |
} |
| 608 |
else if (sc == dmx[i][k] + hmm->tsc[CTDM][k]) |
| 609 |
{ |
| 610 |
tr->statetype[tpos] = CSTD; |
| 611 |
tr->nodeidx[tpos] = k--; |
| 612 |
tr->pos[tpos] = 0; |
| 613 |
} |
| 614 |
else |
| 615 |
{ |
| 616 |
error_flag = TRUE; |
| 617 |
|
| 618 |
|
| 619 |
for(c = 0; c < hmm->el_from_ct[k+1]; c++) |
| 620 |
{ |
| 621 |
|
| 622 |
if(sc == elmx[i][hmm->el_from_idx[k+1][c]]) |
| 623 |
{ |
| 624 |
tr->statetype[tpos] = CSTEL; |
| 625 |
k = hmm->el_from_idx[(k+1)][c]; |
| 626 |
tr->nodeidx[tpos] = k; |
| 627 |
tr->pos[tpos] = i--; |
| 628 |
error_flag = FALSE; |
| 629 |
break; |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
if(error_flag) |
| 634 |
{ |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
if (sc == hmm->bsc[k+1]) |
| 640 |
{ |
| 641 |
tr->statetype[tpos] = CSTB; |
| 642 |
tr->nodeidx[tpos] = 0; |
| 643 |
tr->pos[tpos] = 0; |
| 644 |
if(tr->pos[tpos-1] != 1) |
| 645 |
cm_Fail("traceback failed: premature begin"); |
| 646 |
error_flag = FALSE; |
| 647 |
} |
| 648 |
} |
| 649 |
if(error_flag) |
| 650 |
cm_Fail("traceback failed"); |
| 651 |
break; |
| 652 |
|
| 653 |
case CSTD: |
| 654 |
|
| 655 |
sc = dmx[i][k+1]; |
| 656 |
if (sc <= -INFTY) { CP9FreeTrace(tr); *ret_tr = NULL; return; } |
| 657 |
else if(k == 0) |
| 658 |
{ |
| 659 |
if(sc == mmx[i][k] + hmm->tsc[CTMD][k]) |
| 660 |
{ |
| 661 |
tr->statetype[tpos] = CSTB; |
| 662 |
tr->nodeidx[tpos] = 0; |
| 663 |
tr->pos[tpos] = 0; |
| 664 |
} |
| 665 |
else if (sc == imx[i][k] + hmm->tsc[CTID][k]) |
| 666 |
{ |
| 667 |
tr->statetype[tpos] = CSTI; |
| 668 |
tr->nodeidx[tpos] = k; |
| 669 |
tr->pos[tpos] = i--; |
| 670 |
} |
| 671 |
else cm_Fail("traceback failed"); |
| 672 |
} |
| 673 |
else if (sc == mmx[i][k] + hmm->tsc[CTMD][k]) |
| 674 |
{ |
| 675 |
tr->statetype[tpos] = CSTM; |
| 676 |
tr->nodeidx[tpos] = k--; |
| 677 |
tr->pos[tpos] = i--; |
| 678 |
} |
| 679 |
else if (sc == imx[i][k] + hmm->tsc[CTID][k]) |
| 680 |
{ |
| 681 |
tr->statetype[tpos] = CSTI; |
| 682 |
tr->nodeidx[tpos] = k; |
| 683 |
tr->pos[tpos] = i--; |
| 684 |
} |
| 685 |
else if (sc == dmx[i][k] + hmm->tsc[CTDD][k]) |
| 686 |
{ |
| 687 |
tr->statetype[tpos] = CSTD; |
| 688 |
tr->nodeidx[tpos] = k--; |
| 689 |
tr->pos[tpos] = 0; |
| 690 |
} |
| 691 |
else cm_Fail("traceback failed"); |
| 692 |
break; |
| 693 |
|
| 694 |
case CSTI: |
| 695 |
|
| 696 |
sc = imx[i+1][k] - hmm->isc[dsq[i+1]][k]; |
| 697 |
if (sc <= -INFTY) { CP9FreeTrace(tr); *ret_tr = NULL; return; } |
| 698 |
else if(k == 0) |
| 699 |
{ |
| 700 |
if(sc == mmx[i][k] + hmm->tsc[CTMI][k]) |
| 701 |
{ |
| 702 |
tr->statetype[tpos] = CSTB; |
| 703 |
tr->nodeidx[tpos] = 0; |
| 704 |
tr->pos[tpos] = 0; |
| 705 |
} |
| 706 |
else if (sc == imx[i][k] + hmm->tsc[CTII][k]) |
| 707 |
{ |
| 708 |
tr->statetype[tpos] = CSTI; |
| 709 |
tr->nodeidx[tpos] = k; |
| 710 |
tr->pos[tpos] = i--; |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
else if (sc == mmx[i][k] + hmm->tsc[CTMI][k]) |
| 715 |
{ |
| 716 |
tr->statetype[tpos] = CSTM; |
| 717 |
tr->nodeidx[tpos] = k--; |
| 718 |
tr->pos[tpos] = i--; |
| 719 |
} |
| 720 |
|
| 721 |
else if (sc == imx[i][k] + hmm->tsc[CTII][k]) |
| 722 |
{ |
| 723 |
tr->statetype[tpos] = CSTI; |
| 724 |
tr->nodeidx[tpos] = k; |
| 725 |
tr->pos[tpos] = i--; |
| 726 |
} |
| 727 |
else if (sc == dmx[i][k] + hmm->tsc[CTDI][k]) |
| 728 |
{ |
| 729 |
tr->statetype[tpos] = CSTD; |
| 730 |
tr->nodeidx[tpos] = k--; |
| 731 |
tr->pos[tpos] = 0; |
| 732 |
} |
| 733 |
else cm_Fail("traceback failed"); |
| 734 |
break; |
| 735 |
|
| 736 |
case CSTE: |
| 737 |
|
| 738 |
|
| 739 |
if (erow[i] <= -INFTY) { CP9FreeTrace(tr); *ret_tr = NULL; return; } |
| 740 |
if (erow[i] == imx[i][hmm->M] + hmm->tsc[CTIM][hmm->M]) |
| 741 |
{ |
| 742 |
k = hmm->M; |
| 743 |
tr->statetype[tpos] = CSTI; |
| 744 |
tr->nodeidx[tpos] = k; |
| 745 |
tr->pos[tpos] = i--; |
| 746 |
} |
| 747 |
else if (erow[i] == dmx[i][hmm->M] + hmm->tsc[CTDM][hmm->M]) |
| 748 |
{ |
| 749 |
k = hmm->M; |
| 750 |
tr->statetype[tpos] = CSTD; |
| 751 |
tr->nodeidx[tpos] = k--; |
| 752 |
tr->pos[tpos] = 0; |
| 753 |
} |
| 754 |
else |
| 755 |
{ |
| 756 |
error_flag = TRUE; |
| 757 |
for (k = hmm->M; k >= 1; k--) |
| 758 |
if (erow[i] == mmx[i][k] + hmm->esc[k]) |
| 759 |
{ |
| 760 |
tr->statetype[tpos] = CSTM; |
| 761 |
tr->nodeidx[tpos] = k--; |
| 762 |
tr->pos[tpos] = i--; |
| 763 |
error_flag = FALSE; |
| 764 |
break; |
| 765 |
} |
| 766 |
if(error_flag) |
| 767 |
{ |
| 768 |
|
| 769 |
|
| 770 |
for(c = hmm->el_from_ct[hmm->M+1]-1; c >= 0; c--) |
| 771 |
{ |
| 772 |
|
| 773 |
if(erow[i] == elmx[i][hmm->el_from_idx[hmm->M+1][c]]) |
| 774 |
{ |
| 775 |
tr->statetype[tpos] = CSTEL; |
| 776 |
k = hmm->el_from_idx[(hmm->M+1)][c]; |
| 777 |
tr->nodeidx[tpos] = k; |
| 778 |
tr->pos[tpos] = i--; |
| 779 |
error_flag = FALSE; |
| 780 |
break; |
| 781 |
} |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
if (k < 0 || error_flag) cm_Fail("traceback failed"); |
| 786 |
break; |
| 787 |
|
| 788 |
case CSTEL: |
| 789 |
|
| 790 |
|
| 791 |
sc = elmx[i+1][k]; |
| 792 |
if (sc == elmx[i][k] + hmm->el_selfsc) |
| 793 |
{ |
| 794 |
tr->statetype[tpos] = CSTEL; |
| 795 |
tr->nodeidx[tpos] = k; |
| 796 |
tr->pos[tpos] = i--; |
| 797 |
} |
| 798 |
else if(sc == mmx[i+1][k] + hmm->tsc[CTMEL][k]) |
| 799 |
{ |
| 800 |
tr->statetype[tpos] = CSTM; |
| 801 |
tr->nodeidx[tpos] = k--; |
| 802 |
tr->pos[tpos] = i+1; |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
} |
| 808 |
else cm_Fail("traceback failed"); |
| 809 |
break; |
| 810 |
|
| 811 |
default: |
| 812 |
cm_Fail("traceback failed"); |
| 813 |
|
| 814 |
} |
| 815 |
|
| 816 |
tpos++; |
| 817 |
if (tpos == curralloc) |
| 818 |
{ |
| 819 |
curralloc += (j0-i0+1); |
| 820 |
CP9ReallocTrace(tr, curralloc); |
| 821 |
} |
| 822 |
|
| 823 |
} |
| 824 |
tr->tlen = tpos; |
| 825 |
CP9ReverseTrace(tr); |
| 826 |
|
| 827 |
*ret_tr = tr; |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
void |
| 851 |
CP9ReverseTrace(CP9trace_t *tr) |
| 852 |
{ |
| 853 |
int status; |
| 854 |
char *statetype; |
| 855 |
int *nodeidx; |
| 856 |
int *pos; |
| 857 |
int opos, npos; |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
ESL_ALLOC(statetype, sizeof(char)* tr->tlen); |
| 862 |
ESL_ALLOC(nodeidx, sizeof(int) * tr->tlen); |
| 863 |
ESL_ALLOC(pos, sizeof(int) * tr->tlen); |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
for (opos = tr->tlen-1, npos = 0; npos < tr->tlen; npos++, opos--) |
| 868 |
{ |
| 869 |
statetype[npos] = tr->statetype[opos]; |
| 870 |
nodeidx[npos] = tr->nodeidx[opos]; |
| 871 |
pos[npos] = tr->pos[opos]; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
free(tr->statetype); |
| 877 |
free(tr->nodeidx); |
| 878 |
free(tr->pos); |
| 879 |
tr->statetype = statetype; |
| 880 |
tr->nodeidx = nodeidx; |
| 881 |
tr->pos = pos; |
| 882 |
return; |
| 883 |
|
| 884 |
ERROR: |
| 885 |
cm_Fail("Memory allocation error."); |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
int |
| 924 |
CP9Traces2Alignment(CM_t *cm, CP9_t *cp9, const ESL_ALPHABET *abc, ESL_SQ **sq, float *wgt, |
| 925 |
int nseq, CP9trace_t **tr, int do_full, int do_matchonly, |
| 926 |
ESL_MSA **ret_msa) |
| 927 |
{ |
| 928 |
int status; |
| 929 |
ESL_MSA *msa = NULL; |
| 930 |
int idx; |
| 931 |
int alen; |
| 932 |
int *maxins = NULL; |
| 933 |
int *maxels = NULL; |
| 934 |
int *matmap = NULL; |
| 935 |
int nins; |
| 936 |
int cpos; |
| 937 |
int apos; |
| 938 |
int rpos; |
| 939 |
int tpos; |
| 940 |
int epos; |
| 941 |
int statetype; |
| 942 |
CMEmitMap_t *emap = NULL; |
| 943 |
int *imap = NULL; |
| 944 |
int *elmap = NULL; |
| 945 |
int *matuse = NULL; |
| 946 |
int *eluse = NULL; |
| 947 |
int **eposmap = NULL; |
| 948 |
int *iuse = NULL; |
| 949 |
CMConsensus_t *con = NULL; |
| 950 |
int next_match; |
| 951 |
int c; |
| 952 |
int *insleft; |
| 953 |
int nd; |
| 954 |
int max_ins_or_el[2]; |
| 955 |
int pass_offset[2]; |
| 956 |
int pass; |
| 957 |
int aulen; |
| 958 |
char errbuf[eslERRBUFSIZE]; |
| 959 |
|
| 960 |
|
| 961 |
if(cp9 == NULL) |
| 962 |
cm_Fail("ERROR in CP9Traces2Alignment, cp9 is NULL.\n"); |
| 963 |
if(cm->cp9map == NULL) |
| 964 |
cm_Fail("ERROR in CP9Traces2Alignment, cm->cp9map is NULL.\n"); |
| 965 |
|
| 966 |
|
| 967 |
if(cm->abc->type == eslRNA) |
| 968 |
{ |
| 969 |
if(abc->type != eslRNA && abc->type != eslDNA) |
| 970 |
cm_Fail("ERROR in CP9Traces2Alignment(), cm alphabet is RNA, but requested output alphabet is neither DNA nor RNA."); |
| 971 |
} |
| 972 |
else if(cm->abc->K != abc->K) |
| 973 |
cm_Fail("ERROR in CP9Traces2Alignment(), cm alphabet size is %d, but requested output alphabet size is %d.", cm->abc->K, abc->K); |
| 974 |
|
| 975 |
|
| 976 |
emap = CreateEmitMap(cm); |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
ESL_ALLOC(insleft, sizeof(int) * (emap->clen+1)); |
| 989 |
esl_vec_ISet(insleft, (cm->clen+1), -1); |
| 990 |
for(nd = 0; nd < cm->nodes; nd++) |
| 991 |
{ |
| 992 |
if(cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd || cm->ndtype[nd] == BEGR_nd || cm->ndtype[nd] == ROOT_nd) |
| 993 |
if(insleft[emap->lpos[nd]] == -1) |
| 994 |
insleft[emap->lpos[nd]] = TRUE; |
| 995 |
if(cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd) |
| 996 |
insleft[emap->rpos[nd]-1] = FALSE; |
| 997 |
} |
| 998 |
if(insleft[emap->clen] == -1) insleft[emap->clen] = FALSE; |
| 999 |
|
| 1000 |
for(cpos = 0; cpos <= emap->clen; cpos++) |
| 1001 |
ESL_DASSERT1((insleft[cpos] != -1)); |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
ESL_ALLOC(matuse, sizeof(int) * (emap->clen+1)); |
| 1017 |
ESL_ALLOC(eluse, sizeof(int) * (emap->clen+1)); |
| 1018 |
ESL_ALLOC(iuse, sizeof(int) * (emap->clen+1)); |
| 1019 |
ESL_ALLOC(maxins, sizeof(int) * (emap->clen+1)); |
| 1020 |
ESL_ALLOC(maxels, sizeof(int) * (emap->clen+1)); |
| 1021 |
ESL_ALLOC(matmap, sizeof(int) * (emap->clen+1)); |
| 1022 |
ESL_ALLOC(imap, sizeof(int) * (emap->clen+1)); |
| 1023 |
ESL_ALLOC(elmap, sizeof(int) * (emap->clen+1)); |
| 1024 |
ESL_ALLOC(eposmap,sizeof(int *) * (nseq)); |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
for (cpos = 0; cpos <= emap->clen; cpos++) |
| 1034 |
{ |
| 1035 |
if(!do_full || cpos == 0) |
| 1036 |
matuse[cpos] = 0; |
| 1037 |
else |
| 1038 |
matuse[cpos] = 1; |
| 1039 |
maxins[cpos] = maxels[cpos] = 0; |
| 1040 |
iuse[cpos] = eluse[cpos] = imap[cpos] = elmap[cpos] = 0; |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
for (idx = 0; idx < nseq; idx++) |
| 1050 |
{ |
| 1051 |
ESL_ALLOC(eposmap[idx], sizeof(int) * (emap->clen+1)); |
| 1052 |
for (cpos = 0; cpos <= emap->clen; cpos++) |
| 1053 |
{ |
| 1054 |
iuse[cpos] = eluse[cpos] = 0; |
| 1055 |
eposmap[idx][cpos] = -1; |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
next_match = -1; |
| 1061 |
for (tpos = tr[idx]->tlen - 1; tpos >= 0; tpos--) |
| 1062 |
{ |
| 1063 |
statetype = tr[idx]->statetype[tpos]; |
| 1064 |
cpos = tr[idx]->nodeidx[tpos]; |
| 1065 |
if(statetype == CSTM) next_match = cpos; |
| 1066 |
if(statetype == CSTE) next_match = cp9->M+1; |
| 1067 |
if(statetype == CSTEL) eposmap[idx][cpos] = next_match; |
| 1068 |
} |
| 1069 |
for (cpos = 0; cpos <= emap->clen; cpos++) |
| 1070 |
{ |
| 1071 |
if(eposmap[idx][cpos] != -1) |
| 1072 |
{ |
| 1073 |
|
| 1074 |
|
| 1075 |
for(c = 0; c < cp9->el_from_ct[eposmap[idx][cpos]]; c++) |
| 1076 |
{ |
| 1077 |
if(cp9->el_from_idx[eposmap[idx][cpos]][c] == cpos) |
| 1078 |
{ |
| 1079 |
eposmap[idx][cpos] = emap->epos[cp9->el_from_cmnd[eposmap[idx][cpos]][c]]; |
| 1080 |
break; |
| 1081 |
} |
| 1082 |
if(c == (cp9->el_from_ct[eposmap[idx][cpos]] - 1)) |
| 1083 |
cm_Fail("Couldn't determine epos for cpos: %d\n", cpos); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
for (tpos = 0; tpos < tr[idx]->tlen; tpos++) |
| 1089 |
{ |
| 1090 |
cpos = tr[idx]->nodeidx[tpos]; |
| 1091 |
switch (tr[idx]->statetype[tpos]) { |
| 1092 |
case CSTI: iuse[cpos]++; break; |
| 1093 |
case CSTM: matuse[tr[idx]->nodeidx[tpos]] = 1; break; |
| 1094 |
case CSTEL: |
| 1095 |
eluse[eposmap[idx][cpos]]++; |
| 1096 |
break; |
| 1097 |
case CSTD: |
| 1098 |
case CSTE: |
| 1099 |
case CSTB: |
| 1100 |
break; |
| 1101 |
default: |
| 1102 |
cm_Fail("CP9Traces2Alignment reports unrecognized statetype %c", |
| 1103 |
CP9Statetype(tr[idx]->statetype[tpos])); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
for (cpos = 0; cpos <= emap->clen; cpos++) |
| 1107 |
{ |
| 1108 |
if (iuse[cpos] > maxins[cpos]) maxins[cpos] = iuse[cpos]; |
| 1109 |
if (eluse[cpos] > maxels[cpos]) maxels[cpos] = eluse[cpos]-1; |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
matmap[0] = -1; |
| 1124 |
alen = 0; |
| 1125 |
for (cpos = 0; cpos <= emap->clen; cpos++) |
| 1126 |
{ |
| 1127 |
if (matuse[cpos]) |
| 1128 |
{ |
| 1129 |
matmap[cpos] = alen; |
| 1130 |
alen++; |
| 1131 |
} |
| 1132 |
else |
| 1133 |
matmap[cpos] = -1; |
| 1134 |
|
| 1135 |
if(insleft[cpos]) { |
| 1136 |
imap[cpos] = alen; alen += maxins[cpos]; |
| 1137 |
elmap[cpos] = alen; alen += maxels[cpos]; |
| 1138 |
} |
| 1139 |
else { |
| 1140 |
elmap[cpos] = alen; alen += maxels[cpos]; |
| 1141 |
imap[cpos] = alen; alen += maxins[cpos]; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
if ((msa = esl_msa_Create(nseq, alen)) == NULL) { status = eslEMEM; goto ERROR; } |
| 1146 |
msa->nseq = nseq; |
| 1147 |
msa->alen = alen; |
| 1148 |
msa->abc = (ESL_ALPHABET *) abc; |
| 1149 |
|
| 1150 |
for (idx = 0; idx < nseq; idx++) |
| 1151 |
{ |
| 1152 |
if(sq[idx]->dsq == NULL) cm_Fail("ERROR in CP9Traces2Alignment(), sq's should be digitized.\n"); |
| 1153 |
|
| 1154 |
for (cpos = 0; cpos <= emap->clen; cpos++) |
| 1155 |
iuse[cpos] = eluse[cpos] = 0; |
| 1156 |
|
| 1157 |
for (apos = 0; apos < alen; apos++) |
| 1158 |
msa->aseq[idx][apos] = '.'; |
| 1159 |
for (cpos = 0; cpos <= emap->clen; cpos++) |
| 1160 |
if (matmap[cpos] != -1) msa->aseq[idx][matmap[cpos]] = '-'; |
| 1161 |
msa->aseq[idx][alen] = '\0'; |
| 1162 |
|
| 1163 |
|
| 1164 |
apos = 0; |
| 1165 |
for (tpos = 0; tpos < tr[idx]->tlen; tpos++) |
| 1166 |
{ |
| 1167 |
statetype = tr[idx]->statetype[tpos]; |
| 1168 |
rpos = tr[idx]->pos[tpos]; |
| 1169 |
cpos = tr[idx]->nodeidx[tpos]; |
| 1170 |
|
| 1171 |
if (statetype == CSTM) |
| 1172 |
{ |
| 1173 |
apos = matmap[cpos]; |
| 1174 |
msa->aseq[idx][apos] = abc->sym[sq[idx]->dsq[rpos]]; |
| 1175 |
} |
| 1176 |
else if (statetype == CSTD) |
| 1177 |
apos = matmap[cpos]+1; |
| 1178 |
else if (statetype == CSTI) |
| 1179 |
{ |
| 1180 |
|
| 1181 |
apos = imap[cpos] + iuse[cpos]; |
| 1182 |
msa->aseq[idx][apos] = (char) tolower((int) abc->sym[sq[idx]->dsq[rpos]]); |
| 1183 |
iuse[cpos]++; |
| 1184 |
} |
| 1185 |
else if (statetype == CSTEL) |
| 1186 |
{ |
| 1187 |
|
| 1188 |
epos = eposmap[idx][cpos]; |
| 1189 |
if(tr[idx]->statetype[tpos-1] == CSTEL) |
| 1190 |
{ |
| 1191 |
apos = elmap[epos] + eluse[epos]; |
| 1192 |
msa->aseq[idx][apos] = (char) tolower((int) abc->sym[sq[idx]->dsq[rpos]]); |
| 1193 |
eluse[epos]++; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
else if (statetype == CSTE) |
| 1197 |
apos = matmap[emap->clen]+1; |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
if(!(cm->align_opts & CM_ALIGN_FLUSHINSERTS)) |
| 1220 |
rightjustify(msa->abc, msa->aseq[idx], maxins[0]); |
| 1221 |
if(!(cm->align_opts & CM_ALIGN_FLUSHINSERTS)) |
| 1222 |
rightjustify(msa->abc, msa->aseq[idx]+maxins[0], maxels[0]); |
| 1223 |
|
| 1224 |
for (cpos = 1; cpos < emap->clen; cpos++) |
| 1225 |
{ |
| 1226 |
if(insleft[cpos]) { |
| 1227 |
max_ins_or_el[0] = maxins[cpos]; |
| 1228 |
max_ins_or_el[1] = maxels[cpos]; |
| 1229 |
pass_offset[0] = 0; |
| 1230 |
pass_offset[1] = maxins[cpos]; |
| 1231 |
} |
| 1232 |
else { |
| 1233 |
max_ins_or_el[0] = maxels[cpos]; |
| 1234 |
max_ins_or_el[1] = maxins[cpos]; |
| 1235 |
pass_offset[0] = 0; |
| 1236 |
pass_offset[1] = maxels[cpos]; |
| 1237 |
} |
| 1238 |
for(pass = 0; pass <= 1; pass++) |
| 1239 |
{ |
| 1240 |
if (max_ins_or_el[pass] > 1) |
| 1241 |
{ |
| 1242 |
apos = matmap[cpos]+1 + pass_offset[pass]; |
| 1243 |
if(! (cm->align_opts & CM_ALIGN_FLUSHINSERTS)) |
| 1244 |
{ |
| 1245 |
for (nins = 0; islower((int) (msa->aseq[idx][apos])); apos++) |
| 1246 |
nins++; |
| 1247 |
nins /= 2; |
| 1248 |
rightjustify(msa->abc, msa->aseq[idx]+matmap[cpos]+1 + pass_offset[pass] + nins, max_ins_or_el[pass]-nins); |
| 1249 |
} |
| 1250 |
|
| 1251 |
else if(!(insleft[cpos])) |
| 1252 |
rightjustify(msa->abc, msa->aseq[idx] + apos, max_ins_or_el[pass]); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
if(cm->align_opts & CM_ALIGN_FLUSHINSERTS) |
| 1259 |
rightjustify(msa->abc, msa->aseq[idx]+matmap[emap->clen]+1, maxels[emap->clen]); |
| 1260 |
if(cm->align_opts & CM_ALIGN_FLUSHINSERTS) |
| 1261 |
rightjustify(msa->abc, msa->aseq[idx]+matmap[emap->clen]+1+maxels[emap->clen], maxins[emap->clen]); |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
msa->nseq = nseq; |
| 1268 |
msa->alen = alen; |
| 1269 |
aulen = strlen(INFERNAL_VERSION)+10; |
| 1270 |
ESL_ALLOC(msa->au, sizeof(char) * aulen); |
| 1271 |
snprintf(msa->au, aulen, "Infernal %s", INFERNAL_VERSION); |
| 1272 |
|
| 1273 |
|
| 1274 |
for (idx = 0; idx < nseq; idx++) |
| 1275 |
{ |
| 1276 |
if((status = esl_strdup(sq[idx]->name, -1, &(msa->sqname[idx]))) != eslOK) goto ERROR; |
| 1277 |
if (wgt == NULL) msa->wgt[idx] = 1.0; |
| 1278 |
else msa->wgt[idx] = wgt[idx]; |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
ESL_ALLOC(msa->ss_cons, (sizeof(char) * (alen+1))); |
| 1289 |
ESL_ALLOC(msa->rf, (sizeof(char) * (alen+1))); |
| 1290 |
con = CreateCMConsensus(cm, abc); |
| 1291 |
|
| 1292 |
for (cpos = 0; cpos <= emap->clen; cpos++) |
| 1293 |
{ |
| 1294 |
if (matuse[cpos]) |
| 1295 |
{ |
| 1296 |
if (con->ct[cpos-1] != -1 && matuse[con->ct[cpos-1]+1] == 0) { |
| 1297 |
msa->ss_cons[matmap[cpos]] = '.'; |
| 1298 |
msa->rf[matmap[cpos]] = con->cseq[cpos-1]; |
| 1299 |
} else { |
| 1300 |
msa->ss_cons[matmap[cpos]] = con->cstr[cpos-1]; |
| 1301 |
msa->rf[matmap[cpos]] = con->cseq[cpos-1]; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
if (maxins[cpos] > 0) |
| 1305 |
for (apos = imap[cpos]; apos < imap[cpos] + maxins[cpos]; apos++) |
| 1306 |
{ |
| 1307 |
msa->ss_cons[apos] = '.'; |
| 1308 |
msa->rf[apos] = '.'; |
| 1309 |
} |
| 1310 |
if (maxels[cpos] > 0) |
| 1311 |
{ |
| 1312 |
for (apos = elmap[cpos]; apos < elmap[cpos] + maxels[cpos]; apos++) |
| 1313 |
{ |
| 1314 |
msa->ss_cons[apos] = '~'; |
| 1315 |
msa->rf[apos] = '~'; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
} |
| 1319 |
msa->ss_cons[alen] = '\0'; |
| 1320 |
msa->rf[alen] = '\0'; |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
if(do_matchonly) |
| 1328 |
{ |
| 1329 |
int *useme; |
| 1330 |
ESL_ALLOC(useme, sizeof(int) * (msa->alen)); |
| 1331 |
esl_vec_ISet(useme, msa->alen, FALSE); |
| 1332 |
for(cpos = 0; cpos <= emap->clen; cpos++) |
| 1333 |
if(matmap[cpos] != -1) useme[matmap[cpos]] = TRUE; |
| 1334 |
if((status = esl_msa_ColumnSubset(msa, errbuf, useme)) != eslOK) return status; |
| 1335 |
free(useme); |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
FreeCMConsensus(con); |
| 1340 |
FreeEmitMap(emap); |
| 1341 |
free(eluse); |
| 1342 |
free(iuse); |
| 1343 |
free(matuse); |
| 1344 |
free(maxins); |
| 1345 |
free(maxels); |
| 1346 |
free(matmap); |
| 1347 |
free(imap); |
| 1348 |
free(elmap); |
| 1349 |
free(insleft); |
| 1350 |
esl_Free2D((void **) eposmap, nseq); |
| 1351 |
*ret_msa = msa; |
| 1352 |
return eslOK; |
| 1353 |
|
| 1354 |
ERROR: |
| 1355 |
if(con != NULL) FreeCMConsensus(con); |
| 1356 |
if(emap != NULL) FreeEmitMap(emap); |
| 1357 |
if(matuse!= NULL) free(matuse); |
| 1358 |
if(iuse != NULL) free(iuse); |
| 1359 |
if(elmap != NULL) free(elmap); |
| 1360 |
if(maxels!= NULL) free(maxels); |
| 1361 |
if(matmap!= NULL) free(matmap); |
| 1362 |
esl_Free2D((void **) eposmap, nseq); |
| 1363 |
if(msa != NULL) esl_msa_Destroy(msa); |
| 1364 |
return status; |
| 1365 |
} |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
int |
| 1381 |
CP9TraceScoreCorrectionNull2(CP9_t *hmm, char *errbuf, CP9trace_t *tr, ESL_DSQ *dsq, int start, float omega, float *ret_sc) |
| 1382 |
{ |
| 1383 |
int status; |
| 1384 |
float *p; |
| 1385 |
int *sc; |
| 1386 |
int a; |
| 1387 |
int tpos; |
| 1388 |
int score; |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
if (tr == NULL) return 0.0; |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
ESL_ALLOC(p, sizeof(float) * hmm->abc->K); |
| 1399 |
esl_vec_FSet(p, hmm->abc->K, 0.0); |
| 1400 |
for (tpos = 0; tpos < tr->tlen; tpos++) { |
| 1401 |
if (tr->statetype[tpos] == CSTM) esl_vec_FAdd(p, hmm->mat[tr->nodeidx[tpos]], hmm->abc->K); |
| 1402 |
else if (tr->statetype[tpos] == CSTI) esl_vec_FAdd(p, hmm->ins[tr->nodeidx[tpos]], hmm->abc->K); |
| 1403 |
} |
| 1404 |
esl_vec_FNorm(p, hmm->abc->K); |
| 1405 |
|
| 1406 |
ESL_ALLOC(sc, sizeof(int) * (hmm->abc->Kp)); |
| 1407 |
|
| 1408 |
for (a = 0; a < hmm->abc->K; a++) sc[a] = Prob2Score(p[a], hmm->null[a]); |
| 1409 |
|
| 1410 |
for (a = hmm->abc->K+1; a < hmm->abc->Kp-1; a++) sc[a] = esl_abc_IAvgScore(hmm->abc, a, sc); |
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
score = 0; |
| 1415 |
for (tpos = 0; tpos < tr->tlen; tpos++) |
| 1416 |
if (tr->statetype[tpos] == CSTM || tr->statetype[tpos] == CSTI) score += sc[dsq[tr->pos[tpos]]]; |
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
score += ((int) (sreLOG2(omega))) * INTSCALE; |
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
printf("CP9TraceScoreCorrectionNull2() returning %.3f bits\n", Scorify(ILogsum(0, score))); |
| 1427 |
*ret_sc = Scorify(ILogsum(0, score)); |
| 1428 |
return eslOK; |
| 1429 |
|
| 1430 |
ERROR: |
| 1431 |
ESL_FAIL(status, errbuf, "CP9TraceScoreCorrectionNull2(): memory allocation error."); |
| 1432 |
return status; |
| 1433 |
} |