Debian Codegraph @

hmmer/src/impl_vmx/null2.c

raw symbols SCIP
1 /* "null2" model, biased composition correction; VMX implementations.
2 *
3 * Contents:
4 * 1. Null2 estimation algorithms.
5 * 2. Benchmark driver.
6 * 3. Unit tests.
7 * 4. Test driver.
8 *
9 * SRE, Mon Aug 18 08:31:11 2008 [Janelia]
10 */
11 #include <p7_config.h>
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #ifndef __APPLE_ALTIVEC__
17 #include <altivec.h>
18 #endif
19
20 #include "easel.h"
21 #include "esl_vmx.h"
22 #include "esl_vectorops.h"
23
24 #include "hmmer.h"
25 #include "impl_vmx.h"
26
27 /*****************************************************************
28 * 1. Null2 estimation algorithms.
29 *****************************************************************/
30
31 /* Function: p7_Null2_ByExpectation()
32 * Synopsis: Calculate null2 model from posterior probabilities.
33 * Incept: SRE, Mon Aug 18 08:32:55 2008 [Janelia]
34 *
35 * Purpose: Identical to <p7_GNull2_ByExpectation()> except that
36 * <om>, <pp> are VMX optimized versions of the profile
37 * and the residue posterior probability matrix. See
38 * <p7_GNull2_ByExpectation()> documentation.
39 *
40 * Args: om - profile, in any mode, target length model set to <L>
41 * pp - posterior prob matrix, for <om> against domain envelope <dsq+i-1> (offset)
42 * null2 - RETURN: null2 log odds scores per residue; <0..Kp-1>; caller allocated space
43 */
44 int
45 p7_Null2_ByExpectation(const P7_OPROFILE *om, const P7_OMX *pp, float *null2)
46 {
47 int M = om->M;
48 int Ld = pp->L;
49 int Q = p7O_NQF(M);
50 float *xmx = pp->xmx; /* enables use of XMXo(i,s) macro */
51 float norm;
52 float xfactor;
53 int i,q,x;
54
55 vector float *rp;
56 vector float sv;
57 vector float zerov;
58
59 zerov = (vector float) vec_splat_u32(0);
60
61 /* Calculate expected # of times that each emitting state was used
62 * in generating the Ld residues in this domain.
63 * The 0 row in <wrk> is used to hold these numbers.
64 */
65 memcpy(pp->dpf[0], pp->dpf[1], sizeof(vector float) * 3 * Q);
66 XMXo(0,p7X_N) = XMXo(1,p7X_N);
67 XMXo(0,p7X_C) = XMXo(1,p7X_C); /* 0.0 */
68 XMXo(0,p7X_J) = XMXo(1,p7X_J); /* 0.0 */
69
70 for (i = 2; i <= Ld; i++)
71 {
72 for (q = 0; q < Q; q++)
73 {
74 pp->dpf[0][q*3 + p7X_M] = vec_add(pp->dpf[i][q*3 + p7X_M], pp->dpf[0][q*3 + p7X_M]);
75 pp->dpf[0][q*3 + p7X_I] = vec_add(pp->dpf[i][q*3 + p7X_I], pp->dpf[0][q*3 + p7X_I]);
76 }
77 XMXo(0,p7X_N) += XMXo(i,p7X_N);
78 XMXo(0,p7X_C) += XMXo(i,p7X_C);
79 XMXo(0,p7X_J) += XMXo(i,p7X_J);
80 }
81
82 /* Convert those expected #'s to frequencies, to use as posterior weights. */
83 norm = 1.0 / (float) Ld;
84 sv = esl_vmx_set_float(norm);
85
86 for (q = 0; q < Q; q++)
87 {
88 pp->dpf[0][q*3 + p7X_M] = vec_madd(pp->dpf[0][q*3 + p7X_M], sv, zerov);
89 pp->dpf[0][q*3 + p7X_I] = vec_madd(pp->dpf[0][q*3 + p7X_I], sv, zerov);
90 }
91 XMXo(0,p7X_N) *= norm;
92 XMXo(0,p7X_C) *= norm;
93 XMXo(0,p7X_J) *= norm;
94
95 /* Calculate null2's emission odds, by taking posterior weighted sum
96 * over all emission vectors used in paths explaining the domain.
97 */
98 xfactor = XMXo(0, p7X_N) + XMXo(0, p7X_C) + XMXo(0, p7X_J);
99 for (x = 0; x < om->abc->K; x++)
100 {
101 sv = (vector float) vec_splat_u32(0);
102 rp = om->rfv[x];
103 for (q = 0; q < Q; q++)
104 {
105 sv = vec_madd(pp->dpf[0][q*3 + p7X_M], *rp, sv); rp++;
106 sv = vec_add(sv, pp->dpf[0][q*3 + p7X_I]); /* insert odds implicitly 1.0 */
107 }
108 null2[x] = esl_vmx_hsum_float(sv);
109 null2[x] += xfactor;
110 }
111 /* now null2[x] = \frac{f_d(x)}{f_0(x)} for all x in alphabet,
112 * 0..K-1, where f_d(x) are the ad hoc "null2" residue frequencies
113 * for this envelope.
114 */
115
116 /* make valid scores for all degeneracies, by averaging the odds ratios. */
117 esl_abc_FAvgScVec(om->abc, null2);
118 null2[om->abc->K] = 1.0; /* gap character */
119 null2[om->abc->Kp-2] = 1.0; /* nonresidue "*" */
120 null2[om->abc->Kp-1] = 1.0; /* missing data "~" */
121
122 /* ta-da */
123 return eslOK;
124 }
125
126
127 /* Function: p7_Null2_ByTrace()
128 * Synopsis: Assign null2 scores to an envelope by the sampling method.
129 * Incept: SRE, Mon Aug 18 10:22:49 2008 [Janelia]
130 *
131 * Purpose: Identical to <p7_GNull2_ByTrace()> except that
132 * <om>, <wrk> are VMX optimized versions of the profile
133 * and the residue posterior probability matrix. See
134 * <p7_GNull2_ByTrace()> documentation.
135 */
136 int
137 p7_Null2_ByTrace(const P7_OPROFILE *om, const P7_TRACE *tr, int zstart, int zend, P7_OMX *wrk, float *null2)
138 {
139 union { vector float v; float p[4]; } u;
140 int Q = p7O_NQF(om->M);
141 int Ld = 0;
142 float *xmx = wrk->xmx; /* enables use of XMXo macro */
143 float norm;
144 float xfactor;
145 int q, r;
146 int x;
147 int z;
148
149 vector float sv;
150 vector float *rp;
151 vector float zerov;
152
153 zerov = (vector float) vec_splat_u32(0);
154
155 /* We'll use the i=0 row in wrk for working space: dp[0][] and xmx[][0]. */
156 for (q = 0; q < Q; q++)
157 {
158 wrk->dpf[0][q*3 + p7X_M] = (vector float) vec_splat_u32(0);
159 wrk->dpf[0][q*3 + p7X_I] = (vector float) vec_splat_u32(0);
160 }
161 XMXo(0,p7X_N) = 0.0;
162 XMXo(0,p7X_C) = 0.0;
163 XMXo(0,p7X_J) = 0.0;
164
165 /* Calculate emitting state usage in this particular trace segment */
166 for (z = zstart; z <= zend; z++)
167 {
168 if (tr->i[z] == 0) continue; /* quick test for whether this trace elem emitted or not */
169 Ld++;
170 if (tr->k[z] > 0) /* must be an M or I */
171 { /* surely there's an easier way? but our workspace is striped, interleaved quads... */
172 q = p7X_NSCELLS * ( (tr->k[z] - 1) % Q) + p7X_M;
173 r = (tr->k[z] - 1) / Q;
174 u.v = wrk->dpf[0][q];
175 u.p[r] += 1.0; /* all this to increment a count by one! */
176 wrk->dpf[0][q] = u.v;
177 }
178 else /* emitted an x_i with no k; must be an N,C,J */
179 {
180 switch (tr->st[z]) {
181 case p7T_N: XMXo(0,p7X_N) += 1.0; break;
182 case p7T_C: XMXo(0,p7X_C) += 1.0; break;
183 case p7T_J: XMXo(0,p7X_J) += 1.0; break;
184 }
185 }
186 }
187 norm = 1.0 / (float) Ld;
188 sv = esl_vmx_set_float(norm);
189
190 for (q = 0; q < Q; q++)
191 {
192 wrk->dpf[0][q*3 + p7X_M] = vec_madd(wrk->dpf[0][q*3 + p7X_M], sv, zerov);
193 wrk->dpf[0][q*3 + p7X_I] = vec_madd(wrk->dpf[0][q*3 + p7X_I], sv, zerov);
194 }
195 XMXo(0,p7X_N) *= norm;
196 XMXo(0,p7X_C) *= norm;
197 XMXo(0,p7X_J) *= norm;
198
199 /* Calculate null2's emission odds, by taking posterior weighted sum
200 * over all emission vectors used in paths explaining the domain.
201 */
202 xfactor = XMXo(0,p7X_N) + XMXo(0,p7X_C) + XMXo(0,p7X_J);
203 for (x = 0; x < om->abc->K; x++)
204 {
205 sv = (vector float) vec_splat_u32(0);
206 rp = om->rfv[x];
207 for (q = 0; q < Q; q++)
208 {
209 sv = vec_madd(wrk->dpf[0][q*3 + p7X_M], *rp, sv); rp++;
210 sv = vec_add(sv, wrk->dpf[0][q*3 + p7X_I]); /* insert emission odds implicitly 1.0 */
211 // sv = _mm_add_ps(sv, _mm_mul_ps(wrk->dpf[0][q*3 + p7X_I], *rp)); rp++;
212 }
213 null2[x] = esl_vmx_hsum_float(sv);
214 null2[x] += xfactor;
215 }
216
217 /* now null2[x] = \frac{f_d(x)}{f_0(x)} for all x in alphabet,
218 * 0..K-1, where f_d(x) are the ad hoc "null2" residue frequencies
219 * for this envelope.
220 */
221
222 /* make valid scores for all degeneracies, by averaging the odds ratios. */
223 esl_abc_FAvgScVec(om->abc, null2);
224 null2[om->abc->K] = 1.0; /* gap character */
225 null2[om->abc->Kp-2] = 1.0; /* nonresidue "*" */
226 null2[om->abc->Kp-1] = 1.0; /* missing data "~" */
227 return eslOK;
228 }
229
230
231 /*****************************************************************
232 * 2. Benchmark driver
233 *****************************************************************/
234 #ifdef p7NULL2_BENCHMARK
235 /*
236 icc -O3 -static -o null2_benchmark -I.. -L.. -I../../easel -L../../easel -Dp7NULL2_BENCHMARK null2.c -lhmmer -leasel -lm
237 ./null2_benchmark <hmmfile> Does the expectation version.
238 ./null2_benchmark -t <hmmfile> Does the stochastic-traceback-dependent version.
239 (This version isn't really dependent on M, so Mc/s may not be an appropriate measure.)
240
241 RRM_1 (M=72) Caudal_act (M=136) SMC_N (M=1151)
242 ----------------- ------------------ ---------------
243 21 Aug 2008 3.00u (480 Mc/s) 5.45u (499 Mc/s) 77.56u (297 Mc/s)
244 -t 21 Aug 2008 30.50u (47 Mc/s) 44.96u (61 Mc/s) 32.03u*10 ( 72 Mc/s)
245
246 */
247 #include <p7_config.h>
248
249 #include "easel.h"
250 #include "esl_alphabet.h"
251 #include "esl_getopts.h"
252 #include "esl_random.h"
253 #include "esl_randomseq.h"
254 #include "esl_stopwatch.h"
255
256 #include "hmmer.h"
257 #include "impl_vmx.h"
258
259 static ESL_OPTIONS options[] = {
260 /* name type default env range toggles reqs incomp help docgroup*/
261 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
262 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
263 { "-t", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "benchmark the trace-dependent version of null2", 0 },
264 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
265 { "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
266
267 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
268 };
269 static char usage[] = "[-options] <hmmfile>";
270 static char banner[] = "benchmark driver for null2 estimation, VMX version";
271
272 int
273 main(int argc, char **argv)
274 {
275 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
276 char *hmmfile = esl_opt_GetArg(go, 1);
277 ESL_STOPWATCH *w = esl_stopwatch_Create();
278 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
279 ESL_ALPHABET *abc = NULL;
280 P7_HMMFILE *hfp = NULL;
281 P7_HMM *hmm = NULL;
282 P7_BG *bg = NULL;
283 P7_PROFILE *gm = NULL;
284 P7_OPROFILE *om = NULL;
285 P7_OMX *ox1 = NULL;
286 P7_OMX *ox2 = NULL;
287 int L = esl_opt_GetInteger(go, "-L");
288 int N = esl_opt_GetInteger(go, "-N");
289 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
290 float null2[p7_MAXCODE];
291 int i,j,d,pos;
292 int nsamples = 200;
293 float fsc, bsc;
294 double Mcs;
295
296 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
297 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
298
299 bg = p7_bg_Create(abc); p7_bg_SetLength(bg, L);
300 gm = p7_profile_Create(hmm->M, abc); p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
301 om = p7_oprofile_Create(gm->M, abc); p7_oprofile_Convert(gm, om);
302 p7_oprofile_ReconfigLength(om, L);
303
304 ox1 = p7_omx_Create(gm->M, L, L);
305 ox2 = p7_omx_Create(gm->M, L, L);
306
307 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
308 p7_Forward (dsq, L, om, ox1, &fsc);
309
310 if (esl_opt_GetBoolean(go, "-t"))
311 {
312 P7_TRACE *tr = p7_trace_Create();
313 float *n2sc = malloc(sizeof(float) * (L+1));
314
315 esl_stopwatch_Start(w);
316 for (i = 0; i < N; i++)
317 { /* This is approximately what p7_domaindef.c::region_trace_ensemble() is doing: */
318 for (j = 0; j < nsamples; j++)
319 {
320 p7_StochasticTrace(r, dsq, L, om, ox1, tr);
321 p7_trace_Index(tr);
322 pos = 1;
323 for (d = 0; d < tr->ndom; d++)
324 {
325 p7_Null2_ByTrace(om, tr, tr->tfrom[d], tr->tto[d], ox2, null2);
326 for (; pos <= tr->sqfrom[d]; pos++) n2sc[pos] += 1.0;
327 for (; pos < tr->sqto[d]; pos++) n2sc[pos] += null2[dsq[pos]];
328 }
329 for (; pos <= L; pos++) n2sc[pos] += 1.0;
330 p7_trace_Reuse(tr);
331 }
332
333 for (pos = 1; pos <= L; pos++)
334 n2sc[pos] = logf(n2sc[pos] / nsamples);
335 }
336 esl_stopwatch_Stop(w);
337
338 free(n2sc);
339 p7_trace_Destroy(tr);
340 }
341 else
342 {
343 p7_Backward(dsq, L, om, ox1, ox2, &bsc);
344 p7_Decoding(om, ox1, ox2, ox2);
345
346 esl_stopwatch_Start(w);
347 for (i = 0; i < N; i++)
348 p7_Null2_ByExpectation(om, ox2, null2);
349 esl_stopwatch_Stop(w);
350 }
351
352
353 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) w->user;
354 esl_stopwatch_Display(stdout, w, "# CPU time: ");
355 printf("# M = %d\n", gm->M);
356 printf("# %.1f Mc/s\n", Mcs);
357
358 free(dsq);
359 p7_omx_Destroy(ox1);
360 p7_omx_Destroy(ox2);
361 p7_oprofile_Destroy(om);
362 p7_profile_Destroy(gm);
363 p7_bg_Destroy(bg);
364 p7_hmm_Destroy(hmm);
365 p7_hmmfile_Close(hfp);
366 esl_alphabet_Destroy(abc);
367 esl_stopwatch_Destroy(w);
368 esl_randomness_Destroy(r);
369 esl_getopts_Destroy(go);
370 return 0;
371 }
372 #endif /*p7NULL2_BENCHMARK*/
373 /*------------------ end, benchmark driver ----------------------*/
374
375
376
377
378 /*****************************************************************
379 * 3. Unit tests
380 *****************************************************************/
381 #ifdef p7NULL2_TESTDRIVE
382 #include "esl_random.h"
383 #include "esl_randomseq.h"
384 #include "esl_vectorops.h"
385
386 /* compare results to GDecoding(). */
387 static void
388 utest_null2_expectation(ESL_RANDOMNESS *r, ESL_ALPHABET *abc, P7_BG *bg, int M, int L, int N, float tolerance)
389 {
390 char *msg = "null2 unit test failed";
391 P7_HMM *hmm = NULL;
392 P7_PROFILE *gm = NULL;
393 P7_OPROFILE *om = NULL;
394 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
395 P7_OMX *fwd = p7_omx_Create(M, L, L);
396 P7_OMX *bck = p7_omx_Create(M, L, L);
397 P7_OMX *pp = p7_omx_Create(M, L, L);
398 P7_GMX *gxf = p7_gmx_Create(M, L);
399 P7_GMX *gxb = p7_gmx_Create(M, L);
400 P7_GMX *gpp = p7_gmx_Create(M, L);
401 float *on2 = malloc(sizeof(float) * abc->Kp);
402 float *gn2 = malloc(sizeof(float) * abc->Kp);
403 float fsc1, fsc2;
404 float bsc1, bsc2;
405
406 if (!gn2 || !on2) esl_fatal(msg);
407
408 if (p7_oprofile_Sample(r, abc, bg, M, L, &hmm, &gm, &om) != eslOK) esl_fatal(msg);
409 while (N--)
410 {
411 if (esl_rsq_xfIID(r, bg->f, abc->K, L, dsq) != eslOK) esl_fatal(msg);
412 if (p7_Forward (dsq, L, om, fwd, &fsc1) != eslOK) esl_fatal(msg);
413 if (p7_Backward (dsq, L, om, fwd, bck, &bsc1) != eslOK) esl_fatal(msg);
414 if (p7_Decoding(om, fwd, bck, pp) != eslOK) esl_fatal(msg);
415 if (p7_Null2_ByExpectation(om, pp, on2) != eslOK) esl_fatal(msg);
416
417 if (p7_GForward (dsq, L, gm, gxf, &fsc2) != eslOK) esl_fatal(msg);
418 if (p7_GBackward(dsq, L, gm, gxb, &bsc2) != eslOK) esl_fatal(msg);
419 if (p7_GDecoding(gm, gxf, gxb, gpp) != eslOK) esl_fatal(msg);
420 if (p7_GNull2_ByExpectation(gm, gpp, gn2) != eslOK) esl_fatal(msg);
421
422 if (esl_vec_FCompare(gn2, on2, abc->Kp, tolerance) != eslOK) esl_fatal(msg);
423 }
424
425 p7_gmx_Destroy(gpp);
426 p7_gmx_Destroy(gxf);
427 p7_gmx_Destroy(gxb);
428 p7_omx_Destroy(pp);
429 p7_omx_Destroy(fwd);
430 p7_omx_Destroy(bck);
431 free(on2);
432 free(gn2);
433 free(dsq);
434 p7_oprofile_Destroy(om);
435 p7_profile_Destroy(gm);
436 p7_hmm_Destroy(hmm);
437 }
438 #endif /*p7NULL2_TESTDRIVE*/
439 /*--------------------- end, unit tests -------------------------*/
440
441
442
443
444 /*****************************************************************
445 * 4. Test driver
446 *****************************************************************/
447 #ifdef p7NULL2_TESTDRIVE
448 /*
449 gcc -g -Wall -maltivec -std=gnu99 -o null2_utest -I.. -L.. -I../../easel -L../../easel -Dp7NULL2_TESTDRIVE null2.c -lhmmer -leasel -lm
450 ./null2_utest
451 */
452 #include <p7_config.h>
453
454 #include "easel.h"
455 #include "esl_alphabet.h"
456 #include "esl_getopts.h"
457 #include "esl_random.h"
458
459 #include "hmmer.h"
460 #include "impl_vmx.h"
461
462 static ESL_OPTIONS options[] = {
463 /* name type default env range toggles reqs incomp help docgroup*/
464 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
465 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
466 { "-t", eslARG_REAL, "0.01", NULL, NULL, NULL, NULL, NULL, "floating point comparison tolerance", 0 },
467 { "-L", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "size of random sequences to sample", 0 },
468 { "-M", eslARG_INT, "72", NULL, NULL, NULL, NULL, NULL, "size of random models to sample", 0 },
469 { "-N", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "number of random sequences to sample", 0 },
470 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
471 };
472 static char usage[] = "[-options]";
473 static char banner[] = "test driver for VMX implementation of null2 model";
474
475 int
476 main(int argc, char **argv)
477 {
478 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
479 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
480 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
481 P7_BG *bg = p7_bg_Create(abc);
482 int M = esl_opt_GetInteger(go, "-M");
483 int L = esl_opt_GetInteger(go, "-L");
484 int N = esl_opt_GetInteger(go, "-N");
485 float tol = esl_opt_GetReal (go, "-t");
486
487 p7_FLogsumInit();
488
489 utest_null2_expectation(r, abc, bg, M, L, N, tol);
490
491 esl_getopts_Destroy(go);
492 esl_randomness_Destroy(r);
493 esl_alphabet_Destroy(abc);
494 p7_bg_Destroy(bg);
495 return eslOK;
496 }
497 #endif /*p7NULL2_TESTDRIVE*/
498 /*-------------------- end, test driver -------------------------*/
499
500
501
502