| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <esl_config.h> |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "easel.h" |
| 12 |
#include "esl_fileparser.h" |
| 13 |
#include "esl_getopts.h" |
| 14 |
#include "esl_keyhash.h" |
| 15 |
#include "esl_ssi.h" |
| 16 |
|
| 17 |
#include "hmmer.h" |
| 18 |
|
| 19 |
static char banner[] = "retrieve profile HMM(s) from a file"; |
| 20 |
static char usage1[] = "[options] <hmmfile> <key> (retrieves HMM named <key>)"; |
| 21 |
static char usage2[] = "[options] -f <hmmfile> <keyfile> (retrieves all HMMs in <keyfile>)"; |
| 22 |
static char usage3[] = "[options] --index <hmmfile> (indexes <hmmfile>)"; |
| 23 |
|
| 24 |
static void |
| 25 |
cmdline_failure(char *argv0, char *format, ...) |
| 26 |
{ |
| 27 |
va_list argp; |
| 28 |
|
| 29 |
va_start(argp, format); |
| 30 |
vfprintf(stderr, format, argp); |
| 31 |
va_end(argp); |
| 32 |
esl_usage(stdout, argv0, usage1); |
| 33 |
esl_usage(stdout, argv0, usage2); |
| 34 |
esl_usage(stdout, argv0, usage3); |
| 35 |
printf("\nTo see more help on available options, do %s -h\n\n", argv0); |
| 36 |
exit(1); |
| 37 |
} |
| 38 |
|
| 39 |
static void |
| 40 |
cmdline_help(char *argv0, ESL_GETOPTS *go) |
| 41 |
{ |
| 42 |
esl_banner(stdout, argv0, banner); |
| 43 |
esl_usage (stdout, argv0, usage1); |
| 44 |
esl_usage (stdout, argv0, usage2); |
| 45 |
esl_usage (stdout, argv0, usage3); |
| 46 |
puts("\nOptions:"); |
| 47 |
esl_opt_DisplayHelp(stdout, go, 0, 2, 80); |
| 48 |
exit(0); |
| 49 |
} |
| 50 |
|
| 51 |
static ESL_OPTIONS options[] = { |
| 52 |
|
| 53 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; show brief info on version and usage", 0 }, |
| 54 |
{ "-f", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,"--index", "second cmdline arg is a file of names to retrieve", 0 }, |
| 55 |
{ "-o", eslARG_OUTFILE,FALSE,NULL, NULL, NULL, NULL,"-O,--index", "output HMM to file <f> instead of stdout", 0 }, |
| 56 |
{ "-O", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,"-o,-f,--index","output HMM to file named <key>", 0 }, |
| 57 |
{ "--index", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "index the <hmmfile>, creating <hmmfile>.ssi", 0 }, |
| 58 |
{ 0,0,0,0,0,0,0,0,0,0 }, |
| 59 |
}; |
| 60 |
|
| 61 |
static void create_ssi_index(ESL_GETOPTS *go, P7_HMMFILE *hfp); |
| 62 |
static void multifetch(ESL_GETOPTS *go, FILE *ofp, char *keyfile, P7_HMMFILE *hfp); |
| 63 |
static void onefetch(ESL_GETOPTS *go, FILE *ofp, char *key, P7_HMMFILE *hfp); |
| 64 |
|
| 65 |
int |
| 66 |
main(int argc, char **argv) |
| 67 |
{ |
| 68 |
ESL_GETOPTS *go = NULL; |
| 69 |
char *hmmfile = NULL; |
| 70 |
char *keyfile = NULL; |
| 71 |
char *keyname = NULL; |
| 72 |
P7_HMMFILE *hfp = NULL; |
| 73 |
FILE *ofp = NULL; |
| 74 |
int status; |
| 75 |
char errbuf[eslERRBUFSIZE]; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
go = esl_getopts_Create(options); |
| 81 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) cmdline_failure(argv[0], "Failed to parse command line: %s\n", go->errbuf); |
| 82 |
if (esl_opt_VerifyConfig(go) != eslOK) cmdline_failure(argv[0], "Error in configuration: %s\n", go->errbuf); |
| 83 |
if (esl_opt_GetBoolean(go, "-h") ) cmdline_help (argv[0], go); |
| 84 |
if (esl_opt_ArgNumber(go) < 1) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n"); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
if (esl_opt_GetBoolean(go, "--index")) |
| 89 |
{ |
| 90 |
if (esl_opt_ArgNumber(go) != 1) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n"); |
| 91 |
|
| 92 |
hmmfile = esl_opt_GetArg(go, 1); |
| 93 |
keyfile = NULL; |
| 94 |
keyname = NULL; |
| 95 |
|
| 96 |
if (strcmp(hmmfile, "-") == 0) cmdline_failure(argv[0], "Can't use - with --index, can't index <stdin>.\n"); |
| 97 |
} |
| 98 |
|
| 99 |
else if (esl_opt_GetBoolean(go, "-f")) |
| 100 |
{ |
| 101 |
if (esl_opt_ArgNumber(go) != 2) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n"); |
| 102 |
|
| 103 |
hmmfile = esl_opt_GetArg(go, 1); |
| 104 |
keyfile = esl_opt_GetArg(go, 2); |
| 105 |
keyname = NULL; |
| 106 |
|
| 107 |
if (strcmp(hmmfile, "-") == 0 && strcmp(keyfile, "-") == 0) |
| 108 |
cmdline_failure(argv[0], "Either <hmmfile> or <keyfile> can be - but not both.\n"); |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
if (esl_opt_ArgNumber(go) != 2) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n"); |
| 113 |
|
| 114 |
hmmfile = esl_opt_GetArg(go, 1); |
| 115 |
keyfile = NULL; |
| 116 |
keyname = esl_opt_GetArg(go, 2); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf); |
| 121 |
if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf); |
| 122 |
else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf); |
| 123 |
else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf); |
| 124 |
|
| 125 |
|
| 126 |
if (esl_opt_GetBoolean(go, "-O")) |
| 127 |
{ |
| 128 |
if (! keyname) p7_Fail("No key name? Can't use -O\n"); |
| 129 |
if ((ofp = fopen(keyname, "w")) == NULL) p7_Fail("Failed to open output file %s\n", keyname); |
| 130 |
} |
| 131 |
else if (esl_opt_GetString(go, "-o") != NULL) |
| 132 |
{ |
| 133 |
if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) |
| 134 |
esl_fatal("Failed to open output file %s\n", esl_opt_GetString(go, "-o")); |
| 135 |
} |
| 136 |
else ofp = stdout; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if (esl_opt_GetBoolean(go, "--index")) create_ssi_index(go, hfp); |
| 141 |
else if (esl_opt_GetBoolean(go, "-f")) multifetch(go, ofp, keyfile, hfp); |
| 142 |
else |
| 143 |
{ |
| 144 |
onefetch(go, ofp, keyname, hfp); |
| 145 |
if (ofp != stdout) printf("\n\nRetrieved HMM %s.\n", keyname); |
| 146 |
} |
| 147 |
|
| 148 |
if (esl_opt_GetBoolean(go, "-O") || esl_opt_GetString(go, "-o") != NULL) fclose(ofp); |
| 149 |
p7_hmmfile_Close(hfp); |
| 150 |
esl_getopts_Destroy(go); |
| 151 |
exit(0); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static void |
| 159 |
create_ssi_index(ESL_GETOPTS *go, P7_HMMFILE *hfp) |
| 160 |
{ |
| 161 |
ESL_NEWSSI *ns = NULL; |
| 162 |
ESL_ALPHABET *abc = NULL; |
| 163 |
P7_HMM *hmm = NULL; |
| 164 |
int nhmm = 0; |
| 165 |
char *ssifile = NULL; |
| 166 |
uint16_t fh; |
| 167 |
int status; |
| 168 |
|
| 169 |
if (esl_sprintf(&ssifile, "%s.ssi", hfp->fname) != eslOK) p7_Die("esl_sprintf() failed"); |
| 170 |
|
| 171 |
status = esl_newssi_Open(ssifile, FALSE, &ns); |
| 172 |
if (status == eslENOTFOUND) esl_fatal("failed to open SSI index %s", ssifile); |
| 173 |
else if (status == eslEOVERWRITE) esl_fatal("SSI index %s already exists; delete or rename it", ssifile); |
| 174 |
else if (status != eslOK) esl_fatal("failed to create a new SSI index"); |
| 175 |
|
| 176 |
if (esl_newssi_AddFile(ns, hfp->fname, 0, &fh) != eslOK) |
| 177 |
esl_fatal("Failed to add HMM file %s to new SSI index\n", hfp->fname); |
| 178 |
|
| 179 |
printf("Working... "); |
| 180 |
fflush(stdout); |
| 181 |
|
| 182 |
while ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) != eslEOF) |
| 183 |
{ |
| 184 |
if (status == eslEOD) p7_Fail("read failed, HMM file %s may be truncated?", hfp->fname); |
| 185 |
else if (status == eslEFORMAT) p7_Fail("bad file format in HMM file %s", hfp->fname); |
| 186 |
else if (status == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets", hfp->fname); |
| 187 |
else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s", hfp->fname); |
| 188 |
nhmm++; |
| 189 |
|
| 190 |
if (hmm->name == NULL) p7_Fail("Every HMM must have a name to be indexed. Failed to find name of HMM #%d\n", nhmm); |
| 191 |
|
| 192 |
if (esl_newssi_AddKey(ns, hmm->name, fh, hmm->offset, 0, 0) != eslOK) |
| 193 |
p7_Fail("Failed to add key %s to SSI index", hmm->name); |
| 194 |
|
| 195 |
if (hmm->acc) { |
| 196 |
if (esl_newssi_AddAlias(ns, hmm->acc, hmm->name) != eslOK) |
| 197 |
p7_Fail("Failed to add secondary key %s to SSI index", hmm->acc); |
| 198 |
} |
| 199 |
p7_hmm_Destroy(hmm); |
| 200 |
} |
| 201 |
|
| 202 |
if (esl_newssi_Write(ns) != eslOK) |
| 203 |
p7_Fail("Failed to write keys to ssi file %s\n", ssifile); |
| 204 |
|
| 205 |
printf("done.\n"); |
| 206 |
if (ns->nsecondary > 0) |
| 207 |
printf("Indexed %d HMMs (%ld names and %ld accessions).\n", nhmm, (long) ns->nprimary, (long) ns->nsecondary); |
| 208 |
else |
| 209 |
printf("Indexed %d HMMs (%ld names).\n", nhmm, (long) ns->nprimary); |
| 210 |
printf("SSI index written to file %s\n", ssifile); |
| 211 |
|
| 212 |
free(ssifile); |
| 213 |
esl_alphabet_Destroy(abc); |
| 214 |
esl_newssi_Close(ns); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
static void |
| 233 |
multifetch(ESL_GETOPTS *go, FILE *ofp, char *keyfile, P7_HMMFILE *hfp) |
| 234 |
{ |
| 235 |
ESL_KEYHASH *keys = esl_keyhash_Create(); |
| 236 |
ESL_FILEPARSER *efp = NULL; |
| 237 |
ESL_ALPHABET *abc = NULL; |
| 238 |
P7_HMM *hmm = NULL; |
| 239 |
int nhmm = 0; |
| 240 |
char *key; |
| 241 |
int keylen; |
| 242 |
int keyidx; |
| 243 |
int status; |
| 244 |
|
| 245 |
if (esl_fileparser_Open(keyfile, NULL, &efp) != eslOK) p7_Fail("Failed to open key file %s\n", keyfile); |
| 246 |
esl_fileparser_SetCommentChar(efp, '#'); |
| 247 |
|
| 248 |
while (esl_fileparser_NextLine(efp) == eslOK) |
| 249 |
{ |
| 250 |
if (esl_fileparser_GetTokenOnLine(efp, &key, &keylen) != eslOK) |
| 251 |
p7_Fail("Failed to read HMM name on line %d of file %s\n", efp->linenumber, keyfile); |
| 252 |
|
| 253 |
status = esl_keyhash_Store(keys, key, -1, &keyidx); |
| 254 |
if (status == eslEDUP) p7_Fail("HMM key %s occurs more than once in file %s\n", key, keyfile); |
| 255 |
|
| 256 |
if (hfp->ssi != NULL) { onefetch(go, ofp, key, hfp); nhmm++; } |
| 257 |
} |
| 258 |
|
| 259 |
if (hfp->ssi == NULL) |
| 260 |
{ |
| 261 |
while ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) != eslEOF) |
| 262 |
{ |
| 263 |
if (status == eslEOD) p7_Fail("read failed, HMM file %s may be truncated?", hfp->fname); |
| 264 |
else if (status == eslEFORMAT) p7_Fail("bad file format in HMM file %s", hfp->fname); |
| 265 |
else if (status == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets", hfp->fname); |
| 266 |
else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s", hfp->fname); |
| 267 |
|
| 268 |
if (esl_keyhash_Lookup(keys, hmm->name, -1, &keyidx) == eslOK || |
| 269 |
((hmm->acc) && esl_keyhash_Lookup(keys, hmm->acc, -1, &keyidx) == eslOK)) |
| 270 |
{ |
| 271 |
p7_hmmfile_WriteASCII(ofp, -1, hmm); |
| 272 |
nhmm++; |
| 273 |
} |
| 274 |
|
| 275 |
p7_hmm_Destroy(hmm); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
if (ofp != stdout) printf("\nRetrieved %d HMMs.\n", nhmm); |
| 280 |
if (abc != NULL) esl_alphabet_Destroy(abc); |
| 281 |
esl_keyhash_Destroy(keys); |
| 282 |
esl_fileparser_Close(efp); |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
static void |
| 295 |
onefetch(ESL_GETOPTS *go, FILE *ofp, char *key, P7_HMMFILE *hfp) |
| 296 |
{ |
| 297 |
ESL_ALPHABET *abc = NULL; |
| 298 |
P7_HMM *hmm = NULL; |
| 299 |
int status; |
| 300 |
|
| 301 |
if (hfp->ssi != NULL) |
| 302 |
{ |
| 303 |
status = p7_hmmfile_PositionByKey(hfp, key); |
| 304 |
if (status == eslENOTFOUND) p7_Fail("HMM %s not found in SSI index for file %s\n", key, hfp->fname); |
| 305 |
else if (status == eslEFORMAT) p7_Fail("Failed to parse SSI index for %s\n", hfp->fname); |
| 306 |
else if (status != eslOK) p7_Fail("Failed to look up location of HMM %s in SSI index of file %s\n", key, hfp->fname); |
| 307 |
} |
| 308 |
|
| 309 |
while ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) != eslEOF) |
| 310 |
{ |
| 311 |
if (status == eslEOD) p7_Fail("read failed, HMM file %s may be truncated?", hfp->fname); |
| 312 |
else if (status == eslEFORMAT) p7_Fail("bad file format in HMM file %s", hfp->fname); |
| 313 |
else if (status == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets", hfp->fname); |
| 314 |
else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s", hfp->fname); |
| 315 |
|
| 316 |
if (strcmp(key, hmm->name) == 0 || (hmm->acc && strcmp(key, hmm->acc) == 0)) break; |
| 317 |
p7_hmm_Destroy(hmm); |
| 318 |
hmm = NULL; |
| 319 |
} |
| 320 |
|
| 321 |
if (status == eslOK) |
| 322 |
{ |
| 323 |
p7_hmmfile_WriteASCII(ofp, -1, hmm); |
| 324 |
p7_hmm_Destroy(hmm); |
| 325 |
} |
| 326 |
else p7_Fail("HMM %s not found in file %s\n", key, hfp->fname); |
| 327 |
|
| 328 |
esl_alphabet_Destroy(abc); |
| 329 |
} |