Debian Codegraph @

hmmer/easel/miniapps/esl-selectn.c

raw symbols SCIP
1 /* Select <m> random lines from a file and print them.
2 *
3 * Uses a reservoir sample - O(m) in space, single pass on the input,
4 * never reads the entire input into memory.
5 *
6 */
7 #include <esl_config.h>
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "easel.h"
14 #include "esl_getopts.h"
15 #include "esl_random.h"
16
17 static ESL_OPTIONS options[] = {
18 /* name type default env range toggles reqs incomp help docgroup*/
19 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
20 { "--seed", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number generator's seed to <n>", 1 },
21 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
22 };
23 static char usage[] = "[-options] <n> <file>";
24 static char banner[] = "select n lines randomly from a file";
25
26 static void
27 cmdline_failure(char *argv0, char *format, ...)
28 {
29 va_list argp;
30
31 va_start(argp, format);
32 vfprintf(stderr, format, argp);
33 va_end(argp);
34 esl_usage(stdout, argv0, usage);
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, usage);
44 puts("\n where general options are:");
45 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
46 exit(0);
47 }
48
49 int
50 main(int argc, char **argv)
51 {
52 ESL_GETOPTS *go = NULL;
53 ESL_RANDOMNESS *rng = NULL;
54 char *filename = NULL;
55 FILE *fp = NULL;
56 int m = 0; /* number of lines to sample */
57 char **larr = NULL; /* sampled line ptr array, [0..m-1] */
58 char *buf = NULL;
59 int buflen = 0;
60 int n; /* number of lines read so far */
61 int r; /* random #, 0..n-1 */
62 int i;
63
64 /* Parse command line */
65 go = esl_getopts_Create(options);
66 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) cmdline_failure(argv[0], "Failed to parse command line: %s\n", go->errbuf);
67 if (esl_opt_VerifyConfig(go) != eslOK) cmdline_failure(argv[0], "Error in app configuration: %s\n", go->errbuf);
68 if (esl_opt_GetBoolean(go, "-h") ) cmdline_help(argv[0], go);
69 if (esl_opt_ArgNumber(go) != 2) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
70
71 m = atoi(esl_opt_GetArg(go, 1));
72 filename = esl_opt_GetArg(go, 2);
73 rng = esl_randomness_Create(esl_opt_GetInteger(go, "--seed"));
74
75 if ((larr = malloc(sizeof(char *) * m)) == NULL) esl_fatal("allocation failed");
76 for (i = 0; i < m; i++) larr[i] = NULL;
77
78 if (strcmp(filename, "-") == 0) fp = stdin;
79 else {
80 if ((fp = fopen(filename, "r")) == NULL) esl_fatal("Failed to open file %s\n", filename);
81 }
82
83 n = 0;
84 while (esl_fgets(&buf, &buflen, fp) == eslOK)
85 {
86 n++;
87 if (n <= m) {
88 larr[n-1] = buf;
89 buf = NULL;
90 buflen = 0;
91 } else {
92 r = esl_rnd_Roll(rng, n);
93 if (r < m) {
94 free(larr[r]);
95 larr[r] = buf;
96 buf = NULL;
97 buflen = 0;
98 }
99 }
100 }
101
102 if (n < m)
103 esl_fatal("Input only has %d lines; not enough to select a subset of %d of them.\n", n, m);
104
105 for (i = 0; i < m; i++) printf("%s", larr[i]);
106
107 if (fp != stdin) fclose(fp);
108 for (i = 0; i < m; i++) free(larr[i]);
109 free(larr);
110 if (buf) free(buf);
111 esl_randomness_Destroy(rng);
112 esl_getopts_Destroy(go);
113 return 0;
114 }
115