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