Debian Codegraph @

hmmer/src/generic_msv.c

raw symbols SCIP
1 /* MSV algorithm; generic (non-SIMD) version.
2 *
3 * Contents:
4 * 1. MSV implementation.
5 * 2. Benchmark driver.
6 * 3. Unit tests.
7 * 4. Test driver.
8 * 5. Example.
9 *
10 * SRE, Fri Aug 15 10:38:21 2008 [Janelia]
11 */
12
13 #include <p7_config.h>
14
15 #include "easel.h"
16 #include "esl_alphabet.h"
17 #include "esl_gumbel.h"
18
19
20 #include "hmmer.h"
21
22 /*****************************************************************
23 * 1. MSV implementation.
24 *****************************************************************/
25
26 /* Function: p7_GMSV()
27 * Synopsis: The MSV score algorithm (slow, correct version)
28 * Incept: SRE, Thu Dec 27 08:33:39 2007 [Janelia]
29 *
30 * Purpose: Calculates the maximal score of ungapped local segment
31 * pair alignments, taking advantage of the fact that this
32 * is simply equivalent to setting all MM transitions to 1.0
33 * in a multihit local profile.
34 *
35 * Args: dsq - sequence in digitized form, 1..L
36 * L - length of dsq
37 * gm - profile (can be in any mode)
38 * gx - DP matrix with room for an MxL alignment
39 * nu - configuration: expected number of hits (use 2.0 as a default)
40 * opt_sc - optRETURN: MSV lod score in nats.
41 *
42 * Returns: <eslOK> on success.
43 *
44 * Note: This is written deliberately as a modified p7_GViterbi
45 * routine. It could be faster -- we don't need the
46 * interleaved dp matrix or residue scores, since we aren't
47 * calculating D or I states, for example, and we could do
48 * without some of the special states -- but speed is the
49 * job of the optimized implementations. Rather, the goal
50 * here is to establish a stable, probabilistically correct
51 * reference calculation. (Thus, the CC, NN, JJ transitions
52 * are real scores here, not fixed to 0 as in the optimized
53 * versions.)
54 */
55 int
56 p7_GMSV(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, P7_GMX *gx, float nu, float *opt_sc)
57 {
58 float **dp = gx->dp;
59 float *xmx = gx->xmx;
60 float tloop = logf((float) L / (float) (L+3));
61 float tmove = logf( 3.0f / (float) (L+3));
62 float tbmk = logf( 2.0f / ((float) gm->M * (float) (gm->M+1)));
63 float tej = logf((nu - 1.0f) / nu);
64 float tec = logf(1.0f / nu);
65 int i,k;
66
67
68 XMX(0,p7G_N) = 0;
69 XMX(0,p7G_B) = tmove; /* S->N->B, no N-tail */
70 XMX(0,p7G_E) = XMX(0,p7G_C) = XMX(0,p7G_J) =-eslINFINITY; /* need seq to get here */
71 for (k = 0; k <= gm->M; k++)
72 MMX(0,k) = -eslINFINITY; /* need seq to get here */
73
74
75 for (i = 1; i <= L; i++)
76 {
77
78 float const *rsc = gm->rsc[dsq[i]];
79 MMX(i,0) = -eslINFINITY;
80 XMX(i,p7G_E) = -eslINFINITY;
81 for (k = 1; k <= gm->M; k++)
82 {
83 MMX(i,k) = MSC(k) + ESL_MAX(MMX(i-1,k-1), XMX(i-1,p7G_B) + tbmk);
84 XMX(i,p7G_E) = ESL_MAX(XMX(i,p7G_E), MMX(i,k));
85 }
86
87
88 XMX(i,p7G_J) = ESL_MAX( XMX(i-1,p7G_J) + tloop, XMX(i, p7G_E) + tej);
89 XMX(i,p7G_C) = ESL_MAX( XMX(i-1,p7G_C) + tloop, XMX(i, p7G_E) + tec);
90 XMX(i,p7G_N) = XMX(i-1,p7G_N) + tloop;
91 XMX(i,p7G_B) = ESL_MAX( XMX(i, p7G_N) + tmove, XMX(i, p7G_J) + tmove);
92
93 }
94
95
96 gx->M = gm->M;
97 gx->L = L;
98 if (opt_sc != NULL) *opt_sc = XMX(L,p7G_C) + tmove;
99 return eslOK;
100 }
101 /*---------------------- end, msv -------------------------------*/
102
103
104
105
106 /* Function: p7_GMSV_longtarget()
107 * Synopsis: Finds windows with MSV scores above some threshold (slow, correct version)
108 * Incept: TJW, Thu Jun 17 14:32:08 EDT 2010 [Janelia]
109 *
110 *
111 * Purpose: Calculates the MSV score for regions of sequence <dsq> of length <L>
112 * residues, and captures the positions at which such regions exceed the
113 * score required to be significant in the eyes of the calling function
114 * (usually p=0.02). Note that this variant performs MSV computations,
115 * while the optimized versions typically perform SSV (never passing
116 * through the J state). See comments in impl_sse/p7_MSVFilter_longtarget()
117 * for details
118 *
119 * Rather than simply capturing positions at which a score threshold
120 * is reached, this function establishes windows around those
121 * high-scoring positions, then merges overlapping windows.
122 *
123 *
124 * Args: dsq - digital target sequence, 1..L
125 * L - length of dsq in residues
126 * gm - profile (can be in any mode)
127 * gx - DP matrix
128 * nu - configuration: expected number of hits (use 2.0 as a default)
129 * bg - the background model, required for translating a P-value threshold into a score threshold
130 * P - p-value below which a region is captured as being above threshold
131 * windowlist - RETURN: array of hit windows (start and end of diagonal) for the above-threshold areas
132 * *
133 *
134 * Note: Not worried about speed here. Based on p7_GMSV
135 *
136 * Returns: <eslOK> on success.
137 *
138 * Throws: <eslEINVAL> if <ox> allocation is too small.
139 */
140 int
141 p7_GMSV_longtarget(const ESL_DSQ *dsq, int L, P7_PROFILE *gm, P7_GMX *gx, float nu, P7_BG *bg, double P, P7_HMM_WINDOWLIST *windowlist)
142 {
143
144 /* A couple differences between this MSV and the standard one:
145 *
146 * - the transitions are parameterized based on window length (gm->max_length), not target length.
147 * - because we're scanning along a sequence with the implicit assumption that each
148 * point we're inspecting is part of a window, but we don't know where that window starts/ends,
149 * we don't use the tloop cost in its proper form. Instead of incuring the tloop cost for
150 * each pass through the N/C states, we simply build the whole chunk of loop cost into the
151 * threshold (treating it as though it would've been added at the end of computation)
152 *
153 */
154
155 float **dp = gx->dp;
156 float *xmx = gx->xmx;
157 float tloop = logf((float) gm->max_length / (float) (gm->max_length+3));
158 float tmove = logf( 3.0f / (float) (gm->max_length+3));
159 float tbmk = logf( 2.0f / ((float) gm->M * (float) (gm->M+1)));
160 float tej = logf((nu - 1.0f) / nu);
161 float tec = logf(1.0f / nu);
162 int i,k;
163
164 float tloop_total = tloop * gm->max_length;
165
166 //int target_end;
167 int target_start;
168
169
170 float sc_thresh;
171 float nullsc;
172
173
174 /*
175 * Computing the score required to let P meet the F1 prob threshold
176 * In original code, converting from an MSV score S (the score getting
177 * to state C) to a probability goes like this:
178 * S = XMX(L,p7G_C)
179 * usc = S + tmove + tloop_total
180 * P = f ( (usc - nullsc) / eslCONST_LOG2 , mu, lambda)
181 * and we're computing the threshold score S, so reverse it:
182 * (usc - nullsc) / eslCONST_LOG2 = inv_f( P, mu, lambda)
183 * usc = nullsc + eslCONST_LOG2 * inv_f( P, mu, lambda)
184 * S = usc - tmove - tloop_total
185 *
186 * Here, I compute threshold with length model based on max_length. Usually, the
187 * length of a window returned by this scan will be longer than max_length. Doesn't
188 * really matter - in any case, both the bg and om models will change with roughly
189 * 1 bit for each doubling of the length model, so they offset.
190 */
191 float invP = esl_gumbel_invsurv(P, gm->evparam[p7_MMU], gm->evparam[p7_MLAMBDA]);
192 p7_bg_SetLength(bg, gm->max_length);
193 p7_ReconfigLength(gm, gm->max_length);
194 p7_bg_NullOne (bg, dsq, gm->max_length, &nullsc);
195
196 sc_thresh = nullsc + (invP * eslCONST_LOG2) - tmove - tloop_total;
197
198
199 XMX(0,p7G_N) = 0;
200 XMX(0,p7G_B) = tmove; /* S->N->B, no N-tail */
201 XMX(0,p7G_E) = XMX(0,p7G_C) = XMX(0,p7G_J) =-eslINFINITY; /* need seq to get here */
202 for (k = 0; k <= gm->M; k++)
203 MMX(0,k) = -eslINFINITY; /* need seq to get here */
204
205 for (i = 1; i <= L; i++)
206 {
207 float const *rsc = gm->rsc[dsq[i]];
208
209 MMX(i,0) = -eslINFINITY;
210 XMX(i,p7G_E) = -eslINFINITY;
211
212 for (k = 1; k <= gm->M; k++)
213 {
214 MMX(i,k) = MSC(k) + ESL_MAX(MMX(i-1,k-1), XMX(i-1,p7G_B) + tbmk);
215 XMX(i,p7G_E) = ESL_MAX(XMX(i,p7G_E), MMX(i,k));
216 }
217
218 XMX(i,p7G_J) = ESL_MAX( XMX(i-1,p7G_J) /*+ tloop*/, XMX(i, p7G_E) + tej);
219 XMX(i,p7G_C) = ESL_MAX( XMX(i-1,p7G_C) /*+ tloop*/, XMX(i, p7G_E) + tec);
220 XMX(i,p7G_N) = XMX(i-1,p7G_N) /*+ tloop*/;
221 XMX(i,p7G_B) = ESL_MAX( XMX(i, p7G_N) + tmove, XMX(i, p7G_J) + tmove);
222
223
224 if (XMX(i,p7G_C) > sc_thresh)
225 {
226 target_start = ESL_MAX(1, i - gm->max_length + 1);
227 //target_end = ESL_MIN(L, i + gm->max_length - 1);
228
229 //TODO: this is wrong - it just pretends the hit was to the middle of the model, to get dummy to
230 // quit crashing. Results are definitely wrong.
231 // Need to get diagonal backtracking implemented in dummy
232 p7_hmmwindow_new(windowlist, 0, target_start, 0, gm->M/2, 1, XMX(i,p7G_C), p7_NOCOMPLEMENT, L);
233
234
235 //start the search all over again
236 XMX(i,p7G_N) = 0;
237 XMX(i,p7G_B) = tmove; /* S->N->B, no N-tail */
238 XMX(i,p7G_E) = XMX(i,p7G_C) = XMX(i,p7G_J) =-eslINFINITY; /* need seq to get here */
239 for (k = 0; k <= gm->M; k++)
240 MMX(i,k) = -eslINFINITY;
241
242 }
243
244 }
245
246
247 return eslOK;
248
249 }
250 /*------------------ end, p7_GMSV_longtarget() ------------------------*/
251
252
253 /*****************************************************************
254 * 2. Benchmark driver.
255 *****************************************************************/
256 #ifdef p7GENERIC_MSV_BENCHMARK
257 /*
258 gcc -g -O2 -o generic_msv_benchmark -I. -L. -I../easel -L../easel -Dp7GENERIC_MSV_BENCHMARK generic_msv.c -lhmmer -leasel -lm
259 icc -O3 -static -o generic_msv_benchmark -I. -L. -I../easel -L../easel -Dp7GENERIC_MSV_BENCHMARK generic_msv.c -lhmmer -leasel -lm
260 ./benchmark-generic-msv <hmmfile>
261 */
262 /* As of Fri Dec 28 14:48:39 2007
263 * Viterbi = 61.8 Mc/s
264 * Forward = 8.6 Mc/s
265 * Backward = 7.1 Mc/s
266 * GMSV = 55.9 Mc/s
267 * (gcc -g -O2, 3.2GHz Xeon, N=50K, L=400, M=72 RRM_1 model)
268 */
269 #include <p7_config.h>
270
271 #include "easel.h"
272 #include "esl_alphabet.h"
273 #include "esl_getopts.h"
274 #include "esl_random.h"
275 #include "esl_randomseq.h"
276 #include "esl_stopwatch.h"
277
278 #include "hmmer.h"
279
280 static ESL_OPTIONS options[] = {
281 /* name type default env range toggles reqs incomp help docgroup*/
282 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
283 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
284 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
285 { "-N", eslARG_INT, "20000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
286 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
287 };
288 static char usage[] = "[-options] <hmmfile>";
289 static char banner[] = "benchmark driver for generic MSV";
290
291 int
292 main(int argc, char **argv)
293 {
294 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
295 char *hmmfile = esl_opt_GetArg(go, 1);
296 ESL_STOPWATCH *w = esl_stopwatch_Create();
297 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
298 ESL_ALPHABET *abc = NULL;
299 P7_HMMFILE *hfp = NULL;
300 P7_HMM *hmm = NULL;
301 P7_BG *bg = NULL;
302 P7_PROFILE *gm = NULL;
303 P7_GMX *gx = NULL;
304 int L = esl_opt_GetInteger(go, "-L");
305 int N = esl_opt_GetInteger(go, "-N");
306 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
307 int i;
308 float sc;
309 double base_time, bench_time, Mcs;
310
311 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
312 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
313
314 bg = p7_bg_Create(abc);
315 p7_bg_SetLength(bg, L);
316 gm = p7_profile_Create(hmm->M, abc);
317 p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL);
318 gx = p7_gmx_Create(gm->M, L);
319
320 /* Baseline time. */
321 esl_stopwatch_Start(w);
322 for (i = 0; i < N; i++) esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
323 esl_stopwatch_Stop(w);
324 base_time = w->user;
325
326 /* Benchmark time. */
327 esl_stopwatch_Start(w);
328 for (i = 0; i < N; i++)
329 {
330 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
331 p7_GMSV (dsq, L, gm, gx, 2.0, &sc);
332 }
333 esl_stopwatch_Stop(w);
334 bench_time = w->user - base_time;
335 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) bench_time;
336 esl_stopwatch_Display(stdout, w, "# CPU time: ");
337 printf("# M = %d\n", gm->M);
338 printf("# %.1f Mc/s\n", Mcs);
339
340 free(dsq);
341 p7_gmx_Destroy(gx);
342 p7_profile_Destroy(gm);
343 p7_bg_Destroy(bg);
344 p7_hmm_Destroy(hmm);
345 p7_hmmfile_Close(hfp);
346 esl_alphabet_Destroy(abc);
347 esl_stopwatch_Destroy(w);
348 esl_randomness_Destroy(r);
349 esl_getopts_Destroy(go);
350 return 0;
351 }
352 #endif /*p7GENERIC_MSV_BENCHMARK*/
353 /*----------------- end, benchmark ------------------------------*/
354
355 /*****************************************************************
356 * 3. Unit tests
357 *****************************************************************/
358 #ifdef p7GENERIC_MSV_TESTDRIVE
359 #include "esl_getopts.h"
360 #include "esl_random.h"
361 #include "esl_randomseq.h"
362 #include "esl_vectorops.h"
363 /* The MSV score can be validated against Viterbi (provided we trust
364 * Viterbi), by creating a multihit local profile in which:
365 * 1. All t_MM scores = 0
366 * 2. All other core transitions = -inf
367 * 3. All t_BMk entries uniformly log 2/(M(M+1))
368 */
369 static void
370 utest_msv(ESL_GETOPTS *go, ESL_RANDOMNESS *r, ESL_ALPHABET *abc, P7_BG *bg, P7_PROFILE *gm, int nseq, int L)
371 {
372 P7_PROFILE *g2 = NULL;
373 ESL_DSQ *dsq = NULL;
374 P7_GMX *gx = NULL;
375 float sc1, sc2;
376 int k, idx;
377
378 if ((dsq = malloc(sizeof(ESL_DSQ) *(L+2))) == NULL) esl_fatal("malloc failed");
379 if ((gx = p7_gmx_Create(gm->M, L)) == NULL) esl_fatal("matrix creation failed");
380 if ((g2 = p7_profile_Clone(gm)) == NULL) esl_fatal("profile clone failed");
381
382 /* Make g2's scores appropriate for simulating the MSV algorithm in Viterbi */
383 esl_vec_FSet(g2->tsc, p7P_NTRANS * g2->M, -eslINFINITY);
384 for (k = 1; k < g2->M; k++) p7P_TSC(g2, k, p7P_MM) = 0.0f;
385 for (k = 0; k < g2->M; k++) p7P_TSC(g2, k, p7P_BM) = log(2.0f / ((float) g2->M * (float) (g2->M+1)));
386
387 for (idx = 0; idx < nseq; idx++)
388 {
389 if (esl_rsq_xfIID(r, bg->f, abc->K, L, dsq) != eslOK) esl_fatal("seq generation failed");
390
391 if (p7_GMSV (dsq, L, gm, gx, 2.0, &sc1) != eslOK) esl_fatal("MSV failed");
392 if (p7_GViterbi(dsq, L, g2, gx, &sc2) != eslOK) esl_fatal("viterbi failed");
393 if (fabs(sc1-sc2) > 0.0001) esl_fatal("MSV score not equal to Viterbi score");
394 }
395
396 p7_gmx_Destroy(gx);
397 p7_profile_Destroy(g2);
398 free(dsq);
399 return;
400 }
401 #endif /*p7GENERIC_MSV_TESTDRIVE*/
402 /*----------------- end, unit tests -----------------------------*/
403
404
405 /*****************************************************************
406 * 4. Test driver.
407 *****************************************************************/
408 /* gcc -g -Wall -Dp7GENERIC_MSV_TESTDRIVE -I. -I../easel -L. -L../easel -o generic_msv_utest generic_msv.c -lhmmer -leasel -lm
409 */
410 #ifdef p7GENERIC_MSV_TESTDRIVE
411 #include "easel.h"
412 #include "esl_getopts.h"
413 #include "esl_msa.h"
414
415 #include <p7_config.h>
416 #include "hmmer.h"
417
418 static ESL_OPTIONS options[] = {
419 /* name type default env range toggles reqs incomp help docgroup*/
420 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
421 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
422 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose", 0 },
423 { "--vv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be very verbose", 0 },
424 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
425 };
426 static char usage[] = "[-options]";
427 static char banner[] = "unit test driver for the generic Msv implementation";
428
429 int
430 main(int argc, char **argv)
431 {
432 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
433 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
434 ESL_ALPHABET *abc = NULL;
435 P7_HMM *hmm = NULL;
436 P7_PROFILE *gm = NULL;
437 P7_BG *bg = NULL;
438 int M = 100;
439 int L = 200;
440 int nseq = 20;
441 char errbuf[eslERRBUFSIZE];
442
443 if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create alphabet");
444 if (p7_hmm_Sample(r, M, abc, &hmm) != eslOK) esl_fatal("failed to sample an HMM");
445 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
446 if ((gm = p7_profile_Create(hmm->M, abc)) == NULL) esl_fatal("failed to create profile");
447 if (p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL) != eslOK) esl_fatal("failed to config profile");
448 if (p7_hmm_Validate (hmm, errbuf, 0.0001) != eslOK) esl_fatal("whoops, HMM is bad!: %s", errbuf);
449 if (p7_profile_Validate(gm, errbuf, 0.0001) != eslOK) esl_fatal("whoops, profile is bad!: %s", errbuf);
450
451 utest_msv(go, r, abc, bg, gm, nseq, L);
452
453 p7_profile_Destroy(gm);
454 p7_bg_Destroy(bg);
455 p7_hmm_Destroy(hmm);
456 esl_alphabet_Destroy(abc);
457 esl_randomness_Destroy(r);
458 esl_getopts_Destroy(go);
459 return 0;
460 }
461 #endif /*p7GENERIC_MSV_TESTDRIVE*/
462 /*-------------------- end, test driver -------------------------*/
463
464
465 /*****************************************************************
466 * 5. Example
467 *****************************************************************/
468 #ifdef p7GENERIC_MSV_EXAMPLE
469 /*
470 gcc -g -O2 -Dp7GENERIC_MSV_EXAMPLE -I. -I../easel -L. -L../easel -o generic_msv_example generic_msv.c -lhmmer -leasel -lm
471 */
472 #include <p7_config.h>
473
474 #include "easel.h"
475 #include "esl_alphabet.h"
476 #include "esl_getopts.h"
477 #include "esl_gumbel.h"
478 #include "esl_sq.h"
479 #include "esl_sqio.h"
480
481 #include "hmmer.h"
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 { "--nu", eslARG_REAL, "2.0", NULL, NULL, NULL, NULL, NULL, "set nu param to <x>: expected # MSV diagonals", 0 },
487 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
488 };
489 static char usage[] = "[-options] <hmmfile> <seqfile>";
490 static char banner[] = "example of generic MSV algorithm";
491
492
493 int
494 main(int argc, char **argv)
495 {
496 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
497 char *hmmfile = esl_opt_GetArg(go, 1);
498 char *seqfile = esl_opt_GetArg(go, 2);
499 float nu = esl_opt_GetReal(go, "--nu");
500 ESL_ALPHABET *abc = NULL;
501 P7_HMMFILE *hfp = NULL;
502 P7_HMM *hmm = NULL;
503 P7_BG *bg = NULL;
504 P7_PROFILE *gm = NULL;
505 P7_GMX *fwd = NULL;
506 ESL_SQ *sq = NULL;
507 ESL_SQFILE *sqfp = NULL;
508 P7_TRACE *tr = NULL;
509 int format = eslSQFILE_UNKNOWN;
510 float sc, nullsc, seqscore, lnP;
511 int status;
512
513 /* Read in one HMM */
514 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
515 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
516 p7_hmmfile_Close(hfp);
517
518 /* Open sequence file */
519 sq = esl_sq_CreateDigital(abc);
520 status = esl_sqfile_Open(seqfile, format, NULL, &sqfp);
521 if (status == eslENOTFOUND) p7_Fail("No such file.");
522 else if (status == eslEFORMAT) p7_Fail("Format unrecognized.");
523 else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz.");
524 else if (status != eslOK) p7_Fail("Open failed, code %d.", status);
525
526 /* Configure a profile from the HMM */
527 bg = p7_bg_Create(abc);
528 gm = p7_profile_Create(hmm->M, abc);
529 p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL);
530
531 /* Allocate matrix */
532 fwd = p7_gmx_Create(gm->M, sq->n);
533
534 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK)
535 {
536 p7_ReconfigLength(gm, sq->n);
537 p7_bg_SetLength(bg, sq->n);
538 p7_gmx_GrowTo(fwd, gm->M, sq->n);
539
540 /* Run MSV */
541 p7_GMSV(sq->dsq, sq->n, gm, fwd, nu, &sc);
542
543 /* Calculate bit score and P-value using standard null1 model*/
544 p7_bg_NullOne (bg, sq->dsq, sq->n, &nullsc);
545 seqscore = (sc - nullsc) / eslCONST_LOG2;
546 lnP = esl_gumbel_logsurv(seqscore, gm->evparam[p7_MMU], gm->evparam[p7_MLAMBDA]);
547
548 /* output suitable for direct use in profmark benchmark postprocessors:
549 * <Pvalue> <bitscore> <target name> <query name>
550 */
551 printf("%g\t%.2f\t%s\t%s\n", exp(lnP), seqscore, sq->name, hmm->name);
552
553 esl_sq_Reuse(sq);
554 }
555 if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n", sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
556 else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s", status, sqfp->filename);
557
558 /* Cleanup */
559 esl_sqfile_Close(sqfp);
560 esl_sq_Destroy(sq);
561 p7_trace_Destroy(tr);
562 p7_gmx_Destroy(fwd);
563 p7_profile_Destroy(gm);
564 p7_bg_Destroy(bg);
565 p7_hmm_Destroy(hmm);
566 esl_alphabet_Destroy(abc);
567 esl_getopts_Destroy(go);
568 return 0;
569 }
570 #endif /*p7GENERIC_MSV_EXAMPLE*/
571 /*-------------------- end, example -----------------------------*/
572
573