Debian Codegraph @

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