Debian Codegraph @

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