Debian Codegraph @

hmmer/src/impl_vmx/msvfilter.c

raw symbols SCIP
1 /* The MSV filter implementation; VMX version.
2 *
3 * A "filter" is a one-row, O(M), DP implementation that calculates an
4 * approximated nat score (i.e. in limited precision - here, uchar)
5 * and may have limited numeric range. It will return <eslERANGE> if
6 * its numeric range is exceeded, in which case the caller will have
7 * to obtain the score by another (probably slower) method.
8 *
9 * Contents:
10 * 1. p7_MSVFilter() implementation
11 * 2. Benchmark driver
12 * 3. Unit tests
13 * 4. Test driver
14 * 5. Example
15 *
16 * SRE, Sun Nov 25 11:26:48 2007 [Casa de Gatos]
17 */
18 #include <p7_config.h>
19
20 #include <stdio.h>
21 #include <math.h>
22
23 #ifndef __APPLE_ALTIVEC__
24 #include <altivec.h>
25 #endif
26
27 #include "easel.h"
28 #include "esl_vmx.h"
29 #include "esl_gumbel.h"
30
31 #include "hmmer.h"
32 #include "impl_vmx.h"
33
34 /*****************************************************************
35 * 1. The p7_MSVFilter() DP implementation.
36 *****************************************************************/
37
38 /* Function: p7_MSVFilter()
39 * Synopsis: Calculates MSV score, vewy vewy fast, in limited precision.
40 * Incept: SRE, Wed Dec 26 15:12:25 2007 [Janelia]
41 *
42 * Purpose: Calculates an approximation of the MSV score for sequence
43 * <dsq> of length <L> residues, using optimized profile <om>,
44 * and a preallocated one-row DP matrix <ox>. Return the
45 * estimated MSV score (in nats) in <ret_sc>.
46 *
47 * Score may overflow (and will, on high-scoring
48 * sequences), but will not underflow.
49 *
50 * The model may be in any mode, because only its match
51 * emission scores will be used. The MSV filter inherently
52 * assumes a multihit local mode, and uses its own special
53 * state transition scores, not the scores in the profile.
54 *
55 * Args: dsq - digital target sequence, 1..L
56 * L - length of dsq in residues
57 * om - optimized profile
58 * ox - DP matrix
59 * ret_sc - RETURN: MSV score (in nats)
60 *
61 * Note: We misuse the matrix <ox> here, using only a third of the
62 * first dp row, accessing it as <dp[0..Q-1]> rather than
63 * in triplets via <{MDI}MX(q)> macros, since we only need
64 * to store M state values. We know that if <ox> was big
65 * enough for normal DP calculations, it must be big enough
66 * to hold the MSVFilter calculation.
67 *
68 * Returns: <eslOK> on success.
69 * <eslERANGE> if the score overflows the limited range; in
70 * this case, this is a high-scoring hit.
71 *
72 * Throws: <eslEINVAL> if <ox> allocation is too small.
73 */
74 int
75 p7_MSVFilter(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, P7_OMX *ox, float *ret_sc)
76 {
77 vector unsigned char mpv; /* previous row values */
78 vector unsigned char xEv; /* E state: keeps max for Mk->E as we go */
79 vector unsigned char xBv; /* B state: splatted vector of B[i-1] for B->Mk calculations */
80 vector unsigned char sv; /* temp storage of 1 curr row value in progress */
81 vector unsigned char biasv; /* emission bias in a vector */
82 uint8_t xJ; /* special states' scores */
83 int i; /* counter over sequence positions 1..L */
84 int q; /* counter over vectors 0..nq-1 */
85 int Q = p7O_NQB(om->M); /* segment length: # of vectors */
86 vector unsigned char *dp; /* we're going to use dp[0][0..q..Q-1], not {MDI}MX(q) macros*/
87 vector unsigned char *rsc; /* will point at om->rbv[x] for residue x[i] */
88 vector unsigned char zerov; /* vector of zeros */
89 vector unsigned char xJv; /* vector for states score */
90 vector unsigned char tjbmv; /* vector for B->Mk cost */
91 vector unsigned char tecv; /* vector for E->C cost */
92 vector unsigned char basev; /* offset for scores */
93 vector unsigned char ceilingv; /* saturateed simd value used to test for overflow */
94 vector unsigned char tempv;
95
96 /* Check that the DP matrix is ok for us. */
97 if (Q > ox->allocQ16) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small");
98 ox->M = om->M;
99
100 /* Initialization. In offset unsigned arithmetic, -infinity is 0, and 0 is om->base.
101 */
102 dp = ox->dpb[0];
103 for (q = 0; q < Q; q++) dp[q] = vec_splat_u8(0);
104 xJ = 0;
105
106 biasv = esl_vmx_set_u8(om->bias_b);
107 zerov = vec_splat_u8(0);
108
109 /* saturate simd register for overflow test */
110 tempv = vec_splat_u8(1);
111 ceilingv = (vector unsigned char)vec_cmpeq(biasv, biasv);
112 ceilingv = vec_subs(ceilingv, biasv);
113 ceilingv = vec_subs(ceilingv, tempv);
114
115 basev = esl_vmx_set_u8((int8_t) om->base_b);
116
117 tecv = esl_vmx_set_u8((int8_t) om->tec_b);
118 tjbmv = esl_vmx_set_u8((int8_t) om->tjb_b + (int8_t) om->tbm_b);
119
120 xJv = vec_subs(biasv, biasv);
121 xBv = vec_subs(basev, tjbmv);
122
123 #if eslDEBUGLEVEL > 0
124 if (ox->debugging)
125 {
126 unsigned char xB;
127 vec_ste(xBv, 0, &xB);
128 vec_ste(xJv, 0, &xJ);
129 p7_omx_DumpMFRow(ox, 0, 0, 0, xJ, xB, xJ);
130 }
131 #endif
132
133 for (i = 1; i <= L; i++)
134 {
135 rsc = om->rbv[dsq[i]];
136 xEv = vec_splat_u8(0);
137 // xBv = vec_sub(xBv, tbmv);
138
139 /* Right shifts by 1 byte. 4,8,12,x becomes x,4,8,12.
140 * Because ia32 is littlendian, this means a left bit shift.
141 * Zeros shift on automatically, which is our -infinity.
142 */
143 mpv = vec_sld(zerov, dp[Q-1], 15);
144 for (q = 0; q < Q; q++)
145 {
146 /* Calculate new MMXo(i,q); don't store it yet, hold it in sv. */
147 sv = vec_max(mpv, xBv);
148 sv = vec_adds(sv, biasv);
149 sv = vec_subs(sv, *rsc); rsc++;
150 xEv = vec_max(xEv, sv);
151
152 mpv = dp[q]; /* Load {MDI}(i-1,q) into mpv */
153 dp[q] = sv; /* Do delayed store of M(i,q) now that memory is usable */
154 }
155
156 /* Now the "special" states, which start from Mk->E (->C, ->J->B)
157 * Use rotates instead of shifts so when the last max has completed,
158 * all elements of the simd register will contain the max value.
159 */
160 tempv = vec_sld(xEv, xEv, 1);
161 xEv = vec_max(xEv, tempv);
162 tempv = vec_sld(xEv, xEv, 2);
163 xEv = vec_max(xEv, tempv);
164 tempv = vec_sld(xEv, xEv, 4);
165 xEv = vec_max(xEv, tempv);
166 tempv = vec_sld(xEv, xEv, 8);
167 xEv = vec_max(xEv, tempv);
168
169 /* immediately detect overflow */
170 if (vec_any_gt(xEv, ceilingv))
171 {
172 *ret_sc = eslINFINITY;
173 return eslERANGE;
174 }
175
176 xEv = vec_subs(xEv, tecv);
177 xJv = vec_max(xJv,xEv);
178
179 xBv = vec_max(basev, xJv);
180 xBv = vec_subs(xBv, tjbmv);
181
182 #if eslDEBUGLEVEL > 0
183 if (ox->debugging)
184 {
185 unsigned char xB, xE;
186 vec_ste(xBv, 0, &xB);
187 vec_ste(xEv, 0, &xE);
188 vec_ste(xJv, 0, &xJ);
189 p7_omx_DumpMFRow(ox, i, xE, 0, xJ, xB, xJ);
190 }
191 #endif
192 } /* end loop over sequence residues 1..L */
193
194 /* finally C->T, and add our missing precision on the NN,CC,JJ back */
195 vec_ste(xJv, 0, &xJ);
196 *ret_sc = ((float) (xJ - om->tjb_b) - (float) om->base_b);
197 *ret_sc /= om->scale_b;
198 *ret_sc -= 3.0; /* that's ~ L \log \frac{L}{L+3}, for our NN,CC,JJ */
199
200 return eslOK;
201 }
202 /*------------------ end, p7_MSVFilter() ------------------------*/
203
204
205 /* Function: p7_SSVFilter_longtarget()
206 * Synopsis: Finds windows with SSV scores above some threshold (vewy vewy fast, in limited precision)
207 *
208 * Purpose: Calculates an approximation of the SSV (single ungapped diagonal)
209 * score for regions of sequence <dsq> of length <L> residues, using
210 * optimized profile <om>, and a preallocated one-row DP matrix <ox>,
211 * and captures the positions at which such regions exceed the score
212 * required to be significant in the eyes of the calling function,
213 * which depends on the <bg> and <p> (usually p=0.02 for nhmmer).
214 * Note that this variant performs only SSV computations, never
215 * passing through the J state - the score required to pass SSV at
216 * the default threshold (or less restrictive) is sufficient to
217 * pass MSV in essentially all DNA models we've tested.
218 *
219 * Above-threshold diagonals are captured into a preallocated list
220 * <windowlist>. Rather than simply capturing positions at which a
221 * score threshold is reached, this function establishes windows
222 * around those high-scoring positions, using scores in <msvdata>.
223 * These windows can be merged by the calling function.
224 *
225 *
226 * Args: dsq - digital target sequence, 1..L
227 * L - length of dsq in residues
228 * om - optimized profile
229 * ox - DP matrix
230 * msvdata - compact representation of substitution scores, for backtracking diagonals
231 * bg - the background model, required for translating a P-value threshold into a score threshold
232 * P - p-value below which a region is captured as being above threshold
233 * windowlist - preallocated container for all hits (resized if necessary)
234 *
235 *
236 * Note: We misuse the matrix <ox> here, using only a third of the
237 * first dp row, accessing it as <dp[0..Q-1]> rather than
238 * in triplets via <{MDI}MX(q)> macros, since we only need
239 * to store M state values. We know that if <ox> was big
240 * enough for normal DP calculations, it must be big enough
241 * to hold the MSVFilter calculation.
242 *
243 * Returns: <eslOK> on success.
244 *
245 * Throws: <eslEINVAL> if <ox> allocation is too small.
246 */
247 int
248 p7_SSVFilter_longtarget(const ESL_DSQ *dsq, int L, P7_OPROFILE *om, P7_OMX *ox, const P7_SCOREDATA *ssvdata,
249 P7_BG *bg, double P, P7_HMM_WINDOWLIST *windowlist)
250 {
251
252 vector unsigned char mpv; /* previous row values */
253 vector unsigned char xEv; /* E state: keeps max for Mk->E as we go */
254 vector unsigned char xBv; /* B state: splatted vector of B[i-1] for B->Mk calculations */
255 vector unsigned char sv; /* temp storage of 1 curr row value in progress */
256 vector unsigned char biasv; /* emission bias in a vector */
257 int i; /* counter over sequence positions 1..L */
258 int q; /* counter over vectors 0..nq-1 */
259 int Q = p7O_NQB(om->M); /* segment length: # of vectors */
260 vector unsigned char *dp = ox->dpb[0]; /* we're going to use dp[0][0..q..Q-1], not {MDI}MX(q) macros*/
261 vector unsigned char *rsc; /* will point at om->rbv[x] for residue x[i] */
262 vector unsigned char zerov; /* vector of zeros */
263 vector unsigned char tjbmv; /* vector for [JN]->B->M move cost */
264 vector unsigned char basev; /* offset for scores */
265 union { vector unsigned char v; uint8_t b[16]; } u;
266 int k;
267 int n;
268 int end;
269 int rem_sc;
270 int start;
271 int target_end;
272 int target_start;
273 int max_end;
274 int max_sc;
275 int sc;
276 int pos_since_max;
277 float ret_sc;
278
279
280 /* Computing the score required to let P meet the F1 prob threshold
281 * In original code, converting from a scaled int MSV
282 * score S (the score getting to state E) to a probability goes like this:
283 * usc = S - om->tec_b - om->tjb_b - om->base_b;
284 * usc /= om->scale_b;
285 * usc -= 3.0;
286 * P = f ( (usc - nullsc) / eslCONST_LOG2 , mu, lambda)
287 * and we're computing the threshold usc, so reverse it:
288 * (usc - nullsc) / eslCONST_LOG2 = inv_f( P, mu, lambda)
289 * usc = nullsc + eslCONST_LOG2 * inv_f( P, mu, lambda)
290 * usc += 3
291 * usc *= om->scale_b
292 * S = usc + om->tec_b + om->tjb_b + om->base_b
293 *
294 * Here, I compute threshold with length model based on max_length. Doesn't
295 * matter much - in any case, both the bg and om models will change with roughly
296 * 1 bit for each doubling of the length model, so they offset.
297 */
298 float nullsc;
299 float invP = esl_gumbel_invsurv(P, om->evparam[p7_MMU], om->evparam[p7_MLAMBDA]);
300 vector unsigned char sc_threshv; /* pushes value to saturation if it's above pthresh */
301 int sc_thresh;
302
303 /* Check that the DP matrix is ok for us. */
304 if (Q > ox->allocQ16) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small");
305 ox->M = om->M;
306
307
308 p7_bg_SetLength(bg, om->max_length);
309 p7_oprofile_ReconfigMSVLength(om, om->max_length);
310 p7_bg_NullOne (bg, dsq, om->max_length, &nullsc);
311
312 sc_thresh = (int) ceil( ( ( nullsc + (invP * eslCONST_LOG2) + 3.0 ) * om->scale_b ) + om->base_b + om->tec_b + om->tjb_b );
313 sc_threshv = esl_vmx_set_u8( (int8_t)sc_thresh - 1);
314
315
316 /* Initialization. In offset unsigned arithmetic, -infinity is 0, and 0 is om->base. */
317 biasv = esl_vmx_set_u8(om->bias_b);
318 for (q = 0; q < Q; q++) dp[q] = vec_splat_u8(0);
319 zerov = vec_splat_u8(0);
320 basev = esl_vmx_set_u8((int8_t) om->base_b);
321 tjbmv = esl_vmx_set_u8((int8_t) om->tjb_b + (int8_t) om->tbm_b);
322 xBv = vec_subs(basev, tjbmv);
323
324 for (i = 1; i <= L; i++)
325 {
326 rsc = om->rbv[dsq[i]];
327 xEv = vec_splat_u8(0);
328
329 /* Right shifts by 1 byte. 4,8,12,x becomes x,4,8,12.
330 * Zeros shift on automatically, which is our -infinity.
331 */
332 mpv = vec_sld(zerov, dp[Q-1], 15);
333 for (q = 0; q < Q; q++)
334 {
335 /* Calculate new MMXo(i,q); don't store it yet, hold it in sv. */
336 sv = vec_max(mpv, xBv);
337 sv = vec_adds(sv, biasv);
338 sv = vec_subs(sv, *rsc); rsc++;
339 xEv = vec_max(xEv, sv);
340
341 mpv = dp[q]; /* Load {MDI}(i-1,q) into mpv */
342 dp[q] = sv; /* Do delayed store of M(i,q) now that memory is usable */
343 }
344
345
346 if (vec_any_gt(xEv, sc_threshv) ) // hit pthresh, so add position to list and reset values
347 {
348 // figure out which model state hit threshold
349 end = -1;
350 rem_sc = -1;
351 for (q = 0; q < Q; q++) // Unpack and unstripe, so we can find the state that exceeded pthresh
352 {
353 u.v = dp[q];
354 for (k = 0; k < 16; k++) // unstripe
355 {
356 // (q+Q*k+1) is the model position k at which the xE score is found
357 if (u.b[k] >= sc_thresh && u.b[k] > rem_sc && (q+Q*k+1) <= om->M)
358 {
359 end = (q+Q*k+1);
360 rem_sc = u.b[k];
361 }
362 }
363 dp[q] = vec_splat_u8(0); // while we're here ... this will cause values to get reset to xB in next dp iteration
364 }
365
366 // recover the diagonal that hit threshold
367 start = end;
368 target_end = target_start = i;
369 sc = rem_sc;
370 while (rem_sc > om->base_b - om->tjb_b - om->tbm_b)
371 {
372 rem_sc -= om->bias_b - ssvdata->ssv_scores[start*om->abc->Kp + dsq[target_start]];
373 --start;
374 --target_start;
375 //if ( start == 0 || target_start==0) break;
376 }
377 start++;
378 target_start++;
379
380 //extend diagonal further with single diagonal extension
381 k = end+1;
382 n = target_end+1;
383 max_end = target_end;
384 max_sc = sc;
385 pos_since_max = 0;
386 while (k<om->M && n<=L)
387 {
388 sc += om->bias_b - ssvdata->ssv_scores[k*om->abc->Kp + dsq[n]];
389 if (sc >= max_sc)
390 {
391 max_sc = sc;
392 max_end = n;
393 pos_since_max=0;
394 }
395 else
396 {
397 pos_since_max++;
398 if (pos_since_max == 5)
399 break;
400 }
401 k++;
402 n++;
403 }
404
405 end += (max_end - target_end);
406 target_end = max_end;
407
408 ret_sc = ((float) (max_sc - om->tjb_b) - (float) om->base_b);
409 ret_sc /= om->scale_b;
410 ret_sc -= 3.0; // that's ~ L \log \frac{L}{L+3}, for our NN,CC,JJ
411
412 p7_hmmwindow_new( windowlist,
413 0, // sequence_id; used in the FM-based filter, but not here
414 target_start, // position in the target at which the diagonal starts
415 0, // position in the target fm_index at which diagonal starts; not used here, just in FM-based filter
416 end, // position in the model at which the diagonal ends
417 end-start+1 , // length of diagonal
418 ret_sc, // score of diagonal
419 p7_NOCOMPLEMENT, // always p7_NOCOMPLEMENT here; varies in FM-based filter
420 L);
421
422 i = target_end; // skip forward
423 }
424 } /* end loop over sequence residues 1..L */
425
426 return eslOK;
427 }
428 /*------------------ end, p7_SSVFilter_longtarget() ------------------------*/
429
430
431 /*****************************************************************
432 * 2. Benchmark driver.
433 *****************************************************************/
434 /* The benchmark driver has some additional non-benchmarking options
435 * to facilitate small-scale (by-eye) comparison of MSV scores against
436 * other implementations, for debugging purposes.
437 *
438 * The -c option compares against p7_GMSV() scores. This allows
439 * measuring the error inherent in the VMX implementation's reduced
440 * precision (p7_MSVFilter() runs in uint8_t; p7_GMSV() uses floats).
441 *
442 * The -x option compares against an emulation that should give
443 * exactly the same scores. The emulation is achieved by jiggering the
444 * fp scores in a generic profile to disallow gaps, have the same
445 * rounding and precision as the uint8_t's MSVFilter() is using, and
446 * to make the same post-hoc corrections for the NN, CC, JJ
447 * contributions to the final nat score; under these contrived
448 * circumstances, p7_GViterbi() gives the same scores as
449 * p7_MSVFilter().
450 *
451 * For using either -c or -x, you probably also want to limit the
452 * number of generated target sequences, using -N10 or -N100 for
453 * example.
454 */
455 #ifdef p7MSVFILTER_BENCHMARK
456 /*
457 gcc -o msvfilter-benchmark -std=gnu99 -g -Wall -maltivec -I.. -L.. -I../../easel -L../../easel -Dp7MSVFILTER_BENCHMARK msvfilter.c -lhmmer -leasel -lm
458 icc -o msvfilter-benchmark -O3 -static -I.. -L.. -I../../easel -L../../easel -Dp7MSVFILTER_BENCHMARK msvfilter.c -lhmmer -leasel -lm
459
460 ./benchmark-msvfilter <hmmfile> runs benchmark
461 ./benchmark-msvfilter -N100 -c <hmmfile> compare scores to generic impl
462 ./benchmark-msvfilter -N100 -x <hmmfile> compare scores to exact emulation
463 */
464 #include <p7_config.h>
465
466 #include "easel.h"
467 #include "esl_alphabet.h"
468 #include "esl_getopts.h"
469 #include "esl_random.h"
470 #include "esl_randomseq.h"
471 #include "esl_stopwatch.h"
472
473 #include "hmmer.h"
474 #include "impl_vmx.h"
475
476 static ESL_OPTIONS options[] = {
477 /* name type default env range toggles reqs incomp help docgroup*/
478 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
479 { "-c", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-x", "compare scores to generic implementation (debug)", 0 },
480 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
481 { "-x", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-c", "equate scores to trusted implementation (debug)", 0 },
482 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
483 { "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
484 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
485 };
486 static char usage[] = "[-options] <hmmfile>";
487 static char banner[] = "benchmark driver for MSVFilter() implementation";
488
489 int
490 main(int argc, char **argv)
491 {
492 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
493 char *hmmfile = esl_opt_GetArg(go, 1);
494 ESL_STOPWATCH *w = esl_stopwatch_Create();
495 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
496 ESL_ALPHABET *abc = NULL;
497 P7_HMMFILE *hfp = NULL;
498 P7_HMM *hmm = NULL;
499 P7_BG *bg = NULL;
500 P7_PROFILE *gm = NULL;
501 P7_OPROFILE *om = NULL;
502 P7_OMX *ox = NULL;
503 P7_GMX *gx = NULL;
504 int L = esl_opt_GetInteger(go, "-L");
505 int N = esl_opt_GetInteger(go, "-N");
506 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
507 int i;
508 float sc1, sc2;
509 double base_time, bench_time, Mcs;
510
511 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
512 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
513
514 bg = p7_bg_Create(abc);
515 p7_bg_SetLength(bg, L);
516 gm = p7_profile_Create(hmm->M, abc);
517 p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
518 om = p7_oprofile_Create(gm->M, abc);
519 p7_oprofile_Convert(gm, om);
520 p7_oprofile_ReconfigLength(om, L);
521
522 if (esl_opt_GetBoolean(go, "-x")) p7_profile_SameAsMF(om, gm);
523
524 ox = p7_omx_Create(gm->M, 0, 0);
525 gx = p7_gmx_Create(gm->M, L);
526
527 /* Get a baseline time: how long it takes just to generate the sequences */
528 esl_stopwatch_Start(w);
529 for (i = 0; i < N; i++)
530 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
531 esl_stopwatch_Stop(w);
532 base_time = w->user;
533
534 esl_stopwatch_Start(w);
535 for (i = 0; i < N; i++)
536 {
537 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
538 p7_MSVFilter (dsq, L, om, ox, &sc1);
539
540 /* -c option: compare generic to fast score */
541 if (esl_opt_GetBoolean(go, "-c"))
542 {
543 p7_GMSV (dsq, L, gm, gx, 2.0, &sc2);
544 printf("%.4f %.4f\n", sc1, sc2);
545 }
546
547 /* -x option: compare generic to fast score in a way that should give exactly the same result */
548 if (esl_opt_GetBoolean(go, "-x"))
549 {
550 p7_GViterbi(dsq, L, gm, gx, &sc2);
551 sc2 /= om->scale_b;
552 if (om->mode == p7_UNILOCAL) sc2 -= 2.0; /* that's ~ L \log \frac{L}{L+2}, for our NN,CC,JJ */
553 else if (om->mode == p7_LOCAL) sc2 -= 3.0; /* that's ~ L \log \frac{L}{L+3}, for our NN,CC,JJ */
554 printf("%.4f %.4f\n", sc1, sc2);
555 }
556 }
557 esl_stopwatch_Stop(w);
558 bench_time = w->user - base_time;
559 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) bench_time;
560 esl_stopwatch_Display(stdout, w, "# CPU time: ");
561 printf("# M = %d\n", gm->M);
562 printf("# %.1f Mc/s\n", Mcs);
563
564 free(dsq);
565 p7_omx_Destroy(ox);
566 p7_gmx_Destroy(gx);
567 p7_oprofile_Destroy(om);
568 p7_profile_Destroy(gm);
569 p7_bg_Destroy(bg);
570 p7_hmm_Destroy(hmm);
571 p7_hmmfile_Close(hfp);
572 esl_alphabet_Destroy(abc);
573 esl_stopwatch_Destroy(w);
574 esl_randomness_Destroy(r);
575 esl_getopts_Destroy(go);
576 return 0;
577 }
578 #endif /*p7MSVFILTER_BENCHMARK*/
579 /*------------------ end, benchmark driver ----------------------*/
580
581
582
583
584 /*****************************************************************
585 * 3. Unit tests
586 *****************************************************************/
587 #ifdef p7MSVFILTER_TESTDRIVE
588 #include "esl_random.h"
589 #include "esl_randomseq.h"
590
591 /*
592 * We can check that scores are identical (within machine error) to
593 * scores of generic DP with scores rounded the same way. Do this for
594 * a random model of length <M>, for <N> test sequences of length <L>.
595 *
596 * We assume that we don't accidentally generate a high-scoring random
597 * sequence that overflows MSVFilter()'s limited range.
598 *
599 */
600 static void
601 utest_msv_filter(ESL_RANDOMNESS *r, ESL_ALPHABET *abc, P7_BG *bg, int M, int L, int N)
602 {
603 P7_HMM *hmm = NULL;
604 P7_PROFILE *gm = NULL;
605 P7_OPROFILE *om = NULL;
606 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
607 P7_OMX *ox = p7_omx_Create(M, 0, 0);
608 P7_GMX *gx = p7_gmx_Create(M, L);
609 float sc1, sc2;
610
611 p7_oprofile_Sample(r, abc, bg, M, L, &hmm, &gm, &om);
612 p7_profile_SameAsMF(om, gm);
613 #if 0
614 p7_oprofile_Dump(stdout, om); //dumps the optimized profile
615 p7_omx_SetDumpMode(stdout, ox, TRUE); //makes the fast DP algorithms dump their matrices
616 #endif
617
618 while (N--)
619 {
620 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
621 p7_MSVFilter(dsq, L, om, ox, &sc1);
622 p7_GViterbi (dsq, L, gm, gx, &sc2);
623 #if 0
624 p7_gmx_Dump(stdout, gx, p7_DEFAULT); //dumps a generic DP matrix
625 #endif
626
627 sc2 = sc2 / om->scale_b - 3.0f;
628 if (fabs(sc1-sc2) > 0.001) esl_fatal("msv filter unit test failed: scores differ (%.2f, %.2f)", sc1, sc2);
629 }
630
631 free(dsq);
632 p7_hmm_Destroy(hmm);
633 p7_omx_Destroy(ox);
634 p7_gmx_Destroy(gx);
635 p7_profile_Destroy(gm);
636 p7_oprofile_Destroy(om);
637 }
638 #endif /*p7MSVFILTER_TESTDRIVE*/
639 /*-------------------- end, unit tests --------------------------*/
640
641
642
643
644 /*****************************************************************
645 * 4. Test driver
646 *****************************************************************/
647 #ifdef p7MSVFILTER_TESTDRIVE
648 /*
649 gcc -g -Wall -maltivec -std=gnu99 -I.. -L.. -I../../easel -L../../easel -o msvfilter_utest -Dp7MSVFILTER_TESTDRIVE msvfilter.c -lhmmer -leasel -lm
650 ./msvfilter_utest
651 */
652 #include <p7_config.h>
653
654 #include "easel.h"
655 #include "esl_alphabet.h"
656 #include "esl_getopts.h"
657
658 #include "hmmer.h"
659 #include "impl_vmx.h"
660
661 static ESL_OPTIONS options[] = {
662 /* name type default env range toggles reqs incomp help docgroup*/
663 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
664 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
665 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose", 0 },
666 { "-L", eslARG_INT, "200", NULL, NULL, NULL, NULL, NULL, "size of random sequences to sample", 0 },
667 { "-M", eslARG_INT, "145", NULL, NULL, NULL, NULL, NULL, "size of random models to sample", 0 },
668 { "-N", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of random sequences to sample", 0 },
669 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
670 };
671 static char usage[] = "[-options]";
672 static char banner[] = "test driver for the VMX MSVFilter() implementation";
673
674 int
675 main(int argc, char **argv)
676 {
677 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
678 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
679 ESL_ALPHABET *abc = NULL;
680 P7_BG *bg = NULL;
681 int M = esl_opt_GetInteger(go, "-M");
682 int L = esl_opt_GetInteger(go, "-L");
683 int N = esl_opt_GetInteger(go, "-N");
684
685 /* First round of tests for DNA alphabets. */
686 if ((abc = esl_alphabet_Create(eslDNA)) == NULL) esl_fatal("failed to create alphabet");
687 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
688
689 if (esl_opt_GetBoolean(go, "-v")) printf("MSVFilter() tests, DNA\n");
690 utest_msv_filter(r, abc, bg, M, L, N); /* normal sized models */
691 utest_msv_filter(r, abc, bg, 1, L, 10); /* size 1 models */
692 utest_msv_filter(r, abc, bg, M, 1, 10); /* size 1 sequences */
693
694 esl_alphabet_Destroy(abc);
695 p7_bg_Destroy(bg);
696
697 if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create alphabet");
698 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
699
700 if (esl_opt_GetBoolean(go, "-v")) printf("MSVFilter() tests, protein\n");
701 utest_msv_filter(r, abc, bg, M, L, N);
702 utest_msv_filter(r, abc, bg, 1, L, 10);
703 utest_msv_filter(r, abc, bg, M, 1, 10);
704
705 esl_alphabet_Destroy(abc);
706 p7_bg_Destroy(bg);
707
708 esl_getopts_Destroy(go);
709 esl_randomness_Destroy(r);
710 return eslOK;
711 }
712 #endif /*VITFILTER_TESTDRIVE*/
713
714
715
716 /*****************************************************************
717 * 5. Example
718 *****************************************************************/
719
720 #ifdef p7MSVFILTER_EXAMPLE
721 /* A minimal example.
722 Also useful for debugging on small HMMs and sequences.
723
724 gcc -g -Wall -maltivec -std=gnu99 -I.. -L.. -I../../easel -L../../easel -o msvfilter_example -Dp7MSVFILTER_EXAMPLE msvfilter.c -lhmmer -leasel -lm
725 ./msvfilter_example <hmmfile> <seqfile>
726 */
727 #include <p7_config.h>
728
729 #include "easel.h"
730 #include "esl_alphabet.h"
731 #include "esl_getopts.h"
732 #include "esl_gumbel.h"
733 #include "esl_sq.h"
734 #include "esl_sqio.h"
735
736 #include "hmmer.h"
737 #include "impl_vmx.h"
738
739 static ESL_OPTIONS options[] = {
740 /* name type default env range toggles reqs incomp help docgroup*/
741 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
742 { "-1", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "output in one line awkable format", 0 },
743 { "-P", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "output in profmark format", 0 },
744 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
745 };
746 static char usage[] = "[-options] <hmmfile> <seqfile>";
747 static char banner[] = "example of MSV filter algorithm";
748
749 int
750 main(int argc, char **argv)
751 {
752 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
753 char *hmmfile = esl_opt_GetArg(go, 1);
754 char *seqfile = esl_opt_GetArg(go, 2);
755 ESL_ALPHABET *abc = NULL;
756 P7_HMMFILE *hfp = NULL;
757 P7_HMM *hmm = NULL;
758 P7_BG *bg = NULL;
759 P7_PROFILE *gm = NULL;
760 P7_OPROFILE *om = NULL;
761 P7_OMX *ox = NULL;
762 P7_GMX *gx = NULL;
763 ESL_SQ *sq = NULL;
764 ESL_SQFILE *sqfp = NULL;
765 int format = eslSQFILE_UNKNOWN;
766 float msvraw, nullsc, msvscore;
767 float graw, gscore;
768 double P, gP;
769 int status;
770
771 /* Read in one HMM */
772 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
773 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
774
775 /* Open sequence file for reading */
776 sq = esl_sq_CreateDigital(abc);
777 status = esl_sqfile_Open(seqfile, format, NULL, &sqfp);
778 if (status == eslENOTFOUND) p7_Fail("No such file.");
779 else if (status == eslEFORMAT) p7_Fail("Format unrecognized.");
780 else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz.");
781 else if (status != eslOK) p7_Fail("Open failed, code %d.", status);
782
783 /* create default null model, then create and optimize profile */
784 bg = p7_bg_Create(abc);
785 p7_bg_SetLength(bg, sq->n);
786 gm = p7_profile_Create(hmm->M, abc);
787 p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL);
788 om = p7_oprofile_Create(gm->M, abc);
789 p7_oprofile_Convert(gm, om);
790
791 /* allocate DP matrices, both a generic and an optimized one */
792 ox = p7_omx_Create(gm->M, 0, 0); /* one row version */
793 gx = p7_gmx_Create(gm->M, sq->n);
794
795 /* Useful to place and compile in for debugging:
796 p7_oprofile_Dump(stdout, om); dumps the optimized profile
797 p7_omx_SetDumpMode(stdout, ox, TRUE); makes the fast DP algorithms dump their matrices
798 p7_gmx_Dump(stdout, gx, p7_DEFAULT); dumps a generic DP matrix
799 p7_oprofile_SameMSV(om, gm);
800 */
801 p7_omx_SetDumpMode(stdout, ox, TRUE);
802
803 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK)
804 {
805 p7_oprofile_ReconfigLength(om, sq->n);
806 p7_ReconfigLength(gm, sq->n);
807 p7_bg_SetLength(bg, sq->n);
808 p7_omx_GrowTo(ox, om->M, 0, sq->n);
809 p7_gmx_GrowTo(gx, gm->M, sq->n);
810
811 p7_MSVFilter (sq->dsq, sq->n, om, ox, &msvraw);
812 p7_bg_NullOne (bg, sq->dsq, sq->n, &nullsc);
813 msvscore = (msvraw - nullsc) / eslCONST_LOG2;
814 P = esl_gumbel_surv(msvscore, om->evparam[p7_MMU], om->evparam[p7_MLAMBDA]);
815
816 p7_GMSV(sq->dsq, sq->n, gm, gx, 2.0, &graw);
817 gscore = (graw - nullsc) / eslCONST_LOG2;
818 gP = esl_gumbel_surv(gscore, gm->evparam[p7_MMU], gm->evparam[p7_MLAMBDA]);
819
820 if (esl_opt_GetBoolean(go, "-1"))
821 {
822 printf("%-30s %-20s %9.2g %7.2f %9.2g %7.2f\n", sq->name, hmm->name, P, msvscore, gP, gscore);
823 }
824 else if (esl_opt_GetBoolean(go, "-P"))
825 { /* output suitable for direct use in profmark benchmark postprocessors: */
826 printf("%g %.2f %s %s\n", P, msvscore, sq->name, hmm->name);
827 }
828 else
829 {
830 printf("target sequence: %s\n", sq->name);
831 printf("msv filter raw score: %.2f nats\n", msvraw);
832 printf("null score: %.2f nats\n", nullsc);
833 printf("per-seq score: %.2f bits\n", msvscore);
834 printf("P-value: %g\n", P);
835 printf("GMSV raw score: %.2f nats\n", graw);
836 printf("GSMV per-seq score: %.2f bits\n", gscore);
837 printf("GSMV P-value: %g\n", gP);
838 }
839
840 esl_sq_Reuse(sq);
841 }
842
843 /* cleanup */
844 esl_sq_Destroy(sq);
845 esl_sqfile_Close(sqfp);
846 p7_omx_Destroy(ox);
847 p7_gmx_Destroy(gx);
848 p7_oprofile_Destroy(om);
849 p7_profile_Destroy(gm);
850 p7_bg_Destroy(bg);
851 p7_hmm_Destroy(hmm);
852 p7_hmmfile_Close(hfp);
853 esl_alphabet_Destroy(abc);
854 esl_getopts_Destroy(go);
855 return 0;
856 }
857 #endif /*p7MSVFILTER_EXAMPLE*/
858 /*---------------------- end, example ---------------------------*/
859