Debian Codegraph @

hmmer/src/p7_builder.c

raw symbols SCIP
1 /* Standardized pipeline for construction of new HMMs.
2 *
3 * Contents:
4 * 1. P7_BUILDER: allocation, initialization, destruction
5 * 2. Standardized model construction API.
6 * 3. Internal functions.
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 * 1. P7_BUILDER: allocation, initialization, destruction
27 *****************************************************************/
28
29 /* Function: p7_builder_Create()
30 * Synopsis: Create a default HMM construction configuration.
31 *
32 * Purpose: Create a construction configuration for building
33 * HMMs in alphabet <abc>, and return a pointer to it.
34 *
35 * An application configuration <go> may optionally be
36 * provided. If <go> is <NULL>, default parameters are
37 * used. If <go> is non-<NULL>, it must include appropriate
38 * settings for all of the following ``standard build options'':
39 *
40 * Model construction: --fast --hand --symfrac --fragthresh
41 * Relative weighting: --wgsc --wblosum --wpb --wgiven --wid
42 * Effective seq #: --eent --eclust --enone --eset --ere --esigma --eid
43 * Prior scheme: --pnone --plaplace
44 * E-val calibration: --EmL --EmN --EvL --EvN --EfL --EfN --Eft
45 * run-to-run variation: --seed
46 *
47 * See <hmmbuild.c> or other big users of the build
48 * pipeline for an example of appropriate <ESL_GETOPTS>
49 * initializations of these 24 options.
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; /* -1.0 = unset; must be set if effn_strategy is p7_EFFN_SET */
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 /* The default RE target is alphabet dependent. */
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 /* Normally we reinitialize the RNG to original seed before calibrating each model.
122 * This eliminates run-to-run variation.
123 * As a special case, seed==0 means choose an arbitrary seed and shut off the
124 * reinitialization; this allows run-to-run variation.
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 /* Function: p7_builder_LoadScoreSystem()
159 * Synopsis: Load a standard score system for single sequence queries.
160 *
161 * Purpose: Initialize the builder <bld> to be able to parameterize
162 * single sequence queries, using the standard (built-in) score
163 * matrix named <mx>.
164 *
165 * Available score matrices <mx> include PAM30, 70, 120, and 240;
166 * and BLOSUM45, 50, 62, 80, and 90. See <esl_scorematrix.c>.
167 *
168 * Set the gap-open and gap-extend probabilities to
169 * <popen>, <pextend>, respectively.
170 *
171 * Use background residue frequencies in the null model
172 * <bg> to convert substitution matrix scores to
173 * conditional probability parameters.
174 *
175 * Args: bld - <P7_BUILDER> to initialize
176 * matrix - score matrix file to use
177 * popen - gap open probability
178 * pextend - gap extend probability
179 * bg - null model, containing background frequencies
180 *
181 * Returns: <eslOK> on success.
182 *
183 * <eslENOTFOUND> if <mxfile> can't be found or opened, even
184 * in any of the directories specified by the <env> variable.
185 *
186 * <eslEINVAL> if the score matrix can't be converted into
187 * conditional probabilities; for example, if it has no valid
188 * solution for <lambda>.
189 *
190 * On either error, <bld->errbuf> contains a useful error message
191 * for the user.
192 *
193 * Throws: <eslEMEM> on allocation failure.
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 /* If a score system is already set, delete it. */
205 if (bld->S != NULL) esl_scorematrix_Destroy(bld->S);
206 if (bld->Q != NULL) esl_dmatrix_Destroy(bld->Q);
207
208 /* Get the scoring matrix */
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 /* A wasteful conversion of the HMMER single-precision background probs to Easel double-prec */
215 ESL_ALLOC(f, sizeof(double) * bg->abc->K);
216 esl_vec_F2D(bg->f, bg->abc->K, f);
217
218 /* Backcalculate joint probability matrix Q, given scores S and background freqs bg->f. */
219 /* Failures shouldn't happen here: these are standard matrices. */
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 /* Convert joint probabilities P(ab) to conditionals P(b|a) */
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 /* Function: p7_builder_SetScoreSystem()
240 * Synopsis: Initialize score system for single sequence queries.
241 *
242 * Purpose: Initialize the builder <bld> to be able to parameterize
243 * single sequence queries, using a substitution matrix
244 * from a file.
245 *
246 * Read a standard substitution score matrix from file
247 * <mxfile>. If <mxfile> is <NULL>, default to BLOSUM62
248 * scores. If <mxfile> is "-", read score matrix from
249 * <stdin> stream. If <env> is non-<NULL> and <mxfile> is
250 * not found in the current working directory, look for
251 * <mxfile> in colon-delimited directory list contained in
252 * environment variable <env>.
253 *
254 * Set the gap-open and gap-extend probabilities to
255 * <popen>, <pextend>, respectively.
256 *
257 * Use background residue frequencies in the null model
258 * <bg> to convert substitution matrix scores to
259 * conditional probability parameters.
260 *
261 * Args: bld - <P7_BUILDER> to initialize
262 * mxfile - score matrix file to use, or NULL for BLOSUM62 default
263 * env - env variable containing directory list where <mxfile> may reside
264 * popen - gap open probability
265 * pextend - gap extend probability
266 * bg - null model, containing background frequencies
267 *
268 * Returns: <eslOK> on success.
269 *
270 * <eslENOTFOUND> if <mxfile> can't be found or opened, even
271 * in any of the directories specified by the <env> variable.
272 *
273 * <eslEINVAL> if the score matrix can't be converted into
274 * conditional probabilities; for example, if it has no valid
275 * solution for <lambda>.
276 *
277 * On either error, <bld->errbuf> contains a useful error message
278 * for the user.
279 *
280 * Throws: <eslEMEM> on allocation failure.
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 /* If a score system is already set, delete it. */
293 if (bld->S != NULL) esl_scorematrix_Destroy(bld->S);
294 if (bld->Q != NULL) esl_dmatrix_Destroy(bld->Q);
295
296 /* Get the scoring matrix */
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 /* A wasteful conversion of the HMMER single-precision background probs to Easel double-prec */
316 ESL_ALLOC(f, sizeof(double) * bg->abc->K);
317 esl_vec_F2D(bg->f, bg->abc->K, f);
318
319 /* Backcalculate joint probability matrix Q, given scores S and background freqs bg->f. */
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 /* Convert joint probabilities P(ab) to conditionals P(b|a) */
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 /* Function: p7_builder_Destroy()
343 * Synopsis: Free a <P7_BUILDER>
344 *
345 * Purpose: Frees a <P7_BUILDER> object.
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 /*------------------- end, P7_BUILDER ---------------------------*/
361
362
363
364
365 /*****************************************************************
366 * 2. Standardized model construction API.
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 /* Function: p7_Builder()
379 * Synopsis: Build a new HMM from an MSA.
380 *
381 * Purpose: Take the multiple sequence alignment <msa> and a build configuration <bld>,
382 * and build a new HMM.
383 *
384 * Effective sequence number determination and calibration steps require
385 * additionally providing a null model <bg>.
386 *
387 * Args: bld - build configuration
388 * msa - multiple sequence alignment
389 * bg - null model
390 * opt_hmm - optRETURN: new HMM
391 * opt_trarr - optRETURN: array of faux tracebacks, <0..nseq-1>
392 * opt_gm - optRETURN: profile corresponding to <hmm>
393 * opt_om - optRETURN: optimized profile corresponding to <gm>
394 * opt_postmsa - optRETURN: RF-annotated, possibly modified MSA
395 *
396 * Returns: <eslOK> on success. The new HMM is optionally returned in
397 * <*opt_hmm>, along with optional returns of an array of faux tracebacks
398 * for each sequence in <*opt_trarr>, the annotated MSA used to construct
399 * the model in <*opt_postmsa>, a configured search profile in
400 * <*opt_gm>, and an optimized search profile in <*opt_om>. These are
401 * all optional returns because the caller may, for example, be interested
402 * only in an optimized profile, or may only be interested in the HMM.
403 *
404 * Returns <eslENORESULT> if no consensus columns were annotated.
405 * Returns <eslEFORMAT> on MSA format problems, such as a missing RF annotation
406 * line in hand architecture construction. On any returned error,
407 * <bld->errbuf> contains an informative error message.
408 *
409 * Throws: <eslEMEM> on allocation error.
410 * <eslEINVAL> if relative weights couldn't be calculated from <msa>.
411 *
412 * Xref: J4/30.
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; /* checksum calculated for the input MSA. hmmalign --mapali verifies against this. */
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 //Ensures that the weighted-average I->I count <= bld->max_insert_len
433 //(MI currently contains the number of observed insert-starts)
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 //force masked positions to background (it'll be close already, so no relevant impact on weighting)
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 /* Function: p7_SingleBuilder()
474 * Synopsis: Build a new HMM from a single sequence.
475 *
476 * Purpose: Take the sequence <sq> and a build configuration <bld>, and
477 * build a new HMM.
478 *
479 * The single sequence scoring system in the <bld>
480 * configuration must have been previously initialized by
481 * <p7_builder_SetScoreSystem()>.
482 *
483 * Args: bld - build configuration
484 * sq - query sequence
485 * bg - null model (needed to parameterize insert emission probs)
486 * opt_hmm - optRETURN: new HMM
487 * opt_gm - optRETURN: profile corresponding to <hmm>
488 * opt_om - optRETURN: optimized profile corresponding to <gm>
489 *
490 * Returns: <eslOK> on success.
491 *
492 * Throws: <eslEMEM> on allocation error.
493 * <eslEINVAL> if <bld> isn't properly configured somehow.
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 /* build a faux trace: relative to core model (B->M_1..M_L->E) */
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 /* note that <opt_gm> and <opt_om> were already set by calibrate() call above. */
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 /* Function: p7_Builder_MaxLength()
546 *
547 * Purpose: Compute the maximum likely length of an emitted sequence
548 *
549 * Synopsis: Computes a fairly tight upper bound on domain length, by computing the
550 * probability of the model emitting sequences of all lengths up to some
551 * threshold, based on a dynamic-programming approach. See TJW 01/14/2010 notes (p1)
552 *
553 * The idea is to find the length such that all but e.g. 1e-7 sequences emitted
554 * by the model are at most that long. The method conceptually fills in a table of
555 * length at most length_bound (usually 20 * model_length, up to at most 100,000),
556 * though in practice, only two columns are used to store values;
557 *
558 * Letting i correspond to the ith state of the model,
559 * j to a length j of emitted sequence, and
560 * T[i][P7H_*M] := transition prob from *_i to M_{i+1}
561 * T[i][P7H_*I] := transition prob from *_i to I_i
562 * T[i][P7H_*D] := transition prob from *_i to D_{i+1}
563 *
564 *
565 * in general,
566 * M(i,j) = T[i-1][P7H_MM] * M(i-1,j-1) + T[i-1][P7H_DM] * D(i-1,j-1) + T[i-1][P7H_IM] * I(i-1,j-1);
567 * I(i,j) = T[i][P7H_MI] * M(i,j-1) + T[i][P7H_II] * I(i,j-1);
568 * D(i,j) = T[i-1][P7H_MD] * M(i-1,j) + T[i-1][P7H_DD] * D(i-1,j);
569 *
570 * The process of filling in the dp table is done for only the full core model.
571 * We want to minimize memory consumption, so this is handled column-by-column,
572 * storing only 2 columns at a time.
573 *
574 * Initial values must be set.
575 * This is simple:
576 * M(1,1) = 1;
577 * I(1,1) = 0;
578 * D(1,1) = 0;
579 * D(2,1) = md;
580 * Fill in the remainder of rows
581 * M(r,1) = I(r,1) = 0;
582 * D(r,1) = dd * D(r-1,1)
583 *
584 *
585 * Then the next column:
586 * M(1,2) = D(1,2) = 0;
587 * I(1,2) = mi * M(1,1);
588 * I(2,2) = D(2,2) = 0;
589 * M(2,2) = mm * M(1,1);
590 * D(3,2) = md * M(2,2);
591 * Fill in the remainder of rows r:
592 * M(r,2) = dm * M(r-1,1);
593 * D(r,2) = dd * D(r-1,2);
594 * I(r,2) = 0;
595 *
596 *
597 *
598 * Then for each column c after that,
599 * M(1,c) = D(1,c) = 0;
600 * I(1,c) = ii * I(1,c-1)
601 * Fill in the remainder of rows r based on the default formulas above
602 * Then:
603 * M(i,j) = T[i-1][P7H_MM] * M(i-1,j-1) + T[i-1][P7H_DM] * D(i-1,j-1) + T[i-1][P7H_IM] * I(i-1,j-1);
604 * D(i,j) = T[i-1][P7H_MD] * M(i-1,j) + T[i-1][P7H_DD] * D(i-1,j);
605 * I(i,j) = T[i][P7H_MI] * M(i,j-1) + T[i][P7H_II] * I(i,j-1);
606 *
607 *
608 * We aim to find the length W s.t. nearly all (e.g. all but 1e-7) of the sequences
609 * emitted by the model are at most W long. Ideally, we could track the probability
610 * of emitting each length from 0 up, and accumulate those probabilities until the
611 * threshold is met. The probability of seeing a sequence of a given length emitted
612 * by the full model is simply the sum of the D[m] and M[m] values (for a model of
613 * length m). (I[m] is a false value - see below)
614 *
615 * I say "ideally", because numeric instability can lead the sum of all lengths - up
616 * to infinity - to be <0.99999 or >1.0 ... so instead we keep track of two things for
617 * each length L:
618 * (1) the sum of D[m] and M[m] prob masses for all lengths up to L (call this X), and
619 * (2) the amount of the probability mass that belongs to all L-length-emitting states
620 * except the final M/D states. That's the mass that will end up being spread across
621 * all lengths >L (call this Y).
622 *
623 * If not for numeric instability, X+Y=1, and we'd want to stop when Y <= 1e-7. Because
624 * X+Y might not == 1, instead stop when Y/(X+Y) <= 1e-7.
625 *
626 * A note for computing X: the final position in the model does not actually include an
627 * I-state, so all of the final M state's probability mass should go to the E state.
628 * The value in I[m][] will suggest that some of that probability has gone to that state,
629 * but this will be ignored when tallying X = M[m]+D[m].
630 *
631 * A note on the calculation of Y: it's not quite as simple as adding up all pre-m
632 * states. For a given length j, the only way a D[i]-state can emit a sequence of length
633 * j is if an M[k] state emitted that sequence, with k<i. If k<i-1, then other D states
634 * were also involved. The simplest way to account for this is to bleed the part of the
635 * M[i] or D[i] state that gets pushed forward into the next D state. That amount will
636 * end up being accounted for by either that later D state or (for the small part that
637 * bleeds all the way to the mth D state, it'll be added into X via D[m]. In other words:
638 * (1) each M[i] should contribute (1-t_md)M[i] to Y.
639 * (2) each D[i] should contribute (1-t_dd)D[i] to Y.
640 *
641 * If the probability threshold isn't met before reaching length_bound, then MAXL is
642 * simply set to length_bound (usually 20 * model_length).
643 *
644 *
645 * Args: hmm - p7_HMM (required for the transition probabilities)
646 *
647 * Returns: <eslOK> on success. The max length is set in hmm->max_length.
648
649 */
650 int
651 p7_Builder_MaxLength (P7_HMM *hmm, double emit_thresh)
652 {
653 int col_ptr, prev_col_ptr; // which true column in above 2d-arrays is active
654 int col; // which conceptual column in above 2d-arrays is active (up to table_len)
655 double p_sum; // sum of probabilities for lengths <=L; X from above
656 double surv; // surviving probability mass at length L; Y from above
657 int k; // active state in model
658 int i;
659 double **I = NULL;
660 double **M = NULL;
661 double **D = NULL;
662 int model_len = hmm->M; // model length
663 int length_bound = ESL_MAX(model_len, ESL_MIN(20*model_len, 100000)); // cap on # iterations (aka max model length)
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; //default, if it never reaches the target surviving density
672
673
674 // double I[model_len+1][2], M[model_len+1][2], D[model_len+1][2]; //2 columns for each way of ending a subpath
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 /* Compute max length and max prefix lengths*/
688 // special case for filling in 1st column of DP table, col=1;
689 M[1][0] = 1.0;// 1st match state must emit a character
690 I[1][0] = D[1][0] = M[2][0] = I[2][0] = 0;
691 D[2][0] = hmm->t[1][p7H_MD]; // The 2nd delete state is reached, having emitted only 1 character
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]; // only way to get to the 3rd or greater state with only 1 character
695 }
696
697 //special case for 2nd column
698 M[1][1] = D[1][1] = D[2][1] = I[2][1] = 0; //No way any of these states can be responsible for the second emitted character.
699 I[1][1] = hmm->t[1][p7H_MI] * M[1][0]; //1st insert state can emit char #2.
700 M[2][1] = hmm->t[1][p7H_MM] * M[1][0] ; //2nd match state can emit char #2.
701 for (k=3; k<=model_len; k++){
702 M[k][1] = hmm->t[k-1][p7H_DM] * D[k-1][0] ; //kth match state would have to follow the k-1th delete state, having emitted only 1 char so far
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]; //in general only by extending a delete. For k=3, this could be a transition from M=2, with 2 chars.
705 }
706
707 p_sum = M[model_len][0] + M[model_len][1] + D[model_len][0] + D[model_len][1];
708
709 //general case for all remaining columns
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; //M[i][prev_col_ptr] is zero : no way the first M state could have emitted >=2 chars
715 I[1][col_ptr] = hmm->t[1][p7H_II] * I[1][prev_col_ptr]; // 1st insert state can emit chars indefinitely
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] ) + //this much of M[k]'s mass will bleed into D[k+1], and thus be added to surv then
726 D[k][col_ptr] * ( 1 - hmm->t[k][p7H_DD] ) ; //this much of D[k]'s mass will bleed into D[k+1], and thus be added to surv then
727 }
728 }
729 surv += M[model_len][col_ptr] * ( hmm->t[model_len][p7H_MD] ) //the final state doesn't pass on to the next D state
730 + D[model_len][col_ptr] * ( hmm->t[model_len][p7H_DD] ) // the final state doesn't pass on to the next D state
731 - I[model_len][col_ptr] ; // no I state for final position
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; // alternating between 0 and 1
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 /*------------- end, model construction API ---------------------*/
764
765
766
767
768 /*****************************************************************
769 * 3. Internal functions
770 *****************************************************************/
771
772
773 /* validate_msa:
774 * SRE, Thu Dec 3 16:10:31 2009 [J5/119; bug #h70 fix]
775 *
776 * HMMER uses a convention for missing data characters: they
777 * indicate that a sequence is a fragment. (See
778 * esl_msa_MarkFragments_old()).
779 *
780 * Because of the way these fragments will be handled in tracebacks,
781 * we reject any alignment that uses missing data characters in any
782 * other way.
783 *
784 * This validation step costs negligible time.
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 /* set_relative_weights():
806 * Set msa->wgt vector, using user's choice of relative weighting algorithm.
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); // PB weights use RF consensus columns only if --hand is set. [iss #180]
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, /*ESL_MSAWEIGHT_DAT=*/ 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 /* build_model():
830 * Given <msa>, choose HMM architecture, collect counts;
831 * upon return, <*ret_hmm> is newly allocated and contains
832 * relative-weighted observed counts.
833 * Optionally, caller can request an array of inferred traces for
834 * the <msa> too.
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 /* effective_seqnumber()
865 *
866 * <hmm> comes in with weighted observed counts. It goes out with
867 * those observed counts rescaled to sum to the "effective sequence
868 * number".
869 *
870 * <msa> is needed because we may need to see the sequences in order
871 * to determine effective seq #. (for --eclust)
872 *
873 * <prior> is needed because we may need to parameterize test models
874 * looking for the right relative entropy. (for --eent, the default)
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; /* xref J5/36. */
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; /* xref J5/36. */
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 /* parameterize()
941 * Converts counts to probability parameters.
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 /* annotate()
959 * Transfer annotation information from MSA to new HMM.
960 * Also sets model-specific residue composition (hmm->compo).
961 */
962 static int
963 annotate(P7_BUILDER *bld, const ESL_MSA *msa, P7_HMM *hmm)
964 {
965 int status;
966
967 /* Name. */
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 // if ((status = p7_hmm_AppendComlog(hmm, go->argc, go->argv)) != eslOK) ESL_XFAIL(status, errbuf, "Failed to record command log");
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 /* calibrate()
1004 *
1005 * Sets the E value parameters of the model with two short simulations.
1006 * A profile and an oprofile are created here. If caller wants to keep either
1007 * of them, it can pass non-<NULL> <opt_gm>, <opt_om> pointers.
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 /* make_post_msa()
1026 *
1027 * Optionally, we can return the alignment we actually built the model
1028 * from (including RF annotation on assigned consensus columns, and any
1029 * trace doctoring to enforce Plan7 consistency).
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 /* someday we might want to transfer more info from HMM to postmsa */
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 /*---------------- end, internal functions ----------------------*/
1051