| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <p7_config.h> |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <stdio.h> |
| 12 |
|
| 13 |
#include "easel.h" |
| 14 |
#include "esl_alphabet.h" |
| 15 |
#include "esl_dmatrix.h" |
| 16 |
#include "esl_getopts.h" |
| 17 |
#include "esl_msa.h" |
| 18 |
#include "esl_msacluster.h" |
| 19 |
#include "esl_msaweight.h" |
| 20 |
#include "esl_random.h" |
| 21 |
#include "esl_vectorops.h" |
| 22 |
|
| 23 |
#include "hmmer.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
P7_BUILDER * |
| 52 |
p7_builder_Create(const ESL_GETOPTS *go, const ESL_ALPHABET *abc) |
| 53 |
{ |
| 54 |
P7_BUILDER *bld = NULL; |
| 55 |
int seed; |
| 56 |
int status; |
| 57 |
|
| 58 |
|
| 59 |
ESL_ALLOC(bld, sizeof(P7_BUILDER)); |
| 60 |
bld->prior = NULL; |
| 61 |
bld->r = NULL; |
| 62 |
bld->S = NULL; |
| 63 |
bld->Q = NULL; |
| 64 |
bld->eset = -1.0; |
| 65 |
bld->re_target = -1.0; |
| 66 |
|
| 67 |
if (go == NULL) |
| 68 |
{ |
| 69 |
bld->arch_strategy = p7_ARCH_FAST; |
| 70 |
bld->wgt_strategy = p7_WGT_PB; |
| 71 |
bld->effn_strategy = p7_EFFN_ENTROPY; |
| 72 |
seed = 42; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
if (esl_opt_GetBoolean(go, "--fast")) bld->arch_strategy = p7_ARCH_FAST; |
| 77 |
else if (esl_opt_GetBoolean(go, "--hand")) bld->arch_strategy = p7_ARCH_HAND; |
| 78 |
|
| 79 |
if (esl_opt_GetBoolean(go, "--wpb")) bld->wgt_strategy = p7_WGT_PB; |
| 80 |
else if (esl_opt_GetBoolean(go, "--wgsc")) bld->wgt_strategy = p7_WGT_GSC; |
| 81 |
else if (esl_opt_GetBoolean(go, "--wblosum")) bld->wgt_strategy = p7_WGT_BLOSUM; |
| 82 |
else if (esl_opt_GetBoolean(go, "--wnone")) bld->wgt_strategy = p7_WGT_NONE; |
| 83 |
else if (esl_opt_GetBoolean(go, "--wgiven")) bld->wgt_strategy = p7_WGT_GIVEN; |
| 84 |
|
| 85 |
if (esl_opt_GetBoolean(go, "--eent")) bld->effn_strategy = p7_EFFN_ENTROPY; |
| 86 |
else if (esl_opt_GetBoolean(go, "--eentexp")) bld->effn_strategy = p7_EFFN_ENTROPY_EXP; |
| 87 |
else if (esl_opt_GetBoolean(go, "--eclust")) bld->effn_strategy = p7_EFFN_CLUST; |
| 88 |
else if (esl_opt_GetBoolean(go, "--enone")) bld->effn_strategy = p7_EFFN_NONE; |
| 89 |
else if (esl_opt_IsOn (go, "--eset")) { bld->effn_strategy = p7_EFFN_SET; bld->eset = esl_opt_GetReal(go, "--eset"); } |
| 90 |
|
| 91 |
seed = esl_opt_GetInteger(go, "--seed"); |
| 92 |
} |
| 93 |
|
| 94 |
bld->max_insert_len = 0; |
| 95 |
|
| 96 |
|
| 97 |
if (go != NULL && esl_opt_IsOn (go, "--ere")) |
| 98 |
bld->re_target = esl_opt_GetReal(go, "--ere"); |
| 99 |
else { |
| 100 |
switch (abc->type) { |
| 101 |
case eslAMINO: bld->re_target = p7_ETARGET_AMINO; break; |
| 102 |
case eslDNA: bld->re_target = p7_ETARGET_DNA; break; |
| 103 |
case eslRNA: bld->re_target = p7_ETARGET_DNA; break; |
| 104 |
default: bld->re_target = p7_ETARGET_OTHER; break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
bld->symfrac = (go != NULL) ? esl_opt_GetReal (go, "--symfrac") : 0.5; |
| 109 |
bld->fragthresh = (go != NULL) ? esl_opt_GetReal (go, "--fragthresh") : 0.5; |
| 110 |
bld->wid = (go != NULL) ? esl_opt_GetReal (go, "--wid") : 0.62; |
| 111 |
bld->esigma = (go != NULL) ? esl_opt_GetReal (go, "--esigma") : 45.0; |
| 112 |
bld->eid = (go != NULL) ? esl_opt_GetReal (go, "--eid") : 0.62; |
| 113 |
bld->EmL = (go != NULL) ? esl_opt_GetInteger(go, "--EmL") : 200; |
| 114 |
bld->EmN = (go != NULL) ? esl_opt_GetInteger(go, "--EmN") : 200; |
| 115 |
bld->EvL = (go != NULL) ? esl_opt_GetInteger(go, "--EvL") : 200; |
| 116 |
bld->EvN = (go != NULL) ? esl_opt_GetInteger(go, "--EvN") : 200; |
| 117 |
bld->EfL = (go != NULL) ? esl_opt_GetInteger(go, "--EfL") : 100; |
| 118 |
bld->EfN = (go != NULL) ? esl_opt_GetInteger(go, "--EfN") : 200; |
| 119 |
bld->Eft = (go != NULL) ? esl_opt_GetReal (go, "--Eft") : 0.04; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
bld->r = esl_randomness_CreateFast(seed); |
| 128 |
bld->do_reseeding = (seed == 0) ? FALSE : TRUE; |
| 129 |
|
| 130 |
if (go && esl_opt_GetBoolean(go, "--pnone") ) bld->prior = NULL; |
| 131 |
else if (go && esl_opt_GetBoolean(go, "--plaplace") ) bld->prior = p7_prior_CreateLaplace(abc); |
| 132 |
else |
| 133 |
{ |
| 134 |
switch (abc->type) { |
| 135 |
case eslAMINO: bld->prior = p7_prior_CreateAmino(); break; |
| 136 |
case eslDNA: bld->prior = p7_prior_CreateNucleic(); break; |
| 137 |
case eslRNA: bld->prior = p7_prior_CreateNucleic(); break; |
| 138 |
default: bld->prior = p7_prior_CreateLaplace(abc); break; |
| 139 |
} |
| 140 |
if (bld->prior == NULL) goto ERROR; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
bld->abc = abc; |
| 145 |
bld->errbuf[0] = '\0'; |
| 146 |
|
| 147 |
bld->popen = -1; |
| 148 |
bld->pextend = -1; |
| 149 |
|
| 150 |
return bld; |
| 151 |
|
| 152 |
ERROR: |
| 153 |
p7_builder_Destroy(bld); |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 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 |
p7_builder_LoadScoreSystem(P7_BUILDER *bld, const char *matrix, double popen, double pextend, P7_BG *bg) |
| 197 |
{ |
| 198 |
double *f = NULL; |
| 199 |
double slambda; |
| 200 |
int status; |
| 201 |
|
| 202 |
bld->errbuf[0] = '\0'; |
| 203 |
|
| 204 |
|
| 205 |
if (bld->S != NULL) esl_scorematrix_Destroy(bld->S); |
| 206 |
if (bld->Q != NULL) esl_dmatrix_Destroy(bld->Q); |
| 207 |
|
| 208 |
|
| 209 |
if ((bld->S = esl_scorematrix_Create(bld->abc)) == NULL) { status = eslEMEM; goto ERROR; } |
| 210 |
status = esl_scorematrix_Set(matrix, bld->S); |
| 211 |
if (status == eslENOTFOUND) ESL_XFAIL(status, bld->errbuf, "no matrix named %s is available as a built-in", matrix); |
| 212 |
else if (status != eslOK) ESL_XFAIL(status, bld->errbuf, "failed to set score matrix %s as a built-in", matrix); |
| 213 |
|
| 214 |
|
| 215 |
ESL_ALLOC(f, sizeof(double) * bg->abc->K); |
| 216 |
esl_vec_F2D(bg->f, bg->abc->K, f); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
status = esl_scorematrix_ProbifyGivenBG(bld->S, f, f, &slambda, &(bld->Q)); |
| 221 |
if (status == eslEINVAL) ESL_XFAIL(eslEINVAL, bld->errbuf, "built-in score matrix %s has no valid solution for lambda", matrix); |
| 222 |
else if (status == eslENOHALT) ESL_XFAIL(eslEINVAL, bld->errbuf, "failed to solve score matrix %s for lambda", matrix); |
| 223 |
else if (status != eslOK) ESL_XFAIL(eslEINVAL, bld->errbuf, "unexpected error in solving score matrix %s for probability parameters", matrix); |
| 224 |
|
| 225 |
|
| 226 |
esl_scorematrix_JointToConditionalOnQuery(bld->abc, bld->Q); |
| 227 |
|
| 228 |
bld->popen = popen; |
| 229 |
bld->pextend = pextend; |
| 230 |
free(f); |
| 231 |
return eslOK; |
| 232 |
|
| 233 |
ERROR: |
| 234 |
if (f) free(f); |
| 235 |
return status; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
int |
| 283 |
p7_builder_SetScoreSystem(P7_BUILDER *bld, const char *mxfile, const char *env, double popen, double pextend, P7_BG *bg) |
| 284 |
{ |
| 285 |
ESL_FILEPARSER *efp = NULL; |
| 286 |
double *f = NULL; |
| 287 |
double slambda; |
| 288 |
int status; |
| 289 |
|
| 290 |
bld->errbuf[0] = '\0'; |
| 291 |
|
| 292 |
|
| 293 |
if (bld->S != NULL) esl_scorematrix_Destroy(bld->S); |
| 294 |
if (bld->Q != NULL) esl_dmatrix_Destroy(bld->Q); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
if (mxfile == NULL) |
| 299 |
{ |
| 300 |
if ((bld->S = esl_scorematrix_Create(bld->abc)) == NULL) { status = eslEMEM; goto ERROR; } |
| 301 |
if (bld->abc->type == eslAMINO) { |
| 302 |
if ((status = esl_scorematrix_Set("BLOSUM62", bld->S)) != eslOK) goto ERROR; |
| 303 |
} else { |
| 304 |
if ((status = esl_scorematrix_Set("DNA1", bld->S)) != eslOK) goto ERROR; |
| 305 |
} |
| 306 |
} |
| 307 |
else |
| 308 |
{ |
| 309 |
if ((status = esl_fileparser_Open(mxfile, env, &efp)) != eslOK) ESL_XFAIL(status, bld->errbuf, "Failed to find or open matrix file %s", mxfile); |
| 310 |
if ((status = esl_scorematrix_Read(efp, bld->abc, &(bld->S))) != eslOK) ESL_XFAIL(status, bld->errbuf, "Failed to read matrix from %s:\n%s", mxfile, efp->errbuf); |
| 311 |
esl_fileparser_Close(efp); |
| 312 |
efp = NULL; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
ESL_ALLOC(f, sizeof(double) * bg->abc->K); |
| 317 |
esl_vec_F2D(bg->f, bg->abc->K, f); |
| 318 |
|
| 319 |
|
| 320 |
status = esl_scorematrix_ProbifyGivenBG(bld->S, f, f, &slambda, &(bld->Q)); |
| 321 |
if (status == eslEINVAL) ESL_XFAIL(eslEINVAL, bld->errbuf, "input score matrix %s has no valid solution for lambda", mxfile); |
| 322 |
else if (status == eslENOHALT) ESL_XFAIL(eslEINVAL, bld->errbuf, "failed to solve input score matrix %s for lambda: are you sure it's valid?", mxfile); |
| 323 |
else if (status != eslOK) ESL_XFAIL(eslEINVAL, bld->errbuf, "unexpected error in solving input score matrix %s for probability parameters", mxfile); |
| 324 |
|
| 325 |
|
| 326 |
esl_scorematrix_JointToConditionalOnQuery(bld->abc, bld->Q); |
| 327 |
|
| 328 |
bld->popen = popen; |
| 329 |
bld->pextend = pextend; |
| 330 |
free(f); |
| 331 |
return eslOK; |
| 332 |
|
| 333 |
ERROR: |
| 334 |
if (efp) esl_fileparser_Close(efp); |
| 335 |
if (f) free(f); |
| 336 |
return status; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
void |
| 348 |
p7_builder_Destroy(P7_BUILDER *bld) |
| 349 |
{ |
| 350 |
if (bld == NULL) return; |
| 351 |
|
| 352 |
if (bld->prior != NULL) p7_prior_Destroy(bld->prior); |
| 353 |
if (bld->r != NULL) esl_randomness_Destroy(bld->r); |
| 354 |
if (bld->Q != NULL) esl_dmatrix_Destroy(bld->Q); |
| 355 |
if (bld->S != NULL) esl_scorematrix_Destroy(bld->S); |
| 356 |
|
| 357 |
free(bld); |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static int validate_msa (P7_BUILDER *bld, ESL_MSA *msa); |
| 370 |
static int relative_weights (P7_BUILDER *bld, ESL_MSA *msa); |
| 371 |
static int build_model (P7_BUILDER *bld, ESL_MSA *msa, P7_HMM **ret_hmm, P7_TRACE ***opt_tr); |
| 372 |
static int effective_seqnumber (P7_BUILDER *bld, const ESL_MSA *msa, P7_HMM *hmm, const P7_BG *bg); |
| 373 |
static int parameterize (P7_BUILDER *bld, P7_HMM *hmm); |
| 374 |
static int annotate (P7_BUILDER *bld, const ESL_MSA *msa, P7_HMM *hmm); |
| 375 |
static int calibrate (P7_BUILDER *bld, P7_HMM *hmm, P7_BG *bg, P7_PROFILE **opt_gm, P7_OPROFILE **opt_om); |
| 376 |
static int make_post_msa (P7_BUILDER *bld, const ESL_MSA *premsa, const P7_HMM *hmm, P7_TRACE **tr, ESL_MSA **opt_postmsa); |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
int |
| 415 |
p7_Builder(P7_BUILDER *bld, ESL_MSA *msa, P7_BG *bg, |
| 416 |
P7_HMM **opt_hmm, P7_TRACE ***opt_trarr, P7_PROFILE **opt_gm, P7_OPROFILE **opt_om, |
| 417 |
ESL_MSA **opt_postmsa) |
| 418 |
{ |
| 419 |
int i,j; |
| 420 |
uint32_t checksum = 0; |
| 421 |
P7_HMM *hmm = NULL; |
| 422 |
P7_TRACE **tr = NULL; |
| 423 |
P7_TRACE ***tr_ptr = (opt_trarr != NULL || opt_postmsa != NULL) ? &tr : NULL; |
| 424 |
int status; |
| 425 |
if ((status = validate_msa (bld, msa)) != eslOK) goto ERROR; |
| 426 |
if ((status = esl_msa_Checksum (msa, &checksum)) != eslOK) ESL_XFAIL(status, bld->errbuf, "Failed to calculate checksum"); |
| 427 |
if ((status = relative_weights (bld, msa)) != eslOK) goto ERROR; |
| 428 |
if ((status = esl_msa_MarkFragments_old(msa, bld->fragthresh)) != eslOK) goto ERROR; |
| 429 |
if ((status = build_model (bld, msa, &hmm, tr_ptr)) != eslOK) goto ERROR; |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
if (bld->max_insert_len>0) |
| 435 |
for (i=1; i<hmm->M; i++ ) |
| 436 |
hmm->t[i][p7H_II] = ESL_MIN(hmm->t[i][p7H_II], bld->max_insert_len*hmm->t[i][p7H_MI]); |
| 437 |
|
| 438 |
if ((status = effective_seqnumber (bld, msa, hmm, bg)) != eslOK) goto ERROR; |
| 439 |
if ((status = parameterize (bld, hmm)) != eslOK) goto ERROR; |
| 440 |
if ((status = annotate (bld, msa, hmm)) != eslOK) goto ERROR; |
| 441 |
if ((status = calibrate (bld, hmm, bg, opt_gm, opt_om)) != eslOK) goto ERROR; |
| 442 |
if ((status = make_post_msa (bld, msa, hmm, tr, opt_postmsa)) != eslOK) goto ERROR; |
| 443 |
|
| 444 |
|
| 445 |
if (hmm->mm != NULL) |
| 446 |
for (i=1; i<hmm->M; i++ ) |
| 447 |
if (hmm->mm[i] == 'm') |
| 448 |
for (j=0; j<hmm->abc->K; j++) |
| 449 |
hmm->mat[i][j] = bg->f[j]; |
| 450 |
|
| 451 |
if ( bld->abc->type == eslDNA || bld->abc->type == eslRNA ) { |
| 452 |
if (bld->w_len > 0) hmm->max_length = bld->w_len; |
| 453 |
else if (bld->w_beta == 0.0) hmm->max_length = hmm->M *4; |
| 454 |
else if ( (status = p7_Builder_MaxLength(hmm, bld->w_beta)) != eslOK) goto ERROR; |
| 455 |
} |
| 456 |
|
| 457 |
hmm->checksum = checksum; |
| 458 |
hmm->flags |= p7H_CHKSUM; |
| 459 |
|
| 460 |
if (opt_hmm != NULL) *opt_hmm = hmm; else p7_hmm_Destroy(hmm); |
| 461 |
if (opt_trarr != NULL) *opt_trarr = tr; else p7_trace_DestroyArray(tr, msa->nseq); |
| 462 |
return eslOK; |
| 463 |
|
| 464 |
ERROR: |
| 465 |
p7_hmm_Destroy(hmm); |
| 466 |
p7_trace_DestroyArray(tr, msa->nseq); |
| 467 |
if (opt_gm != NULL) p7_profile_Destroy(*opt_gm); |
| 468 |
if (opt_om != NULL) p7_oprofile_Destroy(*opt_om); |
| 469 |
return status; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
int |
| 496 |
p7_SingleBuilder(P7_BUILDER *bld, ESL_SQ *sq, P7_BG *bg, P7_HMM **opt_hmm, |
| 497 |
P7_TRACE **opt_tr, P7_PROFILE **opt_gm, P7_OPROFILE **opt_om) |
| 498 |
{ |
| 499 |
P7_HMM *hmm = NULL; |
| 500 |
P7_TRACE *tr = NULL; |
| 501 |
int k; |
| 502 |
int status; |
| 503 |
|
| 504 |
bld->errbuf[0] = '\0'; |
| 505 |
if (! bld->Q) ESL_XEXCEPTION(eslEINVAL, "score system not initialized"); |
| 506 |
|
| 507 |
if ((status = p7_Seqmodel(bld->abc, sq->dsq, sq->n, sq->name, bld->Q, bg->f, bld->popen, bld->pextend, &hmm)) != eslOK) goto ERROR; |
| 508 |
if ((status = p7_hmm_SetComposition(hmm)) != eslOK) goto ERROR; |
| 509 |
if ((status = p7_hmm_SetConsensus(hmm, sq)) != eslOK) goto ERROR; |
| 510 |
if ((status = calibrate(bld, hmm, bg, opt_gm, opt_om)) != eslOK) goto ERROR; |
| 511 |
|
| 512 |
if ( bld->abc->type == eslDNA || bld->abc->type == eslRNA ) { |
| 513 |
if (bld->w_len > 0) hmm->max_length = bld->w_len; |
| 514 |
else if (bld->w_beta == 0.0) hmm->max_length = hmm->M *4; |
| 515 |
else if ( (status = p7_Builder_MaxLength(hmm, bld->w_beta)) != eslOK) goto ERROR; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
if (opt_tr != NULL) |
| 521 |
{ |
| 522 |
if ((tr = p7_trace_Create()) == NULL) goto ERROR; |
| 523 |
if ((status = p7_trace_Append(tr, p7T_B, 0, 0)) != eslOK) goto ERROR; |
| 524 |
for (k = 1; k <= sq->n; k++) |
| 525 |
if ((status = p7_trace_Append(tr, p7T_M, k, k)) != eslOK) goto ERROR; |
| 526 |
if ((status = p7_trace_Append(tr, p7T_E, 0, 0)) != eslOK) goto ERROR; |
| 527 |
tr->M = sq->n; |
| 528 |
tr->L = sq->n; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
if (opt_hmm != NULL) *opt_hmm = hmm; else p7_hmm_Destroy(hmm); |
| 533 |
if (opt_tr != NULL) *opt_tr = tr; |
| 534 |
return eslOK; |
| 535 |
|
| 536 |
ERROR: |
| 537 |
p7_hmm_Destroy(hmm); |
| 538 |
if (tr != NULL) p7_trace_Destroy(tr); |
| 539 |
if (opt_gm != NULL) p7_profile_Destroy(*opt_gm); |
| 540 |
if (opt_om != NULL) p7_oprofile_Destroy(*opt_om); |
| 541 |
return status; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
int |
| 651 |
p7_Builder_MaxLength (P7_HMM *hmm, double emit_thresh) |
| 652 |
{ |
| 653 |
int col_ptr, prev_col_ptr; |
| 654 |
int col; |
| 655 |
double p_sum; |
| 656 |
double surv; |
| 657 |
int k; |
| 658 |
int i; |
| 659 |
double **I = NULL; |
| 660 |
double **M = NULL; |
| 661 |
double **D = NULL; |
| 662 |
int model_len = hmm->M; |
| 663 |
int length_bound = ESL_MAX(model_len, ESL_MIN(20*model_len, 100000)); |
| 664 |
int status; |
| 665 |
|
| 666 |
if (model_len==1) { |
| 667 |
hmm->max_length = 1; |
| 668 |
return eslOK; |
| 669 |
} |
| 670 |
|
| 671 |
hmm->max_length = length_bound; |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
ESL_ALLOC(I, (model_len+1) * sizeof(double*)); |
| 676 |
ESL_ALLOC(M, (model_len+1) * sizeof(double*)); |
| 677 |
ESL_ALLOC(D, (model_len+1) * sizeof(double*)); |
| 678 |
for (i = 0; i <= model_len; i++) { |
| 679 |
I[i] = M[i] = D[i] = NULL; |
| 680 |
} |
| 681 |
for (i=0; i <= model_len; i++) { |
| 682 |
ESL_ALLOC(I[i], 2 * sizeof(double)); |
| 683 |
ESL_ALLOC(M[i], 2 * sizeof(double)); |
| 684 |
ESL_ALLOC(D[i], 2 * sizeof(double)); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
M[1][0] = 1.0; |
| 690 |
I[1][0] = D[1][0] = M[2][0] = I[2][0] = 0; |
| 691 |
D[2][0] = hmm->t[1][p7H_MD]; |
| 692 |
for (k=3; k<=model_len; k++){ |
| 693 |
M[k][0] = I[k][0] = 0; |
| 694 |
D[k][0] = hmm->t[k-1][p7H_DD] * D[k-1][0]; |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
M[1][1] = D[1][1] = D[2][1] = I[2][1] = 0; |
| 699 |
I[1][1] = hmm->t[1][p7H_MI] * M[1][0]; |
| 700 |
M[2][1] = hmm->t[1][p7H_MM] * M[1][0] ; |
| 701 |
for (k=3; k<=model_len; k++){ |
| 702 |
M[k][1] = hmm->t[k-1][p7H_DM] * D[k-1][0] ; |
| 703 |
I[k][1] = 0; |
| 704 |
D[k][1] = hmm->t[k-1][p7H_MD] * M[k-1][1] + hmm->t[k-1][p7H_DD] * D[k-1][1]; |
| 705 |
} |
| 706 |
|
| 707 |
p_sum = M[model_len][0] + M[model_len][1] + D[model_len][0] + D[model_len][1]; |
| 708 |
|
| 709 |
|
| 710 |
col_ptr = 0; |
| 711 |
for (col=3; col<=length_bound; col++) { |
| 712 |
prev_col_ptr = 1-col_ptr; |
| 713 |
surv = 0.0; |
| 714 |
M[1][col_ptr] = D[1][col_ptr] = 0; |
| 715 |
I[1][col_ptr] = hmm->t[1][p7H_II] * I[1][prev_col_ptr]; |
| 716 |
surv += I[1][col_ptr]; |
| 717 |
|
| 718 |
for (k=2; k<=model_len; k++){ |
| 719 |
M[k][col_ptr] = hmm->t[k-1][p7H_MM] * M[k-1][prev_col_ptr] + hmm->t[k-1][p7H_DM] * D[k-1][prev_col_ptr] + hmm->t[k-1][p7H_IM] * I[k-1][prev_col_ptr]; |
| 720 |
I[k][col_ptr] = hmm->t[k][p7H_MI] * M[k][prev_col_ptr] + hmm->t[k][p7H_II] * I[k][prev_col_ptr]; |
| 721 |
D[k][col_ptr] = hmm->t[k-1][p7H_MD] * M[k-1][col_ptr] + hmm->t[k-1][p7H_DD] * D[k-1][col_ptr]; |
| 722 |
|
| 723 |
if (k<=model_len) { |
| 724 |
surv += I[k][col_ptr] + |
| 725 |
M[k][col_ptr] * ( 1 - hmm->t[k][p7H_MD] ) + |
| 726 |
D[k][col_ptr] * ( 1 - hmm->t[k][p7H_DD] ) ; |
| 727 |
} |
| 728 |
} |
| 729 |
surv += M[model_len][col_ptr] * ( hmm->t[model_len][p7H_MD] ) |
| 730 |
+ D[model_len][col_ptr] * ( hmm->t[model_len][p7H_DD] ) |
| 731 |
- I[model_len][col_ptr] ; |
| 732 |
|
| 733 |
p_sum += M[model_len][col_ptr] + D[model_len][col_ptr]; |
| 734 |
surv /= surv + p_sum; |
| 735 |
|
| 736 |
if (surv < emit_thresh) { |
| 737 |
hmm->max_length = col; |
| 738 |
break; |
| 739 |
} |
| 740 |
|
| 741 |
col_ptr = 1-col_ptr; |
| 742 |
} |
| 743 |
|
| 744 |
for (i=0; i<model_len+1; i++) { |
| 745 |
free(I[i]); |
| 746 |
free(M[i]); |
| 747 |
free(D[i]); |
| 748 |
} |
| 749 |
free(I); |
| 750 |
free(M); |
| 751 |
free(D); |
| 752 |
|
| 753 |
if (hmm->max_length > length_bound) return eslERANGE; |
| 754 |
return eslOK; |
| 755 |
|
| 756 |
ERROR: |
| 757 |
if (I) { for (i = 0; i <= model_len; i++) { if (I[i]) free(I[i]); } free(I); } |
| 758 |
if (D) { for (i = 0; i <= model_len; i++) { if (D[i]) free(D[i]); } free(D); } |
| 759 |
if (M) { for (i = 0; i <= model_len; i++) { if (M[i]) free(M[i]); } free(M); } |
| 760 |
return status; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
static int |
| 787 |
validate_msa(P7_BUILDER *bld, ESL_MSA *msa) |
| 788 |
{ |
| 789 |
int idx; |
| 790 |
int64_t apos; |
| 791 |
|
| 792 |
for (idx = 0; idx < msa->nseq; idx++) |
| 793 |
{ |
| 794 |
apos = 1; |
| 795 |
while ( esl_abc_XIsMissing(msa->abc, msa->ax[idx][apos]) && apos <= msa->alen) apos++; |
| 796 |
while (! esl_abc_XIsMissing(msa->abc, msa->ax[idx][apos]) && apos <= msa->alen) apos++; |
| 797 |
while ( esl_abc_XIsMissing(msa->abc, msa->ax[idx][apos]) && apos <= msa->alen) apos++; |
| 798 |
if (apos != msa->alen+1) ESL_FAIL(eslEINVAL, bld->errbuf, "msa %s; sequence %s\nhas missing data chars (~) other than at fragment edges", msa->name, msa->sqname[idx]); |
| 799 |
} |
| 800 |
|
| 801 |
return eslOK; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
static int |
| 809 |
relative_weights(P7_BUILDER *bld, ESL_MSA *msa) |
| 810 |
{ |
| 811 |
ESL_MSAWEIGHT_CFG *cfg = esl_msaweight_cfg_Create(); |
| 812 |
int status = eslOK; |
| 813 |
|
| 814 |
cfg->ignore_rf = (bld->arch_strategy == p7_ARCH_HAND ? FALSE : TRUE); |
| 815 |
|
| 816 |
if (bld->wgt_strategy == p7_WGT_NONE) { esl_vec_DSet(msa->wgt, msa->nseq, 1.); } |
| 817 |
else if (bld->wgt_strategy == p7_WGT_GIVEN) ; |
| 818 |
else if (bld->wgt_strategy == p7_WGT_PB) status = esl_msaweight_PB_adv(cfg, msa, NULL); |
| 819 |
else if (bld->wgt_strategy == p7_WGT_GSC) status = esl_msaweight_GSC(msa); |
| 820 |
else if (bld->wgt_strategy == p7_WGT_BLOSUM) status = esl_msaweight_BLOSUM(msa, bld->wid); |
| 821 |
else ESL_EXCEPTION(eslEINCONCEIVABLE, "no such weighting strategy"); |
| 822 |
|
| 823 |
if (status != eslOK) ESL_FAIL(status, bld->errbuf, "failed to set relative weights in alignment"); |
| 824 |
esl_msaweight_cfg_Destroy(cfg); |
| 825 |
return eslOK; |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
static int |
| 837 |
build_model(P7_BUILDER *bld, ESL_MSA *msa, P7_HMM **ret_hmm, P7_TRACE ***opt_tr) |
| 838 |
{ |
| 839 |
int status; |
| 840 |
|
| 841 |
if (bld->arch_strategy == p7_ARCH_FAST) |
| 842 |
{ |
| 843 |
status = p7_Fastmodelmaker( msa, bld->symfrac, bld, ret_hmm, opt_tr); |
| 844 |
if (status == eslENORESULT) ESL_XFAIL(status, bld->errbuf, "Alignment %s has no consensus columns w/ > %d%% residues - can't build a model.\n", msa->name != NULL ? msa->name : "", (int) (100 * bld->symfrac)); |
| 845 |
else if (status == eslEMEM) ESL_XFAIL(status, bld->errbuf, "Memory allocation failure in model construction.\n"); |
| 846 |
else if (status != eslOK) ESL_XFAIL(status, bld->errbuf, "internal error in model construction.\n"); |
| 847 |
} |
| 848 |
else if (bld->arch_strategy == p7_ARCH_HAND) |
| 849 |
{ |
| 850 |
status = p7_Handmodelmaker( msa, bld, ret_hmm, opt_tr); |
| 851 |
if (status == eslENORESULT) ESL_XFAIL(status, bld->errbuf, "Alignment %s has no annotated consensus columns - can't build a model.\n", msa->name != NULL ? msa->name : ""); |
| 852 |
else if (status == eslEFORMAT) ESL_XFAIL(status, bld->errbuf, "Alignment %s has no reference annotation line\n", msa->name != NULL ? msa->name : ""); |
| 853 |
else if (status == eslEMEM) ESL_XFAIL(status, bld->errbuf, "Memory allocation failure in model construction.\n"); |
| 854 |
else if (status != eslOK) ESL_XFAIL(status, bld->errbuf, "internal error in model construction.\n"); |
| 855 |
} |
| 856 |
return eslOK; |
| 857 |
|
| 858 |
ERROR: |
| 859 |
return status; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
static int |
| 877 |
effective_seqnumber(P7_BUILDER *bld, const ESL_MSA *msa, P7_HMM *hmm, const P7_BG *bg) |
| 878 |
{ |
| 879 |
int status; |
| 880 |
int i; |
| 881 |
|
| 882 |
if (bld->effn_strategy == p7_EFFN_ENTROPY_EXP) { |
| 883 |
double etarget; |
| 884 |
double eff_nseq = 0.0; |
| 885 |
double exp; |
| 886 |
etarget = (bld->esigma - eslCONST_LOG2R * log( 2.0 / ((double) hmm->M * (double) (hmm->M+1)))) / (double) hmm->M; |
| 887 |
etarget = ESL_MAX(bld->re_target, etarget); |
| 888 |
|
| 889 |
status = p7_EntropyWeight_exp(hmm, bg, bld->prior, etarget, &exp); |
| 890 |
if (status == eslEMEM) ESL_XFAIL(status, bld->errbuf, "memory allocation failed"); |
| 891 |
else if (status != eslOK) ESL_XFAIL(status, bld->errbuf, "internal failure in entropy weighting algorithm"); |
| 892 |
|
| 893 |
p7_hmm_ScaleExponential(hmm, exp); |
| 894 |
|
| 895 |
for (i = 1; i <= hmm->M; i++) |
| 896 |
eff_nseq += esl_vec_FSum(hmm->mat[i], hmm->abc->K); |
| 897 |
|
| 898 |
|
| 899 |
eff_nseq /= hmm->M; |
| 900 |
hmm->eff_nseq = eff_nseq; |
| 901 |
|
| 902 |
} else { |
| 903 |
|
| 904 |
if (bld->effn_strategy == p7_EFFN_NONE) hmm->eff_nseq = msa->nseq; |
| 905 |
else if (bld->effn_strategy == p7_EFFN_SET) hmm->eff_nseq = bld->eset; |
| 906 |
else if (bld->effn_strategy == p7_EFFN_CLUST) |
| 907 |
{ |
| 908 |
int nclust; |
| 909 |
|
| 910 |
status = esl_msacluster_SingleLinkage(msa, bld->eid, NULL, NULL, &nclust); |
| 911 |
if (status == eslEMEM) ESL_XFAIL(status, bld->errbuf, "memory allocation failed"); |
| 912 |
else if (status != eslOK) ESL_XFAIL(status, bld->errbuf, "single linkage clustering algorithm (at %d%% id) failed", (int)(100 * bld->eid)); |
| 913 |
|
| 914 |
hmm->eff_nseq = (double) nclust; |
| 915 |
} |
| 916 |
else if (bld->effn_strategy == p7_EFFN_ENTROPY) |
| 917 |
{ |
| 918 |
double etarget; |
| 919 |
double eff_nseq; |
| 920 |
etarget = (bld->esigma - eslCONST_LOG2R * log( 2.0 / ((double) hmm->M * (double) (hmm->M+1)))) / (double) hmm->M; |
| 921 |
etarget = ESL_MAX(bld->re_target, etarget); |
| 922 |
|
| 923 |
status = p7_EntropyWeight(hmm, bg, bld->prior, etarget, &eff_nseq); |
| 924 |
if (status == eslEMEM) ESL_XFAIL(status, bld->errbuf, "memory allocation failed"); |
| 925 |
else if (status != eslOK) ESL_XFAIL(status, bld->errbuf, "internal failure in entropy weighting algorithm"); |
| 926 |
hmm->eff_nseq = eff_nseq; |
| 927 |
} |
| 928 |
|
| 929 |
p7_hmm_Scale(hmm, hmm->eff_nseq / (double) hmm->nseq); |
| 930 |
|
| 931 |
} |
| 932 |
|
| 933 |
return eslOK; |
| 934 |
|
| 935 |
ERROR: |
| 936 |
return status; |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
static int |
| 944 |
parameterize(P7_BUILDER *bld, P7_HMM *hmm) |
| 945 |
{ |
| 946 |
int status; |
| 947 |
|
| 948 |
if ((status = p7_ParameterEstimation(hmm, bld->prior)) != eslOK) ESL_XFAIL(status, bld->errbuf, "parameter estimation failed"); |
| 949 |
|
| 950 |
return eslOK; |
| 951 |
|
| 952 |
ERROR: |
| 953 |
return status; |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
static int |
| 963 |
annotate(P7_BUILDER *bld, const ESL_MSA *msa, P7_HMM *hmm) |
| 964 |
{ |
| 965 |
int status; |
| 966 |
|
| 967 |
|
| 968 |
if (msa->name) p7_hmm_SetName(hmm, msa->name); |
| 969 |
else ESL_XFAIL(eslEINVAL, bld->errbuf, "Unable to name the HMM."); |
| 970 |
|
| 971 |
if ((status = p7_hmm_SetAccession (hmm, msa->acc)) != eslOK) ESL_XFAIL(status, bld->errbuf, "Failed to record MSA accession"); |
| 972 |
if ((status = p7_hmm_SetDescription(hmm, msa->desc)) != eslOK) ESL_XFAIL(status, bld->errbuf, "Failed to record MSA description"); |
| 973 |
|
| 974 |
if ((status = p7_hmm_SetCtime(hmm)) != eslOK) ESL_XFAIL(status, bld->errbuf, "Failed to record timestamp"); |
| 975 |
if ((status = p7_hmm_SetComposition(hmm)) != eslOK) ESL_XFAIL(status, bld->errbuf, "Failed to determine model composition"); |
| 976 |
if ((status = p7_hmm_SetConsensus(hmm, NULL)) != eslOK) ESL_XFAIL(status, bld->errbuf, "Failed to set consensus line"); |
| 977 |
|
| 978 |
if (msa->cutset[eslMSA_GA1]) { |
| 979 |
hmm->cutoff[p7_GA1] = msa->cutoff[eslMSA_GA1]; |
| 980 |
hmm->flags |= p7H_GA; |
| 981 |
if (msa->cutset[eslMSA_GA2]) |
| 982 |
hmm->cutoff[p7_GA2] = msa->cutoff[eslMSA_GA2]; |
| 983 |
} |
| 984 |
if (msa->cutset[eslMSA_TC1]) { |
| 985 |
hmm->cutoff[p7_TC1] = msa->cutoff[eslMSA_TC1]; |
| 986 |
hmm->flags |= p7H_TC; |
| 987 |
if (msa->cutset[eslMSA_TC2]) |
| 988 |
hmm->cutoff[p7_TC2] = msa->cutoff[eslMSA_TC2]; |
| 989 |
} |
| 990 |
if (msa->cutset[eslMSA_NC1]) { |
| 991 |
hmm->cutoff[p7_NC1] = msa->cutoff[eslMSA_NC1]; |
| 992 |
hmm->flags |= p7H_NC; |
| 993 |
if (msa->cutset[eslMSA_NC2]) |
| 994 |
hmm->cutoff[p7_NC2] = msa->cutoff[eslMSA_NC2]; |
| 995 |
} |
| 996 |
|
| 997 |
return eslOK; |
| 998 |
|
| 999 |
ERROR: |
| 1000 |
return status; |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
static int |
| 1010 |
calibrate(P7_BUILDER *bld, P7_HMM *hmm, P7_BG *bg, P7_PROFILE **opt_gm, P7_OPROFILE **opt_om) |
| 1011 |
{ |
| 1012 |
int status; |
| 1013 |
|
| 1014 |
if (opt_gm != NULL) *opt_gm = NULL; |
| 1015 |
if (opt_om != NULL) *opt_om = NULL; |
| 1016 |
|
| 1017 |
if ((status = p7_Calibrate(hmm, bld, &(bld->r), &bg, opt_gm, opt_om)) != eslOK) goto ERROR; |
| 1018 |
return eslOK; |
| 1019 |
|
| 1020 |
ERROR: |
| 1021 |
return status; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
static int |
| 1032 |
make_post_msa(P7_BUILDER *bld, const ESL_MSA *premsa, const P7_HMM *hmm, P7_TRACE **tr, ESL_MSA **opt_postmsa) |
| 1033 |
{ |
| 1034 |
ESL_MSA *postmsa = NULL; |
| 1035 |
int optflags = p7_DEFAULT; |
| 1036 |
int status; |
| 1037 |
|
| 1038 |
if (opt_postmsa == NULL) return eslOK; |
| 1039 |
|
| 1040 |
|
| 1041 |
if ((status = p7_tracealign_MSA(premsa, tr, hmm->M, optflags, &postmsa)) != eslOK) goto ERROR; |
| 1042 |
|
| 1043 |
*opt_postmsa = postmsa; |
| 1044 |
return eslOK; |
| 1045 |
|
| 1046 |
ERROR: |
| 1047 |
if (postmsa != NULL) esl_msa_Destroy(postmsa); |
| 1048 |
return status; |
| 1049 |
} |
| 1050 |
|
| 1051 |
|