Debian Codegraph @

hmmer/src/impl_neon/io.c

raw symbols SCIP
1 /* Saving optimized profiles in two pieces: MSV part and the rest.
2 *
3 * To accelerate hmmscan, which is limited by speed of HMM input,
4 * hmmpress saves an optimized profile in two pieces. One file gets
5 * a bare minimum of information needed to run the MSV filter.
6 * The other file gets the rest of the profile. Both files are binary,
7 * stored exactly as the <P7_OPROFILE> has the information internally.
8 *
9 * By convention, hmmpress calls the two files <hmmfile>.h3f and
10 * <hmmfile>.h3p, which nominally stand for "H3 filter" and "H3
11 * profile".
12 *
13 * Contents:
14 * 1. Writing optimized profiles to two files.
15 * 2. Reading optimized profiles in two stages.
16 * 3. Utility routines.
17 * 4. Benchmark driver.
18 * 5. Unit tests.
19 * 6. Test driver.
20 * 7. Example.
21 *
22 * TODO:
23 * - crossplatform binary compatibility (endedness and off_t)
24 * - Write() could save a tag (model #) instead of name for verifying
25 * that MSV and Rest parts match, saving a malloc for var-lengthed name
26 * in ReadRest().
27 */
28 #include <p7_config.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef HMMER_THREADS
35 #include <pthread.h>
36 #endif
37
38 #include <arm_neon.h> /* NEON */
39
40 #include "easel.h"
41
42 #include "hmmer.h"
43 #include "impl_neon.h"
44
45 static uint32_t v3f_fmagic = 0xb3e6e6f3; /* 3/f binary MSV file, NEON: "3ffs" = 0x 33 66 66 73 + 0x80808080 */
46 static uint32_t v3f_pmagic = 0xb3e6f0f3; /* 3/f binary profile file, NEON: "3fps" = 0x 33 66 70 73 + 0x80808080 */
47
48 static uint32_t v3e_fmagic = 0xb3e5e6f3; /* 3/e binary MSV file, NEON: "3efs" = 0x 33 65 66 73 + 0x80808080 */
49 static uint32_t v3e_pmagic = 0xb3e5f0f3; /* 3/e binary profile file, NEON: "3eps" = 0x 33 65 70 73 + 0x80808080 */
50
51 static uint32_t v3d_fmagic = 0xb3e4e6f3; /* 3/d binary MSV file, NEON: "3dfs" = 0x 33 64 66 73 + 0x80808080 */
52 static uint32_t v3d_pmagic = 0xb3e4f0f3; /* 3/d binary profile file, NEON: "3dps" = 0x 33 64 70 73 + 0x80808080 */
53
54 static uint32_t v3c_fmagic = 0xb3e3e6f3; /* 3/c binary MSV file, NEON: "3cfs" = 0x 33 63 66 73 + 0x80808080 */
55 static uint32_t v3c_pmagic = 0xb3e3f0f3; /* 3/c binary profile file, NEON: "3cps" = 0x 33 63 70 73 + 0x80808080 */
56
57 static uint32_t v3b_fmagic = 0xb3e2e6f3; /* 3/b binary MSV file, NEON: "3bfs" = 0x 33 62 66 73 + 0x80808080 */
58 static uint32_t v3b_pmagic = 0xb3e2f0f3; /* 3/b binary profile file, NEON: "3bps" = 0x 33 62 70 73 + 0x80808080 */
59
60 static uint32_t v3a_fmagic = 0xe8b3e6f3; /* 3/a binary MSV file, NEON: "h3fs" = 0x 68 33 66 73 + 0x80808080 */
61 static uint32_t v3a_pmagic = 0xe8b3f0f3; /* 3/a binary profile file, NEON: "h3ps" = 0x 68 33 70 73 + 0x80808080 */
62
63
64 /*****************************************************************
65 *# 1. Writing optimized profiles to two files.
66 *****************************************************************/
67
68 /* Function: p7_oprofile_Write()
69 * Synopsis: Write an optimized profile in two files.
70 *
71 * Purpose: Write the MSV filter part of <om> to open binary stream
72 * <ffp>, and the rest of the model to <pfp>. These two
73 * streams will typically be <.h3f> and <.h3p> files
74 * being created by hmmpress.
75 *
76 * Args: ffp - open binary stream for saving MSV filter part
77 * pfp - open binary stream for saving rest of profile
78 * om - optimized profile to save
79 *
80 * Returns: <eslOK> on success.
81 *
82 * Throws: <eslEWRITE> on any write failure, such as filling
83 * the disk.
84 */
85 int
86 p7_oprofile_Write(FILE *ffp, FILE *pfp, P7_OPROFILE *om)
87 {
88 int Q4 = p7O_NQF(om->M);
89 int Q8 = p7O_NQW(om->M);
90 int Q16 = p7O_NQB(om->M);
91 int Q16x = p7O_NQB(om->M) + p7O_EXTRA_SB;
92 int n = strlen(om->name);
93 int x;
94
95 /* <ffp> is the part of the oprofile that MSVFilter() needs */
96 if (fwrite((char *) &(v3f_fmagic), sizeof(uint32_t), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
97 if (fwrite((char *) &(om->M), sizeof(int), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
98 if (fwrite((char *) &(om->abc->type), sizeof(int), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
99 if (fwrite((char *) &n, sizeof(int), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
100 if (fwrite((char *) om->name, sizeof(char), n+1, ffp) != n+1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
101 if (fwrite((char *) &(om->max_length),sizeof(int), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
102 if (fwrite((char *) &(om->tbm_b), sizeof(uint8_t), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
103 if (fwrite((char *) &(om->tec_b), sizeof(uint8_t), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
104 if (fwrite((char *) &(om->tjb_b), sizeof(uint8_t), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
105 if (fwrite((char *) &(om->scale_b), sizeof(float), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
106 if (fwrite((char *) &(om->base_b), sizeof(uint8_t), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
107 if (fwrite((char *) &(om->bias_b), sizeof(uint8_t), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
108
109 for (x = 0; x < om->abc->Kp; x++)
110 if (fwrite( (char *) om->sbv[x], sizeof(uint8x16_t), Q16x, ffp) != Q16x) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
111
112 for (x = 0; x < om->abc->Kp; x++)
113 if (fwrite( (char *) om->rbv[x], sizeof(uint8x16_t), Q16, ffp) != Q16) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
114
115 if (fwrite((char *) om->evparam, sizeof(float), p7_NEVPARAM, ffp) != p7_NEVPARAM) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
116 if (fwrite((char *) om->offs, sizeof(off_t), p7_NOFFSETS, ffp) != p7_NOFFSETS) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
117 if (fwrite((char *) om->compo, sizeof(float), p7_MAXABET, ffp) != p7_MAXABET) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
118 if (fwrite((char *) &(v3f_fmagic), sizeof(uint32_t), 1, ffp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed"); /* sentinel */
119
120 /* <pfp> gets the rest of the oprofile */
121 if (fwrite((char *) &(v3f_pmagic), sizeof(uint32_t), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
122 if (fwrite((char *) &(om->M), sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
123 if (fwrite((char *) &(om->abc->type), sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
124 if (fwrite((char *) &n, sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
125 if (fwrite((char *) om->name, sizeof(char), n+1, pfp) != n+1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
126
127 if (om->acc == NULL) {
128 n = 0;
129 if (fwrite((char *) &n, sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
130 } else {
131 n = strlen(om->acc);
132 if (fwrite((char *) &n, sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
133 if (fwrite((char *) om->acc, sizeof(char), n+1, pfp) != n+1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
134 }
135
136 if (om->desc == NULL) {
137 n = 0;
138 if (fwrite((char *) &n, sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
139 } else {
140 n = strlen(om->desc);
141 if (fwrite((char *) &n, sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
142 if (fwrite((char *) om->desc, sizeof(char), n+1, pfp) != n+1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
143 }
144
145 if (fwrite((char *) om->rf, sizeof(char), om->M+2, pfp) != om->M+2) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
146 if (fwrite((char *) om->mm, sizeof(char), om->M+2, pfp) != om->M+2) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
147 if (fwrite((char *) om->cs, sizeof(char), om->M+2, pfp) != om->M+2) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
148 if (fwrite((char *) om->consensus, sizeof(char), om->M+2, pfp) != om->M+2) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
149
150 /* ViterbiFilter part */
151 if (fwrite((char *) om->twv, sizeof(int16x8_t), 8*Q8, pfp) != 8*Q8) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
152 for (x = 0; x < om->abc->Kp; x++)
153 if (fwrite( (char *) om->rwv[x], sizeof(int16x8_t), Q8, pfp) != Q8) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
154 for (x = 0; x < p7O_NXSTATES; x++)
155 if (fwrite( (char *) om->xw[x], sizeof(int16_t), p7O_NXTRANS, pfp) != p7O_NXTRANS) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
156 if (fwrite((char *) &(om->scale_w), sizeof(float), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
157 if (fwrite((char *) &(om->base_w), sizeof(int16_t), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
158 if (fwrite((char *) &(om->ddbound_w), sizeof(int16_t), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
159 if (fwrite((char *) &(om->ncj_roundoff), sizeof(float), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
160
161 /* Forward/Backward part */
162 if (fwrite((char *) om->tfv, sizeof(float32x4_t), 8*Q4, pfp) != 8*Q4) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
163 for (x = 0; x < om->abc->Kp; x++)
164 if (fwrite( (char *) om->rfv[x], sizeof(float32x4_t), Q4, pfp) != Q4) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
165 for (x = 0; x < p7O_NXSTATES; x++)
166 if (fwrite( (char *) om->xf[x], sizeof(float), p7O_NXTRANS, pfp) != p7O_NXTRANS) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
167
168 if (fwrite((char *) om->cutoff, sizeof(float), p7_NCUTOFFS, pfp) != p7_NCUTOFFS) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
169 if (fwrite((char *) &(om->nj), sizeof(float), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
170 if (fwrite((char *) &(om->mode), sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
171 if (fwrite((char *) &(om->L) , sizeof(int), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed");
172 if (fwrite((char *) &(v3f_pmagic), sizeof(uint32_t), 1, pfp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "oprofile write failed"); /* sentinel */
173 return eslOK;
174 }
175 /*---------------- end, writing oprofile ------------------------*/
176
177
178
179
180 /*****************************************************************
181 * 2. Reading optimized profiles in two stages.
182 *****************************************************************/
183
184 /* Function: p7_oprofile_ReadMSV()
185 * Synopsis: Read MSV filter part of an optimized profile.
186 *
187 * Purpose: Read the MSV filter part of a profile from the
188 * <.h3f> file associated with an open HMM file <hfp>.
189 * Allocate a new model, populate it with this minimal
190 * MSV filter information, and return a pointer to it
191 * in <*ret_om>.
192 *
193 * Our alphabet may get set by the first HMM we read. If
194 * <*byp_abc> is <NULL> at start, create a new alphabet and
195 * return a pointer to it in <*byp_abc>. If <*byp_abc> is
196 * non-<NULL>, it is assumed to be a pointer to an existing
197 * alphabet; we verify that the HMM's alphabet matches it
198 * and <*ret_abc> isn't changed. This is the same
199 * convention used by <p7_hmmfile_Read()>.
200 *
201 * The <.h3f> file was opened automatically, if it existed,
202 * when the HMM file was opened with <p7_hmmfile_Open()>.
203 *
204 * When no more HMMs remain in the file, return <eslEOF>.
205 *
206 * Args: hfp - open HMM file, with associated .h3p file
207 * byp_abc - BYPASS: <*byp_abc == ESL_ALPHABET *> if known;
208 * <*byp_abc == NULL> if desired;
209 * <NULL> if unwanted.
210 * ret_om - RETURN: newly allocated <om> with MSV filter
211 * data filled in.
212 *
213 * Returns: <eslOK> on success. <*ret_om> is allocated here;
214 * caller free's with <p7_oprofile_Destroy()>.
215 * <*byp_abc> is allocated here if it was requested;
216 * caller free's with <esl_alphabet_Destroy()>.
217 *
218 * Returns <eslEFORMAT> if <hfp> has no <.h3f> file open,
219 * or on any parsing error.
220 *
221 * Returns <eslEINCOMPAT> if the HMM we read is incompatible
222 * with the existing alphabet <*byp_abc> led us to expect.
223 *
224 * On any returned error, <hfp->errbuf> contains an
225 * informative error message.
226 *
227 * Throws: <eslEMEM> on allocation error.
228 */
229 int
230 p7_oprofile_ReadMSV(P7_HMMFILE *hfp, ESL_ALPHABET **byp_abc, P7_OPROFILE **ret_om)
231 {
232 P7_OPROFILE *om = NULL;
233 ESL_ALPHABET *abc = NULL;
234 uint32_t magic;
235 off_t roff;
236 int M, Q16, Q16x;
237 int x,n;
238 int alphatype;
239 int status;
240
241 hfp->errbuf[0] = '\0';
242 if (hfp->ffp == NULL) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "no MSV profile file; hmmpress probably wasn't run");
243 if (feof(hfp->ffp)) { status = eslEOF; goto ERROR; } /* normal EOF: no more profiles */
244
245 /* keep track of the starting offset of the MSV model */
246 roff = ftello(hfp->ffp);
247
248 if (! fread( (char *) &magic, sizeof(uint32_t), 1, hfp->ffp)) { status = eslEOF; goto ERROR; }
249 if (magic == v3a_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/a); please hmmpress your HMM file again");
250 if (magic == v3b_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/b); please hmmpress your HMM file again");
251 if (magic == v3c_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/c); please hmmpress your HMM file again");
252 if (magic == v3d_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/d); please hmmpress your HMM file again");
253 if (magic == v3e_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/e); please hmmpress your HMM file again");
254 if (magic != v3f_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad magic; not an HMM database?");
255
256 if (! fread( (char *) &M, sizeof(int), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read model size M");
257 if (! fread( (char *) &alphatype, sizeof(int), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read alphabet type");
258 Q16 = p7O_NQB(M);
259 Q16x = p7O_NQB(M) + p7O_EXTRA_SB;
260
261 /* Set or verify alphabet. */
262 if (byp_abc == NULL || *byp_abc == NULL) { /* alphabet unknown: whether wanted or unwanted, make a new one */
263 if ((abc = esl_alphabet_Create(alphatype)) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failed: alphabet");
264 } else { /* alphabet already known: verify it against what we see in the HMM */
265 abc = *byp_abc;
266 if (abc->type != alphatype)
267 ESL_XFAIL(eslEINCOMPAT, hfp->errbuf, "Alphabet type mismatch: was %s, but current profile says %s",
268 esl_abc_DecodeType(abc->type), esl_abc_DecodeType(alphatype));
269 }
270 /* Now we know the sizes of things, so we can allocate. */
271 if ((om = p7_oprofile_Create(M, abc)) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failed: oprofile");
272 om->M = M;
273 om->roff = roff;
274
275 if (! fread((char *) &n, sizeof(int), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read name length");
276 ESL_ALLOC(om->name, sizeof(char) * (n+1));
277 if (! fread((char *) om->name, sizeof(char), n+1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read name");
278
279 if (! fread((char *) &(om->max_length),sizeof(int), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read max_length");
280 if (! fread((char *) &(om->tbm_b), sizeof(uint8_t), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read tbm");
281 if (! fread((char *) &(om->tec_b), sizeof(uint8_t), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read tec");
282 if (! fread((char *) &(om->tjb_b), sizeof(uint8_t), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read tjb");
283 if (! fread((char *) &(om->scale_b), sizeof(float), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read scale");
284 if (! fread((char *) &(om->base_b), sizeof(uint8_t), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read base");
285 if (! fread((char *) &(om->bias_b), sizeof(uint8_t), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read bias");
286 for (x = 0; x < abc->Kp; x++)
287 if (! fread((char *) om->sbv[x], sizeof(uint8x16_t), Q16x, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read ssv scores at %d [residue %c]", x, abc->sym[x]);
288 for (x = 0; x < abc->Kp; x++)
289 if (! fread((char *) om->rbv[x], sizeof(uint8x16_t), Q16, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read msv scores at %d [residue %c]", x, abc->sym[x]);
290 if (! fread((char *) om->evparam, sizeof(float), p7_NEVPARAM, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read stat params");
291 if (! fread((char *) om->offs, sizeof(off_t), p7_NOFFSETS, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read hmmpfam offsets");
292 if (! fread((char *) om->compo, sizeof(float), p7_MAXABET, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read model composition");
293
294 /* record ends with magic sentinel, for detecting binary file corruption */
295 if (! fread( (char *) &magic, sizeof(uint32_t), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "no sentinel magic: .h3f file corrupted?");
296 if (magic != v3f_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad sentinel magic; .h3f file corrupted?");
297
298 /* keep track of the ending offset of the MSV model */
299 om->eoff = ftello(hfp->ffp) - 1;;
300
301 if (byp_abc != NULL) *byp_abc = abc; /* pass alphabet (whether new or not) back to caller, if caller wanted it */
302 *ret_om = om;
303 return eslOK;
304
305 ERROR:
306 if (abc != NULL && (byp_abc == NULL || *byp_abc == NULL)) esl_alphabet_Destroy(abc); /* destroy alphabet if we created it here */
307 if (om != NULL) p7_oprofile_Destroy(om);
308 *ret_om = NULL;
309 return status;
310 }
311
312
313 /* Function: p7_oprofile_ReadInfoMSV()
314 * Synopsis: Read MSV filter info, but not the scores.
315 *
316 * Purpose: Read just enough of the MSV filter header from the
317 * <.h3f> file associated with an open HMM file <hfp>
318 * to skip ahead to the next MSV filter. Allocate a new
319 * model, populate it with just the file offsets of this
320 * model and return a pointer to it in <*ret_om>.
321 *
322 * The <.h3f> file was opened automatically, if it existed,
323 * when the HMM file was opened with <p7_hmmfile_Open()>.
324 *
325 * When no more HMMs remain in the file, return <eslEOF>.
326 *
327 * Args: hfp - open HMM file, with associated .h3p file
328 * byp_abc - BYPASS: <*byp_abc == ESL_ALPHABET *> if known;
329 * <*byp_abc == NULL> if desired;
330 * <NULL> if unwanted.
331 * ret_om - RETURN: newly allocated <om> with partial MSV
332 * filter data filled in.
333 *
334 * Returns: <eslOK> on success. <*ret_om> is allocated here;
335 * caller free's with <p7_oprofile_Destroy()>.
336 * <*byp_abc> is allocated here if it was requested;
337 * caller free's with <esl_alphabet_Destroy()>.
338 *
339 * Returns <eslEFORMAT> if <hfp> has no <.h3f> file open,
340 * or on any parsing error.
341 *
342 * Returns <eslEINCOMPAT> if the HMM we read is incompatible
343 * with the existing alphabet <*byp_abc> led us to expect.
344 *
345 * On any returned error, <hfp->errbuf> contains an
346 * informative error message.
347 *
348 * Throws: <eslEMEM> on allocation error.
349 */
350 int
351 p7_oprofile_ReadInfoMSV(P7_HMMFILE *hfp, ESL_ALPHABET **byp_abc, P7_OPROFILE **ret_om)
352 {
353 P7_OPROFILE *om = NULL;
354 ESL_ALPHABET *abc = NULL;
355 uint32_t magic;
356 off_t roff;
357 int M, Q16, Q16x;
358 int n;
359 int alphatype;
360 int status;
361
362 hfp->errbuf[0] = '\0';
363 if (hfp->ffp == NULL) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "no MSV profile file; hmmpress probably wasn't run");
364 if (feof(hfp->ffp)) { status = eslEOF; goto ERROR; } /* normal EOF: no more profiles */
365
366 /* keep track of the starting offset of the MSV model */
367 roff = ftello(hfp->ffp);
368
369 if (! fread( (char *) &magic, sizeof(uint32_t), 1, hfp->ffp)) { status = eslEOF; goto ERROR; }
370 if (magic == v3a_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/a); please hmmpress your HMM file again");
371 if (magic == v3b_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/b); please hmmpress your HMM file again");
372 if (magic == v3c_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/c); please hmmpress your HMM file again");
373 if (magic == v3d_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/d); please hmmpress your HMM file again");
374 if (magic == v3e_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "binary auxfiles are in an outdated HMMER format (3/e); please hmmpress your HMM file again");
375 if (magic != v3f_fmagic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad magic; not an HMM database?");
376
377 if (! fread( (char *) &M, sizeof(int), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read model size M");
378 if (! fread( (char *) &alphatype, sizeof(int), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read alphabet type");
379 Q16 = p7O_NQB(M);
380 Q16x = p7O_NQB(M) + p7O_EXTRA_SB;
381
382 /* Set or verify alphabet. */
383 if (byp_abc == NULL || *byp_abc == NULL) { /* alphabet unknown: whether wanted or unwanted, make a new one */
384 if ((abc = esl_alphabet_Create(alphatype)) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failed: alphabet");
385 } else { /* alphabet already known: verify it against what we see in the HMM */
386 abc = *byp_abc;
387 if (abc->type != alphatype)
388 ESL_XFAIL(eslEINCOMPAT, hfp->errbuf, "Alphabet type mismatch: was %s, but current profile says %s",
389 esl_abc_DecodeType(abc->type), esl_abc_DecodeType(alphatype));
390 }
391 /* Now we know the sizes of things, so we can allocate. */
392 if ((om = p7_oprofile_Create(M, abc)) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failed: oprofile");
393 om->M = M;
394 om->roff = roff;
395
396 /* calculate the remaining length of the msv model */
397 om->name = NULL;
398 if (!fread((char *) &n, sizeof(int), 1, hfp->ffp)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read name length");
399 roff += (sizeof(int) * 5); /* magic, model size, alphabet type, max length, name length */
400 roff += (sizeof(char) * (n + 1)); /* name string and terminator '\0' */
401 roff += (sizeof(float) + sizeof(uint8_t) * 5); /* transition costs, bias, scale and base */
402 roff += (sizeof(uint8x16_t) * abc->Kp * Q16x); /* ssv scores */
403 roff += (sizeof(uint8x16_t) * abc->Kp * Q16); /* msv scores */
404 roff += (sizeof(float) * p7_NEVPARAM); /* stat params */
405 roff += (sizeof(off_t) * p7_NOFFSETS); /* hmmscan offsets */
406 roff += (sizeof(float) * p7_MAXABET); /* model composition */
407 roff += sizeof(uint32_t); /* sentinel magic */
408
409 /* keep track of the ending offset of the MSV model */
410 p7_oprofile_Position(hfp, roff);
411 om->eoff = ftello(hfp->ffp) - 1;
412
413 if (byp_abc != NULL) *byp_abc = abc; /* pass alphabet (whether new or not) back to caller, if caller wanted it */
414 *ret_om = om;
415 return eslOK;
416
417 ERROR:
418 if (abc != NULL && (byp_abc == NULL || *byp_abc == NULL)) esl_alphabet_Destroy(abc); /* destroy alphabet if we created it here */
419 if (om != NULL) p7_oprofile_Destroy(om);
420 *ret_om = NULL;
421 return status;
422 }
423
424
425 /* Function: p7_oprofile_ReadBlockMSV()
426 * Synopsis: Read the next block of optimized profiles from a hmm file.
427 *
428 * Purpose: Reads a block of optimized profiles from open hmm file <hfp> into
429 * <hmmBlock>.
430 *
431 * Returns: <eslOK> on success; the new sequence is stored in <sqBlock>.
432 *
433 * Returns <eslEOF> when there is no profiles left in the
434 * file (including first attempt to read an empty file).
435 *
436 * Otherwise return the status of the p7_oprofile_ReadMSV function.
437 */
438 int
439 p7_oprofile_ReadBlockMSV(P7_HMMFILE *hfp, ESL_ALPHABET **byp_abc, P7_OM_BLOCK *hmmBlock)
440 {
441 int i;
442 int status = eslOK;
443
444 hmmBlock->count = 0;
445 for (i = 0; i < hmmBlock->listSize; ++i)
446 {
447 status = p7_oprofile_ReadMSV(hfp, byp_abc, &hmmBlock->list[i]);
448 if (status != eslOK) break;
449 ++hmmBlock->count;
450 }
451
452 /* EOF will be returned only in the case were no profiles were read */
453 if (status == eslEOF && i > 0) status = eslOK;
454
455 return status;
456 }
457
458 /* Function: p7_oprofile_ReadRest()
459 * Synopsis: Read the rest of an optimized profile.
460 *
461 * Purpose: Read the rest of an optimized profile <om> from
462 * the <.h3p> file associated with an open HMM
463 * file <hfp>.
464 *
465 * This is the second part of a two-part calling sequence.
466 * The <om> here must be the result of a previous
467 * successful <p7_oprofile_ReadMSV()> call on the same
468 * open <hfp>.
469 *
470 * Args: hfp - open HMM file, from which we've previously
471 * called <p7_oprofile_ReadMSV()>.
472 * om - optimized profile that was successfully
473 * returned by <p7_oprofile_ReadMSV()>.
474 *
475 * Returns: <eslOK> on success, and <om> is now a complete
476 * optimized profile.
477 *
478 * Returns <eslEFORMAT> if <hfp> has no <.h3p> file open,
479 * or on any parsing error, and set <hfp->rr_errbuf> to
480 * an informative error message.
481 *
482 * Throws: <eslESYS> if an <fseek()> fails to reposition the
483 * binary <.h3p> file.
484 *
485 * <eslEMEM> on allocation error.
486 */
487 int
488 p7_oprofile_ReadRest(P7_HMMFILE *hfp, P7_OPROFILE *om)
489 {
490 uint32_t magic;
491 int M, Q4, Q8;
492 int x,n;
493 char *name = NULL;
494 int alphatype;
495 int status;
496
497 #ifdef HMMER_THREADS
498 /* lock the mutex to prevent other threads from reading from the optimized
499 * profile at the same time.
500 */
501 if (hfp->syncRead)
502 {
503 if (pthread_mutex_lock (&hfp->readMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
504 }
505 #endif
506
507 hfp->rr_errbuf[0] = '\0';
508 if (hfp->pfp == NULL) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "no MSV profile file; hmmpress probably wasn't run");
509
510 /* Position the <hfp->pfp> using offset stored in <om> */
511 if (fseeko(hfp->pfp, om->offs[p7_POFFSET], SEEK_SET) != 0) ESL_EXCEPTION(eslESYS, "fseeko() failed");
512
513 if (! fread( (char *) &magic, sizeof(uint32_t), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read magic");
514 if (magic == v3a_pmagic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "binary auxfiles are in an outdated HMMER format (3/a); please hmmpress your HMM file again");
515 if (magic == v3b_pmagic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "binary auxfiles are in an outdated HMMER format (3/b); please hmmpress your HMM file again");
516 if (magic == v3c_pmagic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "binary auxfiles are in an outdated HMMER format (3/c); please hmmpress your HMM file again");
517 if (magic == v3d_pmagic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "binary auxfiles are in an outdated HMMER format (3/d); please hmmpress your HMM file again");
518 if (magic == v3e_pmagic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "binary auxfiles are in an outdated HMMER format (3/e); please hmmpress your HMM file again");
519 if (magic != v3f_pmagic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "bad magic; not an HMM database file?");
520
521 if (! fread( (char *) &M, sizeof(int), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read model size M");
522 if (! fread( (char *) &alphatype, sizeof(int), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read alphabet type");
523 if (! fread( (char *) &n, sizeof(int), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read name length");
524 if (M != om->M) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "p/f model length mismatch");
525 if (alphatype != om->abc->type) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "p/f alphabet type mismatch");
526
527 ESL_ALLOC(name, sizeof(char) * (n+1));
528 if (! fread( (char *) name, sizeof(char), n+1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read name");
529 if (strcmp(name, om->name) != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "p/f name mismatch");
530
531 if (! fread((char *) &n, sizeof(int), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read accession length");
532 if (n > 0) {
533 ESL_ALLOC(om->acc, sizeof(char) * (n+1));
534 if (! fread( (char *) om->acc, sizeof(char), n+1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read accession");
535 }
536 if (! fread((char *) &n, sizeof(int), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read description length");
537 if (n > 0) {
538 ESL_ALLOC(om->desc, sizeof(char) * (n+1));
539 if (! fread( (char *) om->desc, sizeof(char), n+1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read description");
540 }
541
542 if (! fread((char *) om->rf, sizeof(char), M+2, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read rf annotation");
543 if (! fread((char *) om->mm, sizeof(char), M+2, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read mm annotation");
544 if (! fread((char *) om->cs, sizeof(char), M+2, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read cs annotation");
545 if (! fread((char *) om->consensus, sizeof(char), M+2, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read consensus annotation");
546
547 Q4 = p7O_NQF(om->M);
548 Q8 = p7O_NQW(om->M);
549
550 if (! fread((char *) om->twv, sizeof(int16x8_t), 8*Q8, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read <tu>, vitfilter transitions");
551 for (x = 0; x < om->abc->Kp; x++)
552 if (! fread( (char *) om->rwv[x], sizeof(int16x8_t), Q8, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read <ru>[%d], vitfilter emissions for sym %c", x, om->abc->sym[x]);
553 for (x = 0; x < p7O_NXSTATES; x++)
554 if (! fread( (char *) om->xw[x], sizeof(int16_t), p7O_NXTRANS, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read <xu>[%d], vitfilter special transitions", x);
555 if (! fread((char *) &(om->scale_w), sizeof(float), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read scale_w");
556 if (! fread((char *) &(om->base_w), sizeof(int16_t), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read base_w");
557 if (! fread((char *) &(om->ddbound_w), sizeof(int16_t), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read ddbound_w");
558 if (! fread((char *) &(om->ncj_roundoff), sizeof(float), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read ddbound_w");
559
560 if (! fread((char *) om->tfv, sizeof(float32x4_t), 8*Q4, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read <tf> transitions");
561 for (x = 0; x < om->abc->Kp; x++)
562 if (! fread( (char *) om->rfv[x], sizeof(float32x4_t), Q4, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read <rf>[%d] emissions for sym %c", x, om->abc->sym[x]);
563 for (x = 0; x < p7O_NXSTATES; x++)
564 if (! fread( (char *) om->xf[x], sizeof(float), p7O_NXTRANS, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read <xf>[%d] special transitions", x);
565
566 if (! fread((char *) om->cutoff, sizeof(float), p7_NCUTOFFS, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read Pfam score cutoffs");
567 if (! fread((char *) &(om->nj), sizeof(float), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read nj");
568 if (! fread((char *) &(om->mode), sizeof(int), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read mode");
569 if (! fread((char *) &(om->L) , sizeof(int), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read L");
570
571 /* record ends with magic sentinel, for detecting binary file corruption */
572 if (! fread( (char *) &magic, sizeof(uint32_t), 1, hfp->pfp)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "no sentinel magic: .h3p file corrupted?");
573 if (magic != v3f_pmagic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "bad sentinel magic; .h3p file corrupted?");
574
575 #ifdef HMMER_THREADS
576 if (hfp->syncRead)
577 {
578 if (pthread_mutex_unlock (&hfp->readMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
579 }
580 #endif
581
582 free(name);
583 return eslOK;
584
585 ERROR:
586
587 #ifdef HMMER_THREADS
588 if (hfp->syncRead)
589 {
590 if (pthread_mutex_unlock (&hfp->readMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
591 }
592 #endif
593
594 if (name != NULL) free(name);
595 return status;
596 }
597 /*----------- end, reading optimized profiles -------------------*/
598
599
600 /*****************************************************************
601 * 3. Utility routines
602 *****************************************************************/
603 /* Function: p7_oprofile_CreateBlock()
604 * Synopsis: Create a new block of empty <P7_OM_BLOCK>.
605 *
606 * Purpose: Creates a block of empty <P7_OM_BLOCK> profile objects.
607 *
608 * Returns: a pointer to the new <P7_OM_BLOCK>. Caller frees this
609 * with <p7_oprofile_DestroyBlock()>.
610 *
611 * Throws: <NULL> if allocation fails.
612 */
613 P7_OM_BLOCK *
614 p7_oprofile_CreateBlock(int count)
615 {
616 int i = 0;
617
618 P7_OM_BLOCK *block = NULL;
619 int status = eslOK;
620
621 ESL_ALLOC(block, sizeof(*block));
622
623 block->count = 0;
624 block->listSize = 0;
625 block->list = NULL;
626
627 ESL_ALLOC(block->list, sizeof(P7_OPROFILE *) * count);
628 block->listSize = count;
629
630 for (i = 0; i < count; ++i)
631 {
632 block->list[i] = NULL;
633 }
634
635 return block;
636
637 ERROR:
638 if (block != NULL)
639 {
640 if (block->list != NULL) free(block->list);
641 free(block);
642 }
643
644 return NULL;
645 }
646
647 /* Function: p7_oprofile_DestroyBlock()
648 * Synopsis: Frees an <P7_OM_BLOCK>.
649 *
650 * Purpose: Free a Create()'d block of profiles.
651 */
652 void
653 p7_oprofile_DestroyBlock(P7_OM_BLOCK *block)
654 {
655 int i;
656
657 if (block == NULL) return;
658
659 if (block->list != NULL)
660 {
661 for (i = 0; i < block->listSize; ++i)
662 {
663 if (block->list[i] != NULL) p7_oprofile_Destroy(block->list[i]);
664 }
665 free(block->list);
666 }
667
668 free(block);
669 return;
670 }
671
672 /* Function: p7_oprofile_Position()
673 * Synopsis: Reposition an open hmm file to an offset.
674 *
675 * Purpose: Reposition an open <hfp> to offset <offset>.
676 * <offset> would usually be the first byte of a
677 * desired hmm record.
678 *
679 * Returns: <eslOK> on success;
680 * <eslEOF> if no data can be read from this position.
681 *
682 * Throws: <eslEINVAL> if the <sqfp> is not positionable.
683 * <eslEFORMAT> if no msv profile opened.
684 * <eslESYS> if the fseeko() call fails.
685 */
686 int
687 p7_oprofile_Position(P7_HMMFILE *hfp, off_t offset)
688 {
689 if (hfp->ffp == NULL) ESL_EXCEPTION(eslEFORMAT, hfp->errbuf, "no MSV profile file; hmmpress probably wasn't run");
690 if (hfp->do_stdin) ESL_EXCEPTION(eslEINVAL, "can't Position() in standard input");
691 if (hfp->do_gzip) ESL_EXCEPTION(eslEINVAL, "can't Position() in a gzipped file");
692 if (offset < 0) ESL_EXCEPTION(eslEINVAL, "bad offset");
693
694 if (fseeko(hfp->ffp, offset, SEEK_SET) != 0) ESL_EXCEPTION(eslESYS, "fseeko() failed");
695
696 return eslOK;
697 }
698
699 /*-------------------- end, utility routines ---------------------*/
700
701
702 /*****************************************************************
703 * 4. Benchmark driver.
704 *****************************************************************/
705 #ifdef p7IO_BENCHMARK
706 /*
707 gcc -g -Wall -o benchmark-io -I.. -L.. -I../../easel -L../../easel -Dp7IO_BENCHMARK io.c -lhmmer -leasel -lm
708 icc -O3 -static -o benchmark-io -I.. -L.. -I../../easel -L../../easel -Dp7IO_BENCHMARK io.c -lhmmer -leasel -lm
709
710 ./benchmark-io Pfam.msv
711 */
712 #include <p7_config.h>
713
714 #include <stdlib.h>
715 #include <stdio.h>
716
717 #include "easel.h"
718 #include "esl_getopts.h"
719 #include "esl_stopwatch.h"
720
721 #include "hmmer.h"
722
723 static ESL_OPTIONS options[] = {
724 /* name type default env range toggles reqs incomp help docgroup*/
725 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
726 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
727 };
728 static char usage[] = "[-options] <HMM MSV profile file>";
729 static char banner[] = "benchmark driver for profile input";
730
731 int
732 main(int argc, char **argv)
733 {
734 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
735 ESL_STOPWATCH *w = esl_stopwatch_Create();
736 ESL_ALPHABET *abc = NULL;
737 char *msvfile = esl_opt_GetArg(go, 1);
738 FILE *msvfp = NULL;
739 P7_OPROFILE *om = NULL;
740 int nmodel = 0;
741 uint64_t totM = 0;
742 int status;
743
744 esl_stopwatch_Start(w);
745
746 if ((msvfp = fopen(msvfile, "r")) == NULL) p7_Fail("Failed to open MSV file %s for reading.\n", msvfile);
747
748 while ((status = p7_oprofile_ReadMSV(msvfp, &abc, NULL, &om)) == eslOK)
749 {
750 nmodel++;
751 totM += om->M;
752
753 p7_oprofile_Destroy(om);
754 }
755 if (status == eslEFORMAT) p7_Fail("bad file format in profile file %s", msvfile);
756 else if (status == eslEINCOMPAT) p7_Fail("profile file %s contains different alphabets", msvfile);
757 else if (status != eslEOF) p7_Fail("Unexpected error in reading profiles from %s", msvfile);
758
759 esl_stopwatch_Stop(w);
760 esl_stopwatch_Display(stdout, w, "# CPU time: ");
761 printf("# number of models: %d\n", nmodel);
762 printf("# total M: %" PRId64 "\n", totM);
763
764 fclose(msvfp);
765 esl_alphabet_Destroy(abc);
766 esl_stopwatch_Destroy(w);
767 esl_getopts_Destroy(go);
768 return 0;
769 }
770 #endif /*IO_BENCHMARK*/
771 /*---------------- end, benchmark driver ------------------------*/
772
773
774 /*****************************************************************
775 * 5. Unit tests.
776 *****************************************************************/
777 #ifdef p7IO_TESTDRIVE
778
779 static void
780 utest_ReadWrite(P7_HMM *hmm, P7_OPROFILE *om)
781 {
782 char *msg = "oprofile read/write unit test failure";
783 ESL_ALPHABET *abc = NULL;
784 P7_OPROFILE *om2 = NULL;
785 char tmpfile[16] = "esltmpXXXXXX";
786 char *mfile = NULL;
787 char *ffile = NULL;
788 char *pfile = NULL;
789 char *ssifile = NULL;
790 FILE *fp = NULL;
791 FILE *mfp = NULL;
792 FILE *ffp = NULL;
793 FILE *pfp = NULL;
794 ESL_NEWSSI *nssi = NULL;
795 P7_HMMFILE *hfp = NULL;
796 uint16_t fh = 0;
797 float tolerance = 0.001;
798 char errbuf[eslERRBUFSIZE];
799
800
801 /* 1. A mini version of hmmpress: save the test HMM to a file along with its associated .h3{mfpi} files
802 */
803 if ( esl_tmpfile_named(tmpfile, &fp) != eslOK) esl_fatal(msg);
804 if ( esl_sprintf(&mfile, "%s.h3m", tmpfile) != eslOK) esl_fatal(msg);
805 if ( esl_sprintf(&ffile, "%s.h3f", tmpfile) != eslOK) esl_fatal(msg);
806 if ( esl_sprintf(&pfile, "%s.h3p", tmpfile) != eslOK) esl_fatal(msg);
807 if ( esl_sprintf(&ssifile, "%s.h3i", tmpfile) != eslOK) esl_fatal(msg);
808
809 if ( esl_newssi_Open(ssifile, TRUE, &nssi) != eslOK) esl_fatal(msg);
810 if (( mfp = fopen(mfile, "wb")) == NULL) esl_fatal(msg);
811 if (( ffp = fopen(ffile, "wb")) == NULL) esl_fatal(msg);
812 if (( pfp = fopen(pfile, "wb")) == NULL) esl_fatal(msg);
813
814 /* the disk offsets are all 0 by construction, if there's only one
815 * HMM in the file - but don't want to forget them, if we change the
816 * unit test in the future to be multi HMM
817 */
818 if ((om->offs[p7_MOFFSET] = ftello(mfp)) == -1) esl_fatal(msg);
819 if ((om->offs[p7_FOFFSET] = ftello(ffp)) == -1) esl_fatal(msg);
820 if ((om->offs[p7_POFFSET] = ftello(pfp)) == -1) esl_fatal(msg);
821
822 if ( p7_hmmfile_WriteASCII(fp, -1, hmm) != eslOK) esl_fatal(msg);
823 if ( p7_hmmfile_WriteBinary(mfp, -1, hmm) != eslOK) esl_fatal(msg);
824 if ( p7_oprofile_Write(ffp, pfp, om) != eslOK) esl_fatal(msg);
825
826 if ( esl_newssi_AddFile(nssi, tmpfile, 0, &fh) != eslOK) esl_fatal(msg);
827 if ( esl_newssi_AddKey (nssi, hmm->name, fh, om->offs[p7_MOFFSET], 0, 0) != eslOK) esl_fatal(msg);
828 if ( esl_newssi_Write(nssi) != eslOK) esl_fatal(msg);
829
830 fclose(fp);
831 fclose(mfp);
832 fclose(ffp);
833 fclose(pfp);
834 esl_newssi_Close(nssi);
835
836 /* 2. read the optimized profile back in */
837 if ( p7_hmmfile_Open(tmpfile, NULL, &hfp, NULL) != eslOK) esl_fatal(msg);
838 if ( p7_oprofile_ReadMSV(hfp, &abc, &om2) != eslOK) esl_fatal(msg);
839 if ( p7_oprofile_ReadRest(hfp, om2) != eslOK) esl_fatal(msg);
840
841 /* 3. it should be identical to the original */
842 if ( p7_oprofile_Compare(om, om2, tolerance, errbuf) != eslOK) esl_fatal("%s\n%s", msg, errbuf);
843
844 p7_oprofile_Destroy(om2);
845 p7_hmmfile_Close(hfp);
846 esl_alphabet_Destroy(abc);
847 remove(ssifile);
848 remove(ffile);
849 remove(pfile);
850 remove(mfile);
851 remove(tmpfile);
852
853 free(ssifile);
854 free(mfile);
855 free(ffile);
856 free(pfile);
857 }
858
859 #endif /*p7IO_TESTDRIVE*/
860 /*------------------ end, unit tests ----------------------------*/
861
862
863 /*****************************************************************
864 * 6. Test driver
865 *****************************************************************/
866 #ifdef p7IO_TESTDRIVE
867 /*
868 ./io_utest
869 */
870 #include <p7_config.h>
871
872 #include "easel.h"
873 #include "esl_alphabet.h"
874 #include "esl_getopts.h"
875
876 #include "hmmer.h"
877 #include "impl_neon.h"
878
879 static ESL_OPTIONS options[] = {
880 /* name type default env range toggles reqs incomp help docgroup*/
881 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
882 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
883 { "-M", eslARG_INT, "45", NULL, NULL, NULL, NULL, NULL, "size of random model to sample", 0 },
884 { "-L", eslARG_INT, "45", NULL, NULL, NULL, NULL, NULL, "configure model for length <n>", 0 },
885 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
886 };
887 static char usage[] = "[-options]";
888 static char banner[] = "test driver for NEON Forward, Backward implementations";
889
890 int
891 main(int argc, char **argv)
892 {
893 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
894 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
895 ESL_ALPHABET *abc = NULL;
896 P7_BG *bg = NULL;
897 P7_HMM *hmm = NULL;
898 P7_OPROFILE *om = NULL;
899 int M = esl_opt_GetInteger(go, "-M");
900 int L = esl_opt_GetInteger(go, "-L");
901
902 /* Sample a random HMM and optimized profile, in amino acid alphabet. */
903 if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create alphabet");
904 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
905 if (( p7_oprofile_Sample(r, abc, bg, M, L, &hmm, NULL, &om)) != eslOK) esl_fatal("failed to sample HMM and profile");
906
907 /* unit test(s) */
908 utest_ReadWrite(hmm, om);
909
910 p7_oprofile_Destroy(om);
911 p7_hmm_Destroy(hmm);
912 p7_bg_Destroy(bg);
913 esl_alphabet_Destroy(abc);
914 esl_randomness_Destroy(r);
915 esl_getopts_Destroy(go);
916 return eslOK;
917 }
918
919 #endif /*p7IO_TESTDRIVE*/
920 /*------------------- end, test driver --------------------------*/
921
922
923
924 /*****************************************************************
925 * 7. Example.
926 *****************************************************************/
927 #ifdef p7IO_EXAMPLE
928 /* gcc -g -Wall -Dp7IO_EXAMPLE -I.. -I../../easel -L.. -L../../easel -o io_example io.c -lhmmer -leasel -lm
929 * ./io_example <hmmfile>
930 */
931 #include <p7_config.h>
932
933 #include <stdlib.h>
934 #include <stdio.h>
935
936 #include "easel.h"
937 #include "esl_getopts.h"
938
939 #include "hmmer.h"
940
941 static ESL_OPTIONS options[] = {
942 /* name type default env range toggles reqs incomp help docgroup*/
943 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
944 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "verbose: print model info as they're read", 0 },
945 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
946 };
947 static char usage[] = "[-options] <HMM file>";
948 static char banner[] = "example of writing MSV profile part";
949
950 int
951 main(int argc, char **argv)
952 {
953 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
954 ESL_ALPHABET *abc = NULL;
955 char *hmmfile = esl_opt_GetArg(go, 1);
956 P7_HMMFILE *hfp = NULL;
957 P7_HMM *hmm = NULL;
958 P7_BG *bg = NULL;
959 P7_PROFILE *gm = NULL;
960 P7_OPROFILE *om = NULL;
961 char *fname = NULL;
962 char *pname = NULL;
963 FILE *ffp = NULL;
964 FILE *pfp = NULL;
965 int nmodel = 0;
966 int status;
967 char errbuf[eslERRBUFSIZE];
968
969 status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf);
970 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
971 else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
972 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf);
973
974 esl_sprintf(&fname, "%s.h3f", hmmfile);
975 esl_sprintf(&pname, "%s.h3f", hmmfile);
976 if ((ffp = fopen(fname, "wb")) == NULL) p7_Fail("failed to open %s\n", fname);
977 if ((pfp = fopen(pname, "wb")) == NULL) p7_Fail("failed to open %s\n", pname);
978 free(fname);
979 free(pname);
980
981 while ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) == eslOK)
982 {
983 if (nmodel == 0) { /* first time initialization, now that alphabet known */
984 bg = p7_bg_Create(abc);
985 p7_bg_SetLength(bg, 400);
986 }
987
988 if (esl_opt_GetBoolean(go, "-v")) printf("%s\n", hmm->name);
989 nmodel++;
990
991 gm = p7_profile_Create(hmm->M, abc);
992 p7_ProfileConfig(hmm, bg, gm, 400, p7_LOCAL);
993 om = p7_oprofile_Create(gm->M, abc);
994 p7_oprofile_Convert(gm, om);
995
996 p7_oprofile_Write(ffp, pfp, om);
997
998 p7_profile_Destroy(gm);
999 p7_oprofile_Destroy(om);
1000 p7_hmm_Destroy(hmm);
1001 }
1002 if (status == eslEFORMAT) p7_Fail("bad file format in HMM file %s", hmmfile);
1003 else if (status == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets", hmmfile);
1004 else if (status != eslEOF) p7_Fail("Unexpected error in reading HMMs from %s", hmmfile);
1005
1006 fclose(ffp);
1007 fclose(pfp);
1008 p7_bg_Destroy(bg);
1009 p7_hmmfile_Close(hfp);
1010 esl_alphabet_Destroy(abc);
1011 esl_getopts_Destroy(go);
1012 return 0;
1013 }
1014 #endif /*IO_EXAMPLE*/