Debian Codegraph @

hmmer/src/impl_neon/p7_oprofile.c

raw symbols SCIP
1 /* Routines for the P7_OPROFILE structure:
2 * a search profile in an optimized implementation.
3 *
4 * Contents:
5 * 1. The P7_OPROFILE object: allocation, initialization, destruction.
6 * 2. Conversion from generic P7_PROFILE to optimized P7_OPROFILE
7 * 3. Conversion from optimized P7_OPROFILE to compact score arrays
8 * 4. Debugging and development utilities.
9 * 5. Benchmark driver.
10 * 6. Unit tests.
11 * 7. Test driver.
12 * 8. Example.
13 */
14 #include <p7_config.h>
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <math.h> /* roundf() */
19
20 #include <arm_neon.h> /* NEON */
21
22 #include "easel.h"
23 #include "esl_random.h"
24 #include "esl_neon.h"
25 #include "esl_vectorops.h"
26
27 #include "hmmer.h"
28 #include "impl_neon.h"
29
30 static uint8_t unbiased_byteify(P7_OPROFILE *om, float sc);
31 static uint8_t biased_byteify(P7_OPROFILE *om, float sc);
32 static int16_t wordify(P7_OPROFILE *om, float sc);
33 static int sf_conversion(P7_OPROFILE *om);
34
35 /*****************************************************************
36 * 1. The P7_OPROFILE structure: a score profile.
37 *****************************************************************/
38
39 /* Function: p7_oprofile_Create()
40 * Synopsis: Allocate an optimized profile structure.
41 * Incept: ML, Fri Mar 12 11:03:02 2021 [Heidelberg]
42 *
43 * Purpose: Allocate for profiles of up to <allocM> nodes for digital alphabet <abc>.
44 *
45 * Throws: <NULL> on allocation error.
46 */
47 P7_OPROFILE *
48 p7_oprofile_Create(int allocM, const ESL_ALPHABET *abc)
49 {
50 int status;
51 P7_OPROFILE *om = NULL;
52 int nqb = p7O_NQB(allocM); /* # of uchar vectors needed for query */
53 int nqw = p7O_NQW(allocM); /* # of sword vectors needed for query */
54 int nqf = p7O_NQF(allocM); /* # of float vectors needed for query */
55 int nqs = nqb + p7O_EXTRA_SB;
56 int x;
57
58 /* level 0 */
59 ESL_ALLOC(om, sizeof(P7_OPROFILE));
60 om->rbv_mem = NULL;
61 om->sbv_mem = NULL;
62 om->rwv_mem = NULL;
63 om->twv_mem = NULL;
64 om->rfv_mem = NULL;
65 om->tfv_mem = NULL;
66 om->rbv = NULL;
67 om->sbv = NULL;
68 om->rwv = NULL;
69 om->twv = NULL;
70 om->rfv = NULL;
71 om->tfv = NULL;
72 om->clone = 0;
73
74 /* level 1 */
75 ESL_ALLOC(om->rbv_mem, sizeof(uint8x16_t) * nqb * abc->Kp +15); /* +15 is for manual 16-byte alignment */
76 ESL_ALLOC(om->sbv_mem, sizeof(uint8x16_t) * nqs * abc->Kp +15);
77 ESL_ALLOC(om->rwv_mem, sizeof(int16x8_t) * nqw * abc->Kp +15);
78 ESL_ALLOC(om->twv_mem, sizeof(int16x8_t) * nqw * p7O_NTRANS +15);
79 ESL_ALLOC(om->rfv_mem, sizeof(float32x4_t) * nqf * abc->Kp +15);
80 ESL_ALLOC(om->tfv_mem, sizeof(float32x4_t) * nqf * p7O_NTRANS +15);
81
82 ESL_ALLOC(om->rbv, sizeof(uint8x16_t *) * abc->Kp);
83 ESL_ALLOC(om->sbv, sizeof(uint8x16_t *) * abc->Kp);
84 ESL_ALLOC(om->rwv, sizeof(int16x8_t *) * abc->Kp);
85 ESL_ALLOC(om->rfv, sizeof(float32x4_t *) * abc->Kp);
86
87 /* align vector memory on 16-byte boundaries */
88 om->rbv[0] = (uint8x16_t *) (((unsigned long int) om->rbv_mem + 15) & (~0xf));
89 om->sbv[0] = (uint8x16_t *) (((unsigned long int) om->sbv_mem + 15) & (~0xf));
90 om->rwv[0] = (int16x8_t *) (((unsigned long int) om->rwv_mem + 15) & (~0xf));
91 om->twv = (int16x8_t *) (((unsigned long int) om->twv_mem + 15) & (~0xf));
92 om->rfv[0] = (float32x4_t *) (((unsigned long int) om->rfv_mem + 15) & (~0xf));
93 om->tfv = (float32x4_t *) (((unsigned long int) om->tfv_mem + 15) & (~0xf));
94
95 /* set the rest of the row pointers for match emissions */
96 for (x = 1; x < abc->Kp; x++) {
97 om->rbv[x] = om->rbv[0] + (x * nqb);
98 om->sbv[x] = om->sbv[0] + (x * nqs);
99 om->rwv[x] = om->rwv[0] + (x * nqw);
100 om->rfv[x] = om->rfv[0] + (x * nqf);
101 }
102 om->allocQ16 = nqb;
103 om->allocQ8 = nqw;
104 om->allocQ4 = nqf;
105
106 /* Remaining initializations */
107 om->tbm_b = 0;
108 om->tec_b = 0;
109 om->tjb_b = 0;
110 om->scale_b = 0.0f;
111 om->base_b = 0;
112 om->bias_b = 0;
113
114 om->scale_w = 0.0f;
115 om->base_w = 0;
116 om->ddbound_w = 0;
117 om->ncj_roundoff = 0.0f;
118
119 for (x = 0; x < p7_NOFFSETS; x++) om->offs[x] = -1;
120 for (x = 0; x < p7_NEVPARAM; x++) om->evparam[x] = p7_EVPARAM_UNSET;
121 for (x = 0; x < p7_NCUTOFFS; x++) om->cutoff[x] = p7_CUTOFF_UNSET;
122 for (x = 0; x < p7_MAXABET; x++) om->compo[x] = p7_COMPO_UNSET;
123
124 om->name = NULL;
125 om->acc = NULL;
126 om->desc = NULL;
127
128 /* in a P7_OPROFILE, we always allocate for the optional RF, CS annotation.
129 * we only rely on the leading \0 to signal that it's unused, but
130 * we initialize all this memory to zeros to shut valgrind up about
131 * fwrite'ing uninitialized memory in the io functions.
132 */
133 ESL_ALLOC(om->rf, sizeof(char) * (allocM+2));
134 ESL_ALLOC(om->mm, sizeof(char) * (allocM+2));
135 ESL_ALLOC(om->cs, sizeof(char) * (allocM+2));
136 ESL_ALLOC(om->consensus, sizeof(char) * (allocM+2));
137 memset(om->rf, '\0', sizeof(char) * (allocM+2));
138 memset(om->mm, '\0', sizeof(char) * (allocM+2));
139 memset(om->cs, '\0', sizeof(char) * (allocM+2));
140 memset(om->consensus,'\0', sizeof(char) * (allocM+2));
141
142 om->abc = abc;
143 om->L = 0;
144 om->M = 0;
145 om->max_length = -1;
146 om->allocM = allocM;
147 om->mode = p7_NO_MODE;
148 om->nj = 0.0f;
149 return om;
150
151 ERROR:
152 p7_oprofile_Destroy(om);
153 return NULL;
154 }
155
156 /* Function: p7_oprofile_IsLocal()
157 * Synopsis: Returns TRUE if profile is in local alignment mode.
158 * Incept: SRE, Sat Aug 16 08:46:00 2008 [Janelia]
159 */
160 int
161 p7_oprofile_IsLocal(const P7_OPROFILE *om)
162 {
163 if (om->mode == p7_LOCAL || om->mode == p7_UNILOCAL) return TRUE;
164 return FALSE;
165 }
166
167 /* Function: p7_oprofile_Destroy()
168 * Synopsis: Frees an optimized profile structure.
169 * Incept: SRE, Sun Nov 25 12:22:21 2007 [Casa de Gatos]
170 */
171 void
172 p7_oprofile_Destroy(P7_OPROFILE *om)
173 {
174 if (om == NULL) return;
175
176 if (om->clone == 0)
177 {
178 if (om->rbv_mem != NULL) free(om->rbv_mem);
179 if (om->sbv_mem != NULL) free(om->sbv_mem);
180 if (om->rwv_mem != NULL) free(om->rwv_mem);
181 if (om->twv_mem != NULL) free(om->twv_mem);
182 if (om->rfv_mem != NULL) free(om->rfv_mem);
183 if (om->tfv_mem != NULL) free(om->tfv_mem);
184 if (om->rbv != NULL) free(om->rbv);
185 if (om->sbv != NULL) free(om->sbv);
186 if (om->rwv != NULL) free(om->rwv);
187 if (om->rfv != NULL) free(om->rfv);
188 if (om->name != NULL) free(om->name);
189 if (om->acc != NULL) free(om->acc);
190 if (om->desc != NULL) free(om->desc);
191 if (om->rf != NULL) free(om->rf);
192 if (om->mm != NULL) free(om->mm);
193 if (om->cs != NULL) free(om->cs);
194 if (om->consensus != NULL) free(om->consensus);
195 }
196
197 free(om);
198 }
199
200 /* Function: p7_oprofile_Sizeof()
201 * Synopsis: Return the allocated size of a <P7_OPROFILE>.
202 * Incept: ML, Fri Mar 12 11:08:30 2021 [Heidelberg]
203 *
204 * Purpose: Returns the allocated size of a <P7_OPROFILE>,
205 * in bytes.
206 */
207 size_t
208 p7_oprofile_Sizeof(P7_OPROFILE *om)
209 {
210 size_t n = 0;
211 int nqb = om->allocQ16; /* # of uchar vectors needed for query */
212 int nqw = om->allocQ8; /* # of sword vectors needed for query */
213 int nqf = om->allocQ4; /* # of float vectors needed for query */
214 int nqs = nqb + p7O_EXTRA_SB;
215
216 /* Stuff below exactly mirrors the malloc()'s in
217 * p7_oprofile_Create(); so even though we could
218 * write this more compactly, leave it like this
219 * w/ one:one correspondence to _Create(), for
220 * maintainability and clarity.
221 */
222 n += sizeof(P7_OPROFILE);
223 n += sizeof(uint8x16_t) * nqb * om->abc->Kp +15; /* om->rbv_mem */
224 n += sizeof(uint8x16_t) * nqs * om->abc->Kp +15; /* om->sbv_mem */
225 n += sizeof(int16x8_t) * nqw * om->abc->Kp +15; /* om->rwv_mem */
226 n += sizeof(int16x8_t) * nqw * p7O_NTRANS +15; /* om->twv_mem */
227 n += sizeof(float32x4_t) * nqf * om->abc->Kp +15; /* om->rfv_mem */
228 n += sizeof(float32x4_t) * nqf * p7O_NTRANS +15; /* om->tfv_mem */
229
230 n += sizeof(uint8x16_t *) * om->abc->Kp; /* om->rbv */
231 n += sizeof(uint8x16_t *) * om->abc->Kp; /* om->sbv */
232 n += sizeof(int16x8_t *) * om->abc->Kp; /* om->rwv */
233 n += sizeof(float32x4_t *) * om->abc->Kp; /* om->rfv */
234
235 n += sizeof(char) * (om->allocM+2); /* om->rf */
236 n += sizeof(char) * (om->allocM+2); /* om->mm */
237 n += sizeof(char) * (om->allocM+2); /* om->cs */
238 n += sizeof(char) * (om->allocM+2); /* om->consensus */
239
240 return n;
241 }
242
243
244 /* TODO: this is not following the _Copy interface guidelines; it's a _Clone */
245 /* TODO: its documentation header is a cut/paste of _Create; FIXME */
246 /* Function: p7_oprofile_Copy()
247 * Synopsis: Allocate an optimized profile structure.
248 * Incept: ML, Fri Mar 12 11:13:38 2021 [Heidelberg]
249 *
250 * Purpose: Allocate for profiles of up to <allocM> nodes for digital alphabet <abc>.
251 *
252 * Throws: <NULL> on allocation error.
253 */
254 P7_OPROFILE *
255 p7_oprofile_Copy(P7_OPROFILE *om1)
256 {
257 int x, y;
258 int status;
259
260 int nqb = p7O_NQB(om1->allocM); /* # of uchar vectors needed for query */
261 int nqw = p7O_NQW(om1->allocM); /* # of sword vectors needed for query */
262 int nqf = p7O_NQF(om1->allocM); /* # of float vectors needed for query */
263 int nqs = nqb + p7O_EXTRA_SB;
264
265 size_t size = sizeof(char) * (om1->allocM+2);
266
267 P7_OPROFILE *om2 = NULL;
268
269 const ESL_ALPHABET *abc = om1->abc;
270
271 /* level 0 */
272 ESL_ALLOC(om2, sizeof(P7_OPROFILE));
273 om2->rbv_mem = NULL;
274 om2->sbv_mem = NULL;
275 om2->rwv_mem = NULL;
276 om2->twv_mem = NULL;
277 om2->rfv_mem = NULL;
278 om2->tfv_mem = NULL;
279 om2->rbv = NULL;
280 om2->sbv = NULL;
281 om2->rwv = NULL;
282 om2->twv = NULL;
283 om2->rfv = NULL;
284 om2->tfv = NULL;
285
286 /* level 1 */
287 ESL_ALLOC(om2->rbv_mem, sizeof(uint8x16_t) * nqb * abc->Kp +15); /* +15 is for manual 16-byte alignment */
288 ESL_ALLOC(om2->sbv_mem, sizeof(uint8x16_t) * nqs * abc->Kp +15);
289 ESL_ALLOC(om2->rwv_mem, sizeof(int16x8_t) * nqw * abc->Kp +15);
290 ESL_ALLOC(om2->twv_mem, sizeof(int16x8_t) * nqw * p7O_NTRANS +15);
291 ESL_ALLOC(om2->rfv_mem, sizeof(float32x4_t) * nqf * abc->Kp +15);
292 ESL_ALLOC(om2->tfv_mem, sizeof(float32x4_t) * nqf * p7O_NTRANS +15);
293
294 ESL_ALLOC(om2->rbv, sizeof(uint8x16_t *) * abc->Kp);
295 ESL_ALLOC(om2->sbv, sizeof(uint8x16_t *) * abc->Kp);
296 ESL_ALLOC(om2->rwv, sizeof(int16x8_t *) * abc->Kp);
297 ESL_ALLOC(om2->rfv, sizeof(float32x4_t *) * abc->Kp);
298
299 /* align vector memory on 16-byte boundaries */
300 om2->rbv[0] = (uint8x16_t *) (((unsigned long int) om2->rbv_mem + 15) & (~0xf));
301 om2->sbv[0] = (uint8x16_t *) (((unsigned long int) om2->sbv_mem + 15) & (~0xf));
302 om2->rwv[0] = (int16x8_t *) (((unsigned long int) om2->rwv_mem + 15) & (~0xf));
303 om2->twv = (int16x8_t *) (((unsigned long int) om2->twv_mem + 15) & (~0xf));
304 om2->rfv[0] = (float32x4_t *) (((unsigned long int) om2->rfv_mem + 15) & (~0xf));
305 om2->tfv = (float32x4_t *) (((unsigned long int) om2->tfv_mem + 15) & (~0xf));
306
307 /* copy the vector data */
308 memcpy(om2->rbv[0], om1->rbv[0], sizeof(uint8x16_t) * nqb * abc->Kp);
309 memcpy(om2->sbv[0], om1->sbv[0], sizeof(uint8x16_t) * nqs * abc->Kp);
310 memcpy(om2->rwv[0], om1->rwv[0], sizeof(int16x8_t) * nqw * abc->Kp);
311 memcpy(om2->rfv[0], om1->rfv[0], sizeof(float32x4_t) * nqf * abc->Kp);
312
313 /* set the rest of the row pointers for match emissions */
314 for (x = 1; x < abc->Kp; x++) {
315 om2->rbv[x] = om2->rbv[0] + (x * nqb);
316 om2->sbv[x] = om2->sbv[0] + (x * nqs);
317 om2->rwv[x] = om2->rwv[0] + (x * nqw);
318 om2->rfv[x] = om2->rfv[0] + (x * nqf);
319 }
320 om2->allocQ16 = nqb;
321 om2->allocQ8 = nqw;
322 om2->allocQ4 = nqf;
323
324 /* Remaining initializations */
325 om2->tbm_b = om1->tbm_b;
326 om2->tec_b = om1->tec_b;
327 om2->tjb_b = om1->tjb_b;
328 om2->scale_b = om1->scale_b;
329 om2->base_b = om1->base_b;
330 om2->bias_b = om1->bias_b;
331
332 om2->scale_w = om1->scale_w;
333 om2->base_w = om1->base_w;
334 om2->ddbound_w = om1->ddbound_w;
335 om2->ncj_roundoff = om1->ncj_roundoff;
336
337 for (x = 0; x < p7_NOFFSETS; x++) om2->offs[x] = om1->offs[x];
338 for (x = 0; x < p7_NEVPARAM; x++) om2->evparam[x] = om1->evparam[x];
339 for (x = 0; x < p7_NCUTOFFS; x++) om2->cutoff[x] = om1->cutoff[x];
340 for (x = 0; x < p7_MAXABET; x++) om2->compo[x] = om1->compo[x];
341
342 for (x = 0; x < nqw * p7O_NTRANS; ++x) om2->twv[x] = om1->twv[x];
343 for (x = 0; x < nqf * p7O_NTRANS; ++x) om2->tfv[x] = om1->tfv[x];
344
345 for (x = 0; x < p7O_NXSTATES; x++)
346 for (y = 0; y < p7O_NXTRANS; y++)
347 {
348 om2->xw[x][y] = om1->xw[x][y];
349 om2->xf[x][y] = om1->xf[x][y];
350 }
351
352 if ((status = esl_strdup(om1->name, -1, &om2->name)) != eslOK) goto ERROR;
353 if ((status = esl_strdup(om1->acc, -1, &om2->acc)) != eslOK) goto ERROR;
354 if ((status = esl_strdup(om1->desc, -1, &om2->desc)) != eslOK) goto ERROR;
355
356 /* in a P7_OPROFILE, we always allocate for the optional RF, CS annotation.
357 * we only rely on the leading \0 to signal that it's unused, but
358 * we initialize all this memory to zeros to shut valgrind up about
359 * fwrite'ing uninitialized memory in the io functions.
360 */
361 ESL_ALLOC(om2->rf, size);
362 ESL_ALLOC(om2->mm, size);
363 ESL_ALLOC(om2->cs, size);
364 ESL_ALLOC(om2->consensus, size);
365
366 memcpy(om2->rf, om1->rf, size);
367 memcpy(om2->mm, om1->mm, size);
368 memcpy(om2->cs, om1->cs, size);
369 memcpy(om2->consensus, om1->consensus, size);
370
371 om2->abc = om1->abc;
372 om2->L = om1->L;
373 om2->M = om1->M;
374 om2->allocM = om1->allocM;
375 om2->mode = om1->mode;
376 om2->nj = om1->nj;
377 om2->max_length = om1->max_length;
378
379 om2->clone = om1->clone;
380
381 return om2;
382
383 ERROR:
384 p7_oprofile_Destroy(om2);
385 return NULL;
386 }
387
388 /* Function: p7_oprofile_Clone()
389 * Synopsis: Allocate a cloned copy of the optimized profile structure. All
390 * allocated memory from the original profile is not reallocated.
391 * The cloned copy will point to the same memory as the original.
392 * Incept: SRE, Sun Nov 25 12:03:19 2007 [Casa de Gatos]
393 *
394 * Purpose: Quick copy of an optimized profile used in mutiple threads.
395 *
396 * Throws: <NULL> on allocation error.
397 */
398 P7_OPROFILE *
399 p7_oprofile_Clone(const P7_OPROFILE *om1)
400 {
401 int status;
402
403 P7_OPROFILE *om2 = NULL;
404
405 ESL_ALLOC(om2, sizeof(P7_OPROFILE));
406 memcpy(om2, om1, sizeof(P7_OPROFILE));
407
408 om2->clone = 1;
409 return om2;
410
411 ERROR:
412 p7_oprofile_Destroy(om2);
413 return NULL;
414 }
415
416
417 /* Function: p7_oprofile_UpdateFwdEmissionScores()
418 * Synopsis: Update the Forward/Backward part of the optimized profile
419 * match emissions to account for new background distribution.
420 *
421 * Purpose: This implementation re-orders the loops used to access/modify
422 * the rfv array relative to how it's accessed for example in
423 * fb_conversion(), to minimize the required size of sc_arr.
424 *
425 * Args: om - optimized profile to be updated.
426 * bg - the new bg distribution
427 * fwd_emissions - precomputed Fwd (float) residue emission
428 * probabilities in serial order (gathered from
429 * the optimized striped <om> with
430 * p7_oprofile_GetFwdEmissionArray() ).
431 * sc_arr Preallocated array of at least Kp*4 floats
432 */
433 int
434 p7_oprofile_UpdateFwdEmissionScores(P7_OPROFILE *om, P7_BG *bg, float *fwd_emissions, float *sc_arr)
435 {
436 int M = om->M; /* length of the query */
437 int k, q, x, z;
438 int nq = p7O_NQF(M); /* segment length; total # of striped vectors needed */
439 int K = om->abc->K;
440 int Kp = om->abc->Kp;
441 union { float32x4_t v; float x[4]; } tmp; /* used to align and load simd minivectors */
442
443
444 for (k = 1, q = 0; q < nq; q++, k++) {
445
446 //First compute the core characters of the alphabet
447 for (x = 0; x < K; x++) {
448 for (z = 0; z < 4; z++) {
449 if (k+ z*nq <= M) sc_arr[z*Kp + x] = (om->mm && om->mm[(k+z*nq)]=='m') ? 0 : log( (double)(fwd_emissions[Kp * (k+z*nq) + x])/bg->f[x]);
450 else sc_arr[z*Kp + x] = -eslINFINITY;
451
452 tmp.x[z] = sc_arr[z*Kp + x];
453 }
454 om->rfv[x][q] = esl_neon_expf((esl_neon_128f_t) tmp.v).f32x4;
455
456 }
457
458 /* gap, nonresidue, and missing data residue codes don't get set by FExpectScVec(),
459 * so do them
460 */
461 for (z = 0; z < 4; z++)
462 {
463 sc_arr[z*Kp + K] = -eslINFINITY; /* gap char - */
464 sc_arr[z*Kp + (Kp - 2)] = -eslINFINITY; /* nonresidue * */
465 sc_arr[z*Kp + (Kp - 1)] = -eslINFINITY; /* missing data ~ */
466 }
467
468 // Then compute corresponding scores for ambiguity codes.
469 for (z = 0; z < 4; z++)
470 esl_abc_FExpectScVec(om->abc, sc_arr+(z*Kp), bg->f);
471
472 //finish off the interleaved values
473 for (x = K; x < Kp; x++) {
474 for (z = 0; z < 4; z++)
475 tmp.x[z] = sc_arr[z*Kp + x]; // computed in FExpectScVec call above
476 om->rfv[x][q] = esl_neon_expf((esl_neon_128f_t) tmp.v).f32x4;
477 }
478 }
479
480 return eslOK;
481 }
482
483
484 /* Function: p7_oprofile_UpdateVitEmissionScores()
485 * Synopsis: Update the Viterbi part of the optimized profile match
486 * emissions to account for new background distribution.
487 *.
488 * Purpose: This implementation re-orders the loops used to access/modify
489 * the rmv array relative to how it's accessed for example in
490 * vf_conversion(), to minimize the required size of sc_arr.
491 *
492 * Args: om - optimized profile to be updated.
493 * bg - the new bg distribution
494 * fwd_emissions - precomputed Fwd (float) residue emission
495 * probabilities in serial order (gathered from
496 * the optimized striped <om> with
497 * p7_oprofile_GetFwdEmissionArray() ).
498 * sc_arr Preallocated array of at least Kp*8 floats
499 */
500 int
501 p7_oprofile_UpdateVitEmissionScores(P7_OPROFILE *om, P7_BG *bg, float *fwd_emissions, float *sc_arr)
502 {
503 int M = om->M; /* length of the query */
504 int k, q, x, z;
505 int nq = p7O_NQW(M); /* segment length; total # of striped vectors needed */
506 int K = om->abc->K;
507 int Kp = om->abc->Kp;
508 int idx;
509 union { int16x8_t v; int16_t i[8]; } tmp; /* used to align and load simd minivectors */
510
511 for (k = 1, q = 0; q < nq; q++, k++) {
512
513 //First compute the core characters of the alphabet
514 for (x = 0; x < K; x++) {
515 for (z = 0; z < 8; z++) {
516 idx = z*Kp + x;
517 if (k+ z*nq <= M) {
518 sc_arr[idx] = (om->mm && om->mm[(k+z*nq)]=='m') ? 0 : log( (double)(fwd_emissions[Kp * (k+z*nq) + x])/bg->f[x]);
519 tmp.i[z] = wordify(om, sc_arr[idx]);
520 }
521 else
522 {
523 sc_arr[idx] = -eslINFINITY;
524 tmp.i[z] = -32768;
525 }
526
527 }
528 om->rwv[x][q] = tmp.v;
529
530 }
531
532 // Then compute corresponding scores for ambiguity codes.
533 for (z = 0; z < 8; z++)
534 esl_abc_FExpectScVec(om->abc, sc_arr+(z*Kp), bg->f);
535
536 //finish off the interleaved values
537 for (x = K; x < Kp; x++) {
538 for (z = 0; z < 8; z++) {
539 idx = z*Kp + x;
540 if (x==K || x>Kp-3 || sc_arr[idx] == -eslINFINITY)
541 tmp.i[z] = -32768;
542 else
543 tmp.i[z] = wordify(om, sc_arr[idx]);
544 }
545 om->rwv[x][q] = tmp.v;
546 }
547 }
548
549 return eslOK;
550 }
551
552
553
554 /* Function: p7_oprofile_UpdateMSVEmissionScores()
555 * Synopsis: Update the MSV part of the optimized profile match
556 * emissions to account for new background distribution.
557 *.
558 * Purpose: This implementation re-orders the loops used to access/modify
559 * the rbv array relative to how it's accessed for example in
560 * mf_conversion(), to minimize the required size of sc_arr.
561 *
562 * Args: om - optimized profile to be updated.
563 * bg - the new bg distribution
564 * fwd_emissions - precomputed Fwd (float) residue emission
565 * probabilities in serial order (gathered from
566 * the optimized striped <om> with
567 * p7_oprofile_GetFwdEmissionArray() ).
568 * sc_arr Preallocated array of at least Kp*16 floats
569 */
570 int
571 p7_oprofile_UpdateMSVEmissionScores(P7_OPROFILE *om, P7_BG *bg, float *fwd_emissions, float *sc_arr)
572 {
573 int M = om->M; /* length of the query */
574 int k, q, x, z;
575 int nq = p7O_NQB(M); /* segment length; total # of striped vectors needed */
576 int K = om->abc->K;
577 int Kp = om->abc->Kp;
578 int idx;
579 float max = 0.0; /* maximum residue score: used for unsigned emission score bias */
580 union { uint8x16_t v; uint8_t i[16]; } tmp; /* used to align and load simd minivectors */
581
582
583 /* First we determine the basis for the limited-precision MSVFilter scoring system.
584 * Default: 1/3 bit units, base offset 190: range 0..255 => -190..65 => -63.3..21.7 bits
585 * See J2/66, J4/138 for analysis.
586 * This depends on having computed scores. I do this in a first pass, to get the max
587 * score ... then re-compute those scores so they can be converted to 8bit scores
588 */
589 for (k = 1, q = 0; q < nq; q++, k++) {
590 for (x = 0; x < K; x++) {
591 for (z = 0; z < 16; z++) {
592 idx = z*Kp + x;
593 if (k+ z*nq <= M && !(om->mm && om->mm[(k+z*nq)]=='m'))
594 max = ESL_MAX(max, log( (double)(fwd_emissions[Kp * (k+z*nq) + x])/bg->f[x]));
595 }
596 }
597 }
598 om->scale_b = 3.0 / eslCONST_LOG2; /* scores in units of third-bits */
599 om->base_b = 190;
600 om->bias_b = unbiased_byteify(om, -1.0 * max);
601
602 for (k = 1, q = 0; q < nq; q++, k++) {
603
604 //First compute the core characters of the alphabet
605 for (x = 0; x < K; x++) {
606 for (z = 0; z < 16; z++) {
607 idx = z*Kp + x;
608 if (k+ z*nq <= M) {
609 sc_arr[idx] = (om->mm && om->mm[(k+z*nq)]=='m') ? 0 : log( (double)(fwd_emissions[Kp * (k+z*nq) + x])/bg->f[x]);
610 tmp.i[z] = biased_byteify(om, sc_arr[idx]);
611 }
612 else
613 {
614 sc_arr[idx] = -eslINFINITY;
615 tmp.i[z] = 255;
616 }
617
618 }
619 om->rbv[x][q] = tmp.v;
620
621 }
622
623 // Then compute corresponding scores for ambiguity codes.
624 for (z = 0; z < 16; z++)
625 esl_abc_FExpectScVec(om->abc, sc_arr+(z*Kp), bg->f);
626
627 //finish off the interleaved values
628 for (x = K; x < Kp; x++) {
629 for (z = 0; z < 16; z++) {
630 idx = z*Kp + x;
631 if (x==K || x>Kp-3 || sc_arr[idx] == -eslINFINITY)
632 tmp.i[z] = 255;
633 else
634 tmp.i[z] = biased_byteify(om, sc_arr[idx]);
635 }
636 om->rbv[x][q] = tmp.v;
637 }
638 }
639
640 sf_conversion(om);
641
642 return eslOK;
643 }
644
645
646 /*----------------- end, P7_OPROFILE structure ------------------*/
647
648
649
650 /*****************************************************************
651 * 2. Conversion from generic P7_PROFILE to optimized P7_OPROFILE
652 *****************************************************************/
653
654 /* biased_byteify()
655 * Converts original log-odds residue score to a rounded biased uchar cost.
656 * Match emission scores for MSVFilter get this treatment.
657 * e.g. a score of +3.2, with scale 3.0 and bias 12, becomes 2.
658 * 3.2*3 = 9.6; rounded = 10; bias-10 = 2.
659 * When used, we add the bias, then subtract this cost.
660 * (A cost of +255 is our -infinity "prohibited event")
661 *
662 * ML: Contrary to the SSE implementation, we cast only at the end,
663 * because weird results were found on an AARCH64 platform when
664 * casting happened at the beginning.
665 */
666 static uint8_t
667 biased_byteify(P7_OPROFILE *om, float sc)
668 {
669 float b;
670 sc = -1.0f * roundf(om->scale_b * sc);
671 b = (sc > 255.0 - om->bias_b) ? 255.0 : sc + om->bias_b;
672 return (uint8_t) b;
673 }
674
675 /* unbiased_byteify()
676 * Convert original transition score to a rounded uchar cost
677 * Transition scores for MSVFilter get this treatment.
678 * e.g. a score of -2.1, with scale 3.0, becomes a cost of 6.
679 * (A cost of +255 is our -infinity "prohibited event")
680 */
681 static uint8_t
682 unbiased_byteify(P7_OPROFILE *om, float sc)
683 {
684 uint8_t b;
685
686 sc = -1.0f * roundf(om->scale_b * sc); /* ugh. sc is now an integer cost represented in a float... */
687 b = (sc > 255.) ? 255 : (uint8_t) sc; /* and now we cast and saturate it to an unsigned char cost... */
688 return b;
689 }
690
691 /* wordify()
692 * Converts log probability score to a rounded signed 16-bit integer cost.
693 * Both emissions and transitions for ViterbiFilter get this treatment.
694 * No bias term needed, because we use signed words.
695 * e.g. a score of +3.2, with scale 500.0, becomes +1600.
696 */
697 static int16_t
698 wordify(P7_OPROFILE *om, float sc)
699 {
700 sc = roundf(om->scale_w * sc);
701 if (sc >= 32767.0) return 32767;
702 else if (sc <= -32768.0) return -32768;
703 else return (int16_t) sc;
704 }
705
706
707 /* sf_conversion():
708 * Author: Bjarne Knudsen
709 *
710 * Generates the SSVFilter() parts of the profile <om> scores
711 * from the completed MSV score. This includes calculating
712 * special versions of the match scores for using the the
713 * ssv filter.
714 *
715 * Returns: <eslOK> on success.
716 *
717 * Throws: (no abnormal error conditions)
718 */
719 static int
720 sf_conversion(P7_OPROFILE *om)
721 {
722 int M = om->M; /* length of the query */
723 int nq = p7O_NQB(M); /* segment length; total # of striped vectors needed */
724 int x; /* counter over residues */
725 int q; /* q counts over total # of striped vectors, 0..nq-1 */
726 uint8x16_t tmp;
727 uint8x16_t tmp2;
728
729 /* We now want to fill out om->sbv with om->rbv - bias for use in the
730 * SSV filter. The only challenge is that the om->rbv values are
731 * unsigned and generally use the whole scale while the om->sbv
732 * values are signed. To solve that problem we perform the following
733 * calculation:
734 *
735 * ((127 + bias) - rbv) ^ 127
736 *
737 * where the subtraction is unsigned saturated and the addition is
738 * unsigned (it will not overflow, since bias is a small positive
739 * number). The f(x) = x ^ 127 combined with a change from unsigned
740 * to signed numbers have the same effect as f(x) = -x + 127. So if
741 * we regard the above as signed instead of unsigned it is equal to:
742 *
743 * -((127 + bias) - rbv) + 127 = rbv - bias
744 *
745 * which is what we want. The reason for this slightly complex idea
746 * is that we wish the transformation to be fast, especially for
747 * hmmscan where many models are loaded.
748 */
749
750 tmp = vreinterpretq_u8_s8(vmovq_n_s8((int8_t) (om->bias_b + 127)));
751 tmp2 = vreinterpretq_u8_s8(vmovq_n_s8(127));
752
753 for (x = 0; x < om->abc->Kp; x++)
754 {
755 for (q = 0; q < nq; q++) om->sbv[x][q] = veorq_u8(vqsubq_u8(tmp, om->rbv[x][q]), tmp2);
756 for (q = nq; q < nq + p7O_EXTRA_SB; q++) om->sbv[x][q] = om->sbv[x][q % nq];
757 }
758
759 return eslOK;
760 }
761
762 /* mf_conversion():
763 *
764 * This builds the MSVFilter() parts of the profile <om>, scores
765 * in lspace uchars (16-way parallel), by rescaling, rounding, and
766 * casting the scores in <gm>.
767 *
768 * Returns <eslOK> on success;
769 * throws <eslEINVAL> if <om> hasn't been allocated properly.
770 */
771 static int
772 mf_conversion(const P7_PROFILE *gm, P7_OPROFILE *om)
773 {
774 int M = gm->M; /* length of the query */
775 int nq = p7O_NQB(M); /* segment length; total # of striped vectors needed */
776 float max = 0.0; /* maximum residue score: used for unsigned emission score bias */
777 int x; /* counter over residues */
778 int q; /* q counts over total # of striped vectors, 0..nq-1 */
779 int k; /* the usual counter over model nodes 1..M */
780 int z; /* counter within elements of one SIMD minivector */
781 union { uint8x16_t v; uint8_t i[16]; } tmp; /* used to align and load simd minivectors */
782
783 if (nq > om->allocQ16) ESL_EXCEPTION(eslEINVAL, "optimized profile is too small to hold conversion");
784
785 /* First we determine the basis for the limited-precision MSVFilter scoring system.
786 * Default: 1/3 bit units, base offset 190: range 0..255 => -190..65 => -63.3..21.7 bits
787 * See J2/66, J4/138 for analysis.
788 */
789 for (x = 0; x < gm->abc->K; x++) max = ESL_MAX(max, esl_vec_FMax(gm->rsc[x], (M+1)*2));
790 om->scale_b = 3.0 / eslCONST_LOG2; /* scores in units of third-bits */
791 om->base_b = 190;
792 om->bias_b = unbiased_byteify(om, -1.0 * max);
793
794 /* striped match costs: start at k=1. */
795 for (x = 0; x < gm->abc->Kp; x++)
796 {
797 for (q = 0, k = 1; q < nq; q++, k++)
798 {
799 for (z = 0; z < 16; z++) tmp.i[z] = ((k+ z*nq <= M) ? biased_byteify(om, p7P_MSC(gm, k+z*nq, x)) : 255);
800 om->rbv[x][q] = tmp.v;
801 }
802 }
803
804 /* transition costs */
805 om->tbm_b = unbiased_byteify(om, logf(2.0f / ((float) gm->M * (float) (gm->M+1)))); /* constant B->Mk penalty */
806 om->tec_b = unbiased_byteify(om, logf(0.5f)); /* constant multihit E->C = E->J */
807 om->tjb_b = unbiased_byteify(om, logf(3.0f / (float) (gm->L+3))); /* this adopts the L setting of the parent profile */
808
809 sf_conversion(om);
810
811 return eslOK;
812 }
813
814
815 /* vf_conversion():
816 *
817 * This builds the ViterbiFilter() parts of the profile <om>, scores
818 * in lspace swords (8-way parallel), by rescaling, rounding, and
819 * casting the scores in <gm>.
820 *
821 * Returns <eslOK> on success;
822 * throws <eslEINVAL> if <om> hasn't been allocated properly.
823 */
824 static int
825 vf_conversion(const P7_PROFILE *gm, P7_OPROFILE *om)
826 {
827 int M = gm->M; /* length of the query */
828 int nq = p7O_NQW(M); /* segment length; total # of striped vectors needed */
829 int x; /* counter over residues */
830 int q; /* q counts over total # of striped vectors, 0..nq-1 */
831 int k; /* the usual counter over model nodes 1..M */
832 int kb; /* possibly offset base k for loading om's TSC vectors */
833 int z; /* counter within elements of one SIMD minivector */
834 int t; /* counter over transitions 0..7 = p7O_{BM,MM,IM,DM,MD,MI,II,DD}*/
835 int tg; /* transition index in gm */
836 int j; /* counter in interleaved vector arrays in the profile */
837 int ddtmp; /* used in finding worst DD transition bound */
838 int16_t maxval; /* used to prevent zero cost II */
839 int16_t val;
840 union { int16x8_t v; int16_t i[8]; } tmp; /* used to align and load simd minivectors */
841
842 if (nq > om->allocQ8) ESL_EXCEPTION(eslEINVAL, "optimized profile is too small to hold conversion");
843
844 /* First set the basis for the limited-precision scoring system.
845 * Default: 1/500 bit units, base offset 12000: range -32768..32767 => -44768..20767 => -89.54..41.53 bits
846 * See J4/138 for analysis.
847 */
848 om->scale_w = 500.0 / eslCONST_LOG2;
849 om->base_w = 12000;
850
851 /* striped match scores */
852 for (x = 0; x < gm->abc->Kp; x++)
853 for (k = 1, q = 0; q < nq; q++, k++)
854 {
855 for (z = 0; z < 8; z++) tmp.i[z] = ((k+ z*nq <= M) ? wordify(om, p7P_MSC(gm, k+z*nq, x)) : -32768);
856 om->rwv[x][q] = tmp.v;
857 }
858
859 /* Transition costs, all but the DD's. */
860 for (j = 0, k = 1, q = 0; q < nq; q++, k++)
861 {
862 for (t = p7O_BM; t <= p7O_II; t++) /* this loop of 7 transitions depends on the order in p7o_tsc_e */
863 {
864 switch (t) {
865 case p7O_BM: tg = p7P_BM; kb = k-1; maxval = 0; break; /* gm has tBMk stored off by one! start from k=0 not 1 */
866 case p7O_MM: tg = p7P_MM; kb = k-1; maxval = 0; break; /* MM, DM, IM vectors are rotated by -1, start from k=0 */
867 case p7O_IM: tg = p7P_IM; kb = k-1; maxval = 0; break;
868 case p7O_DM: tg = p7P_DM; kb = k-1; maxval = 0; break;
869 case p7O_MD: tg = p7P_MD; kb = k; maxval = 0; break; /* the remaining ones are straight up */
870 case p7O_MI: tg = p7P_MI; kb = k; maxval = 0; break;
871 case p7O_II: tg = p7P_II; kb = k; maxval = -1; break;
872 }
873
874 for (z = 0; z < 8; z++) {
875 val = ((kb+ z*nq < M) ? wordify(om, p7P_TSC(gm, kb+ z*nq, tg)) : -32768);
876 tmp.i[z] = (val <= maxval) ? val : maxval; /* do not allow an II transition cost of 0, or hell may occur. */
877 }
878 om->twv[j++] = tmp.v;
879 }
880 }
881
882 /* Finally the DD's, which are at the end of the optimized tsc vector; (j is already sitting there) */
883 for (k = 1, q = 0; q < nq; q++, k++)
884 {
885 for (z = 0; z < 8; z++) tmp.i[z] = ((k+ z*nq < M) ? wordify(om, p7P_TSC(gm, k+ z*nq, p7P_DD)) : -32768);
886 om->twv[j++] = tmp.v;
887 }
888
889 /* Specials. (Actually in same order in om and gm, but we copy in general form anyway.) */
890 /* VF CC,NN,JJ transitions hardcoded zero; -3.0 nat approximation used instead; this papers
891 * over a length independence problem, where the approximation weirdly outperforms the
892 * exact solution, probably indicating that the model's Pascal distribution is problematic,
893 * and the "approximation" is in fact closer to the One True Model, the mythic H4 supermodel.
894 * [xref J5/36]
895 */
896 om->xw[p7O_E][p7O_LOOP] = wordify(om, gm->xsc[p7P_E][p7P_LOOP]);
897 om->xw[p7O_E][p7O_MOVE] = wordify(om, gm->xsc[p7P_E][p7P_MOVE]);
898 om->xw[p7O_N][p7O_MOVE] = wordify(om, gm->xsc[p7P_N][p7P_MOVE]);
899 om->xw[p7O_N][p7O_LOOP] = 0; /* was wordify(om, gm->xsc[p7P_N][p7P_LOOP]); */
900 om->xw[p7O_C][p7O_MOVE] = wordify(om, gm->xsc[p7P_C][p7P_MOVE]);
901 om->xw[p7O_C][p7O_LOOP] = 0; /* was wordify(om, gm->xsc[p7P_C][p7P_LOOP]); */
902 om->xw[p7O_J][p7O_MOVE] = wordify(om, gm->xsc[p7P_J][p7P_MOVE]);
903 om->xw[p7O_J][p7O_LOOP] = 0; /* was wordify(om, gm->xsc[p7P_J][p7P_LOOP]); */
904
905 om->ncj_roundoff = 0.0; /* goes along with NN=CC=JJ=0, -3.0 nat approximation */
906 /* otherwise, would be = om->scale_w * gm->xsc[p7P_N][p7P_LOOP] - om->xw[p7O_N][p7O_LOOP]; */
907 /* see J4/150 for discussion of VF error suppression, superceded by the -3.0 nat approximation */
908
909 /* Transition score bound for "lazy F" DD path evaluation (xref J2/52) */
910 om->ddbound_w = -32768;
911 for (k = 2; k < M-1; k++)
912 {
913 ddtmp = (int) wordify(om, p7P_TSC(gm, k, p7P_DD));
914 ddtmp += (int) wordify(om, p7P_TSC(gm, k+1, p7P_DM));
915 ddtmp -= (int) wordify(om, p7P_TSC(gm, k+1, p7P_BM));
916 om->ddbound_w = ESL_MAX(om->ddbound_w, ddtmp);
917 }
918
919 return eslOK;
920 }
921
922
923 /* fb_conversion()
924 * This builds the Forward/Backward part of the optimized profile <om>,
925 * where we use odds ratios (not log-odds scores).
926 */
927 static int
928 fb_conversion(const P7_PROFILE *gm, P7_OPROFILE *om)
929 {
930 int M = gm->M; /* length of the query */
931 int nq = p7O_NQF(M); /* segment length; total # of striped vectors needed */
932 int x; /* counter over residues */
933 int q; /* q counts over total # of striped vectors, 0..nq-1 */
934 int k; /* the usual counter over model nodes 1..M */
935 int kb; /* possibly offset base k for loading om's TSC vectors */
936 int z; /* counter within elements of one SIMD minivector */
937 int t; /* counter over transitions 0..7 = p7O_{BM,MM,IM,DM,MD,MI,II,DD}*/
938 int tg; /* transition index in gm */
939 int j; /* counter in interleaved vector arrays in the profile */
940 union { float32x4_t v; float x[4]; } tmp; /* used to align and load simd minivectors */
941
942 if (nq > om->allocQ4) ESL_EXCEPTION(eslEINVAL, "optimized profile is too small to hold conversion");
943
944 /* striped match scores: start at k=1 */
945 for (x = 0; x < gm->abc->Kp; x++){
946 for (k = 1, q = 0; q < nq; q++, k++)
947 {
948 // printf("\n");
949 for (z = 0; z < 4; z++)
950 {
951 tmp.x[z] = (k + z * nq <= M) ? p7P_MSC(gm, k + z * nq, x) : -eslINFINITY;
952 }
953 // esl_neon_dump_float(stdout, (esl_neon_128f_t) tmp.v);
954 om->rfv[x][q] = esl_neon_expf((esl_neon_128f_t)tmp.v).f32x4;
955 // esl_neon_dump_float(stdout, (esl_neon_128f_t) om->rfv[x][q]);
956 // printf("\n");
957 }
958 }
959
960 /* Transition scores, all but the DD's. */
961 for (j = 0, k = 1, q = 0; q < nq; q++, k++)
962 {
963 for (t = p7O_BM; t <= p7O_II; t++) /* this loop of 7 transitions depends on the order in the definition of p7o_tsc_e */
964 {
965 switch (t) {
966 case p7O_BM: tg = p7P_BM; kb = k-1; break; /* gm has tBMk stored off by one! start from k=0 not 1 */
967 case p7O_MM: tg = p7P_MM; kb = k-1; break; /* MM, DM, IM quads are rotated by -1, start from k=0 */
968 case p7O_IM: tg = p7P_IM; kb = k-1; break;
969 case p7O_DM: tg = p7P_DM; kb = k-1; break;
970 case p7O_MD: tg = p7P_MD; kb = k; break; /* the remaining ones are straight up */
971 case p7O_MI: tg = p7P_MI; kb = k; break;
972 case p7O_II: tg = p7P_II; kb = k; break;
973 }
974
975 for (z = 0; z < 4; z++) tmp.x[z] = (kb+z*nq < M) ? p7P_TSC(gm, kb+z*nq, tg) : -eslINFINITY;
976 om->tfv[j++] = esl_neon_expf((esl_neon_128f_t) tmp.v).f32x4;
977 }
978 }
979
980 /* And finally the DD's, which are at the end of the optimized tfv vector; (j is already there) */
981 for (k = 1, q = 0; q < nq; q++, k++)
982 {
983 for (z = 0; z < 4; z++) tmp.x[z] = (k+z*nq < M) ? p7P_TSC(gm, k+z*nq, p7P_DD) : -eslINFINITY;
984 om->tfv[j++] = esl_neon_expf((esl_neon_128f_t) tmp.v).f32x4;
985 }
986
987 /* Specials. (These are actually in exactly the same order in om and
988 * gm, but we copy in general form anyway.)
989 */
990 om->xf[p7O_E][p7O_LOOP] = expf(gm->xsc[p7P_E][p7P_LOOP]);
991 om->xf[p7O_E][p7O_MOVE] = expf(gm->xsc[p7P_E][p7P_MOVE]);
992 om->xf[p7O_N][p7O_LOOP] = expf(gm->xsc[p7P_N][p7P_LOOP]);
993 om->xf[p7O_N][p7O_MOVE] = expf(gm->xsc[p7P_N][p7P_MOVE]);
994 om->xf[p7O_C][p7O_LOOP] = expf(gm->xsc[p7P_C][p7P_LOOP]);
995 om->xf[p7O_C][p7O_MOVE] = expf(gm->xsc[p7P_C][p7P_MOVE]);
996 om->xf[p7O_J][p7O_LOOP] = expf(gm->xsc[p7P_J][p7P_LOOP]);
997 om->xf[p7O_J][p7O_MOVE] = expf(gm->xsc[p7P_J][p7P_MOVE]);
998
999 return eslOK;
1000 }
1001
1002
1003 /* Function: p7_oprofile_Convert()
1004 * Synopsis: Converts standard profile to an optimized one.
1005 * Incept: SRE, Mon Nov 26 07:38:57 2007 [Janelia]
1006 *
1007 * Purpose: Convert a standard profile <gm> to an optimized profile <om>,
1008 * where <om> has already been allocated for a profile of at
1009 * least <gm->M> nodes and the same emission alphabet <gm->abc>.
1010 *
1011 * Args: gm - profile to optimize
1012 * om - allocated optimized profile for holding the result.
1013 *
1014 * Returns: <eslOK> on success.
1015 *
1016 * Throws: <eslEINVAL> if <gm>, <om> aren't compatible.
1017 * <eslEMEM> on allocation failure.
1018 */
1019 int
1020 p7_oprofile_Convert(const P7_PROFILE *gm, P7_OPROFILE *om)
1021 {
1022 int status, z;
1023
1024 /* Set these first so they are available in the following calls */
1025 om->mode = gm->mode;
1026 om->L = gm->L;
1027 om->M = gm->M;
1028 om->nj = gm->nj;
1029 om->max_length = gm->max_length;
1030
1031 if (gm->abc->type != om->abc->type) ESL_EXCEPTION(eslEINVAL, "alphabets of the two profiles don't match");
1032 if (gm->M > om->allocM) ESL_EXCEPTION(eslEINVAL, "oprofile is too small");
1033
1034 if ((status = mf_conversion(gm, om)) != eslOK) return status; /* MSVFilter()'s information */
1035 if ((status = vf_conversion(gm, om)) != eslOK) return status; /* ViterbiFilter()'s information */
1036 if ((status = fb_conversion(gm, om)) != eslOK) return status; /* ForwardFilter()'s information */
1037
1038 if (om->name != NULL) free(om->name);
1039 if (om->acc != NULL) free(om->acc);
1040 if (om->desc != NULL) free(om->desc);
1041 if ((status = esl_strdup(gm->name, -1, &(om->name))) != eslOK) goto ERROR;
1042 if ((status = esl_strdup(gm->acc, -1, &(om->acc))) != eslOK) goto ERROR;
1043 if ((status = esl_strdup(gm->desc, -1, &(om->desc))) != eslOK) goto ERROR;
1044 strcpy(om->rf, gm->rf);
1045 strcpy(om->mm, gm->mm);
1046 strcpy(om->cs, gm->cs);
1047 strcpy(om->consensus, gm->consensus);
1048 for (z = 0; z < p7_NEVPARAM; z++) om->evparam[z] = gm->evparam[z];
1049 for (z = 0; z < p7_NCUTOFFS; z++) om->cutoff[z] = gm->cutoff[z];
1050 for (z = 0; z < p7_MAXABET; z++) om->compo[z] = gm->compo[z];
1051
1052 return eslOK;
1053
1054 ERROR:
1055 return status;
1056 }
1057
1058 /* Function: p7_oprofile_ReconfigLength()
1059 * Synopsis: Set the target sequence length of a model.
1060 * Incept: SRE, Thu Dec 20 09:56:40 2007 [Janelia]
1061 *
1062 * Purpose: Given an already configured model <om>, quickly reset its
1063 * expected length distribution for a new mean target sequence
1064 * length of <L>.
1065 *
1066 * This doesn't affect the length distribution of the null
1067 * model. That must also be reset, using <p7_bg_SetLength()>.
1068 *
1069 * We want this routine to run as fast as possible, because
1070 * this call is in the critical path: it must be called at
1071 * each new target sequence in a database search.
1072 *
1073 * Returns: <eslOK> on success. Costs/scores for N,C,J transitions are set
1074 * here.
1075 */
1076 int
1077 p7_oprofile_ReconfigLength(P7_OPROFILE *om, int L)
1078 {
1079 int status;
1080 if ((status = p7_oprofile_ReconfigMSVLength (om, L)) != eslOK) return status;
1081 if ((status = p7_oprofile_ReconfigRestLength(om, L)) != eslOK) return status;
1082 return eslOK;
1083 }
1084
1085 /* Function: p7_oprofile_ReconfigMSVLength()
1086 * Synopsis: Set the target sequence length of the MSVFilter part of the model.
1087 * Incept: SRE, Tue Dec 16 13:39:17 2008 [Janelia]
1088 *
1089 * Purpose: Given an already configured model <om>, quickly reset its
1090 * expected length distribution for a new mean target sequence
1091 * length of <L>, only for the part of the model that's used
1092 * for the accelerated MSV filter.
1093 *
1094 * The acceleration pipeline uses this to defer reconfiguring the
1095 * length distribution of the main model, mostly because hmmscan
1096 * reads the model in two pieces, MSV part first, then the rest.
1097 *
1098 * Returns: <eslOK> on success.
1099 */
1100 int
1101 p7_oprofile_ReconfigMSVLength(P7_OPROFILE *om, int L)
1102 {
1103 om->tjb_b = unbiased_byteify(om, logf(3.0f / (float) (L+3)));
1104 return eslOK;
1105 }
1106
1107 /* Function: p7_oprofile_ReconfigRestLength()
1108 * Synopsis: Set the target sequence length of the main profile.
1109 * Incept: SRE, Tue Dec 16 13:41:30 2008 [Janelia]
1110 *
1111 * Purpose: Given an already configured model <om>, quickly reset its
1112 * expected length distribution for a new mean target sequence
1113 * length of <L>, for everything except the MSV filter part
1114 * of the model.
1115 *
1116 * Calling <p7_oprofile_ReconfigMSVLength()> then
1117 * <p7_oprofile_ReconfigRestLength()> is equivalent to
1118 * just calling <p7_oprofile_ReconfigLength()>. The two
1119 * part version is used in the acceleration pipeline.
1120 *
1121 * Returns: <eslOK> on success.
1122 */
1123 int
1124 p7_oprofile_ReconfigRestLength(P7_OPROFILE *om, int L)
1125 {
1126 float pmove, ploop;
1127
1128 pmove = (2.0f + om->nj) / ((float) L + 2.0f + om->nj); /* 2/(L+2) for sw; 3/(L+3) for fs */
1129 ploop = 1.0f - pmove;
1130
1131 /* ForwardFilter() parameters: pspace floats */
1132 om->xf[p7O_N][p7O_LOOP] = om->xf[p7O_C][p7O_LOOP] = om->xf[p7O_J][p7O_LOOP] = ploop;
1133 om->xf[p7O_N][p7O_MOVE] = om->xf[p7O_C][p7O_MOVE] = om->xf[p7O_J][p7O_MOVE] = pmove;
1134
1135 /* ViterbiFilter() parameters: lspace signed 16-bit ints */
1136 om->xw[p7O_N][p7O_MOVE] = om->xw[p7O_C][p7O_MOVE] = om->xw[p7O_J][p7O_MOVE] = wordify(om, logf(pmove));
1137 /* om->xw[p7O_N][p7O_LOOP] = om->xw[p7O_C][p7O_LOOP] = om->xw[p7O_J][p7O_LOOP] = wordify(om, logf(ploop)); */ /* 3nat approx in force: these stay 0 */
1138 /* om->ncj_roundoff = (om->scale_w * logf(ploop)) - om->xw[p7O_N][p7O_LOOP]; */ /* and this does too */
1139
1140 om->L = L;
1141 return eslOK;
1142 }
1143
1144
1145 /* Function: p7_oprofile_ReconfigMultihit()
1146 * Synopsis: Quickly reconfig model into multihit mode for target length <L>.
1147 * Incept: SRE, Thu Aug 21 10:04:07 2008 [Janelia]
1148 *
1149 * Purpose: Given a profile <om> that's already been configured once,
1150 * quickly reconfigure it into a multihit mode for target
1151 * length <L>.
1152 *
1153 * This gets called in domain definition, when we need to
1154 * flip the model in and out of unihit mode to
1155 * process individual domains.
1156 *
1157 * Note: You can't just flip uni/multi mode alone, because that
1158 * parameterization also affects target length
1159 * modeling. You need to make sure uni vs. multi choice is
1160 * made before the length model is set, and you need to
1161 * make sure the length model is recalculated if you change
1162 * the uni/multi mode. Hence, these functions call
1163 * <p7_oprofile_ReconfigLength()>.
1164 */
1165 int
1166 p7_oprofile_ReconfigMultihit(P7_OPROFILE *om, int L)
1167 {
1168 om->xf[p7O_E][p7O_MOVE] = 0.5;
1169 om->xf[p7O_E][p7O_LOOP] = 0.5;
1170 om->nj = 1.0f;
1171
1172 om->xw[p7O_E][p7O_MOVE] = wordify(om, -eslCONST_LOG2);
1173 om->xw[p7O_E][p7O_LOOP] = wordify(om, -eslCONST_LOG2);
1174
1175 return p7_oprofile_ReconfigLength(om, L);
1176 }
1177
1178 /* Function: p7_oprofile_ReconfigUnihit()
1179 * Synopsis: Quickly reconfig model into unihit mode for target length <L>.
1180 * Incept: SRE, Thu Aug 21 10:10:32 2008 [Janelia]
1181 *
1182 * Purpose: Given a profile <om> that's already been configured once,
1183 * quickly reconfigure it into a unihit mode for target
1184 * length <L>.
1185 *
1186 * This gets called in domain definition, when we need to
1187 * flip the model in and out of unihit <L=0> mode to
1188 * process individual domains.
1189 */
1190 int
1191 p7_oprofile_ReconfigUnihit(P7_OPROFILE *om, int L)
1192 {
1193 om->xf[p7O_E][p7O_MOVE] = 1.0f;
1194 om->xf[p7O_E][p7O_LOOP] = 0.0f;
1195 om->nj = 0.0f;
1196
1197 om->xw[p7O_E][p7O_MOVE] = 0;
1198 om->xw[p7O_E][p7O_LOOP] = -32768;
1199
1200 return p7_oprofile_ReconfigLength(om, L);
1201 }
1202 /*------------ end, conversions to P7_OPROFILE ------------------*/
1203
1204 /*******************************************************************
1205 * 3. Conversion from optimized P7_OPROFILE to compact score arrays
1206 *******************************************************************/
1207
1208 /* Function: p7_oprofile_GetFwdTransitionArray()
1209 * Synopsis: Retrieve full 32-bit float transition probabilities from an
1210 * optimized profile into a flat array
1211 *
1212 * Purpose: Extract an array of <type> (e.g. p7O_II) transition probabilities
1213 * from the underlying <om> profile. In SIMD implementations,
1214 * these are striped and interleaved, making them difficult to
1215 * directly access.
1216 *
1217 * Args: <om> - optimized profile, containing transition information
1218 * <type> - transition type (e.g. p7O_II)
1219 * <arr> - preallocated array into which floats will be placed
1220 *
1221 * Returns: <eslOK> on success.
1222 *
1223 * Throws: (no abnormal error conditions)
1224 */
1225 int
1226 p7_oprofile_GetFwdTransitionArray(const P7_OPROFILE *om, int type, float *arr )
1227 {
1228 int nq = p7O_NQF(om->M); /* # of striped vectors needed */
1229 int i, j;
1230 union { float32x4_t v; float x[4]; } tmp; /* used to align and read simd minivectors */
1231
1232
1233 for (i=0; i<nq; i++) {
1234 // because DD transitions are held at the end of the tfv array
1235 tmp.v = om->tfv[ (type==p7O_DD ? nq*7+i : type+7*i) ];
1236 for (j=0; j<4; j++)
1237 if ( i+1+ j*nq < om->M+1)
1238 arr[i+1+ j*nq] = tmp.x[j];
1239 }
1240
1241 return eslOK;
1242
1243 }
1244
1245 /* Function: p7_oprofile_GetSSVEmissionScoreArray()
1246 * Synopsis: Retrieve MSV residue emission scores from an optimized
1247 * profile into an array
1248 *
1249 * Purpose: Extract an implicitly 2D array of 8-bit int SSV residue
1250 * emission scores from an optimized profile <om>. <arr> must
1251 * be allocated by the calling function to be of size
1252 * ( om->abc->Kp * ( om->M + 1 )), and indexing into the array
1253 * is done as [om->abc->Kp * i + c ] for character c at
1254 * position i.
1255 *
1256 * In SIMD implementations, the residue scores are striped
1257 * and interleaved, making them somewhat difficult to
1258 * directly access. Faster access is desired, for example,
1259 * in SSV back-tracking of a high-scoring diagonal
1260 *
1261 * Args: <om> - optimized profile, containing transition information
1262 * <arr> - preallocated array into which scores will be placed
1263 *
1264 * Returns: <eslOK> on success.
1265 *
1266 * Throws: (no abnormal error conditions)
1267 */
1268 int
1269 p7_oprofile_GetSSVEmissionScoreArray(const P7_OPROFILE *om, uint8_t *arr )
1270 {
1271 int x, q, z, k;
1272 union { uint8x16_t v; uint8_t i[16]; } tmp; /* used to align and read simd minivectors */
1273 int M = om->M; /* length of the query */
1274 int K = om->abc->Kp;
1275 int nq = p7O_NQB(M); /* segment length; total # of striped vectors needed */
1276 int cell_cnt = (om->M + 1) * K;
1277
1278 for (x = 0; x < K ; x++) {
1279 for (q = 0, k = 1; q < nq; q++, k++) {
1280 tmp.v = om->rbv[x][q];
1281 for (z=0; z<16; z++)
1282 if ( (K * (k+z*nq) + x) < cell_cnt)
1283 arr[ K * (k+z*nq) + x ] = tmp.i[z];
1284 }
1285 }
1286
1287 return eslOK;
1288 }
1289
1290
1291 /* Function: p7_oprofile_GetFwdEmissionScoreArray()
1292 * Synopsis: Retrieve Fwd (float) residue emission scores from an optimized
1293 * profile into an array
1294 *
1295 * Purpose: Extract an implicitly 2D array of 32-bit float Fwd residue
1296 * emission scores from an optimized profile <om>. <arr> must
1297 * be allocated by the calling function to be of size
1298 * ( om->abc->Kp * ( om->M + 1 )), and indexing into the array
1299 * is done as [om->abc->Kp * i + c ] for character c at
1300 * position i.
1301 *
1302 * In SIMD implementations, the residue scores are striped
1303 * and interleaved, making them somewhat difficult to
1304 * directly access.
1305 *
1306 * Args: <om> - optimized profile, containing transition information
1307 * <arr> - preallocated array into which scores will be placed
1308 *
1309 * Returns: <eslOK> on success.
1310 *
1311 * Throws: (no abnormal error conditions)
1312 */
1313 int
1314 p7_oprofile_GetFwdEmissionScoreArray(const P7_OPROFILE *om, float *arr )
1315 {
1316 int x, q, z, k;
1317 union { float32x4_t v; float f[4]; } tmp; /* used to align and read simd minivectors */
1318 int M = om->M; /* length of the query */
1319 int K = om->abc->Kp;
1320 int nq = p7O_NQF(M); /* segment length; total # of striped vectors needed */
1321 int cell_cnt = (om->M + 1) * K;
1322
1323 for (x = 0; x < K; x++) {
1324 for (q = 0, k = 1; q < nq; q++, k++) {
1325 tmp.v = esl_neon_logf((esl_neon_128f_t) om->rfv[x][q]).f32x4;
1326 for (z = 0; z < 4; z++)
1327 if ( (K * (k+z*nq) + x) < cell_cnt)
1328 arr[ K * (k+z*nq) + x ] = tmp.f[z];
1329 }
1330 }
1331
1332 return eslOK;
1333 }
1334
1335 /* Function: p7_oprofile_GetFwdEmissionArray()
1336 * Synopsis: Retrieve Fwd (float) residue emission values from an optimized
1337 * profile into an array
1338 *
1339 * Purpose: Extract an implicitly 2D array of 32-bit float Fwd residue
1340 * emission values from an optimized profile <om>, converting
1341 * back to emission values based on the background. <arr> must
1342 * be allocated by the calling function to be of size
1343 * ( om->abc->Kp * ( om->M + 1 )), and indexing into the array
1344 * is done as [om->abc->Kp * i + c ] for character c at
1345 * position i.
1346 *
1347 * In SIMD implementations, the residue scores are striped
1348 * and interleaved, making them somewhat difficult to
1349 * directly access.
1350 *
1351 * Args: <om> - optimized profile, containing transition information
1352 * <bg> - background frequencies
1353 * <arr> - preallocated array into which scores will be placed
1354 *
1355 * Returns: <eslOK> on success.
1356 *
1357 * Throws: (no abnormal error conditions)
1358 */
1359 int
1360 p7_oprofile_GetFwdEmissionArray(const P7_OPROFILE *om, P7_BG *bg, float *arr )
1361 {
1362 int x, q, z, k;
1363 union { float32x4_t v; float f[4]; } tmp; /* used to align and read simd minivectors */
1364 int M = om->M; /* length of the query */
1365 int Kp = om->abc->Kp;
1366 int K = om->abc->K;
1367 int nq = p7O_NQF(M); /* segment length; total # of striped vectors needed */
1368 int cell_cnt = (om->M + 1) * Kp;
1369
1370 for (x = 0; x < K; x++) {
1371 for (q = 0, k = 1; q < nq; q++, k++) {
1372 tmp.v = om->rfv[x][q];
1373 for (z = 0; z < 4; z++)
1374 if ( (Kp * (k+z*nq) + x) < cell_cnt)
1375 arr[ Kp * (k+z*nq) + x ] = tmp.f[z] * bg->f[x];
1376 }
1377 }
1378
1379 //degeneracy emissions for each position
1380 for (x = 0; x <= M; x++)
1381 esl_abc_FExpectScVec(om->abc, arr+Kp*x, bg->f);
1382
1383 return eslOK;
1384 }
1385
1386
1387 /*------------ end, conversions from P7_OPROFILE ------------------*/
1388
1389
1390 /*****************************************************************
1391 * 4. Debugging and development utilities.
1392 *****************************************************************/
1393
1394
1395 /* oprofile_dump_mf()
1396 *
1397 * Dump the MSVFilter part of a profile <om> to <stdout>.
1398 */
1399 static int
1400 oprofile_dump_mf(FILE *fp, const P7_OPROFILE *om)
1401 {
1402 int M = om->M; /* length of the query */
1403 int nq = p7O_NQB(M); /* segment length; total # of striped vectors needed */
1404 int x; /* counter over residues */
1405 int q; /* q counts over total # of striped vectors, 0..nq-1 */
1406 int k; /* counter over nodes 1..M */
1407 int z; /* counter within elements of one SIMD minivector */
1408 union { uint8x16_t v; uint8_t i[16]; } tmp; /* used to align and read simd minivectors */
1409
1410 /* Header (rearranged column numbers, in the vectors) */
1411 fprintf(fp, " ");
1412 for (k =1, q = 0; q < nq; q++, k++)
1413 {
1414 fprintf(fp, "[ ");
1415 for (z = 0; z < 16; z++)
1416 if (k+z*nq <= M) fprintf(fp, "%4d ", k+z*nq);
1417 else fprintf(fp, "%4s ", "xx");
1418 fprintf(fp, "]");
1419 }
1420 fprintf(fp, "\n");
1421
1422 /* Table of residue emissions */
1423 for (x = 0; x < om->abc->Kp; x++)
1424 {
1425 fprintf(fp, "(%c): ", om->abc->sym[x]);
1426
1427 for (q = 0; q < nq; q++)
1428 {
1429 fprintf(fp, "[ ");
1430 // _mm_store_si128(&tmp.v, om->rbv[x][q]);
1431 tmp.v = om->rbv[x][q];
1432 for (z = 0; z < 16; z++) fprintf(fp, "%4d ", tmp.i[z]);
1433 fprintf(fp, "]");
1434 }
1435 fprintf(fp, "\n");
1436 }
1437 fprintf(fp, "\n");
1438
1439 fprintf(fp, "t_EC,EJ: %4d\n", om->tec_b);
1440 fprintf(fp, "t_NB,JB,CT: %4d\n", om->tjb_b);
1441 fprintf(fp, "t_BMk: %4d\n", om->tbm_b);
1442 fprintf(fp, "scale: %.2f\n", om->scale_b);
1443 fprintf(fp, "base: %4d\n", om->base_b);
1444 fprintf(fp, "bias: %4d\n", om->bias_b);
1445 fprintf(fp, "Q: %4d\n", nq);
1446 fprintf(fp, "M: %4d\n", M);
1447 return eslOK;
1448 }
1449
1450
1451
1452 /* oprofile_dump_vf()
1453 *
1454 * Dump the ViterbiFilter part of a profile <om> to <stdout>.
1455 */
1456 static int
1457 oprofile_dump_vf(FILE *fp, const P7_OPROFILE *om)
1458 {
1459 int M = om->M; /* length of the query */
1460 int nq = p7O_NQW(M); /* segment length; total # of striped vectors needed */
1461 int x; /* counter over residues */
1462 int q; /* q counts over total # of striped vectors, 0..nq-1 */
1463 int k; /* the usual counter over model nodes 1..M */
1464 int kb; /* possibly offset base k for loading om's TSC vectors */
1465 int z; /* counter within elements of one SIMD minivector */
1466 int t; /* counter over transitions 0..7 = p7O_{BM,MM,IM,DM,MD,MI,II,DD}*/
1467 int j; /* counter in interleaved vector arrays in the profile */
1468 union { int16x8_t v; int16_t i[8]; } tmp; /* used to align and read simd minivectors */
1469
1470 /* Emission score header (rearranged column numbers, in the vectors) */
1471 fprintf(fp, " ");
1472 for (k =1, q = 0; q < nq; q++, k++)
1473 {
1474 fprintf(fp, "[ ");
1475 for (z = 0; z < 8; z++)
1476 if (k+z*nq <= M) fprintf(fp, "%6d ", k+z*nq);
1477 else fprintf(fp, "%6s ", "xx");
1478 fprintf(fp, "]");
1479 }
1480 fprintf(fp, "\n");
1481
1482 /* Table of residue emissions */
1483 for (x = 0; x < om->abc->Kp; x++)
1484 {
1485 fprintf(fp, "(%c): ", om->abc->sym[x]);
1486
1487 /* Match emission scores (insert emissions are assumed zero by design) */
1488 for (q = 0; q < nq; q++)
1489 {
1490 fprintf(fp, "[ ");
1491 tmp.v = om->rwv[x][q];
1492 for (z = 0; z < 8; z++) fprintf(fp, "%6d ", tmp.i[z]);
1493 fprintf(fp, "]");
1494 }
1495 fprintf(fp, "\n");
1496 }
1497 fprintf(fp, "\n");
1498
1499 /* Transitions */
1500 for (t = p7O_BM; t <= p7O_II; t++)
1501 {
1502 switch (t) {
1503 case p7O_BM: fprintf(fp, "\ntBM: "); break;
1504 case p7O_MM: fprintf(fp, "\ntMM: "); break;
1505 case p7O_IM: fprintf(fp, "\ntIM: "); break;
1506 case p7O_DM: fprintf(fp, "\ntDM: "); break;
1507 case p7O_MD: fprintf(fp, "\ntMD: "); break;
1508 case p7O_MI: fprintf(fp, "\ntMI: "); break;
1509 case p7O_II: fprintf(fp, "\ntII: "); break;
1510 }
1511
1512 for (k = 1, q = 0; q < nq; q++, k++)
1513 {
1514 switch (t) {
1515 case p7O_BM: kb = k; break;
1516 case p7O_MM: kb = (1 + (nq+k-2)) % nq; break; /* MM, DM, IM quads rotated by +1 */
1517 case p7O_IM: kb = (1 + (nq+k-2)) % nq; break;
1518 case p7O_DM: kb = (1 + (nq+k-2)) % nq; break;
1519 case p7O_MD: kb = k; break; /* the remaining ones are straight up */
1520 case p7O_MI: kb = k; break;
1521 case p7O_II: kb = k; break;
1522 }
1523 fprintf(fp, "[ ");
1524 for (z = 0; z < 8; z++)
1525 if (kb+z*nq <= M) fprintf(fp, "%6d ", kb+z*nq);
1526 else fprintf(fp, "%6s ", "xx");
1527 fprintf(fp, "]");
1528 }
1529 fprintf(fp, "\n ");
1530 for (q = 0; q < nq; q++)
1531 {
1532 fprintf(fp, "[ ");
1533 tmp.v = om->twv[q*7 + t];
1534 for (z = 0; z < 8; z++) fprintf(fp, "%6d ", tmp.i[z]);
1535 fprintf(fp, "]");
1536 }
1537 fprintf(fp, "\n");
1538 }
1539
1540 /* DD transitions */
1541 fprintf(fp, "\ntDD: ");
1542 for (k =1, q = 0; q < nq; q++, k++)
1543 {
1544 fprintf(fp, "[ ");
1545 for (z = 0; z < 8; z++)
1546 if (k+z*nq <= M) fprintf(fp, "%6d ", k+z*nq);
1547 else fprintf(fp, "%6s ", "xx");
1548 fprintf(fp, "]");
1549 }
1550 fprintf(fp, "\n ");
1551 for (j = nq*7, q = 0; q < nq; q++, j++)
1552 {
1553 fprintf(fp, "[ ");
1554 tmp.v = om->twv[j];
1555 for (z = 0; z < 8; z++) fprintf(fp, "%6d ", tmp.i[z]);
1556 fprintf(fp, "]");
1557 }
1558 fprintf(fp, "\n");
1559
1560 fprintf(fp, "E->C: %6d E->J: %6d\n", om->xw[p7O_E][p7O_MOVE], om->xw[p7O_E][p7O_LOOP]);
1561 fprintf(fp, "N->B: %6d N->N: %6d\n", om->xw[p7O_N][p7O_MOVE], om->xw[p7O_N][p7O_LOOP]);
1562 fprintf(fp, "J->B: %6d J->J: %6d\n", om->xw[p7O_J][p7O_MOVE], om->xw[p7O_J][p7O_LOOP]);
1563 fprintf(fp, "C->T: %6d C->C: %6d\n", om->xw[p7O_C][p7O_MOVE], om->xw[p7O_C][p7O_LOOP]);
1564
1565 fprintf(fp, "scale: %6.2f\n", om->scale_w);
1566 fprintf(fp, "base: %6d\n", om->base_w);
1567 fprintf(fp, "bound: %6d\n", om->ddbound_w);
1568 fprintf(fp, "Q: %6d\n", nq);
1569 fprintf(fp, "M: %6d\n", M);
1570 return eslOK;
1571 }
1572
1573
1574 /* oprofile_dump_fb()
1575 *
1576 * Dump the Forward/Backward part of a profile <om> to <stdout>.
1577 * <width>, <precision> control the floating point output:
1578 * 8,5 is a reasonable choice for prob space,
1579 * 5,2 is reasonable for log space.
1580 */
1581 static int
1582 oprofile_dump_fb(FILE *fp, const P7_OPROFILE *om, int width, int precision)
1583 {
1584 int M = om->M; /* length of the query */
1585 int nq = p7O_NQF(M); /* segment length; total # of striped vectors needed */
1586 int x; /* counter over residues */
1587 int q; /* q counts over total # of striped vectors, 0..nq-1 */
1588 int k; /* the usual counter over model nodes 1..M */
1589 int kb; /* possibly offset base k for loading om's TSC vectors */
1590 int z; /* counter within elements of one SIMD minivector */
1591 int t; /* counter over transitions 0..7 = p7O_{BM,MM,IM,DM,MD,MI,II,DD}*/
1592 int j; /* counter in interleaved vector arrays in the profile */
1593 union { float32x4_t v; float x[4]; } tmp; /* used to align and read simd minivectors */
1594
1595 /* Residue emissions */
1596 for (x = 0; x < om->abc->Kp; x++)
1597 {
1598 fprintf(fp, "(%c): ", om->abc->sym[x]);
1599 for (k =1, q = 0; q < nq; q++, k++)
1600 {
1601 fprintf(fp, "[ ");
1602 for (z = 0; z < 4; z++)
1603 if (k+z*nq <= M) fprintf(fp, "%*d ", width, k+z*nq);
1604 else fprintf(fp, "%*s ", width, "xx");
1605 fprintf(fp, "]");
1606 }
1607 fprintf(fp, "\nmat: ");
1608 for (q = 0; q < nq; q++)
1609 {
1610 fprintf(fp, "[ ");
1611 tmp.v = om->rfv[x][q];
1612 for (z = 0; z < 4; z++) fprintf(fp, "%*.*f ", width, precision, tmp.x[z]);
1613 fprintf(fp, "]");
1614 }
1615 fprintf(fp, "\n\n");
1616 }
1617
1618 /* Transitions */
1619 for (t = p7O_BM; t <= p7O_II; t++)
1620 {
1621 switch (t) {
1622 case p7O_BM: fprintf(fp, "\ntBM: "); break;
1623 case p7O_MM: fprintf(fp, "\ntMM: "); break;
1624 case p7O_IM: fprintf(fp, "\ntIM: "); break;
1625 case p7O_DM: fprintf(fp, "\ntDM: "); break;
1626 case p7O_MD: fprintf(fp, "\ntMD: "); break;
1627 case p7O_MI: fprintf(fp, "\ntMI: "); break;
1628 case p7O_II: fprintf(fp, "\ntII: "); break;
1629 }
1630 for (k = 1, q = 0; q < nq; q++, k++)
1631 {
1632 switch (t) {
1633 case p7O_MM:/* MM, DM, IM quads rotated by +1 */
1634 case p7O_IM:
1635 case p7O_DM:
1636 kb = (1 + (nq+k-2)) % nq;
1637 break;
1638 case p7O_BM:/* the remaining ones are straight up */
1639 case p7O_MD:
1640 case p7O_MI:
1641 case p7O_II:
1642 kb = k;
1643 break;
1644 }
1645 fprintf(fp, "[ ");
1646 for (z = 0; z < 4; z++)
1647 if (kb+z*nq <= M) fprintf(fp, "%*d ", width, kb+z*nq);
1648 else fprintf(fp, "%*s ", width, "xx");
1649 fprintf(fp, "]");
1650 }
1651 fprintf(fp, "\n ");
1652 for (q = 0; q < nq; q++)
1653 {
1654 fprintf(fp, "[ ");
1655 tmp.v = om->tfv[q*7 + t];
1656 for (z = 0; z < 4; z++) fprintf(fp, "%*.*f ", width, precision, tmp.x[z]);
1657 fprintf(fp, "]");
1658 }
1659 fprintf(fp, "\n");
1660 }
1661
1662 /* DD transitions */
1663 fprintf(fp, "\ntDD: ");
1664 for (k =1, q = 0; q < nq; q++, k++)
1665 {
1666 fprintf(fp, "[ ");
1667 for (z = 0; z < 4; z++)
1668 if (k+z*nq <= M) fprintf(fp, "%*d ", width, k+z*nq);
1669 else fprintf(fp, "%*s ", width, "xx");
1670 fprintf(fp, "]");
1671 }
1672 fprintf(fp, "\n ");
1673 for (j = nq*7, q = 0; q < nq; q++, j++)
1674 {
1675 fprintf(fp, "[ ");
1676 tmp.v = om->tfv[j];
1677 for (z = 0; z < 4; z++) fprintf(fp, "%*.*f ", width, precision, tmp.x[z]);
1678 fprintf(fp, "]");
1679 }
1680 fprintf(fp, "\n");
1681
1682 /* Specials */
1683 fprintf(fp, "E->C: %*.*f E->J: %*.*f\n", width, precision, om->xf[p7O_E][p7O_MOVE], width, precision, om->xf[p7O_E][p7O_LOOP]);
1684 fprintf(fp, "N->B: %*.*f N->N: %*.*f\n", width, precision, om->xf[p7O_N][p7O_MOVE], width, precision, om->xf[p7O_N][p7O_LOOP]);
1685 fprintf(fp, "J->B: %*.*f J->J: %*.*f\n", width, precision, om->xf[p7O_J][p7O_MOVE], width, precision, om->xf[p7O_J][p7O_LOOP]);
1686 fprintf(fp, "C->T: %*.*f C->C: %*.*f\n", width, precision, om->xf[p7O_C][p7O_MOVE], width, precision, om->xf[p7O_C][p7O_LOOP]);
1687 fprintf(fp, "Q: %d\n", nq);
1688 fprintf(fp, "M: %d\n", M);
1689 return eslOK;
1690 }
1691
1692
1693 /* Function: p7_oprofile_Dump()
1694 * Synopsis: Dump internals of a <P7_OPROFILE>
1695 * Incept: SRE, Thu Dec 13 08:49:30 2007 [Janelia]
1696 *
1697 * Purpose: Dump the internals of <P7_OPROFILE> structure <om>
1698 * to stream <fp>; generally for testing or debugging
1699 * purposes.
1700 *
1701 * Args: fp - output stream (often stdout)
1702 * om - optimized profile to dump
1703 *
1704 * Returns: <eslOK> on success.
1705 *
1706 * Throws: (no abnormal error conditions)
1707 */
1708 int
1709 p7_oprofile_Dump(FILE *fp, const P7_OPROFILE *om)
1710 {
1711 int status;
1712
1713 fprintf(fp, "Dump of a <P7_OPROFILE> ::\n");
1714
1715 fprintf(fp, "\n -- float part, odds ratios for Forward/Backward:\n");
1716 if ((status = oprofile_dump_fb(fp, om, 8, 8)) != eslOK) return status;
1717
1718 fprintf(fp, "\n -- sword part, log odds for ViterbiFilter(): \n");
1719 if ((status = oprofile_dump_vf(fp, om)) != eslOK) return status;
1720
1721 fprintf(fp, "\n -- uchar part, log odds for MSVFilter(): \n");
1722 if ((status = oprofile_dump_mf(fp, om)) != eslOK) return status;
1723
1724 return eslOK;
1725 }
1726
1727
1728 /* Function: p7_oprofile_Sample()
1729 * Synopsis: Sample a random profile.
1730 * Incept: SRE, Wed Jul 30 13:11:52 2008 [Janelia]
1731 *
1732 * Purpose: Sample a random profile of <M> nodes for alphabet <abc>,
1733 * using <r> as the source of random numbers. Parameterize
1734 * it for generation of target sequences of mean length
1735 * <L>. Calculate its log-odds scores using background
1736 * model <bg>.
1737 *
1738 * Args: r - random number generator
1739 * abc - emission alphabet
1740 * bg - background frequency model
1741 * M - size of sampled profile, in nodes
1742 * L - configured target seq mean length
1743 * opt_hmm - optRETURN: sampled HMM
1744 * opt_gm - optRETURN: sampled normal profile
1745 * opt_om - RETURN: optimized profile
1746 *
1747 * Returns: <eslOK> on success.
1748 *
1749 * Throws: (no abnormal error conditions)
1750 */
1751 int
1752 p7_oprofile_Sample(ESL_RANDOMNESS *r, const ESL_ALPHABET *abc, const P7_BG *bg, int M, int L,
1753 P7_HMM **opt_hmm, P7_PROFILE **opt_gm, P7_OPROFILE **ret_om)
1754 {
1755 P7_HMM *hmm = NULL;
1756 P7_PROFILE *gm = NULL;
1757 P7_OPROFILE *om = NULL;
1758 int status;
1759
1760 if ((gm = p7_profile_Create (M, abc)) == NULL) { status = eslEMEM; goto ERROR; }
1761 if ((om = p7_oprofile_Create(M, abc)) == NULL) { status = eslEMEM; goto ERROR; }
1762
1763 if ((status = p7_hmm_Sample(r, M, abc, &hmm)) != eslOK) goto ERROR;
1764 if ((status = p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL)) != eslOK) goto ERROR;
1765 if ((status = p7_oprofile_Convert(gm, om)) != eslOK) goto ERROR;
1766 if ((status = p7_oprofile_ReconfigLength(om, L)) != eslOK) goto ERROR;
1767
1768 if (opt_hmm != NULL) *opt_hmm = hmm; else p7_hmm_Destroy(hmm);
1769 if (opt_gm != NULL) *opt_gm = gm; else p7_profile_Destroy(gm);
1770 *ret_om = om;
1771 return eslOK;
1772
1773 ERROR:
1774 if (opt_hmm != NULL) *opt_hmm = NULL;
1775 if (opt_gm != NULL) *opt_gm = NULL;
1776 *ret_om = NULL;
1777 return status;
1778 }
1779
1780
1781 /* Function: p7_oprofile_Compare()
1782 * Synopsis: Compare two optimized profiles for equality.
1783 * Incept: SRE, Wed Jan 21 13:29:10 2009 [Janelia]
1784 *
1785 * Purpose: Compare the contents of <om1> and <om2>; return
1786 * <eslOK> if they are effectively identical profiles,
1787 * or <eslFAIL> if not.
1788 *
1789 * Floating point comparisons are done to a tolerance
1790 * of <tol> using <esl_FCompare_old()>.
1791 *
1792 * If a comparison fails, an informative error message is
1793 * left in <errmsg> to indicate why.
1794 *
1795 * Internal allocation sizes are not compared, only the
1796 * data.
1797 *
1798 * Args: om1 - one optimized profile to compare
1799 * om2 - the other
1800 * tol - floating point comparison tolerance; see <esl_FCompare_old()>
1801 * errmsg - ptr to array of at least <eslERRBUFSIZE> characters.
1802 *
1803 * Returns: <eslOK> on effective equality; <eslFAIL> on difference.
1804 */
1805 int
1806 p7_oprofile_Compare(const P7_OPROFILE *om1, const P7_OPROFILE *om2, float tol, char *errmsg)
1807 {
1808 int Q4 = p7O_NQF(om1->M);
1809 int Q8 = p7O_NQW(om1->M);
1810 int Q16 = p7O_NQB(om1->M);
1811 int q, r, x, y;
1812 union { uint8x16_t v; uint8_t c[16]; } a16, b16;
1813 union { int16x8_t v; int16_t w[8]; } a8, b8;
1814 union { float32x4_t v; float x[4]; } a4, b4;
1815
1816 if (om1->mode != om2->mode) ESL_FAIL(eslFAIL, errmsg, "comparison failed: mode");
1817 if (om1->L != om2->L) ESL_FAIL(eslFAIL, errmsg, "comparison failed: L");
1818 if (om1->M != om2->M) ESL_FAIL(eslFAIL, errmsg, "comparison failed: M");
1819 if (om1->nj != om2->nj) ESL_FAIL(eslFAIL, errmsg, "comparison failed: nj");
1820 if (om1->abc->type != om2->abc->type) ESL_FAIL(eslFAIL, errmsg, "comparison failed: alphabet type");
1821
1822 /* MSVFilter part */
1823 for (x = 0; x < om1->abc->Kp; x++)
1824 for (q = 0; q < Q16; q++)
1825 {
1826 a16.v = om1->rbv[x][q]; b16.v = om2->rbv[x][q];
1827 for (r = 0; r < 16; r++) if (a16.c[r] != b16.c[r]) ESL_FAIL(eslFAIL, errmsg, "comparison failed: rb[%d] elem %d", q, r);
1828 }
1829 if (om1->tbm_b != om2->tbm_b) ESL_FAIL(eslFAIL, errmsg, "comparison failed: tbm_b");
1830 if (om1->tec_b != om2->tec_b) ESL_FAIL(eslFAIL, errmsg, "comparison failed: tec_b");
1831 if (om1->tjb_b != om2->tjb_b) ESL_FAIL(eslFAIL, errmsg, "comparison failed: tjb_b");
1832 if (om1->scale_b != om2->scale_b) ESL_FAIL(eslFAIL, errmsg, "comparison failed: scale_b");
1833 if (om1->base_b != om2->base_b) ESL_FAIL(eslFAIL, errmsg, "comparison failed: base_b");
1834 if (om1->bias_b != om2->bias_b) ESL_FAIL(eslFAIL, errmsg, "comparison failed: bias_b");
1835
1836 /* ViterbiFilter() part */
1837 for (x = 0; x < om1->abc->Kp; x++)
1838 for (q = 0; q < Q8; q++)
1839 {
1840 a8.v = om1->rwv[x][q]; b8.v = om2->rwv[x][q];
1841 for (r = 0; r < 8; r++) if (a8.w[r] != b8.w[r]) ESL_FAIL(eslFAIL, errmsg, "comparison failed: rw[%d] elem %d", q, r);
1842 }
1843 for (q = 0; q < 8*Q16; q++)
1844 {
1845 a8.v = om1->twv[q]; b8.v = om2->twv[q];
1846 for (r = 0; r < 8; r++) if (a8.w[r] != b8.w[r]) ESL_FAIL(eslFAIL, errmsg, "comparison failed: tw[%d] elem %d", q, r);
1847 }
1848 for (x = 0; x < p7O_NXSTATES; x++)
1849 for (y = 0; y < p7O_NXTRANS; y++)
1850 if (om1->xw[x][y] != om2->xw[x][y]) ESL_FAIL(eslFAIL, errmsg, "comparison failed: xw[%d][%d]", x, y);
1851
1852 if (om1->scale_w != om2->scale_w) ESL_FAIL(eslFAIL, errmsg, "comparison failed: scale");
1853 if (om1->base_w != om2->base_w) ESL_FAIL(eslFAIL, errmsg, "comparison failed: base");
1854 if (om1->ddbound_w != om2->ddbound_w) ESL_FAIL(eslFAIL, errmsg, "comparison failed: ddbound_w");
1855
1856 /* Forward/Backward part */
1857 for (x = 0; x < om1->abc->Kp; x++)
1858 for (q = 0; q < Q4; q++)
1859 {
1860 a4.v = om1->rfv[x][q]; b4.v = om2->rfv[x][q];
1861 for (r = 0; r < 4; r++) if (esl_FCompare_old(a4.x[r], b4.x[r], tol) != eslOK) ESL_FAIL(eslFAIL, errmsg, "comparison failed: rf[%d] elem %d", q, r);
1862 }
1863 for (q = 0; q < 8*Q4; q++)
1864 {
1865 a4.v = om1->tfv[q]; b4.v = om2->tfv[q];
1866 for (r = 0; r < 4; r++) if (a4.x[r] != b4.x[r]) ESL_FAIL(eslFAIL, errmsg, "comparison failed: tf[%d] elem %d", q, r);
1867 }
1868 for (x = 0; x < p7O_NXSTATES; x++)
1869 if (esl_vec_FCompare(om1->xf[x], om2->xf[x], p7O_NXTRANS, tol) != eslOK) ESL_FAIL(eslFAIL, errmsg, "comparison failed: xf[%d] vector", x);
1870
1871 for (x = 0; x < p7_NOFFSETS; x++)
1872 if (om1->offs[x] != om2->offs[x]) ESL_FAIL(eslFAIL, errmsg, "comparison failed: offs[%d]", x);
1873
1874 if (esl_strcmp(om1->name, om2->name) != 0) ESL_FAIL(eslFAIL, errmsg, "comparison failed: name");
1875 if (esl_strcmp(om1->acc, om2->acc) != 0) ESL_FAIL(eslFAIL, errmsg, "comparison failed: acc");
1876 if (esl_strcmp(om1->desc, om2->desc) != 0) ESL_FAIL(eslFAIL, errmsg, "comparison failed: desc");
1877 if (esl_strcmp(om1->rf, om2->rf) != 0) ESL_FAIL(eslFAIL, errmsg, "comparison failed: ref");
1878 if (esl_strcmp(om1->mm, om2->mm) != 0) ESL_FAIL(eslFAIL, errmsg, "comparison failed: mm");
1879 if (esl_strcmp(om1->cs, om2->cs) != 0) ESL_FAIL(eslFAIL, errmsg, "comparison failed: cs");
1880 if (esl_strcmp(om1->consensus, om2->consensus) != 0) ESL_FAIL(eslFAIL, errmsg, "comparison failed: consensus");
1881
1882 if (esl_vec_FCompare(om1->evparam, om2->evparam, p7_NEVPARAM, tol) != eslOK) ESL_FAIL(eslFAIL, errmsg, "comparison failed: evparam vector");
1883 if (esl_vec_FCompare(om1->cutoff, om2->cutoff, p7_NCUTOFFS, tol) != eslOK) ESL_FAIL(eslFAIL, errmsg, "comparison failed: cutoff vector");
1884 if (esl_vec_FCompare(om1->compo, om2->compo, p7_MAXABET, tol) != eslOK) ESL_FAIL(eslFAIL, errmsg, "comparison failed: compo vector");
1885
1886 return eslOK;
1887 }
1888
1889
1890 /* Function: p7_profile_SameAsMF()
1891 * Synopsis: Set a generic profile's scores to give MSV scores.
1892 * Incept: SRE, Wed Jul 30 13:42:49 2008 [Janelia]
1893 *
1894 * Purpose: Set a generic profile's scores so that the normal <dp_generic> DP
1895 * algorithms will give the same score as <p7_MSVFilter()>:
1896 * all t_MM scores = 0; all other core transitions = -inf;
1897 * multihit local mode; all <t_BMk> entries uniformly <log 2/(M(M+1))>;
1898 * <tCC, tNN, tJJ> scores 0; total approximated later as -3;
1899 * rounded in the same way as the 8-bit limited precision.
1900 *
1901 * Returns: <eslOK> on success.
1902 */
1903 int
1904 p7_profile_SameAsMF(const P7_OPROFILE *om, P7_PROFILE *gm)
1905 {
1906 int k,x;
1907 float tbm = roundf(om->scale_b * (log(2.0f / ((float) gm->M * (float) (gm->M+1)))));
1908
1909 /* Transitions */
1910 esl_vec_FSet(gm->tsc, p7P_NTRANS * gm->M, -eslINFINITY);
1911 for (k = 1; k < gm->M; k++) p7P_TSC(gm, k, p7P_MM) = 0.0f;
1912 for (k = 0; k < gm->M; k++) p7P_TSC(gm, k, p7P_BM) = tbm;
1913
1914 /* Emissions */
1915 for (x = 0; x < gm->abc->Kp; x++)
1916 for (k = 0; k <= gm->M; k++)
1917 {
1918 gm->rsc[x][k*2] = (gm->rsc[x][k*2] <= -eslINFINITY) ? -eslINFINITY : roundf(om->scale_b * gm->rsc[x][k*2]);
1919 gm->rsc[x][k*2+1] = 0; /* insert score: VF makes it zero no matter what. */
1920 }
1921
1922 /* Specials */
1923 for (k = 0; k < p7P_NXSTATES; k++)
1924 for (x = 0; x < p7P_NXTRANS; x++)
1925 gm->xsc[k][x] = (gm->xsc[k][x] <= -eslINFINITY) ? -eslINFINITY : roundf(om->scale_b * gm->xsc[k][x]);
1926
1927 /* NN, CC, JJ hardcoded 0 in limited precision */
1928 gm->xsc[p7P_N][p7P_LOOP] = gm->xsc[p7P_J][p7P_LOOP] = gm->xsc[p7P_C][p7P_LOOP] = 0;
1929
1930 return eslOK;
1931 }
1932
1933
1934 /* Function: p7_profile_SameAsVF()
1935 * Synopsis: Round a generic profile to match ViterbiFilter scores.
1936 * Incept: SRE, Wed Jul 30 13:37:48 2008 [Janelia]
1937 *
1938 * Purpose: Round all the scores in a generic (lspace) <P7_PROFILE> <gm> in
1939 * exactly the same way that the scores in the
1940 * <P7_OPROFILE> <om> were rounded. Then we can test that two profiles
1941 * give identical internal scores in testing, say,
1942 * <p7_ViterbiFilter()> against <p7_GViterbi()>.
1943 *
1944 * The 3nat approximation is used; NN=CC=JJ=0, and 3 nats are
1945 * subtracted at the end to account for their contribution.
1946 *
1947 * To convert a generic Viterbi score <gsc> calculated with this profile
1948 * to a nat score that should match ViterbiFilter() exactly,
1949 * do <(gsc / om->scale_w) - 3.0>.
1950 *
1951 * <gm> must be the same profile that <om> was constructed from.
1952 *
1953 * <gm> is irrevocably altered by this call.
1954 *
1955 * Do not call this more than once on any given <gm>!
1956 *
1957 * Args: <om> - optimized profile, containing scale information.
1958 * <gm> - generic profile that <om> was built from.
1959 *
1960 * Returns: <eslOK> on success.
1961 *
1962 * Throws: (no abnormal error conditions)
1963 */
1964 int
1965 p7_profile_SameAsVF(const P7_OPROFILE *om, P7_PROFILE *gm)
1966 {
1967 int k;
1968 int x;
1969
1970 /* Transitions */
1971 /* <= -eslINFINITY test is used solely to silence compiler. really testing == -eslINFINITY */
1972 for (x = 0; x < gm->M*p7P_NTRANS; x++)
1973 gm->tsc[x] = (gm->tsc[x] <= -eslINFINITY) ? -eslINFINITY : roundf(om->scale_w * gm->tsc[x]);
1974
1975 /* Enforce the rule that no II can be 0; max of -1 */
1976 for (x = p7P_II; x < gm->M*p7P_NTRANS; x += p7P_NTRANS)
1977 if (gm->tsc[x] == 0.0) gm->tsc[x] = -1.0;
1978
1979 /* Emissions */
1980 for (x = 0; x < gm->abc->Kp; x++)
1981 for (k = 0; k <= gm->M; k++)
1982 {
1983 gm->rsc[x][k*2] = (gm->rsc[x][k*2] <= -eslINFINITY) ? -eslINFINITY : roundf(om->scale_w * gm->rsc[x][k*2]);
1984 gm->rsc[x][k*2+1] = 0.0; /* insert score: VF makes it zero no matter what. */
1985 }
1986
1987 /* Specials */
1988 for (k = 0; k < p7P_NXSTATES; k++)
1989 for (x = 0; x < p7P_NXTRANS; x++)
1990 gm->xsc[k][x] = (gm->xsc[k][x] <= -eslINFINITY) ? -eslINFINITY : roundf(om->scale_w * gm->xsc[k][x]);
1991
1992 /* 3nat approximation: NN, CC, JJ hardcoded 0 in limited precision */
1993 gm->xsc[p7P_N][p7P_LOOP] = gm->xsc[p7P_J][p7P_LOOP] = gm->xsc[p7P_C][p7P_LOOP] = 0.0;
1994
1995 return eslOK;
1996 }
1997 /*------------ end, P7_OPROFILE debugging tools ----------------*/
1998
1999
2000
2001 /*****************************************************************
2002 * 5. Benchmark driver.
2003 *****************************************************************/
2004
2005 #ifdef p7OPROFILE_BENCHMARK
2006
2007 #include <p7_config.h>
2008
2009 #include "easel.h"
2010 #include "esl_alphabet.h"
2011 #include "esl_getopts.h"
2012 #include "esl_random.h"
2013 #include "esl_stopwatch.h"
2014
2015 #include "hmmer.h"
2016 #include "impl_neon.h"
2017
2018 static ESL_OPTIONS options[] = {
2019 /* name type default env range toggles reqs incomp help docgroup*/
2020 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
2021 { "-L", eslARG_INT, "400", NULL, NULL, NULL, NULL, NULL, "length of target sequence", 0 },
2022 { "-N", eslARG_INT, "100000", NULL, NULL, NULL, NULL, NULL, "number of conversions to time", 0 },
2023 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
2024 };
2025 static char usage[] = "[-options] <hmmfile>";
2026 static char banner[] = "benchmark driver for the generic implementation";
2027
2028 int
2029 main(int argc, char **argv)
2030 {
2031 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
2032 char *hmmfile = esl_opt_GetArg(go, 1);
2033 ESL_STOPWATCH *w = esl_stopwatch_Create();
2034 ESL_ALPHABET *abc = NULL;
2035 P7_HMMFILE *hfp = NULL;
2036 P7_HMM *hmm = NULL;
2037 P7_BG *bg = NULL;
2038 P7_PROFILE *gm = NULL;
2039 P7_OPROFILE *om = NULL;
2040 int L = esl_opt_GetInteger(go, "-L");
2041 int N = esl_opt_GetInteger(go, "-N");
2042 int i;
2043
2044 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
2045 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
2046
2047 bg = p7_bg_Create(abc);
2048 p7_bg_SetLength(bg, L);
2049 gm = p7_profile_Create(hmm->M, abc);
2050 p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
2051 om = p7_oprofile_Create(gm->M, abc);
2052
2053 esl_stopwatch_Start(w);
2054 for (i = 0; i < N; i++)
2055 p7_oprofile_Convert(gm, om);
2056 esl_stopwatch_Stop(w);
2057 esl_stopwatch_Display(stdout, w, "# CPU time: ");
2058 printf("# M = %d\n", gm->M);
2059
2060 p7_oprofile_Destroy(om);
2061 p7_profile_Destroy(gm);
2062 p7_bg_Destroy(bg);
2063 p7_hmm_Destroy(hmm);
2064 p7_hmmfile_Close(hfp);
2065 esl_alphabet_Destroy(abc);
2066 esl_stopwatch_Destroy(w);
2067 esl_getopts_Destroy(go);
2068 return 0;
2069 }
2070 #endif /*p7OPROFILE_BENCHMARK*/
2071 /*---------------- end, benchmark driver ------------------------*/
2072
2073
2074
2075
2076
2077 /*****************************************************************
2078 * 6. Unit tests
2079 *****************************************************************/
2080 #ifdef p7OPROFILE_TESTDRIVE
2081
2082
2083 #endif /*p7OPROFILE_TESTDRIVE*/
2084 /*------------------- end, unit tests ---------------------------*/
2085
2086
2087
2088
2089 /*****************************************************************
2090 * 7. Test driver
2091 *****************************************************************/
2092 #ifdef p7OPROFILE_TESTDRIVE
2093
2094
2095 #endif /*p7OPROFILE_TESTDRIVE*/
2096 /*------------------- end, test driver --------------------------*/
2097
2098
2099 /*****************************************************************
2100 * 8. Example
2101 *****************************************************************/
2102 #ifdef p7OPROFILE_EXAMPLE
2103 /* gcc -std=gnu99 -g -Wall -Dp7OPROFILE_EXAMPLE -I.. -I../../easel -L.. -L../../easel -o p7_oprofile_example p7_oprofile.c -lhmmer -leasel -lm
2104 * ./p7_oprofile_example <hmmfile>
2105 */
2106 #include <p7_config.h>
2107 #include <stdlib.h>
2108 #include "easel.h"
2109 #include "hmmer.h"
2110
2111 int
2112 main(int argc, char **argv)
2113 {
2114 char *hmmfile = argv[1];
2115 ESL_ALPHABET *abc = NULL;
2116 P7_HMMFILE *hfp = NULL;
2117 P7_HMM *hmm = NULL;
2118 P7_BG *bg = NULL;
2119 P7_PROFILE *gm = NULL;
2120 P7_OPROFILE *om1 = NULL;
2121 P7_OPROFILE *om2 = NULL;
2122 int status;
2123 char errbuf[eslERRBUFSIZE];
2124
2125 status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf);
2126 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
2127 else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
2128 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf);
2129
2130 status = p7_hmmfile_Read(hfp, &abc, &hmm);
2131 if (status == eslEFORMAT) p7_Fail("Bad file format in HMM file %s:\n%s\n", hfp->fname, hfp->errbuf);
2132 else if (status == eslEINCOMPAT) p7_Fail("HMM in %s is not in the expected %s alphabet\n", hfp->fname, esl_abc_DecodeType(abc->type));
2133 else if (status == eslEOF) p7_Fail("Empty HMM file %s? No HMM data found.\n", hfp->fname);
2134 else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s\n", hfp->fname);
2135
2136 bg = p7_bg_Create(abc);
2137 gm = p7_profile_Create(hmm->M, abc);
2138 om1 = p7_oprofile_Create(hmm->M, abc);
2139 p7_ProfileConfig(hmm, bg, gm, 400, p7_LOCAL);
2140 p7_oprofile_Convert(gm, om1);
2141
2142 p7_oprofile_Dump(stdout, om1);
2143
2144 om2 = p7_oprofile_Copy(om1);
2145 if (p7_oprofile_Compare(om1, om2, 0.001f, errbuf) != eslOK) printf ("ERROR %s\n", errbuf);
2146
2147 p7_oprofile_Destroy(om1);
2148 p7_oprofile_Destroy(om2);
2149 p7_profile_Destroy(gm);
2150 p7_bg_Destroy(bg);
2151 p7_hmm_Destroy(hmm);
2152 p7_hmmfile_Close(hfp);
2153 esl_alphabet_Destroy(abc);
2154 return eslOK;
2155 }
2156 #endif /*p7OPROFILE_EXAMPLE*/
2157 /*----------------------- end, example --------------------------*/