| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <esl_config.h> |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include "easel.h" |
| 17 |
#include "esl_dirichlet.h" |
| 18 |
#include "esl_getopts.h" |
| 19 |
#include "esl_matrixops.h" |
| 20 |
#include "esl_mixdchlet.h" |
| 21 |
#include "esl_random.h" |
| 22 |
#include "esl_subcmd.h" |
| 23 |
#include "esl_vectorops.h" |
| 24 |
|
| 25 |
static int cmd_score (const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv); |
| 26 |
static int cmd_sample(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv); |
| 27 |
static int cmd_fit (const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv); |
| 28 |
static int cmd_gen (const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv); |
| 29 |
|
| 30 |
static ESL_OPTIONS top_options[] = { |
| 31 |
|
| 32 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show overall brief help summary", 1 }, |
| 33 |
{ "--version", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show version number", 1 }, |
| 34 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 35 |
}; |
| 36 |
|
| 37 |
static ESL_SUBCMD subcommands[] = { |
| 38 |
{ cmd_fit, "fit", 4, "[-options] <Q> <K> <in_countfile> <out_mixdchlet>", "fit new mixture Dirichlet to count data" }, |
| 39 |
{ cmd_score, "score", 2, "[-options] <mixdchlet_file> <counts_file>", "score count data with a mixture Dirichlet" }, |
| 40 |
{ cmd_gen, "gen", 1, "[-options] <mixdchlet_file>", "generate synthetic count data from a mixture Dirichlet" }, |
| 41 |
{ cmd_sample, "sample", 0, "[-options]", "sample a random mixture Dirichlet" }, |
| 42 |
}; |
| 43 |
|
| 44 |
static int |
| 45 |
top_usage(const char *topcmd) |
| 46 |
{ |
| 47 |
char *lastslash = strrchr(topcmd, '/'); |
| 48 |
if (lastslash) topcmd = lastslash+1; |
| 49 |
|
| 50 |
if (printf("Usage:\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 51 |
if (printf(" %s -h : show overall brief help summary\n", topcmd) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 52 |
if (printf(" %s --version : show version number\n", topcmd) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 53 |
if (printf(" %s <cmd> -h : show brief help for a subcommand\n", topcmd) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 54 |
if (printf(" %s <cmd> [<args>...] : run a subcommand\n", topcmd) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 55 |
return eslOK; |
| 56 |
} |
| 57 |
|
| 58 |
static int |
| 59 |
top_help(const char *topcmd, const char *description) |
| 60 |
{ |
| 61 |
int ncmds = sizeof(subcommands) / sizeof(ESL_SUBCMD); |
| 62 |
char *lastslash = strrchr(topcmd, '/'); |
| 63 |
int i; |
| 64 |
int status; |
| 65 |
if (lastslash) topcmd = lastslash+1; |
| 66 |
|
| 67 |
if ( printf("%s: %s\n", topcmd, description) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 68 |
if ( printf("Easel %s (%s): %s\n\n", EASEL_VERSION, EASEL_DATE, EASEL_URL) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 69 |
if (( status = top_usage(topcmd)) != eslOK) return status; |
| 70 |
if ( printf("\nSubcommands:\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 71 |
for (i = 0; i < ncmds; i++) |
| 72 |
if ( printf(" %-12s %s\n", subcommands[i].subcmd, subcommands[i].description) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "printf failed"); |
| 73 |
|
| 74 |
return eslOK; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
int |
| 79 |
main(int argc, char **argv) |
| 80 |
{ |
| 81 |
char banner[] = "utilities for estimating new mixture Dirichlet priors"; |
| 82 |
ESL_GETOPTS *go = esl_getopts_Create(top_options); |
| 83 |
int ncmds = sizeof(subcommands) / sizeof(ESL_SUBCMD); |
| 84 |
int idx; |
| 85 |
int status; |
| 86 |
|
| 87 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) status = esl_printf("Failed to parse command line: %s\n\n", go->errbuf); |
| 88 |
else if (esl_opt_VerifyConfig(go) != eslOK) status = esl_printf("Failed to parse command line: %s\n\n", go->errbuf); |
| 89 |
else if (esl_opt_GetBoolean(go, "--version") == TRUE) status = esl_printf("%s\n", EASEL_VERSION); |
| 90 |
else if (esl_opt_GetBoolean(go, "-h") == TRUE) status = top_help(argv[0], banner); |
| 91 |
else if (argc - go->optind == 0) status = top_help(argv[0], banner); |
| 92 |
else |
| 93 |
{ |
| 94 |
for (idx = 0; idx < ncmds; idx++) |
| 95 |
if (strcmp(go->argv[go->optind], subcommands[idx].subcmd) == 0) break; |
| 96 |
|
| 97 |
if (idx == ncmds) status = top_usage(argv[0]); |
| 98 |
else status = subcommands[idx].func(argv[0], &subcommands[idx], argc-go->optind, argv+go->optind); |
| 99 |
} |
| 100 |
|
| 101 |
esl_getopts_Destroy(go); |
| 102 |
return status; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static ESL_OPTIONS fit_options[] = { |
| 111 |
|
| 112 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 113 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 114 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 115 |
}; |
| 116 |
|
| 117 |
static int |
| 118 |
cmd_fit(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv) |
| 119 |
{ |
| 120 |
ESL_GETOPTS *go = esl_subcmd_CreateDefaultApp(topcmd, sub, fit_options, argc, argv); |
| 121 |
ESL_RANDOMNESS *rng = esl_randomness_Create( esl_opt_GetInteger(go, "-s")); |
| 122 |
int Q = strtol(esl_opt_GetArg(go, 1), NULL, 10); |
| 123 |
int K = strtol(esl_opt_GetArg(go, 2), NULL, 10); |
| 124 |
char *ctfile = esl_opt_GetArg(go, 3); |
| 125 |
char *outfile = esl_opt_GetArg(go, 4); |
| 126 |
ESL_FILEPARSER *efp = NULL; |
| 127 |
FILE *ofp = NULL; |
| 128 |
ESL_MIXDCHLET *dchl = esl_mixdchlet_Create(Q,K); |
| 129 |
int Nalloc = 1024; |
| 130 |
double **ct = esl_mat_DCreate(Nalloc, K); |
| 131 |
int N = 0; |
| 132 |
char *tok = NULL; |
| 133 |
int toklen = 0; |
| 134 |
int a; |
| 135 |
double nll; |
| 136 |
int status; |
| 137 |
|
| 138 |
if ( esl_fileparser_Open(ctfile, NULL, &efp) != eslOK) esl_fatal("failed to open %s for reading", ctfile); |
| 139 |
if (( ofp = fopen(outfile, "w")) == NULL) esl_fatal("failed to open %s for writing", outfile); |
| 140 |
|
| 141 |
|
| 142 |
esl_fileparser_SetCommentChar(efp, '#'); |
| 143 |
while ((status = esl_fileparser_NextLine(efp)) == eslOK) |
| 144 |
{ |
| 145 |
if (N == Nalloc-1) { Nalloc *= 2; esl_mat_DGrowTo(&ct, Nalloc, K); } |
| 146 |
|
| 147 |
a = 0; |
| 148 |
while ((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) == eslOK) |
| 149 |
{ |
| 150 |
if (a == K) esl_fatal("parse failed, %s:%d: > K=%d fields on line", ctfile, efp->linenumber, K); |
| 151 |
if (! esl_str_IsReal(tok)) esl_fatal("parse failed, %s:%d: field %d (%s) not a real number", ctfile, efp->linenumber, a+1, tok); |
| 152 |
ct[N][a++] = atof(tok); |
| 153 |
} |
| 154 |
N++; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
esl_mixdchlet_Sample(rng, dchl); |
| 159 |
|
| 160 |
|
| 161 |
esl_mixdchlet_Fit(ct, N, dchl, &nll); |
| 162 |
|
| 163 |
|
| 164 |
esl_mixdchlet_Write(ofp, dchl); |
| 165 |
|
| 166 |
printf("nll = %g\n", nll); |
| 167 |
|
| 168 |
fclose(ofp); |
| 169 |
esl_fileparser_Close(efp); |
| 170 |
esl_mat_DDestroy(ct); |
| 171 |
esl_mixdchlet_Destroy(dchl); |
| 172 |
esl_randomness_Destroy(rng); |
| 173 |
esl_getopts_Destroy(go); |
| 174 |
return eslOK; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
static ESL_OPTIONS score_options[] = { |
| 183 |
|
| 184 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 185 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 186 |
}; |
| 187 |
|
| 188 |
static int |
| 189 |
cmd_score(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv) |
| 190 |
{ |
| 191 |
ESL_GETOPTS *go = esl_subcmd_CreateDefaultApp(topcmd, sub, score_options, argc, argv); |
| 192 |
char *dfile = esl_opt_GetArg(go, 1); |
| 193 |
char *ctfile = esl_opt_GetArg(go, 2); |
| 194 |
ESL_FILEPARSER *efp = NULL; |
| 195 |
ESL_MIXDCHLET *dchl = NULL; |
| 196 |
double *ct = NULL; |
| 197 |
char *tok = NULL; |
| 198 |
int toklen = 0; |
| 199 |
int a; |
| 200 |
double nll = 0; |
| 201 |
int status; |
| 202 |
|
| 203 |
|
| 204 |
if ( esl_fileparser_Open(dfile, NULL, &efp) != eslOK) esl_fatal("failed to open %s for reading", dfile); |
| 205 |
esl_fileparser_SetCommentChar(efp, '#'); |
| 206 |
if ( esl_mixdchlet_Read(efp, &dchl) != eslOK) esl_fatal("failed to parse %s\n %s", dfile, efp->errbuf); |
| 207 |
esl_fileparser_Close(efp); |
| 208 |
efp = NULL; |
| 209 |
|
| 210 |
|
| 211 |
if ( esl_fileparser_Open(ctfile, NULL, &efp) != eslOK) esl_fatal("failed to open %s for reading", ctfile); |
| 212 |
esl_fileparser_SetCommentChar(efp, '#'); |
| 213 |
ct = malloc(sizeof(double) * dchl->K); |
| 214 |
while ((status = esl_fileparser_NextLine(efp)) == eslOK) |
| 215 |
{ |
| 216 |
a = 0; |
| 217 |
while ((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) == eslOK) |
| 218 |
{ |
| 219 |
if (a == dchl->K) esl_fatal("parse failed, %s:%d: > K=%d fields on line", ctfile, efp->linenumber, dchl->K); |
| 220 |
if (! esl_str_IsReal(tok)) esl_fatal("parse failed, %s:%d: field %d (%s) not a real number", ctfile, efp->linenumber, a+1, tok); |
| 221 |
ct[a++] = atof(tok); |
| 222 |
} |
| 223 |
|
| 224 |
nll += esl_mixdchlet_logp_c(dchl, ct); |
| 225 |
} |
| 226 |
esl_fileparser_Close(efp); |
| 227 |
|
| 228 |
printf("nll = %g\n", -nll); |
| 229 |
|
| 230 |
free(ct); |
| 231 |
esl_mixdchlet_Destroy(dchl); |
| 232 |
esl_getopts_Destroy(go); |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
static ESL_OPTIONS gen_options[] = { |
| 243 |
|
| 244 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 245 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed", 0 }, |
| 246 |
{ "-M", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of counts per vector", 0 }, |
| 247 |
{ "-N", eslARG_INT, "1000", NULL, NULL, NULL, NULL, NULL, "number of countvectors to generate", 0 }, |
| 248 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 249 |
}; |
| 250 |
|
| 251 |
static int |
| 252 |
cmd_gen(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv) |
| 253 |
{ |
| 254 |
ESL_GETOPTS *go = esl_subcmd_CreateDefaultApp(topcmd, sub, gen_options, argc, argv); |
| 255 |
char *dfile = esl_opt_GetArg(go, 1); |
| 256 |
int N = esl_opt_GetInteger(go, "-N"); |
| 257 |
int M = esl_opt_GetInteger(go, "-M"); |
| 258 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 259 |
ESL_MIXDCHLET *dchl = NULL; |
| 260 |
ESL_FILEPARSER *efp = NULL; |
| 261 |
double *prob = NULL; |
| 262 |
int *ct = NULL; |
| 263 |
int a,i,j,k; |
| 264 |
|
| 265 |
if ( esl_fileparser_Open(dfile, NULL, &efp) != eslOK) esl_fatal("failed to open %s for reading", dfile); |
| 266 |
esl_fileparser_SetCommentChar(efp, '#'); |
| 267 |
if ( esl_mixdchlet_Read(efp, &dchl) != eslOK) esl_fatal("failed to parse %s\n %s", dfile, efp->errbuf); |
| 268 |
esl_fileparser_Close(efp); |
| 269 |
|
| 270 |
prob = malloc(sizeof(double) * dchl->K); |
| 271 |
ct = malloc(sizeof(int) * dchl->K); |
| 272 |
|
| 273 |
for (i = 0; i < N; i++) |
| 274 |
{ |
| 275 |
k = esl_rnd_DChoose(rng, dchl->q, dchl->Q); |
| 276 |
esl_dirichlet_DSample(rng, dchl->alpha[k], dchl->K, prob); |
| 277 |
esl_vec_ISet(ct, dchl->K, 0); |
| 278 |
for (j = 0; j < M; j++) |
| 279 |
{ |
| 280 |
a = esl_rnd_DChoose(rng, prob, dchl->K); |
| 281 |
ct[a] ++; |
| 282 |
} |
| 283 |
|
| 284 |
for (a = 0; a < dchl->K; a++) |
| 285 |
printf("%6d ", ct[a]); |
| 286 |
printf("\n"); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
free(prob); |
| 291 |
free(ct); |
| 292 |
esl_mixdchlet_Destroy(dchl); |
| 293 |
esl_randomness_Destroy(rng); |
| 294 |
esl_getopts_Destroy(go); |
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
static ESL_OPTIONS sample_options[] = { |
| 304 |
|
| 305 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 306 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed", 0 }, |
| 307 |
{ "-K", eslARG_INT, "20", NULL, NULL, NULL, NULL, NULL, "alphabet size", 0 }, |
| 308 |
{ "-Q", eslARG_INT, "9", NULL, NULL, NULL, NULL, NULL, "number of mixture components", 0 }, |
| 309 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 310 |
}; |
| 311 |
|
| 312 |
static int |
| 313 |
cmd_sample(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv) |
| 314 |
{ |
| 315 |
ESL_GETOPTS *go = esl_subcmd_CreateDefaultApp(topcmd, sub, sample_options, argc, argv); |
| 316 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 317 |
int Q = esl_opt_GetInteger(go, "-Q"); |
| 318 |
int K = esl_opt_GetInteger(go, "-K"); |
| 319 |
ESL_MIXDCHLET *dchl = esl_mixdchlet_Create(Q, K); |
| 320 |
|
| 321 |
esl_mixdchlet_Sample(rng, dchl); |
| 322 |
esl_mixdchlet_Write(stdout, dchl); |
| 323 |
|
| 324 |
esl_mixdchlet_Destroy(dchl); |
| 325 |
esl_randomness_Destroy(rng); |
| 326 |
esl_getopts_Destroy(go); |
| 327 |
return 0; |
| 328 |
} |