Debian Codegraph @

easel/miniapps/easel.c

raw symbols SCIP
1 /* easel - little utilities for biological sequence analysis
2 *
3 * A single program with many subcommands, subsuming the former Easel
4 * miniapps.
5 *
6 * For the implementations of individual miniapps:
7 * cmd_alistat.c alignment summary statistics
8 * cmd_downsample.c downsampling random subsets of things
9 * cmd_filter.c remove similar seqs from an MSA
10 * cmd_index.c create SSI index for sequence file
11 */
12 #include <esl_config.h>
13
14 #include <string.h>
15
16 #include "easel.h"
17 #include "esl_subcmd.h"
18
19
20 /* Each subcommand has an implementation in a separate `cmd_*.c`
21 * file, using an interface dictated by `esl_subcmd`.
22 */
23 extern int esl_cmd_alistat (const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv); // cmd_alistat.c
24 extern int esl_cmd_downsample(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv); // cmd_downsample.c
25 extern int esl_cmd_filter (const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv); // cmd_filter.c
26 extern int esl_cmd_index (const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv); // cmd_index.c
27
28
29
30 /* The ESL_SUBCMD array associates subcommand names with their
31 * implementations and command-line help strings.
32 */
33 ESL_SUBCMD subcommands[] = {
34 /* function subcmd_name nargs arg_description help_line */
35 { esl_cmd_alistat, "alistat", 1, "[-options] <msafile>", "summary statistics for a multiple seq alignment file" },
36 { esl_cmd_downsample, "downsample", 2, "[-options] <m> <infile>", "downsample <m> things from larger <infile> of n things" },
37 { esl_cmd_filter, "filter", 2, "[-options] <maxid> <msafile>", "remove seqs >= <maxid> fractional identity from MSA" },
38 { esl_cmd_index, "index", 1, "[-options] <infile>", "create SSI fast lookup index for sequence/alignment file" },
39 };
40
41
42
43 /* `easel` has its own options; each subcommand also has its own
44 * options (specified in `cmd_*.c` files)
45 */
46 static ESL_OPTIONS top_options[] = {
47 /* name type default env range tog's reqs incomp help docgroup*/
48 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show overall brief help summary", 1 },
49 { "--version", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show version number", 1 },
50 { "--help", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show overall brief help summary", 99 }, // accept --help as an undocumented special case
51 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
52 };
53
54
55 static int
56 top_usage(const char *topcmd)
57 {
58 char *lastslash = strrchr(topcmd, '/');
59 if (lastslash) topcmd = lastslash+1;
60
61 if (printf("Usage:\n") < 0) esl_fatal("printf failed");
62 if (printf(" %s -h : show overall brief help summary\n", topcmd) < 0) esl_fatal("printf failed");
63 if (printf(" %s --version : show version number\n", topcmd) < 0) esl_fatal("printf failed");
64 if (printf(" %s <cmd> -h : show brief help for an Easel command\n", topcmd) < 0) esl_fatal("printf failed");
65 if (printf(" %s <cmd> [<args>...] : run an Easel command\n", topcmd) < 0) esl_fatal("printf failed");
66 return eslOK;
67 }
68
69 static int
70 top_help(const char *topcmd)
71 {
72 int ncmds = sizeof(subcommands) / sizeof(ESL_SUBCMD);
73 int i;
74 int status;
75
76 if ( printf("easel: little utilities for biological sequence analysis\n") < 0) esl_fatal("printf failed");
77 if ( printf("version %s (%s): %s\n\n", EASEL_VERSION, EASEL_DATE, EASEL_URL) < 0) esl_fatal("printf failed");
78 if (( status = top_usage(topcmd)) != eslOK) return status;
79 if ( printf("\navailable commands:\n") < 0) esl_fatal("printf failed");
80 for (i = 0; i < ncmds; i++)
81 if ( printf(" %-12s %s\n", subcommands[i].subcmd, subcommands[i].description) < 0) esl_fatal("printf failed");
82 return eslOK;
83 }
84
85
86 int
87 main(int argc, char **argv)
88 {
89 ESL_GETOPTS *go = esl_getopts_Create(top_options);
90 int ncmds = sizeof(subcommands) / sizeof(ESL_SUBCMD);
91 int idx;
92 int status;
93
94 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) esl_fatal("Failed to parse command line: %s\n\n", go->errbuf);
95 if (esl_opt_VerifyConfig(go) != eslOK) esl_fatal("Failed to parse command line: %s\n\n", go->errbuf);
96
97 if (esl_opt_GetBoolean(go, "--version") == TRUE) { printf("%s\n", EASEL_VERSION); status = eslOK; goto DONE; }
98 if (esl_opt_GetBoolean(go, "--help") == TRUE) { status = top_help(argv[0]); goto DONE; }
99 if (esl_opt_GetBoolean(go, "-h") == TRUE) { status = top_help(argv[0]); goto DONE; }
100 if (argc - go->optind == 0) { status = top_help(argv[0]); goto DONE; }
101
102 for (idx = 0; idx < ncmds; idx++)
103 if (strcmp(go->argv[go->optind], subcommands[idx].subcmd) == 0) break;
104 if (idx == ncmds) { status = top_usage(argv[0]); goto DONE; }
105
106 status = subcommands[idx].func(argv[0], &subcommands[idx], argc-go->optind, argv+go->optind);
107
108 DONE:
109 esl_getopts_Destroy(go);
110 return status;
111 }
112