Debian Codegraph @

hmmer/src/evalues.c

raw symbols SCIP
1 /* Calculations and simulations relevant to E-value calculations.
2 *
3 * Contents:
4 * 1. p7_Calibrate(): model calibration wrapper
5 * 2. Determination of individual E-value parameters
6 * 3. Statistics and specific experiment drivers
7 * 4. Benchmark driver
8 *
9 * SRE, Mon Aug 6 13:00:06 2007
10 */
11 #include <p7_config.h>
12
13 #include "easel.h"
14 #include "esl_gumbel.h"
15 #include "esl_random.h"
16 #include "esl_randomseq.h"
17 #include "esl_vectorops.h"
18
19 #include "hmmer.h"
20
21 /*****************************************************************
22 * 1. p7_Calibrate(): model calibration wrapper
23 *****************************************************************/
24
25 /* Function: p7_Calibrate()
26 * Synopsis: Calibrate the E-value parameters of a model.
27 * Incept: SRE, Thu Dec 25 09:29:31 2008 [Magallon]
28 *
29 * Purpose: Calibrate the E-value parameters of a model with
30 * one calculation ($\lambda$) and two brief simulations
31 * (Viterbi $\mu$, Forward $\tau$).
32 *
33 * Args: hmm - HMM to be calibrated
34 * cfg_b - OPTCFG: ptr to optional build configuration;
35 * if <NULL>, use default parameters.
36 * byp_rng - BYPASS optimization: pass ptr to <ESL_RANDOMNESS> generator
37 * if already known;
38 * <*byp_rng> == NULL> if <rng> return is desired;
39 * pass <NULL> to use and discard internal default.
40 * byp_bg - BYPASS optimization: pass ptr to <P7_BG> if already known;
41 * <*byp_bg == NULL> if <bg> return is desired;
42 * pass <NULL> to use and discard internal default.
43 * byp_gm - BYPASS optimization: pass ptr to <gm> profile if already known;
44 * pass <*byp_gm == NULL> if <gm> return desired;
45 * pass <NULL> to use and discard internal default.
46 * byp_om - BYPASS optimization: pass ptr to <om> profile if already known;
47 * pass <*byp_om == NULL> if <om> return desired;
48 * pass <NULL> to use and discard internal default.
49 *
50 * Returns: <eslOK> on success.
51 *
52 * Throws: <eslEMEM> on allocation failure.
53 * <eslEINVAL> if <hmm>, <gm>, <om> aren't compatible somehow.
54 *
55 * Xref: J4/41
56 */
57 int
58 p7_Calibrate(P7_HMM *hmm, P7_BUILDER *cfg_b, ESL_RANDOMNESS **byp_rng, P7_BG **byp_bg, P7_PROFILE **byp_gm, P7_OPROFILE **byp_om)
59 {
60 P7_BG *bg = (esl_byp_IsProvided(byp_bg) ? *byp_bg : NULL);
61 P7_PROFILE *gm = (esl_byp_IsProvided(byp_gm) ? *byp_gm : NULL);
62 P7_OPROFILE *om = (esl_byp_IsProvided(byp_om) ? *byp_om : NULL);
63 ESL_RANDOMNESS *r = (esl_byp_IsProvided(byp_rng) ? *byp_rng : NULL);
64 char *errbuf = ((cfg_b != NULL) ? cfg_b->errbuf : NULL);
65 int EmL = ((cfg_b != NULL) ? cfg_b->EmL : 200);
66 int EmN = ((cfg_b != NULL) ? cfg_b->EmN : 200);
67 int EvL = ((cfg_b != NULL) ? cfg_b->EvL : 200);
68 int EvN = ((cfg_b != NULL) ? cfg_b->EvN : 200);
69 int EfL = ((cfg_b != NULL) ? cfg_b->EfL : 100);
70 int EfN = ((cfg_b != NULL) ? cfg_b->EfN : 200);
71 double Eft = ((cfg_b != NULL) ? cfg_b->Eft : 0.04);
72 double lambda, mmu, vmu, tau;
73 int status;
74
75 /* Configure any objects we need
76 * that weren't already passed to us as a bypass optimization
77 */
78 if (r == NULL) {
79 if ((r = esl_randomness_CreateFast(42)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "failed to create RNG");
80 } else if (cfg_b != NULL && cfg_b->do_reseeding) {
81 esl_randomness_Init(r, esl_randomness_GetSeed(r));
82 }
83
84 if (bg == NULL) {
85 if ((bg = p7_bg_Create(hmm->abc)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "failed to allocate background");
86 }
87
88 /* there's an odd case where the <om> is provided and a <gm> isn't going to be returned
89 * where we don't need a <gm> at all, and <gm> stays <NULL> after the next block.
90 * Note that the <EvL> length in the ProfileConfig doesn't matter; the individual
91 * calibration routines MSVMu(), etc. contain their own length reconfig calls.
92 */
93 if ((esl_byp_IsInternal(byp_gm) && ! esl_byp_IsProvided(byp_om)) || esl_byp_IsReturned(byp_gm)) {
94 if ( (gm = p7_profile_Create(hmm->M, hmm->abc)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "failed to allocate profile");
95 if ( (status = p7_ProfileConfig(hmm, bg, gm, EvL, p7_LOCAL)) != eslOK) ESL_XFAIL(status, errbuf, "failed to configure profile");
96 }
97
98 if (om == NULL) {
99 if ((om = p7_oprofile_Create(hmm->M, hmm->abc)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "failed to create optimized profile");
100 if ((status = p7_oprofile_Convert(gm, om)) != eslOK) ESL_XFAIL(status, errbuf, "failed to convert to optimized profile");
101 }
102
103 /* The calibration steps themselves */
104 if ((status = p7_Lambda(hmm, bg, &lambda)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine lambda");
105 if ((status = p7_MSVMu (r, om, bg, EmL, EmN, lambda, &mmu)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine msv mu");
106 if ((status = p7_ViterbiMu(r, om, bg, EvL, EvN, lambda, &vmu)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine vit mu");
107 if ((status = p7_Tau (r, om, bg, EfL, EfN, lambda, Eft, &tau)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine fwd tau");
108
109 /* Store results */
110 hmm->evparam[p7_MLAMBDA] = om->evparam[p7_MLAMBDA] = lambda;
111 hmm->evparam[p7_VLAMBDA] = om->evparam[p7_VLAMBDA] = lambda;
112 hmm->evparam[p7_FLAMBDA] = om->evparam[p7_FLAMBDA] = lambda;
113 hmm->evparam[p7_MMU] = om->evparam[p7_MMU] = mmu;
114 hmm->evparam[p7_VMU] = om->evparam[p7_VMU] = vmu;
115 hmm->evparam[p7_FTAU] = om->evparam[p7_FTAU] = tau;
116 hmm->flags |= p7H_STATS;
117
118 if (gm != NULL) {
119 gm->evparam[p7_MLAMBDA] = lambda;
120 gm->evparam[p7_VLAMBDA] = lambda;
121 gm->evparam[p7_FLAMBDA] = lambda;
122 gm->evparam[p7_MMU] = mmu;
123 gm->evparam[p7_VMU] = vmu;
124 gm->evparam[p7_FTAU] = tau;
125 }
126
127 if (byp_rng != NULL) *byp_rng = r; else esl_randomness_Destroy(r); /* bypass convention: no-op if rng was provided.*/
128 if (byp_bg != NULL) *byp_bg = bg; else p7_bg_Destroy(bg); /* bypass convention: no-op if bg was provided. */
129 if (byp_gm != NULL) *byp_gm = gm; else p7_profile_Destroy(gm); /* bypass convention: no-op if gm was provided. */
130 if (byp_om != NULL) *byp_om = om; else p7_oprofile_Destroy(om); /* bypass convention: no-op if om was provided. */
131 return eslOK;
132
133 ERROR:
134 if (! esl_byp_IsProvided(byp_rng)) esl_randomness_Destroy(r);
135 if (! esl_byp_IsProvided(byp_bg)) p7_bg_Destroy(bg);
136 if (! esl_byp_IsProvided(byp_gm)) p7_profile_Destroy(gm);
137 if (! esl_byp_IsProvided(byp_om)) p7_oprofile_Destroy(om);
138 return status;
139 }
140 /*---------------------- end, wrapper API -----------------------*/
141
142
143
144
145
146 /*****************************************************************
147 * 2. Determination of individual E-value parameters
148 *****************************************************************/
149
150 /* Function: p7_Lambda()
151 * Synopsis: Determines length-corrected local lambda parameter.
152 * Incept: SRE, Wed Aug 8 17:54:55 2007 [Janelia]
153 *
154 * Purpose: Determine the effective scale parameter $\hat{\lambda}$ to
155 * use for model <hmm>. This will be applied both to
156 * Viterbi Gumbel distributions and Forward exponential
157 * tails.
158 *
159 * The 'true' $\lambda$ is always $\log 2 = 0.693$. The effective
160 * lambda is corrected for edge effect, using the equation
161 *
162 * \[
163 * \hat{\lambda} = \lambda + \frac{1.44}{MH}
164 * \]
165 *
166 * where $M$ is the model length and $H$ is the model
167 * relative entropy. The model relative entropy is
168 * approximated by the average relative entropy of match
169 * emission distributions. The 1.44 is an empirically
170 * determined fudge factor [J1/125]. This edge-effect
171 * correction is based largely on \citep{Altschul01},
172 * except for the fudge factor, which we don't understand
173 * and can't theoretically justify.
174 *
175 * Args: hmm : model to calculate corrected lambda for
176 * bg : null model (source of background frequencies)
177 * ret_lambda : RETURN: edge-corrected lambda
178 *
179 * Returns: <eslOK> on success, and <*ret_lambda> is the result.
180 *
181 * Throws: (no abnormal error conditions)
182 */
183 int
184 p7_Lambda(P7_HMM *hmm, P7_BG *bg, double *ret_lambda)
185 {
186 double H = p7_MeanMatchRelativeEntropy(hmm, bg);
187
188 *ret_lambda = eslCONST_LOG2 + 1.44 / ((double) hmm->M * H);
189 return eslOK;
190 }
191
192
193 /* Function: p7_MSVMu()
194 * Synopsis: Determines the local MSV Gumbel mu parameter for a model.
195 * Incept: SRE, Mon Aug 6 13:00:57 2007 [Janelia]
196 *
197 * Purpose: Given model <om> configured for local alignment (typically
198 * multihit, but may be unihit), determine the Gumbel
199 * location parameter $\mu$ for MSV scores by brief simulation. The
200 * simulation generates <N> random sequences of length <L>
201 * using background frequencies in the null model <bg> and
202 * the random number generator <r>; scores them with <gm>
203 * and <bg> with the MSV algorithm; and fits the
204 * resulting distribution to a Gumbel of assumed <lambda>.
205 *
206 * Typical default choices are L=100, N=200, which gives
207 * $\hat{\mu}$ estimates with precision (standard
208 * deviation) of $\pm$ 0.1 bits, corresponding to an error
209 * of $\pm$ 8\% in E-value estimates. [J1/135]. (Default L
210 * was later increased to 200 to improve length dependence
211 * slightly.)
212 *
213 * This function changes the length configuration of both
214 * <om> and <bg>. The caller must remember to reconfigure
215 * both of their length models appropriately for any
216 * subsequent alignments.
217 *
218 * Args: r : source of random numbers
219 * om : score profile (length config is changed upon return!)
220 * bg : null model (length config is changed upon return!)
221 * L : length of sequences to simulate
222 * N : number of sequences to simulate
223 * lambda : known Gumbel lambda parameter
224 * ret_mmu : RETURN: ML estimate of location param mu
225 *
226 * Returns: <eslOK> on success, and <ret_mu> contains the ML estimate
227 * of $\mu$.
228 *
229 * Throws: (no abnormal error conditions)
230 *
231 * Note: The FitCompleteLoc() function is simple, and it's tempting
232 * to inline it here and save the <xv> working memory. However,
233 * the FitCompleteLoc() function is vulnerable
234 * to under/overflow error, and we'll probably fix it
235 * eventually - need to be sure that fix applies here too.
236 */
237 int
238 p7_MSVMu(ESL_RANDOMNESS *r, P7_OPROFILE *om, P7_BG *bg, int L, int N, double lambda, double *ret_mmu)
239 {
240 P7_OMX *ox = p7_omx_Create(om->M, 0, 0); /* DP matrix: 1 row version */
241 ESL_DSQ *dsq = NULL;
242 double *xv = NULL;
243 int i;
244 float sc, nullsc;
245 float maxsc = (255 - om->base_b) / om->scale_b; /* if score overflows, use this */
246 int status;
247
248 if (ox == NULL) { status = eslEMEM; goto ERROR; }
249 ESL_ALLOC(xv, sizeof(double) * N);
250 ESL_ALLOC(dsq, sizeof(ESL_DSQ) * (L+2));
251
252 p7_oprofile_ReconfigLength(om, L);
253 p7_bg_SetLength(bg, L);
254
255 for (i = 0; i < N; i++)
256 {
257 if ((status = esl_rsq_xfIID(r, bg->f, om->abc->K, L, dsq)) != eslOK) goto ERROR;
258 if ((status = p7_bg_NullOne(bg, dsq, L, &nullsc)) != eslOK) goto ERROR;
259
260 status = p7_MSVFilter(dsq, L, om, ox, &sc);
261 if (status == eslERANGE) { sc = maxsc; status = eslOK; }
262 if (status != eslOK) goto ERROR;
263
264 xv[i] = (sc - nullsc) / eslCONST_LOG2;
265 }
266
267 if ((status = esl_gumbel_FitCompleteLoc(xv, N, lambda, ret_mmu)) != eslOK) goto ERROR;
268 p7_omx_Destroy(ox);
269 free(xv);
270 free(dsq);
271 return eslOK;
272
273 ERROR:
274 *ret_mmu = 0.0;
275 if (ox != NULL) p7_omx_Destroy(ox);
276 if (xv != NULL) free(xv);
277 if (dsq != NULL) free(dsq);
278 return status;
279
280 }
281
282 /* Function: p7_ViterbiMu()
283 * Synopsis: Determines the local Viterbi Gumbel mu parameter for a model.
284 * Incept: SRE, Tue May 19 10:26:19 2009 [Janelia]
285 *
286 * Purpose: Identical to p7_MSVMu(), above, except that it fits
287 * Viterbi scores instead of MSV scores.
288 *
289 * The difference between the two mus is small, but can be
290 * up to ~1 bit or so for large, low-info models [J4/126] so
291 * decided to calibrate the two mus separately [J5/8].
292 *
293 * Args: r : source of random numbers
294 * om : score profile (length config is changed upon return!)
295 * bg : null model (length config is changed upon return!)
296 * L : length of sequences to simulate
297 * N : number of sequences to simulate
298 * lambda : known Gumbel lambda parameter
299 * ret_vmu : RETURN: ML estimate of location param mu
300 *
301 * Returns: <eslOK> on success, and <ret_mu> contains the ML estimate
302 * of $\mu$.
303 *
304 * Throws: (no abnormal error conditions)
305 */
306 int
307 p7_ViterbiMu(ESL_RANDOMNESS *r, P7_OPROFILE *om, P7_BG *bg, int L, int N, double lambda, double *ret_vmu)
308 {
309 P7_OMX *ox = p7_omx_Create(om->M, 0, 0); /* DP matrix: 1 row version */
310 ESL_DSQ *dsq = NULL;
311 double *xv = NULL;
312 int i;
313 float sc, nullsc;
314 float maxsc = (32767.0 - om->base_w) / om->scale_w; /* if score overflows, use this [J4/139] */
315 int status;
316
317 if (ox == NULL) { status = eslEMEM; goto ERROR; }
318 ESL_ALLOC(xv, sizeof(double) * N);
319 ESL_ALLOC(dsq, sizeof(ESL_DSQ) * (L+2));
320
321 p7_oprofile_ReconfigLength(om, L);
322 p7_bg_SetLength(bg, L);
323
324 for (i = 0; i < N; i++)
325 {
326 if ((status = esl_rsq_xfIID(r, bg->f, om->abc->K, L, dsq)) != eslOK) goto ERROR;
327 if ((status = p7_bg_NullOne(bg, dsq, L, &nullsc)) != eslOK) goto ERROR;
328
329 status = p7_ViterbiFilter(dsq, L, om, ox, &sc);
330 if (status == eslERANGE) { sc = maxsc; status = eslOK; }
331 if (status != eslOK) goto ERROR;
332
333 xv[i] = (sc - nullsc) / eslCONST_LOG2;
334 }
335
336 if ((status = esl_gumbel_FitCompleteLoc(xv, N, lambda, ret_vmu)) != eslOK) goto ERROR;
337 p7_omx_Destroy(ox);
338 free(xv);
339 free(dsq);
340 return eslOK;
341
342 ERROR:
343 *ret_vmu = 0.0;
344 if (ox != NULL) p7_omx_Destroy(ox);
345 if (xv != NULL) free(xv);
346 if (dsq != NULL) free(dsq);
347 return status;
348
349 }
350
351
352 /* Function: p7_Tau()
353 * Synopsis: Determine Forward tau by brief simulation.
354 * Incept: SRE, Thu Aug 9 15:08:39 2007 [Janelia]
355 *
356 * Purpose: Determine the <tau> parameter for an exponential tail fit
357 * to the Forward score distribution for model <om>, on
358 * random sequences with the composition of the background
359 * model <bg>. This <tau> parameter is for an exponential
360 * distribution anchored from $P=1.0$, so it's not really a
361 * tail per se; but it's only an accurate fit in the tail
362 * of the Forward score distribution, from about $P=0.001$
363 * or so.
364 *
365 * The determination of <tau> is done by a brief simulation
366 * in which we fit a Gumbel distribution to a small number
367 * of Forward scores of random sequences, and use that to
368 * predict the location of the tail at probability <tailp>.
369 *
370 * The Gumbel is of course inaccurate, but we can use it
371 * here solely as an empirical distribution to determine
372 * the location of a reasonable <tau> more accurately on a
373 * smaller number of samples than we could do with raw
374 * order statistics.
375 *
376 * Typical choices are L=100, N=200, tailp=0.04, which
377 * typically yield estimates $\hat{\mu}$ with a precision
378 * (standard deviation) of $\pm$ 0.2 bits, corresponding to
379 * a $\pm$ 15\% error in E-values. See [J1/135].
380 *
381 * The use of Gumbel fitting to a small number of $N$
382 * samples and the extrapolation of $\hat{\mu}$ from the
383 * estimated location of the 0.04 tail mass are both
384 * empirical and carefully optimized against several
385 * tradeoffs. Most importantly, around this choice of tail
386 * probability, a systematic error introduced by the use of
387 * the Gumbel fit is being cancelled by systematic error
388 * introduced by the use of a higher tail probability than
389 * the regime in which the exponential tail is a valid
390 * approximation. See [J1/135] for discussion.
391 *
392 * This function changes the length configuration of both
393 * <om> and <bg>. The caller must remember to reconfigure
394 * both of their length models appropriately for any
395 * subsequent alignments.
396 *
397 * Args: r : source of randomness
398 * om : configured profile to sample sequences from
399 * bg : null model (for background residue frequencies)
400 * L : mean length model for seq emission from profile
401 * N : number of sequences to generate
402 * lambda : expected slope of the exponential tail (from p7_Lambda())
403 * tailp : tail mass from which we will extrapolate mu
404 * ret_mu : RETURN: estimate for the Forward mu (base of exponential tail)
405 *
406 * Returns: <eslOK> on success, and <*ret_fv> is the score difference
407 * in bits.
408 *
409 * Throws: <eslEMEM> on allocation error, and <*ret_fv> is 0.
410 */
411 int
412 p7_Tau(ESL_RANDOMNESS *r, P7_OPROFILE *om, P7_BG *bg, int L, int N, double lambda, double tailp, double *ret_tau)
413 {
414 P7_OMX *ox = p7_omx_Create(om->M, 0, L); /* DP matrix: for ForwardParser, L rows */
415 ESL_DSQ *dsq = NULL;
416 double *xv = NULL;
417 float fsc, nullsc;
418 double gmu, glam;
419 int status;
420 int i;
421
422 ESL_ALLOC(xv, sizeof(double) * N);
423 ESL_ALLOC(dsq, sizeof(ESL_DSQ) * (L+2));
424 if (ox == NULL) { status = eslEMEM; goto ERROR; }
425
426 p7_oprofile_ReconfigLength(om, L);
427 p7_bg_SetLength(bg, L);
428
429 for (i = 0; i < N; i++)
430 {
431 if ((status = esl_rsq_xfIID(r, bg->f, om->abc->K, L, dsq)) != eslOK) goto ERROR;
432 if ((status = p7_ForwardParser(dsq, L, om, ox, &fsc)) != eslOK) goto ERROR;
433 if ((status = p7_bg_NullOne(bg, dsq, L, &nullsc)) != eslOK) goto ERROR;
434 xv[i] = (fsc - nullsc) / eslCONST_LOG2;
435 }
436 if ((status = esl_gumbel_FitComplete(xv, N, &gmu, &glam)) != eslOK) goto ERROR;
437
438 /* Explanation of the eqn below: first find the x at which the Gumbel tail
439 * mass is predicted to be equal to tailp. Then back up from that x
440 * by log(tailp)/lambda to set the origin of the exponential tail to 1.0
441 * instead of tailp.
442 */
443 *ret_tau = esl_gumbel_invcdf(1.0-tailp, gmu, glam) + (log(tailp) / lambda);
444
445 free(xv);
446 free(dsq);
447 p7_omx_Destroy(ox);
448 return eslOK;
449
450 ERROR:
451 *ret_tau = 0.;
452 if (xv != NULL) free(xv);
453 if (dsq != NULL) free(dsq);
454 if (ox != NULL) p7_omx_Destroy(ox);
455 return status;
456 }
457 /*-------------- end, determining individual parameters ---------*/
458
459
460
461
462 /*****************************************************************
463 * 3. Statistics and specific experiment drivers
464 *****************************************************************/
465 #ifdef p7EVALUES_STATS
466 /* gcc -o evalues_stats -g -O2 -msse2 -I. -L. -I../easel -L../easel -Dp7EVALUES_STATS evalues.c -lhmmer -leasel -lm
467 * ./evalues_stats <hmmfile>
468 */
469 /* The J1/135 experiment determining precision of mu, tau estimates can be done with this driver by setting Z=1000 or so.
470 * There used to be a separate script, tagged p7EXP_J1_135, to specifically run that experiment.
471 */
472 #include <p7_config.h>
473
474 #include "easel.h"
475 #include "esl_getopts.h"
476 #include "esl_random.h"
477 #include "esl_alphabet.h"
478
479 #include "hmmer.h"
480
481 #define BMARKS "--msvonly,--vitonly,--fwdonly"
482
483 static ESL_OPTIONS options[] = {
484 /* name type default env range toggles reqs incomp help docgroup*/
485 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
486 { "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number generator seed to <n>", 0 },
487 { "--EmL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for MSV Gumbel mu fit", 0 },
488 { "--EmN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for MSV Gumbel mu fit", 0 },
489 { "--EvL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Viterbi Gumbel mu fit", 0 },
490 { "--EvN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Viterbi Gumbel mu fit", 0 },
491 { "--EfL", eslARG_INT, "100", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Forward exp tail tau fit",0 },
492 { "--EfN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Forward exp tail tau fit",0 },
493 { "--Eft", eslARG_REAL, "0.04", NULL,"0<x<1",NULL, NULL, NULL, "tail mass for Forward exponential tail tau fit", 0 },
494 { "-Z", eslARG_INT, "1", NULL, NULL, NULL, NULL, NULL, "set number of iterations per model to <n>", 0 },
495 { "--lambda", eslARG_REAL, NULL, NULL, NULL, NULL, NULL, NULL, "set lambda param to <x>", 0 },
496 { "--msvonly", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,BMARKS, "only run MSV mu calibration (for benchmarking)", 0 },
497 { "--vitonly", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,BMARKS, "only run Vit mu calibration (for benchmarking)", 0 },
498 { "--fwdonly", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,BMARKS, "only run Fwd tau calibration (for benchmarking)", 0 },
499 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
500 };
501 static char usage[] = "[-options] <hmmfile>";
502 static char banner[] = "collect test statistics for E-value calculations";
503
504 int
505 main(int argc, char **argv)
506 {
507 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
508 char *hmmfile = esl_opt_GetArg(go, 1);
509 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
510 ESL_ALPHABET *abc = NULL;
511 P7_HMMFILE *hfp = NULL;
512 P7_HMM *hmm = NULL;
513 P7_BG *bg = NULL;
514 P7_PROFILE *gm = NULL;
515 P7_OPROFILE *om = NULL;
516 double lambda = 0.0;
517 double mmu = 0.0;
518 double vmu = 0.0;
519 double ftau = 0.0;
520 int Z = esl_opt_GetInteger(go, "-Z");
521 int EmL = esl_opt_GetInteger(go, "--EmL");
522 int EmN = esl_opt_GetInteger(go, "--EmN");
523 int EvL = esl_opt_GetInteger(go, "--EvL");
524 int EvN = esl_opt_GetInteger(go, "--EvN");
525 int EfL = esl_opt_GetInteger(go, "--EfL");
526 int EfN = esl_opt_GetInteger(go, "--EfN");
527 int Eft = esl_opt_GetReal (go, "--Eft");
528 int iteration;
529 int do_msv, do_vit, do_fwd;
530 int status;
531
532 if (esl_opt_GetBoolean(go, "--msvonly") == TRUE) { do_msv = TRUE; do_vit = FALSE; do_fwd = FALSE; }
533 else if (esl_opt_GetBoolean(go, "--vitonly") == TRUE) { do_msv = FALSE; do_vit = TRUE; do_fwd = FALSE; }
534 else if (esl_opt_GetBoolean(go, "--fwdonly") == TRUE) { do_msv = FALSE; do_vit = FALSE; do_fwd = TRUE; }
535 else { do_msv = TRUE; do_vit = TRUE; do_fwd = TRUE; }
536
537 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
538 while ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) != eslEOF)
539 {
540 if (bg == NULL) bg = p7_bg_Create(abc);
541 gm = p7_profile_Create(hmm->M, abc);
542 p7_ProfileConfig(hmm, bg, gm, EvL, p7_LOCAL); /* the EvL doesn't matter */
543 om = p7_oprofile_Create(hmm->M, abc);
544 p7_oprofile_Convert(gm, om);
545
546 if (esl_opt_IsOn(go, "--lambda")) lambda = esl_opt_GetReal(go, "--lambda");
547 else p7_Lambda(hmm, bg, &lambda);
548
549 for (iteration = 0; iteration < Z; iteration++)
550 {
551 if (do_msv) p7_MSVMu (r, om, bg, EmL, EmN, lambda, &mmu);
552 if (do_vit) p7_ViterbiMu (r, om, bg, EvL, EvN, lambda, &vmu);
553 if (do_fwd) p7_Tau (r, om, bg, EfL, EfN, lambda, Eft, &ftau);
554
555 printf("%s %.4f %.4f %.4f %.4f\n", hmm->name, lambda, mmu, vmu, ftau);
556 }
557
558 p7_hmm_Destroy(hmm);
559 p7_profile_Destroy(gm);
560 p7_oprofile_Destroy(om);
561 }
562
563 p7_hmmfile_Close(hfp);
564 p7_bg_Destroy(bg);
565 esl_alphabet_Destroy(abc);
566 esl_randomness_Destroy(r);
567 esl_getopts_Destroy(go);
568 return eslOK;
569 }
570 #endif /*p7EVALUES_STATS*/
571 /*----------------- end, stats/experiment drivers ---------------*/
572
573
574 /*****************************************************************
575 * 4. Benchmark driver
576 *****************************************************************/
577
578 #ifdef p7EVALUES_BENCHMARK
579 /*
580 * gcc -O3 -malign-double -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
581 * gcc -g -O -pg -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
582 * gcc -g -Wall -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
583 *
584 * ./evalues-benchmark <hmmfile>
585 *
586 * -malign-double is needed for gcc if the rest of HMMER was compiled w/ -malign-double
587 * (i.e., our default gcc optimization)
588 *
589 * 27 Dec 08 on wanderoo: 24 msec per RRM_1 calibration; 37 msec for Caudal_act
590 * profiling shows 75% in Forward; 12% esl_random(); <3% in MSVFilter.
591 */
592 #include <p7_config.h>
593
594 #include "easel.h"
595 #include "esl_alphabet.h"
596 #include "esl_getopts.h"
597 #include "esl_stopwatch.h"
598
599 #include "hmmer.h"
600
601 static ESL_OPTIONS options[] = {
602 /* name type default env range toggles reqs incomp help docgroup*/
603 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
604 { "-N", eslARG_INT, "100", NULL, "n>0", NULL, NULL, NULL, "number of calibrations to do", 0 },
605 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
606 };
607 static char usage[] = "[-options] <hmmfile>";
608 static char banner[] = "benchmark driver for E-value calibration";
609
610 int
611 main(int argc, char **argv)
612 {
613 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
614 char *hmmfile = esl_opt_GetArg(go, 1);
615 int N = esl_opt_GetInteger(go, "-N");
616 ESL_STOPWATCH *w = esl_stopwatch_Create();
617 ESL_ALPHABET *abc = NULL;
618 P7_HMMFILE *hfp = NULL;
619 P7_HMM *hmm = NULL;
620
621 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
622 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
623 p7_hmmfile_Close(hfp);
624
625 esl_stopwatch_Start(w);
626 while (N--)
627 { /* cfg rng bg gm om */
628 p7_Calibrate(hmm, NULL, NULL, NULL, NULL, NULL);
629 }
630 esl_stopwatch_Stop(w);
631 esl_stopwatch_Display(stdout, w, "# CPU time: ");
632
633 p7_hmm_Destroy(hmm);
634 esl_alphabet_Destroy(abc);
635 esl_stopwatch_Destroy(w);
636 esl_getopts_Destroy(go);
637 return 0;
638 }
639 #endif /*p7EVALUES_BENCHMARK*/
640
641