Debian Codegraph @

src/cmfetch.c

raw symbols SCIP
1 /* cmfetch: Fetch a CM from a CM database (such as Rfam).
2 * Based heavily on SRE's hmmfetch from HMMER3.
3 *
4 * EPN, Sat Mar 20 11:17:30 2010
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 /* name type default env range togs reqs incomp help docgroup */
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; /* application configuration */
73 char *cmfile = NULL; /* CM file name */
74 char *keyfile = NULL; /* keyfile name */
75 char *keyname = NULL; /* key name */
76 CM_FILE *cmfp = NULL; /* open CM file */
77 FILE *ofp = NULL; /* output stream for CMs */
78 char errbuf[eslERRBUFSIZE];
79
80 /***********************************************
81 * Parse command line
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 /* Check arguments. Consider three modes separately.
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 /* Open the CM file. */
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 /* Die if we're already press'd: SSI index exists and going forward
130 * would create <cmfile>.i1m.ssi which would never be used (<cmfile>.i1i
131 * would always be used instead).
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 /* Open the output file, if any */
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 /* Hand off to the appropriate routine */
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 /* Create an SSI index file for open CM file <cmfp>.
168 * Both name and accession of CMs are stored as keys.
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) /* 0 = format code (CMs don't have any yet) */
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 } /* end of while cm_file_Read() */
209 if(status != eslEOF) cm_Fail(cmfp->errbuf); /* cm_file_Read() returned an error, die. */
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 /* multifetch:
229 * given a file containing lines with one name or key per line;
230 * parse the file line-by-line;
231 * if we have an SSI index available, retrieve the CMs by key
232 * as we see each line;
233 * else, without an SSI index, store the keys in a hash, then
234 * read the entire CM file in a single pass, outputting CMs
235 * that are in our keylist.
236 *
237 * Note that with an SSI index, you get the CMs in the order they
238 * appear in the <keyfile>, but without an SSI index, you get CMs in
239 * the order they occur in the CM file.
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 } /* end of while cm_file_Read() */
280 if(status != eslEOF) cm_Fail(cmfp->errbuf); /* cm_file_Read() returned an error, die. */
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 /* onefetch():
293 * Given one <key> (a CM name or accession), retrieve the corresponding CM.
294 * In SSI mode, we can do this quickly by positioning the file, then reading
295 * and writing the CM that's at that position.
296 * Without an SSI index, we have to parse the CMs sequentially 'til we find
297 * the one we're after.
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); /* cm_file_Read() returned an error, die. */
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