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