Debian Codegraph @

src/cm_file.c

raw symbols SCIP
1 /* Input/output of CMs.
2 *
3 * EPN, Fri Jun 17 09:37:56 2011
4 * Based on p7_hmmfile.c copied from HMMER svn revision 3570.
5 *
6 * Contents:
7 * 1. The CM_FILE object for reading CMs
8 * 2. Writing CM files.
9 * 3. API for reading CM files in various formats.
10 * 4. API for reading/writing p7 HMMs/profile filters.
11 * 5. Private, specific CM file format parsers.
12 * 6. Other private functions involved in i/o.
13 * 7. Legacy v1.0 ascii file format output.
14 * 8. Benchmark driver.
15 * 9. Unit tests.
16 * 10. Test driver.
17 * 11. Example.
18 */
19 #include <esl_config.h>
20 #include <p7_config.h>
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #ifdef HMMER_THREADS
28 #include <pthread.h>
29 #endif
30
31 #include "easel.h"
32 #include "esl_alphabet.h"
33 #include "esl_ssi.h" /* this gives us esl_byteswap */
34 #include "esl_vectorops.h" /* gives us esl_vec_FCopy() */
35
36 #include "hmmer.h"
37
38 #include "infernal.h"
39
40 /* Magic numbers identifying binary formats.
41 * Do not change the old magics! Necessary for backwards compatibility.
42 */
43 #if 0 /* temporarily remove all the magic; write backwards compat stuff later */
44 static unsigned int v01magic = 0xe3edb0b1; /* v0.1 binary: "cm01" + 0x80808080 */
45 #endif
46
47 static uint32_t v1a_magic = 0xe3edb0b2; /* v1.1 binary: "cm02" + 0x80808080 */
48 static uint32_t v1a_fmagic = 0xb1e1e6f3; /* 1/a binary MSV/SSV file: "1afs" = 0x 31 61 66 73 + 0x80808080 */
49 /* Note: 's' at end of 1afs is arbitrary. It is consistent with H3's
50 * trailing 's' iforSSE binary files, but in Infernal this is used
51 * whether we're building for SSE or VMX (since this code is neither
52 * SSE nor VMX-specific). I suppose we could say it stands for SSV,
53 * with 'f' for filter... Also, http://www.ascii-code.com/ and
54 * hmmer/src/impl_sse/io.c might be useful to determine how each code
55 * derives from its text string.
56 */
57
58 static int read_asc_1p1_cm(CM_FILE *hfp, int read_fp7, ESL_ALPHABET **ret_abc, CM_t **opt_cm);
59 static int read_bin_1p1_cm(CM_FILE *hfp, int read_fp7, ESL_ALPHABET **ret_abc, CM_t **opt_cm);
60 static int read_asc_1p0_cm(CM_FILE *hfp, int read_fp7, ESL_ALPHABET **ret_abc, CM_t **opt_cm);
61
62 static int write_bin_string(FILE *fp, char *s);
63 static int read_bin_string (FILE *fp, char **ret_s);
64
65 static char *prob2ascii(float p, float null);
66 static float ascii2prob(char *s, float null);
67 static int is_integer(char *s);
68 static int is_real(char *s);
69
70 /* from p7_hmmfile.c, for reading p7 filters */
71 static uint32_t v3a_magic = 0xe8ededb6; /* 3/a binary: "hmm6" + 0x80808080 */
72 static uint32_t v3b_magic = 0xe8ededb7; /* 3/b binary: "hmm7" + 0x80808080 */
73 static uint32_t v3c_magic = 0xe8ededb8; /* 3/c binary: "hmm8" + 0x80808080 */
74 static uint32_t v3d_magic = 0xe8ededb9; /* 3/d binary: "hmm9" + 0x80808080 */
75 static uint32_t v3e_magic = 0xe8ededb0; /* 3/e binary: "hmm0" + 0x80808080 */
76 static uint32_t v3f_magic = 0xe8ededba; /* 3/f binary: "hmma" + 0x80808080 */
77
78 static int read_asc30hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm);
79 static int read_bin30hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm);
80
81 /*****************************************************************
82 * 1. The CM_FILE object for reading CMs.
83 *****************************************************************/
84 static int open_engine(char *filename, char *env, CM_FILE **ret_cmfp, int do_ascii_only, int allow_1p0, char *errbuf);
85
86 /* Function: cm_file_Open()
87 * Synopsis: Open an CM file <filename>.
88 * Incept: EPN, Fri Jun 17 09:42:13 2011
89 * SRE, Tue Dec 21 10:44:38 2010 [Zaragoza] (p7_hmmfile_OpenE())
90 *
91 * Purpose: Open an CM file <filename>, and prepare to read the first
92 * CM from it.
93 *
94 * The format should be INFERNAL1/a or more recent. If
95 * <allow_1p0>, we also allow the file to be in Infernal
96 * v1.0 to v1.0.2 ascii format.
97 *
98 * We look for <filename> relative to the current working
99 * directory. Additionally, if we don't find it in the cwd
100 * and <env> is non-NULL, we will look for <filename>
101 * relative to one or more directories in a colon-delimited
102 * list obtained from the environment variable <env>. For
103 * example, if we had <setenv INFERNALDB
104 * /misc/db/Rfam:/misc/db/Pfam> in the environment, a
105 * CM application might pass "INFERNALDB" as <env>.
106 *
107 * As a special case, if <filename> is "-", then CMs will
108 * be read from <stdin>. In this case, <env> has no effect.
109 *
110 * As another special case, if <filename> ends in a <.gz>
111 * suffix, the file is assumed to be compressed by GNU
112 * <gzip>, and it is opened for reading from a pipe with
113 * <gunzip -dc>. This feature is only available on
114 * POSIX-compliant systems that have a <popen()> call, and
115 * <HAVE_POPEN> is defined by the configure script at
116 * compile time.
117 *
118 * Args: filename - CM file to open; or "-" for <stdin>
119 * env - list of paths to look for <cmfile> in, in
120 * addition to current working dir; or <NULL>
121 * allow_1p0 - TRUE to allow 1.0 formatted files
122 * ret_cmfp - RETURN: opened <P7_CMFILE>.
123 * errbuf - error message buffer: <NULL>, or a ptr
124 * to <eslERRBUFSIZE> chars of allocated space.
125 *
126 * Returns: <eslOK> on success, and the open <CM_FILE> is returned
127 * in <*ret_cmfp>.
128 *
129 * <eslENOTFOUND> if <filename> can't be opened for
130 * reading, even after the list of directories in <env> (if
131 * any) is checked.
132 *
133 * <eslEFORMAT> if <filename> is not in a recognized Infernal
134 * CM file format.
135 *
136 * On either type of error, if a non-NULL <errbuf> was provided,
137 * a useful user error message is left in it.
138 *
139 * Throws: <eslEMEM> on allocation failure.
140 */
141 int
142 cm_file_Open(char *filename, char *env, int allow_1p0, CM_FILE **ret_cmfp, char *errbuf)
143 {
144 return open_engine(filename, env, ret_cmfp, FALSE, allow_1p0, errbuf);
145 }
146
147 /* Function: cm_file_OpenNoDB()
148 * Synopsis: Open only a CM flatfile, even if pressed db exists.
149 * Incept: SRE, Tue Dec 21 10:52:35 2010 [Zaragoza]
150 *
151 * Purpose: Same as <cm_file_OpenE()> except that if a pressed
152 * database exists for <filename>, it is ignored. Only
153 * <filename> itself is opened.
154 *
155 * hmmpress needs this call. Otherwise, it opens a press'ed
156 * database that it may be about to overwrite.
157 */
158 int
159 cm_file_OpenNoDB(char *filename, char *env, int allow_1p0, CM_FILE **ret_cmfp, char *errbuf)
160 {
161 return open_engine(filename, env, ret_cmfp, TRUE, allow_1p0, errbuf);
162 }
163
164
165 /* Function: cm_file_OpenBuffer()
166 * Incept: EPN, Fri Jun 17 09:47:34 2011
167 * MSF, Thu Aug 19 2010 [Janelia] (p7_hmmfile_OpenBuffer())
168 *
169 * Purpose: Perparse a buffer containing an ascii CM for parsing.
170 *
171 * As another special case, if <filename> ends in a <.gz>
172 * suffix, the file is assumed to be compressed by GNU
173 * <gzip>, and it is opened for reading from a pipe with
174 * <gunzip -dc>. This feature is only available on
175 * POSIX-compliant systems that have a <popen()> call, and
176 * <HAVE_POPEN> is defined by the configure script at
177 * compile time.
178 *
179 * Args: filename - CM file to open; or "-" for <stdin>
180 * env - list of paths to look for <cmfile> in, in
181 * addition to current working dir; or <NULL>
182 * ret_cmfp - RETURN: opened <CM_FILE>.
183 *
184 * Returns: <eslOK> on success, and the open <CM_FILE> is returned
185 * in <*ret_cmfp>.
186 *
187 * <eslENOTFOUND> if <filename> can't be opened for
188 * reading, even after the list of directories in <env> (if
189 * any) is checked.
190 *
191 * <eslEFORMAT> if <filename> is not in a recognized HMMER
192 * CM file format.
193 *
194 * Throws: <eslEMEM> on allocation failure.
195 */
196 int
197 cm_file_OpenBuffer(char *buffer, int size, int allow_1p0, CM_FILE **ret_cmfp)
198 {
199 CM_FILE *cmfp = NULL;
200 int status;
201 char *tok;
202 int toklen;
203
204 ESL_ALLOC(cmfp, sizeof(CM_FILE));
205 cmfp->f = NULL;
206 cmfp->fname = NULL;
207 cmfp->do_gzip = FALSE;
208 cmfp->do_stdin = FALSE;
209 cmfp->newly_opened = TRUE; /* well, it will be, real soon now */
210 cmfp->is_pressed = FALSE;
211 #ifdef HMMER_THREADS
212 cmfp->syncRead = FALSE;
213 #endif
214 cmfp->parser = NULL;
215 cmfp->efp = NULL;
216 cmfp->ffp = NULL;
217 cmfp->pfp = NULL;
218 cmfp->ssi = NULL;
219 cmfp->errbuf[0] = '\0';
220 cmfp->msv_errbuf[0] = '\0';
221
222 if ((cmfp->efp = esl_fileparser_CreateMapped(buffer, size)) == NULL) { status = eslEMEM; goto ERROR; }
223 if ((status = esl_fileparser_SetCommentChar(cmfp->efp, '#')) != eslOK) goto ERROR;
224 if ((status = esl_fileparser_GetToken(cmfp->efp, &tok, &toklen)) != eslOK) goto ERROR;
225
226 if ( strcmp("INFERNAL1/a", tok) == 0) { cmfp->format = CM_FILE_1a; cmfp->parser = read_asc_1p1_cm; }
227 else if (allow_1p0 && strcmp("INFERNAL-1", tok) == 0) { cmfp->format = CM_FILE_1; cmfp->parser = read_asc_1p0_cm; }
228
229 if (cmfp->parser == NULL) { status = eslEFORMAT; goto ERROR; }
230
231 *ret_cmfp = cmfp;
232 return eslOK;
233
234 ERROR:
235 if (cmfp != NULL) cm_file_Close(cmfp);
236 *ret_cmfp = NULL;
237 if (status == eslEMEM) return status;
238 else if (status == eslENOTFOUND) return status;
239 else return eslEFORMAT;
240 }
241
242
243 /* open_engine()
244 *
245 * Implements the file opening functions:
246 * <cm_file_Open()>, <cm_file_OpenNoDB()>,
247 * See their comments above.
248 *
249 * Only returns three types of errors:
250 * eslENOTFOUND - file (the CM file) or program (gzip, for .gz files) not found
251 * eslEFORMAT - bad CM file format (or format of associated file)
252 * eslEMEM - allocation failure somewhere
253 * <errbuf>, if non-NULL, will contain a useful error message.
254 *
255 */
256 static int
257 open_engine(char *filename, char *env, CM_FILE **ret_cmfp, int do_ascii_only, int allow_1p0, char *errbuf)
258 {
259 CM_FILE *cmfp = NULL;
260 char *envfile = NULL; /* full path to filename after using environment */
261 char *dbfile = NULL; /* constructed name of an index or binary db file */
262 char *cmd = NULL; /* constructed gzip -dc pipe command */
263 int status;
264 int n = strlen(filename);
265 union { char c[4]; uint32_t n; } magic;
266 char *tok;
267 int toklen;
268
269 ESL_ALLOC(cmfp, sizeof(CM_FILE));
270 cmfp->f = NULL;
271 cmfp->fname = NULL;
272 cmfp->do_gzip = FALSE;
273 cmfp->do_stdin = FALSE;
274 cmfp->newly_opened = TRUE; /* well, it will be, real soon now */
275 cmfp->is_pressed = FALSE;
276 cmfp->is_binary = FALSE;
277 #ifdef HMMER_THREADS
278 cmfp->syncRead = FALSE;
279 #endif
280 cmfp->parser = NULL;
281 cmfp->efp = NULL;
282 cmfp->hfp = NULL;
283 cmfp->ffp = NULL;
284 cmfp->pfp = NULL;
285 cmfp->ssi = NULL;
286 cmfp->errbuf[0] = '\0';
287 cmfp->msv_errbuf[0] = '\0';
288
289 /* 1. There's two special reading modes that have limited indexing
290 * and optimization capability: reading from standard input, and
291 * reading a gzip'ped file. Once we've set one of these up and set
292 * either the <do_stdin> or <do_gzip> flag, we won't try to open
293 * any associated indexes or binary database files.
294 */
295 if (strcmp(filename, "-") == 0) /* "-" means read from stdin */
296 {
297 cmfp->f = stdin;
298 cmfp->do_stdin = TRUE;
299 if ((status = esl_strdup("[STDIN]", -1, &(cmfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup failed; shouldn't happen");
300 }
301 #ifdef HAVE_POPEN
302 else if (n > 3 && strcmp(filename+n-3, ".gz") == 0) /* a <*.gz> filename means read via gunzip pipe */
303 {
304 if (! esl_FileExists(filename)) ESL_XFAIL(eslENOTFOUND, errbuf, ".gz file %s not found or not readable", filename);
305 if ((status = esl_sprintf(&cmd, "gzip -dc %s", filename)) != eslOK) ESL_XFAIL(status, errbuf, "when setting up .gz pipe: esl_sprintf() failed");
306 if ((cmfp->f = popen(cmd, "r")) == NULL) ESL_XFAIL(eslENOTFOUND, errbuf, "gzip -dc %s failed; gzip not installed or not in PATH?", filename);
307 if ((status = esl_strdup(filename, n, &(cmfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
308 cmfp->do_gzip = TRUE;
309 free(cmd); cmd = NULL;
310 }
311 #endif /*HAVE_POPEN: gzip mode */
312
313
314 /* 2. If <cmfp->f> is still NULL, then we're in the usual situation
315 * of looking for a file on disk. It may either be in the cwd, or
316 * in one of the directories listed in the <env> string. Find it,
317 * open it to <cmfp->f>, and set <cmfp->filename>. The
318 * <cmfp->filename> string will be used later to construct the
319 * names of expected index and binary database files.
320 */
321 if (cmfp->f == NULL)
322 {
323 if ((cmfp->f = fopen(filename, "r")) != NULL)
324 {
325 if ((status = esl_strdup(filename, n, &(cmfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
326 }
327 else if (esl_FileEnvOpen(filename, env, &(cmfp->f), &envfile) == eslOK)
328 {
329 n = strlen(envfile);
330 if ((status = esl_strdup(envfile, n, &(cmfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
331 free(envfile); envfile = NULL;
332 }
333 else
334 { /* temporarily copy filename over to cmfp->fname, even though we haven't opened anything: we'll next try to open <filename>.h3m */
335 if ((status = esl_strdup(filename, n, &(cmfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
336 }
337 }
338 /* <cmfp->f> may *still* be NULL, if <filename> is a press'ed database and ASCII file is deleted */
339
340
341 /* 3. Look for the binary model file component of a press'ed CM database.
342 *
343 * If <cmfp->f> is still NULL, this is our last chance to find it.
344 * (The ASCII base file may have been deleted to save space, leaving
345 * binary press'ed files.)
346 *
347 * If we've been asked to open only an ASCII file -- because we're being
348 * called by cmpress, for example! -- then don't do this.
349 */
350 if (! do_ascii_only && ! cmfp->do_stdin && ! cmfp->do_gzip)
351 {
352 FILE *tmpfp;
353 /* if we opened an ASCII file in the INFERNALDB directory, cmfp->fname contains fully qualified name of file including the path */
354 if ((status = esl_sprintf(&dbfile, "%s.i1m", cmfp->fname) != eslOK)) ESL_XFAIL(status, errbuf, "esl_sprintf() failed; shouldn't happen");
355
356 if ((tmpfp = fopen(dbfile, "rb")) != NULL)
357 {
358 if (cmfp->f != NULL) fclose(cmfp->f); /* preferentially read the .i1m file, not the original */
359 cmfp->f = tmpfp;
360 cmfp->is_pressed = TRUE;
361 free(cmfp->fname);
362 if ((status = esl_strdup(dbfile, -1, &(cmfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed, shouldn't happen");
363 }
364 else if (cmfp->f == NULL && esl_FileEnvOpen(dbfile, env, &(cmfp->f), &envfile) == eslOK)
365 { /* found a binary-only press'ed db in one of the env directories. */
366 free(cmfp->fname);
367 if ((status = esl_strdup(envfile, -1, &(cmfp->fname))) != eslOK) ESL_XFAIL(status, errbuf, "esl_strdup() failed; shouldn't happen");
368 cmfp->is_pressed = TRUE;
369 }
370 free(dbfile); dbfile = NULL;
371 }
372
373 /* 4. <cmfp->f> must now point to a valid model input stream: if not, we fail.
374 */
375 if (cmfp->f == NULL)
376 {
377 if (env) ESL_XFAIL(eslENOTFOUND, errbuf, "CM file %s not found (nor an .i1m binary of it); also looked in %s", filename, env);
378 else ESL_XFAIL(eslENOTFOUND, errbuf, "CM file %s not found (nor an .i1m binary of it)", filename);
379 }
380
381 /* 5. Set up the HMM file <cmfp->hfp> that we'll use to read p7
382 * filter HMMs from within the CM file. We do this before we
383 * handle press'd model files, so we can set <cmfp->hfp->ffp>
384 * and <cmfp->hfp->pfp> if necessary.
385 *
386 */
387 ESL_ALLOC(cmfp->hfp, sizeof(P7_HMMFILE));
388
389 if (!cmfp->do_stdin && !cmfp->do_gzip) {
390 if ((cmfp->hfp->f = fopen(cmfp->fname, "r")) == NULL) goto ERROR;
391 }
392 else if (cmfp->do_stdin) {
393 cmfp->hfp->f = stdin;
394 }
395 #ifdef HAVE_POPEN /* gzip functionality */
396 else if (cmfp->do_gzip) { /* will only possibly be TRUE if HAVE_POPEN */
397 /* we don't open the file separately for cmfp->hfp in gzip case,
398 * which works fine (since we're never a press'd db) but we have
399 * to be careful when closing cmfp in cm_file_Close().
400 */
401 cmfp->hfp->f = cmfp->f;
402 }
403 #endif
404 cmfp->hfp->do_gzip = cmfp->do_gzip;
405 cmfp->hfp->do_stdin = cmfp->do_stdin;
406 cmfp->hfp->newly_opened = TRUE; /* well, it will be, real soon now */
407 cmfp->hfp->is_pressed = cmfp->is_pressed;
408 #ifdef HMMER_THREADS
409 cmfp->hfp->syncRead = FALSE;
410 #endif
411 cmfp->hfp->parser = NULL;
412 cmfp->hfp->efp = NULL;
413 cmfp->hfp->ffp = NULL;
414 cmfp->hfp->pfp = NULL;
415 cmfp->hfp->ssi = NULL; /* not sure if this should point to cmfp->ssi */
416 cmfp->hfp->errbuf[0] = '\0';
417 cmfp->hfp->rr_errbuf[0] = '\0';
418 if ((status = esl_strdup(cmfp->fname, -1, &(cmfp->hfp->fname))) != eslOK) goto ERROR;
419
420 /* 6. If we found and opened a binary model file .i1m, open the rest of
421 * the press'd model files. (this can't be true if do_ascii_only is set).
422 * Note that we set cmfp->hfp->ffp and cmfp->hfp->pfp at the end of the
423 * function after we've
424 */
425 if (cmfp->is_pressed)
426 {
427 /* here we rely on the fact that the suffixes are .i1{mfpi}, to construct other names from .i1m file name !! */
428 n = strlen(cmfp->fname); /* so, n = '\0', n-1 = 'm' */
429 esl_strdup(cmfp->fname, n, &dbfile);
430
431 dbfile[n-1] = 'f'; /* the MSV filter part of the optimized sequence profiles (HMMs) */
432 if ((cmfp->ffp = fopen(dbfile, "rb")) == NULL) ESL_XFAIL(eslENOTFOUND, errbuf, "Opened %s, a pressed CM file; but no .i1f file found", cmfp->fname);
433 if ((cmfp->hfp->ffp = fopen(dbfile, "rb")) == NULL) ESL_XFAIL(eslENOTFOUND, errbuf, "Opened %s, a pressed CM file; but no .i1f file found", cmfp->fname);
434
435 dbfile[n-1] = 'p'; /* the remainder of the optimized sequence profiles (HMMs) */
436 if ((cmfp->hfp->pfp = fopen(dbfile, "rb")) == NULL) ESL_XFAIL(eslENOTFOUND, errbuf, "Opened %s, a pressed CM file; but no .i1p file found", cmfp->fname);
437
438 dbfile[n-1] = 'i'; /* the SSI index for the .i1m file */
439 status = esl_ssi_Open(dbfile, &(cmfp->ssi));
440 if (status == eslENOTFOUND) ESL_XFAIL(eslENOTFOUND, errbuf, "Opened %s, a pressed CM file; but no .i1i file found", cmfp->fname);
441 else if (status == eslEFORMAT) ESL_XFAIL(eslEFORMAT, errbuf, "Opened %s, a pressed CM file; but format of its .i1i file unrecognized", cmfp->fname);
442 else if (status == eslERANGE) ESL_XFAIL(eslEFORMAT, errbuf, "Opened %s, a pressed CM file; but its .i1i file is 64-bit and your system is 32-bit", cmfp->fname);
443 else if (status != eslOK) ESL_XFAIL(eslEFORMAT, errbuf, "Opened %s, a pressed CM file; but failed to open its .i1i file", cmfp->fname);
444
445 free(dbfile); dbfile = NULL;
446 }
447 else
448 {
449 if ((status = esl_sprintf(&dbfile, "%s.ssi", cmfp->fname)) != eslOK) ESL_XFAIL(status, errbuf, "esl_sprintf() failed");
450
451 status = esl_ssi_Open(dbfile, &(cmfp->ssi)); /* not finding an SSI file is ok. we open it if we find it. */
452 if (status == eslEFORMAT) ESL_XFAIL(status, errbuf, "a %s.ssi file exists (an SSI index), but its SSI format is not recognized", cmfp->fname);
453 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", cmfp->fname);
454 else if (status != eslOK && status != eslENOTFOUND) ESL_XFAIL(status, errbuf, "esl_ssi_Open() failed");
455 free(dbfile); dbfile = NULL;
456 }
457
458
459 /* 7. Check for binary file format. A pressed db is automatically binary: verify. */
460 if (! fread((char *) &(magic.n), sizeof(uint32_t), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, errbuf, "File exists, but appears to be empty?");
461 if (magic.n == v1a_magic) { cmfp->format = CM_FILE_1a; cmfp->parser = read_bin_1p1_cm; cmfp->is_binary = TRUE; }
462 else if (cmfp->is_pressed) ESL_XFAIL(eslEFORMAT, errbuf, "Binary format tag in %s unrecognized\nCurrent Infernal format is INFERNAL1/a. Previous binary formats are not supported.", cmfp->fname);
463
464 /* 8. Checks for ASCII file format */
465 if (cmfp->parser == NULL)
466 {
467 /* Does the magic appear to be binary, yet we didn't recognize it? */
468 if (magic.n & 0x80000000) ESL_XFAIL(eslEFORMAT, errbuf, "Format tag appears binary, but unrecognized\nCurrent Infernal format is INFERNAL1/a. Previous binary formats are not supported.");
469
470 if ((cmfp->efp = esl_fileparser_Create(cmfp->f)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "internal error in esl_fileparser_Create()");
471 if ((status = esl_fileparser_SetCommentChar(cmfp->efp, '#')) != eslOK) ESL_XFAIL(status, errbuf, "internal error in esl_fileparser_SetCommentChar()");
472 if ((status = esl_fileparser_NextLinePeeked(cmfp->efp, magic.c, 4)) != eslOK) ESL_XFAIL(status, errbuf, "internal error in esl_fileparser_NextLinePeeked()");
473 if ((status = esl_fileparser_GetToken(cmfp->efp, &tok, &toklen)) != eslOK) ESL_XFAIL(status, errbuf, "internal error in esl_fileparser_GetToken()");
474
475 if ( strcmp("INFERNAL1/a", tok) == 0) { cmfp->format = CM_FILE_1a; cmfp->parser = read_asc_1p1_cm; }
476 else if (( allow_1p0) && strcmp("INFERNAL-1", tok) == 0) { cmfp->format = CM_FILE_1; cmfp->parser = read_asc_1p0_cm; }
477 else if ((! allow_1p0) && strcmp("INFERNAL-1", tok) == 0) { ESL_XFAIL(eslEFORMAT, errbuf, "Format tag is '%s': use cmconvert to reformat Infernal v1.0 to v1.0.2 CM files to current format", tok); }
478 else { ESL_XFAIL(eslEFORMAT, errbuf, "Format tag is '%s': unrecognized or not supported.", tok); }
479 }
480
481 *ret_cmfp = cmfp;
482 return eslOK;
483
484 ERROR:
485 if (cmd != NULL) free(cmd);
486 if (dbfile != NULL) free(dbfile);
487 if (envfile != NULL) free(envfile);
488 if (cmfp != NULL) cm_file_Close(cmfp);
489 *ret_cmfp = NULL;
490 if (status == eslEMEM) return status;
491 else if (status == eslENOTFOUND) return status;
492 else return eslEFORMAT;
493 }
494
495 /* Function: cm_file_Close()
496 * Incept: EPN, Fri Jun 17 10:06:42 2011
497 * SRE, Wed Jan 3 18:48:44 2007 [Casa de Gatos] (p7_hmmfile_Close())
498 *
499 * Purpose: Closes an open CM file <cmfp>.
500 *
501 * Returns: (void)
502 */
503 void
504 cm_file_Close(CM_FILE *cmfp)
505 {
506 if (cmfp == NULL) return;
507
508 #ifdef HAVE_POPEN /* gzip functionality */
509 if (cmfp->do_gzip && cmfp->f != NULL) {
510 pclose(cmfp->f);
511 /* careful here: in gzip mode we defined cmfp->hfp->f == cmfp->f,
512 * instead of reopening for cmfp->hfp->f, so we need to set
513 * cmfp->hfp->f to NULL so p7_hmmfile_Close() doesn't try to close
514 * it.
515 */
516 if(cmfp->f == cmfp->hfp->f) { /* this should be TRUE */
517 cmfp->hfp->f = NULL;
518 }
519 cmfp->f = NULL;
520 }
521 #endif
522 if (!cmfp->do_gzip && !cmfp->do_stdin && cmfp->f != NULL) fclose(cmfp->f);
523 if (cmfp->ffp != NULL) fclose(cmfp->ffp);
524 if (cmfp->pfp != NULL) fclose(cmfp->pfp);
525 if (cmfp->fname != NULL) free(cmfp->fname);
526 if (cmfp->efp != NULL) esl_fileparser_Destroy(cmfp->efp);
527 if (cmfp->ssi != NULL) esl_ssi_Close(cmfp->ssi);
528 #ifdef HMMER_THREADS
529 if (cmfp->syncRead) pthread_mutex_destroy (&cmfp->readMutex);
530 #endif
531
532 if(cmfp->hfp != NULL) p7_hmmfile_Close(cmfp->hfp);
533
534 free(cmfp);
535 }
536
537 #ifdef HMMER_THREADS
538 /* Function: cm_file_CreateLock()
539 * Incept: EPN, Fri Jun 17 10:07:06 2011
540 * MSF, Wed July 15 2009 (p7_hmmfile_CreateLock())
541 *
542 * Purpose: Create a lock to syncronize readers.
543 *
544 * Returns: <eslOK> on success.
545 */
546 int
547 cm_file_CreateLock(CM_FILE *cmfp)
548 {
549 int status;
550
551 if (cmfp == NULL) return eslEINVAL;
552
553 /* make sure the lock is not created twice */
554 if (!cmfp->syncRead)
555 {
556 cmfp->syncRead = TRUE;
557 status = pthread_mutex_init(&cmfp->readMutex, NULL);
558 if (status != 0) goto ERROR;
559 }
560
561 /* create lock on hmm files as well */
562 if (cmfp->hfp != NULL)
563 if((status = p7_hmmfile_CreateLock(cmfp->hfp)) != eslOK) goto ERROR;
564
565 return eslOK;
566
567 ERROR:
568 cmfp->syncRead = FALSE;
569 return eslFAIL;
570 }
571 #endif
572 /*----------------- end, CM_FILE object ----------------------*/
573
574
575 /*****************************************************************
576 * 2. Writing CM files.
577 *****************************************************************/
578 static int multiline(FILE *fp, const char *pfx, char *s);
579
580 /* Function: cm_file_WriteASCII()
581 * Synopsis: Write an Infernal 1.1 ASCII save file.
582 * Incept: EPN, Fri Jun 17 10:07:46 2011
583 * SRE, Tue May 19 09:39:31 2009 [Janelia] (p7_hmmfile_WriteASCII())
584 *
585 * Purpose: Write a covariance model <cm> in an ASCII save file format to
586 * an open stream <fp>.
587 *
588 * Currently only outputs in the default standard format,
589 * so format must be <CM_FILE_1a> or <-1> (which specifies
590 * the current default format be used).
591 * In the future other formats will be accepted.
592 *
593 * Args: fp - open stream for writing
594 * format - -1 for default format, or a 1.x format code like <CM_FILE_1a>
595 * cm - CM to save
596 *
597 * Returns: <eslOK> on success.
598 *
599 * Throws: <eslEINVAL> if <format> isn't a valid 3.0 format code.
600 */
601 int
602 cm_file_WriteASCII(FILE *fp, int format, CM_t *cm)
603 {
604 int x, v, nd, y;
605
606 if((cm->flags & CMH_LOCAL_BEGIN) || (cm->flags & CMH_LOCAL_END)) cm_Fail("cm_file_WriteASCII(): CM is in local mode");
607
608 if (format == -1) format = CM_FILE_1a;
609
610 if (format == CM_FILE_1a) fprintf(fp, "INFERNAL1/a [%s | %s]\n", INFERNAL_VERSION, INFERNAL_DATE);
611 else ESL_EXCEPTION(eslEINVAL, "invalid CM file format code");
612
613 fprintf(fp, "NAME %s\n", cm->name);
614 if (cm->acc) fprintf(fp, "ACC %s\n", cm->acc);
615 if (cm->desc) fprintf(fp, "DESC %s\n", cm->desc);
616 fprintf(fp, "STATES %d\n", cm->M);
617 fprintf(fp, "NODES %d\n", cm->nodes);
618 fprintf(fp, "CLEN %d\n", cm->clen);
619 fprintf(fp, "W %d\n", cm->W);
620 fprintf(fp, "ALPH %s\n", esl_abc_DecodeType(cm->abc->type));
621 fprintf(fp, "RF %s\n", (cm->flags & CMH_RF) ? "yes" : "no");
622 fprintf(fp, "CONS %s\n", (cm->flags & CMH_CONS) ? "yes" : "no");
623 fprintf(fp, "MAP %s\n", (cm->flags & CMH_MAP) ? "yes" : "no");
624 if (cm->ctime != NULL) fprintf (fp, "DATE %s\n", cm->ctime);
625 if (cm->comlog != NULL) multiline(fp, "COM ", cm->comlog);
626 fprintf(fp, "PBEGIN %g\n", cm->pbegin);
627 fprintf(fp, "PEND %g\n", cm->pend);
628 fprintf(fp, "WBETA %g\n", cm->beta_W);
629 fprintf(fp, "QDBBETA1 %g\n", cm->qdbinfo->beta1);
630 fprintf(fp, "QDBBETA2 %g\n", cm->qdbinfo->beta2);
631 fprintf(fp, "N2OMEGA %6g\n",cm->null2_omega);
632 fprintf(fp, "N3OMEGA %6g\n",cm->null3_omega);
633 fprintf(fp, "ELSELF %.8f\n",cm->el_selfsc);
634 fprintf(fp, "NSEQ %d\n", cm->nseq);
635 fprintf(fp, "EFFN %f\n",cm->eff_nseq);
636 if (cm->flags & CMH_CHKSUM) fprintf(fp, "CKSUM %u\n", cm->checksum); /* unsigned 32-bit */
637 fputs("NULL ", fp);
638 for (x = 0; x < cm->abc->K; x++) { fprintf(fp, "%6s ", prob2ascii(cm->null[x], 1/(float)(cm->abc->K))); }
639 fputc('\n', fp);
640 if (cm->flags & CMH_GA) fprintf(fp, "GA %.2f\n", cm->ga);
641 if (cm->flags & CMH_TC) fprintf(fp, "TC %.2f\n", cm->tc);
642 if (cm->flags & CMH_NC) fprintf(fp, "NC %.2f\n", cm->nc);
643
644 if (cm->flags & CMH_FP7) {
645 fprintf(fp, "EFP7GF %.4f %.5f\n", cm->fp7_evparam[CM_p7_GFMU], cm->fp7_evparam[CM_p7_GFLAMBDA]);
646 }
647 if (cm->flags & CMH_EXPTAIL_STATS)
648 {
649 /* make sure our dbsize values can be cast to a long reliably,
650 * even on 32 bit systems (<= 2 Gb), they certainly should be,
651 * max value in cmcalibrate is 160Mb. (This is related to bug
652 * i31.)
653 */
654 if(cm->expA[EXP_CM_LC]->dbsize > (2000. * 1000000.)) ESL_EXCEPTION(eslEINVAL, "invalid dbsize (too big) EXP_CM_LC");
655 if(cm->expA[EXP_CM_GC]->dbsize > (2000. * 1000000.)) ESL_EXCEPTION(eslEINVAL, "invalid dbsize (too big) EXP_CM_GC");
656 if(cm->expA[EXP_CM_LI]->dbsize > (2000. * 1000000.)) ESL_EXCEPTION(eslEINVAL, "invalid dbsize (too big) EXP_CM_LI");
657 if(cm->expA[EXP_CM_GI]->dbsize > (2000. * 1000000.)) ESL_EXCEPTION(eslEINVAL, "invalid dbsize (too big) EXP_CM_GI");
658
659 fprintf(fp, "ECMLC %.5f %10.5f %10.5f %10ld %10d %.6f\n",
660 cm->expA[EXP_CM_LC]->lambda, cm->expA[EXP_CM_LC]->mu_extrap, cm->expA[EXP_CM_LC]->mu_orig,
661 (long) (cm->expA[EXP_CM_LC]->dbsize + 0.5), cm->expA[EXP_CM_LC]->nrandhits, cm->expA[EXP_CM_LC]->tailp);
662 fprintf(fp, "ECMGC %.5f %10.5f %10.5f %10ld %10d %.6f\n",
663 cm->expA[EXP_CM_GC]->lambda, cm->expA[EXP_CM_GC]->mu_extrap, cm->expA[EXP_CM_GC]->mu_orig,
664 (long) (cm->expA[EXP_CM_GC]->dbsize + 0.5), cm->expA[EXP_CM_GC]->nrandhits, cm->expA[EXP_CM_GC]->tailp);
665 fprintf(fp, "ECMLI %.5f %10.5f %10.5f %10ld %10d %.6f\n",
666 cm->expA[EXP_CM_LI]->lambda, cm->expA[EXP_CM_LI]->mu_extrap, cm->expA[EXP_CM_LI]->mu_orig,
667 (long) (cm->expA[EXP_CM_LI]->dbsize + 0.5), cm->expA[EXP_CM_LI]->nrandhits, cm->expA[EXP_CM_LI]->tailp);
668 fprintf(fp, "ECMGI %.5f %10.5f %10.5f %10ld %10d %.6f\n",
669 cm->expA[EXP_CM_GI]->lambda, cm->expA[EXP_CM_GI]->mu_extrap, cm->expA[EXP_CM_GI]->mu_orig,
670 (long) (cm->expA[EXP_CM_GI]->dbsize + 0.5), cm->expA[EXP_CM_GI]->nrandhits, cm->expA[EXP_CM_GI]->tailp);
671 }
672
673 /* main model section */
674 fputs("CM\n", fp);
675
676 /* Create emit map if nec, so we can output map, consensus and rf info appropriately */
677 if(cm->emap == NULL) {
678 cm->emap = CreateEmitMap(cm);
679 if(cm->emap == NULL) ESL_EXCEPTION(eslEINVAL, "unable to create an emit map");
680 }
681
682 for (v = 0; v < cm->M; v++) {
683 nd = cm->ndidx[v];
684
685 /* Node line. node type and additional per-consensus position annotation */
686 if (cm->nodemap[nd] == v) {
687 fprintf(fp, "%45s[ %-4s %4d ]", "", Nodetype(cm->ndtype[nd]), nd);
688
689 /* additional annotation */
690 /* map (optional) */
691 if(cm->flags & CMH_MAP) {
692 if (cm->ndtype[nd] == MATP_nd) fprintf(fp, " %6d %6d", cm->map[cm->emap->lpos[nd]], cm->map[cm->emap->rpos[nd]]);
693 else if(cm->ndtype[nd] == MATL_nd) fprintf(fp, " %6d %6s", cm->map[cm->emap->lpos[nd]], "-");
694 else if(cm->ndtype[nd] == MATR_nd) fprintf(fp, " %6s %6d", "-", cm->map[cm->emap->rpos[nd]]);
695 else fprintf(fp, " %6s %6s", "-", "-");
696 }
697 else { /* no map annotation */
698 fprintf(fp, " %6s %6s", "-", "-");
699 }
700 /* consensus sequence (mandatory) */
701 if (cm->ndtype[nd] == MATP_nd) fprintf(fp, " %c %c", cm->consensus[cm->emap->lpos[nd]], cm->consensus[cm->emap->rpos[nd]]);
702 else if(cm->ndtype[nd] == MATL_nd) fprintf(fp, " %c %c", cm->consensus[cm->emap->lpos[nd]], '-');
703 else if(cm->ndtype[nd] == MATR_nd) fprintf(fp, " %c %c", '-', cm->consensus[cm->emap->rpos[nd]]);
704 else fprintf(fp, " %c %c", '-', '-');
705 /* RF (optional) */
706 if(cm->flags & CMH_RF) {
707 if (cm->ndtype[nd] == MATP_nd) fprintf(fp, " %c %c", cm->rf[cm->emap->lpos[nd]], cm->rf[cm->emap->rpos[nd]]);
708 else if(cm->ndtype[nd] == MATL_nd) fprintf(fp, " %c %c", cm->rf[cm->emap->lpos[nd]], '-');
709 else if(cm->ndtype[nd] == MATR_nd) fprintf(fp, " %c %c", '-', cm->rf[cm->emap->rpos[nd]]);
710 else fprintf(fp, " %c %c", '-', '-');
711 }
712 else { /* no RF annotation */
713 fprintf(fp, " %c %c", '-', '-');
714 }
715 fputs("\n", fp);
716 }
717
718 /* State line, w/ parents, children, dmin2, dmin1, dmax1, dmax2, transitions and emissions */
719 fprintf(fp, " %2s %5d %5d %1d %5d %5d %5d %5d %5d %5d ",
720 Statetype(cm->sttype[v]), v,
721 cm->plast[v], cm->pnum[v],
722 cm->cfirst[v], cm->cnum[v],
723 cm->qdbinfo->dmin2[v], cm->qdbinfo->dmin1[v],
724 cm->qdbinfo->dmax1[v], cm->qdbinfo->dmax2[v]);
725
726 /* Transitions */
727 if (cm->sttype[v] != B_st) {
728 for (x = 0; x < cm->cnum[v]; x++) {
729 fprintf(fp, "%7s ", prob2ascii(cm->t[v][x], 1.));
730 }
731 }
732 else {
733 x = 0;
734 }
735 for (; x < 6; x++) {
736 fprintf(fp, "%7s ", "");
737 }
738
739 /* Emissions */
740 if (cm->sttype[v] == MP_st) {
741 for (x = 0; x < cm->abc->K; x++) {
742 for (y = 0; y < cm->abc->K; y++) {
743 fprintf(fp, "%6s ", prob2ascii(cm->e[v][x*cm->abc->K+y], cm->null[x]*cm->null[y]));
744 }
745 }
746 }
747 else if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st || cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) {
748 for (x = 0; x < cm->abc->K; x++) {
749 fprintf(fp, "%6s ", prob2ascii(cm->e[v][x], cm->null[x]));
750 }
751 }
752 fputs("\n", fp);
753 }
754 fputs("//\n", fp);
755
756 /* print additional p7 hmms if any */
757 if(cm->flags & CMH_FP7 && cm->fp7 != NULL) {
758 p7_hmmfile_WriteASCII(fp, -1, cm->fp7);
759 }
760 return eslOK;
761 }
762
763
764 /* Function: cm_file_WriteBinary()
765 * Incept: EPN, Fri Jun 17 11:01:17 2011
766 * SRE, Wed Jan 3 13:50:26 2007 [Janelia] (p7_hmmfile_WriteBinary())
767 * Purpose: Writes an CM to a file in INFERNAL binary format.
768 *
769 * Legacy binary file formats will eventually be supported
770 * by specifying the <format> code, but currently only one
771 * valid format exists. Passing <-1> as format specifies
772 * the default current standard format; pass a valid
773 * code such as <CM_FILE_1a> to select a specific
774 * binary format.
775 *
776 * Returns: <eslOK> on success. File position of start of fp7 is
777 * sent back in <*opt_fp7_offset> if it is non-NULL. If no
778 * fp7 is written, (<*opt_fp7_offset> is 0) and caller will
779 * know no fp7 is written because (cm->fp7 == NULL) || (!
780 * (cm->flags & CMH_FP7)). <eslFAIL> if any writes fail
781 * (for instance, if disk fills up, which did happen during
782 * testing!).
783 *
784 * Throws: <eslEINVAL> if <format> isn't a valid 3.0 format code.
785 */
786 int
787 cm_file_WriteBinary(FILE *fp, int format, CM_t *cm, off_t *opt_fp7_offset)
788 {
789 int v, z;
790 off_t fp7_offset;
791
792 if((cm->flags & CMH_LOCAL_BEGIN) || (cm->flags & CMH_LOCAL_END)) cm_Fail("cm_file_WriteASCII(): CM is in local mode");
793
794 if (format == -1) format = CM_FILE_1a;
795
796 /* ye olde magic number */
797 if (format == CM_FILE_1a) { if (fwrite((char *) &(v1a_magic), sizeof(uint32_t), 1, fp) != 1) return eslFAIL; }
798 else ESL_EXCEPTION(eslEINVAL, "invalid CM file format code");
799
800 /* info necessary for sizes of things
801 */
802 if (fwrite((char *) &(cm->flags), sizeof(int), 1, fp) != 1) return eslFAIL;
803 if (fwrite((char *) &(cm->M), sizeof(int), 1, fp) != 1) return eslFAIL;
804 if (fwrite((char *) &(cm->nodes), sizeof(int), 1, fp) != 1) return eslFAIL;
805 if (fwrite((char *) &(cm->clen), sizeof(int), 1, fp) != 1) return eslFAIL;
806 if (fwrite((char *) &(cm->abc->type), sizeof(int), 1, fp) != 1) return eslFAIL;
807
808 /* main model section
809 */
810
811 if (fwrite((char *) cm->sttype, sizeof(char), cm->M, fp) != cm->M) return eslFAIL;
812 if (fwrite((char *) cm->ndidx, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
813 if (fwrite((char *) cm->stid, sizeof(char), cm->M, fp) != cm->M) return eslFAIL;
814 if (fwrite((char *) cm->cfirst, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
815 if (fwrite((char *) cm->cnum, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
816 if (fwrite((char *) cm->plast, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
817 if (fwrite((char *) cm->pnum, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
818 if (fwrite((char *) cm->nodemap, sizeof(int), cm->nodes, fp) != cm->nodes) return eslFAIL;
819 if (fwrite((char *) cm->ndtype, sizeof(char), cm->nodes, fp) != cm->nodes) return eslFAIL;
820 if (fwrite((char *) cm->qdbinfo->dmin1, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
821 if (fwrite((char *) cm->qdbinfo->dmax1, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
822 if (fwrite((char *) cm->qdbinfo->dmin2, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
823 if (fwrite((char *) cm->qdbinfo->dmax2, sizeof(int), cm->M, fp) != cm->M) return eslFAIL;
824
825 for (v = 0; v < cm->M; v++) {
826 if (fwrite((char *) cm->t[v], sizeof(float), MAXCONNECT, fp) != MAXCONNECT) return eslFAIL;
827 if (fwrite((char *) cm->e[v], sizeof(float), cm->abc->K*cm->abc->K, fp) != (cm->abc->K*cm->abc->K)) return eslFAIL;
828 }
829
830 /* annotation section
831 */
832 if ( write_bin_string(fp, cm->name) != eslOK) return eslFAIL;
833 if ((cm->flags & CMH_ACC) && (write_bin_string(fp, cm->acc) != eslOK)) return eslFAIL;
834 if ((cm->flags & CMH_DESC) && (write_bin_string(fp, cm->desc) != eslOK)) return eslFAIL;
835 if ((cm->flags & CMH_RF) && (fwrite((char *) cm->rf, sizeof(char), cm->clen+2, fp) != cm->clen+2)) return eslFAIL; /* +2: 1..clen and trailing \0 */
836 if ((cm->flags & CMH_CONS) && (fwrite((char *) cm->consensus, sizeof(char), cm->clen+2, fp) != cm->clen+2)) return eslFAIL; /* consensus is mandatory */
837 if ((cm->flags & CMH_MAP) && (fwrite((char *) cm->map, sizeof(int), cm->clen+1, fp) != cm->clen+1)) return eslFAIL; /* +2: 1..clen and trailing \0 */
838 if (fwrite((char *) &(cm->W), sizeof(int), 1, fp) != 1) return eslFAIL;
839
840 if ((write_bin_string(fp, cm->ctime)) != eslOK) return eslFAIL;
841 if ((write_bin_string(fp, cm->comlog)) != eslOK) return eslFAIL;
842
843 if (fwrite((char *) &(cm->pbegin), sizeof(float), 1, fp) != 1) return eslFAIL;
844 if (fwrite((char *) &(cm->pend), sizeof(float), 1, fp) != 1) return eslFAIL;
845 if (fwrite((char *) &(cm->beta_W), sizeof(double), 1, fp) != 1) return eslFAIL;
846 if (fwrite((char *) &(cm->qdbinfo->beta1), sizeof(double), 1, fp) != 1) return eslFAIL;
847 if (fwrite((char *) &(cm->qdbinfo->beta2), sizeof(double), 1, fp) != 1) return eslFAIL;
848 if (fwrite((char *) &(cm->null2_omega), sizeof(float), 1, fp) != 1) return eslFAIL;
849 if (fwrite((char *) &(cm->null3_omega), sizeof(float), 1, fp) != 1) return eslFAIL;
850 if (fwrite((char *) &(cm->el_selfsc), sizeof(float), 1, fp) != 1) return eslFAIL;
851 if (fwrite((char *) &(cm->nseq), sizeof(int), 1, fp) != 1) return eslFAIL;
852 if (fwrite((char *) &(cm->eff_nseq), sizeof(float), 1, fp) != 1) return eslFAIL;
853 if (fwrite((char *) &(cm->checksum), sizeof(uint32_t), 1, fp) != 1) return eslFAIL;
854 if (fwrite((char *) cm->null, sizeof(float), cm->abc->K, fp) != cm->abc->K) return eslFAIL;
855
856 /* Rfam cutoffs
857 */
858 if ((cm->flags & CMH_GA) && (fwrite((char *) &(cm->ga), sizeof(float), 1, fp) != 1)) return eslFAIL;
859 if ((cm->flags & CMH_TC) && (fwrite((char *) &(cm->tc), sizeof(float), 1, fp) != 1)) return eslFAIL;
860 if ((cm->flags & CMH_NC) && (fwrite((char *) &(cm->nc), sizeof(float), 1, fp) != 1)) return eslFAIL;
861
862 /* E-value parameters
863 */
864 if (cm->flags & CMH_FP7) { /* should always be true */
865 if (fwrite((char *) &(cm->fp7_evparam[CM_p7_GFMU]), sizeof(float), 1, fp) != 1) return eslFAIL;
866 if (fwrite((char *) &(cm->fp7_evparam[CM_p7_GFLAMBDA]), sizeof(float), 1, fp) != 1) return eslFAIL;
867 }
868 if (cm->flags & CMH_EXPTAIL_STATS) {
869 long dbsize_long;
870 for(z = 0; z < EXP_NMODES; z++) {
871 /* make sure our dbsize values can be cast to a long reliably,
872 * even on 32 bit systems (<= 2 Gb), they certainly should be,
873 * max value in cmcalibrate is 160Mb. (This is related to bug
874 * i31.)
875 */
876 if(cm->expA[z]->dbsize > (2000. * 1000000.)) ESL_EXCEPTION(eslEINVAL, "invalid dbsize (too big)");
877 dbsize_long = (long) cm->expA[z]->dbsize + 0.5;
878 if (fwrite((char *) &(cm->expA[z]->lambda), sizeof(double), 1, fp) != 1) return eslFAIL;
879 if (fwrite((char *) &(cm->expA[z]->mu_extrap), sizeof(double), 1, fp) != 1) return eslFAIL;
880 if (fwrite((char *) &(cm->expA[z]->mu_orig), sizeof(double), 1, fp) != 1) return eslFAIL;
881 if (fwrite((char *) &(dbsize_long), sizeof(long), 1, fp) != 1) return eslFAIL;
882 if (fwrite((char *) &(cm->expA[z]->nrandhits), sizeof(int), 1, fp) != 1) return eslFAIL;
883 if (fwrite((char *) &(cm->expA[z]->tailp), sizeof(double), 1, fp) != 1) return eslFAIL;
884 }
885 }
886
887 /* finally, write the filter p7 HMM */
888 fp7_offset = 0;
889 /* fp7_offset remains 0 if we don't write a fp7, caller will know that no
890 * fp7 was written if (! (cm->flags & CMH_FP7)) || cm->fp7 == NULL
891 */
892 if(cm->flags & CMH_FP7 && cm->fp7 != NULL) {
893 if(opt_fp7_offset != NULL) {
894 /* figure out offset only if we are returning it, otherwise it's irrelevant and can cause a failure
895 * in some cases (e.g. cmconvert -b test.cm | cat > /dev/null), this was bug i41
896 */
897 if((fp7_offset = ftello(fp)) == -1) ESL_EXCEPTION(eslEINVAL, "failed to determine file position for p7 filter");
898 }
899 p7_hmmfile_WriteBinary(fp, -1, cm->fp7);
900 }
901
902 if(opt_fp7_offset != NULL) *opt_fp7_offset = fp7_offset;
903 return eslOK;
904 }
905 /*----------------- end, save file output ----------------------*/
906
907
908 /* Function: write_bin_string()
909 * Date: SRE, Wed Oct 29 13:49:27 1997 [TWA 721 over Canada]
910 *
911 * Purpose: Write a string in binary save format: an integer
912 * for the string length (including \0), followed by
913 * the string.
914 *
915 * Return: <eslOK> on success;
916 * <eslFAIL> if a write fails due to system error, such
917 * as a filled disk (as happened in testing).
918 */
919 static int
920 write_bin_string(FILE *fp, char *s)
921 {
922 int len;
923 if (s != NULL)
924 {
925 len = strlen(s) + 1;
926 if (fwrite((char *) &len, sizeof(int), 1, fp) != 1) return eslFAIL;
927 if (fwrite((char *) s, sizeof(char), len, fp) != len) return eslFAIL;
928 }
929 else
930 {
931 len = 0;
932 if (fwrite((char *) &len, sizeof(int), 1, fp) != 1) return eslFAIL;
933 }
934 return eslOK;
935 }
936
937 /*****************************************************************
938 * 3. API for reading CMs in various formats.
939 *****************************************************************/
940
941 /* Function: cm_file_Read()
942 * Incept: EPN, Tue Jun 21 10:41:54 2011
943 * SRE, Sat Jan 6 18:04:58 2007 [Casa de Gatos] (p7_hmmfile_Read())
944 *
945 * Purpose: Read the next CM from open save file <cmfp>, and
946 * optionally return this newly allocated CM in <opt_cm>.
947 * (The optional return is so that an application is
948 * only interested in whether the file contains a valid
949 * CM or not -- for example, to verify that a file contains
950 * only a single CM instead of a database of them.)
951 *
952 * Read a p7 filter HMM (if one exists) for the CM if
953 * <read_fp7> is TRUE, else, don't read one. This allows us
954 * to read only the CM if we're using a pressed file in
955 * cmscan and already have read the p7 filter HMM
956 * previously. However be careful with this, because caller
957 * will have problems if it tries to read the file
958 * sequentially with <read_fp7> as FALSE because file
959 * offset will not be at the beginning of the next CM upon
960 * return, it will be at the beginning of the p7 filter for
961 * the last CM read, if one exists.
962 *
963 * Caller may or may not already know what alphabet the CM
964 * is expected to be in. A reference to the pointer to the
965 * current alphabet is passed in <*ret_abc>. If the alphabet
966 * is unknown, pass <*ret_abc = NULL>, and when the
967 * new HMM is read, an appropriate new alphabet object is
968 * allocated and passed back to the caller in <*ret_abc>.
969 * If the alphabet is already known, <ret_abc> points to
970 * that object ptr, and the new CM's alphabet type is
971 * verified to agree with it. This mechanism allows an
972 * application to let the first CM determine the alphabet
973 * type for the application, while still keeping the
974 * alphabet under the application's scope of control.
975 *
976 * Returns: <eslOK> on success, and the newly allocated CM is
977 * optionally returned via <opt_cm>. Additionally, if
978 * <ret_abc> pointed to <NULL>, it now points to a newly
979 * allocated alphabet.
980 *
981 * Returns <eslEOF> if no CMs remain in the file; this may
982 * indicate success or failure, depending on what the
983 * caller is expecting.
984 *
985 * Returns <eslEFORMAT> on any format problems, including
986 * premature end of data or bad magic at the start of a
987 * binary file. An informative error message is left in
988 * <cmfp->errbuf>; the filename (fully qualified, if opened
989 * in a directory specified by an <env> list) is in
990 * <cmfp->fname>; and if <cmfp->efp> is non-<NULL>, the CM
991 * file is in an ASCII text format, and the caller may also
992 * obtain the line number at which the format error was
993 * detected, in <cmfp->efp->linenumber>, and use it to
994 * format informative output for a user.
995 *
996 * Returns <eslEINCOMPAT> if the caller passed a known
997 * alphabet (a non-<NULL> <*ret_abc>), but the alphabet
998 * of the CM doesn't match this expectation.
999 *
1000 * Upon any return that is not <eslOK>, <*opt_cm> is
1001 * <NULL> and <*ret_abc> is left unchanged from what caller
1002 * passed it as.
1003 *
1004 * Throws: <eslEMEM> upon an allocation error.
1005 * <eslESYS> on failure of other system calls, such
1006 * as file positioning functions (<fseeko()> or <ftello()>.
1007 */
1008 int
1009 cm_file_Read(CM_FILE *cmfp, int read_fp7, ESL_ALPHABET **ret_abc, CM_t **opt_cm)
1010 {
1011 /* A call to SSI to remember file position may eventually go here. */
1012 return (*cmfp->parser)(cmfp, read_fp7, ret_abc, opt_cm);
1013 }
1014
1015 /* Function: cm_file_PositionByKey()
1016 * Synopsis: Use SSI to reposition file to start of named CM.
1017 * Incept: EPN, Tue Jun 21 10:46:33 2011
1018 * SRE, Mon Jun 18 10:57:15 2007 [Janelia] (p7_hmmfile_PositionByKey())
1019 *
1020 * Purpose: Reposition <cmfp> so the next CM we read will be the
1021 * one named (or accessioned) <key>.
1022 *
1023 * Returns: <eslOK> on success.
1024 *
1025 * Returns <eslENOTFOUND> if <key> isn't found in the index for
1026 * <cmfp>.
1027 *
1028 * Returns <eslEFORMAT> is something goes wrong trying to
1029 * read the index, indicating a file format problem in the
1030 * SSI file.
1031 *
1032 * In the event of either error, the state of <cmfp> is left
1033 * unchanged.
1034 *
1035 * Throws: <eslEMEM> on allocation failure, or <eslESYS> on system i/o
1036 * call failure, or <eslEINVAL> if <cmfp> doesn't have an SSI
1037 * index or is not a seekable stream.
1038 */
1039 int
1040 cm_file_PositionByKey(CM_FILE *cmfp, const char *key)
1041 {
1042 uint16_t fh;
1043 off_t offset;
1044 int status;
1045
1046 if (cmfp->ssi == NULL) ESL_EXCEPTION(eslEINVAL, "Need an open SSI index to call cm_file_PositionByKey()");
1047 if ((status = esl_ssi_FindName(cmfp->ssi, key, &fh, &offset, NULL, NULL)) != eslOK) return status;
1048 if (fseeko(cmfp->f, offset, SEEK_SET) != 0) ESL_EXCEPTION(eslESYS, "fseek failed");
1049
1050 cmfp->newly_opened = FALSE; /* because we're poised on the magic number, and must read it */
1051 return eslOK;
1052 }
1053
1054
1055 /* Function: cm_file_Position()
1056 * Synopsis: Reposition file to a given offset.
1057 * Incept: EPN, Tue Jun 21 10:47:48 2011
1058 * MSF Wed Nov 4, 2009 [Janelia] (p7_hmmfile_Position())
1059 *
1060 * Purpose: Reposition <cmfp> to position <offset>.
1061 *
1062 * Returns: <eslOK> on success.
1063 *
1064 * In the event an error, the state of <cmfp> is left
1065 * unchanged.
1066 *
1067 * Throws: <eslESYS> on system i/o call failure, or <eslEINVAL> if
1068 * <cmfp> is not a seekable stream.
1069 */
1070 int
1071 cm_file_Position(CM_FILE *cmfp, const off_t offset)
1072 {
1073 if (fseeko(cmfp->f, offset, SEEK_SET) != 0) ESL_EXCEPTION(eslESYS, "fseek failed");
1074
1075 cmfp->newly_opened = FALSE; /* because we're poised on the magic number, and must read it */
1076 return eslOK;
1077 }
1078
1079 /*------------------- end, input API ----------------------------*/
1080
1081
1082 /*****************************************************************
1083 * 4. API for reading/writing p7 HMMs/profiles to filter for CMs.
1084 *****************************************************************/
1085
1086 /* Function: cm_p7_hmmfile_Read()
1087 * Incept: EPN, Fri Jul 22 09:21:53 2011
1088 *
1089 * Purpose: Position an HMM save file <cmfp->hfp> to <offset> and
1090 * read an HMM from it at that position. Return the HMM
1091 * in <ret_hmm>.
1092 *
1093 * Caller must already know the alphabet the HMM is
1094 * expected to be in, passed in as <abc>. If the alphabet
1095 * for the HMM is of a different type than <abc>, we return
1096 * eslEINCOMPAT.
1097 *
1098 * Returns: <eslOK> on success, and the newly allocated HMM is
1099 * in <ret_hmm>.
1100 *
1101 * Returns <eslEOF> if no HMMs remain in the file; this
1102 * is a failure, as we expect there to be one at <offset>.
1103 *
1104 * Returns <eslEFORMAT> on any format problems, including
1105 * premature end of data or bad magic at the start of a
1106 * binary file. An informative error message is left in
1107 * <cmfp->errbuf>; the filename (fully qualified, if opened
1108 * in a directory specified by an <env> list) is in
1109 * <cmfp->fname>; and if <cmfp->hfp->efp> is non-<NULL>,
1110 * the HMM file is in an ASCII text format, and the caller
1111 * may also obtain the line number at which the format
1112 * error was detected, in <cmfp->hfp->efp->linenumber>, and
1113 * use it to format informative output for a user.
1114 *
1115 * Returns <eslEINCOMPAT> if the alphabet of the HMM doesn't
1116 * match the expectation in <abc>.
1117 *
1118 * Upon any return that is not <eslOK>, <*ret_hmm> is
1119 * <NULL>.
1120 *
1121 * Throws: <eslEMEM> upon an allocation error.
1122 * <eslESYS> on failure of other system calls, such
1123 * as file positioning functions (<fseeko()> or <ftello()>.
1124 */
1125 int
1126 cm_p7_hmmfile_Read(CM_FILE *cmfp, ESL_ALPHABET *abc, off_t offset, P7_HMM **ret_hmm)
1127 {
1128 int status;
1129 P7_HMM *hmm = NULL;
1130 char *tok1 = NULL;
1131 uint32_t magic;
1132
1133 #ifdef HMMER_THREADS
1134 if(!cmfp->hfp->do_stdin && !cmfp->hfp->do_gzip) {
1135 /* lock the mutex to prevent other threads from reading the file at the same time */
1136 if (cmfp->hfp->syncRead) {
1137 if (pthread_mutex_lock (&cmfp->hfp->readMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex lock failed");
1138 }
1139 }
1140 #endif
1141 if(!cmfp->hfp->do_stdin && !cmfp->hfp->do_gzip) {
1142 if (fseeko(cmfp->hfp->f, offset, SEEK_SET) != 0) ESL_XFAIL(eslEINCOMPAT, cmfp->errbuf, "Failed to set position for HMM file parser");
1143 }
1144
1145 /* set the parser if it's unset (which it is only for first HMM read from cmfp->hfp) */
1146 if(cmfp->hfp->parser == NULL) {
1147 if(cmfp->is_binary) { /* CM file is binary, HMM file must be too */
1148 if (! fread((char *) &(magic), sizeof(uint32_t), 1, cmfp->hfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Failed to read magic number at start of p7 additional filters");
1149 if (magic == v3a_magic) { cmfp->hfp->format = p7_HMMFILE_3a; cmfp->hfp->parser = read_bin30hmm; }
1150 else if (magic == v3b_magic) { cmfp->hfp->format = p7_HMMFILE_3b; cmfp->hfp->parser = read_bin30hmm; }
1151 else if (magic == v3c_magic) { cmfp->hfp->format = p7_HMMFILE_3c; cmfp->hfp->parser = read_bin30hmm; }
1152 else if (magic == v3d_magic) { cmfp->hfp->format = p7_HMMFILE_3d; cmfp->hfp->parser = read_bin30hmm; }
1153 else if (magic == v3e_magic) { cmfp->hfp->format = p7_HMMFILE_3e; cmfp->hfp->parser = read_bin30hmm; }
1154 else if (magic == v3f_magic) { cmfp->hfp->format = p7_HMMFILE_3f; cmfp->hfp->parser = read_bin30hmm; }
1155 else ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Unknown magic number for p7 HMM filter");
1156 }
1157 else { /* CM file is ascii, HMM file must be too */
1158 if(cmfp->hfp->efp != NULL) ESL_XFAIL(eslEINVAL, cmfp->errbuf, "HMM ascii file parsers are out of sync");
1159 if ((cmfp->hfp->efp = esl_fileparser_Create(cmfp->hfp->f)) == NULL) { status = eslEMEM; goto ERROR; }
1160 if ((status = esl_fileparser_SetCommentChar(cmfp->hfp->efp, '#')) != eslOK) goto ERROR;
1161 if ((status = esl_fileparser_GetToken(cmfp->hfp->efp, &tok1, NULL)) != eslOK) goto ERROR;
1162
1163 if (strcmp("HMMER3/f", tok1) == 0) { cmfp->hfp->format = p7_HMMFILE_3f; cmfp->hfp->parser = read_asc30hmm; }
1164 else if (strcmp("HMMER3/e", tok1) == 0) { cmfp->hfp->format = p7_HMMFILE_3e; cmfp->hfp->parser = read_asc30hmm; }
1165 else if (strcmp("HMMER3/d", tok1) == 0) { cmfp->hfp->format = p7_HMMFILE_3d; cmfp->hfp->parser = read_asc30hmm; }
1166 else if (strcmp("HMMER3/c", tok1) == 0) { cmfp->hfp->format = p7_HMMFILE_3c; cmfp->hfp->parser = read_asc30hmm; }
1167 else if (strcmp("HMMER3/b", tok1) == 0) { cmfp->hfp->format = p7_HMMFILE_3b; cmfp->hfp->parser = read_asc30hmm; }
1168 else if (strcmp("HMMER3/a", tok1) == 0) { cmfp->hfp->format = p7_HMMFILE_3a; cmfp->hfp->parser = read_asc30hmm; }
1169 else ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Unknown format for p7 HMM filter");
1170 }
1171 }
1172
1173 /* read the HMM */
1174 status = p7_hmmfile_Read(cmfp->hfp, &abc, &hmm);
1175 if (status == eslEOF) ESL_XFAIL(status, cmfp->errbuf, "failed to read profile HMM piece, CM file may be truncated?");
1176 else if (status == eslEFORMAT) ESL_XFAIL(status, cmfp->errbuf, "bad file format for HMM filter");
1177 else if (status == eslEINCOMPAT) ESL_XFAIL(status, cmfp->errbuf, "HMM filters are of different alphabets");
1178 else if (status != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Unexpected error in reading HMM filters");
1179
1180 #ifdef HMMER_THREADS
1181 if(!cmfp->hfp->do_stdin && !cmfp->hfp->do_gzip) {
1182 if (cmfp->hfp->syncRead) {
1183 if (pthread_mutex_unlock (&cmfp->hfp->readMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
1184 }
1185 }
1186 #endif
1187
1188 *ret_hmm = hmm;
1189 return status;
1190
1191 ERROR:
1192 #ifdef HMMER_THREADS
1193 if(cmfp->hfp->f != NULL) {
1194 if (cmfp->hfp->syncRead) {
1195 if (pthread_mutex_unlock (&cmfp->hfp->readMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
1196 }
1197 }
1198 #endif
1199
1200 if(hmm != NULL) p7_hmm_Destroy(hmm);
1201 *ret_hmm = NULL;
1202 return status;
1203 }
1204
1205 /* Function: cm_p7_oprofile_Write()
1206 * Synopsis: Write an optimized p7 profile in two files.
1207 * Incept: EPN, Fri Jul 8 06:57:50 2011
1208 *
1209 * Purpose: Write the MSV filter part of <om> to open binary stream
1210 * <ffp>, and the rest of the model to <pfp>. These two
1211 * streams will typically be <.i1f> and <.i1p> files
1212 * being created by cmpress.
1213 *
1214 * Most of the work is done by p7_oprofile_Write(). This
1215 * function is only necessary to write five pieces of data
1216 * that are specific to CMs to the MSV file and one
1217 * to the profile file:
1218 *
1219 * 1. <cm_offset>: the position in the corresponding <.i1m>
1220 * file at which the CM corresponding to the optimized
1221 * profile can be found.
1222 *
1223 * 2. <cm_clen>: consensus length of the CM this p7 is a
1224 * filter for.
1225 *
1226 * 3. <cm_W>: window length of the CM this p7 is a
1227 * filter for.
1228 *
1229 * 4. <cm_nbp>: number of base pairs in CM (if 0, pipeline
1230 * will be run in HMM only mode.
1231 *
1232 * 5. <gfmu>: glocal forward mu for this <om>
1233 *
1234 * 6. <gflambda>: glocal forward lambda for this <om>
1235 *
1236 * Args: ffp - open binary stream for saving MSV filter part
1237 * pfp - open binary stream for saving rest of profile
1238 * cm_offset - disk offset for CM in <.i1m> file that corresponds
1239 * to this profile
1240 * cm_clen - consensus length of CM corresponding to this om (usually om->M)
1241 * cm_W - window length for the CM
1242 * cm_nbp - number of basepairs in the CM
1243 * gfmu - E value mu param for glocal forward for this om
1244 * gflambda - E value lambda param for glocal forward for this om
1245 * om - optimized profile to save
1246 *
1247 * Returns: <eslOK> on success.
1248 *
1249 * Returns <eslFAIL> on any write failure; for example,
1250 * if disk is full.
1251 *
1252 * Throws: (no abnormal error conditions)
1253 */
1254 int
1255 cm_p7_oprofile_Write(FILE *ffp, FILE *pfp, off_t cm_offset, int cm_clen, int cm_W, int cm_nbp, float gfmu, float gflambda, P7_OPROFILE *om)
1256 {
1257 /* <ffp> is the part of the oprofile that MSVFilter() needs */
1258 if (fwrite((char *) &(v1a_fmagic), sizeof(uint32_t), 1, ffp) != 1) return eslFAIL;
1259 if (fwrite((char *) &(cm_offset), sizeof(off_t), 1, ffp) != 1) return eslFAIL;
1260 if (fwrite((char *) &(cm_clen), sizeof(int), 1, ffp) != 1) return eslFAIL;
1261 if (fwrite((char *) &(cm_W), sizeof(int), 1, ffp) != 1) return eslFAIL;
1262 if (fwrite((char *) &(cm_nbp), sizeof(int), 1, ffp) != 1) return eslFAIL;
1263 if (fwrite((char *) &(gfmu), sizeof(float), 1, ffp) != 1) return eslFAIL;
1264 if (fwrite((char *) &(gflambda), sizeof(float), 1, ffp) != 1) return eslFAIL;
1265
1266 /* <pfp> gets the rest of the oprofile, we don't need to write anything extra, so
1267 * p7_oprofile_Write handles it all */
1268
1269 /* pass to p7_oprofile_Write to do the rest */
1270 return p7_oprofile_Write(ffp, pfp, om);
1271 }
1272
1273
1274 /* Function: cm_p7_oprofile_Position()
1275 * Synopsis: Reposition the hmm filter file part of a CM file to an offset.
1276 * Incept: EPN, Fri Jul 22 10:49:41 2011
1277 * MSF, Thu Oct 15, 2009 [Janelia] (p7_oprofile_Position())
1278 *
1279 * Purpose: Reposition an open <cmfp->ffp> to offset <offset>.
1280 * <offset> would usually be the first byte of a
1281 * desired hmm record.
1282 *
1283 * Returns: <eslOK> on success;
1284 * <eslEOF> if no data can be read from this position.
1285 *
1286 * Throws: <eslEINVAL> if the <sqfp> is not positionable.
1287 * <eslEFORMAT> if no msv profile opened.
1288 * <eslESYS> if the fseeko() call fails.
1289 */
1290 int
1291 cm_p7_oprofile_Position(CM_FILE *cmfp, off_t offset)
1292 {
1293 if (cmfp->ffp == NULL) ESL_EXCEPTION(eslEFORMAT, cmfp->errbuf, "no MSV profile file; cmpress probably wasn't run");
1294 if (cmfp->do_stdin) ESL_EXCEPTION(eslEINVAL, "can't Position() in standard input");
1295 if (cmfp->do_gzip) ESL_EXCEPTION(eslEINVAL, "can't Position() in a gzipped file");
1296 if (offset < 0) ESL_EXCEPTION(eslEINVAL, "bad offset");
1297
1298 if (fseeko(cmfp->ffp, offset, SEEK_SET) != 0) ESL_EXCEPTION(eslESYS, "fseeko() failed");
1299
1300 return eslOK;
1301 }
1302
1303
1304 /* Function: cm_p7_oprofile_ReadMSV()
1305 * Synopsis: Read MSV filter part of an optimized profile.
1306 * Incept: EPN, Fri Jul 8 08:19:58 2011
1307 *
1308 * Purpose: Read the MSV filter part of a p7 filter profile from the
1309 * <.i1f> file associated with an open CM file <cmfp>.
1310 * Allocate a new model, populate it with this minimal
1311 * MSV filter information, and return a pointer to them
1312 * in <*ret_om>. Return pointers to additional information
1313 * on the model in <*ret_cm_offset>, <*ret_cm_clen> and
1314 * <*ret_cm_W>.
1315 *
1316 * Our alphabet may get set by the first HMM we read. If
1317 * <*byp_abc> is <NULL> at start, create a new alphabet and
1318 * return a pointer to it in <*byp_abc>. If <*byp_abc> is
1319 * non-<NULL>, it is assumed to be a pointer to an existing
1320 * alphabet; we verify that the HMM's alphabet matches it
1321 * and <*ret_abc> isn't changed. This is the same
1322 * convention used by <cm_file_Read()>.
1323 *
1324 * The <.i1f> file was opened automatically, if it existed,
1325 * when the CM file was opened with <cm_file_Open()>.
1326 *
1327 * When no more HMMs remain in the file, return <eslEOF>.
1328 *
1329 * Most of the work is done by p7_oprofile_ReadMSV(). This
1330 * function is only necessary to read five pieces of data
1331 * that are specific to CMs. See cm_p7_oprofile_Write()
1332 * for more explanation.
1333 *
1334 * If <read_scores> is TRUE: read all the MSV data
1335 * (including scores) using p7_oprofile_ReadMSV().
1336 * If <read_scores> is FALSE: read only the MSV info
1337 * (no scores) using p7_oprofile_ReadInfoMSV().
1338 *
1339 *
1340 * Args: cmfp - open CM file, with associated .i1p file
1341 * byp_abc - BYPASS: <*byp_abc == ESL_ALPHABET *> if known;
1342 * <*byp_abc == NULL> if desired;
1343 * <NULL> if unwanted.
1344 * ret_cm_offset - RETURN: offset of CM each om corresponds to
1345 * ret_cm_clen - RETURN: clen of CM each om corresponds to
1346 * ret_cm_W - RETURN: W of CM each om corresponds to
1347 * ret_cm_nbp - RETURN: number of bps in CM each om corresponds to
1348 * ret_gfmu - RETURN: glocal fwd mu the om
1349 * ret_gflambda - RETURN: glocal forward lambda the om
1350 * ret_om - RETURN: the read <om> with MSV filter
1351 * data filled in.
1352 *
1353 * Returns: <eslOK> on success. <*ret_om> is allocated here;
1354 * caller free's with <p7_oprofile_Destroy()>.
1355 * <*byp_abc> is allocated here if it was requested;
1356 * caller free's with <esl_alphabet_Destroy()>.
1357 *
1358 * Returns <eslEFORMAT> if <cmfp> has no <.i1f> file open,
1359 * or on any parsing error.
1360 *
1361 * Returns <eslEINCOMPAT> if the HMM we read is incompatible
1362 * with the existing alphabet <*byp_abc> led us to expect.
1363 *
1364 * On any returned error, <cmfp->msv_errbuf> contains an
1365 * informative error message.
1366 *
1367 * Throws: <eslEMEM> on allocation error.
1368 */
1369 int
1370 cm_p7_oprofile_ReadMSV(CM_FILE *cmfp, int read_scores, ESL_ALPHABET **byp_abc, off_t *ret_cm_offset, int *ret_cm_clen, int *ret_cm_W, int *ret_cm_nbp, float *ret_gfmu, float *ret_gflambda, P7_OPROFILE **ret_om)
1371 {
1372 int status = eslOK; /* return status */
1373 uint32_t magic; /* magic number used to verify format */
1374 off_t cm_offset; /* offset of the corresponding CM in cmfp->mfp */
1375 int cm_clen; /* CM consensus length (will likely be om->M) */
1376 int cm_W; /* CM window length (will likely differ from om->max_length) */
1377 int cm_nbp; /* number of basepairs in CM (if 0 pipeline will be run in HMM only mode) */
1378 float gfmu; /* glocal fwd mu parameter for current hmm */
1379 float gflambda; /* glocal fwd lambda parameter for current hmm */
1380 P7_OPROFILE *om = NULL; /* the om we've read */
1381
1382 /* We use cmfp->msv_errbuf here instead of cmfp->errbuf to avoid a
1383 * thread race in cmscan with read_bin_1p1_cm which also uses
1384 * cmfp->errbuf.
1385 */
1386 cmfp->msv_errbuf[0] = '\0'; // don't touch cmfp->errbuf, master is exclusively using ReadMSV, workers are using cm_file_Read()
1387 if (cmfp->ffp == NULL) ESL_XFAIL(eslEFORMAT, cmfp->msv_errbuf, "no MSV profile file; cmpress probably wasn't run");
1388
1389 if (feof(cmfp->ffp)) { status = eslEOF; goto ERROR; } /* normal EOF: no more profiles */
1390 if (! fread( (char *) &magic, sizeof(uint32_t), 1, cmfp->ffp)) { status = eslEOF; goto ERROR; }
1391 if (magic != v1a_fmagic) ESL_XFAIL(eslEFORMAT, cmfp->msv_errbuf, "bad magic; not a CM database?");
1392
1393 if (! fread( (char *) &cm_offset, sizeof(off_t), 1, cmfp->ffp)) ESL_XFAIL(eslEFORMAT, cmfp->msv_errbuf, "failed to read CM offset");
1394 if (! fread( (char *) &cm_clen, sizeof(int), 1, cmfp->ffp)) ESL_XFAIL(eslEFORMAT, cmfp->msv_errbuf, "failed to read CM consensus length");
1395 if (! fread( (char *) &cm_W, sizeof(int), 1, cmfp->ffp)) ESL_XFAIL(eslEFORMAT, cmfp->msv_errbuf, "failed to read CM window length (W)");
1396 if (! fread( (char *) &cm_nbp, sizeof(int), 1, cmfp->ffp)) ESL_XFAIL(eslEFORMAT, cmfp->msv_errbuf, "failed to read CM number of bps");
1397 if (! fread( (char *) &gfmu, sizeof(int), 1, cmfp->ffp)) ESL_XFAIL(eslEFORMAT, cmfp->msv_errbuf, "failed to read glocal fwd mu parameter");
1398 if (! fread( (char *) &gflambda, sizeof(int), 1, cmfp->ffp)) ESL_XFAIL(eslEFORMAT, cmfp->msv_errbuf, "failed to read glocal fwd lambda parameter");
1399
1400 /* move HMM file position (cmfp->hfp->ffp) to where we are in CM file (cmfp->ffp) */
1401 if (fseeko(cmfp->hfp->ffp, ftello(cmfp->ffp), SEEK_SET) != 0) ESL_XFAIL(eslEINCOMPAT, cmfp->msv_errbuf, "failed to set position for HMM file parser (MSV)");
1402 if (read_scores) {
1403 if ((status = p7_oprofile_ReadMSV(cmfp->hfp, byp_abc, &om)) != eslOK) ESL_XFAIL(status, cmfp->msv_errbuf, "failed to read MSV filter model");
1404 }
1405 else {
1406 if ((status = p7_oprofile_ReadInfoMSV(cmfp->hfp, byp_abc, &om)) != eslOK) ESL_XFAIL(status, cmfp->msv_errbuf, "failed to read MSV info");
1407 }
1408 /* move CM file position (cmfp->ffp) to where we are in HMM file (cmfp->hfp->ffp) */
1409 if (fseeko(cmfp->ffp, ftello(cmfp->hfp->ffp), SEEK_SET) != 0) ESL_XFAIL(eslEINCOMPAT, cmfp->msv_errbuf, "Failed to set position for CM file parser (MSV)");
1410
1411 if(ret_cm_offset != NULL) *ret_cm_offset = cm_offset;
1412 if(ret_cm_clen != NULL) *ret_cm_clen = cm_clen;
1413 if(ret_cm_W != NULL) *ret_cm_W = cm_W;
1414 if(ret_cm_nbp != NULL) *ret_cm_nbp = cm_nbp;
1415 if(ret_gfmu != NULL) *ret_gfmu = gfmu;
1416 if(ret_gflambda != NULL) *ret_gflambda = gflambda;
1417 if(ret_om != NULL) *ret_om = om;
1418 return eslOK;
1419
1420 ERROR:
1421 if (om != NULL) p7_oprofile_Destroy(om);
1422 if (ret_om != NULL) *ret_om = NULL;
1423 return status;
1424 }
1425
1426 /* Function: cm_p7_oprofile_ReadBlockMSV()
1427 * Synopsis: Read the next block of MSV filter parts from a HMM file.
1428 * Incept: EPN, Thu Jul 21 04:44:19 2011
1429 *
1430 * Purpose: Reads a block of the MSV filter parts of optimized
1431 * p7 profiles from open cm file <cmfp> into
1432 * <hmmBlock>.
1433 *
1434 * Most of the work is done by p7_oprofile_ReadMSV(). This
1435 * function is only necessary to read five pieces of data
1436 * that are specific to CMs. See cm_p7_oprofile_Write()
1437 * for more explanation.
1438 *
1439 * Args: cmfp - open CM file, with associated .i1f file
1440 * idx0 - the index of the next profile in the file (0 if first profile)
1441 * byp_abc - BYPASS: <*byp_abc == ESL_ALPHABET *> if known;
1442 * <*byp_abc == NULL> if desired;
1443 * <NULL> if unwanted.
1444 * hmmBlock- RETURN: the block of profiles.
1445 *
1446 * Returns: <eslOK> on success; block in <hmmBlock>.
1447 *
1448 * Returns <eslEFORMAT> if <cmfp> has no <.i1f> file open,
1449 * or on any parsing error.
1450 *
1451 * Returns <eslEINCOMPAT> if any HMM we read is incompatible
1452 * with the existing alphabet <*byp_abc> led us to expect.
1453 *
1454 * Returns <eslEOF> when there is no profiles left in the
1455 * file (including first attempt to read an empty file).
1456 */
1457 int
1458 cm_p7_oprofile_ReadBlockMSV(CM_FILE *cmfp, int64_t idx0, ESL_ALPHABET **byp_abc, CM_P7_OM_BLOCK *hmmBlock)
1459 {
1460 int status = eslOK; /* return status */
1461 int i;
1462
1463 hmmBlock->count = 0;
1464 hmmBlock->idx0 = idx0;
1465 for (i = 0; i < hmmBlock->listSize; ++i)
1466 {
1467 status = cm_p7_oprofile_ReadMSV(cmfp, TRUE, byp_abc,
1468 &(hmmBlock->cm_offsetA[i]),
1469 &(hmmBlock->cm_clenA[i]),
1470 &(hmmBlock->cm_WA[i]),
1471 &(hmmBlock->cm_nbpA[i]),
1472 &(hmmBlock->gfmuA[i]),
1473 &(hmmBlock->gflambdaA[i]),
1474 &(hmmBlock->list[i]));
1475 if (status != eslOK) break;
1476 ++hmmBlock->count;
1477 }
1478 /* create the MSV data */
1479 for (i = 0; i < hmmBlock->count; ++i) {
1480 hmmBlock->msvdataA[i] = p7_hmm_ScoreDataCreate(hmmBlock->list[i], FALSE);
1481 }
1482 /* initiate the clan idx data, caller will need to fill this if nec */
1483 for (i = 0; i < hmmBlock->count; ++i) {
1484 hmmBlock->clan_idxA[i] = -1;
1485 }
1486
1487 /* EOF will be returned only in the case were no profiles were read */
1488 if (status == eslEOF && i > 0) status = eslOK;
1489
1490 return status;
1491 }
1492
1493 /*----------------- end of API for p7 filters ----------------------*/
1494
1495
1496 /*****************************************************************
1497 * 5. Private, specific profile CM file format parsers.
1498 *****************************************************************/
1499
1500 /* Parsing save files from INFERNAL 1.x
1501 * All parsers follow the same API.
1502 *
1503 * Returns <eslOK> on success, and if <opt_cm> is non-NULL,
1504 * <*opt_cm> points at a newly allocated CM.
1505 *
1506 * Additionally, if <*ret_abc> was NULL, then a new alphabet is
1507 * allocated according to the alphabet type of this CM, and returned
1508 * thru <ret_abc>. This allocation mechanism allows a main()
1509 * application that doesn't yet know its alphabet to determine the
1510 * alphabet when the first CM is read, while also allowing an
1511 * application to allocate its own alphabet and assure that the
1512 * input CMs are appropriate for that alphabet.
1513 *
1514 * Returns <eslEOF> when no CM remains in the file, indicating a
1515 * normal end-of-file.
1516 *
1517 * Two types of "normal error" may happen, which the caller must check
1518 * for. Returns <eslEFORMAT> on any save file format error, including
1519 * bad magic (i.e. this is not an INFERNAL file at all). Returns
1520 * <eslEINCOMPAT> if the expected alphabet (a non-<NULL> alphabet
1521 * specified by <*ret_abc>) does not match the alphabet type of the
1522 * HMM.
1523 *
1524 * When these normal errors occur, the caller can construct its error
1525 * message from:
1526 * <cmfp->errbuf>: contains an informative error message
1527 * <cmfp->fname>: name of the CM file (or '-' if STDIN)
1528 * and if <cmfp->efp> is non-<NULL>, the CM file is in ASCII text,
1529 * and the caller may also use:
1530 * <cmfp->efp->linenumber>: line on which the parse error occurred.
1531 *
1532 * Throws: <eslEMEM> on allocation error.
1533 * <eslESYS> if a system i/o call fails.
1534 * In cases of error (including both thrown error and normal error), <*ret_abc>
1535 * is left in its original state as passed by the caller, and <*ret_hmm> is
1536 * returned <NULL>.
1537 */
1538 static int
1539 read_asc_1p1_cm(CM_FILE *cmfp, int read_fp7, ESL_ALPHABET **ret_abc, CM_t **opt_cm)
1540 {
1541 int status;
1542 ESL_ALPHABET *abc = NULL;
1543 CM_t *cm = NULL;
1544 P7_HMM *hmm = NULL;
1545 char *tag = NULL;
1546 char *tok1 = NULL;
1547 char *tok2 = NULL;
1548 char *tok3 = NULL;
1549 char *tok4 = NULL;
1550 char *tok5 = NULL;
1551 char *tok6 = NULL;
1552 int alphatype;
1553 off_t offset = 0;
1554 off_t fp7_offset = 0;
1555 int v, x, y, nd; /* counters */
1556 int read_fp7_stats = FALSE;
1557 uint32_t cm_statstracker = 0; /* for making sure we have all CM E-value stats, if we have any */
1558 int exp_mode;
1559 int read_el_selfsc = FALSE; /* set to true when we read ELSELF line */
1560
1561 /* temporary parameters, for storing values prior to their allocation in the CM */
1562 float *tmp_null = NULL;
1563 float tmp_fp7_gfmu;
1564 float tmp_fp7_gflambda;
1565 double tmp_qdbbeta1;
1566 double tmp_qdbbeta2;
1567
1568 /* temporary per-node annotation, will be converted to per-consensus position once the model
1569 * architecture is known, after the full model is read */
1570 char *tmp_rf_left = NULL;
1571 char *tmp_rf_right = NULL;
1572 char *tmp_cons_left = NULL;
1573 char *tmp_cons_right = NULL;
1574 int *tmp_map_left = NULL;
1575 int *tmp_map_right = NULL;
1576
1577 cmfp->errbuf[0] = '\0';
1578
1579 if (cmfp->newly_opened)
1580 {
1581 offset = 0;
1582 cmfp->newly_opened = FALSE;
1583 }
1584 else
1585 {
1586 /* Record where this CM starts on disk */
1587 if ((! cmfp->do_stdin) && (! cmfp->do_gzip) && (offset = ftello(cmfp->f)) < 0) ESL_XEXCEPTION(eslESYS, "ftello() failed");
1588
1589 /* First line of file: "INFERNAL1/a". Allocate shell for CM annotation information (we don't know M,nodes yet) */
1590 if ((status = esl_fileparser_NextLine(cmfp->efp)) != eslOK) goto ERROR; /* EOF here is normal; could also be a thrown EMEM */
1591 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tag, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "unexpected absence of tokens on data line");
1592
1593 if (cmfp->format == CM_FILE_1a) { if (strcmp(tag, "INFERNAL1/a") != 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Didn't find INFERNAL1/a tag: bad format or not an INFERNAL save file?"); }
1594 else ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No such CM file format code: this shouldn't happen");
1595 }
1596
1597 if ((cm = CreateCMShell()) == NULL) ESL_XFAIL(eslEMEM, cmfp->errbuf, "allocation failure, CM shell");
1598 cm->offset = offset;
1599
1600 /* Header section */
1601 while ((status = esl_fileparser_NextLine(cmfp->efp)) == eslOK)
1602 {
1603 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tag, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Premature end of line");
1604
1605 if (strcmp(tag, "NAME") == 0) {
1606 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No name found on NAME line");
1607 cm_SetName(cm, tok1);
1608 }
1609
1610 else if (strcmp(tag, "ACC") == 0) {
1611 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No accession found on ACC line");
1612 cm_SetAccession(cm, tok1);
1613 }
1614
1615 else if (strcmp(tag, "DESC") == 0) {
1616 if ((status = esl_fileparser_GetRemainingLine(cmfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No description found on DESC line");
1617 cm_SetDescription(cm, tok1);
1618 }
1619
1620 else if (strcmp(tag, "STATES") == 0) {
1621 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Number of model states not found on STATES line");
1622 if ((cm->M = atoi(tok1)) == 0) ESL_XFAIL(status, cmfp->errbuf, "Invalid number of states %s on STATES line", tok1);
1623 }
1624
1625 else if (strcmp(tag, "NODES") == 0) {
1626 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Number of model nodes not found on NODES line");
1627 if ((cm->nodes = atoi(tok1)) == 0) ESL_XFAIL(status, cmfp->errbuf, "Invalid number of nodes %s on NODES line", tok1);
1628 }
1629
1630 else if (strcmp(tag, "CLEN") == 0) {
1631 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No consensus length found on CLEN line");
1632 if ((cm->clen = atoi(tok1)) == 0) ESL_XFAIL(status, cmfp->errbuf, "Invalid consensus length %s on CLEN line", tok1);
1633 }
1634
1635 else if (strcmp(tag, "W") == 0) {
1636 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No consensus length found on W line");
1637 if ((cm->W = atoi(tok1)) == 0) ESL_XFAIL(status, cmfp->errbuf, "Invalid consensus length %s on W line", tok1);
1638 }
1639
1640 else if (strcmp(tag, "ALPH") == 0) {
1641 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No alphabet type found on ALPH");
1642 if ((alphatype = esl_abc_EncodeType(tok1)) == eslUNKNOWN) ESL_XFAIL(status, cmfp->errbuf, "Unrecognized alphabet type %s", tok1);
1643 if (*ret_abc == NULL) {
1644 if ((abc = esl_alphabet_Create(alphatype)) == NULL) ESL_XFAIL(eslEMEM, cmfp->errbuf, "Failed to create alphabet");
1645 } else {
1646 if ((*ret_abc)->type != alphatype) ESL_XFAIL(eslEINCOMPAT,cmfp->errbuf,"Alphabet type mismatch: was %s, but current CM says %s", esl_abc_DecodeType( (*ret_abc)->type), tok1);
1647 abc = *ret_abc;
1648 }
1649 }
1650
1651 else if (strcmp(tag, "RF") == 0) {
1652 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No yes/no found for RF line");
1653 if (strcasecmp(tok1, "yes") == 0) cm->flags |= CMH_RF;
1654 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "RF header line must say yes/no, not %s", tok1);
1655 }
1656
1657 else if (strcmp(tag, "CONS") == 0) {
1658 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No yes/no found for CONS line");
1659 if (strcasecmp(tok1, "yes") == 0) cm->flags |= CMH_CONS;
1660 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "CONS header line must say yes/no, not %s", tok1);
1661 }
1662
1663 else if (strcmp(tag, "MAP") == 0) {
1664 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No yes/no found for MAP line");
1665 if (strcasecmp(tok1, "yes") == 0) cm->flags |= CMH_MAP;
1666 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "MAP header line must say yes/no, not %s", tok1);
1667 }
1668
1669 else if (strcmp(tag, "DATE") == 0) {
1670 if ((status = esl_fileparser_GetRemainingLine(cmfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No date found on DATE line");
1671 if (esl_strdup(tok1, -1, &(cm->ctime)) != eslOK) ESL_XFAIL(eslEMEM, cmfp->errbuf, "strdup() failed to set date");
1672 }
1673
1674 else if (strcmp(tag, "COM") == 0) {
1675 /* just skip the first token; it's something like [1], numbering the command lines */
1676 if ((status = esl_fileparser_GetTokenOnLine (cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No command number on COM line");
1677 if ((status = esl_fileparser_GetRemainingLine(cmfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "No command on COM line");
1678 if (cm->comlog == NULL) {
1679 if (esl_strdup(tok1, -1, &(cm->comlog)) != eslOK) ESL_XFAIL(eslEMEM, cmfp->errbuf, "esl_strdup() failed");
1680 } else {
1681 if (esl_strcat(&(cm->comlog), -1, "\n", -1) != eslOK) ESL_XFAIL(eslEMEM, cmfp->errbuf, "esl_strcat() failed");
1682 if (esl_strcat(&(cm->comlog), -1, tok1, -1) != eslOK) ESL_XFAIL(eslEMEM, cmfp->errbuf, "esl_strcat() failed");
1683 }
1684 }
1685
1686 else if (strcmp(tag, "PBEGIN") == 0) {
1687 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows PBEGIN tag");
1688 if ((cm->pbegin = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid beta on PBEGIN line: should be a positive real number, not %s", tok1);
1689 }
1690
1691 else if (strcmp(tag, "PEND") == 0) {
1692 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows PEND tag");
1693 if ((cm->pend = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid beta on PEND line: should be a positive real number, not %s", tok1);
1694 }
1695
1696 else if (strcmp(tag, "WBETA") == 0) {
1697 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows WBETA tag");
1698 if ((cm->beta_W = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid beta on WBETA line: should be a positive real number, not %s", tok1);
1699 }
1700
1701 else if (strcmp(tag, "QDBBETA1") == 0) {
1702 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows QDBBETA tag");
1703 if ((tmp_qdbbeta1 = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid beta on QDBBETA line: should be a positive real number, not %s", tok1);
1704 }
1705
1706 else if (strcmp(tag, "QDBBETA2") == 0) {
1707 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows QDBBETA tag");
1708 if ((tmp_qdbbeta2 = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid beta on QDBBETA line: should be a positive real number, not %s", tok1);
1709 }
1710
1711 else if (strcmp(tag, "N2OMEGA") == 0) {
1712 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows N2OMEGA tag");
1713 if ((cm->null2_omega = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid omega on N2OMEGA line: should be a positive real number, not %s", tok1);
1714 }
1715
1716 else if (strcmp(tag, "N3OMEGA") == 0) {
1717 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows N3OMEGA tag");
1718 if ((cm->null3_omega = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid omega on N3OMEGA line: should be a positive real number, not %s", tok1);
1719 }
1720
1721 else if (strcmp(tag, "ELSELF") == 0) {
1722 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows ELSELF tag");
1723 cm->el_selfsc = atof(tok1);
1724 read_el_selfsc = TRUE;
1725 }
1726
1727 else if (strcmp(tag, "NSEQ") == 0) {
1728 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows NSEQ tag");
1729 if ((cm->nseq = atoi(tok1)) == 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid nseq on NSEQ line: should be integer, not %s", tok1);
1730 }
1731
1732 else if (strcmp(tag, "EFFN") == 0) {
1733 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows EFFN tag");
1734 if ((cm->eff_nseq = atof(tok1)) < (-1.*eslSMALLX1)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid eff_nseq on EFFN line: should be a positive real number, not %s", tok1);
1735 }
1736
1737 else if (strcmp(tag, "CKSUM") == 0) {
1738 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Nothing follows CKSUM tag");
1739 cm->checksum = atoll(tok1); /* if atoi(), then you may truncate uint32_t checksums > 2^31-1 */
1740 cm->flags |= CMH_CHKSUM;
1741 }
1742
1743 else if (strcmp(tag, "NULL") == 0) {
1744 if(abc == NULL) ESL_XFAIL(status, cmfp->errbuf, "Read NULL line before ALPH line, ALPH line must come first");
1745 ESL_ALLOC(tmp_null, sizeof(float) * abc->K);
1746 for (x = 0; x < abc->K; x++) {
1747 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on NULL line");
1748 tmp_null[x] = ascii2prob(tok1, (1./(float) abc->K));
1749 }
1750 }
1751
1752 else if (strcmp(tag, "GA") == 0) {
1753 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on GA line");
1754 cm->ga = atof(tok1);
1755 cm->flags |= CMH_GA;
1756 }
1757
1758 else if (strcmp(tag, "TC") == 0) {
1759 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on TC line");
1760 cm->tc = atof(tok1);
1761 cm->flags |= CMH_TC;
1762 }
1763
1764 else if (strcmp(tag, "NC") == 0) {
1765 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on NC line");
1766 cm->nc = atof(tok1);
1767 cm->flags |= CMH_NC;
1768 }
1769
1770 else if (strcmp(tag, "EFP7GF") == 0) {
1771 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on EFP7GF line"); /* tau */
1772 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on EFP7GF line"); /* lambda */
1773 tmp_fp7_gfmu = atof(tok1);
1774 tmp_fp7_gflambda = atof(tok2);
1775 read_fp7_stats = TRUE;
1776 }
1777
1778 else if (strncmp(tag, "ECM", 3) == 0) { /* one of 4 possible CM E-value lines */
1779 /* determine which one */
1780 if (strncmp(tag+3, "LC", 2) == 0) { exp_mode = EXP_CM_LC; cm_statstracker += 1; }
1781 else if (strncmp(tag+3, "GC", 2) == 0) { exp_mode = EXP_CM_GC; cm_statstracker += 2; }
1782 else if (strncmp(tag+3, "LI", 2) == 0) { exp_mode = EXP_CM_LI; cm_statstracker += 4; }
1783 else if (strncmp(tag+3, "GI", 2) == 0) { exp_mode = EXP_CM_GI; cm_statstracker += 8; }
1784 else { ESL_XFAIL(status, cmfp->errbuf, "Invalid tag beginning with ECM"); }
1785 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on ECM.. line"); /* lambda */
1786 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on ECM.. line"); /* mu_extrap */
1787 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok3, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on ECM.. line"); /* mu_orig */
1788 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok4, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on ECM.. line"); /* dbsize */
1789 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok5, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on ECM.. line"); /* nrandhits */
1790 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok6, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on ECM.. line"); /* tailp */
1791 if (cm->expA == NULL) {
1792 ESL_ALLOC(cm->expA, sizeof(ExpInfo_t *) * EXP_NMODES);
1793 for(x = 0; x < EXP_NMODES; x++) { cm->expA[x] = CreateExpInfo(); }
1794 }
1795 cm->expA[exp_mode]->lambda = atof(tok1);
1796 cm->expA[exp_mode]->mu_extrap = atof(tok2);
1797 cm->expA[exp_mode]->mu_orig = atof(tok3);
1798 cm->expA[exp_mode]->dbsize = atof(tok4); /* store as double, even though it was written as a long */
1799 cm->expA[exp_mode]->nrandhits = atoi(tok5);
1800 cm->expA[exp_mode]->tailp = atof(tok6);
1801 cm->expA[exp_mode]->is_valid = TRUE;
1802 }
1803 else if (strcmp(tag, "CM") == 0) {
1804 /* skip the remainder of this line */
1805 if ((status = esl_fileparser_NextLine(cmfp->efp)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Premature end of data before main model section");
1806 break;
1807 }
1808 } /* end, loop over possible header tags */
1809 if (status != eslOK) goto ERROR;
1810
1811 /* Done reading the header information.
1812 * Check that everything is ok and mandatory info is present before moving on.
1813 */
1814 if (cm->M < 1) ESL_XFAIL(status, cmfp->errbuf, "Failed to read STATES line in header section");
1815 if (cm->nodes < 1) ESL_XFAIL(status, cmfp->errbuf, "Failed to read NODES line in header section");
1816 if (cm->clen < 1) ESL_XFAIL(status, cmfp->errbuf, "Failed to read CLEN line in header section");
1817 if (cm->W < 1) ESL_XFAIL(status, cmfp->errbuf, "Failed to read W line in header section");
1818 if (! read_el_selfsc) ESL_XFAIL(status, cmfp->errbuf, "Failed to read ELSELF line in header section");
1819 if (! read_fp7_stats) ESL_XFAIL(status, cmfp->errbuf, "Failed to read EFP7GF line in header section");
1820 if (cm->name == NULL) ESL_XFAIL(status, cmfp->errbuf, "Failed to read NAME line in header section");
1821 if (abc == NULL) ESL_XFAIL(status, cmfp->errbuf, "Failed to read ALPH line in header section");
1822 if (tmp_null == NULL) ESL_XFAIL(status, cmfp->errbuf, "Failed to read NULL line in header section");
1823
1824 /* Check to make sure we parsed CM E-value stats correctly.
1825 */
1826 if (cm->expA != NULL) {
1827 if (cm_statstracker == 15) cm->flags |= CMH_EXPTAIL_STATS;
1828 else if (cm_statstracker != 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Missing one or more ECM.. parameter lines");
1829 }
1830
1831 /* Allocate body of CM now that # states (M) and # nodes (nnodes) are known */
1832 CreateCMBody(cm, cm->nodes, cm->M, cm->clen, abc);
1833
1834 /* Copy values we stored in temp parameters awaiting CM allocation in CreateCMBody() */
1835 esl_vec_FCopy(tmp_null, abc->K, cm->null); /* cm->null allocated in CreateCMBody */
1836 cm->qdbinfo->beta1 = tmp_qdbbeta1;
1837 cm->qdbinfo->beta2 = tmp_qdbbeta2;
1838
1839 /* Allocate and initialize temporary parameters for values we can't store
1840 * until after we've read the full model and are able to construct an emit map */
1841 ESL_ALLOC(tmp_map_left, sizeof(int) * cm->nodes);
1842 ESL_ALLOC(tmp_map_right, sizeof(int) * cm->nodes);
1843 esl_vec_ISet(tmp_map_left, cm->nodes, -1);
1844 esl_vec_ISet(tmp_map_right, cm->nodes, -1);
1845
1846 ESL_ALLOC(tmp_rf_left, sizeof(char) * (cm->nodes+1));
1847 ESL_ALLOC(tmp_rf_right, sizeof(char) * (cm->nodes+1));
1848 tmp_rf_left[cm->nodes] = '\0';
1849 tmp_rf_right[cm->nodes] = '\0';
1850
1851 ESL_ALLOC(tmp_cons_left, sizeof(char) * (cm->nodes+1));
1852 ESL_ALLOC(tmp_cons_right, sizeof(char) * (cm->nodes+1));
1853 tmp_cons_left[cm->nodes] = '\0';
1854 tmp_cons_right[cm->nodes] = '\0';
1855
1856 nd = -1;
1857 cm->clen = 0;
1858
1859 for (v = 0; v < cm->M; v++)
1860 {
1861 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Premature end of data before main model section");
1862
1863 /* Ah, a node line. Process it and get the following line.
1864 */
1865 if (*tok1 == '[')
1866 {
1867 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 1);
1868 if ((x = NodeCode(tok1)) == -1) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid node type %s", tok1);
1869 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 2);
1870 if (!is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid node index on node line: should be integer >= 0, not %s", tok1);
1871 nd = atoi(tok1);
1872 if (nd < 0) ESL_XFAIL(status, cmfp->errbuf, "Invalid node index on node line: should be integer >= 0, not %s", tok1);
1873 if (nd >= cm->nodes) ESL_XFAIL(status, cmfp->errbuf, "Invalid node index on node line: should not exceed %d, read %s", cm->nodes, tok1);
1874 cm->ndtype[nd] = x;
1875 if (cm->ndtype[nd] == MATP_nd) cm->clen+=2;
1876 else if(cm->ndtype[nd] == MATL_nd) cm->clen++;
1877 else if(cm->ndtype[nd] == MATR_nd) cm->clen++;
1878 cm->nodemap[nd] = v;
1879
1880 /* chew up ']' */
1881 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 3);
1882
1883 /* read annotation: MAP, consensus sequence and RF. Proper format depends on node type.
1884 */
1885 /* MAP (optional: CMH_MAP? yes, else no */
1886 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 4);
1887 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 5);
1888 if ((cm->flags & CMH_MAP) && cm->ndtype[nd] == MATP_nd) {
1889 if (!is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid 1st MAP value on MATP node line: should be positive integer, not %s", tok1);
1890 if (!is_integer(tok2)) ESL_XFAIL(status, cmfp->errbuf, "Invalid 2nd MAP value on MATP node line: should be positive integer, not %s", tok2);
1891 tmp_map_left[nd] = atoi(tok1);
1892 tmp_map_right[nd] = atoi(tok2);
1893 }
1894 else if((cm->flags & CMH_MAP) && cm->ndtype[nd] == MATL_nd) {
1895 if (!is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid 1st MAP value on MATL node line: should be positive integer, not %s", tok1);
1896 if (*tok2 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 2nd MAP value on MATL node line: should be '-', not %s", tok2);
1897 tmp_map_left[nd] = atoi(tok1);
1898 tmp_map_right[nd] = -1;
1899 }
1900 else if((cm->flags & CMH_MAP) && cm->ndtype[nd] == MATR_nd) {
1901 if (*tok1 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 1st MAP value on MATR node line: should be '-', not %s", tok1);
1902 if (!is_integer(tok2)) ESL_XFAIL(status, cmfp->errbuf, "Invalid 2nd MAP value on MATR node line: should be positive integer, not %s", tok2);
1903 tmp_map_left[nd] = -1;
1904 tmp_map_right[nd] = atoi(tok2);
1905 }
1906 else { /* either (! (cm->flags & CMH_MAP)) or ndtype is not MATP, MATL nor MATR */
1907 if (*tok1 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 1st MAP value on node line: should be '-', not %s", tok1);
1908 if (*tok2 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 2nd MAP value on node line: should be '-', not %s", tok2);
1909 tmp_map_left[nd] = -1;
1910 tmp_map_right[nd] = -1;
1911 }
1912
1913 /* consensus sequence (optional: CMH_CONS? yes, else no */
1914 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 6);
1915 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 7);
1916 if ((cm->flags & CMH_CONS) && (cm->ndtype[nd] == MATP_nd)) {
1917 tmp_cons_left[nd] = *tok1;
1918 tmp_cons_right[nd] = *tok2;
1919 }
1920 else if((cm->flags & CMH_CONS) && cm->ndtype[nd] == MATL_nd) {
1921 if (*tok2 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 2nd consensus character on MATL node line: should be '-', not %s", tok2);
1922 tmp_cons_left[nd] = *tok1;
1923 tmp_cons_right[nd] = *tok2;
1924 }
1925 else if((cm->flags & CMH_CONS) && cm->ndtype[nd] == MATR_nd) {
1926 if (*tok1 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 1st consensus character on MATR node line: should be '-', not %s", tok1);
1927 tmp_cons_left[nd] = *tok1;
1928 tmp_cons_right[nd] = *tok2;
1929 }
1930 else { /* either (! (cm->flags & CMH_CONS) or ndtype is not MATP, MATL nor MATR */
1931 if (*tok1 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 1st consensus character on node line: should be '-', not %s", tok1);
1932 if (*tok2 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 2nd consensus character on node line: should be '-', not %s", tok2);
1933 tmp_cons_left[nd] = *tok1;
1934 tmp_cons_right[nd] = *tok2;
1935 }
1936
1937 /* RF (optional: CMH_RF? yes, else no */
1938 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 8);
1939 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on node line: expected %d, got %d", 10, 9);
1940 if ((cm->flags & CMH_RF) && cm->ndtype[nd] == MATP_nd) {
1941 tmp_rf_left[nd] = *tok1;
1942 tmp_rf_right[nd] = *tok2;
1943 }
1944 else if((cm->flags & CMH_RF) && cm->ndtype[nd] == MATL_nd) {
1945 if (*tok2 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 2nd RF character on MATL node line: should be '-', not %s", tok2);
1946 tmp_rf_left[nd] = *tok1;
1947 tmp_rf_right[nd] = *tok2;
1948 }
1949 else if((cm->flags & CMH_RF) && cm->ndtype[nd] == MATR_nd) {
1950 if (*tok1 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 1st RF character on MATR node line: should be '-', not %s", tok1);
1951 tmp_rf_left[nd] = *tok1;
1952 tmp_rf_right[nd] = *tok2;
1953 }
1954 else { /* either (! (cm->flags & CMH_RF)) or ndtype is not MATP, MATL nor MATR */
1955 if (*tok1 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 1st RF character on node line: should be '-', not %s", tok1);
1956 if (*tok2 != '-') ESL_XFAIL(status, cmfp->errbuf, "Invalid 2nd RF character on node line: should be '-', not %s", tok2);
1957 tmp_rf_left[nd] = *tok1;
1958 tmp_rf_right[nd] = *tok2;
1959 }
1960 if ((status = esl_fileparser_NextLine(cmfp->efp)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Premature end of data in main model: no state %d line", v);
1961 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state line: expected at least %d, got %d", 8, 0);
1962 } /* done with node line */
1963
1964 /* Process state line.
1965 * <statecode> <v> <plast> <pnum> <cfirst> <cnum> <dmin2> <dmin1> <dmax1> <dmax2> <transition probs (variable number)> <emission probs (variable number)>
1966 */
1967
1968 /* <statecode> */
1969 if((cm->sttype[v] = StateCode(tok1)) == -1) ESL_XFAIL(status, cmfp->errbuf, "Invalid state type %s\n", tok1);
1970
1971 /* <v> */
1972 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 1);
1973 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid state index on state line: should be integer >= 0, not %s", tok1);
1974 if (atoi(tok1) != v) ESL_XFAIL(status, cmfp->errbuf, "Invalid state index on state line: should be %d, not %s", v, tok1);
1975
1976 /* <plast> */
1977 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 2);
1978 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid plast value on state line: should be integer, not %s", tok1);
1979 cm->plast[v] = atoi(tok1);
1980
1981 /* <pnum> */
1982 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 3);
1983 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid pnum value on state line: should be integer, not %s", tok1);
1984 cm->pnum[v] = atoi(tok1);
1985
1986 /* <cfirst> */
1987 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 4);
1988 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid cfirst value on state line: should be integer, not %s", tok1);
1989 cm->cfirst[v] = atoi(tok1);
1990
1991 /* <cnum> */
1992 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 5);
1993 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid cnum value on state line: should be integer, not %s", tok1);
1994 cm->cnum[v] = atoi(tok1);
1995
1996 /* <dmin2> */
1997 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 6);
1998 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid dmin2 value on state line: should be integer, not %s", tok1);
1999 cm->qdbinfo->dmin2[v] = atoi(tok1);
2000
2001 /* <dmin1> */
2002 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 6);
2003 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid dmin1 value on state line: should be integer, not %s", tok1);
2004 cm->qdbinfo->dmin1[v] = atoi(tok1);
2005
2006 /* <dmax1> */
2007 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 7);
2008 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid dmax1 value on state line: should be integer, not %s", tok1);
2009 cm->qdbinfo->dmax1[v] = atoi(tok1);
2010
2011 /* <dmax2> */
2012 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8, 7);
2013 if (! is_integer(tok1)) ESL_XFAIL(status, cmfp->errbuf, "Invalid dmax2 value on state line: should be integer, not %s", tok1);
2014 cm->qdbinfo->dmax2[v] = atoi(tok1);
2015
2016 /* Transition probabilities. */
2017 if (cm->sttype[v] != B_st) {
2018 for (x = 0; x < cm->cnum[v]; x++) {
2019 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected at least %d, got %d", v, 8+cm->cnum[v], 8+x);
2020 if ((! is_real(tok1) && (*tok1 != '*'))) ESL_XFAIL(status, cmfp->errbuf, "Invalid transition score %d on state line: should be real number or '*', not %s", x+1, tok1);
2021 cm->t[v][x] = ascii2prob(tok1, 1.);
2022 }
2023 }
2024 /* Emission probabilities. */
2025 if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st || cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) {
2026 for (x = 0; x < cm->abc->K; x++) {
2027 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected %d, got %d", v, 8+cm->cnum[v]+cm->abc->K, 8+cm->cnum[v]+x);
2028 if ((! is_real(tok1) && (*tok1 != '*'))) ESL_XFAIL(status, cmfp->errbuf, "Invalid emission score %d on state line: should be real number or '*', not %s", x+1, tok1);
2029 cm->e[v][x] = ascii2prob(tok1, cm->null[x]);
2030 }
2031 }
2032 else if (cm->sttype[v] == MP_st) {
2033 for (x = 0; x < cm->abc->K; x++) {
2034 for (y = 0; y < cm->abc->K; y++) {
2035 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Too few fields on state %d line: expected %d, got %d", v, 8+cm->cnum[v]+(cm->abc->K*cm->abc->K), 8+cm->cnum[v]+(x*cm->abc->K)+y);
2036 if ((! is_real(tok1) && (*tok1 != '*'))) ESL_XFAIL(status, cmfp->errbuf, "Invalid emission score %d on state line: should be real number or '*', not %s", (x*cm->abc->K)+y+1, tok1);
2037 cm->e[v][x*cm->abc->K+y] = ascii2prob(tok1, cm->null[x]*cm->null[y]);
2038 }
2039 }
2040 }
2041 cm->ndidx[v] = nd;
2042 cm->stid[v] = DeriveUniqueStateCode(cm->ndtype[nd], cm->sttype[v]);
2043 if ((status = esl_fileparser_NextLine(cmfp->efp)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Premature end of data in main model: no state %d line", v+1);
2044 } /* end of loop over states */
2045 /* The closing // */
2046 if ((status = esl_fileparser_GetTokenOnLine(cmfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Premature end of data: missing //?");
2047 if (strcmp(tok1, "//") != 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Expected closing //; found %s instead", tok1);
2048
2049 /* Finally, read the filter HMM for this CM, unless we're explicitly told not to */
2050 if(read_fp7) {
2051 if(!cmfp->hfp->do_stdin && !cmfp->hfp->do_gzip) {
2052 if((fp7_offset = ftello(cmfp->f)) < 0) ESL_XFAIL(eslESYS, cmfp->errbuf, "ftello() failed");
2053 }
2054 else {
2055 fp7_offset = -1; /* this is irrelevant if stdin or gzip mode, cm_p7_hmmfile_Read will ignore it */
2056 }
2057 if((status = cm_p7_hmmfile_Read(cmfp, abc, fp7_offset, &hmm)) != eslOK) goto ERROR;
2058 if((status = cm_SetFilterHMM(cm, hmm, tmp_fp7_gfmu, tmp_fp7_gflambda)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Unable to set filter HMM for CM");
2059 if(!cmfp->hfp->do_stdin && !cmfp->hfp->do_gzip) {
2060 /* now position the CM file to the end of the HMM we just read */
2061 if (fseeko(cmfp->f, ftello(cmfp->hfp->f), SEEK_SET) != 0) ESL_XFAIL(eslESYS, cmfp->errbuf, "Failed to set position for CM file parser after reading filter HMMs");
2062 }
2063 }
2064
2065 CMRenormalize(cm);
2066 cm->qdbinfo->setby = CM_QDBINFO_SETBY_CMFILE;
2067 cm->W_setby = CM_W_SETBY_CMFILE;
2068
2069 /* Create emit map now that we know the model architecture */
2070 cm->emap = CreateEmitMap(cm);
2071 if(cm->emap == NULL) ESL_XFAIL(eslEINVAL, cmfp->errbuf, "After reading complete model, failed to create an emit map");
2072
2073 /* Use emit map to map the per-node annotation to per-consensus position */
2074 if (cm->flags & CMH_RF) {
2075 cm->rf[0] = ' ';
2076 for(nd = 0; nd < cm->nodes; nd++) {
2077 if(cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd) cm->rf[cm->emap->lpos[nd]] = tmp_rf_left[nd];
2078 if(cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd) cm->rf[cm->emap->rpos[nd]] = tmp_rf_right[nd];
2079 }
2080 cm->rf[cm->clen+1] = '\0';
2081 }
2082 if (cm->flags & CMH_CONS) {
2083 cm->consensus[0] = ' ';
2084 for(nd = 0; nd < cm->nodes; nd++) {
2085 if(cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd) cm->consensus[cm->emap->lpos[nd]] = tmp_cons_left[nd];
2086 if(cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd) cm->consensus[cm->emap->rpos[nd]] = tmp_cons_right[nd];
2087 }
2088 cm->consensus[cm->clen+1] = '\0';
2089 }
2090 if (cm->flags & CMH_MAP) {
2091 cm->map[0] = 0;
2092 for(nd = 0; nd < cm->nodes; nd++) {
2093 if(cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd) cm->map[cm->emap->lpos[nd]] = tmp_map_left[nd];
2094 if(cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd) cm->map[cm->emap->rpos[nd]] = tmp_map_right[nd];
2095 }
2096 }
2097
2098 if(tmp_null != NULL) free(tmp_null);
2099
2100 /* these get allocated regardless of flag status, free them */
2101 free(tmp_rf_left);
2102 free(tmp_rf_right);
2103 free(tmp_cons_left);
2104 free(tmp_cons_right);
2105 free(tmp_map_left);
2106 free(tmp_map_right);
2107
2108 if (*ret_abc == NULL) *ret_abc = abc;
2109 if ( opt_cm != NULL) *opt_cm = cm; else FreeCM(cm);
2110 return eslOK;
2111
2112 ERROR:
2113 if (*ret_abc == NULL && abc != NULL) esl_alphabet_Destroy(abc);
2114 if (cm != NULL) FreeCM(cm);
2115 if (opt_cm != NULL) *opt_cm = NULL;
2116 if (status == eslEMEM || status == eslESYS) return status;
2117 else if (status == eslEOF) return status;
2118 else if (status == eslEINCOMPAT) return status;
2119 else return eslEFORMAT; /* anything else is a format error: includes premature EOF, EOL, EOD */
2120 }
2121
2122
2123 static int
2124 read_bin_1p1_cm(CM_FILE *cmfp, int read_fp7, ESL_ALPHABET **ret_abc, CM_t **opt_cm)
2125 {
2126 ESL_ALPHABET *abc = NULL;
2127 CM_t *cm = NULL;
2128 P7_HMM *hmm = NULL;
2129 uint32_t magic;
2130 int alphabet_type;
2131 int v, x;
2132 off_t offset = 0;
2133 off_t fp7_offset = 0;
2134 int status;
2135 float tmp_fp7_gfmu;
2136 float tmp_fp7_gflambda;
2137
2138 cmfp->errbuf[0] = '\0';
2139 if (feof(cmfp->f)) { status = eslEOF; goto ERROR; }
2140
2141 if (cmfp->newly_opened)
2142 {
2143 offset = 0;
2144 cmfp->newly_opened = FALSE;
2145 }
2146 else
2147 { /* Check magic. */
2148 if ((!cmfp->do_stdin) && (! cmfp->do_gzip)) {
2149 if ((offset = ftello(cmfp->f)) < 0) ESL_XEXCEPTION(eslESYS, "ftello() failed");
2150 }
2151 if (! fread((char *) &magic, sizeof(uint32_t), 1, cmfp->f)) { status = eslEOF; goto ERROR; }
2152
2153 if (cmfp->format == CM_FILE_1a) { if (magic != v1a_magic) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "bad magic number at start of CM"); }
2154 else ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "no such CM file format code");
2155 }
2156
2157 /* Allocate shell of the new CM.
2158 * Two-step allocation lets us read/set the flags first;
2159 * then the later CreateBody() call will allocate optional internal fields we need.
2160 */
2161 if ((cm = CreateCMShell()) == NULL) ESL_XFAIL(eslEMEM, cmfp->errbuf, "allocation failure, CM shell");
2162 cm->offset = offset;
2163
2164 /* Get sizes of things */
2165 if (! fread((char *) &(cm->flags), sizeof(int), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read flags");
2166
2167 /* Important step that's easy to overlook: lower flags that may have
2168 * been up when CM was output to file, but that won't be true of the
2169 * CM we're about to read (since not all CM parameters go into the
2170 * file).
2171 */
2172 cm->flags &= ~CMH_BITS;
2173 cm->flags &= ~CMH_CP9;
2174 cm->flags &= ~CMH_CP9_TRUNC;
2175 cm->flags &= ~CMH_MLP7;
2176 cm->flags &= ~CM_IS_CONFIGURED;
2177
2178 if (! fread((char *) &(cm->M), sizeof(int), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read number of states");
2179 if (! fread((char *) &(cm->nodes), sizeof(int), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read number of nodes");
2180 if (! fread((char *) &(cm->clen), sizeof(int), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read consensus length");
2181 if (! fread((char *) &alphabet_type, sizeof(int), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read alphabet_type");
2182
2183 /* Set or verify alphabet. */
2184 if (*ret_abc == NULL) { /* still unknown: set it, pass control of it back to caller */
2185 if ((abc = esl_alphabet_Create(alphabet_type)) == NULL) ESL_XFAIL(eslEMEM, cmfp->errbuf, "allocation failed, alphabet");
2186 } else { /* already known: check it */
2187 abc = *ret_abc;
2188 if (abc->type != alphabet_type) ESL_XFAIL(eslEINCOMPAT, cmfp->errbuf, "Alphabet type mismatch: was %s, but current HMM says %s", esl_abc_DecodeType( abc->type), esl_abc_DecodeType(alphabet_type));
2189 }
2190
2191 /* Finish the allocation of the CM
2192 */
2193 CreateCMBody(cm, cm->nodes, cm->M, cm->clen, abc);
2194
2195 /* Core model probabilities. */
2196 if (! fread((char *) cm->sttype, sizeof(char), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read sttype array");
2197 if (! fread((char *) cm->ndidx, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read ndidx array");
2198 if (! fread((char *) cm->stid, sizeof(char), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read stid array");
2199 if (! fread((char *) cm->cfirst, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read cfirst array");
2200 if (! fread((char *) cm->cnum, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read cnum array");
2201 if (! fread((char *) cm->plast, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read plast array");
2202 if (! fread((char *) cm->pnum, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read pnum array");
2203 if (! fread((char *) cm->nodemap, sizeof(int), cm->nodes, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read nodemap array");
2204 if (! fread((char *) cm->ndtype, sizeof(char), cm->nodes, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read ndtype array");
2205 if (! fread((char *) cm->qdbinfo->dmin1, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read dmin1 array");
2206 if (! fread((char *) cm->qdbinfo->dmax1, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read dmax1 array");
2207 if (! fread((char *) cm->qdbinfo->dmin2, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read dmin2 array");
2208 if (! fread((char *) cm->qdbinfo->dmax2, sizeof(int), cm->M, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read dmax2 array");
2209
2210 cm->qdbinfo->setby = CM_QDBINFO_SETBY_CMFILE;
2211 cm->W_setby = CM_W_SETBY_CMFILE;
2212
2213 /* Create emit map now that we know the model architecture */
2214 cm->emap = CreateEmitMap(cm);
2215 if(cm->emap == NULL) ESL_XFAIL(eslEINVAL, cmfp->errbuf, "After reading complete model, failed to create an emit map");
2216
2217 for (v = 0; v < cm->M; v++) {
2218 if (! fread((char *) cm->t[v], sizeof(float), MAXCONNECT, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read transitions for state %d", v);
2219 if (! fread((char *) cm->e[v], sizeof(float), cm->abc->K*cm->abc->K,cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read emissions for state %d", v);
2220 }
2221
2222 /* Annotations. */
2223 if (read_bin_string(cmfp->f, &(cm->name)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read name");
2224 if ((cm->flags & CMH_ACC) && read_bin_string(cmfp->f, &(cm->acc)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read acc");
2225 if ((cm->flags & CMH_DESC) && read_bin_string(cmfp->f, &(cm->desc)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read desc");
2226 if ((cm->flags & CMH_RF) && ! fread((char *) cm->rf, sizeof(char), cm->clen+2, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read rf"); /* +2: 1..M and trailing \0 */
2227 if ((cm->flags & CMH_CONS) && ! fread((char *) cm->consensus, sizeof(char), cm->clen+2, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read consensus"); /* don't need to test for >=3e format, because the flag is sufficient (didn't exist pre-3e) */
2228 if ((cm->flags & CMH_MAP) && ! fread((char *) cm->map, sizeof(int), cm->clen+1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read map");
2229 if (! fread((char *) &(cm->W), sizeof(int), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read nseq");
2230
2231 if (read_bin_string(cmfp->f, &(cm->ctime)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read ctime");
2232 if (read_bin_string(cmfp->f, &(cm->comlog)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read comlog");
2233
2234 if (! fread((char *) &(cm->pbegin), sizeof(float), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read beta_W");
2235 if (! fread((char *) &(cm->pend), sizeof(float), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read beta_W");
2236 if (! fread((char *) &(cm->beta_W), sizeof(double), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read beta_W");
2237 if (! fread((char *) &(cm->qdbinfo->beta1), sizeof(double), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read betaqdb1");
2238 if (! fread((char *) &(cm->qdbinfo->beta2), sizeof(double), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read betaqdb2");
2239 if (! fread((char *) &(cm->null2_omega), sizeof(float), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read null2_omega");
2240 if (! fread((char *) &(cm->null3_omega), sizeof(float), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read null3_omega");
2241 if (! fread((char *) &(cm->el_selfsc), sizeof(float), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read el_selfsc");
2242 if (! fread((char *) &(cm->nseq), sizeof(int), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read nseq");
2243 if (! fread((char *) &(cm->eff_nseq), sizeof(float), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read eff_nseq");
2244 if (! fread((char *) &(cm->checksum), sizeof(uint32_t), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read checksum");
2245 if (! fread((char *) cm->null, sizeof(float), cm->abc->K, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read null vector");
2246
2247 /* Rfam cutoffs */
2248 if ((cm->flags & CMH_GA) && (! fread((char *) &(cm->ga), sizeof(float), 1, cmfp->f))) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read GA cutoff");
2249 if ((cm->flags & CMH_TC) && (! fread((char *) &(cm->tc), sizeof(float), 1, cmfp->f))) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read TC cutoff");
2250 if ((cm->flags & CMH_NC) && (! fread((char *) &(cm->nc), sizeof(float), 1, cmfp->f))) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read NC cutoff");
2251
2252 /* E-value parameters */
2253 if (cm->flags & CMH_FP7) { /* should always be true */
2254 if (! fread((char *) &tmp_fp7_gfmu, sizeof(float), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read additional P7 E-value stats");
2255 if (! fread((char *) &tmp_fp7_gflambda, sizeof(float), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read additional P7 E-value stats");
2256 }
2257 if (cm->flags & CMH_EXPTAIL_STATS) {
2258 long dbsize_long;
2259 ESL_ALLOC(cm->expA, sizeof(ExpInfo_t *) * EXP_NMODES);
2260 for(x = 0; x < EXP_NMODES; x++) {
2261 cm->expA[x] = CreateExpInfo();
2262 if (! fread((char *) &(cm->expA[x]->lambda), sizeof(double), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read CM E-value stats");
2263 if (! fread((char *) &(cm->expA[x]->mu_extrap), sizeof(double), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read CM E-value stats");
2264 if (! fread((char *) &(cm->expA[x]->mu_orig), sizeof(double), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read CM E-value stats");
2265 if (! fread((char *) &(dbsize_long), sizeof(long), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read CM E-value stats");
2266 cm->expA[x]->dbsize = (double) dbsize_long;
2267 if (! fread((char *) &(cm->expA[x]->nrandhits), sizeof(int), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read CM E-value stats");
2268 if (! fread((char *) &(cm->expA[x]->tailp), sizeof(double), 1, cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "failed to read CM E-value stats");
2269 }
2270 }
2271
2272 /* Finally, read the filter HMM for this CM, unless we're explicitly asked not to. */
2273 if(read_fp7) {
2274 if(!cmfp->hfp->do_stdin && !cmfp->hfp->do_gzip) {
2275 if((fp7_offset = ftello(cmfp->f)) < 0) ESL_XFAIL(eslESYS, cmfp->errbuf, "ftello() failed");
2276 }
2277 else {
2278 fp7_offset = -1; /* this is irrelevant if stdin or gzip mode, cm_p7_hmmfile_Read will ignore it */
2279 }
2280 if((status = cm_p7_hmmfile_Read(cmfp, abc, fp7_offset, &hmm)) != eslOK) goto ERROR;
2281 if((status = cm_SetFilterHMM(cm, hmm, tmp_fp7_gfmu, tmp_fp7_gflambda)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Unable to set filter HMM for CM");
2282 if(!cmfp->hfp->do_stdin && !cmfp->hfp->do_gzip) {
2283 /* now position the CM file to the end of the HMM we just read */
2284 if (fseeko(cmfp->f, ftello(cmfp->hfp->f), SEEK_SET) != 0) ESL_XFAIL(eslESYS, cmfp->errbuf, "Failed to set position for CM file parser after reading filter HMMs");
2285 }
2286 }
2287
2288 if (*ret_abc == NULL) *ret_abc = abc; /* pass our new alphabet back to caller, if caller didn't know it already */
2289 if ( opt_cm != NULL) *opt_cm = cm; else FreeCM(cm);
2290 return eslOK;
2291
2292 ERROR:
2293 if (*ret_abc == NULL && abc != NULL) esl_alphabet_Destroy(abc); /* the test is for an alphabet created here, not passed */
2294 if (cm != NULL) FreeCM(cm);
2295 if (opt_cm != NULL) *opt_cm = NULL;
2296 return status;
2297 }
2298
2299 /* read_asc_1p0_cm(): inputting version 1.0-->1.0.2 CM file format.
2300 *
2301 * This function was stolen from Infernal v1.0.2's
2302 * cm_io.c:read_ascii_cm() (which is identical to the function of the
2303 * same time in v1.0 and v1.0.1.), and minmally changed to work in the
2304 * context of new CM_FILE data structure. This version also includes
2305 * better error message handling.
2306 *
2307 * Note that the value of <read_fp7> is irrelevant. It is only
2308 * in the prototype so that read_asc_1p0_cm is consistent with
2309 * other parser prototypes.
2310 */
2311 static int
2312 read_asc_1p0_cm(CM_FILE *cmfp, int read_fp7, ESL_ALPHABET **ret_abc, CM_t **opt_cm)
2313 {
2314 int status;
2315 CM_t *cm;
2316 char *buf;
2317 int n; /* length of buf */
2318 char *s;
2319 int M,N; /* number of states, nodes in model */
2320 int v,x,y,nd; /* counters for states, events, nodes */
2321 char *tok;
2322 int toklen;
2323 int exp_flags[EXP_NMODES]; /* keep track of which exp tails we've read */
2324 int exp_mode; /* index of exp tail info */
2325 int have_exps; /* for checking we get 0 or all exp tails*/
2326 int have_ga = FALSE; /* we have GA cutoff, needed b/c we can't set cm->flags until after CreateCMBody() call */
2327 int have_tc = FALSE; /* we have TC cutoff, needed b/c we can't set cm->flags until after CreateCMBody() call */
2328 int have_nc = FALSE; /* we have NC cutoff, needed b/c we can't set cm->flags until after CreateCMBody() call */
2329 int p; /* counter for partitions */
2330 int i; /* counter over exp_modes for exp tails */
2331 int alphabet_type; /* type of ESL_ALPHABET */
2332 ESL_ALPHABET *abc = NULL;
2333 int read_nstates = FALSE; /* TRUE once we've read the number of states */
2334 int read_nnodes = FALSE; /* TRUE once we've read the number of nodes */
2335 int read_atype = FALSE; /* TRUE once we've read the alphabet type */
2336 int read_clen = FALSE;
2337 int evalues_are_invalid = FALSE; /* TRUE if the PART line reports more than 1 partition */
2338 int clen = 0;
2339 int npartitions = 0;
2340 off_t offset = 0;
2341
2342 cm = NULL;
2343 buf = NULL;
2344 n = 0;
2345 for(i = 0; i < EXP_NMODES; i++) exp_flags[i] = FALSE;
2346
2347 cmfp->errbuf[0] = '\0';
2348
2349 if (cmfp->newly_opened)
2350 {
2351 offset = 0;
2352 cmfp->newly_opened = FALSE;
2353 }
2354 else
2355 {
2356 /* Record where this CM starts on disk */
2357 if ((! cmfp->do_stdin) && (! cmfp->do_gzip) && (offset = ftello(cmfp->f)) < 0) ESL_XEXCEPTION(eslESYS, "ftello() failed");
2358
2359 /* First line of file: "INFERNAL-1" */
2360 if (feof(cmfp->f) || esl_fgets(&buf, &n, cmfp->f) != eslOK) { /* end of file, free buf and return eslEOF */
2361 if(buf != NULL) free(buf);
2362 return eslEOF;
2363 }
2364 if (cmfp->format == CM_FILE_1) { if (strncmp(buf, "INFERNAL-1", 10) != 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Didn't find INFERNAL-1 tag: bad format or not an INFERNAL save file?"); }
2365 else ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No such CM file format code: this shouldn't happen");
2366 }
2367
2368
2369 /* Parse the header information
2370 * These are all tag/value.
2371 * Ignore unknown tags (forward compatibility).
2372 */
2373 cm = CreateCMShell();
2374 M = N = -1;
2375 while (esl_fgets(&buf, &n, cmfp->f) != eslEOF)
2376 {
2377 s = buf;
2378 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Premature end of data, prior to MODEL section");
2379 else if (strcmp(tok, "NAME") == 0)
2380 {
2381 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No name found on NAME line");
2382 if ((status = esl_strdup(tok, toklen, &(cm->name))) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting name for CM");
2383 }
2384 else if (strcmp(tok, "ACC") == 0)
2385 {
2386 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No accession found on ACC line");
2387 if ((status = esl_strdup(tok, toklen, &(cm->acc))) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting accession for CM");
2388 }
2389 else if (strcmp(tok, "DESC") == 0)
2390 {
2391 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No description found on DESC line");
2392 if ((status = esl_strdup(tok, toklen, &(cm->desc))) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting description for CM");
2393 }
2394 else if (strcmp(tok, "GA") == 0)
2395 {
2396 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No GA threshold found on GA line");
2397 cm->ga = atof(tok);
2398 have_ga = TRUE;
2399 }
2400 else if (strcmp(tok, "TC") == 0)
2401 {
2402 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No TC threshold found on TC line");
2403 cm->tc = atof(tok);
2404 have_tc = TRUE;
2405 }
2406 else if (strcmp(tok, "NC") == 0)
2407 {
2408 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No NC threshold found on NC line");
2409 cm->nc = atof(tok);
2410 have_nc = TRUE;
2411 }
2412 else if (strcmp(tok, "STATES") == 0)
2413 {
2414 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No number of STATES found on STATES line");
2415 M = atoi(tok);
2416 read_nstates = TRUE;
2417 }
2418 else if (strcmp(tok, "NODES") == 0)
2419 {
2420 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No number of NODES found on NODES line");
2421 N = atoi(tok);
2422 read_nnodes = TRUE;
2423 }
2424 else if (strcmp(tok, "ALPHABET") == 0)
2425 {
2426 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No alphabet type found on ALPHABET line");
2427 alphabet_type = atoi(tok);
2428 /* Set or verify alphabet. */
2429 if (*ret_abc == NULL) { /* still unknown: set it, pass control of it back to caller */
2430 if ((abc = esl_alphabet_Create(alphabet_type)) == NULL) ESL_XFAIL(eslEMEM, cmfp->errbuf, "Failed to create alphabet");
2431 } else { /* already known: check it */
2432 abc = *ret_abc;
2433 if ((*ret_abc)->type != alphabet_type) ESL_XFAIL(eslEINCOMPAT,cmfp->errbuf,"Alphabet type mismatch");
2434 }
2435 read_atype = TRUE;
2436 }
2437 else if (strcmp(tok, "ELSELF") == 0)
2438 {
2439 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No ELSELF score found on ELSELF line");
2440 cm->el_selfsc = atof(tok);
2441 }
2442 else if (strcmp(tok, "WBETA") == 0)
2443 {
2444 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No WBETA found on WBETA line");
2445 cm->beta_W = (double) atof(tok);
2446 }
2447 else if (strcmp(tok, "NSEQ") == 0)
2448 {
2449 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No name found on NAME line");
2450 cm->nseq = atoi(tok);
2451 }
2452 else if (strcmp(tok, "EFFNSEQ") == 0)
2453 {
2454 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No effective seq number found on EFFNSEQ line");
2455 cm->eff_nseq = atof(tok);
2456 }
2457 else if (strcmp(tok, "CLEN") == 0)
2458 {
2459 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No consensus length found on CLEN line");
2460 clen = atoi(tok); /* we'll compare this to what we calculate at end of func */
2461 read_clen = TRUE;
2462 /* Now we have the clen and we should have the alphabet type and N and M, so we can build the
2463 * full model, and set the alphabet (which we need to do before alloc'ing/setting
2464 * the null model */
2465 if(! (read_nstates && read_nnodes && read_atype))
2466 {
2467 ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "ERROR, ALPHABET, STATES and NODES lines should precede CLEN line");
2468 }
2469 CreateCMBody(cm, N, M, clen, abc);
2470 }
2471 /* comlog and ctime info. Careful, we want the full line, so a token becomes a full line
2472 * Also we stored this data differently in version 1.0, we have to throw out the CDATE
2473 * info, we don't store that anymore.
2474 */
2475 else if (strcmp(tok, "BCOM") == 0)
2476 {
2477 while(isspace((int) (*s))) s++; /* chew up leading whitespace */
2478 if ((status = esl_strtok_adv(&s, "\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No build command found on BCOM line");
2479 if(cm->comlog == NULL) {
2480 if((status = esl_strdup(tok, toklen, &(cm->comlog))) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting build date");
2481 }
2482 else {
2483 if((status = esl_strcat(&(cm->comlog), -1,"\n", 1)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting build date");
2484 if((status = esl_strcat(&(cm->comlog), -1, tok, toklen)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting build date");
2485 }
2486 }
2487 else if (strcmp(tok, "BDATE") == 0)
2488 {
2489 while(isspace((int) (*s))) s++; /* chew up leading whitespace */
2490 if ((status = esl_strtok_adv(&s, "\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No date found on BDATE line");
2491 if (esl_strdup(tok, toklen, &(cm->ctime)) != eslOK) ESL_XFAIL(eslEMEM, cmfp->errbuf, "strdup() failed to set date");
2492 }
2493 else if (strcmp(tok, "CCOM") == 0)
2494 {
2495 while(isspace((int) (*s))) s++; /* chew up leading whitespace */
2496 if ((status = esl_strtok_adv(&s, "\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No calibrate command found on CCOM line");
2497 if(cm->comlog == NULL) {
2498 if((status = esl_strdup(tok, toklen, &(cm->comlog))) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting calibrate date");
2499 }
2500 else {
2501 if((status = esl_strcat(&(cm->comlog), -1,"\n", 1)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting calibrate date");
2502 if((status = esl_strcat(&(cm->comlog), -1, tok, toklen)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Problem setting calibrate date");
2503 }
2504 }
2505 else if (strcmp(tok, "CDATE") == 0)
2506 {
2507 while(isspace((int) (*s))) s++; /* chew up leading whitespace */
2508 if ((status = esl_strtok_adv(&s, "\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No date found on CDATE line");
2509 /* we don't store this anymore */
2510 }
2511 else if (strcmp(tok, "NULL") == 0)
2512 {
2513 if(cm->abc == NULL) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "NULL line must be preceded by ALPHABET line");
2514 /* cm-> null already allocated in CreateCMBody() */
2515 for (x = 0; x < abc->K; x++)
2516 {
2517 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Problem reading NULL line");
2518 cm->null[x] = ascii2prob(tok, (1./(float) abc->K));
2519 }
2520 }
2521 /* exp tail distribution information */
2522 else if (strcmp(tok, "PART") == 0)
2523 {
2524 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No number of partitions on PART line");
2525 if (! is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "number of partitions is not an integer on PART line");
2526 npartitions = atoi(tok);
2527 if(npartitions != 1) {
2528 evalues_are_invalid = TRUE;
2529 /* we can't deal with more than 1 partition in the current codebase, if there are more, throw away any E-value parameters we read */
2530 }
2531 /* else 1 partition, we can handle this (nearly all infernal
2532 * 1.0--1.0.2 files should have 1 partitions, you could
2533 * only create a multi-partition file with the undocument
2534 * --exp-pfile option to cmcalibrate). */
2535
2536 /* Ignore the rest of this line, it includes partition start/stop info that's no longer parsed */
2537 if ((status = esl_strtok_adv(&s, "\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid PART- line");
2538 }
2539 /* exp tail info */
2540 else if ((strncmp(tok, "E-", 2) == 0) && (! evalues_are_invalid)) /* skip the E- lines if we've read more than one partition above */
2541 {
2542 /* determine which exp tail we're reading */
2543 if (strncmp(tok+2, "LC", 2) == 0) exp_mode = EXP_CM_LC;
2544 else if (strncmp(tok+2, "GC", 2) == 0) exp_mode = EXP_CM_GC;
2545 else if (strncmp(tok+2, "LI", 2) == 0) exp_mode = EXP_CM_LI;
2546 else if (strncmp(tok+2, "GI", 2) == 0) exp_mode = EXP_CM_GI;
2547 else if (strncmp(tok+2, "LV", 2) == 0) continue; /* cp9 HMM local viterbi, irrelevant in current format */
2548 else if (strncmp(tok+2, "GV", 2) == 0) continue; /* cp9 HMM glocal viterbi, irrelevant in current format */
2549 else if (strncmp(tok+2, "LF", 2) == 0) continue; /* cp9 HMM local forward, irrelevant in current format */
2550 else if (strncmp(tok+2, "GF", 2) == 0) continue; /* cp9 HMM glocal forward, irrelevant in current format */
2551 else ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid xx on E-xx line");
2552
2553 /* create the expA array if this is the first E- line we've read */
2554 if (cm->expA == NULL) {
2555 ESL_ALLOC(cm->expA, sizeof(ExpInfo_t *) * EXP_NMODES);
2556 for(x = 0; x < EXP_NMODES; x++) { cm->expA[x] = CreateExpInfo(); }
2557 }
2558
2559 /* now we know what exp tail we're reading, read it */
2560 /* chew up partition */
2561 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No partition read on E-xx line");
2562 p = atoi(tok);
2563 if (p != 0) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid partition on E-xx line");
2564
2565 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No lambda read on E-xx line");
2566 if (! is_real(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "lambda not real number on E-xx line");
2567 cm->expA[exp_mode]->lambda = atof(tok);
2568
2569 if ((esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No mu_extrap read on E-xx line");
2570 if (! is_real(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "mu_extrap not real number on E-xx line");
2571 cm->expA[exp_mode]->mu_extrap = atof(tok);
2572
2573 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No mu_orig read on E-xx line");
2574 if (! is_real(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "mu_orign is not real number on E-xx line");
2575 cm->expA[exp_mode]->mu_orig = atof(tok);
2576
2577 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No dbsize read on E-xx line");
2578 if (! is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "dbsize is not integer on E-xx line");
2579 cm->expA[exp_mode]->dbsize = atof(tok); /* read it as a double, even though it's written as a long */
2580
2581 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No nrandhits read on E-xx line");
2582 if (! is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "nrandhits is not integer on E-xx line");
2583 cm->expA[exp_mode]->nrandhits = atoi(tok);
2584
2585 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "No tailp read on E-xx line");
2586 if (! is_real(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "tailp is not real number on E-xx line");
2587 cm->expA[exp_mode]->tailp = atof(tok);
2588
2589 cm->expA[exp_mode]->cur_eff_dbsize = (double) (cm->expA[exp_mode]->nrandhits);
2590 /* Previous line is to set cur_eff_dbsize as if database was of size cm->stats->expAA[p]->dbsize, we
2591 * act as if the max hits we'll see is nrandhits, the number of hits we saw in cmcalibrate,
2592 * so this is the highest possible E-value we can get.
2593 * cur_eff_dbsize will be updated in cmsearch for whatever the target database size is. */
2594 cm->expA[exp_mode]->is_valid = TRUE; /* set valid flag */
2595 exp_flags[exp_mode] = TRUE;
2596 }
2597 else if (strncmp(tok, "FT-", 3) == 0)
2598 {
2599 /* filter thresholds statistics are deprecated, chew up the rest ot this line, and following two lines */
2600 if ((status = esl_strtok_adv(&s, "\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid FT- line");
2601 if (esl_fgets(&buf, &n, cmfp->f) == eslEOF) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Premature end to file, while reading FT- line 2");
2602 if (esl_fgets(&buf, &n, cmfp->f) == eslEOF) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Premature end to file, while reading FT- line 3");
2603 }
2604 else if (strcmp(tok, "MODEL:") == 0)
2605 break;
2606 }
2607
2608 /* Done reading the header information.
2609 * Check that everything is ok and mandatory info is present before moving on.
2610 */
2611 if (feof(cmfp->f)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Premature end to CM file, file truncated?");
2612 if (! read_nstates) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "MODEL: line precedes STATES line");
2613 if (! read_nnodes) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "MODEL: line precedes NODES line");
2614 if (! read_clen) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "MODEL: line precedes CLEN line");
2615 if (! read_atype) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "MODEL: line precedes ALPHABET line");
2616 if (cm->name == NULL) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "MODEL: line precedes NAME line");
2617
2618 /* if we have any exp tail stats, we (currently) require all of them */
2619 have_exps = exp_flags[0];
2620 for(exp_mode = 1; exp_mode < EXP_NMODES; exp_mode++)
2621 if(((have_exps && (!exp_flags[exp_mode]))) ||
2622 ((!have_exps) && (exp_flags[exp_mode])))
2623 ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Some but not all E-xx lines read, expected 8");
2624
2625 /* Main model section.
2626 */
2627 CMZero(cm);
2628 if(have_exps) cm->flags |= CMH_EXPTAIL_STATS;
2629 if(have_ga) cm->flags |= CMH_GA;
2630 if(have_tc) cm->flags |= CMH_TC;
2631 if(have_nc) cm->flags |= CMH_NC;
2632 nd = -1;
2633 clen = 0;
2634 for (v = 0; v < cm->M; v++)
2635 {
2636 if ((status = esl_fgets(&buf, &n, cmfp->f)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Premature end of data before main model section");
2637 s = buf;
2638 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(status, cmfp->errbuf, "Premature end of data before main model section");
2639
2640 /* Ah, a node line. Process it and get the following line.
2641 */
2642 if (*tok == '[')
2643 {
2644 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Too few fields on node line");
2645 if ((x = NodeCode(tok)) == -1) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid node type %s", tok);
2646 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Too few fields on node line");
2647 if (!is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid node index on node line");
2648 nd = atoi(tok);
2649 cm->ndtype[nd] = x;
2650 if(cm->ndtype[nd] == MATP_nd) clen+=2;
2651 else if(cm->ndtype[nd] == MATL_nd) clen++;
2652 else if(cm->ndtype[nd] == MATR_nd) clen++;
2653 cm->nodemap[nd] = v;
2654
2655 if ((status = esl_fgets(&buf, &n, cmfp->f)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Too few fields on NODE line");
2656 s = buf;
2657 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Too few fields on NODE line");
2658 }
2659
2660 /* Process state line.
2661 */
2662 cm->sttype[v] = StateCode(tok);
2663 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2664 if (! is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2665 if (atoi(tok) != v) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2666 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2667 if (! is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2668 cm->plast[v] = atoi(tok);
2669 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2670 if (! is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2671 cm->pnum[v] = atoi(tok);
2672 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2673 if (! is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2674 cm->cfirst[v] = atoi(tok);
2675 if ((status= esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2676 if (! is_integer(tok)) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2677 cm->cnum[v] = atoi(tok);
2678 /* Transition probabilities. */
2679 if (cm->sttype[v] != B_st)
2680 {
2681 for (x = 0; x < cm->cnum[v]; x++)
2682 {
2683 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2684 if (! is_real(tok) && *tok != '*') ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2685 cm->t[v][x] = ascii2prob(tok, 1.);
2686 }
2687 }
2688 /* Emission probabilities. */
2689 if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st ||
2690 cm->sttype[v] == IL_st || cm->sttype[v] == IR_st)
2691 {
2692 for (x = 0; x < cm->abc->K; x++)
2693 {
2694 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2695 if (! is_real(tok) && *tok != '*') ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2696 cm->e[v][x] = ascii2prob(tok, cm->null[x]);
2697 }
2698 }
2699 else if (cm->sttype[v] == MP_st)
2700 {
2701 for (x = 0; x < cm->abc->K; x++)
2702 for (y = 0; y < cm->abc->K; y++)
2703 {
2704 if ((status = esl_strtok_adv(&s, " \t\n", &tok, &toklen, NULL)) != eslOK) ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2705 if (! is_real(tok) && *tok != '*') ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "Invalid state line for cm: %s state: %d", cm->name, v);
2706 cm->e[v][x*cm->abc->K+y] = ascii2prob(tok, cm->null[x]*cm->null[y]);
2707 }
2708 }
2709
2710 cm->ndidx[v] = nd;
2711 cm->stid[v] = DeriveUniqueStateCode(cm->ndtype[nd], cm->sttype[v]);
2712 } /* end of loop over states */
2713
2714 /* Advance to record separator
2715 */
2716 while (esl_fgets(&buf, &n, cmfp->f) != eslEOF)
2717 if (strncmp(buf, "//", 2) == 0)
2718 break;
2719
2720 /* EPN 10.29.06 Remove the sole source of CM ambiguities. Find and detach insert states
2721 * that are 1 state before an END_E. */
2722 cm_find_and_detach_dual_inserts(cm,
2723 FALSE, /* Don't check END_E-1 states have 0 counts, they may not if
2724 * an old version (0.7 or earlier) of cmbuild was used, or
2725 * cmbuild --nodetach was used to build the CM */
2726 TRUE); /* Detach the states by setting trans probs into them as 0.0 */
2727
2728 /* check that the clen we calc'ed is the same as the CLEN line said */
2729 if (read_clen && clen != cm->clen)
2730 {
2731 ESL_XFAIL(eslEFORMAT, cmfp->errbuf, "calculated consensus length %d does not equal read CLEN: %d.\n", clen, cm->clen);
2732 }
2733
2734 /* Success.
2735 * Renormalize the CM, and return.
2736 */
2737 CMRenormalize(cm);
2738
2739 if (buf != NULL) free(buf);
2740 if (*ret_abc == NULL) *ret_abc = abc;
2741 if ( opt_cm != NULL) *opt_cm = cm; else FreeCM(cm);
2742 return eslOK;
2743
2744 ERROR:
2745 if (buf != NULL) free(buf);
2746 if (*ret_abc == NULL && abc != NULL) esl_alphabet_Destroy(abc);
2747 if (cm != NULL) FreeCM(cm);
2748 if (opt_cm != NULL) *opt_cm = NULL;
2749 if (status == eslEMEM || status == eslESYS) return status;
2750 else if (status == eslEOF) return status;
2751 else if (status == eslEINCOMPAT) return status;
2752 else return eslEFORMAT; /* anything else is a format error: includes premature EOF, EOL, EOD */
2753 }
2754
2755
2756 /* Below, read_asc30hmm(), etc. are EXACT copies from H3 p7_hmmfile.c
2757 * This isn't the right way to do it, but it's what we did. Do NOT
2758 * edit these here. If you edit them in H3, you MUST bring those edits
2759 * here. [SRE:24-Jan-17]
2760 *
2761 * EPN, Wed Aug 30 13:01:21 2023
2762 * Despite above comment, I modified read_{asc,bin}30hmm below to
2763 * avoid a race condition, to use hfp->rr_errbuf instead of
2764 * hfp->errbuf. See comment near top of read_asc30hmm for more
2765 * info.
2766 */
2767
2768 /* Parsing save files from HMMER 3.x
2769 * All parsers follow the same API.
2770 *
2771 * Returns <eslOK> on success, and if <opt_hmm> is non-NULL,
2772 * <*opt_hmm> points at a newly allocated HMM.
2773 *
2774 * Additionally, if <*ret_abc> was NULL, then a new alphabet is
2775 * allocated according to the alphabet type of this HMM, and returned
2776 * thru <ret_abc>. This allocation mechanism allows a main()
2777 * application that doesn't yet know its alphabet to determine the
2778 * alphabet when the first HMM is read, while also allowing an
2779 * application to allocate its own alphabet and assure that the
2780 * input HMMs are appropriate for that alphabet.
2781 *
2782 * Returns <eslEOF> when no HMM remains in the file, indicating a
2783 * normal end-of-file.
2784 *
2785 * Two types of "normal error" may happen, which the caller must check
2786 * for. Returns <eslEFORMAT> on any save file format error, including
2787 * bad magic (i.e. this is not a HMMER file at all). Returns
2788 * <eslEINCOMPAT> if the expected alphabet (a non-<NULL> alphabet
2789 * specified by <*ret_abc>) does not match the alphabet type of the
2790 * HMM.
2791 *
2792 * When these normal errors occur, the caller can construct its error
2793 * message from:
2794 * <hfp->rr_errbuf>: contains an informative error message
2795 * <hfp->fname>: name of the HMM file (or '-' if STDIN)
2796 * and if <hfp->efp> is non-<NULL>, the HMM file is in ASCII text,
2797 * and the caller may also use:
2798 * <hfp->efp->linenumber>: line on which the parse error occurred.
2799 *
2800 * Throws: <eslEMEM> on allocation error.
2801 * <eslESYS> if a system i/o call fails.
2802 * In cases of error (including both thrown error and normal error), <*ret_abc>
2803 * is left in its original state as passed by the caller, and <*ret_hmm> is
2804 * returned <NULL>.
2805 */
2806 static int
2807 read_asc30hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm)
2808 {
2809 ESL_ALPHABET *abc = NULL;
2810 P7_HMM *hmm = NULL;
2811 char *tag = NULL;
2812 char *tok1 = NULL;
2813 char *tok2 = NULL;
2814 char *tok3 = NULL;
2815 char *tok4 = NULL;
2816 int alphatype;
2817 int k,x;
2818 off_t offset = 0;
2819 int status;
2820 uint32_t statstracker = 0;
2821
2822 /* The following line is the only difference infernal and hmmer's
2823 * p7_hmmfile.c version of this function. In infernal we avoid use
2824 * of hfp->errbuf to avoid a race condition in cmscan. That
2825 * race previously occurred because the master
2826 * modified hfp->errbuf in cm_p7_oprofile_ReadMSV
2827 * but workers also modified hfp->errbuf (previous to this change)
2828 * it they reached the glocal envelope definition stage of
2829 * cm_Pipeline() when the full (non-optimized) HMM is read.
2830 * This is related to Sean's fix to a similar race condition in
2831 * hmmscan in which he added the rr_errbuf buffer for use in
2832 * p7_oprofile_ReadRest (hmmer commit 3867dac and github issues #277
2833 * and #278.)
2834 */
2835
2836 hfp->rr_errbuf[0] = '\0'; /* hmmer version of this function uses hfp->errbuf, don't use that here, see above comment */
2837
2838 if (hfp->newly_opened)
2839 {
2840 offset = 0;
2841 hfp->newly_opened = FALSE;
2842 }
2843 else
2844 {
2845 /* Record where this HMM starts on disk */
2846 if ((! hfp->do_stdin) && (! hfp->do_gzip) && (offset = ftello(hfp->f)) < 0) ESL_XEXCEPTION(eslESYS, "ftello() failed");
2847
2848 /* First line of file: "HMMER3/f". Allocate shell for HMM annotation information (we don't know K,M yet) */
2849 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) goto ERROR; /* EOF here is normal; could also be a thrown EMEM */
2850 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tag, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "unexpected absence of tokens on data line");
2851
2852 if (hfp->format == p7_HMMFILE_3f) { if (strcmp(tag, "HMMER3/f") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Didn't find HMMER3/f tag: bad format or not a HMMER save file?"); }
2853 else if (hfp->format == p7_HMMFILE_3e) { if (strcmp(tag, "HMMER3/e") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Didn't find HMMER3/e tag: bad format or not a HMMER save file?"); }
2854 else if (hfp->format == p7_HMMFILE_3d) { if (strcmp(tag, "HMMER3/d") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Didn't find HMMER3/d tag: bad format or not a HMMER save file?"); }
2855 else if (hfp->format == p7_HMMFILE_3c) { if (strcmp(tag, "HMMER3/c") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Didn't find HMMER3/c tag: bad format or not a HMMER save file?"); }
2856 else if (hfp->format == p7_HMMFILE_3b) { if (strcmp(tag, "HMMER3/b") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Didn't find HMMER3/b tag: bad format or not a HMMER save file?"); }
2857 else if (hfp->format == p7_HMMFILE_3a) { if (strcmp(tag, "HMMER3/a") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Didn't find HMMER3/a tag: bad format or not a HMMER save file?"); }
2858 else ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "No such HMM file format code: this shouldn't happen");
2859 }
2860
2861 if ((hmm = p7_hmm_CreateShell()) == NULL) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "allocation failure, HMM shell");
2862 hmm->offset = offset;
2863
2864 /* Header section */
2865 while ((status = esl_fileparser_NextLine(hfp->efp)) == eslOK)
2866 {
2867 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tag, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of line");
2868
2869 if (strcmp(tag, "NAME") == 0) {
2870 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No name found on NAME line");
2871 p7_hmm_SetName(hmm, tok1);
2872 }
2873
2874 else if (strcmp(tag, "ACC") == 0) {
2875 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No accession found on ACC line");
2876 p7_hmm_SetAccession(hmm, tok1);
2877 }
2878
2879 else if (strcmp(tag, "DESC") == 0) {
2880 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No description found on DESC line");
2881 p7_hmm_SetDescription(hmm, tok1);
2882 }
2883
2884 else if (strcmp(tag, "LENG") == 0) {
2885 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No model length found on LENG line");
2886 if ((hmm->M = atoi(tok1)) == 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Invalid model length %s on LENG line", tok1);
2887 }
2888
2889 else if (hfp->format >= p7_HMMFILE_3c && strcmp(tag, "MAXL") == 0) {
2890 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No max length found on MAXL line");
2891 if ((hmm->max_length = atoi(tok1)) == 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Invalid max length %s on MAXL line", tok1);
2892 }
2893
2894 else if (strcmp(tag, "ALPH") == 0) {
2895 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No alphabet type found on ALPH");
2896 if ((alphatype = esl_abc_EncodeType(tok1)) == eslUNKNOWN) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Unrecognized alphabet type %s", tok1);
2897 if (*ret_abc == NULL) {
2898 if ((abc = esl_alphabet_Create(alphatype)) == NULL) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "Failed to create alphabet");
2899 } else {
2900 if ((*ret_abc)->type != alphatype) ESL_XFAIL(eslEINCOMPAT, hfp->rr_errbuf,"Alphabet type mismatch: was %s, but current HMM says %s", esl_abc_DecodeType( (*ret_abc)->type), tok1);
2901 abc = *ret_abc;
2902 }
2903 }
2904
2905 else if (strcmp(tag, "RF") == 0) {
2906 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No yes/no found for RF line");
2907 if (strcasecmp(tok1, "yes") == 0)
2908 hmm->flags |= p7H_RF;
2909 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "RF header line must say yes/no, not %s", tok1);
2910 }
2911
2912 else if (strcmp(tag, "MM") == 0) {
2913 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No yes/no found for MM line");
2914 if (strcasecmp(tok1, "yes") == 0)
2915 hmm->flags |= p7H_MMASK;
2916 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "MM header line must say yes/no, not %s", tok1);
2917 }
2918
2919
2920 else if (strcmp(tag, "CONS") == 0) {
2921 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No yes/no found for CONS line");
2922 if (strcasecmp(tok1, "yes") == 0)
2923 hmm->flags |= p7H_CONS;
2924 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "CONS header line must say yes/no, not %s", tok1);
2925 }
2926
2927 else if (strcmp(tag, "CS") == 0) {
2928 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No yes/no found for CS line");
2929 if (strcasecmp(tok1, "yes") == 0)
2930 hmm->flags |= p7H_CS;
2931 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "CS header line must say yes/no, not %s", tok1);
2932 }
2933
2934 else if (strcmp(tag, "MAP") == 0) {
2935 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No yes/no found for MAP line");
2936 if (strcasecmp(tok1, "yes") == 0)
2937 hmm->flags |= p7H_MAP;
2938 else if (strcasecmp(tok1, "no") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "MAP header line must say yes/no, not %s", tok1);
2939 }
2940
2941 else if (strcmp(tag, "DATE") == 0) {
2942 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No date found on DATE line");
2943 if (esl_strdup(tok1, -1, &(hmm->ctime)) != eslOK) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "strdup() failed to set date");
2944 }
2945
2946 else if (strcmp(tag, "COM") == 0) {
2947 /* just skip the first token; it's something like [1], numbering the command lines */
2948 if ((status = esl_fileparser_GetTokenOnLine (hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No command number on COM line");
2949 if ((status = esl_fileparser_GetRemainingLine(hfp->efp, &tok1)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "No command on COM line");
2950 if (hmm->comlog == NULL) {
2951 if (esl_strdup(tok1, -1, &(hmm->comlog)) != eslOK) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "esl_strdup() failed");
2952 } else {
2953 if (esl_strcat(&(hmm->comlog), -1, "\n", -1) != eslOK) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "esl_strcat() failed");
2954 if (esl_strcat(&(hmm->comlog), -1, tok1, -1) != eslOK) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "esl_strcat() failed");
2955 }
2956 }
2957
2958 else if (strcmp(tag, "NSEQ") == 0) {
2959 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Nothing follows NSEQ tag");
2960 if ((hmm->nseq = atoi(tok1)) == 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Invalid nseq on NSEQ line: should be integer, not %s", tok1);
2961 }
2962
2963 else if (strcmp(tag, "EFFN") == 0) {
2964 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Nothing follows EFFN tag");
2965 if ((hmm->eff_nseq = atof(tok1)) <= 0.0f) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Invalid eff_nseq on EFFN line: should be a real number, not %s", tok1);
2966 }
2967
2968 else if (strcmp(tag, "CKSUM") == 0) {
2969 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Nothing follows CKSUM tag");
2970 hmm->checksum = atoll(tok1); /* if atoi(), then you may truncate uint32_t checksums > 2^31-1 */
2971 hmm->flags |= p7H_CHKSUM;
2972 }
2973
2974 else if (strcmp(tag, "STATS") == 0) {
2975 if (hfp->format >= p7_HMMFILE_3b)
2976 {
2977 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on STATS line"); /* LOCAL */
2978 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on STATS line"); /* MSV | VITERBI | FORWARD */
2979 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok3, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on STATS line"); /* mu | tau */
2980 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok4, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on STATS line"); /* lambda */
2981 if (strcasecmp(tok1, "LOCAL") == 0)
2982 {
2983 if (strcasecmp(tok2, "MSV") == 0) { hmm->evparam[p7_MMU] = atof(tok3); hmm->evparam[p7_MLAMBDA] = atof(tok4); statstracker |= 0x1; }
2984 else if (strcasecmp(tok2, "VITERBI") == 0) { hmm->evparam[p7_VMU] = atof(tok3); hmm->evparam[p7_VLAMBDA] = atof(tok4); statstracker |= 0x2; }
2985 else if (strcasecmp(tok2, "FORWARD") == 0) { hmm->evparam[p7_FTAU] = atof(tok3); hmm->evparam[p7_FLAMBDA] = atof(tok4); statstracker |= 0x4; }
2986 else ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Failed to parse STATS, %s unrecognized as field 3", tok2);
2987 } else ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Failed to parse STATS, %s unrecognized as field 2", tok1);
2988 }
2989 else if (hfp->format == p7_HMMFILE_3a) /* reverse compatibility with 30a */
2990 {
2991 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on STATS line"); /* LOCAL */
2992 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on STATS line"); /* VLAMBDA | VMU | FTAU */
2993 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok3, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on STATS line"); /* value */
2994 if (strcasecmp(tok1, "LOCAL") == 0)
2995 {
2996 if (strcasecmp(tok2, "VLAMBDA") == 0) { hmm->evparam[p7_MLAMBDA] = hmm->evparam[p7_VLAMBDA] = hmm->evparam[p7_FLAMBDA] = atof(tok3); statstracker |= 0x1; }
2997 else if (strcasecmp(tok2, "VMU") == 0) { hmm->evparam[p7_MMU] = hmm->evparam[p7_VMU] = atof(tok3); statstracker |= 0x2; }
2998 else if (strcasecmp(tok2, "FTAU") == 0) { hmm->evparam[p7_FTAU] = atof(tok3); statstracker |= 0x4; }
2999 else ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Failed to parse STATS, %s unrecognized as field 3", tok2);
3000 } else ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Failed to parse STATS, %s unrecognized as field 2", tok1);
3001 }
3002 }
3003
3004 else if (strcmp(tag, "GA") == 0) {
3005 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on GA line");
3006 hmm->cutoff[p7_GA1] = atof(tok1);
3007 if ( (abc->type == eslDNA || abc->type == eslRNA) ) { //if DNA, there's no need for a 2nd value (domain GA)
3008 hmm->cutoff[p7_GA2] = hmm->cutoff[p7_GA1];
3009 } else {
3010 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on GA line");
3011 hmm->cutoff[p7_GA2] = atof(tok2);
3012 }
3013 hmm->flags |= p7H_GA;
3014 }
3015
3016 else if (strcmp(tag, "TC") == 0) {
3017 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on TC line");
3018 hmm->cutoff[p7_TC1] = atof(tok1);
3019 if ( (abc->type == eslDNA || abc->type == eslRNA) ) { //if DNA, there's no need for a 2nd value (domain GA)
3020 hmm->cutoff[p7_TC2] = hmm->cutoff[p7_TC1];
3021 } else {
3022 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on TC line");
3023 hmm->cutoff[p7_TC2] = atof(tok2);
3024 }
3025 hmm->flags |= p7H_TC;
3026 }
3027
3028 else if (strcmp(tag, "NC") == 0) {
3029 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on NC line");
3030 hmm->cutoff[p7_NC1] = atof(tok1);
3031 if ( (abc->type == eslDNA || abc->type == eslRNA) ) { //if DNA, there's no need for a 2nd value (domain GA)
3032 hmm->cutoff[p7_NC2] = hmm->cutoff[p7_NC1];
3033 } else {
3034 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok2, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on NC line");
3035 hmm->cutoff[p7_NC2] = atof(tok2);
3036 }
3037 hmm->flags |= p7H_NC;
3038 }
3039
3040 else if (strcmp(tag, "HMM") == 0)
3041 break;
3042 } /* end, loop over possible header tags */
3043
3044 if (status != eslOK) goto ERROR;
3045
3046 /* If we saw one STATS line, we need all 3. (True for both 3/a and 3/b formats) */
3047 if (statstracker == 0x7) hmm->flags |= p7H_STATS;
3048 else if (statstracker != 0x0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Missing one or more STATS parameter lines");
3049
3050
3051 /* Skip main model header lines; allocate body of HMM now that K,M are known */
3052 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data before main model section");
3053 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data before main model section");
3054 if ((status = p7_hmm_CreateBody(hmm, hmm->M, abc)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Failed to allocate body of the new HMM");
3055 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data before main model section");
3056
3057 /* Optional model composition (filter null model) may immediately follow headers */
3058 if (strcmp(tok1, "COMPO") == 0) {
3059 for (x = 0; x < abc->K; x++) {
3060 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on COMPO line");
3061 hmm->compo[x] = (*tok1 == '*' ? 0.0 : expf(-1.0 * atof(tok1)));
3062 }
3063 hmm->flags |= p7H_COMPO;
3064 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data after COMPO line");
3065 if ((esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data after COMPO line");
3066 }
3067
3068 /* First two lines are node 0: insert emissions, then transitions from node 0 (begin) */
3069
3070 hmm->ins[0][0] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
3071 for (x = 1; x < abc->K; x++) {
3072 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on insert line, node 0: expected %d, got %d\n", abc->K, x);
3073 hmm->ins[0][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
3074 }
3075 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data in main model: no node 0 transition line");
3076 for (x = 0; x < p7H_NTRANSITIONS; x++) {
3077 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few fields on begin (0) transition line");
3078 hmm->t[0][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
3079 }
3080
3081 /* The main model section. */
3082 for (k = 1; k <= hmm->M; k++)
3083 {
3084
3085 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data in main model section, at node %d (expected %d)", k, hmm->M);
3086 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data in main model section, at node %d (expected %d)", k, hmm->M);
3087 if (atoi(tok1) != k) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Expected match line to start with %d (of %d); saw %s", k, hmm->M, tok1);
3088
3089 for (x = 0; x < abc->K; x++) {
3090 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few probability fields on match line, node %d: expected %d, got %d\n", k, abc->K, x);
3091 hmm->mat[k][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
3092 }
3093
3094 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Missing MAP field on match line for node %d: should at least be -", k);
3095 if (hmm->flags & p7H_MAP) hmm->map[k] = atoi(tok1);
3096
3097 if (hfp->format >= p7_HMMFILE_3e) {
3098 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Missing CONS field on match line for node %d: should at least be -", k);
3099 if (hmm->flags & p7H_CONS) hmm->consensus[k] = *tok1;
3100 }
3101 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Missing RF field on match line for node %d: should at least be -", k);
3102 if (hmm->flags & p7H_RF) hmm->rf[k] = *tok1;
3103
3104 if (hfp->format >= p7_HMMFILE_3f) {
3105 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Missing MM field on match line for node %d: should at least be -", k);
3106 if (hmm->flags & p7H_MMASK) hmm->mm[k] = *tok1;
3107 }
3108
3109
3110 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Missing CS field on match line for node %d: should at least be -", k);
3111 if (hmm->flags & p7H_CS) hmm->cs[k] = *tok1;
3112
3113
3114 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data in main model: no insert emission line, node %d", k);
3115 for (x = 0; x < abc->K; x++) {
3116 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few probability fields on insert line, node %d: expected %d, got %d\n", k, abc->K, x);
3117 hmm->ins[k][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
3118 }
3119 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data in main model: no transition line, node %d", k);
3120 for (x = 0; x < p7H_NTRANSITIONS; x++) {
3121 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Too few probability fields on transition line, node %d: expected %d, got %d\n", k, abc->K, x);
3122 hmm->t[k][x] = (*tok1 == '*' ? 0.0 : expf(-1.0 *atof(tok1)));
3123 }
3124 }
3125
3126 /* The closing // */
3127 if ((status = esl_fileparser_NextLine(hfp->efp)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data: missing //?");
3128 if ((status = esl_fileparser_GetTokenOnLine(hfp->efp, &tok1, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Premature end of data: missing //?");
3129 if (strcmp(tok1, "//") != 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "Expected closing //; found %s instead", tok1);
3130
3131 /* legacy issues */
3132 if (hfp->format < p7_HMMFILE_3e && (status = p7_hmm_SetConsensus(hmm, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Failed to set consensus on legacy HMM format");
3133
3134 /* Finish up. */
3135 if (hmm->flags & p7H_RF) { hmm->rf[0] = ' '; hmm->rf[hmm->M+1] = '\0'; }
3136 if (hmm->flags & p7H_MMASK){ hmm->mm[0] = ' '; hmm->mm[hmm->M+1] = '\0'; }
3137 if (hmm->flags & p7H_CONS) { hmm->consensus[0] = ' '; hmm->consensus[hmm->M+1] = '\0'; }
3138 if (hmm->flags & p7H_CS) { hmm->cs[0] = ' '; hmm->cs[hmm->M+1] = '\0'; }
3139 if (hmm->flags & p7H_MAP) { hmm->map[0] = 0; }
3140 if (hmm->name == NULL) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "No NAME found for HMM");
3141 if (hmm->M <= 0) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "No LENG found for HMM (or LENG <= 0)");
3142 if (abc == NULL) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "No ALPH found for HMM");
3143
3144 if (*ret_abc == NULL) *ret_abc = abc;
3145 if ( opt_hmm != NULL) *opt_hmm = hmm; else p7_hmm_Destroy(hmm);
3146 return eslOK;
3147
3148 ERROR:
3149 if (*ret_abc == NULL && abc != NULL) esl_alphabet_Destroy(abc);
3150 if (hmm != NULL) p7_hmm_Destroy(hmm);
3151 if (opt_hmm != NULL) *opt_hmm = NULL;
3152 if (status == eslEMEM || status == eslESYS) return status;
3153 else if (status == eslEOF) return status;
3154 else if (status == eslEINCOMPAT) return status;
3155 else return eslEFORMAT; /* anything else is a format error: includes premature EOF, EOL, EOD */
3156 }
3157
3158
3159 static int
3160 read_bin30hmm(P7_HMMFILE *hfp, ESL_ALPHABET **ret_abc, P7_HMM **opt_hmm)
3161 {
3162 ESL_ALPHABET *abc = NULL;
3163 P7_HMM *hmm = NULL;
3164 uint32_t magic;
3165 int alphabet_type;
3166 int k;
3167 off_t offset = 0;
3168 int status;
3169
3170 hfp->rr_errbuf[0] = '\0'; /* hmmer version of this function uses hfp->errbuf, don't use that here, see comment near top of read_asc30hmm */
3171 if (feof(hfp->f)) { status = eslEOF; goto ERROR; }
3172
3173 if (hfp->newly_opened)
3174 {
3175 offset = 0;
3176 hfp->newly_opened = FALSE;
3177 }
3178 else
3179 { /* Check magic. */
3180 if ((!hfp->do_stdin) && (! hfp->do_gzip)) {
3181 if ((offset = ftello(hfp->f)) < 0) ESL_XEXCEPTION(eslESYS, "ftello() failed");
3182 }
3183 if (! fread((char *) &magic, sizeof(uint32_t), 1, hfp->f)) { status = eslEOF; goto ERROR; }
3184
3185 if (hfp->format == p7_HMMFILE_3f) { if (magic != v3f_magic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "bad magic number at start of HMM"); }
3186 else if (hfp->format == p7_HMMFILE_3e) { if (magic != v3e_magic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "bad magic number at start of HMM"); }
3187 else if (hfp->format == p7_HMMFILE_3d) { if (magic != v3d_magic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "bad magic number at start of HMM"); }
3188 else if (hfp->format == p7_HMMFILE_3c) { if (magic != v3c_magic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "bad magic number at start of HMM"); }
3189 else if (hfp->format == p7_HMMFILE_3b) { if (magic != v3b_magic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "bad magic number at start of HMM"); }
3190 else if (hfp->format == p7_HMMFILE_3a) { if (magic != v3a_magic) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "bad magic number at start of HMM"); }
3191 else ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "no such HMM file format code");
3192 }
3193
3194 /* Allocate shell of the new HMM.
3195 * Two-step allocation lets us read/set the flags first;
3196 * then the later CreateBody() call will allocate optional internal fields we need.
3197 */
3198 if ((hmm = p7_hmm_CreateShell()) == NULL) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "allocation failed, HMM shell");
3199 hmm->offset = offset;
3200
3201 /* Get sizes of things */
3202 /* xref J5/114 for a legacy use of <flags> for optional acc, desc annotation */
3203 if (! fread((char *) &(hmm->flags), sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read flags");
3204 if (! fread((char *) &(hmm->M), sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read model size M");
3205 if (! fread((char *) &alphabet_type, sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read alphabet_type");
3206
3207 /* Set or verify alphabet. */
3208 if (*ret_abc == NULL) { /* still unknown: set it, pass control of it back to caller */
3209 if ((abc = esl_alphabet_Create(alphabet_type)) == NULL) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "allocation failed, alphabet");
3210 } else { /* already known: check it */
3211 abc = *ret_abc;
3212 if (abc->type != alphabet_type) ESL_XFAIL(eslEINCOMPAT, hfp->rr_errbuf, "Alphabet type mismatch: was %s, but current HMM says %s", esl_abc_DecodeType( abc->type), esl_abc_DecodeType(alphabet_type));
3213 }
3214
3215 /* Finish the allocation of the HMM
3216 */
3217 if ((status = p7_hmm_CreateBody(hmm, hmm->M, abc)) != eslOK) ESL_XFAIL(eslEMEM, hfp->rr_errbuf, "allocation failed, HMM body");
3218
3219 /* Core model probabilities. */
3220 for (k = 1; k <= hmm->M; k++)
3221 if (! fread((char *) hmm->mat[k], sizeof(float), hmm->abc->K, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read mat[%d]", k);
3222 for (k = 0; k <= hmm->M; k++)
3223 if (! fread((char *) hmm->ins[k], sizeof(float), hmm->abc->K, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read ins[%d]", k);
3224 for (k = 0; k <= hmm->M; k++)
3225 if (! fread((char *) hmm->t[k], sizeof(float), p7H_NTRANSITIONS, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read t[%d]", k);
3226
3227 /* Annotations. */
3228 if (read_bin_string(hfp->f, &(hmm->name)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read name");
3229 if ((hmm->flags & p7H_ACC) && read_bin_string(hfp->f, &(hmm->acc)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read acc");
3230 if ((hmm->flags & p7H_DESC) && read_bin_string(hfp->f, &(hmm->desc)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read desc");
3231 if ((hmm->flags & p7H_RF) && ! fread((char *) hmm->rf, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read rf"); /* +2: 1..M and trailing \0 */
3232 if ((hmm->flags & p7H_MMASK)&& ! fread((char *) hmm->mm, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read mm"); /* +2: 1..M and trailing \0 */
3233 if ((hmm->flags & p7H_CONS) && ! fread((char *) hmm->consensus, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read consensus"); /* don't need to test for >=3e format, because the flag is sufficient (didn't exist pre-3e) */
3234 if ((hmm->flags & p7H_CS) && ! fread((char *) hmm->cs, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read cs");
3235 if ((hmm->flags & p7H_CA) && ! fread((char *) hmm->ca, sizeof(char), hmm->M+2, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read ca");
3236 if (read_bin_string(hfp->f, &(hmm->comlog)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read comlog");
3237 if (! fread((char *) &(hmm->nseq), sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read nseq");
3238 if (! fread((char *) &(hmm->eff_nseq), sizeof(float), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read eff_nseq");
3239 if (hfp->format >= p7_HMMFILE_3c) {
3240 if (! fread((char *) &(hmm->max_length), sizeof(int), 1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read max_length");
3241 }
3242 if (read_bin_string(hfp->f, &(hmm->ctime)) != eslOK) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read ctime");
3243 if ((hmm->flags & p7H_MAP) && ! fread((char *) hmm->map, sizeof(int), hmm->M+1, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read map");
3244 if (! fread((char *) &(hmm->checksum), sizeof(uint32_t),1,hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read checksum");
3245
3246 /* E-value parameters and Pfam cutoffs */
3247 if (hfp->format >= p7_HMMFILE_3b) {
3248 if (! fread((char *) hmm->evparam, sizeof(float), p7_NEVPARAM, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read statistical params");
3249 } else if (hfp->format == p7_HMMFILE_3a) {
3250 /* 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 */
3251 if (! fread((char *) hmm->evparam, sizeof(float), 3, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read statistical params");
3252 hmm->evparam[p7_FLAMBDA] = hmm->evparam[0];
3253 hmm->evparam[p7_FTAU] = hmm->evparam[2];
3254 hmm->evparam[p7_VLAMBDA] = hmm->evparam[0];
3255 hmm->evparam[p7_VMU] = hmm->evparam[1];
3256 hmm->evparam[p7_MLAMBDA] = hmm->evparam[p7_VLAMBDA];
3257 hmm->evparam[p7_MMU] = hmm->evparam[p7_VMU];
3258 }
3259 if (! fread((char *) hmm->cutoff, sizeof(float), p7_NCUTOFFS, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read Pfam score cutoffs");
3260 if ((hmm->flags & p7H_COMPO) && ! fread((char *) hmm->compo, sizeof(float), hmm->abc->K, hfp->f)) ESL_XFAIL(eslEFORMAT, hfp->rr_errbuf, "failed to read model composition");
3261
3262 /* other legacy issues */
3263 if (hfp->format < p7_HMMFILE_3e && (status = p7_hmm_SetConsensus(hmm, NULL)) != eslOK) ESL_XFAIL(status, hfp->rr_errbuf, "Failed to set consensus on legacy HMM format");
3264
3265 if (*ret_abc == NULL) *ret_abc = abc; /* pass our new alphabet back to caller, if caller didn't know it already */
3266 if ( opt_hmm != NULL) *opt_hmm = hmm; else p7_hmm_Destroy(hmm);
3267 return eslOK;
3268
3269 ERROR:
3270 if (*ret_abc == NULL && abc != NULL) esl_alphabet_Destroy(abc); /* the test is for an alphabet created here, not passed */
3271 if (hmm != NULL) p7_hmm_Destroy(hmm);
3272 if (opt_hmm != NULL) *opt_hmm = NULL;
3273 return status;
3274 }
3275 /*--------------- end, private format parsers -------------------*/
3276
3277
3278
3279
3280
3281 /*****************************************************************
3282 * 6. Other private functions involved in i/o
3283 *****************************************************************/
3284
3285 /*****************************************************************
3286 * Some miscellaneous utility functions
3287 *****************************************************************/
3288
3289 /* Function: read_bin_string()
3290 * Date: SRE, Wed Oct 29 14:03:23 1997 [TWA 721]
3291 *
3292 * Purpose: Read in a string from a binary file, where
3293 * the first integer is the length (including '\0').
3294 * If the length is 0, <*ret_s> is set to <NULL>.
3295 *
3296 * This is a reasonable convention for storing/ reading
3297 * strings in binary files. Note that because the length is
3298 * inclusive of '\0', there's a difference between a NULL
3299 * string and an empty string.
3300 *
3301 * Args: fp - FILE to read from
3302 * ret_s - string to read into
3303 *
3304 * Return: <eslOK> on success. ret_s is malloc'ed here.
3305 * <eslEOD> if a read fails - likely because no more
3306 * data in file.
3307 *
3308 * Throws <eslEMEM> on allocation error.
3309 */
3310 static int
3311 read_bin_string(FILE *fp, char **ret_s)
3312 {
3313 int status;
3314 char *s = NULL;
3315 int len;
3316
3317 if (! fread((char *) &len, sizeof(int), 1, fp)) { status = eslEOD; goto ERROR; }
3318 if (len > 0) {
3319 ESL_ALLOC(s, (sizeof(char) * len));
3320 if (! fread((char *) s, sizeof(char), len, fp)) { status = eslEOD; goto ERROR; }
3321 }
3322 *ret_s = s;
3323 return eslOK;
3324
3325 ERROR:
3326 if (s != NULL) free(s);
3327 *ret_s = NULL;
3328 return status;
3329 }
3330
3331 /* Function: prob2ascii()
3332 *
3333 * Purpose: Format a probability for output to an ASCII save
3334 * file. Returns a ptr to a static internal buffer.
3335 *
3336 */
3337 static char *
3338 prob2ascii(float p, float null)
3339 {
3340 static char buffer[32];
3341
3342 if (p == 0.0) return "*";
3343 snprintf(buffer, 32, "%.3f", sreLOG2(p/null)); /* 32 matches static buffer size */
3344 return buffer;
3345 }
3346
3347
3348 /* Function: ascii2prob()
3349 *
3350 * Purpose: Convert a saved string back to a probability.
3351 */
3352 static float
3353 ascii2prob(char *s, float null)
3354 {
3355 return (*s == '*') ? 0. : exp(atof(s)/1.44269504)*null;
3356 }
3357
3358 /* EPN, Tue Aug 7 15:54:15 2007
3359 * is_integer() and is_real(), savagely ripped verbatim out
3360 * of Easel's esl_getopts.c, where they were private.
3361 */
3362 /* Function: is_integer()
3363 *
3364 * Returns TRUE if <s> points to something that atoi() will parse
3365 * completely and convert to an integer.
3366 */
3367 static int
3368 is_integer(char *s)
3369 {
3370 int hex = 0;
3371
3372 if (s == NULL) return 0;
3373 while (isspace((int) (*s))) s++; /* skip whitespace */
3374 if (*s == '-' || *s == '+') s++; /* skip leading sign */
3375 /* skip leading conversion signals */
3376 if ((strncmp(s, "0x", 2) == 0 && (int) strlen(s) > 2) ||
3377 (strncmp(s, "0X", 2) == 0 && (int) strlen(s) > 2))
3378 {
3379 s += 2;
3380 hex = 1;
3381 }
3382 else if (*s == '0' && (int) strlen(s) > 1)
3383 s++;
3384 /* examine remainder for garbage chars */
3385 if (!hex)
3386 while (*s != '\0')
3387 {
3388 if (!isdigit((int) (*s))) return 0;
3389 s++;
3390 }
3391 else
3392 while (*s != '\0')
3393 {
3394 if (!isxdigit((int) (*s))) return 0;
3395 s++;
3396 }
3397 return 1;
3398 }
3399
3400
3401 /* is_real()
3402 *
3403 * Returns TRUE if <s> is a string representation
3404 * of a valid floating point number, convertable
3405 * by atof().
3406 */
3407 static int
3408 is_real(char *s)
3409 {
3410 int gotdecimal = 0;
3411 int gotexp = 0;
3412 int gotreal = 0;
3413
3414 if (s == NULL) return 0;
3415
3416 while (isspace((int) (*s))) s++; /* skip leading whitespace */
3417 if (*s == '-' || *s == '+') s++; /* skip leading sign */
3418
3419 /* Examine remainder for garbage. Allowed one '.' and
3420 * one 'e' or 'E'; if both '.' and e/E occur, '.'
3421 * must be first.
3422 */
3423 while (*s != '\0')
3424 {
3425 if (isdigit((int) (*s))) gotreal++;
3426 else if (*s == '.')
3427 {
3428 if (gotdecimal) return 0; /* can't have two */
3429 if (gotexp) return 0; /* e/E preceded . */
3430 else gotdecimal++;
3431 }
3432 else if (*s == 'e' || *s == 'E')
3433 {
3434 if (gotexp) return 0; /* can't have two */
3435 else gotexp++;
3436 }
3437 else if (isspace((int) (*s)))
3438 break;
3439 s++;
3440 }
3441
3442 while (isspace((int) (*s))) s++; /* skip trailing whitespace */
3443 if (*s == '\0' && gotreal) return 1;
3444 else return 0;
3445 }
3446
3447 /*---------------- end, private utilities -----------------------*/
3448
3449 /*****************************************************************
3450 * 7. Legacy v1.0 ascii file format output.
3451 *****************************************************************/
3452
3453 /* Function: cm_file_Write1p0Ascii()
3454 * Incept: EPN, Tue Feb 28 14:35:47 2012
3455 *
3456 * Purpose: Write a CM in version 1.0 format.
3457 * cm_io.c:write_ascii_cm() from Infernal 1.0.2
3458 * was used as the starting point for this function.
3459 *
3460 * Calibration parameters: E-values and HMM filter
3461 * thresholds are not written here. This is because 1.0
3462 * expects either both E-values and HMM filter thresholds
3463 * or neither and HMM filter thresholds don't exist
3464 * in the new format.
3465 *
3466 * Returns: eslOK on success;
3467 */
3468 int
3469 cm_file_Write1p0ASCII(FILE *fp, CM_t *cm)
3470 {
3471 int status;
3472 int v,x,y,nd;
3473 char *comlog2print = NULL;
3474
3475 fprintf(fp, "INFERNAL-1 [converted from %s]\n", INFERNAL_VERSION);
3476
3477 fprintf(fp, "NAME %s\n", cm->name);
3478 if (cm->acc != NULL) fprintf(fp, "ACC %s\n", cm->acc);
3479 if (cm->desc != NULL) fprintf(fp, "DESC %s\n", cm->desc);
3480 /* Rfam cutoffs */
3481 if (cm->flags & CMH_GA) fprintf(fp, "GA %.2f\n", cm->ga);
3482 if (cm->flags & CMH_TC) fprintf(fp, "TC %.2f\n", cm->tc);
3483 if (cm->flags & CMH_NC) fprintf(fp, "NC %.2f\n", cm->nc);
3484 fprintf(fp, "STATES %d\n", cm->M);
3485 fprintf(fp, "NODES %d\n", cm->nodes);
3486 fprintf(fp, "ALPHABET %d\n", cm->abc->type);
3487 fprintf(fp, "ELSELF %.8f\n", cm->el_selfsc);
3488 fprintf(fp, "WBETA %g\n", cm->beta_W);
3489 fprintf(fp, "NSEQ %d\n", cm->nseq);
3490 fprintf(fp, "EFFNSEQ %.3f\n", cm->eff_nseq);
3491 fprintf(fp, "CLEN %d\n", cm->clen);
3492
3493 /* Print out the BCOM line as the cm->comlog string up to the first
3494 * '\n' or the end of the string. Print cm->ctime as BDATE. We
3495 * don't print CCOM and CDATE. This should be okay because we won't
3496 * likely have E-value and HMM filter stats since we're probably
3497 * converting from a 1.1 or later file. But it is possible we're
3498 * converting from a 1.0 file to a 1.0 file in which case we output
3499 * E-value stats and filter threshold stats but no CCOM and CDATE
3500 * lines. This won't affect the parsing of the file by 1.0 though.
3501 */
3502 if(cm->comlog != NULL) {
3503 ESL_ALLOC(comlog2print, sizeof(char) * (strlen(cm->comlog)+1));
3504 x = 0;
3505 while(x < strlen(cm->comlog) && cm->comlog[x] != '\n') { comlog2print[x] = cm->comlog[x]; x++; }
3506 comlog2print[x] = '\0';
3507 fprintf(fp, "BCOM %s\n", comlog2print);
3508 free(comlog2print);
3509 }
3510 if(cm->ctime != NULL) fprintf(fp, "BDATE %s\n", cm->ctime);
3511
3512 fputs( "NULL ", fp);
3513 for (x = 0; x < cm->abc->K; x++)
3514 fprintf(fp, "%6s ", prob2ascii(cm->null[x], 1/(float)(cm->abc->K)));
3515 fputs("\n", fp);
3516
3517 /* E-value statistics skipped in converted output
3518 * mainly because HMM filter thresholds no longer exist
3519 * in current version so we can't output them here.
3520 */
3521 /* main model section */
3522 fputs("MODEL:\n", fp);
3523 for (v = 0; v < cm->M; v++)
3524 {
3525 nd = cm->ndidx[v];
3526
3527 /* Node line.
3528 */
3529 if (cm->nodemap[nd] == v)
3530 fprintf(fp, "\t\t\t\t[ %-4s %4d ]\n", Nodetype(cm->ndtype[nd]), nd);
3531
3532 /* State line, w/ parents, children, and transitions
3533 */
3534 fprintf(fp, " %2s %5d %5d %1d %5d %5d ",
3535 Statetype(cm->sttype[v]), v,
3536 cm->plast[v], cm->pnum[v],
3537 cm->cfirst[v], cm->cnum[v]);
3538 if (cm->sttype[v] != B_st)
3539 for (x = 0; x < cm->cnum[v]; x++)
3540 fprintf(fp, "%7s ", prob2ascii(cm->t[v][x], 1.));
3541 else x = 0;
3542 for (; x < 6; x++)
3543 fprintf(fp, "%7s ", "");
3544
3545 /* Emission line
3546 */
3547 if (cm->sttype[v] == MP_st)
3548 {
3549 for (x = 0; x < cm->abc->K; x++)
3550 for (y = 0; y < cm->abc->K; y++)
3551 fprintf(fp, "%6s ", prob2ascii(cm->e[v][x*cm->abc->K+y], cm->null[x]*cm->null[y]));
3552 }
3553 else if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st || cm->sttype[v] == IL_st || cm->sttype[v] == IR_st)
3554 {
3555 for (x = 0; x < cm->abc->K; x++)
3556 fprintf(fp, "%6s ", prob2ascii(cm->e[v][x], cm->null[x]));
3557 }
3558 fputs("\n", fp);
3559 }
3560 fputs("//\n", fp);
3561 return eslOK;
3562
3563 ERROR:
3564 ESL_EXCEPTION(eslEMEM, "out of memory");
3565 return status;
3566 }
3567
3568 /* multiline()
3569 *
3570 * Stolen from HMMER3, verbatim.
3571 *
3572 * Used to print the command log to ASCII save files.
3573 *
3574 * Given a record (like the comlog) that contains
3575 * multiple lines, print it as multiple lines with
3576 * a given prefix. e.g.:
3577 *
3578 * given: "COM ", "foo\nbar\nbaz"
3579 * print: COM 1 foo
3580 * COM 2 bar
3581 * COM 3 baz
3582 *
3583 * If <s> is NULL, no-op. Otherwise <s> must be a <NUL>-terminated
3584 * string. It does not matter if it ends in <\n> or not. <pfx>
3585 * must be a valid <NUL>-terminated string; it may be empty.
3586 *
3587 * Args: fp: FILE to print to
3588 * pfx: prefix for each line
3589 * s: line to break up and print; tolerates a NULL
3590 *
3591 * Returns: <eslOK> on success.
3592 *
3593 * Throws: <eslEWRITE> on write error.
3594 */
3595 static int
3596 multiline(FILE *fp, const char *pfx, char *s)
3597 {
3598 char *sptr = s;
3599 char *end = NULL;
3600 int n = 0;
3601 int nline = 1;
3602
3603 do {
3604 end = strchr(sptr, '\n');
3605
3606 if (end != NULL) /* if there's no \n left, end == NULL */
3607 {
3608 n = end - sptr; /* n chars exclusive of \n */
3609 if (fprintf(fp, "%s [%d] ", pfx, nline++) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed");
3610 if (fwrite(sptr, sizeof(char), n, fp) != n) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); /* using fwrite lets us write fixed # of chars */
3611 if (fprintf(fp, "\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); /* while writing \n w/ printf allows newline conversion */
3612 sptr += n + 1; /* +1 to get past \n */
3613 }
3614 else
3615 {
3616 if (fprintf(fp, "%s [%d] %s\n", pfx, nline++, sptr) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "hmm write failed"); /* last line */
3617 }
3618 } while (end != NULL && *sptr != '\0'); /* *sptr == 0 if <s> terminates with a \n */
3619 return eslOK;
3620 }
3621
3622
3623 /*****************************************************************
3624 * 8. Benchmark driver.
3625 *****************************************************************/
3626 #ifdef CM_FILE_BENCHMARK
3627 /*
3628 gcc -pthread -std=gnu99 -g -Wall -static -o cm_file_benchmark -I. -L. -I../hmmer/src -L../hmmer/src -I../easel -L../easel -DCM_FILE_BENCHMARK cm_file.c -linfernal -lhmmer -leasel -lm
3629 icc -pthread -O3 -static -o cm_file_benchmark -I. -L. -I../hmmer/src -L../hmmer/src -I../easel -L../easel -DCM_FILE_BENCHMARK cm_file.c -linfernal -lhmmer -leasel -lm
3630 ./cm_file_benchmark Rfam.cm
3631 */
3632 #include <esl_config.h>
3633 #include <p7_config.h>
3634 #include "config.h"
3635
3636 #include <stdlib.h>
3637 #include <stdio.h>
3638
3639 #include "easel.h"
3640 #include "esl_getopts.h"
3641 #include "esl_stopwatch.h"
3642
3643 #include "hmmer.h"
3644
3645 #include "infernal.h"
3646
3647 static ESL_OPTIONS options[] = {
3648 /* name type default env range toggles reqs incomp help docgroup*/
3649 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
3650 { "-a", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "include time of CM configuration", 0 },
3651 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "verbose: print model info as they're read", 0 },
3652 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
3653 };
3654 static char usage[] = "[-options] <CM file>";
3655 static char banner[] = "benchmark driver for CM input";
3656
3657 int
3658 main(int argc, char **argv)
3659 {
3660 ESL_GETOPTS *go = cm_CreateDefaultApp(options, 1, argc, argv, banner, usage);
3661 ESL_STOPWATCH *w = esl_stopwatch_Create();
3662 ESL_STOPWATCH *w2 = esl_stopwatch_Create();
3663 ESL_ALPHABET *abc = NULL;
3664 char *cmfile = esl_opt_GetArg(go, 1);
3665 CM_FILE *cmfp = NULL;
3666 CM_t *cm = NULL;
3667 int nmodel = 0;
3668 uint64_t tot_clen = 0;
3669 int status;
3670 int be_verbose = esl_opt_GetBoolean(go, "-v");
3671 char errbuf[eslERRBUFSIZE];
3672
3673 esl_stopwatch_Start(w);
3674
3675 status = cm_file_Open(cmfile, NULL, FALSE, &cmfp, errbuf);
3676 if (status == eslENOTFOUND) cm_Fail("File existence/permissions problem in trying to open CM file %s.\n%s\n", cmfile, errbuf);
3677 else if (status == eslEFORMAT) cm_Fail("File format problem in trying to open CM file %s.\n%s\n", cmfile, errbuf);
3678 else if (status != eslOK) cm_Fail("Unexpected error %d in opening CM file %s.\n%s\n", status, cmfile, errbuf);
3679 if (cmfp->do_gzip) cm_Fail("Reading gzipped CM files is not supported");
3680 if (cmfp->do_stdin) cm_Fail("Reading CM files from stdin is not supported");
3681
3682 if(be_verbose) esl_stopwatch_Start(w2);
3683 while ((status = cm_file_Read(cmfp, &abc, &cm)) == eslOK)
3684 {
3685 nmodel++;
3686 tot_clen += cm->clen;
3687
3688 if (esl_opt_GetBoolean(go, "-a")) {
3689 if((status = cm_Configure(cm, errbuf, -1)) != eslOK) cm_Fail(errbuf);
3690 }
3691
3692 if(be_verbose) {
3693 esl_stopwatch_Stop(w2);
3694 printf("%-30s ", cm->name);
3695 esl_stopwatch_Display(stdout, w2, "CPU time: ");
3696 esl_stopwatch_Start(w2);
3697 }
3698 FreeCM(cm);
3699 }
3700 if (status == eslEFORMAT) cm_Fail("bad file format in CM file %s\n%s", cmfile, cmfp->errbuf);
3701 else if (status == eslEINCOMPAT) cm_Fail("CM file %s contains different alphabets\n%s", cmfile, cmfp->errbuf);
3702 else if (status != eslEOF) cm_Fail("Unexpected error in reading CMs from %s\n%s", cmfile, cmfp->errbuf);
3703
3704 esl_stopwatch_Stop(w);
3705 esl_stopwatch_Display(stdout, w, "# CPU time: ");
3706 printf("# number of models: %d\n", nmodel);
3707 printf("# total clen: %" PRId64 "\n", tot_clen);
3708
3709 cm_file_Close(cmfp);
3710 esl_alphabet_Destroy(abc);
3711 esl_stopwatch_Destroy(w);
3712 esl_stopwatch_Destroy(w2);
3713 esl_getopts_Destroy(go);
3714 return 0;
3715 }
3716 #endif /*cm_FILE_BENCHMARK*/
3717 /*---------------- end, benchmark driver ------------------------*/
3718
3719
3720 /*****************************************************************
3721 * 9. Example.
3722 *****************************************************************/
3723 /* On using the example to test error messages from cm_file_Open():
3724 * Message
3725 * --------------
3726 * .gz file missing/not readable \rm test.cm.gz; touch test.cm.gz; src/cmfile_example test.cm.gz
3727 * gzip -dc doesn't exist \cp testsuite/20aa.cm test.cm; gzip test.cm; sudo mv /usr/bin/gzip /usr/bin/gzip.old; src/cmfile_example test.cm.gz
3728 * cm file not found \rm test.cm; src/cmfile_example test.cm
3729 * bad SSI file format \cp testsuite/20aa.cm test.cm; \rm test.cm.ssi; touch test.cm.ssi; src/cmfile_example test.cm
3730 * 64-bit SSI on 32-bit sys
3731 * empty file \rm test.cm; touch test.cm
3732 * unrecognized format (binary) cat testsuite/20aa.cm > test.cm; src/cmpress test.cm; \rm test.cm; [edit test.cm.h3m, delete first byte]
3733 * unrecognized format (ascii) cat testsuite/20aa.cm | sed -e 's/^HMMER3\/b/HMMER3\/x/' > test.hmm
3734 *
3735 */
3736
3737 #ifdef CMFILE_EXAMPLE
3738 /* gcc -g -Wall -DCMFILE_EXAMPLE -I. -I../easel -L. -L../easel -o cmfile_example cm_file.c -linfernal -lhmmer -leasel -lm
3739 */
3740 #include <esl_config.h>
3741 #include <p7_config.h>
3742 #include "config.h"
3743
3744 #include "easel.h"
3745 #include "esl_alphabet.h"
3746
3747 #include "hmmer.h"
3748
3749 #include "infernal.h"
3750
3751 int
3752 main(int argc, char **argv)
3753 {
3754 char *cmfile = argv[1];
3755 CM_FILE *cmfp = NULL;
3756 CM_t *cm = NULL;
3757 ESL_ALPHABET *abc = NULL;
3758 char errbuf[eslERRBUFSIZE];
3759 int status;
3760
3761 /* An example of reading a single CM from a file, and checking that it is the only one. */
3762 status = cm_file_Open(cmfile, NULL, FALSE, &cmfp, errbuf);
3763 if (status == eslENOTFOUND) cm_Fail("File existence/permissions problem in trying to open CM file %s.\n%s\n", cmfile, errbuf);
3764 else if (status == eslEFORMAT) cm_Fail("File format problem in trying to open CM file %s.\n%s\n", cmfile, errbuf);
3765 else if (status != eslOK) cm_Fail("Unexpected error %d in opening CM file %s.\n%s\n", status, cmfile, errbuf);
3766
3767 status = cm_file_Read(cmfp, TRUE, &abc, &cm);
3768 if (status == eslEFORMAT) cm_Fail("Bad file format in CM file %s:\n%s\n", cmfp->fname, cmfp->errbuf);
3769 else if (status == eslEINCOMPAT) cm_Fail("CM in %s is not in the expected %s alphabet\n", cmfp->fname, esl_abc_DecodeType(abc->type));
3770 else if (status == eslEOF) cm_Fail("Empty CM file %s? No CM data found.\n", cmfp->fname);
3771 else if (status != eslOK) cm_Fail("Unexpected error in reading CMs from %s\n", cmfp->fname);
3772
3773 status = cm_file_Read(cmfp, TRUE, &abc, NULL);
3774 if (status != eslEOF) cm_Fail("CM file %s does not contain just one CM\n", cmfp->fname);
3775
3776 cm_file_Close(cmfp);
3777
3778 cm_file_WriteASCII(stdout, -1, cm);
3779
3780 esl_alphabet_Destroy(abc);
3781 FreeCM(cm);
3782 return 0;
3783 }
3784 #endif /*CMFILE_EXAMPLE*/
3785 /*----------------------- end, example --------------------------*/
3786