Debian Codegraph @

easel/miniapps/cmd_index.c

raw symbols SCIP
1 /* `easel index` - create SSI index for sequence file
2 *
3 */
4 #include <esl_config.h>
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "easel.h"
11 #include "esl_getopts.h"
12 #include "esl_regexp.h"
13 #include "esl_sq.h"
14 #include "esl_sqio.h"
15 #include "esl_ssi.h"
16 #include "esl_subcmd.h"
17
18
19 static ESL_OPTIONS cmd_options[] = {
20 /* name type default env range toggles reqs incomp help docgroup*/
21 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
22 { "-a", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "index accessions too, if present", 0 },
23 { "-u", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "parse UniProt db|acc|id names; index id too (and acc, w/ -a)", 0 },
24 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that input file is in format <s>", 0 },
25 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
26 };
27
28
29 /* esl_cmd_index(): implements `easel index`
30 *
31 * <topcmd> is argv[0] of the main program; `easel`, `/path/to/easel`.
32 *
33 * <sub> is the <ESL_SUBCMD> corresponding to this subcommand, passed
34 * from the `easel` program:
35 * sub->func = esl_cmd_index
36 * sub->subcmd = "index"
37 * sub->nargs = 1
38 * sub->usage = usage string defined in miniapps/easel.c header
39 * sub->description = help string defined in miniapps/easel.c header
40 *
41 * <argc> is the number of subcommand arguments, including "index" but
42 * not including "easel" or any top command options.
43 *
44 * <argv> is the list of subcommand arguments, starting with argv[0] =
45 * "index".
46 */
47 int
48 esl_cmd_index(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv)
49 {
50 ESL_GETOPTS *go = esl_subcmd_CreateDefaultApp(topcmd, sub, cmd_options, argc, argv);
51 char *seqfile = esl_opt_GetArg(go, 1);
52 char *ssifile = NULL;
53 int infmt = eslSQFILE_UNKNOWN;
54 int do_accessions = esl_opt_GetBoolean(go, "-a");
55 int do_uniprot = esl_opt_GetBoolean(go, "-u");
56 ESL_SQFILE *sqfp = NULL;
57 ESL_NEWSSI *ssifp = NULL;
58 ESL_SQ *sq = esl_sq_Create();
59 ESL_REGEXP *rem = esl_regexp_Create();
60 char *id = NULL;
61 char *acc = NULL;
62 int64_t nseq = 0;
63 uint16_t fh;
64 int status;
65
66 /* Open input sequence file */
67 if (esl_opt_GetString(go, "--informat") != NULL)
68 {
69 infmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--informat"));
70 if (infmt == eslSQFILE_UNKNOWN) esl_fatal("%s is not a valid input sequence file format for --informat", esl_opt_GetString(go, "--informat"));
71 }
72 status = esl_sqfile_Open(seqfile, infmt, NULL, &sqfp);
73 if (status == eslENOTFOUND) esl_fatal("Sequence file %s not found.\n", seqfile);
74 else if (status == eslEFORMAT) esl_fatal("Format of file %s unrecognized.\n", seqfile);
75 else if (status == eslEINVAL) esl_fatal("Can't autodetect stdin or .gz.\n");
76 else if (status != eslOK) esl_fatal("Open failed, code %d.\n", status);
77
78 /* Open output SSI index file */
79 esl_sprintf(&ssifile, "%s.ssi", sqfp->filename);
80 status = esl_newssi_Open(ssifile, /* allow_overwrite:*/ TRUE, &ssifp);
81 if (status == eslENOTFOUND) esl_fatal("failed to open SSI index %s", ssifile);
82 else if (status == eslEOVERWRITE) esl_fatal("SSI index %s already exists; delete or rename it", ssifile); /* won't happen, see TRUE above... */
83 else if (status != eslOK) esl_fatal("failed to create a new SSI index");
84
85 if (esl_newssi_AddFile(ssifp, sqfp->filename, sqfp->format, &fh) != eslOK)
86 esl_fatal("Failed to add sequence file %s to new SSI index\n", sqfp->filename);
87
88 printf("Creating SSI index %s for sequence file %s... ", ssifile, sqfp->filename);
89 fflush(stdout);
90
91 /* Precompile the regexp pattern for a little efficiency */
92 esl_regexp_Compile(rem, "^.+\\|(.+)\\|(.+)$");
93
94 /* Read each sequence, index names */
95 while ((status = esl_sqio_ReadInfo(sqfp, sq)) == eslOK)
96 {
97 nseq++;
98 if (sq->name == NULL)
99 esl_fatal("Every seq must have a name to be indexed. Failed to find name of seq #%" PRId64 "\n", nseq);
100
101 if (esl_newssi_AddKey(ssifp, sq->name, fh, sq->roff, sq->doff, sq->L) != eslOK)
102 esl_fatal("Failed to add name %s to SSI index primary keys", sq->name);
103
104 if (do_accessions && sq->acc[0] != '\0')
105 {
106 if (esl_newssi_AddAlias(ssifp, sq->acc, sq->name) != eslOK)
107 esl_fatal("Failed to add accession %s to SSI index secondary keys", sq->acc);
108 }
109
110 if (do_uniprot)
111 {
112 if (esl_regexp_Match(rem, NULL, sq->name) == eslOK) // NULL because pattern was precompiled, same every time
113 {
114 if (do_accessions)
115 {
116 acc = esl_regexp_SubmatchDup(rem, 1);
117 if (esl_newssi_AddAlias(ssifp, acc, sq->name) != eslOK)
118 esl_fatal("Failed to add parsed accession %s to SSI index secondary keys", acc);
119 free(acc);
120 }
121
122 id = esl_regexp_SubmatchDup(rem, 2);
123 if (esl_newssi_AddAlias(ssifp, id, sq->name) != eslOK)
124 esl_fatal("Failed to add parsed id %s to SSI index secondary keys", id);
125 }
126 }
127 esl_sq_Reuse(sq);
128 }
129
130 /* Determine if the file was suitable for fast subseq lookup. */
131 if (sqfp->data.ascii.bpl > 0 && sqfp->data.ascii.rpl > 0) {
132 if ((status = esl_newssi_SetSubseq(ssifp, fh, sqfp->data.ascii.bpl, sqfp->data.ascii.rpl)) != eslOK)
133 esl_fatal("Failed to set %s for fast subseq lookup.");
134 }
135
136 /* Save the SSI file to disk */
137 if (esl_newssi_Write(ssifp) != eslOK)
138 esl_fatal("\nFailed to write index to SSI file %s:\n %s", ssifile, ssifp->errbuf);
139
140 printf("done.\n");
141 if (ssifp->nsecondary > 0)
142 printf("Indexed %" PRId64 " sequences (%" PRIu64 " names and %" PRIu64 " secondary keys).\n", nseq, ssifp->nprimary, ssifp->nsecondary);
143 else
144 printf("Indexed %" PRId64 " sequences (%" PRIu64 " names).\n", nseq, ssifp->nprimary);
145 printf("SSI index written to file %s\n", ssifile);
146
147 free(ssifile);
148 esl_regexp_Destroy(rem);
149 esl_sq_Destroy(sq);
150 esl_newssi_Close(ssifp);
151 esl_sqfile_Close(sqfp);
152 esl_getopts_Destroy(go);
153 exit(0);
154 }
155
156