Debian Codegraph @

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