Debian Codegraph @

hmmer/src/p7_hmmfile.c

raw symbols SCIP
1 /* Input/output of HMMs.
2 *
3 * Contents:
4 * 1. The P7_HMMFILE object for reading HMMs
5 * 2. Writing HMMER3 HMM files.
6 * 3. API for reading HMM files in various formats.
7 * 4. Private, specific profile HMM file format parsers.
8 * 5. Other private functions involved in i/o.
9 * 6. Benchmark driver.
10 * 7. Unit tests.
11 * 8. Test driver.
12 * 9. Example.
13 */
14 #include <p7_config.h>
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 #ifdef HMMER_THREADS
21 #include <pthread.h>
22 #endif
23
24 #include "easel.h"
25 #include "esl_alphabet.h"
26 #include "esl_ssi.h" /* this gives us esl_byteswap */
27 #include "esl_vectorops.h" /* gives us esl_vec_FCopy() */
28
29 #include "hmmer.h"
30
31 /* Magic numbers identifying binary formats.
32 * Do not change the old magics! Necessary for backwards compatibility.
33 */
34 #if 0 /* temporarily remove all the magic; write backwards compat stuff later */
35 static uint32_t v10magic = 0xe8ededb1; /* v1.0 binary: "hmm1" + 0x80808080 */
36 static uint32_t v10swap = 0xb1edede8; /* byteswapped v1.0 */
37 static uint32_t v11magic = 0xe8ededb2; /* v1.1 binary: "hmm2" + 0x80808080 */
38 static uint32_t v11swap = 0xb2edede8; /* byteswapped v1.1 */
39 static uint32_t v17magic = 0xe8ededb3; /* v1.7 binary: "hmm3" + 0x80808080 */
40 static uint32_t v17swap = 0xb3edede8; /* byteswapped v1.7 */
41 static uint32_t v19magic = 0xe8ededb4; /* V1.9 binary: "hmm4" + 0x80808080 */
42 static uint32_t v19swap = 0xb4edede8; /* V1.9 binary, byteswapped */
43 static uint32_t v20magic = 0xe8ededb5; /* V2.0 binary: "hmm5" + 0x80808080 */
44 static uint32_t v20swap = 0xb5edede8; /* V2.0 binary, byteswapped */
45 #endif
46
47 static uint32_t v3a_magic = 0xe8ededb6; /* 3/a binary: "hmm6" + 0x80808080 */
48 static uint32_t v3b_magic = 0xe8ededb7; /* 3/b binary: "hmm7" + 0x80808080 */
49 static uint32_t v3c_magic = 0xe8ededb8; /* 3/c binary: "hmm8" + 0x80808080 */
50 static uint32_t v3d_magic = 0xe8ededb9; /* 3/d binary: "hmm9" + 0x80808080 */
51 static uint32_t v3e_magic = 0xe8ededb0; /* 3/e binary: "hmm0" + 0x80808080 */
52 static uint32_t v3f_magic = 0xe8ededba; /* 3/f binary: "hmma" + 0x80808080 */
53
54
55 static int read_asc30hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm);
56 static int read_bin30hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm);
57 static int read_asc20hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm);
58
59 static int write_bin_string(FILE *fp, char *s);
60 static int read_bin_string (FILE *fp, char **ret_s);
61 static float h2ascii2prob(char *s, float null);
62
63
64 /*****************************************************************
65 * 1. The P7_HMMFILE object for reading HMMs.
66 *****************************************************************/
67
68 static int open_engine(const char *filename, char *env, P7_HMMFILE **ret_hfp, int do_ascii_only, char *errbuf);
69
70
71 /* Function: p7_hmmfile_Open()
72 * Synopsis: Open an HMM file <filename>.
73 *
74 * Purpose: Open an HMM file <filename>, and prepare to read the first
75 * HMM from it.
76 *
77 * We look for <filename> relative to the current working
78 * directory. Additionally, if we don't find it in the cwd
79 * and <env> is non-NULL, we will look for <filename>
80 * relative to one or more directories in a colon-delimited
81 * list obtained from the environment variable <env>. For
82 * example, if we had <setenv HMMERDB
83 * /misc/db/Pfam:/misc/db/Rfam> in the environment, a
84 * profile HMM application might pass "HMMERDB" as <env>.
85 *
86 * As a special case, if <filename> is "-", then HMMs will
87 * be read from <stdin>. In this case, <env> has no effect.
88 *
89 * As another special case, if <filename> ends in a <.gz>
90 * suffix, the file is assumed to be compressed by GNU
91 * <gzip>, and it is opened for reading from a pipe with
92 * <gunzip -dc>. This feature is only available on
93 * POSIX-compliant systems that have a <popen()> call, and
94 * <HAVE_POPEN> is defined by the configure script at
95 * compile time.
96 *
97 * Args: filename - HMM file to open; or "-" for <stdin>
98 * env - list of paths to look for <hmmfile> in, in
99 * addition to current working dir; or <NULL>
100 * ret_hfp - RETURN: opened <P7_HMMFILE>.
101 * errbuf - error message buffer: <NULL>, or a ptr
102 * to <eslERRBUFSIZE> chars of allocated space.
103 *
104 * Returns: <eslOK> on success, and the open <ESL_HMMFILE> is returned
105 * in <*ret_hfp>.
106 *
107 * <eslENOTFOUND> if <filename> can't be opened for
108 * reading, even after the list of directories in <env> (if
109 * any) is checked.
110 *
111 * <eslEFORMAT> if <filename> is not in a recognized HMMER
112 * HMM file format.
113 *
114 * On all types of error, if a non-NULL <errbuf> was provided,
115 * a useful user error message is left in it.
116 *
117 * Throws: <eslEMEM> on allocation failure.
118 */
119 int
120 p7_hmmfile_Open(const char *filename, char *env, P7_HMMFILE **ret_hfp, char *errbuf)
121 {
122 return open_engine(filename, env, ret_hfp, /*do_ascii_only=*/FALSE, errbuf);
123 }
124
125
126 /* Function: p7_hmmfile_OpenNoDB()
127 * Synopsis: Open only an HMM flatfile, even if pressed db exists.
128 *
129 * Purpose: Same as <p7_hmmfile_Open()>, except that if a pressed
130 * database exists for <filename>, it is ignored. Only
131 * <filename> itself is opened.
132 *
133 * hmmpress needs this call. Otherwise, it would open a
134 * press'ed database that it may be about to overwrite.
135 */
136 int
137 p7_hmmfile_OpenNoDB(const char *filename, char *env, P7_HMMFILE **ret_hfp, char *errbuf)
138 {
139 return open_engine(filename, env, ret_hfp, /*do_ascii_only=*/TRUE, errbuf);
140 }
141
142
143 /* Function: p7_hmmfile_OpenBuffer()
144 *
145 * Purpose: Like p7_hmmfile_Open(), but we're going to parse
146 * a buffer containing ASCII HMM file format.
147 *
148 * Args: buffer - buffer to "open". Not necessarily NUL-terminated.
149 * size - length of buffer in chars
150 * ret_hfp - RETURN: opened <P7_HMMFILE>.
151 *
152 * Returns: <eslOK> on success, and the open <ESL_HMMFILE> is returned
153 * in <*ret_hfp>.
154 *
155 * <eslEFORMAT> if <filename> is not in a recognized HMMER
156 * HMM file format.
157 *
158 * Throws: <eslEMEM> on allocation failure.
159 */
160 int
161 p7_hmmfile_OpenBuffer(const char *buffer, int size, P7_HMMFILE **ret_hfp)
162 {
163 P7_HMMFILE *hfp = NULL;
164 int status;
165 char *tok;
166 int toklen;
167
168 ESL_ALLOC(hfp, sizeof(P7_HMMFILE));
169 hfp->f = NULL;
170 hfp->fname = NULL;
171 hfp->do_gzip = FALSE;
172 hfp->do_stdin = FALSE;
173 hfp->newly_opened = TRUE; /* well, it will be, real soon now */
174 hfp->is_pressed = FALSE;
175 #ifdef HMMER_THREADS
176 hfp->syncRead = FALSE;
177 #endif
178 hfp->parser = NULL;
179 hfp->efp = NULL;
180 hfp->ffp = NULL;
181 hfp->pfp = NULL;
182 hfp->ssi = NULL;
183 hfp->errbuf[0] = '\0';
184 hfp->rr_errbuf[0] = '\0';
185
186 if ((hfp->efp = esl_fileparser_CreateMapped(buffer, size)) == NULL) { status = eslEMEM; goto ERROR; }
187 if ((status = esl_fileparser_SetCommentChar(hfp->efp, '#')) != eslOK) goto ERROR;
188 if ((status = esl_fileparser_GetToken(hfp->efp, &tok, &toklen)) != eslOK) goto ERROR;
189
190 if (strcmp("HMMER3/f", tok) == 0) { hfp->format = p7_HMMFILE_3f; hfp->parser = read_asc30hmm; }
191 else if (strcmp("HMMER3/e", tok) == 0) { hfp->format = p7_HMMFILE_3e; hfp->parser = read_asc30hmm; }
192 else if (strcmp("HMMER3/d", tok) == 0) { hfp->format = p7_HMMFILE_3d; hfp->parser = read_asc30hmm; }
193 else if (strcmp("HMMER3/c", tok) == 0) { hfp->format = p7_HMMFILE_3c; hfp->parser = read_asc30hmm; }
194 else if (strcmp("HMMER3/b", tok) == 0) { hfp->format = p7_HMMFILE_3b; hfp->parser = read_asc30hmm; }
195 else if (strcmp("HMMER3/a", tok) == 0) { hfp->format = p7_HMMFILE_3a; hfp->parser = read_asc30hmm; }
196 else if (strcmp("HMMER2.0", tok) == 0) { hfp->format = p7_HMMFILE_20; hfp->parser = read_asc20hmm; }
197
198 if (hfp->parser == NULL) { status = eslEFORMAT; goto ERROR; }
199
200 *ret_hfp = hfp;
201 return eslOK;
202
203 ERROR:
204 if (hfp != NULL) p7_hmmfile_Close(hfp);
205 *ret_hfp = NULL;
206 if (status == eslEMEM) return status;
207 else if (status == eslENOTFOUND) return status;
208 else return eslEFORMAT;
209 }
210
211
212 /* open_engine()
213 *
214 * Implements the meat of the file opening functions
215 * <p7_hmmfile_Open()> and <p7_hmmfile_OpenNoDB()>.
216 * See their comments above.
217 *
218 * Only returns three types of errors:
219 * eslENOTFOUND - file (the HMM file) or program (gzip, for .gz files) not found
220 * eslEFORMAT - bad HMM file format (or format of associated file)
221 * eslEMEM - allocation failure somewhere
222 * <errbuf>, if non-NULL, will contain a useful error message.
223 *
224 */
225 static int
226 open_engine(const char *filename, char *env, P7_HMMFILE **ret_hfp, int do_ascii_only, char *errbuf)
227 {
228 P7_HMMFILE *hfp = NULL;
229 char *envfile = NULL; /* full path to filename after using environment */
230 char *dbfile = NULL; /* constructed name of an index or binary db file */
231 char *cmd = NULL; /* constructed gzip -dc pipe command */
232 int status;
233 int n = strlen(filename);
234 union { char c[4]; uint32_t n; } magic;
235 char *tok;
236 int toklen;
237
238 ESL_ALLOC(hfp, sizeof(P7_HMMFILE));
239 hfp->f = NULL;
240 hfp->fname = NULL;
241 hfp->do_gzip = FALSE;
242 hfp->do_stdin = FALSE;
243 hfp->newly_opened = TRUE; /* well, it will be, real soon now */
244 hfp->is_pressed = FALSE;
245 #ifdef HMMER_THREADS
246 hfp->syncRead = FALSE;
247 #endif
248 hfp->parser = NULL;
249 hfp->efp = NULL;
250 hfp->ffp = NULL;
251 hfp->pfp = NULL;
252 hfp->ssi = NULL;
253 hfp->errbuf[0] = '\0';
254 hfp->rr_errbuf[0] = '\0';
255
256 /* 1. There's two special reading modes that have limited indexing
257 * and optimization capability: reading from standard input, and
258 * reading a gzip'ped file. Once we've set one of these up and set
259 * either the <do_stdin> or <do_gzip> flag, we won't try to open
260 * any associated indexes or binary database files.
261 */
262 if (strcmp(filename, "-") == 0) /* "-" means read from stdin */
263 {
264 hfp->f = stdin;
265 hfp->do_stdin = TRUE;
266 if ((status = esl_strdup("[STDIN]", -1, &(hfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup failed; shouldn't happen");
267 }
268 #ifdef HAVE_POPEN
269 else if (n > 3 && strcmp(filename+n-3, ".gz") == 0) /* a <*.gz> filename means read via gunzip pipe */
270 {
271 if (! esl_FileExists(filename)) ESL_XFAIL(eslENOTFOUND, errbuf, ".gz file %s not found or not readable", filename);
272 if ((status = esl_sprintf(&cmd, "gzip -dc %s", filename)) != eslOK) ESL_XFAIL(status, errbuf, "when setting up .gz pipe: esl_sprintf() failed");
273 if ((hfp->f = popen(cmd, "r")) == NULL) ESL_XFAIL(eslENOTFOUND, errbuf, "gzip -dc %s failed; gzip not installed or not in PATH?", filename);
274 if ((status = esl_strdup(filename, n, &(hfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
275 hfp->do_gzip = TRUE;
276 free(cmd); cmd = NULL;
277 }
278 #endif /*HAVE_POPEN: gzip mode */
279
280
281 /* 2. If <hfp->f> is still NULL, then we're in the usual situation
282 * of looking for a file on disk. It may either be in the cwd, or
283 * in one of the directories listed in the <env> string. Find it,
284 * open it to <hfp->f>, and set <hfp->filename>. The
285 * <hfp->filename> string will be used later to construct the
286 * names of expected index and binary database files.
287 */
288 if (hfp->f == NULL) {
289 if ((hfp->f = fopen(filename, "r")) != NULL) {
290 if ((status = esl_strdup(filename, n, &(hfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
291 } else if (esl_FileEnvOpen(filename, env, &(hfp->f), &envfile) == eslOK) {
292 n = strlen(envfile);
293 if ((status = esl_strdup(envfile, n, &(hfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
294 free(envfile); envfile = NULL;
295 } else
296 { /* temporarily copy filename over to hfp->fname, even though we haven't opened anything: we'll next try to open <filename>.h3m */
297 if ((status = esl_strdup(filename, n, &(hfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
298 }
299 }
300 /* <hfp->f> may *still* be NULL, if <filename> is a press'ed database and ASCII file is deleted */
301
302
303 /* 3. Look for the binary model file component of a press'ed HMM database.
304 *
305 * If <hfp->f> is still NULL, this is our last chance to find it.
306 * (The ASCII base file may have been deleted to save space, leaving
307 * binary press'ed files.)
308 *
309 * If we've been asked to open only an ASCII file -- because we're being
310 * called by hmmpress, for example! -- then don't do this.
311 */
312 if (! do_ascii_only && ! hfp->do_stdin && ! hfp->do_gzip) {
313 FILE *tmpfp;
314 /* if we opened an ASCII file in the HMMERDB directory, hfp->fname contains fully qualified name of file including the path */
315 if ((status = esl_sprintf(&dbfile, "%s.h3m", hfp->fname) != eslOK)) ESL_XFAIL(status, errbuf, "esl_sprintf() failed; shouldn't happen");
316
317 if ((tmpfp = fopen(dbfile, "rb")) != NULL)
318 {
319 if (hfp->f != NULL) fclose(hfp->f); /* preferentially read the .h3m file, not the original */
320 hfp->f = tmpfp;
321 hfp->is_pressed = TRUE;
322 free(hfp->fname);
323 if ((status = esl_strdup(dbfile, -1, &(hfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
324 }
325 else if (hfp->f == NULL && esl_FileEnvOpen(dbfile, env, &(hfp->f), &envfile) == eslOK)
326 { /* found a binary-only press'ed db in one of the env directories. */
327 free(hfp->fname);
328 if ((status = esl_strdup(envfile, -1, &(hfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed; shouldn't happen");
329 hfp->is_pressed = TRUE;
330 }
331 free(dbfile); dbfile = NULL;
332 }
333
334 /* 4. <hfp->f> must now point to a valid model input stream: if not, we fail.
335 */
336 if (hfp->f == NULL)
337 {
338 if (env) ESL_XFAIL(eslENOTFOUND, errbuf, "HMM file %s not found (nor an .h3m binary of it); also looked in %s", filename, env);
339 else ESL_XFAIL(eslENOTFOUND, errbuf, "HMM file %s not found (nor an .h3m binary of it)", filename);
340 }
341
342
343 /* 5. If we found and opened a binary model file .h3m, open the rest of
344 * the press'd model files. (this can't be true if do_ascii_only is set)
345 */
346 if (hfp->is_pressed)
347 {
348 /* here we rely on the fact that the suffixes are .h3{mfpi}, to construct other names from .h3m file name !! */
349 n = strlen(hfp->fname); /* so, n = '\0', n-1 = 'm' */
350 esl_strdup(hfp->fname, n, &dbfile);
351
352 dbfile[n-1] = 'f'; /* the MSV filter part of the optimized profiles */
353 if ((hfp->ffp = fopen(dbfile, "rb")) == NULL) ESL_XFAIL(eslENOTFOUND, errbuf, "Opened %s, a pressed HMM file; but no .h3f file found", hfp->fname);
354
355 dbfile[n-1] = 'p'; /* the remainder of the optimized profiles */
356 if ((hfp->pfp = fopen(dbfile, "rb")) == NULL) ESL_XFAIL(eslENOTFOUND, errbuf, "Opened %s, a pressed HMM file; but no .h3p file found", hfp->fname);
357
358 dbfile[n-1] = 'i'; /* the SSI index for the .h3m file */
359 status = esl_ssi_Open(dbfile, &(hfp->ssi));
360 if (status == eslENOTFOUND) ESL_XFAIL(eslENOTFOUND, errbuf, "Opened %s, a pressed HMM file; but no .h3i file found", hfp->fname);
361 else if (status == eslEFORMAT) ESL_XFAIL(eslEFORMAT, errbuf, "Opened %s, a pressed HMM file; but format of its .h3i file unrecognized", hfp->fname);
362 else if (status == eslERANGE) ESL_XFAIL(eslEFORMAT, errbuf, "Opened %s, a pressed HMM file; but its .h3i file is 64-bit and your system is 32-bit", hfp->fname);
363 else if (status != eslOK) ESL_XFAIL(eslEFORMAT, errbuf, "Opened %s, a pressed HMM file; but failed to open its .h3i file", hfp->fname);
364
365 free(dbfile); dbfile = NULL;
366 }
367 else
368 {
369 if ((status = esl_sprintf(&dbfile, "%s.ssi", hfp->fname)) != eslOK) ESL_XFAIL(status, errbuf, "esl_sprintf() failed");
370
371 status = esl_ssi_Open(dbfile, &(hfp->ssi)); /* not finding an SSI file is ok. we open it if we find it. */
372 if (status == eslEFORMAT) ESL_XFAIL(status, errbuf, "a %s.ssi file exists (an SSI index), but its SSI format is not recognized", hfp->fname);
373 else if (status == eslERANGE) ESL_XFAIL(status, errbuf, "a %s.ssi file exists (an SSI index), but is 64-bit, and your system is 32-bit", hfp->fname);
374 else if (status != eslOK && status != eslENOTFOUND) ESL_XFAIL(status, errbuf, "esl_ssi_Open() failed");
375 free(dbfile); dbfile = NULL;
376 }
377
378
379 /* 6. Check for binary file format. A pressed db is automatically binary: verify. */
380 if (! fread((char *) &(magic.n), sizeof(uint32_t), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, errbuf, "File exists, but appears to be empty?");
381 if (magic.n == v3a_magic) { hfp->format = p7_HMMFILE_3a; hfp->parser = read_bin30hmm; }
382 else if (magic.n == v3b_magic) { hfp->format = p7_HMMFILE_3b; hfp->parser = read_bin30hmm; }
383 else if (magic.n == v3c_magic) { hfp->format = p7_HMMFILE_3c; hfp->parser = read_bin30hmm; }
384 else if (magic.n == v3d_magic) { hfp->format = p7_HMMFILE_3d; hfp->parser = read_bin30hmm; }
385 else if (magic.n == v3e_magic) { hfp->format = p7_HMMFILE_3e; hfp->parser = read_bin30hmm; }
386 else if (magic.n == v3f_magic) { hfp->format = p7_HMMFILE_3f; hfp->parser = read_bin30hmm; }
387 else if (hfp->is_pressed) ESL_XFAIL(eslEFORMAT, errbuf, "Binary format tag in %s unrecognized\nCurrent H3 format is HMMER3/f. Previous H2/H3 formats also supported.", hfp->fname);
388
389 /* 7. Checks for ASCII file format */
390 if (hfp->parser == NULL)
391 {
392 /* Does the magic appear to be binary, yet we didn't recognize it? */
393 if (magic.n & 0x80000000) ESL_XFAIL(eslEFORMAT, errbuf, "Format tag appears binary, but unrecognized\nCurrent H3 format is HMMER3/f. Previous H2/H3 formats also supported.");
394
395 if ((hfp->efp = esl_fileparser_Create(hfp->f)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "internal error in esl_fileparser_Create()");
396 if ((status = esl_fileparser_SetCommentChar(hfp->efp, '#')) != eslOK) ESL_XFAIL(status, errbuf, "internal error in esl_fileparser_SetCommentChar()");
397 if ((status = esl_fileparser_NextLinePeeked(hfp->efp, magic.c, 4)) != eslOK) ESL_XFAIL(status, errbuf, "internal error in esl_fileparser_NextLinePeeked()");
398 if ((status = esl_fileparser_GetToken(hfp->efp, &tok, &toklen)) != eslOK) ESL_XFAIL(status, errbuf, "internal error in esl_fileparser_GetToken()");
399
400 if (strcmp("HMMER3/f", tok) == 0) { hfp->format = p7_HMMFILE_3f; hfp->parser = read_asc30hmm; }
401 else if (strcmp("HMMER3/e", tok) == 0) { hfp->format = p7_HMMFILE_3e; hfp->parser = read_asc30hmm; }
402 else if (strcmp("HMMER3/d", tok) == 0) { hfp->format = p7_HMMFILE_3d; hfp->parser = read_asc30hmm; }
403 else if (strcmp("HMMER3/c", tok) == 0) { hfp->format = p7_HMMFILE_3c; hfp->parser = read_asc30hmm; }
404 else if (strcmp("HMMER3/b", tok) == 0) { hfp->format = p7_HMMFILE_3b; hfp->parser = read_asc30hmm; }
405 else if (strcmp("HMMER3/a", tok) == 0) { hfp->format = p7_HMMFILE_3a; hfp->parser = read_asc30hmm; }
406 else if (strcmp("HMMER2.0", tok) == 0) { hfp->format = p7_HMMFILE_20; hfp->parser = read_asc20hmm; }
407 else ESL_XFAIL(eslEFORMAT, errbuf, "Format tag is '%s': unrecognized.\nCurrent H3 format is 'HMMER3/f'. Previous H2/H3 formats also supported.", tok);
408 }
409
410 *ret_hfp = hfp;
411 return eslOK;
412
413 ERROR:
414 if (cmd != NULL) free(cmd);
415 if (dbfile != NULL) free(dbfile);
416 if (envfile != NULL) free(envfile);
417 if (hfp != NULL) p7_hmmfile_Close(hfp);
418 *ret_hfp = NULL;
419 if (status == eslEMEM) return status;
420 else if (status == eslENOTFOUND) return status;
421 else return eslEFORMAT;
422 }
423
424 /* Function: p7_hmmfile_Close()
425 *
426 * Purpose: Closes an open HMM file <hfp>.
427 *
428 * Returns: (void)
429 */
430 void
431 p7_hmmfile_Close(P7_HMMFILE *hfp)
432 {
433 if (hfp == NULL) return;
434
435 #ifdef HAVE_POPEN /* gzip functionality */
436 if (hfp->do_gzip && hfp->f != NULL) pclose(hfp->f);
437 #endif
438 if (!hfp->do_gzip && !hfp->do_stdin && hfp->f != NULL) fclose(hfp->f);
439 if (hfp->ffp != NULL) fclose(hfp->ffp);
440 if (hfp->pfp != NULL) fclose(hfp->pfp);
441 if (hfp->fname != NULL) free(hfp->fname);
442 if (hfp->efp != NULL) esl_fileparser_Destroy(hfp->efp);
443 if (hfp->ssi != NULL) esl_ssi_Close(hfp->ssi);
444 #ifdef HMMER_THREADS
445 if (hfp->syncRead) pthread_mutex_destroy (&hfp->readMutex);
446 #endif
447 free(hfp);
448 }
449
450 #ifdef HMMER_THREADS
451 /* Function: p7_hmmfile_CreateLock()
452 *
453 * Purpose: Create a lock to synchronize readers
454 *
455 * Returns: <eslOK> on success.
456 */
457 int
458 p7_hmmfile_CreateLock(P7_HMMFILE *hfp)
459 {
460 int status;
461
462 if (hfp == NULL) return eslEINVAL;
463
464 /* make sure the lock is not created twice */
465 if (!hfp->syncRead)
466 {
467 hfp->syncRead = TRUE;
468 status = pthread_mutex_init(&hfp->readMutex, NULL);
469 if (status != 0) goto ERROR;
470 }
471
472 return eslOK;
473
474 ERROR:
475 hfp->syncRead = FALSE;
476 return eslFAIL;
477 }
478 #endif
479 /*----------------- end, P7_HMMFILE object ----------------------*/
480
481
482
483 /*****************************************************************
484 * 2. Writing HMMER3 HMM files.
485 *****************************************************************/
486 static int multiline(FILE *fp, const char *pfx, char *s);
487 static int multilineString(char *str, int size, const char *pfx, char *s, int *offset);
488 static int printprob(FILE *fp, int fieldwidth, float p);
489 static int probToString(char *str, int size, int fieldwidth, float p, int offset);
490
491 /* Function: p7_hmmfile_WriteASCII()
492 * Synopsis: Write a HMMER3 ASCII save file.
493 *
494 * Purpose: Write a profile HMM <hmm> in an ASCII save file format to
495 * an open stream <fp>.
496 *
497 * Legacy file formats in the 3.x release series are
498 * supported by specifying the <format> code. Pass <-1> to
499 * use the default current standard format; pass a valid
500 * code such as <p7_HMMFILE_3a> to select a specific
501 * format.
502 *
503 * Args: fp - open stream for writing
504 * format - -1 for default format, or a 3.x format code like <p7_HMMFILE_3a>
505 * hmm - HMM to save
506 *
507 * Returns: <eslOK> on success.
508 *
509 * Throws: <eslEINVAL> if <format> isn't a valid 3.0 format code.
510 * <eslEWRITE> on write error.
511 */
512 int
513 p7_hmmfile_WriteASCII(FILE *fp, int format, P7_HMM *hmm)
514 {
515 int k, x;
516 int status;
517
518
519 if (format == -1) format = p7_HMMFILE_3f;
520
521 if (format == p7_HMMFILE_3f) { if (fprintf(fp, "HMMER3/f [%s | %s]\n", HMMER_VERSION, HMMER_DATE) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");}
522 else if (format == p7_HMMFILE_3e) { if (fprintf(fp, "HMMER3/e [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
523 else if (format == p7_HMMFILE_3d) { if (fprintf(fp, "HMMER3/d [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
524 else if (format == p7_HMMFILE_3c) { if (fprintf(fp, "HMMER3/c [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
525 else if (format == p7_HMMFILE_3b) { if (fprintf(fp, "HMMER3/b [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
526 else if (format == p7_HMMFILE_3a) { if (fprintf(fp, "HMMER3/a [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
527 else ESL_EXCEPTION(eslEINVAL, "invalid HMM file format code");
528
529 if (fprintf(fp, "NAME %s\n", hmm->name) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
530 if (hmm->acc && fprintf(fp, "ACC %s\n", hmm->acc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
531 if (hmm->desc && fprintf(fp, "DESC %s\n", hmm->desc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
532 if (fprintf(fp, "LENG %d\n", hmm->M) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
533 if (format >= p7_HMMFILE_3c && hmm->max_length > 0 && fprintf(fp, "MAXL %d\n", hmm->max_length) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
534 if (fprintf(fp, "ALPH %s\n", esl_abc_DecodeType(hmm->abc->type)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
535 if (fprintf(fp, "RF %s\n", (hmm->flags & p7H_RF) ? "yes" : "no") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
536 if (format >= p7_HMMFILE_3f && fprintf(fp, "MM %s\n", (hmm->flags & p7H_MMASK) ? "yes" : "no") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
537 if (format >= p7_HMMFILE_3e && fprintf(fp, "CONS %s\n", (hmm->flags & p7H_CONS) ? "yes" : "no") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
538 if (fprintf(fp, "CS %s\n", (hmm->flags & p7H_CS) ? "yes" : "no") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
539 if (fprintf(fp, "MAP %s\n", (hmm->flags & p7H_MAP) ? "yes" : "no") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
540 if (hmm->ctime != NULL) { if ( fprintf (fp, "DATE %s\n", hmm->ctime) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
541 if (hmm->comlog != NULL) { if ( (status = multiline(fp, "COM ", hmm->comlog)) != eslOK) return status; }
542 if (hmm->nseq > 0) { if ( fprintf (fp, "NSEQ %d\n", hmm->nseq) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
543 if (hmm->eff_nseq >= 0) { if ( fprintf (fp, "EFFN %f\n", hmm->eff_nseq) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
544 if (hmm->flags & p7H_CHKSUM) { if ( fprintf (fp, "CKSUM %u\n", hmm->checksum) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); } /* unsigned 32-bit */
545
546 if (hmm->abc->type == eslRNA || hmm->abc->type == eslDNA ) {
547 if ((hmm->flags & p7H_GA) && fprintf(fp, "GA %.2f\n", hmm->cutoff[p7_GA1]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
548 if ((hmm->flags & p7H_TC) && fprintf(fp, "TC %.2f\n", hmm->cutoff[p7_TC1]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
549 if ((hmm->flags & p7H_NC) && fprintf(fp, "NC %.2f\n", hmm->cutoff[p7_NC1]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
550 } else {
551 if ((hmm->flags & p7H_GA) && fprintf(fp, "GA %.2f %.2f\n", hmm->cutoff[p7_GA1], hmm->cutoff[p7_GA2]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
552 if ((hmm->flags & p7H_TC) && fprintf(fp, "TC %.2f %.2f\n", hmm->cutoff[p7_TC1], hmm->cutoff[p7_TC2]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
553 if ((hmm->flags & p7H_NC) && fprintf(fp, "NC %.2f %.2f\n", hmm->cutoff[p7_NC1], hmm->cutoff[p7_NC2]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
554 }
555 if (hmm->flags & p7H_STATS) {
556 if (format == p7_HMMFILE_3a) { /* reverse compatibility */
557 if (fprintf(fp, "STATS LOCAL VLAMBDA %f\n", hmm->evparam[p7_MLAMBDA]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
558 if (fprintf(fp, "STATS LOCAL VMU %f\n", hmm->evparam[p7_MMU]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
559 if (fprintf(fp, "STATS LOCAL FTAU %f\n", hmm->evparam[p7_FTAU]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
560 } else { /* default stats lines */
561 if (fprintf(fp, "STATS LOCAL MSV %8.4f %8.5f\n", hmm->evparam[p7_MMU], hmm->evparam[p7_MLAMBDA]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
562 if (fprintf(fp, "STATS LOCAL VITERBI %8.4f %8.5f\n", hmm->evparam[p7_VMU], hmm->evparam[p7_VLAMBDA]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
563 if (fprintf(fp, "STATS LOCAL FORWARD %8.4f %8.5f\n", hmm->evparam[p7_FTAU], hmm->evparam[p7_FLAMBDA]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
564 }
565 }
566
567 if (fprintf(fp, "HMM ") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
568 for (x = 0; x < hmm->abc->K; x++)
569 { if (fprintf(fp, " %c ", hmm->abc->sym[x]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
570 if (fputc('\n', fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
571 if (fprintf(fp, " %8s %8s %8s %8s %8s %8s %8s\n",
572 "m->m", "m->i", "m->d", "i->m", "i->i", "d->m", "d->d") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
573 if (hmm->flags & p7H_COMPO) {
574 if (fprintf(fp, " COMPO ") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
575 for (x = 0; x < hmm->abc->K; x++)
576 { if ( (status = printprob(fp, 8, hmm->compo[x])) != eslOK) return status; }
577 if (fputc('\n', fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
578 }
579
580 /* node 0 is special: insert emissions, and B-> transitions */
581 if (fputs(" ", fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
582 for (x = 0; x < hmm->abc->K; x++)
583 { if ( (status = printprob(fp, 8, hmm->ins[0][x])) != eslOK) return status; }
584 if (fputc('\n', fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
585
586 if (fputs(" ", fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
587 for (x = 0; x < p7H_NTRANSITIONS; x++)
588 { if ( (status = printprob(fp, 8, hmm->t[0][x])) != eslOK) return status; }
589 if (fputc('\n', fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
590 for (k = 1; k <= hmm->M; k++) {
591 /* Line 1: k; match emissions; optional map, RF, MM, CS */
592 if (fprintf(fp, " %6d ", k) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
593 for (x = 0; x < hmm->abc->K; x++)
594 { if ( (status = printprob(fp, 8, hmm->mat[k][x])) != eslOK) return status; }
595
596 if (hmm->flags & p7H_MAP) { if (fprintf(fp, " %6d", hmm->map[k]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
597 else { if (fprintf(fp, " %6s", "-") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
598
599 if (format >= p7_HMMFILE_3e) {
600 if (format >= p7_HMMFILE_3f && (hmm->flags & p7H_MMASK) && hmm->mm[k] == 'm' )
601 x = tolower(hmm->abc->sym[hmm->abc->Kp-3]);
602 else if (hmm->flags & p7H_CONS)
603 x = hmm->consensus[k];
604 else
605 x = '-';
606 if (fprintf(fp, " %c", x) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
607 }
608
609
610 if (hmm->rf && hmm->rf[k] == ' ') ESL_EXCEPTION_SYS(eslEWRITE, "input alignment contains an RF line with spaces");
611 if (fprintf(fp, " %c", (hmm->flags & p7H_RF) ? hmm->rf[k] : '-') < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
612 if (format >= p7_HMMFILE_3f) { if (fprintf(fp, " %c", (hmm->flags & p7H_MMASK) ? hmm->mm[k] : '-') < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
613 if (fprintf(fp, " %c\n", (hmm->flags & p7H_CS) ? hmm->cs[k] : '-') < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
614
615 /* Line 2: insert emissions */
616 if (fputs(" ", fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
617 for (x = 0; x < hmm->abc->K; x++)
618 { if ( (status = printprob(fp, 8, hmm->ins[k][x])) != eslOK) return status; }
619 /* Line 3: transitions */
620 if (fputs("\n ", fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
621 for (x = 0; x < p7H_NTRANSITIONS; x++)
622 { if ( (status = printprob(fp, 8, hmm->t[k][x])) != eslOK) return status; }
623 if (fputc('\n', fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
624 }
625 if (fputs("//\n", fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
626 return eslOK;
627 }
628
629
630 /* Function: p7_hmmfile_WriteToString()
631 * Synopsis: Write a HMMER3 HMM to char.
632 *
633 * Purpose: Write a profile HMM <hmm> in to string <ascii_hmm> in ASCII
634 * format.
635 *
636 * Should produce same output as p7_hmmfile_WriteASCII
637 *
638 * Legacy file formats in the 3.x release series are
639 * supported by specifying the <format> code. Pass <-1> to
640 * use the default current standard format; pass a valid
641 * code such as <p7_HMMFILE_3a> to select a specific
642 * format.
643 *
644 * Calling function is responsible for freeing the returned string.
645 *
646 * Args: ascii_hmm - char pointer where the string will be allocated and set
647 * format - -1 for default format, or a 3.x format code like <p7_HMMFILE_3a>
648 * hmm - HMM to save
649 *
650 * Returns: <eslOK> on success or <eslEWRITE> on error.
651 */
652 int
653 p7_hmmfile_WriteToString(char **ascii_hmm, int format, P7_HMM *hmm)
654 {
655 int k, x;
656 int status;
657
658 int offset;
659 int coffset = 0;
660 /* These 3 chars and int are used in the size determiantion */
661 int size;
662 int n = 0;
663 char buff[100];
664 char *end = NULL;
665 char *sptr;
666
667 char *ret_hmm;
668
669 if (format == -1) format = p7_HMMFILE_3f;
670
671 /* In this block of code, interrogate the HMM to work out the amount of memory needed to write it out as an ASCII string */
672 /* The number in each row is the number of fixed chars, inlcuding the '\n' */
673
674 /* The header block containing the tag/value pairs */
675 size = 50 + strlen(HMMER_VERSION) + strlen(HMMER_DATE); /* HMMER version text */
676 size += 7 + strlen(hmm->name); /* NAME line */
677 size += (hmm->acc ? ( 7 + strlen(hmm->acc)) : 0); /* ACC line, if present */
678 size += (hmm->desc ? ( 7 + strlen(hmm->desc)) : 0); /* DESC line, if present */
679 size += 7 + snprintf(buff, 100, "%d", hmm->M); /*LENG tag, we determine size of field later */
680 size += ((format >= p7_HMMFILE_3c && hmm->max_length) ? 7 + snprintf(buff, 100, "%d", hmm->max_length) : 0); /*MAXL line, later formats only, optional */
681 size += 7 + strlen( esl_abc_DecodeType(hmm->abc->type)); /*ALPH tag */
682 size += 10; /*RF tag, yes/no */
683 size += (format >= p7_HMMFILE_3f ? 10 : 0 ); /*MM line, only later formats*/
684 size += (format >= p7_HMMFILE_3e ? 10 : 0 ); /*CONS line, only later formats*/
685 size += 10; /*Consensus secondary structure lines */
686 size += 10; /*MAP line*/
687 size += (hmm->ctime != NULL ? (7 + strlen(hmm->ctime)) : 0); /*DATE line*/
688
689 /* Complicated as it can cover multiple lines */
690 if(hmm->comlog != NULL){
691 /* Determine the number of COM lines by counting the number of '\n' */
692 sptr = hmm->comlog;
693 do {
694 n++; /* last line should not have \n, so count before to get count */
695 end = strchr(sptr, '\n');
696 sptr += (end - sptr) +1;
697 } while (end != NULL && *sptr != '\0');
698 size += ((snprintf(buff, 100, "%d", n) + 8) * n); /*length of all the COM tags*/
699 size += strlen(hmm->comlog);
700 }
701
702 size += (hmm->nseq > 0 ? 7 + snprintf(buff, 100, "%d", hmm->nseq) : 0); /* NSEQ line */
703 size += (hmm->eff_nseq >= 0 ? 7 + snprintf(buff, 100, "%f", hmm->eff_nseq) : 0); /* EFFN line */
704 size += (hmm->flags & p7H_CHKSUM ? 7 + snprintf(buff, 100, "%u", hmm->checksum) : 0); /*CKSUM line */
705
706 /* Thresholds section */
707 size += ((hmm->flags & p7H_GA) ? 8 + snprintf(buff, 100, "%.2f", hmm->cutoff[p7_GA1]) + snprintf(buff, 100, "%.2f", hmm->cutoff[p7_GA2]) : 0);
708 size += ((hmm->flags & p7H_TC) ? 8 + snprintf(buff, 100, "%.2f", hmm->cutoff[p7_TC1]) + snprintf(buff, 100, "%.2f", hmm->cutoff[p7_TC2]) : 0);
709 size += ((hmm->flags & p7H_NC) ? 8 + snprintf(buff, 100, "%.2f", hmm->cutoff[p7_NC1]) + snprintf(buff, 100, "%.2f", hmm->cutoff[p7_NC2]) : 0);
710
711 /* E-value stats */
712 size += ((hmm->flags & p7H_STATS) ?
713 ((format == p7_HMMFILE_3a) ? ( 75 + snprintf(buff, 100, "%f", hmm->evparam[p7_MLAMBDA]) +
714 snprintf(buff, 100, "%f", hmm->evparam[p7_MMU]) +
715 snprintf(buff, 100, "%f", hmm->evparam[p7_FTAU])) :
716 ( 75 + snprintf(buff, 100, "%8.4f", hmm->evparam[p7_MMU]) + snprintf(buff, 100, "%8.5f", hmm->evparam[p7_MLAMBDA]) +
717 snprintf(buff, 100, "%8.4f", hmm->evparam[p7_VMU]) + snprintf(buff, 100, "%8.5f", hmm->evparam[p7_VLAMBDA]) +
718 snprintf(buff, 100, "%8.4f", hmm->evparam[p7_FTAU]) + snprintf(buff, 100, "%8.5f", hmm->evparam[p7_FLAMBDA])))
719 : 0); /* No STATS */
720
721 /* Now on to the body of the HMM */
722 size += 9 + (hmm->abc->K * 9); /* Alphabet labels */
723 size += 71; /* Transitions line labels */
724 size += ((hmm->flags & p7H_COMPO) ? 9 + (hmm->abc->K * 9) : 0); /* Composition line */
725 /* node 0 */
726 size += 9 + (hmm->abc->K * 9); /* Insert emissions */
727 size += 9 + ( p7H_NTRANSITIONS * 9); /* Transitions */
728
729 /* Matrix of probabilities */
730 size += (hmm->M * ( 9 + (hmm->abc->K * 9 ) + 7 + 8 )); /* Line 1: k; match emissions; map (although optional just going to add it, RF, CS, MM) */
731 size += (hmm->M * ( 9 + (hmm->abc->K * 9 ))) ; /* Line 2: insert emissions */
732 size += (hmm->M * ( 9 + (p7H_NTRANSITIONS * 9) )); /* Line 3: transitions */
733 size += 3; /* Final terminating line */
734
735 /* Now allocate the memory for the HMM string */
736 ret_hmm = malloc(sizeof(char) * (size));
737
738 /* Now added the HMM text to the string, remembering to offset the position */
739 /* If anything fails, return an eslEWRITE error */
740
741 /* Header block */
742 if (format == p7_HMMFILE_3f) { if ((offset = snprintf(ret_hmm, size, "HMMER3/f [%s | %s]\n", HMMER_VERSION, HMMER_DATE)) < 0) return eslEWRITE; }
743 else if (format == p7_HMMFILE_3e) { if ((offset = snprintf(ret_hmm, size, "HMMER3/e [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE)) < 0) return eslEWRITE; }
744 else if (format == p7_HMMFILE_3d) { if ((offset = snprintf(ret_hmm, size, "HMMER3/d [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE)) < 0) return eslEWRITE; }
745 else if (format == p7_HMMFILE_3c) { if ((offset = snprintf(ret_hmm, size, "HMMER3/c [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE)) < 0) return eslEWRITE; }
746 else if (format == p7_HMMFILE_3b) { if ((offset = snprintf(ret_hmm, size, "HMMER3/b [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE)) < 0) return eslEWRITE; }
747 else if (format == p7_HMMFILE_3a) { if ((offset = snprintf(ret_hmm, size, "HMMER3/a [%s | %s; reverse compatibility mode]\n", HMMER_VERSION, HMMER_DATE)) < 0) return eslEWRITE; }
748 else return eslEINVAL;
749 coffset = offset;
750
751 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "NAME %s\n", hmm->name)) < 0) return eslEWRITE;
752 coffset += offset;
753
754 if (hmm->acc){
755 if((offset = snprintf(ret_hmm + coffset, size-coffset, "ACC %s\n", hmm->acc)) < 0) return eslEWRITE;
756 coffset += offset;
757 }
758
759 if (hmm->desc){
760 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "DESC %s\n", hmm->desc)) < 0) return eslEWRITE;
761 coffset += offset;
762 }
763
764 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "LENG %d\n", hmm->M)) < 0) return eslEWRITE;
765 coffset += offset;
766
767 if (format >= p7_HMMFILE_3c && hmm->max_length > 0){
768 if((offset = snprintf(ret_hmm + coffset, size-coffset, "MAXL %d\n", hmm->max_length)) < 0) return eslEWRITE;
769 coffset += offset;
770 }
771
772 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "ALPH %s\n", esl_abc_DecodeType(hmm->abc->type))) < 0) return eslEWRITE;
773 coffset += offset;
774
775 if ((offset = snprintf(ret_hmm+coffset, size-coffset, "RF %s\n", (hmm->flags & p7H_RF) ? "yes" : "no")) < 0) return eslEWRITE;
776 coffset += offset;
777
778 if ((format >= p7_HMMFILE_3f)){
779 if ((offset = snprintf(ret_hmm+coffset, size-coffset, "MM %s\n", (hmm->flags & p7H_MMASK) ? "yes" : "no")) < 0) return eslEWRITE;
780 coffset += offset;
781 }
782
783 if ((format >= p7_HMMFILE_3e)){
784 if((offset = snprintf(ret_hmm+coffset, size-coffset, "CONS %s\n", (hmm->flags & p7H_CONS) ? "yes" : "no")) < 0) return eslEWRITE;
785 coffset += offset;
786 }
787
788 if ((offset = snprintf(ret_hmm+coffset, size-coffset, "CS %s\n", (hmm->flags & p7H_CS) ? "yes" : "no")) < 0) return eslEWRITE;
789 coffset += offset;
790
791 if ((offset = snprintf(ret_hmm+coffset, size-coffset, "MAP %s\n", (hmm->flags & p7H_MAP) ? "yes" : "no")) < 0) return eslEWRITE;
792 coffset += offset;
793
794 if (hmm->ctime) {
795 if((offset = snprintf(ret_hmm + coffset, size-coffset, "DATE %s\n", hmm->ctime)) < 0) return eslEWRITE;
796 coffset += offset;
797 }
798
799 if (hmm->comlog) {
800 if ( (status = multilineString(ret_hmm, size, "COM ", hmm->comlog, &coffset)) != eslOK) return status;
801 }
802
803 if (hmm->nseq > 0){
804 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "NSEQ %d\n", hmm->nseq)) < 0) return eslEWRITE;
805 coffset += offset;
806 }
807
808 if (hmm->eff_nseq >= 0){
809 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "EFFN %f\n", hmm->eff_nseq)) < 0) return eslEWRITE;
810 coffset += offset;
811 }
812
813 if (hmm->flags & p7H_CHKSUM) {
814 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "CKSUM %u\n", hmm->checksum)) < 0) return eslEWRITE;
815 coffset += offset;
816 } /* unsigned 32-bit */
817
818
819 /* Thresholds */
820 if ((hmm->flags & p7H_GA)) {
821 if(( offset = snprintf(ret_hmm + coffset, size-coffset, "GA %.2f %.2f\n", hmm->cutoff[p7_GA1], hmm->cutoff[p7_GA2])) < 0) return eslEWRITE;
822 coffset += offset;
823 }
824
825 if ((hmm->flags & p7H_TC)) {
826 if (( offset = snprintf(ret_hmm + coffset, size-coffset, "TC %.2f %.2f\n", hmm->cutoff[p7_TC1], hmm->cutoff[p7_TC2])) < 0) return eslEWRITE;
827 coffset += offset;
828 }
829
830 if ((hmm->flags & p7H_NC)) {
831 if(( offset = snprintf(ret_hmm + coffset , size-coffset, "NC %.2f %.2f\n", hmm->cutoff[p7_NC1], hmm->cutoff[p7_NC2])) < 0) return eslEWRITE;
832 coffset += offset;
833 }
834
835
836 /* E-value stats */
837 if (hmm->flags & p7H_STATS) {
838 if (format == p7_HMMFILE_3a){
839 if ((offset =snprintf(ret_hmm + coffset, size-coffset, "STATS LOCAL VLAMBDA %f\n", hmm->evparam[p7_MLAMBDA])) < 0) return eslEWRITE;
840 coffset += offset;
841 if ((offset =snprintf(ret_hmm + coffset, size-coffset, "STATS LOCAL VMU %f\n", hmm->evparam[p7_MMU])) < 0) return eslEWRITE;
842 coffset += offset;
843 if ((offset =snprintf(ret_hmm + coffset, size-coffset, "STATS LOCAL FTAU %f\n", hmm->evparam[p7_FTAU])) < 0) return eslEWRITE;
844 coffset += offset;
845 }else{
846 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "STATS LOCAL MSV %8.4f %8.5f\n", hmm->evparam[p7_MMU], hmm->evparam[p7_MLAMBDA])) < 0) return eslEWRITE;
847 coffset += offset;
848 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "STATS LOCAL VITERBI %8.4f %8.5f\n", hmm->evparam[p7_VMU], hmm->evparam[p7_VLAMBDA])) < 0) return eslEWRITE;
849 coffset += offset;
850 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "STATS LOCAL FORWARD %8.4f %8.5f\n", hmm->evparam[p7_FTAU], hmm->evparam[p7_FLAMBDA])) < 0) return eslEWRITE;
851 coffset += offset;
852 }
853 }
854
855 /* HMM body */
856 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "HMM ")) < 0) return eslEWRITE;
857 coffset += offset;
858
859 for (x = 0; x < hmm->abc->K; x++){
860 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " %c ", hmm->abc->sym[x])) < 0) return eslEWRITE;
861 coffset += offset;
862 }
863 if((offset = snprintf(ret_hmm + coffset, size-coffset, "\n")) < 0) return eslEWRITE;
864 coffset += offset;
865
866 if ((offset = snprintf(ret_hmm + coffset, size-coffset,
867 " %8s %8s %8s %8s %8s %8s %8s\n",
868 "m->m", "m->i", "m->d", "i->m", "i->i", "d->m", "d->d")) < 0) return eslEWRITE;
869 coffset += offset;
870
871 if (hmm->flags & p7H_COMPO) {
872 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " COMPO ")) < 0) return eslEWRITE;
873 coffset += offset;
874 for (x = 0; x < hmm->abc->K; x++){
875 if ( (status = probToString(ret_hmm, size, 8, hmm->compo[x], coffset)) != eslOK) return status;
876 coffset += 9;
877 }
878 if((offset = snprintf(ret_hmm + coffset, size-coffset, "\n")) < 0) return eslEWRITE;
879 coffset += offset;
880 }
881
882 /* node 0 is special: insert emissions, and B-> transitions */
883 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " ")) < 0) return eslEWRITE;
884 coffset += offset;
885 for (x = 0; x < hmm->abc->K; x++){
886 if ( (status = probToString(ret_hmm, size, 8, hmm->ins[0][x], coffset)) != eslOK) return status;
887 coffset += 9;
888 }
889
890 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "\n")) < 0) return eslEWRITE;
891 coffset += offset;
892
893 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " ")) < 0) return eslEWRITE;
894 coffset += offset;
895 for (x = 0; x < p7H_NTRANSITIONS; x++){
896 if ( (status = probToString(ret_hmm, size, 8, hmm->t[0][x], coffset)) != eslOK) return status;
897 coffset += 9;
898 }
899
900 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "\n")) < 0) return eslEWRITE;
901 coffset += offset;
902
903
904 for (k = 1; k <= hmm->M; k++) {
905 /* Line 1: k; match emissions; optional map, RF, CS */
906 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " %6d ", k)) < 0) return eslEWRITE;
907 coffset += offset;
908
909 for (x = 0; x < hmm->abc->K; x++){
910 if ( (status = probToString(ret_hmm, size, 8, hmm->mat[k][x], coffset)) != eslOK) return status;
911 coffset += 9;
912 }
913
914 if (hmm->flags & p7H_MAP) {
915 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " %6d", hmm->map[k])) < 0) return eslEWRITE;
916 coffset += offset;
917 } else {
918 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " %6s", "-")) < 0) return eslEWRITE;
919 coffset += offset;
920 }
921
922 if (format >= p7_HMMFILE_3e) {
923 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " %c", (hmm->flags & p7H_CONS) ? hmm->consensus[k] : '-')) < 0) return eslEWRITE;
924 coffset += offset;
925 }
926
927 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " %c", (hmm->flags & p7H_RF) ? hmm->rf[k] : '-')) < 0) return eslEWRITE;
928 coffset += offset;
929
930 if (format >= p7_HMMFILE_3f) {
931 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " %c", (hmm->flags & p7H_MMASK) ? hmm->mm[k] : '-')) < 0) return eslEWRITE;
932 coffset += offset;
933 }
934
935 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " %c\n", (hmm->flags & p7H_CS) ? hmm->cs[k] : '-')) < 0) return eslEWRITE;
936 coffset += offset;
937
938 /* Line 2: insert emissions */
939 if ((offset = snprintf(ret_hmm + coffset, size-coffset, " ")) < 0) return eslEWRITE;
940 coffset += offset;
941
942 for (x = 0; x < hmm->abc->K; x++){
943 if( (status = probToString(ret_hmm, size, 8, hmm->ins[k][x], coffset)) != eslOK) return status;
944 coffset += 9; /*Fieldwidth + 1 for space*/
945 }
946
947 /* Line 3: transitions */
948 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "\n ")) < 0) return eslEWRITE;
949 coffset += offset;
950
951 for (x = 0; x < p7H_NTRANSITIONS; x++){
952 if ( (status = probToString(ret_hmm, size, 8, hmm->t[k][x], coffset)) != eslOK) return status;
953 coffset += 9;/*Fieldwidth + 1 for space*/
954 }
955 if ((offset = snprintf(ret_hmm + coffset, size-coffset, "\n")) < 0) return eslEWRITE;
956 coffset += offset;
957 }
958
959 if (snprintf(ret_hmm + coffset, size-coffset, "//\n") < 0) return eslEWRITE;
960 *ascii_hmm = ret_hmm;
961
962 return eslOK;
963 }
964
965 /* Function: p7_hmmfile_WriteBinary()
966 *
967 * Purpose: Writes an HMM to a file in HMMER3 binary format.
968 *
969 * Legacy binary file formats in the 3.x release series are
970 * supported by specifying the <format> code. Pass <-1> to
971 * use the default current standard format; pass a valid
972 * code such as <p7_HMMFILE_3a> to select a specific
973 * binary format.
974 *
975 * Returns: <eslOK> on success.
976 *
977 * Throws: <eslEINVAL> if <format> isn't a valid 3.0 format code.
978 * <eslEWRITE> on write error.
979 */
980 int
981 p7_hmmfile_WriteBinary(FILE *fp, int format, P7_HMM *hmm)
982 {
983 int k;
984 int status;
985
986 if (format == -1) format = p7_HMMFILE_3f;
987
988 /* Legacy: p7H_{ACC, DESC} flags used to be used to indicate
989 * whether optional acc, desc were present. Now we just use
990 * the <NULL> convention. The reason to use the flags was for
991 * saving binary files - we thought we needed to know whether
992 * the acc, desc were present in the binary file before trying
993 * to read them, and having <flags> as one of the first
994 * data fields in the file solved that problem. It's not
995 * necessary - the {read,write}_bin_string() convention is fine.
996 * But write_bin_string() writes a 0 for length for a NULL string,
997 * whereas we weren't writing anything with the previous
998 * flag convention - so to maintain consistency with previous
999 * HMMER binary save files, we use the HMM flags fields here
1000 * and in binary file reads. [xref J5/114]
1001 *
1002 * If binary format is ever revised substantially - revisit this
1003 * issue too - and remove the flags.
1004 */
1005 if (hmm->desc == NULL) hmm->flags &= ~p7H_DESC; else hmm->flags |= p7H_DESC;
1006 if (hmm->acc == NULL) hmm->flags &= ~p7H_ACC; else hmm->flags |= p7H_ACC;
1007
1008 /* ye olde magic number */
1009 if (format == p7_HMMFILE_3f) { if (fwrite((char *) &(v3f_magic), sizeof(uint32_t), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); }
1010 else if (format == p7_HMMFILE_3e) { if (fwrite((char *) &(v3e_magic), sizeof(uint32_t), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); }
1011 else if (format == p7_HMMFILE_3d) { if (fwrite((char *) &(v3d_magic), sizeof(uint32_t), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); }
1012 else if (format == p7_HMMFILE_3c) { if (fwrite((char *) &(v3c_magic), sizeof(uint32_t), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); }
1013 else if (format == p7_HMMFILE_3b) { if (fwrite((char *) &(v3b_magic), sizeof(uint32_t), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); }
1014 else if (format == p7_HMMFILE_3a) { if (fwrite((char *) &(v3a_magic), sizeof(uint32_t), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); }
1015 else ESL_EXCEPTION(eslEINVAL, "invalid HMM file format code");
1016
1017 /* info necessary for sizes of things
1018 */
1019 if (fwrite((char *) &(hmm->flags), sizeof(int), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1020 if (fwrite((char *) &(hmm->M), sizeof(int), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1021 if (fwrite((char *) &(hmm->abc->type), sizeof(int), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1022
1023 /* The core model probabilities
1024 */
1025 for (k = 1; k <= hmm->M; k++) /* match emissions (0) 1..M */
1026 if (fwrite((char *) hmm->mat[k], sizeof(float), hmm->abc->K, fp) != hmm->abc->K) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1027 for (k = 0; k <= hmm->M; k++) /* insert emissions 0..M */
1028 if (fwrite((char *) hmm->ins[k], sizeof(float), hmm->abc->K, fp) != hmm->abc->K) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1029 for (k = 0; k <= hmm->M; k++) /* note: start from 0, to include B state */
1030 if (fwrite((char *) hmm->t[k], sizeof(float), 7, fp) != 7) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1031
1032 /* annotation section
1033 */
1034 if ( (status = write_bin_string(fp, hmm->name)) != eslOK) return status;
1035 if ((hmm->flags & p7H_ACC) && (status = write_bin_string(fp, hmm->acc)) != eslOK) return status;
1036 if ((hmm->flags & p7H_DESC) && (status = write_bin_string(fp, hmm->desc)) != eslOK) return status;
1037 if ((hmm->flags & p7H_RF) && (fwrite((char *) hmm->rf, sizeof(char), hmm->M+2, fp) != hmm->M+2)) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); /* +2: 1..M and trailing \0 */
1038 if ((hmm->flags & p7H_MMASK)&& (fwrite((char *) hmm->mm, sizeof(char), hmm->M+2, fp) != hmm->M+2)) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); /* +2: 1..M and trailing \0 */
1039 if ((hmm->flags & p7H_CONS) && (fwrite((char *) hmm->consensus, sizeof(char), hmm->M+2, fp) != hmm->M+2)) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed"); /* don't need to test for >=3e format; p7H_CONS flag suffices (didn't exist pre-3e) */
1040 if ((hmm->flags & p7H_CS) && (fwrite((char *) hmm->cs, sizeof(char), hmm->M+2, fp) != hmm->M+2)) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1041 if ((hmm->flags & p7H_CA) && (fwrite((char *) hmm->ca, sizeof(char), hmm->M+2, fp) != hmm->M+2)) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1042 if ( (status = write_bin_string(fp, hmm->comlog)) != eslOK) return status;
1043 if ( fwrite((char *) &(hmm->nseq), sizeof(int), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1044 if ( fwrite((char *) &(hmm->eff_nseq), sizeof(float), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1045 if (format >= p7_HMMFILE_3c && fwrite((char *) &(hmm->max_length), sizeof(int), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1046 if ( (status = write_bin_string(fp, hmm->ctime)) != eslOK) return status;
1047 if ((hmm->flags & p7H_MAP) && (fwrite((char *) hmm->map, sizeof(int), hmm->M+1, fp) != hmm->M+1)) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1048 if ( fwrite((char *)&(hmm->checksum),sizeof(uint32_t), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1049
1050 /* E-value parameters and Pfam cutoffs */
1051 if (format == p7_HMMFILE_3a)
1052 { /* reverse compatibility; 3/a format stored LAMBDA, MU, TAU */
1053 float oldparam[3];
1054 oldparam[0] = hmm->evparam[p7_MLAMBDA];
1055 oldparam[1] = hmm->evparam[p7_MMU];
1056 oldparam[2] = hmm->evparam[p7_FTAU];
1057 if (fwrite((char *) oldparam, sizeof(float), 3, fp) != 3) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1058 }
1059 else
1060 { /* default stats values */
1061 if (fwrite((char *) hmm->evparam, sizeof(float), p7_NEVPARAM, fp) != p7_NEVPARAM) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1062 }
1063 if (fwrite((char *) hmm->cutoff, sizeof(float), p7_NCUTOFFS, fp) != p7_NCUTOFFS) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1064 if ((hmm->flags & p7H_COMPO) && (fwrite((char *) hmm->compo, sizeof(float), hmm->abc->K, fp) != hmm->abc->K)) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
1065
1066 return eslOK;
1067 }
1068 /*----------------- end, save file output ----------------------*/
1069
1070
1071
1072 /*****************************************************************
1073 * 3. API for reading profile HMM files in various formats.
1074 *****************************************************************/
1075
1076 /* Function: p7_hmmfile_Read()
1077 *
1078 * Purpose: Read the next HMM from open save file <hfp>, and
1079 * optionally return this newly allocated HMM in <opt_hmm>.
1080 * (The optional return is so that an application is
1081 * only interested in whether the file contains a valid
1082 * HMM or not -- for example, to verify that a file contains
1083 * only a single HMM instead of a database of them.)
1084 *
1085 * Caller may or may not already know what alphabet the HMM
1086 * is expected to be in. A reference to the pointer to the
1087 * current alphabet is passed in <*ret_abc>. If the alphabet
1088 * is unknown, pass <*ret_abc = NULL>, and when the
1089 * new HMM is read, an appropriate new alphabet object is
1090 * allocated and passed back to the caller in <*ret_abc>.
1091 * If the alphabet is already known, <ret_abc> points to
1092 * that object ptr, and the new HMM's alphabet type is
1093 * verified to agree with it. This mechanism allows an
1094 * application to let the first HMM determine the alphabet
1095 * type for the application, while still keeping the
1096 * alphabet under the application's scope of control.
1097 *
1098 * Returns: <eslOK> on success, and the newly allocated HMM is
1099 * optionally returned via <opt_hmm>. Additionally, if
1100 * <ret_abc> pointed to <NULL>, it now points to a newly
1101 * allocated alphabet.
1102 *
1103 * Returns <eslEOF> if no HMMs remain in the file; this may
1104 * indicate success or failure, depending on what the
1105 * caller is expecting.
1106 *
1107 * Returns <eslEFORMAT> on any format problems, including
1108 * premature end of data or bad magic at the start of a
1109 * binary file. An informative error message is left in
1110 * <hfp->errbuf>; the filename (fully qualified, if opened
1111 * in a directory specified by an <env> list) is in
1112 * <hfp->fname>; and if <hfp->efp> is non-<NULL>, the HMM
1113 * file is in an ASCII text format, and the caller may also
1114 * obtain the line number at which the format error was
1115 * detected, in <hfp->efp->linenumber>, and use it to
1116 * format informative output for a user.
1117 *
1118 * Returns <eslEINCOMPAT> if the caller passed a known
1119 * alphabet (a non-<NULL> <*ret_abc>), but the alphabet
1120 * of the HMM doesn't match this expectation.
1121 *
1122 * Upon any return that is not <eslOK>, <*opt_hmm> is
1123 * <NULL> and <*ret_abc> is left unchanged from what caller
1124 * passed it as.
1125 *
1126 * Throws: <eslEMEM> upon an allocation error.
1127 * <eslESYS> on failure of other system calls, such
1128 * as file positioning functions (<fseeko()> or <ftello()>.
1129 */
1130 int
1131 p7_hmmfile_Read(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm)
1132 {
1133 /* A call to SSI to remember file position may eventually go here. */
1134 return (*hfp->parser)(hfp, ret_abc, opt_hmm);
1135 }
1136
1137
1138
1139 /* Function: p7_hmmfile_PositionByKey()
1140 * Synopsis: Use SSI to reposition file to start of named HMM.
1141 *
1142 * Purpose: Reposition <hfp> so tha next HMM we read will be the
1143 * one named (or accessioned) <key>.
1144 *
1145 * Returns: <eslOK> on success.
1146 *
1147 * Returns <eslENOTFOUND> if <key> isn't found in the index for
1148 * <hfp>.
1149 *
1150 * Returns <eslEFORMAT> is something goes wrong trying to
1151 * read the index, indicating a file format problem in the
1152 * SSI file.
1153 *
1154 * In the event of either error, the state of <hfp> is left
1155 * unchanged.
1156 *
1157 * Throws: <eslEMEM> on allocation failure, or <eslESYS> on system i/o
1158 * call failure, or <eslEINVAL> if <hfp> doesn't have an SSI
1159 * index or is not a seekable stream.
1160 */
1161 int
1162 p7_hmmfile_PositionByKey(P7_HMMFILE *hfp, const char *key)
1163 {
1164 uint16_t fh;
1165 off_t offset;
1166 int status;
1167
1168 if (hfp->ssi == NULL) ESL_EXCEPTION(eslEINVAL, "Need an open SSI index to call p7_hmmfile_PositionByKey()");
1169 if ((status = esl_ssi_FindName(hfp->ssi, key, &fh, &offset, NULL, NULL)) != eslOK) return status;
1170 if (fseeko(hfp->f, offset, SEEK_SET) != 0) ESL_EXCEPTION(eslESYS, "fseek failed");
1171
1172 hfp->newly_opened = FALSE; /* because we're poised on the magic number, and must read it */
1173 return eslOK;
1174 }
1175
1176
1177 /* Function: p7_hmmfile_Position()
1178 * Synopsis: Reposition file to start of named HMM.
1179 *
1180 * Purpose: Reposition <hfp> so tha start of the requested HMM.
1181 *
1182 * Returns: <eslOK> on success.
1183 *
1184 * In the event of either error, the state of <hfp> is left
1185 * unchanged.
1186 *
1187 * Throws: <eslESYS> on system i/o call failure, or <eslEINVAL> if
1188 * <hfp> is not a seekable stream.
1189 */
1190 int
1191 p7_hmmfile_Position(P7_HMMFILE *hfp, const off_t offset)
1192 {
1193 if (fseeko(hfp->f, offset, SEEK_SET) != 0) ESL_EXCEPTION(eslESYS, "fseek failed");
1194
1195 hfp->newly_opened = FALSE; /* because we're poised on the magic number, and must read it */
1196 return eslOK;
1197 }
1198 /*------------------- end, input API ----------------------------*/
1199
1200
1201
1202 /*****************************************************************
1203 * 4. Private, specific profile HMM file format parsers.
1204 *****************************************************************/
1205
1206 /* Parsing save files from HMMER 3.x
1207 * All parsers follow the same API.
1208 *
1209 * Returns <eslOK> on success, and if <opt_hmm> is non-NULL,
1210 * <*opt_hmm> points at a newly allocated HMM.
1211 *
1212 * Additionally, if <*ret_abc> was NULL, then a new alphabet is
1213 * allocated according to the alphabet type of this HMM, and returned
1214 * thru <ret_abc>. This allocation mechanism allows a main()
1215 * application that doesn't yet know its alphabet to determine the
1216 * alphabet when the first HMM is read, while also allowing an
1217 * application to allocate its own alphabet and assure that the
1218 * input HMMs are appropriate for that alphabet.
1219 *
1220 * Returns <eslEOF> when no HMM remains in the file, indicating a
1221 * normal end-of-file.
1222 *
1223 * Two types of "normal error" may happen, which the caller must check
1224 * for. Returns <eslEFORMAT> on any save file format error, including
1225 * bad magic (i.e. this is not a HMMER file at all). Returns
1226 * <eslEINCOMPAT> if the expected alphabet (a non-<NULL> alphabet
1227 * specified by <*ret_abc>) does not match the alphabet type of the
1228 * HMM.
1229 *
1230 * When these normal errors occur, the caller can construct its error
1231 * message from:
1232 * <hfp->errbuf>: contains an informative error message
1233 * <hfp->fname>: name of the HMM file (or '-' if STDIN)
1234 * and if <hfp->efp> is non-<NULL>, the HMM file is in ASCII text,
1235 * and the caller may also use:
1236 * <hfp->efp->linenumber>: line on which the parse error occurred.
1237 *
1238 * Throws: <eslEMEM> on allocation error.
1239 * <eslESYS> if a system i/o call fails.
1240 * In cases of error (including both thrown error and normal error), <*ret_abc>
1241 * is left in its original state as passed by the caller, and <*ret_hmm> is
1242 * returned <NULL>.
1243 */
1244 static int
1245 read_asc30hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm)
1246 {
1247 ESL_ALPHABET *abc = NULL;
1248 P7_HMM *hmm = NULL;
1249 char *tag = NULL;
1250 char *tok1 = NULL;
1251 char *tok2 = NULL;
1252 char *tok3 = NULL;
1253 char *tok4 = NULL;
1254 int alphatype;
1255 int k,x;
1256 off_t offset = 0;
1257 int status;
1258 uint32_t statstracker = 0;
1259
1260 hfp->errbuf[0] = '\0';
1261 hfp->rr_errbuf[0] = '\0';
1262
1263 if (hfp->newly_opened)
1264 {
1265 offset = 0;
1266 hfp->newly_opened = FALSE;
1267 }
1268 else
1269 {
1270 /* Record where this HMM starts on disk */
1271 if ((! hfp->do_stdin) && (! hfp->do_gzip) && (offset = ftello(hfp->f)) < 0) ESL_XEXCEPTION(eslESYS, "ftello() failed");
1272
1273 /* First line of file: "HMMER3/f". Allocate shell for HMM annotation information (we don't know K,M yet) */
1274 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) goto ERROR; /* EOF here is normal; could also be a thrown EMEM */
1275 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tag, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "unexpected absence of tokens on data line");
1276
1277 if (hfp->format == p7_HMMFILE_3f) { if (strcmp(tag, "HMMER3/f") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Didn't find HMMER3/f tag: bad format or not a HMMER save file?"); }
1278 else if (hfp->format == p7_HMMFILE_3e) { if (strcmp(tag, "HMMER3/e") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Didn't find HMMER3/e tag: bad format or not a HMMER save file?"); }
1279 else if (hfp->format == p7_HMMFILE_3d) { if (strcmp(tag, "HMMER3/d") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Didn't find HMMER3/d tag: bad format or not a HMMER save file?"); }
1280 else if (hfp->format == p7_HMMFILE_3c) { if (strcmp(tag, "HMMER3/c") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Didn't find HMMER3/c tag: bad format or not a HMMER save file?"); }
1281 else if (hfp->format == p7_HMMFILE_3b) { if (strcmp(tag, "HMMER3/b") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Didn't find HMMER3/b tag: bad format or not a HMMER save file?"); }
1282 else if (hfp->format == p7_HMMFILE_3a) { if (strcmp(tag, "HMMER3/a") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Didn't find HMMER3/a tag: bad format or not a HMMER save file?"); }
1283 else ESL_XFAIL(eslEFORMAT, hfp->errbuf, "No such HMM file format code: this shouldn't happen");
1284 }
1285
1286 if ((hmm = p7_hmm_CreateShell()) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failure, HMM shell");
1287 hmm->offset = offset;
1288
1289 /* Header section */
1290 while ((status = esl_fileparser_NextLine(hfp->efp)) == eslOK)
1291 {
1292 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tag, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of line");
1293
1294 if (strcmp(tag, "NAME") == 0) {
1295 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No name found on NAME line");
1296 p7_hmm_SetName(hmm, tok1);
1297 }
1298
1299 else if (strcmp(tag, "ACC") == 0) {
1300 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No accession found on ACC line");
1301 p7_hmm_SetAccession(hmm, tok1);
1302 }
1303
1304 else if (strcmp(tag, "DESC") == 0) {
1305 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No description found on DESC line");
1306 p7_hmm_SetDescription(hmm, tok1);
1307 }
1308
1309 else if (strcmp(tag, "LENG") == 0) {
1310 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No model length found on LENG line");
1311 if ((hmm->M = atoi(tok1)) == 0) ESL_XFAIL(status, hfp->errbuf, "Invalid model length %s on LENG line", tok1);
1312 }
1313
1314 else if (hfp->format >= p7_HMMFILE_3c && strcmp(tag, "MAXL") == 0) {
1315 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No max length found on MAXL line");
1316 if ((hmm->max_length = atoi(tok1)) == 0) ESL_XFAIL(status, hfp->errbuf, "Invalid max length %s on MAXL line", tok1);
1317 }
1318
1319 else if (strcmp(tag, "ALPH") == 0) {
1320 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No alphabet type found on ALPH");
1321 if ((alphatype = esl_abc_EncodeType(tok1)) == eslUNKNOWN) ESL_XFAIL(status, hfp->errbuf, "Unrecognized alphabet type %s", tok1);
1322 if (*ret_abc == NULL) {
1323 if ((abc = esl_alphabet_Create(alphatype)) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "Failed to create alphabet");
1324 } else {
1325 if ((*ret_abc)->type != alphatype) ESL_XFAIL(eslEINCOMPAT,hfp->errbuf,"Alphabet type mismatch: was %s, but current HMM says %s", esl_abc_DecodeType( (*ret_abc)->type), tok1);
1326 abc = *ret_abc;
1327 }
1328 }
1329
1330 else if (strcmp(tag, "RF") == 0) {
1331 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No yes/no found for RF line");
1332 if (strcasecmp(tok1, "yes") == 0)
1333 hmm->flags |= p7H_RF;
1334 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "RF header line must say yes/no, not %s", tok1);
1335 }
1336
1337 else if (strcmp(tag, "MM") == 0) {
1338 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No yes/no found for MM line");
1339 if (strcasecmp(tok1, "yes") == 0)
1340 hmm->flags |= p7H_MMASK;
1341 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "MM header line must say yes/no, not %s", tok1);
1342 }
1343
1344
1345 else if (strcmp(tag, "CONS") == 0) {
1346 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No yes/no found for CONS line");
1347 if (strcasecmp(tok1, "yes") == 0)
1348 hmm->flags |= p7H_CONS;
1349 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "CONS header line must say yes/no, not %s", tok1);
1350 }
1351
1352 else if (strcmp(tag, "CS") == 0) {
1353 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No yes/no found for CS line");
1354 if (strcasecmp(tok1, "yes") == 0)
1355 hmm->flags |= p7H_CS;
1356 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "CS header line must say yes/no, not %s", tok1);
1357 }
1358
1359 else if (strcmp(tag, "MAP") == 0) {
1360 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No yes/no found for MAP line");
1361 if (strcasecmp(tok1, "yes") == 0)
1362 hmm->flags |= p7H_MAP;
1363 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "MAP header line must say yes/no, not %s", tok1);
1364 }
1365
1366 else if (strcmp(tag, "DATE") == 0) {
1367 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No date found on DATE line");
1368 if (esl_strdup(tok1, -1, &(hmm->ctime)) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "strdup() failed to set date");
1369 }
1370
1371 else if (strcmp(tag, "COM") == 0) {
1372 /* just skip the first token; it's something like [1], numbering the command lines */
1373 if ((status = esl_fileparser_GetTokenOnLine (hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No command number on COM line");
1374 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No command on COM line");
1375 if (hmm->comlog == NULL) {
1376 if (esl_strdup(tok1, -1, &(hmm->comlog)) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "esl_strdup() failed");
1377 } else {
1378 if (esl_strcat(&(hmm->comlog), -1, "\n", -1) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "esl_strcat() failed");
1379 if (esl_strcat(&(hmm->comlog), -1, tok1, -1) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "esl_strcat() failed");
1380 }
1381 }
1382
1383 else if (strcmp(tag, "NSEQ") == 0) {
1384 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Nothing follows NSEQ tag");
1385 if ((hmm->nseq = atoi(tok1)) == 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Invalid nseq on NSEQ line: should be integer, not %s", tok1);
1386 }
1387
1388 else if (strcmp(tag, "EFFN") == 0) {
1389 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Nothing follows EFFN tag");
1390 if ((hmm->eff_nseq = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Invalid eff_nseq on EFFN line: should be a real number, not %s", tok1);
1391 }
1392
1393 else if (strcmp(tag, "CKSUM") == 0) {
1394 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Nothing follows CKSUM tag");
1395 hmm->checksum = atoll(tok1); /* if atoi(), then you may truncate uint32_t checksums > 2^31-1 */
1396 hmm->flags |= p7H_CHKSUM;
1397 }
1398
1399 else if (strcmp(tag, "STATS") == 0) {
1400 if (hfp->format >= p7_HMMFILE_3b)
1401 {
1402 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on STATS line"); /* LOCAL */
1403 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on STATS line"); /* MSV | VITERBI | FORWARD */
1404 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok3, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on STATS line"); /* mu | tau */
1405 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok4, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on STATS line"); /* lambda */
1406 if (strcasecmp(tok1, "LOCAL") == 0)
1407 {
1408 if (strcasecmp(tok2, "MSV") == 0) { hmm->evparam[p7_MMU] = atof(tok3); hmm->evparam[p7_MLAMBDA] = atof(tok4); statstracker |= 0x1; }
1409 else if (strcasecmp(tok2, "VITERBI") == 0) { hmm->evparam[p7_VMU] = atof(tok3); hmm->evparam[p7_VLAMBDA] = atof(tok4); statstracker |= 0x2; }
1410 else if (strcasecmp(tok2, "FORWARD") == 0) { hmm->evparam[p7_FTAU] = atof(tok3); hmm->evparam[p7_FLAMBDA] = atof(tok4); statstracker |= 0x4; }
1411 else ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Failed to parse STATS, %s unrecognized as field 3", tok2);
1412 } else ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Failed to parse STATS, %s unrecognized as field 2", tok1);
1413 }
1414 else if (hfp->format == p7_HMMFILE_3a) /* reverse compatibility with 30a */
1415 {
1416 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on STATS line"); /* LOCAL */
1417 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on STATS line"); /* VLAMBDA | VMU | FTAU */
1418 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok3, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on STATS line"); /* value */
1419 if (strcasecmp(tok1, "LOCAL") == 0)
1420 {
1421 if (strcasecmp(tok2, "VLAMBDA") == 0) { hmm->evparam[p7_MLAMBDA] = hmm->evparam[p7_VLAMBDA] = hmm->evparam[p7_FLAMBDA] = atof(tok3); statstracker |= 0x1; }
1422 else if (strcasecmp(tok2, "VMU") == 0) { hmm->evparam[p7_MMU] = hmm->evparam[p7_VMU] = atof(tok3); statstracker |= 0x2; }
1423 else if (strcasecmp(tok2, "FTAU") == 0) { hmm->evparam[p7_FTAU] = atof(tok3); statstracker |= 0x4; }
1424 else ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Failed to parse STATS, %s unrecognized as field 3", tok2);
1425 } else ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Failed to parse STATS, %s unrecognized as field 2", tok1);
1426 }
1427 }
1428
1429 else if (strcmp(tag, "GA") == 0) {
1430 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on GA line");
1431 hmm->cutoff[p7_GA1] = atof(tok1);
1432 if ( (abc->type == eslDNA || abc->type == eslRNA) ) { //if DNA, there's no need for a 2nd value (domain GA)
1433 hmm->cutoff[p7_GA2] = hmm->cutoff[p7_GA1];
1434 } else {
1435 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on GA line");
1436 hmm->cutoff[p7_GA2] = atof(tok2);
1437 }
1438 hmm->flags |= p7H_GA;
1439 }
1440
1441 else if (strcmp(tag, "TC") == 0) {
1442 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on TC line");
1443 hmm->cutoff[p7_TC1] = atof(tok1);
1444 if ( (abc->type == eslDNA || abc->type == eslRNA) ) { //if DNA, there's no need for a 2nd value (domain GA)
1445 hmm->cutoff[p7_TC2] = hmm->cutoff[p7_TC1];
1446 } else {
1447 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on TC line");
1448 hmm->cutoff[p7_TC2] = atof(tok2);
1449 }
1450 hmm->flags |= p7H_TC;
1451 }
1452
1453 else if (strcmp(tag, "NC") == 0) {
1454 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on NC line");
1455 hmm->cutoff[p7_NC1] = atof(tok1);
1456 if ( (abc->type == eslDNA || abc->type == eslRNA) ) { //if DNA, there's no need for a 2nd value (domain GA)
1457 hmm->cutoff[p7_NC2] = hmm->cutoff[p7_NC1];
1458 } else {
1459 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on NC line");
1460 hmm->cutoff[p7_NC2] = atof(tok2);
1461 }
1462 hmm->flags |= p7H_NC;
1463 }
1464
1465 else if (strcmp(tag, "HMM") == 0)
1466 break;
1467 } /* end, loop over possible header tags */
1468
1469 if (status != eslOK) goto ERROR;
1470
1471 /* If we saw one STATS line, we need all 3. (True for both 3/a and 3/b formats) */
1472 if (statstracker == 0x7) hmm->flags |= p7H_STATS;
1473 else if (statstracker != 0x0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Missing one or more STATS parameter lines");
1474
1475
1476 /* Skip main model header lines; allocate body of HMM now that K,M are known */
1477 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data before main model section");
1478 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data before main model section");
1479 if ((status = p7_hmm_CreateBody(hmm, hmm->M, abc)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Failed to allocate body of the new HMM");
1480 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data before main model section");
1481
1482 /* Optional model composition (filter null model) may immediately follow headers */
1483 if (strcmp(tok1, "COMPO") == 0) {
1484 for (x = 0; x < abc->K; x++) {
1485 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on COMPO line");
1486 hmm->compo[x] = (*tok1 == '*' ? 0.0 : expf(-1.0 * atof(tok1)));
1487 }
1488 hmm->flags |= p7H_COMPO;
1489 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data after COMPO line");
1490 if ((esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data after COMPO line");
1491 }
1492
1493 /* First two lines are node 0: insert emissions, then transitions from node 0 (begin) */
1494
1495 hmm->ins[0][0] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
1496 for (x = 1; x < abc->K; x++) {
1497 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on insert line, node 0: expected %d, got %d\n", abc->K, x);
1498 hmm->ins[0][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
1499 }
1500 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model: no node 0 transition line");
1501 for (x = 0; x < p7H_NTRANSITIONS; x++) {
1502 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on begin (0) transition line");
1503 hmm->t[0][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
1504 }
1505
1506 /* The main model section. */
1507 for (k = 1; k <= hmm->M; k++)
1508 {
1509
1510 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model section, at node %d (expected %d)", k, hmm->M);
1511 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model section, at node %d (expected %d)", k, hmm->M);
1512 if (atoi(tok1) != k) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Expected match line to start with %d (of %d); saw %s", k, hmm->M, tok1);
1513
1514 for (x = 0; x < abc->K; x++) {
1515 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few probability fields on match line, node %d: expected %d, got %d\n", k, abc->K, x);
1516 hmm->mat[k][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
1517 }
1518
1519 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Missing MAP field on match line for node %d: should at least be -", k);
1520 if (hmm->flags & p7H_MAP) hmm->map[k] = atoi(tok1);
1521
1522 if (hfp->format >= p7_HMMFILE_3e) {
1523 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Missing CONS field on match line for node %d: should at least be -", k);
1524 if (hmm->flags & p7H_CONS) hmm->consensus[k] = *tok1;
1525 }
1526 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Missing RF field on match line for node %d: should at least be -", k);
1527 if (hmm->flags & p7H_RF) hmm->rf[k] = *tok1;
1528
1529 if (hfp->format >= p7_HMMFILE_3f) {
1530 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Missing MM field on match line for node %d: should at least be -", k);
1531 if (hmm->flags & p7H_MMASK) hmm->mm[k] = *tok1;
1532 }
1533
1534
1535 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Missing CS field on match line for node %d: should at least be -", k);
1536 if (hmm->flags & p7H_CS) hmm->cs[k] = *tok1;
1537
1538
1539 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model: no insert emission line, node %d", k);
1540 for (x = 0; x < abc->K; x++) {
1541 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few probability fields on insert line, node %d: expected %d, got %d\n", k, abc->K, x);
1542 hmm->ins[k][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
1543 }
1544 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model: no transition line, node %d", k);
1545 for (x = 0; x < p7H_NTRANSITIONS; x++) {
1546 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few probability fields on transition line, node %d: expected %d, got %d\n", k, abc->K, x);
1547 hmm->t[k][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
1548 }
1549 }
1550
1551 /* The closing // */
1552 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data: missing //?");
1553 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data: missing //?");
1554 if (strcmp(tok1, "//") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Expected closing //; found %s instead", tok1);
1555
1556 /* legacy issues */
1557 if (hfp->format < p7_HMMFILE_3e && (status = p7_hmm_SetConsensus(hmm, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Failed to set consensus on legacy HMM format");
1558
1559 /* Finish up. */
1560 if (hmm->flags & p7H_RF) { hmm->rf[0] = ' '; hmm->rf[hmm->M+1] = '\0'; }
1561 if (hmm->flags & p7H_MMASK){ hmm->mm[0] = ' '; hmm->mm[hmm->M+1] = '\0'; }
1562 if (hmm->flags & p7H_CONS) { hmm->consensus[0] = ' '; hmm->consensus[hmm->M+1] = '\0'; }
1563 if (hmm->flags & p7H_CS) { hmm->cs[0] = ' '; hmm->cs[hmm->M+1] = '\0'; }
1564 if (hmm->flags & p7H_MAP) { hmm->map[0] = 0; }
1565 if (hmm->name == NULL) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "No NAME found for HMM");
1566 if (hmm->M <= 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "No LENG found for HMM (or LENG <= 0)");
1567 if (abc == NULL) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "No ALPH found for HMM");
1568
1569 if (*ret_abc == NULL) *ret_abc = abc;
1570 if ( opt_hmm != NULL) *opt_hmm = hmm; else p7_hmm_Destroy(hmm);
1571 return eslOK;
1572
1573 ERROR:
1574 if (*ret_abc == NULL && abc != NULL) esl_alphabet_Destroy(abc);
1575 if (hmm != NULL) p7_hmm_Destroy(hmm);
1576 if (opt_hmm != NULL) *opt_hmm = NULL;
1577 if (status == eslEMEM || status == eslESYS) return status;
1578 else if (status == eslEOF) return status;
1579 else if (status == eslEINCOMPAT) return status;
1580 else return eslEFORMAT; /* anything else is a format error: includes premature EOF, EOL, EOD */
1581 }
1582
1583
1584 static int
1585 read_bin30hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm)
1586 {
1587 ESL_ALPHABET *abc = NULL;
1588 P7_HMM *hmm = NULL;
1589 uint32_t magic;
1590 int alphabet_type;
1591 int k;
1592 off_t offset = 0;
1593 int status;
1594
1595 hfp->errbuf[0] = '\0';
1596 hfp->rr_errbuf[0] = '\0';
1597 if (feof(hfp->f)) { status = eslEOF; goto ERROR; }
1598
1599 if (hfp->newly_opened)
1600 {
1601 offset = 0;
1602 hfp->newly_opened = FALSE;
1603 }
1604 else
1605 { /* Check magic. */
1606 if ((!hfp->do_stdin) && (! hfp->do_gzip)) {
1607 if ((offset = ftello(hfp->f)) < 0) ESL_XEXCEPTION(eslESYS, "ftello() failed");
1608 }
1609 if (! fread((char *) &magic, sizeof(uint32_t), 1, hfp->f)) { status = eslEOF; goto ERROR; }
1610
1611 if (hfp->format == p7_HMMFILE_3f) { if (magic != v3f_magic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad magic number at start of HMM"); }
1612 else if (hfp->format == p7_HMMFILE_3e) { if (magic != v3e_magic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad magic number at start of HMM"); }
1613 else if (hfp->format == p7_HMMFILE_3d) { if (magic != v3d_magic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad magic number at start of HMM"); }
1614 else if (hfp->format == p7_HMMFILE_3c) { if (magic != v3c_magic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad magic number at start of HMM"); }
1615 else if (hfp->format == p7_HMMFILE_3b) { if (magic != v3b_magic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad magic number at start of HMM"); }
1616 else if (hfp->format == p7_HMMFILE_3a) { if (magic != v3a_magic) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "bad magic number at start of HMM"); }
1617 else ESL_XFAIL(eslEFORMAT, hfp->errbuf, "no such HMM file format code");
1618 }
1619
1620 /* Allocate shell of the new HMM.
1621 * Two-step allocation lets us read/set the flags first;
1622 * then the later CreateBody() call will allocate optional internal fields we need.
1623 */
1624 if ((hmm = p7_hmm_CreateShell()) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failed, HMM shell");
1625 hmm->offset = offset;
1626
1627 /* Get sizes of things */
1628 /* xref J5/114 for a legacy use of <flags> for optional acc, desc annotation */
1629 if (! fread((char *) &(hmm->flags), sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read flags");
1630 if (! fread((char *) &(hmm->M), sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read model size M");
1631 if (! fread((char *) &alphabet_type, sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read alphabet_type");
1632
1633 /* Set or verify alphabet. */
1634 if (*ret_abc == NULL) { /* still unknown: set it, pass control of it back to caller */
1635 if ((abc = esl_alphabet_Create(alphabet_type)) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failed, alphabet");
1636 } else { /* already known: check it */
1637 abc = *ret_abc;
1638 if (abc->type != alphabet_type) ESL_XFAIL(eslEINCOMPAT, hfp->errbuf, "Alphabet type mismatch: was %s, but current HMM says %s", esl_abc_DecodeType( abc->type), esl_abc_DecodeType(alphabet_type));
1639 }
1640
1641 /* Finish the allocation of the HMM
1642 */
1643 if ((status = p7_hmm_CreateBody(hmm, hmm->M, abc)) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failed, HMM body");
1644
1645 /* Core model probabilities. */
1646 for (k = 1; k <= hmm->M; k++)
1647 if (! fread((char *) hmm->mat[k], sizeof(float), hmm->abc->K, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read mat[%d]", k);
1648 for (k = 0; k <= hmm->M; k++)
1649 if (! fread((char *) hmm->ins[k], sizeof(float), hmm->abc->K, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read ins[%d]", k);
1650 for (k = 0; k <= hmm->M; k++)
1651 if (! fread((char *) hmm->t[k], sizeof(float), p7H_NTRANSITIONS, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read t[%d]", k);
1652
1653 /* Annotations. */
1654 if (read_bin_string(hfp->f, &(hmm->name)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read name");
1655 if ((hmm->flags & p7H_ACC) && read_bin_string(hfp->f, &(hmm->acc)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read acc");
1656 if ((hmm->flags & p7H_DESC) && read_bin_string(hfp->f, &(hmm->desc)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read desc");
1657 if ((hmm->flags & p7H_RF) && ! fread((char *) hmm->rf, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read rf"); /* +2: 1..M and trailing \0 */
1658 if ((hmm->flags & p7H_MMASK)&& ! fread((char *) hmm->mm, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read mm"); /* +2: 1..M and trailing \0 */
1659 if ((hmm->flags & p7H_CONS) && ! fread((char *) hmm->consensus, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read consensus"); /* don't need to test for >=3e format, because the flag is sufficient (didn't exist pre-3e) */
1660 if ((hmm->flags & p7H_CS) && ! fread((char *) hmm->cs, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read cs");
1661 if ((hmm->flags & p7H_CA) && ! fread((char *) hmm->ca, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read ca");
1662 if (read_bin_string(hfp->f, &(hmm->comlog)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read comlog");
1663 if (! fread((char *) &(hmm->nseq), sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read nseq");
1664 if (! fread((char *) &(hmm->eff_nseq), sizeof(float), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read eff_nseq");
1665 if (hfp->format >= p7_HMMFILE_3c) {
1666 if (! fread((char *) &(hmm->max_length), sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read max_length");
1667 }
1668 if (read_bin_string(hfp->f, &(hmm->ctime)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read ctime");
1669 if ((hmm->flags & p7H_MAP) && ! fread((char *) hmm->map, sizeof(int), hmm->M+1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read map");
1670 if (! fread((char *) &(hmm->checksum), sizeof(uint32_t),1,hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read checksum");
1671
1672 /* E-value parameters and Pfam cutoffs */
1673 if (hfp->format >= p7_HMMFILE_3b) {
1674 if (! fread((char *) hmm->evparam, sizeof(float), p7_NEVPARAM, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read statistical params");
1675 } else if (hfp->format == p7_HMMFILE_3a) {
1676 /* a backward compatibility mode. 3/a files stored 3 floats: LAMBDA, MU, TAU. Read 3 #'s and carefully copy/rearrange them into new 6 format */
1677 if (! fread((char *) hmm->evparam, sizeof(float), 3, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read statistical params");
1678 hmm->evparam[p7_FLAMBDA] = hmm->evparam[0];
1679 hmm->evparam[p7_FTAU] = hmm->evparam[2];
1680 hmm->evparam[p7_VLAMBDA] = hmm->evparam[0];
1681 hmm->evparam[p7_VMU] = hmm->evparam[1];
1682 hmm->evparam[p7_MLAMBDA] = hmm->evparam[p7_VLAMBDA];
1683 hmm->evparam[p7_MMU] = hmm->evparam[p7_VMU];
1684 }
1685 if (! fread((char *) hmm->cutoff, sizeof(float), p7_NCUTOFFS, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read Pfam score cutoffs");
1686 if ((hmm->flags & p7H_COMPO) && ! fread((char *) hmm->compo, sizeof(float), hmm->abc->K, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "failed to read model composition");
1687
1688 /* other legacy issues */
1689 if (hfp->format < p7_HMMFILE_3e && (status = p7_hmm_SetConsensus(hmm, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Failed to set consensus on legacy HMM format");
1690
1691 if (*ret_abc == NULL) *ret_abc = abc; /* pass our new alphabet back to caller, if caller didn't know it already */
1692 if ( opt_hmm != NULL) *opt_hmm = hmm; else p7_hmm_Destroy(hmm);
1693 return eslOK;
1694
1695 ERROR:
1696 if (*ret_abc == NULL && abc != NULL) esl_alphabet_Destroy(abc); /* the test is for an alphabet created here, not passed */
1697 if (hmm != NULL) p7_hmm_Destroy(hmm);
1698 if (opt_hmm != NULL) *opt_hmm = NULL;
1699 return status;
1700 }
1701
1702 /* read_asc20hmm()
1703 * Read a HMMER2.0 ASCII format HMM file, for backward compatibility
1704 * SRE, Thu Dec 25 09:13:36 2008 [Magallon]
1705 */
1706 static int
1707 read_asc20hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm)
1708 {
1709 ESL_ALPHABET *abc = NULL;
1710 P7_HMM *hmm = NULL;
1711 P7_BG *bg = NULL;
1712 char *tag = NULL;
1713 char *tok1 = NULL;
1714 char *tok2 = NULL;
1715 char *tok3 = NULL;
1716 float null[p7_MAXABET];
1717 int alphatype;
1718 int k,x;
1719 off_t offset = 0;
1720 int status;
1721
1722 hfp->errbuf[0] = '\0';
1723 hfp->rr_errbuf[0] = '\0';
1724
1725 if (hfp->newly_opened)
1726 {
1727 offset = 0;
1728 hfp->newly_opened = FALSE;
1729 }
1730 else
1731 {
1732 /* Record where this HMM starts on disk */
1733 if ((! hfp->do_stdin) && (! hfp->do_gzip) && (offset = ftello(hfp->f)) < 0) ESL_XEXCEPTION(eslESYS, "ftello() failed");
1734
1735 /* First line of file: "HMMER2.0". Allocate shell for HMM annotation information (we don't know K,M yet) */
1736 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) goto ERROR; /* EOF here is normal; could also be a thrown EMEM */
1737 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tag, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "unexpected absence of tokens on data line");
1738 if (strcmp(tag, "HMMER2.0") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Didn't find HMMER3/a tag: not a HMMER save file?");
1739 }
1740
1741 if ((hmm = p7_hmm_CreateShell()) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "allocation failure, HMM shell");
1742 hmm->offset = offset;
1743
1744 /* Header */
1745 /* H2 save files have no EFFN;
1746 * COM lines don't have number tags like [1];
1747 * they have CKSUM but we ignore it because it uses different algorithm;
1748 * have EVD line, we ignore it, H3 stats are different;
1749 * XT, NULT lines are ignored; algorithm-dependent config is all internal in H3
1750 */
1751 while ((status = esl_fileparser_NextLine(hfp->efp)) == eslOK)
1752 {
1753 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tag, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of line");
1754
1755 if (strcmp(tag, "NAME") == 0) {
1756 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No name found on NAME line");
1757 p7_hmm_SetName(hmm, tok1);
1758 }
1759
1760 else if (strcmp(tag, "ACC") == 0) {
1761 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No accession found on ACC line");
1762 p7_hmm_SetAccession(hmm, tok1);
1763 }
1764
1765 else if (strcmp(tag, "DESC") == 0) {
1766 /* #h106. Allow "DESC" bare, with nothing following. Looks like some SMART models circa 1998 are like this. */
1767 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) == eslOK) p7_hmm_SetDescription(hmm, tok1);
1768 }
1769
1770 else if (strcmp(tag, "LENG") == 0) {
1771 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No model length found on LENG line");
1772 if ((hmm->M = atoi(tok1)) == 0) ESL_XFAIL(status, hfp->errbuf, "Invalid model length %s on LENG line", tok1);
1773 }
1774
1775 else if (strcmp(tag, "ALPH") == 0) {
1776 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No alphabet type found on ALPH");
1777 /* Bug #h80: H2 tags DNA/RNA files as "Nucleic"; modern Easel/H3
1778 * expects tag "DNA" or "RNA", so you can't pass tok1 to esl_abc_EncodeType().
1779 */
1780 if (strcasecmp(tok1, "nucleic") == 0) alphatype = eslDNA;
1781 else if (strcasecmp(tok1, "amino") == 0) alphatype = eslAMINO;
1782 else ESL_XFAIL(status, hfp->errbuf, "Unrecognized alphabet type %s", tok1);
1783
1784 if (*ret_abc == NULL) {
1785 if ((abc = esl_alphabet_Create(alphatype)) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "Failed to create alphabet");
1786 } else {
1787 if ((*ret_abc)->type != alphatype) ESL_XFAIL(eslEINCOMPAT,hfp->errbuf,"Alphabet type mismatch: was %s, but current HMM says %s", esl_abc_DecodeType( (*ret_abc)->type), tok1);
1788 abc = *ret_abc;
1789 }
1790 }
1791
1792 else if (strcmp(tag, "RF") == 0) {
1793 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No yes/no found for RF line");
1794 if (strcasecmp(tok1, "yes") == 0)
1795 hmm->flags |= p7H_RF;
1796 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "RF header line must say yes/no, not %s", tok1);
1797 }
1798
1799 else if (strcmp(tag, "CS") == 0) {
1800 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No yes/no found for CS line");
1801 if (strcasecmp(tok1, "yes") == 0)
1802 hmm->flags |= p7H_CS;
1803 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "CS header line must say yes/no, not %s", tok1);
1804 }
1805
1806 else if (strcmp(tag, "MAP") == 0) {
1807 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No yes/no found for MAP line");
1808 if (strcasecmp(tok1, "yes") == 0)
1809 hmm->flags |= p7H_MAP;
1810 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "MAP header line must say yes/no, not %s", tok1);
1811 }
1812
1813 else if (strcmp(tag, "DATE") == 0) {
1814 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No date found on DATE line");
1815 if (esl_strdup(tok1, -1, &(hmm->ctime)) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "strdup() failed to set date");
1816 }
1817
1818 else if (strcmp(tag, "COM") == 0) {
1819 /* in an H2 save file, there's no [1] number tags. The H3 format parser skips these */
1820 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "No command on COM line");
1821 if (hmm->comlog == NULL) {
1822 if (esl_strdup(tok1, -1, &(hmm->comlog)) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "esl_strdup() failed");
1823 } else {
1824 if (esl_strcat(&(hmm->comlog), -1, "\n", -1) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "esl_strcat() failed");
1825 if (esl_strcat(&(hmm->comlog), -1, tok1, -1) != eslOK) ESL_XFAIL(eslEMEM, hfp->errbuf, "esl_strcat() failed");
1826 }
1827 }
1828
1829 else if (strcmp(tag, "NSEQ") == 0) {
1830 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Nothing follows NSEQ tag");
1831 if ((hmm->nseq = atoi(tok1)) == 0 && strcmp(tok1, "0") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Invalid nseq on NSEQ line: should be integer, not %s", tok1);
1832 }
1833
1834 else if (strcmp(tag, "GA") == 0) {
1835 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on GA line");
1836 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on GA line");
1837 hmm->cutoff[p7_GA1] = atof(tok1);
1838 hmm->cutoff[p7_GA2] = atof(tok2);
1839 hmm->flags |= p7H_GA;
1840 }
1841
1842 else if (strcmp(tag, "TC") == 0) {
1843 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on TC line");
1844 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on TC line");
1845 hmm->cutoff[p7_TC1] = atof(tok1);
1846 hmm->cutoff[p7_TC2] = atof(tok2);
1847 hmm->flags |= p7H_TC;
1848 }
1849
1850 else if (strcmp(tag, "NC") == 0) {
1851 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on NC line");
1852 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on NC line");
1853 hmm->cutoff[p7_NC1] = atof(tok1);
1854 hmm->cutoff[p7_NC2] = atof(tok2);
1855 hmm->flags |= p7H_NC;
1856 }
1857
1858 else if (strcmp(tag, "NULE") == 0) {
1859 if (abc->type == eslUNKNOWN) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "ALPH must precede NULE in HMMER2 save files");
1860 for (x = 0; x < abc->K; x++) {
1861 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few fields on NULE line");
1862 null[x] = h2ascii2prob(tok1, 1./(float)abc->K);
1863 }
1864 }
1865
1866 else if (strcmp(tag, "HMM") == 0)
1867 break;
1868 } /* end, loop over possible header tags */
1869 if (status != eslOK) goto ERROR;
1870
1871
1872 /* Skip main model header lines; allocate body of HMM now that K,M are known */
1873 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data before main model section");
1874 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data before main model section");
1875 if ((status = p7_hmm_CreateBody(hmm, hmm->M, abc)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Failed to allocate body of the new HMM");
1876 if (( bg = p7_bg_Create(abc)) == NULL) ESL_XFAIL(eslEMEM, hfp->errbuf, "failed to create background model");
1877
1878 /* H2's tbd1 line ==> translated to H3's node 0 */
1879 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data before main model section");
1880 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data before main model section");
1881 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok3, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data before main model section");
1882 hmm->t[0][p7H_MM] = h2ascii2prob(tok1, 1.0); /* B->M1 */
1883 hmm->t[0][p7H_MI] = 0.0; /* B->I0 */
1884 hmm->t[0][p7H_MD] = h2ascii2prob(tok3, 1.0); /* B->D1 */
1885 hmm->t[0][p7H_IM] = 1.0;
1886 hmm->t[0][p7H_II] = 0.0;
1887 hmm->t[0][p7H_DM] = 1.0;
1888 hmm->t[0][p7H_DD] = 0.0;
1889 for (x = 0; x < abc->K; x++) hmm->ins[0][x] = bg->f[x];
1890
1891 /* The main model section. */
1892 for (k = 1; k <= hmm->M; k++)
1893 {
1894 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model section, at node %d (expected %d)", k, hmm->M);
1895 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model section, at node %d (expected %d)", k, hmm->M);
1896 if (atoi(tok1) != k) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Expected match line to start with %d (of %d); saw %s", k, hmm->M, tok1);
1897
1898 /* Line 1: match emissions; optional map info */
1899 for (x = 0; x < abc->K; x++) {
1900 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few probability fields on match line, node %d: expected %d, got %d\n", k, abc->K, x);
1901 hmm->mat[k][x] = h2ascii2prob(tok1, null[x]);
1902 }
1903 if (hmm->flags & p7H_MAP) {
1904 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Missing MAP field on match line for node %d: should at least be -", k);
1905 hmm->map[k] = atoi(tok1);
1906 }
1907
1908 /* Line 2: optional RF; then we ignore insert emissions */
1909 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model: no insert emission line, node %d", k);
1910 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Missing RF field on match line for node %d: should at least be -", k);
1911 if (hmm->flags & p7H_RF) hmm->rf[k] = *tok1;
1912 for (x = 0; x < abc->K; x++) hmm->ins[k][x] = bg->f[x];
1913
1914 /* Line 3: optional CS, then transitions (ignoring last 2, which are entry/exit */
1915 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data in main model: no transition line, node %d", k);
1916 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Missing CS field on match line for node %d: should at least be -", k);
1917 if (hmm->flags & p7H_CS) hmm->cs[k] = *tok1;
1918 if (k < hmm->M) { /* ignore last insert transition line; H3/H2 not compatible there */
1919 for (x = 0; x < p7H_NTRANSITIONS; x++) {
1920 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Too few probability fields on transition line, node %d: expected %d, got %d\n", k, abc->K, x);
1921 hmm->t[k][x] = h2ascii2prob(tok1, 1.0);
1922 }
1923 }
1924 }
1925
1926 /* node M transitions: H2 doesn't have an I_M state */
1927 hmm->t[hmm->M][p7H_MM] = 1.0;
1928 hmm->t[hmm->M][p7H_MI] = 0.0;
1929 hmm->t[hmm->M][p7H_MD] = 0.0;
1930 hmm->t[hmm->M][p7H_IM] = 1.0;
1931 hmm->t[hmm->M][p7H_II] = 0.0;
1932 hmm->t[hmm->M][p7H_DM] = 1.0;
1933 hmm->t[hmm->M][p7H_DD] = 0.0;
1934
1935 /* The closing // */
1936 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data: missing //?");
1937 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Premature end of data: missing //?");
1938 if (strcmp(tok1, "//") != 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "Expected closing //; found %s instead", tok1);
1939
1940 /* Tidy up. */
1941 if (hmm->flags & p7H_RF) { hmm->rf[0] = ' '; hmm->rf[hmm->M+1] = '\0'; }
1942 if (hmm->flags & p7H_CS) { hmm->cs[0] = ' '; hmm->cs[hmm->M+1] = '\0'; }
1943 if (hmm->name == NULL) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "No NAME found for HMM");
1944 if (hmm->M <= 0) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "No LENG found for HMM (or LENG <= 0)");
1945 if (abc == NULL) ESL_XFAIL(eslEFORMAT, hfp->errbuf, "No ALPH found for HMM");
1946
1947 /* Part of #h106 fix: */
1948 if ((status = p7_hmm_Renormalize(hmm)) != eslOK) return status;
1949
1950 /* legacy issues */
1951 if (( status = p7_hmm_SetConsensus(hmm, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Failed to create consensus line");
1952
1953 /* Calibrate the model: cfg rng bg gm om */
1954 if ((status = p7_Calibrate(hmm, NULL, NULL, &bg, NULL, NULL)) != eslOK) ESL_XFAIL(status, hfp->errbuf, "Failed to calibrate HMMER2 model after input conversion");
1955
1956 if (*ret_abc == NULL) *ret_abc = abc;
1957 if ( opt_hmm != NULL) *opt_hmm = hmm; else p7_hmm_Destroy(hmm);
1958 p7_bg_Destroy(bg);
1959 return eslOK;
1960
1961 ERROR:
1962 if (*ret_abc == NULL && abc != NULL) esl_alphabet_Destroy(abc);
1963 if (hmm != NULL) p7_hmm_Destroy(hmm);
1964 if (bg != NULL) p7_bg_Destroy(bg);
1965 if (opt_hmm != NULL) *opt_hmm = NULL;
1966 if (status == eslEMEM || status == eslESYS) return status;
1967 else if (status == eslEOF) return status;
1968 else if (status == eslEINCOMPAT) return status;
1969 else return eslEFORMAT; /* anything else is a format error: includes premature EOF, EOL, EOD */
1970 }
1971 /*--------------- end, private format parsers -------------------*/
1972
1973
1974
1975
1976
1977 /*****************************************************************
1978 * 5. Other private functions involved in i/o
1979 *****************************************************************/
1980
1981
1982 /* multiline()
1983 *
1984 * Used to print the command log to ASCII save files.
1985 *
1986 * Given a record (like the comlog) that contains
1987 * multiple lines, print it as multiple lines with
1988 * a given prefix. e.g.:
1989 *
1990 * given: "COM ", "foo\nbar\nbaz"
1991 * print: COM 1 foo
1992 * COM 2 bar
1993 * COM 3 baz
1994 *
1995 * If <s> is NULL, no-op. Otherwise <s> must be a <NUL>-terminated
1996 * string. It does not matter if it ends in <\n> or not. <pfx>
1997 * must be a valid <NUL>-terminated string; it may be empty.
1998 *
1999 * Args: fp: FILE to print to
2000 * pfx: prefix for each line
2001 * s: line to break up and print; tolerates a NULL
2002 *
2003 * Returns: <eslOK> on success.
2004 *
2005 * Throws: <eslEWRITE> on write error.
2006 */
2007 static int
2008 multiline(FILE *fp, const char *pfx, char *s)
2009 {
2010 char *sptr = s;
2011 char *end = NULL;
2012 int n = 0;
2013 int nline = 1;
2014
2015 do {
2016 end = strchr(sptr, '\n');
2017
2018 if (end != NULL) /* if there's no \n left, end == NULL */
2019 {
2020 n = end - sptr; /* n chars exclusive of \n */
2021 if (fprintf(fp, "%s [%d] ", pfx, nline++) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
2022 if (fwrite(sptr, sizeof(char), n, fp) != n) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); /* using fwrite lets us write fixed # of chars */
2023 if (fprintf(fp, "\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); /* while writing \n w/ printf allows newline conversion */
2024 sptr += n + 1; /* +1 to get past \n */
2025 }
2026 else
2027 {
2028 if (fprintf(fp, "%s [%d] %s\n", pfx, nline++, sptr) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); /* last line */
2029 }
2030 } while (end != NULL && *sptr != '\0'); /* *sptr == 0 if <s> terminates with a \n */
2031 return eslOK;
2032 }
2033
2034 /* multilineString()
2035 *
2036 * Used to print the command log to a string.
2037 *
2038 * Given a record (like the comlog) that contains
2039 * multiple lines, print it as multiple lines with
2040 * a given prefix. e.g.:
2041 *
2042 * given: "COM ", "foo\nbar\nbaz"
2043 * print: COM 1 foo
2044 * COM 2 bar
2045 * COM 3 baz
2046 *
2047 * If <s> is NULL, no-op. Otherwise <s> must be a <NUL>-terminated
2048 * string. It does not matter if it ends in <\n> or not. <pfx>
2049 * must be a valid <NUL>-terminated string; it may be empty.
2050 *
2051 * Args: str: string we're writing/appending to
2052 * size: original allocated length of <str>, total. (We're at position <*coffset> in the whole thing.)
2053 * pfx: prefix for each line we add to <str>
2054 * s: the line(s) to break up and add to <str> (can be NULL)
2055 * coffset: the current write position in the string (pointer so we can add to it; will be updated upon return)
2056 *
2057 * Returns: <eslOK> on success or <eslEWRITE> on error.
2058 *
2059 */
2060 static int
2061 multilineString(char *str, int size, const char *pfx, char *s, int *coffset)
2062 {
2063 char *sptr = s;
2064 char *end = NULL;
2065 int n = 0;
2066 int nline = 1;
2067 int offset;
2068
2069 do {
2070 end = strchr(sptr, '\n');
2071 if (end != NULL) { /* if there's no \n left, end == NULL */
2072 n = end - sptr; /* n chars exclusive of \n */
2073 if ((offset = snprintf(str + *coffset, size-*coffset, "%s [%d] ", pfx, nline++)) < 0) return eslEWRITE;
2074 *coffset += offset;
2075
2076 strncpy(str + *coffset, sptr, sizeof(char) * n); /* using strncpy lets us write fixed # of chars */
2077 *coffset +=n;
2078 if ((offset = snprintf(str + *coffset, size-*coffset, "\n")) < 0) return eslEWRITE;
2079 sptr += n + 1; /* +1 to get past \n */
2080 } else {
2081 if ((offset = snprintf(str + *coffset, size-*coffset, "%s [%d] %s\n", pfx, nline++, sptr)) < 0) return eslEWRITE;
2082 *coffset += offset;
2083 }
2084 } while (end != NULL && *sptr != '\0'); /* *sptr == 0 if <s> terminates with a \n */
2085 return eslOK;
2086 }
2087
2088
2089
2090 static int
2091 printprob(FILE *fp, int fieldwidth, float p)
2092 {
2093 if (p == 0.0) { if (fprintf(fp, " %*s", fieldwidth, "*") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
2094 else if (p == 1.0) { if (fprintf(fp, " %*.5f", fieldwidth, 0.0) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
2095 else { if (fprintf(fp, " %*.5f", fieldwidth, -logf(p)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); }
2096 return eslOK;
2097 }
2098
2099 /* probToString
2100 *
2101 * Used to print probabilities floats in a fixed field to a growing string. Based on printprob.
2102 *
2103 *
2104 * given : 4.115212345633
2105 * append: " 4.11521"
2106 *
2107 *
2108 * If p is 0.0 or 1.0, append * or 0.00000
2109 *
2110 * Args: str: string we're appending to
2111 * size: original (total) allocation of <str>. (We're currently at position <offset> in it.)
2112 * fieldwidth: The size of the number to be printed. Note, a space is prepended
2113 * p: float
2114 * offset: current position in str. We don't need to update offset, because we know to update it by +(fieldwidth+1).
2115 *
2116 * Returns: <eslOK> on success or <eslEWRITE> on error.
2117 */
2118 static int
2119 probToString(char *str, int size, int fieldwidth, float p, int offset)
2120 {
2121 if (p == 0.0) { if (snprintf(str+offset, size-offset, " %*s", fieldwidth, "*") < 0) return eslEWRITE; }
2122 else if (p == 1.0) { if (snprintf(str+offset, size-offset, " %*.5f", fieldwidth, 0.0) < 0) return eslEWRITE; }
2123 else { if (snprintf(str+offset, size-offset, " %*.5f", fieldwidth, -logf(p)) < 0) return eslEWRITE; }
2124 return eslOK;
2125 }
2126
2127 /* Function: write_bin_string()
2128 *
2129 * Purpose: Write a string in binary save format: an integer
2130 * for the string length (including \0), followed by
2131 * the string.
2132 *
2133 * Return: <eslOK> on success;
2134 *
2135 * Throw: <eslEWRITE> on write error, such as a filled disk.
2136 */
2137 static int
2138 write_bin_string(FILE *fp, char *s)
2139 {
2140 int len;
2141 if (s != NULL)
2142 {
2143 len = strlen(s) + 1;
2144 if (fwrite((char *) &len, sizeof(int), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
2145 if (fwrite((char *) s, sizeof(char), len, fp) != len) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
2146 }
2147 else
2148 {
2149 len = 0;
2150 if (fwrite((char *) &len, sizeof(int), 1, fp) != 1) ESL_EXCEPTION_SYS(eslEWRITE, "hmm binary write failed");
2151 }
2152 return eslOK;
2153 }
2154
2155 /* Function: read_bin_string()
2156 *
2157 * Purpose: Read in a string from a binary file, where
2158 * the first integer is the length (including '\0').
2159 * If the length is 0, <*ret_s> is set to <NULL>.
2160 *
2161 * This is a reasonable convention for storing/ reading
2162 * strings in binary files. Note that because the length is
2163 * inclusive of '\0', there's a difference between a NULL
2164 * string and an empty string.
2165 *
2166 * Args: fp - FILE to read from
2167 * ret_s - string to read into
2168 *
2169 * Return: <eslOK> on success. ret_s is malloc'ed here.
2170 * <eslEOD> if a read fails - likely because no more
2171 * data in file.
2172 *
2173 * Throws <eslEMEM> on allocation error.
2174 */
2175 static int
2176 read_bin_string(FILE *fp, char **ret_s)
2177 {
2178 int status;
2179 char *s = NULL;
2180 int len;
2181
2182 if (! fread((char *) &len, sizeof(int), 1, fp)) { status = eslEOD; goto ERROR; }
2183 if (len > 0) {
2184 ESL_ALLOC(s, (sizeof(char) * len));
2185 if (! fread((char *) s, sizeof(char), len, fp)) { status = eslEOD; goto ERROR; }
2186 }
2187 *ret_s = s;
2188 return eslOK;
2189
2190 ERROR:
2191 if (s != NULL) free(s);
2192 *ret_s = NULL;
2193 return status;
2194 }
2195
2196 static float
2197 h2ascii2prob(char *s, float null)
2198 {
2199 return ((*s == '*') ? 0. : null * exp( atoi(s) * 0.00069314718));
2200 }
2201 /*---------------- end, private utilities -----------------------*/
2202
2203
2204
2205 /*****************************************************************
2206 * 6. Benchmark driver.
2207 *****************************************************************/
2208 #ifdef p7HMMFILE_BENCHMARK
2209 /*
2210 icc -O3 -static -o p7_hmmfile_benchmark -I. -L. -I../easel -L../easel -Dp7HMMFILE_BENCHMARK p7_hmmfile.c -lhmmer -leasel -lm
2211 ./p7_hmmfile_benchmark Pfam.hmm
2212 */
2213 #include <p7_config.h>
2214
2215 #include <stdlib.h>
2216 #include <stdio.h>
2217
2218 #include "easel.h"
2219 #include "esl_getopts.h"
2220 #include "esl_stopwatch.h"
2221
2222 #include "hmmer.h"
2223
2224 static ESL_OPTIONS options[] = {
2225 /* name type default env range toggles reqs incomp help docgroup*/
2226 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
2227 { "-a", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "include time of profile configuration", 0 },
2228 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "verbose: print model info as they're read", 0 },
2229 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
2230 };
2231 static char usage[] = "[-options] <HMM file>";
2232 static char banner[] = "benchmark driver for HMM input";
2233
2234 int
2235 main(int argc, char **argv)
2236 {
2237 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
2238 ESL_STOPWATCH *w = esl_stopwatch_Create();
2239 ESL_ALPHABET *abc = NULL;
2240 char *hmmfile = esl_opt_GetArg(go, 1);
2241 P7_HMMFILE *hfp = NULL;
2242 P7_HMM *hmm = NULL;
2243 P7_BG *bg = NULL;
2244 P7_PROFILE *gm = NULL;
2245 P7_OPROFILE *om = NULL;
2246 int nmodel = 0;
2247 uint64_t totM = 0;
2248 int status;
2249 char errbuf[eslERRBUFSIZE];
2250
2251 esl_stopwatch_Start(w);
2252
2253 status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf);
2254 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
2255 else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
2256 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf);
2257
2258 while ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) == eslOK)
2259 {
2260 if (nmodel == 0) { /* first time initialization, now that alphabet known */
2261 bg = p7_bg_Create(abc);
2262 p7_bg_SetLength(bg, 400);
2263 }
2264
2265 if (esl_opt_GetBoolean(go, "-v")) printf("%s\n", hmm->name);
2266 nmodel++;
2267 totM += hmm->M;
2268
2269 if (esl_opt_GetBoolean(go, "-a") == TRUE)
2270 {
2271 gm = p7_profile_Create(hmm->M, abc);
2272 p7_ProfileConfig(hmm, bg, gm, 400, p7_LOCAL);
2273 om = p7_oprofile_Create(gm->M, abc);
2274 p7_oprofile_Convert(gm, om);
2275 p7_oprofile_ReconfigLength(om, 400);
2276
2277 p7_profile_Destroy(gm);
2278 p7_oprofile_Destroy(om);
2279 }
2280
2281 p7_hmm_Destroy(hmm);
2282 }
2283 if (status == eslEFORMAT) p7_Fail("bad file format in HMM file %s", hmmfile);
2284 else if (status == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets", hmmfile);
2285 else if (status != eslEOF) p7_Fail("Unexpected error in reading HMMs from %s", hmmfile);
2286
2287 esl_stopwatch_Stop(w);
2288 esl_stopwatch_Display(stdout, w, "# CPU time: ");
2289 printf("# number of models: %d\n", nmodel);
2290 printf("# total M: %" PRId64 "\n", totM);
2291
2292 p7_bg_Destroy(bg);
2293 p7_hmmfile_Close(hfp);
2294 esl_alphabet_Destroy(abc);
2295 esl_stopwatch_Destroy(w);
2296 esl_getopts_Destroy(go);
2297 return 0;
2298 }
2299 #endif /*p7HMMFILE_BENCHMARK*/
2300 /*---------------- end, benchmark driver ------------------------*/
2301
2302
2303 /*****************************************************************
2304 * 7. Unit tests.
2305 *****************************************************************/
2306 #ifdef p7HMMFILE_TESTDRIVE
2307
2308 /* utest_io_30: tests read/write for 3.0 save files.
2309 * Caller provides a named tmpfile that we can
2310 * open, write to, close, reopen, then read from.
2311 * <format> can be -1 or any specified 3.x save
2312 * file format.
2313 * Caller also provides a test HMM, which might
2314 * be a nasty random-sampled HMM.
2315 */
2316 static int
2317 utest_io_30(char *tmpfile, int format, P7_HMM *hmm)
2318 {
2319 FILE *fp = NULL;
2320 P7_HMMFILE *hfp = NULL;
2321 P7_HMM *new = NULL;
2322 ESL_ALPHABET *newabc = NULL;
2323 char msg[] = "3.0 file i/o unit test failed";
2324
2325 /* Write the HMM to disk as ASCII */
2326 if ((fp = fopen(tmpfile, "w")) == NULL) esl_fatal(msg);
2327 if (p7_hmmfile_WriteASCII(fp, format, hmm) != eslOK) esl_fatal(msg);
2328 fclose(fp);
2329
2330 /* Read it back */
2331 if (p7_hmmfile_Open(tmpfile, NULL, &hfp, NULL) != eslOK) esl_fatal(msg);
2332 if (p7_hmmfile_Read(hfp, &newabc, &new) != eslOK) esl_fatal(msg);
2333
2334 /* It should have determined the right file format */
2335 if (format == -1) { if (hfp->format != p7_HMMFILE_3f) esl_fatal(msg); }
2336 else { if (hfp->format != format) esl_fatal(msg); }
2337
2338 /* It should be identical to what we started with, modulo some legacy format issues */
2339 if (format < p7_HMMFILE_3e) { strcpy(new->consensus, hmm->consensus); }
2340 if (p7_hmm_Compare(hmm, new, 0.0001) != eslOK) esl_fatal(msg);
2341 p7_hmm_Destroy(new);
2342
2343 /* Trying to read one more HMM should give us a normal EOF */
2344 if (p7_hmmfile_Read(hfp, &newabc, &new) != eslEOF) esl_fatal(msg);
2345 p7_hmmfile_Close(hfp);
2346
2347 /* Do it all again, but with binary format */
2348 if ((fp = fopen(tmpfile, "w")) == NULL) esl_fatal(msg);
2349 if (p7_hmmfile_WriteBinary(fp, format, hmm) != eslOK) esl_fatal(msg);
2350 fclose(fp);
2351 if (p7_hmmfile_Open(tmpfile, NULL, &hfp, NULL) != eslOK) esl_fatal(msg);
2352 if (p7_hmmfile_Read(hfp, &newabc, &new) != eslOK) esl_fatal(msg);
2353 if (format < p7_HMMFILE_3e) { strcpy(new->consensus, hmm->consensus); }
2354 if (p7_hmm_Compare(hmm, new, 0.0001) != eslOK) esl_fatal(msg);
2355
2356 if (format == -1) { if (hfp->format != p7_HMMFILE_3f) esl_fatal(msg); }
2357 else { if (hfp->format != format) esl_fatal(msg); }
2358
2359 p7_hmm_Destroy(new);
2360 p7_hmmfile_Close(hfp);
2361
2362 esl_alphabet_Destroy(newabc);
2363 return eslOK;
2364 }
2365
2366
2367 /* Test current (3/e) file formats */
2368 static int
2369 utest_io_current(char *tmpfile, P7_HMM *hmm)
2370 {
2371 /* Try to break the 32-bit unsigned checksum, setting high order bit */
2372 hmm->checksum = 0xffeeddcc;
2373 hmm->flags |= p7H_CHKSUM;
2374
2375 utest_io_30(tmpfile, -1, hmm);
2376 return eslOK;
2377 }
2378
2379
2380 /* Test compatibility mode for 3/a file formats */
2381 static int
2382 utest_io_3a(char *tmpfile, P7_HMM *hmm)
2383 {
2384 float oldparam[p7_NEVPARAM];
2385
2386 /* Try to break the 32-bit unsigned checksum, setting high order bit */
2387 hmm->checksum = 0xffeeddcc;
2388 hmm->flags |= p7H_CHKSUM;
2389
2390 /* Make a copy of the old statistics.
2391 * Rearrange stats params to satisfy 3/a's constraints: vmu=mmu, mlambda=vlambda=flambda
2392 */
2393 esl_vec_FCopy(hmm->evparam, p7_NEVPARAM, oldparam);
2394 hmm->evparam[p7_VMU] = hmm->evparam[p7_MMU];
2395 hmm->evparam[p7_VLAMBDA] = hmm->evparam[p7_MLAMBDA];
2396 hmm->evparam[p7_FLAMBDA] = hmm->evparam[p7_MLAMBDA];
2397
2398 utest_io_30(tmpfile, p7_HMMFILE_3a, hmm);
2399
2400 /* Restore the original statistics */
2401 esl_vec_FCopy(oldparam, p7_NEVPARAM, hmm->evparam);
2402 return eslOK;
2403 }
2404
2405 #endif /*p7HMMFILE_TESTDRIVE*/
2406 /*-------------------- end, unit tests --------------------------*/
2407
2408
2409
2410
2411 /*****************************************************************
2412 * 8. Test driver.
2413 *****************************************************************/
2414
2415 #ifdef p7HMMFILE_TESTDRIVE
2416 /* gcc -g -Wall -Dp7HMMFILE_TESTDRIVE -I. -I../easel -L. -L../easel -o p7_hmmfile_test p7_hmmfile.c -lhmmer -leasel -lm
2417 */
2418 #include <p7_config.h>
2419
2420 #include "easel.h"
2421 #include "esl_alphabet.h"
2422 #include "esl_random.h"
2423
2424 #include "hmmer.h"
2425
2426 int
2427 main(int argc, char **argv)
2428 {
2429 ESL_RANDOMNESS *r = NULL;
2430 ESL_ALPHABET *aa_abc = NULL,
2431 *nt_abc = NULL;
2432 P7_HMM *hmm = NULL;
2433 FILE *fp = NULL;
2434 char tmpfile[32] = "tmp-hmmerXXXXXX";
2435 int M = 20;
2436
2437 if ((aa_abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create amino alphabet");
2438 if ((nt_abc = esl_alphabet_Create(eslDNA)) == NULL) esl_fatal("failed to create DNA alphabet");
2439 if ((r = esl_randomness_CreateFast(0)) == NULL) esl_fatal("failed to create randomness");
2440 if ((esl_tmpfile_named(tmpfile, &fp)) != eslOK) esl_fatal("failed to create tmp file");
2441 fclose(fp);
2442
2443 /* Protein HMMs */
2444 p7_hmm_Sample(r, M, aa_abc, &hmm);
2445 utest_io_current(tmpfile, hmm);
2446 utest_io_3a (tmpfile, hmm);
2447 p7_hmm_Destroy(hmm);
2448
2449 /* Nucleic acid HMMs */
2450 p7_hmm_Sample(r, M, nt_abc, &hmm);
2451 utest_io_current(tmpfile, hmm);
2452 utest_io_3a (tmpfile, hmm);
2453 p7_hmm_Destroy(hmm);
2454
2455 esl_alphabet_Destroy(aa_abc);
2456 esl_alphabet_Destroy(nt_abc);
2457 esl_randomness_Destroy(r);
2458 remove(tmpfile);
2459 exit(0);
2460 }
2461 #endif /*p7HMMFILE_TESTDRIVE*/
2462 /*-------------------- end, test driver -------------------------*/
2463
2464
2465 /*****************************************************************
2466 * 9. Example.
2467 *****************************************************************/
2468 /* On using the example to test error messages from p7_hmmfile_Open():
2469 * Message
2470 * --------------
2471 * .gz file missing/not readable \rm test.hmm.gz; touch test.hmm.gz; src/p7_hmmfile_example test.hmm.gz
2472 * gzip -dc doesn't exist \cp testsuite/20aa.hmm test.hmm; gzip test.hmm; sudo mv /usr/bin/gzip /usr/bin/gzip.old; src/p7_hmmfile_example test.hmm.gz
2473 * hmm file not found \rm test.hmm; src/p7_hmmfile_example test.hmm
2474 * bad SSI file format \cp testsuite/20aa.hmm test.hmm; \rm test.hmm.ssi; touch test.hmm.ssi; src/p7_hmmfile_example test.hmm
2475 * 64-bit SSI on 32-bit sys
2476 * empty file \rm test.hmm; touch test.hmm
2477 * unrecognized format (binary) cat testsuite/20aa.hmm > test.hmm; src/hmmpress test.hmm; \rm test.hmm; [edit test.hmm.h3m, delete first byte]
2478 * unrecognized format (ascii) cat testsuite/20aa.hmm | sed -e 's/^HMMER3\/b/HMMER3\/x/' > test.hmm
2479 *
2480 */
2481
2482 #ifdef p7HMMFILE_EXAMPLE
2483 /* gcc -g -Wall -Dp7HMMFILE_EXAMPLE -I. -I../easel -L. -L../easel -o p7_hmmfile_example p7_hmmfile.c -lhmmer -leasel -lm
2484 */
2485 #include <p7_config.h>
2486
2487 #include "easel.h"
2488 #include "esl_alphabet.h"
2489
2490 #include "hmmer.h"
2491
2492 int
2493 main(int argc, char **argv)
2494 {
2495 char *hmmfile = argv[1];
2496 P7_HMMFILE *hfp = NULL;
2497 P7_HMM *hmm = NULL;
2498 ESL_ALPHABET *abc = NULL;
2499 char errbuf[eslERRBUFSIZE];
2500 int status;
2501
2502 /* An example of reading a single HMM from a file, and checking that it is the only one. */
2503 status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf);
2504 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
2505 else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
2506 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf);
2507
2508 status = p7_hmmfile_Read(hfp, &abc, &hmm);
2509 if (status == eslEFORMAT) p7_Fail("Bad file format in HMM file %s:\n%s\n", hfp->fname, hfp->errbuf);
2510 else if (status == eslEINCOMPAT) p7_Fail("HMM in %s is not in the expected %s alphabet\n", hfp->fname, esl_abc_DecodeType(abc->type));
2511 else if (status == eslEOF) p7_Fail("Empty HMM file %s? No HMM data found.\n", hfp->fname);
2512 else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s\n", hfp->fname);
2513
2514 status = p7_hmmfile_Read(hfp, &abc, NULL);
2515 if (status != eslEOF) p7_Fail("HMM file %s does not contain just one HMM\n", hfp->fname);
2516
2517 p7_hmmfile_Close(hfp);
2518
2519 p7_hmmfile_WriteASCII(stdout, -1, hmm);
2520
2521 esl_alphabet_Destroy(abc);
2522 p7_hmm_Destroy(hmm);
2523 return 0;
2524 }
2525 #endif /*p7HMMFILE_EXAMPLE*/
2526 /*----------------------- end, example --------------------------*/
2527
2528