Debian Codegraph @

hmmer/src/impl_neon/msvfilter.c

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