Debian Codegraph @

easel/miniapps/esl-afetch.c

raw symbols SCIP
1 /* Fetch an MSA from a multi-MSA database (such as Pfam or Rfam).
2 */
3 #include <esl_config.h>
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #include "easel.h"
10 #include "esl_getopts.h"
11 #include "esl_fileparser.h"
12 #include "esl_keyhash.h"
13 #include "esl_mem.h"
14 #include "esl_ssi.h"
15 #include "esl_msa.h"
16 #include "esl_msafile.h"
17
18
19 static char banner[] = "retrieve multiple sequence alignment(s) from a file";
20 static char usage1[] = "[options] <msafile> <name> (retrieves one alignment named <name>)";
21 static char usage2[] = "[options] -f <msafile> <namefile> (retrieves all alignments named in <namefile>)";
22 static char usage3[] = "[options] --index <msafile> (indexes <msafile>)";
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("\n where options are:");
47 esl_opt_DisplayHelp(stdout, go, 0, 2, 80);
48 exit(0);
49 }
50
51 static ESL_OPTIONS options[] = {
52 /* name type default env range togs reqs incomp help docgroup */
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 alignments to file <f> instead of stdout", 0 },
56 { "-O", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,"-o,-f,--index","output alignment to file named <key>", 0 },
57 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that <msafile> is in format <s>", 0 },
58 { "--outformat",eslARG_STRING,"Stockholm", NULL, NULL, NULL, NULL, "--index", "output fetched alignment(s) in format <s>", 0 },
59 { "--index", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "index the <msafile>, creating <msafile>.ssi", 0 },
60 { 0,0,0,0,0,0,0,0,0,0 },
61 };
62
63 static void create_ssi_index(ESL_GETOPTS *go, ESL_MSAFILE *afp);
64 static void multifetch(ESL_GETOPTS *go, FILE *ofp, int outfmt, char *keyfile, ESL_MSAFILE *afp);
65 static void onefetch (ESL_GETOPTS *go, FILE *ofp, int outfmt, char *key, ESL_MSAFILE *afp);
66 static void regurgitate_one_stockholm_entry(FILE *ofp, ESL_MSAFILE *afp);
67
68 int
69 main(int argc, char **argv)
70 {
71 ESL_GETOPTS *go = NULL; /* application configuration */
72 char *alifile = NULL; /* alignment file name */
73 int infmt = eslMSAFILE_UNKNOWN; /* format code for alifile */
74 int outfmt = eslMSAFILE_UNKNOWN; /* output format for fetched msa's */
75 ESL_MSAFILE *afp = NULL; /* open alignment file */
76 FILE *ofp = NULL; /* output stream for alignments */
77 int status; /* easel return code */
78
79 /***********************************************
80 * Parse command line
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 if (esl_opt_IsOn(go, "--informat")) {
90 infmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--informat"));
91 if (infmt == eslMSAFILE_UNKNOWN) esl_fatal("%s is not a valid input alignment file format for --informat", esl_opt_GetString(go, "--informat"));
92 }
93
94 outfmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--outformat"));
95 if (outfmt == eslMSAFILE_UNKNOWN) esl_fatal("%s is not a valid output alignment file format for --outformat", esl_opt_GetString(go, "--outformat"));
96
97 alifile = esl_opt_GetArg(go, 1);
98
99 /* Open the alignment file. */
100 if ( (status = esl_msafile_Open(NULL, alifile, NULL, infmt, NULL, &afp)) != eslOK)
101 esl_msafile_OpenFailure(afp, status);
102
103 /* Open the SSI index, if any */
104 if (! esl_opt_GetBoolean(go, "--index"))
105 {
106 if (afp->bf->mode_is == eslBUFFER_FILE ||
107 afp->bf->mode_is == eslBUFFER_ALLFILE ||
108 afp->bf->mode_is == eslBUFFER_MMAP)
109 {
110 char *ssifile = NULL;
111 esl_sprintf(&ssifile, "%s.ssi", afp->bf->filename);
112
113 status = esl_ssi_Open(ssifile, &(afp->ssi));
114 if (status == eslERANGE ) esl_fatal("SSI index %s has 64-bit offsets; this system doesn't support them", ssifile);
115 else if (status == eslEFORMAT) esl_fatal("SSI index %s has an unrecognized format. Try recreating, w/ esl-afetch --index", ssifile);
116 else if (status == eslENOTFOUND) afp->ssi = NULL;
117 else if (status != eslOK) esl_fatal("SSI index %s: open failed, error code %d\n", ssifile, status);
118
119 free(ssifile);
120 }
121 }
122
123 /* Open the output file, if any
124 */
125 if (esl_opt_GetBoolean(go, "-O"))
126 {
127 if ((ofp = fopen(esl_opt_GetArg(go, 2), "w")) == NULL)
128 esl_fatal("Failed to open output file %s\n", esl_opt_GetArg(go, 2));
129 }
130 else if (esl_opt_GetString(go, "-o") != NULL)
131 {
132 if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL)
133 esl_fatal("Failed to open output file %s\n", esl_opt_GetString(go, "-o"));
134 }
135 else ofp = stdout;
136
137 /* Hand off control flow as appropriate */
138 if (esl_opt_GetBoolean(go, "--index"))
139 {
140 if (esl_opt_ArgNumber(go) != 1) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
141 create_ssi_index(go, afp);
142 }
143 else if (esl_opt_GetBoolean(go, "-f"))
144 {
145 if (esl_opt_ArgNumber(go) != 2) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
146 multifetch(go, ofp, outfmt, esl_opt_GetArg(go, 2), afp);
147 }
148 else
149 {
150 if (esl_opt_ArgNumber(go) != 2) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
151 onefetch(go, ofp, outfmt, esl_opt_GetArg(go, 2), afp);
152 if (ofp != stdout) printf("\n\nRetrieved alignment %s.\n", esl_opt_GetArg(go, 2));
153 }
154
155 esl_msafile_Close(afp);
156 esl_getopts_Destroy(go);
157 exit(0);
158 }
159
160
161 /* Create an SSI index file for open MSA file <afp>.
162 * Both name and accession of MSAs are stored as keys.
163 */
164 static void
165 create_ssi_index(ESL_GETOPTS *go, ESL_MSAFILE *afp)
166 {
167 ESL_NEWSSI *ns = NULL;
168 ESL_MSA *msa = NULL;
169 int nali = 0;
170 char *ssifile = NULL;
171 uint16_t fh;
172 int status;
173
174 if (afp->bf->mode_is != eslBUFFER_FILE &&
175 afp->bf->mode_is != eslBUFFER_ALLFILE &&
176 afp->bf->mode_is != eslBUFFER_MMAP)
177 esl_fatal("<msafile> must be a regular file to be SSI indexed");
178
179 esl_sprintf(&ssifile, "%s.ssi", afp->bf->filename);
180
181 status = esl_newssi_Open(ssifile, FALSE, &ns);
182 if (status == eslENOTFOUND) esl_fatal("failed to open SSI index %s", ssifile);
183 else if (status == eslEOVERWRITE) esl_fatal("SSI index %s already exists; delete or rename it", ssifile);
184 else if (status != eslOK) esl_fatal("failed to create a new SSI index");
185
186 if (esl_newssi_AddFile(ns, afp->bf->filename, afp->format, &fh) != eslOK)
187 esl_fatal("Failed to add MSA file %s to new SSI index\n", afp->bf->filename);
188
189 printf("Working... ");
190 fflush(stdout);
191
192 while ((status = esl_msafile_Read(afp, &msa)) != eslEOF)
193 {
194 if (status != eslOK)
195 esl_msafile_ReadFailure(afp, status);
196
197 nali++;
198
199 if (! msa->name)
200 esl_fatal("Every alignment in file must have a name to be indexed. Failed to find name of alignment #%d\n", nali);
201
202 if (esl_newssi_AddKey(ns, msa->name, fh, msa->offset, 0, 0) != eslOK)
203 esl_fatal("Failed to add key %s to SSI index", msa->name);
204
205 if (msa->acc && esl_newssi_AddAlias(ns, msa->acc, msa->name) != eslOK)
206 esl_fatal("Failed to add secondary key %s to SSI index", msa->acc);
207
208 esl_msa_Destroy(msa);
209 }
210
211 if (esl_newssi_Write(ns) != eslOK)
212 esl_fatal("\nFailed to write keys to ssi file %s:\n %s", ssifile, ns->errbuf);
213
214 printf("done.\n");
215
216 if (ns->nsecondary) printf("Indexed %d alignments (%ld names and %ld accessions).\n", nali, (long) ns->nprimary, (long) ns->nsecondary);
217 else printf("Indexed %d alignments (%ld names).\n", nali, (long) ns->nprimary);
218 printf("SSI index written to file %s\n", ssifile);
219
220 free(ssifile);
221 esl_newssi_Close(ns);
222 return;
223 }
224
225 /* multifetch:
226 * given a file containing lines with one name or key per line;
227 * parse the file line-by-line;
228 * if we have an SSI index available, retrieve the MSAs by key
229 * as we see each line;
230 * else, without an SSI index, store the keys in a hash, then
231 * read the entire MSA file in a single pass, outputting MSAs
232 * that are in our keylist.
233 *
234 * Note that with an SSI index, you get the MSAs in the order they
235 * appear in the <keyfile>, but without an SSI index, you get MSAs in
236 * the order they occur in the MSA file.
237 */
238 static void
239 multifetch(ESL_GETOPTS *go, FILE *ofp, int outfmt, char *keyfile, ESL_MSAFILE *afp)
240 {
241 ESL_KEYHASH *keys = esl_keyhash_Create();
242 ESL_FILEPARSER *efp = NULL;
243 ESL_MSA *msa = NULL;
244 int nali = 0;
245 char *key;
246 int keylen;
247 int keyidx;
248 int status;
249
250 if (esl_fileparser_Open(keyfile, NULL, &efp) != eslOK)
251 esl_fatal("Failed to open key file %s\n", keyfile);
252 esl_fileparser_SetCommentChar(efp, '#');
253
254 while (esl_fileparser_NextLine(efp) == eslOK)
255 {
256 if (esl_fileparser_GetTokenOnLine(efp, &key, &keylen) != eslOK)
257 esl_fatal("Failed to read MSA name on line %d of file %s\n", efp->linenumber, keyfile);
258
259 status = esl_keyhash_Store(keys, key, keylen, &keyidx);
260 if (status == eslEDUP) esl_fatal("MSA key %s occurs more than once in file %s\n", key, keyfile);
261
262 if (afp->ssi) { onefetch(go, ofp, outfmt, key, afp); nali++; }
263
264 }
265
266 if (! afp->ssi)
267 {
268 while ((status = esl_msafile_Read(afp, &msa)) != eslEOF)
269 {
270 if (status != eslOK) esl_msafile_ReadFailure(afp, status);
271 nali++;
272
273 if (msa->name == NULL)
274 esl_fatal("Every alignment in file must have a name to be retrievable. Failed to find name of alignment #%d\n", nali);
275
276 if ( (esl_keyhash_Lookup(keys, msa->name, -1, NULL) == eslOK) ||
277 (msa->acc != NULL && esl_keyhash_Lookup(keys, msa->acc, -1, NULL) == eslOK))
278 esl_msafile_Write(ofp, msa, outfmt);
279
280 esl_msa_Destroy(msa);
281 }
282 }
283
284 if (ofp != stdout) printf("\nRetrieved %d alignments.\n", nali);
285 esl_keyhash_Destroy(keys);
286 esl_fileparser_Close(efp);
287 return;
288 }
289
290
291 /* onefetch():
292 * Given one <key> (an MSA name or accession), retrieve the corresponding MSA.
293 * In SSI mode, we can do this quickly by positioning the file, then regurgitating
294 * every line until the end-of-alignment marker; we don't even have to parse.
295 * Without an SSI index, we have to parse the MSAs sequentially 'til we find
296 * the one we're after.
297 */
298 static void
299 onefetch(ESL_GETOPTS *go, FILE *ofp, int outfmt, char *key, ESL_MSAFILE *afp)
300 {
301 ESL_MSA *msa = NULL;
302 int nali = 1;
303 int status;
304
305 if (afp->ssi)
306 {
307 status = esl_msafile_PositionByKey(afp, key);
308 if (status == eslENOTFOUND) esl_fatal("MSA %s not found in SSI index for file %s\n", key, afp->bf->filename);
309 else if (status == eslEFORMAT) esl_fatal("Failed to parse SSI index for %s\n", afp->bf->filename);
310 else if (status != eslOK) esl_fatal("Failed to look up location of MSA %s in SSI index of file %s\n", key, afp->bf->filename);
311
312 if ( (afp->format == eslMSAFILE_STOCKHOLM && outfmt == eslMSAFILE_STOCKHOLM) ||
313 (afp->format == eslMSAFILE_PFAM && outfmt == eslMSAFILE_PFAM))
314 {
315 regurgitate_one_stockholm_entry(ofp, afp);
316 }
317 else
318 {
319 if ((status = esl_msafile_Read(afp, &msa)) != eslOK)
320 esl_msafile_ReadFailure(afp, status);
321
322 esl_msafile_Write(ofp, msa, outfmt);
323 esl_msa_Destroy(msa);
324 }
325 }
326 else
327 { /* without an index, we have to brute-force search the file */
328 while ((status = esl_msafile_Read(afp, &msa)) != eslEOF)
329 {
330 if (status != eslOK) esl_msafile_ReadFailure(afp, status);
331 if (! msa->name)
332 esl_fatal("Every alignment in file must have a name to be retrievable. Failed to find name of alignment #%d\n", nali);
333
334 if (strcmp(key, msa->name) == 0 || (msa->acc != NULL && strcmp(key, msa->acc) == 0))
335 break;
336
337 nali++;
338 esl_msa_Destroy(msa);
339 }
340
341 if (! msa) esl_fatal("Failed to find alignment %s\n", key);
342
343 esl_msafile_Write(ofp, msa, outfmt);
344 esl_msa_Destroy(msa);
345 }
346 }
347
348
349 /* regurgitate_one_stockholm_entry()
350 * Read and output an alignment line-by-line without parsing it, stopping when
351 * we reach the end-of-alignment marker.
352 */
353 static void
354 regurgitate_one_stockholm_entry(FILE *ofp, ESL_MSAFILE *afp)
355 {
356 char *p;
357 esl_pos_t n;
358 int status;
359
360 while ( (status = esl_msafile_GetLine(afp, &p, &n)) == eslOK)
361 {
362 fwrite(p, sizeof(char), n, ofp);
363 fputs("\n", ofp);
364 if (esl_memstrpfx(p, n, "//")) break;
365 }
366 if (status == eslEOF) esl_fatal("Reached end of file before finding // termination line for alignment");
367 else if (status != eslOK) esl_fatal("Failure in reading alignment line by line");
368 }
369
370