| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <esl_config.h> |
| 9 |
#include "config.h" |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
#include <time.h> |
| 15 |
|
| 16 |
#include "easel.h" |
| 17 |
#include <esl_getopts.h> |
| 18 |
#include <esl_histogram.h> |
| 19 |
#include <esl_random.h> |
| 20 |
#include <esl_randomseq.h> |
| 21 |
#include <esl_stats.h> |
| 22 |
#include <esl_vectorops.h> |
| 23 |
#include <esl_wuss.h> |
| 24 |
|
| 25 |
#include "hmmer.h" |
| 26 |
|
| 27 |
#include "infernal.h" |
| 28 |
|
| 29 |
#define ALPHOPTS "--rna,--dna" |
| 30 |
#define OUTOPTS "-u,-c,-a" |
| 31 |
|
| 32 |
static ESL_OPTIONS options[] = { |
| 33 |
|
| 34 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 }, |
| 35 |
{ "-o", eslARG_OUTFILE, FALSE, NULL, NULL, NULL, NULL, NULL, "send sequence output to file <f>, not stdout", 1 }, |
| 36 |
{ "-N", eslARG_INT, "10", NULL, "n>0", NULL, NULL, NULL, "generate <n> sequences", 1 }, |
| 37 |
{ "-u", eslARG_NONE, "default", NULL, NULL, OUTOPTS, NULL, NULL, "write generated sequences as unaligned FASTA", 1 }, |
| 38 |
{ "-a", eslARG_NONE, FALSE, NULL, NULL, OUTOPTS, NULL, NULL, "write generated sequences as an alignment", 1 }, |
| 39 |
{ "-c", eslARG_NONE, FALSE, NULL, NULL, OUTOPTS, NULL, NULL, "generate a single \"consensus\" sequence only", 1, }, |
| 40 |
{ "-e", eslARG_INT, NULL, NULL, "n>0", NULL, NULL, "-a,-c", "embed emitted sequences within larger random sequences of length <n>", 1 }, |
| 41 |
{ "-l", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "local; emit from a locally configured model [default: global]", 1 }, |
| 42 |
|
| 43 |
{ "--u5p", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "-a,-c", "truncate unaligned sequences 5', choosing a random start posn", 2 }, |
| 44 |
{ "--u3p", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "-a,-c", "truncate unaligned sequences 3', choosing a random end posn", 2 }, |
| 45 |
{ "--a5p", eslARG_INT, NULL, NULL, "n>=0", NULL, "--a3p,-a", NULL, "truncate aln 5', start at match column <n> (use 0 for random posn)", 2 }, |
| 46 |
{ "--a3p", eslARG_INT, NULL, NULL, "n>=0", NULL, "--a5p,-a", NULL, "truncate aln 3', end at match column <n> (use 0 for random posn)", 2 }, |
| 47 |
|
| 48 |
{ "--seed", eslARG_INT, "0", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> [default: one-time arbitrary seed]", 3 }, |
| 49 |
{ "--iid", eslARG_NONE, FALSE, NULL, NULL, NULL, "-e", NULL, "with -e, generate larger sequences as 25% ACGU (iid) ", 3 }, |
| 50 |
{ "--rna", eslARG_NONE, "default", NULL, NULL, ALPHOPTS, NULL, NULL, "output as RNA sequence data", 3 }, |
| 51 |
{ "--dna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "output as DNA sequence data", 3 }, |
| 52 |
{ "--idx", eslARG_INT, "1", NULL, "n>0", NULL, NULL, NULL, "start sequence numbering at <n>", 3 }, |
| 53 |
{ "--outformat", eslARG_STRING, "Stockholm", NULL, NULL, NULL, "-a", NULL, "w/-a output alignment in format <s>", 3 }, |
| 54 |
{ "--tfile", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, "-c,-e,--u5p,--u3p", "dump parsetrees to file <f>", 3 }, |
| 55 |
{ "--exp", eslARG_REAL, NULL, NULL, "x>0", NULL, NULL, NULL, "exponentiate CM probabilities by <x> before emitting", 3 }, |
| 56 |
{ "--hmmonly", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "emit from filter HMM, not from CM", 3 }, |
| 57 |
{ "--nohmmonly", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--hmmonly", "always emit from CM, even for models with 0 basepairs", 3 }, |
| 58 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
struct cfg_s { |
| 71 |
char *cmfile; |
| 72 |
CM_FILE *cmfp; |
| 73 |
ESL_ALPHABET *abc; |
| 74 |
ESL_ALPHABET *abc_out; |
| 75 |
FILE *ofp; |
| 76 |
FILE *tfp; |
| 77 |
ESL_RANDOMNESS *r; |
| 78 |
int ncm; |
| 79 |
int use_cm; |
| 80 |
}; |
| 81 |
|
| 82 |
static char usage[] = "[-options] <cmfile>"; |
| 83 |
static char banner[] = "sample sequences from a covariance model"; |
| 84 |
|
| 85 |
static int init_cfg(const ESL_GETOPTS *go, struct cfg_s *cfg, char *errbuf); |
| 86 |
|
| 87 |
static void master(const ESL_GETOPTS *go, struct cfg_s *cfg); |
| 88 |
|
| 89 |
static int initialize_cm(const ESL_GETOPTS *go, const struct cfg_s *cfg, CM_t *cm, char *errbuf); |
| 90 |
static int emit_unaligned(const ESL_GETOPTS *go, const struct cfg_s *cfg, CM_t *cm, char *errbuf); |
| 91 |
static int emit_alignment(const ESL_GETOPTS *go, const struct cfg_s *cfg, CM_t *cm, char *errbuf); |
| 92 |
static int emit_consensus(const ESL_GETOPTS *go, const struct cfg_s *cfg, CM_t *cm); |
| 93 |
static int truncate_msa(const ESL_GETOPTS *go, const struct cfg_s *cfg, ESL_MSA *msa, const ESL_ALPHABET *abc, char *errbuf); |
| 94 |
|
| 95 |
int |
| 96 |
main(int argc, char **argv) |
| 97 |
{ |
| 98 |
ESL_GETOPTS *go = NULL; |
| 99 |
struct cfg_s cfg; |
| 100 |
|
| 101 |
|
| 102 |
init_ilogsum(); |
| 103 |
FLogsumInit(); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
go = esl_getopts_Create(options); |
| 112 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK || |
| 113 |
esl_opt_VerifyConfig(go) != eslOK) { |
| 114 |
printf("Failed to parse command line: %s\n", go->errbuf); |
| 115 |
esl_usage(stdout, argv[0], usage); |
| 116 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 117 |
exit(1); |
| 118 |
} |
| 119 |
if (esl_opt_GetBoolean(go, "-h")) { |
| 120 |
cm_banner(stdout, argv[0], banner); |
| 121 |
esl_usage(stdout, argv[0], usage); |
| 122 |
puts("\nBasic options:"); |
| 123 |
esl_opt_DisplayHelp(stdout, go, 1, 2, 80); |
| 124 |
puts("\nOptions for truncating sequences:"); |
| 125 |
esl_opt_DisplayHelp(stdout, go, 2, 2, 80); |
| 126 |
puts("\nOther options:"); |
| 127 |
esl_opt_DisplayHelp(stdout, go, 3, 2, 80); |
| 128 |
puts("\nAlignment output formats (-a) include: Stockholm, Pfam, AFA (aligned FASTA), A2M, Clustal, PHYLIP\n"); |
| 129 |
exit(0); |
| 130 |
} |
| 131 |
if (esl_opt_ArgNumber(go) != 1) { |
| 132 |
puts("Incorrect number of command line arguments."); |
| 133 |
esl_usage(stdout, argv[0], usage); |
| 134 |
puts("\n where basic options are:"); |
| 135 |
esl_opt_DisplayHelp(stdout, go, 1, 2, 80); |
| 136 |
printf("\nTo see more help on other available options, do %s -h\n\n", argv[0]); |
| 137 |
exit(1); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
cfg.cmfile = esl_opt_GetArg(go, 1); |
| 143 |
cfg.cmfp = NULL; |
| 144 |
cfg.ofp = NULL; |
| 145 |
cfg.abc = NULL; |
| 146 |
cfg.abc_out = NULL; |
| 147 |
cfg.tfp = NULL; |
| 148 |
cfg.r = NULL; |
| 149 |
|
| 150 |
|
| 151 |
master(go, &cfg); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if(esl_opt_IsOn(go, "-o")) { |
| 156 |
fclose(cfg.ofp); |
| 157 |
} |
| 158 |
if (cfg.tfp != NULL) { |
| 159 |
fclose(cfg.tfp); |
| 160 |
} |
| 161 |
if (cfg.abc != NULL) { esl_alphabet_Destroy(cfg.abc); cfg.abc = NULL; } |
| 162 |
if (cfg.abc_out != NULL) esl_alphabet_Destroy(cfg.abc_out); |
| 163 |
if (cfg.cmfp != NULL) cm_file_Close(cfg.cmfp); |
| 164 |
if (cfg.r != NULL) esl_randomness_Destroy(cfg.r); |
| 165 |
|
| 166 |
esl_getopts_Destroy(go); |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
static int |
| 180 |
init_cfg(const ESL_GETOPTS *go, struct cfg_s *cfg, char *errbuf) |
| 181 |
{ |
| 182 |
int status; |
| 183 |
|
| 184 |
|
| 185 |
status = cm_file_Open(cfg->cmfile, NULL, FALSE, &(cfg->cmfp), errbuf); |
| 186 |
if (status == eslENOTFOUND) cm_Fail("File existence/permissions problem in trying to open CM file %s.\n%s\n", cfg->cmfile, errbuf); |
| 187 |
else if (status == eslEFORMAT) cm_Fail("File format problem in trying to open CM file %s.\n%s\n", cfg->cmfile, errbuf); |
| 188 |
else if (status != eslOK) cm_Fail("Unexpected error %d in opening CM file %s.\n%s\n", status, cfg->cmfile, errbuf); |
| 189 |
|
| 190 |
|
| 191 |
if ( esl_opt_IsOn(go, "-o") ) { |
| 192 |
if ((cfg->ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) ESL_FAIL(eslFAIL, errbuf, "Failed to open output file %s", esl_opt_GetString(go, "-o")); |
| 193 |
} else cfg->ofp = stdout; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if (esl_opt_GetBoolean(go, "--rna")) cfg->abc_out = esl_alphabet_Create(eslRNA); |
| 198 |
else if (esl_opt_GetBoolean(go, "--dna")) cfg->abc_out = esl_alphabet_Create(eslDNA); |
| 199 |
|
| 200 |
|
| 201 |
if (esl_opt_GetString(go, "--tfile") != NULL) { |
| 202 |
if ((cfg->tfp = fopen(esl_opt_GetString(go, "--tfile"), "w")) == NULL) |
| 203 |
ESL_FAIL(eslFAIL, errbuf, "Failed to open --tfile output file %s\n", esl_opt_GetString(go, "--tfile")); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
cfg->r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "--seed")); |
| 208 |
|
| 209 |
if (cfg->abc_out == NULL) ESL_FAIL(eslEINVAL, errbuf, "Output alphabet creation failed."); |
| 210 |
if (cfg->r == NULL) ESL_FAIL(eslEINVAL, errbuf, "Failed to create random number generator: probably out of memory"); |
| 211 |
|
| 212 |
return eslOK; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
static void |
| 222 |
master(const ESL_GETOPTS *go, struct cfg_s *cfg) |
| 223 |
{ |
| 224 |
int status; |
| 225 |
char errbuf[eslERRBUFSIZE]; |
| 226 |
CM_t *cm = NULL; |
| 227 |
|
| 228 |
if ((status = init_cfg(go, cfg, errbuf)) != eslOK) cm_Fail(errbuf); |
| 229 |
|
| 230 |
cfg->ncm = 0; |
| 231 |
|
| 232 |
while ((status = cm_file_Read(cfg->cmfp, TRUE, &(cfg->abc), &cm)) == eslOK) |
| 233 |
{ |
| 234 |
if (cm == NULL) cm_Fail("Failed to read CM from %s -- file corrupt?\n", cfg->cmfile); |
| 235 |
cfg->ncm++; |
| 236 |
|
| 237 |
|
| 238 |
if (esl_opt_GetBoolean(go, "--nohmmonly")) cfg->use_cm = TRUE; |
| 239 |
else if (esl_opt_GetBoolean(go, "--hmmonly")) cfg->use_cm = FALSE; |
| 240 |
else cfg->use_cm = (CMCountNodetype(cm, MATP_nd) > 0) ? TRUE : FALSE; |
| 241 |
|
| 242 |
if((status = initialize_cm(go, cfg, cm, errbuf)) != eslOK) cm_Fail(errbuf); |
| 243 |
|
| 244 |
|
| 245 |
if(esl_opt_GetBoolean(go, "-c")) { |
| 246 |
emit_consensus(go, cfg, cm); |
| 247 |
} |
| 248 |
else if(esl_opt_GetBoolean(go, "-a")) { |
| 249 |
if((status = emit_alignment(go, cfg, cm, errbuf)) != eslOK) cm_Fail(errbuf); |
| 250 |
} |
| 251 |
else { |
| 252 |
if((status = emit_unaligned(go, cfg, cm, errbuf)) != eslOK) cm_Fail(errbuf); |
| 253 |
} |
| 254 |
FreeCM(cm); |
| 255 |
} |
| 256 |
if(status != eslEOF) cm_Fail(cfg->cmfp->errbuf); |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static int |
| 266 |
initialize_cm(const ESL_GETOPTS *go, const struct cfg_s *cfg, CM_t *cm, char *errbuf) |
| 267 |
{ |
| 268 |
int status; |
| 269 |
|
| 270 |
|
| 271 |
if(esl_opt_GetBoolean(go, "-l")) { |
| 272 |
cm->config_opts |= CM_CONFIG_LOCAL; |
| 273 |
cm->config_opts |= CM_CONFIG_HMMLOCAL; |
| 274 |
cm->config_opts |= CM_CONFIG_HMMEL; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
if(esl_opt_IsOn(go, "--exp")) { |
| 279 |
if(cfg->use_cm) cm_Exponentiate (cm, esl_opt_GetReal(go, "--exp")); |
| 280 |
else cm_p7_Exponentiate(cm->fp7, esl_opt_GetReal(go, "--exp")); |
| 281 |
} |
| 282 |
if((status = cm_Configure(cm, errbuf, -1)) != eslOK) return status; |
| 283 |
|
| 284 |
return eslOK; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
static int |
| 291 |
emit_unaligned(const ESL_GETOPTS *go, const struct cfg_s *cfg, CM_t *cm, char *errbuf) |
| 292 |
{ |
| 293 |
int status; |
| 294 |
Parsetree_t *tr = NULL; |
| 295 |
ESL_SQ *esq = NULL; |
| 296 |
ESL_SQ *tsq = NULL; |
| 297 |
ESL_SQ *gsq = NULL; |
| 298 |
ESL_SQ *sq2print = NULL; |
| 299 |
char *name; |
| 300 |
int namelen; |
| 301 |
int i, L, embedL; |
| 302 |
int x; |
| 303 |
float sc, struct_sc; |
| 304 |
int offset = esl_opt_GetInteger(go, "--idx"); |
| 305 |
int start, end, swap; |
| 306 |
|
| 307 |
|
| 308 |
int ghmm_nstates = 0; |
| 309 |
double *ghmm_sA = NULL; |
| 310 |
double **ghmm_tAA = NULL; |
| 311 |
double **ghmm_eAA = NULL; |
| 312 |
double *fq = NULL; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
int hmm_mode = esl_opt_GetBoolean(go, "-l") ? p7_UNILOCAL : p7_UNIGLOCAL; |
| 318 |
P7_TRACE *p7tr = NULL; |
| 319 |
P7_PROFILE *gm = NULL; |
| 320 |
P7_BG *bg = NULL; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if(cm->abc->type != cfg->abc_out->type) { |
| 328 |
if(! (cm->abc->type == eslRNA && cfg->abc_out->type == eslDNA)) { |
| 329 |
ESL_FAIL(eslFAIL, errbuf, "CM alphabet type must match output alphabet type (unless CM=RNA and output=DNA)."); |
| 330 |
} |
| 331 |
} |
| 332 |
if(esl_opt_IsOn(go, "-e") && esl_opt_IsOn(go, "--u5p") && esl_opt_IsOn(go, "--u3p")) { |
| 333 |
ESL_FAIL(eslFAIL, errbuf, "-e works in combination with --u5p or --u3p but not both"); |
| 334 |
} |
| 335 |
|
| 336 |
namelen = IntMaxDigits() + strlen("sample") + 1; |
| 337 |
if(cm->name != NULL) namelen += strlen(cm->name) + 1; |
| 338 |
else namelen += IntMaxDigits() + 1; |
| 339 |
ESL_ALLOC(name, sizeof(char) * (namelen+1)); |
| 340 |
|
| 341 |
if(! cfg->use_cm) { |
| 342 |
if ((gm = p7_profile_Create(cm->fp7->M, cm->fp7->abc)) == NULL) cm_Fail("failed to create profile for filter HMM"); |
| 343 |
if ((bg = p7_bg_Create(cm->fp7->abc)) == NULL) cm_Fail("failed to create null model for filter HMM"); |
| 344 |
if (p7_ProfileConfig(cm->fp7, bg, gm, cm->fp7->max_length, hmm_mode) != eslOK) cm_Fail("failed to configure profile for filter HMM"); |
| 345 |
|
| 346 |
gm->xsc[p7P_N][p7P_MOVE] = gm->xsc[p7P_C][p7P_MOVE] = 0.0f; |
| 347 |
gm->xsc[p7P_N][p7P_LOOP] = gm->xsc[p7P_C][p7P_LOOP] = -eslINFINITY; |
| 348 |
} |
| 349 |
|
| 350 |
if(esl_opt_IsOn(go, "-e")) { |
| 351 |
embedL = esl_opt_GetInteger(go, "-e"); |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if(esl_opt_GetBoolean(go, "--iid")) { |
| 356 |
ESL_ALLOC(fq, sizeof(double) * cfg->abc_out->K); |
| 357 |
esl_vec_DSet(fq, cfg->abc_out->K, 1.0 / (double) cfg->abc_out->K); |
| 358 |
} |
| 359 |
else { |
| 360 |
if((status = CreateGenomicHMM(cfg->abc_out, errbuf, &ghmm_sA, &ghmm_tAA, &ghmm_eAA, &ghmm_nstates)) != eslOK) ESL_FAIL(status, errbuf, "unable to create generative HMM\n%s", errbuf); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
for(i = 0; i < esl_opt_GetInteger(go, "-N"); i++) |
| 365 |
{ |
| 366 |
if(cm->name != NULL) snprintf(name, namelen+1, "%s-sample%d", cm->name, i+offset); |
| 367 |
else snprintf(name, namelen+1, "%d-sample%d", cfg->ncm, i+offset); |
| 368 |
if(cfg->use_cm) { |
| 369 |
if((status = EmitParsetree(cm, errbuf, cfg->r, name, TRUE, &tr, &esq, &L)) != eslOK) return status; |
| 370 |
esq->abc = cfg->abc_out; |
| 371 |
} |
| 372 |
else { |
| 373 |
esq = esl_sq_CreateDigital(cfg->abc_out); |
| 374 |
if((p7tr = p7_trace_Create()) == NULL) cm_Fail("failed to allocate p7 trace"); |
| 375 |
|
| 376 |
if((status = p7_ProfileEmit(cfg->r, cm->fp7, gm, bg, esq, p7tr)) != eslOK) cm_Fail("failed to emit sequence from cm->fp7"); |
| 377 |
if((status = esl_sq_SetName(esq, name)) != eslOK) cm_Fail("failed to name emitted sequence from cm->fp7"); |
| 378 |
} |
| 379 |
sq2print = esq; |
| 380 |
|
| 381 |
|
| 382 |
if(esl_opt_IsOn(go, "--u5p") || esl_opt_IsOn(go, "--u3p")) { |
| 383 |
start = esl_opt_IsOn(go, "--u5p") ? esl_rnd_Roll(cfg->r, sq2print->n) + 1 : 1; |
| 384 |
end = esl_opt_IsOn(go, "--u3p") ? esl_rnd_Roll(cfg->r, sq2print->n) + 1 : sq2print->n; |
| 385 |
if(start > end) { swap = start; start = end; end = swap; } |
| 386 |
if((tsq = esl_sq_CreateDigitalFrom(sq2print->abc, sq2print->name, (sq2print->dsq+start-1), (end-start+1), |
| 387 |
sq2print->desc, sq2print->acc, sq2print->ss)) == NULL) ESL_FAIL(status, errbuf, "out of memory"); |
| 388 |
tsq->dsq[0] = tsq->dsq[tsq->n+1] = eslDSQ_SENTINEL; |
| 389 |
|
| 390 |
esl_sq_Destroy(sq2print); |
| 391 |
sq2print = tsq; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
if(esl_opt_IsOn(go, "-e")) { |
| 396 |
if(sq2print->n > embedL) ESL_FAIL(eslEINCOMPAT, errbuf, "<n>=%d from -eL <n> too small for emitted seq of length %" PRId64 ", increase <n> and rerun", embedL, sq2print->n); |
| 397 |
|
| 398 |
gsq = esl_sq_CreateDigital(cfg->abc_out); |
| 399 |
if((status = esl_sq_GrowTo(gsq, embedL)) != eslOK) ESL_FAIL(status, errbuf, "out of memory"); |
| 400 |
if(esl_opt_GetBoolean(go, "--iid")) { |
| 401 |
esl_rsq_xIID(cfg->r, fq, cfg->abc_out->K, embedL, gsq->dsq); |
| 402 |
} |
| 403 |
else { |
| 404 |
free(gsq->dsq); |
| 405 |
if((status = SampleGenomicSequenceFromHMM(cfg->r, cfg->abc_out, errbuf, ghmm_sA, ghmm_tAA, ghmm_eAA, ghmm_nstates, embedL, &(gsq->dsq))) != eslOK) { |
| 406 |
ESL_FAIL(status, errbuf, "failed to generate random background sequence to embed in"); |
| 407 |
} |
| 408 |
} |
| 409 |
gsq->n = embedL; |
| 410 |
|
| 411 |
|
| 412 |
if (esl_opt_IsOn(go, "--u5p")) { start = 1; } |
| 413 |
else if(esl_opt_IsOn(go, "--u3p")) { start = embedL - sq2print->n + 1; } |
| 414 |
else { start = esl_rnd_Roll(cfg->r, embedL - sq2print->n + 1) + 1; } |
| 415 |
for(x = start; x < start + sq2print->n; x++) gsq->dsq[x] = sq2print->dsq[x - start + 1]; |
| 416 |
|
| 417 |
esl_sq_FormatName(gsq, "%s/%" PRId64 "-%" PRId64, sq2print->name, (int64_t) start, (int64_t) (start + sq2print->n - 1)); |
| 418 |
|
| 419 |
esl_sq_Destroy(sq2print); |
| 420 |
sq2print = gsq; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
if((esl_sqio_Write(cfg->ofp, sq2print, eslSQFILE_FASTA, FALSE)) != eslOK) ESL_FAIL(eslFAIL, errbuf, "Error writing unaligned sequences."); |
| 425 |
|
| 426 |
|
| 427 |
if(cfg->tfp != NULL) { |
| 428 |
fprintf(cfg->tfp, "> %s\n", esq->name); |
| 429 |
if(cfg->use_cm) { |
| 430 |
if((status = ParsetreeScore(cm, NULL, errbuf, tr, sq2print->dsq, FALSE, &sc, &struct_sc, NULL, NULL, NULL)) != eslOK) return status; |
| 431 |
fprintf(cfg->tfp, " %16s %.2f bits\n", "SCORE:", sc); |
| 432 |
fprintf(cfg->tfp, " %16s %.2f bits\n", "STRUCTURE SCORE:", struct_sc); |
| 433 |
ParsetreeDump(cfg->tfp, tr, cm, sq2print->dsq); |
| 434 |
} |
| 435 |
else { |
| 436 |
if((status = p7_trace_Score(p7tr, sq2print->dsq, gm, &sc)) != eslOK) ESL_FAIL(status, errbuf, "unable to score p7 trace"); |
| 437 |
struct_sc = 0.; |
| 438 |
fprintf(cfg->tfp, " %16s %.2f bits\n", "SCORE:", sc); |
| 439 |
fprintf(cfg->tfp, " %16s %.2f bits\n", "STRUCTURE SCORE:", struct_sc); |
| 440 |
fprintf(cfg->tfp, "HMM trace dump\n"); |
| 441 |
fprintf(cfg->tfp, "------------------\n"); |
| 442 |
if((status = p7_trace_Dump(cfg->tfp, p7tr, gm, sq2print->dsq)) != eslOK) ESL_FAIL(status, errbuf, "unable to dump p7 trace"); |
| 443 |
} |
| 444 |
fprintf(cfg->tfp, "//\n"); |
| 445 |
} |
| 446 |
if(tr != NULL) { FreeParsetree(tr); tr = NULL; } |
| 447 |
if(p7tr != NULL) { p7_trace_Destroy(p7tr); p7tr = NULL; } |
| 448 |
esl_sq_Destroy(sq2print); |
| 449 |
|
| 450 |
} |
| 451 |
free(name); |
| 452 |
if(bg != NULL) p7_bg_Destroy(bg); |
| 453 |
if(gm != NULL) p7_profile_Destroy(gm); |
| 454 |
if(fq != NULL) free(fq); |
| 455 |
if(ghmm_eAA != NULL) { |
| 456 |
for(i = 0; i < ghmm_nstates; i++) free(ghmm_eAA[i]); |
| 457 |
free(ghmm_eAA); |
| 458 |
} |
| 459 |
if(ghmm_tAA != NULL) { |
| 460 |
for(i = 0; i < ghmm_nstates; i++) free(ghmm_tAA[i]); |
| 461 |
free(ghmm_tAA); |
| 462 |
} |
| 463 |
if(ghmm_sA != NULL) free(ghmm_sA); |
| 464 |
|
| 465 |
return eslOK; |
| 466 |
|
| 467 |
ERROR: |
| 468 |
return status; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
static int |
| 475 |
emit_alignment(const ESL_GETOPTS *go, const struct cfg_s *cfg, CM_t *cm, char *errbuf) |
| 476 |
{ |
| 477 |
int status; |
| 478 |
Parsetree_t **trA = NULL; |
| 479 |
ESL_SQ **sqA = NULL; |
| 480 |
P7_TRACE **p7trA = NULL; |
| 481 |
char *name; |
| 482 |
int namelen; |
| 483 |
int i, x, L; |
| 484 |
ESL_MSA *msa = NULL; |
| 485 |
int nseq = esl_opt_GetInteger(go, "-N"); |
| 486 |
int do_truncate; |
| 487 |
int offset = esl_opt_GetInteger(go, "--idx"); |
| 488 |
int outfmt; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
if(cm->abc->type != cfg->abc_out->type) { |
| 493 |
if(! (cm->abc->type == eslRNA && cfg->abc_out->type == eslDNA)){ |
| 494 |
ESL_FAIL(eslFAIL, errbuf, "CM alphabet type must match output alphabet type (unless CM=RNA and output=DNA)."); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
outfmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--outformat")); |
| 500 |
if (outfmt == eslMSAFILE_UNKNOWN) { |
| 501 |
ESL_FAIL(eslEINCOMPAT, errbuf, "%s is not a recognized output MSA file format\n\n", esl_opt_GetString(go, "--outformat")); |
| 502 |
} |
| 503 |
|
| 504 |
do_truncate = (esl_opt_IsOn(go, "--a5p") && esl_opt_IsOn(go, "--a3p")) ? TRUE : FALSE; |
| 505 |
|
| 506 |
namelen = IntMaxDigits() + strlen("sample") + 1; |
| 507 |
if(cm->name != NULL) namelen += strlen(cm->name) + 1; |
| 508 |
else namelen += IntMaxDigits() + 1; |
| 509 |
ESL_ALLOC(name, sizeof(char) * (namelen+1)); |
| 510 |
|
| 511 |
ESL_ALLOC(sqA, sizeof(ESL_SQ *) * nseq); |
| 512 |
if(cfg->use_cm) ESL_ALLOC(trA, sizeof(Parsetree_t *) * nseq); |
| 513 |
else ESL_ALLOC(p7trA, sizeof(P7_TRACE *) * nseq); |
| 514 |
if(cfg->use_cm) { |
| 515 |
for(i = 0; i < nseq; i++) |
| 516 |
{ |
| 517 |
if(cm->name != NULL) snprintf(name, namelen+1, "%s-sample%d", cm->name, i+offset); |
| 518 |
else snprintf(name, namelen+1, "%d-sample%d", cfg->ncm, i+offset); |
| 519 |
if((status = EmitParsetree(cm, errbuf, cfg->r, name, TRUE, &(trA[i]), &(sqA[i]), &L)) != eslOK) return status; |
| 520 |
sqA[i]->abc = cfg->abc_out; |
| 521 |
if(cfg->tfp != NULL) { |
| 522 |
fprintf(cfg->tfp, "> %s\n", sqA[i]->name); |
| 523 |
ParsetreeDump(cfg->tfp, trA[i], cm, sqA[i]->dsq); |
| 524 |
fprintf(cfg->tfp, "//\n"); |
| 525 |
} |
| 526 |
} |
| 527 |
if((status = Parsetrees2Alignment(cm, errbuf, cfg->abc_out, sqA, NULL, trA, NULL, nseq, |
| 528 |
NULL, NULL, |
| 529 |
TRUE, |
| 530 |
FALSE, |
| 531 |
FALSE, |
| 532 |
&msa) != eslOK)) |
| 533 |
ESL_XFAIL(eslFAIL, errbuf, "Error generating alignment from parsetrees."); |
| 534 |
} |
| 535 |
else { |
| 536 |
for (i = 0; i < nseq; i++) { |
| 537 |
if ((sqA[i] = esl_sq_CreateDigital(cm->fp7->abc)) == NULL) cm_Fail("failed to allocate seq"); |
| 538 |
if ((p7trA[i] = p7_trace_Create()) == NULL) cm_Fail("failed to allocate trace"); |
| 539 |
} |
| 540 |
for (i = 0; i < nseq; i++) { |
| 541 |
if (p7_CoreEmit(cfg->r, cm->fp7, sqA[i], p7trA[i]) != eslOK) cm_Fail("Failed to emit sequence"); |
| 542 |
if(cm->name != NULL) snprintf(name, namelen+1, "%s-sample%d", cm->name, i+offset); |
| 543 |
else snprintf(name, namelen+1, "%d-sample%d", cfg->ncm, i+offset); |
| 544 |
if (esl_sq_SetName(sqA[i], name) != eslOK) cm_Fail("Failed to set sequence name\n"); |
| 545 |
} |
| 546 |
p7_tracealign_Seqs(sqA, p7trA, nseq, cm->fp7->M, p7_ALL_CONSENSUS_COLS, cm->fp7, &msa); |
| 547 |
|
| 548 |
if(msa->ss_cons == NULL) { |
| 549 |
if(msa->rf == NULL) cm_Fail("HMM emitted MSA has no reference annotation"); |
| 550 |
ESL_ALLOC(msa->ss_cons, sizeof(char) * (msa->alen+1)); |
| 551 |
for(x = 0; x < msa->alen; x++) msa->ss_cons[x] = (msa->rf[x] == '.') ? '.' : ':'; |
| 552 |
msa->ss_cons[msa->alen] = '\0'; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
if(cm->name != NULL) if((status = esl_strdup(cm->name, -1, &(msa->name))) != eslOK) goto ERROR; |
| 557 |
if((status = esl_msa_FormatDesc(msa, "%s%s", "Synthetic sequence alignment generated by cmemit", |
| 558 |
(cfg->use_cm ? "" : " [hmm-mode]"))) != eslOK) goto ERROR; |
| 559 |
|
| 560 |
|
| 561 |
if(do_truncate) { |
| 562 |
if((status = truncate_msa(go, cfg, msa, cm->abc, errbuf)) != eslOK) cm_Fail(errbuf); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
status = esl_msafile_Write(cfg->ofp, msa, outfmt); |
| 567 |
if (status == eslEMEM) ESL_XFAIL(status, errbuf, "Memory error when outputting alignment\n"); |
| 568 |
else if (status != eslOK) ESL_XFAIL(status, errbuf, "Writing alignment file failed with error %d\n", status); |
| 569 |
|
| 570 |
if(sqA != NULL) { |
| 571 |
for(i = 0; i < nseq; i++) { |
| 572 |
if(sqA[i] != NULL) esl_sq_Destroy(sqA[i]); |
| 573 |
} |
| 574 |
free(sqA); |
| 575 |
} |
| 576 |
if(trA != NULL) { |
| 577 |
for(i = 0; i < nseq; i++) { |
| 578 |
if(trA[i] != NULL) FreeParsetree(trA[i]); |
| 579 |
} |
| 580 |
free(trA); |
| 581 |
} |
| 582 |
if(p7trA != NULL) { |
| 583 |
for (i = 0; i < nseq; i++) { |
| 584 |
if(p7trA[i] != NULL) p7_trace_Destroy(p7trA[i]); |
| 585 |
} |
| 586 |
free(p7trA); |
| 587 |
} |
| 588 |
|
| 589 |
free(name); |
| 590 |
esl_msa_Destroy(msa); |
| 591 |
return eslOK; |
| 592 |
|
| 593 |
ERROR: |
| 594 |
|
| 595 |
if(sqA != NULL) { |
| 596 |
for(i = 0; i < nseq; i++) |
| 597 |
if(sqA[i] != NULL) esl_sq_Destroy(sqA[i]); |
| 598 |
free(sqA); |
| 599 |
} |
| 600 |
if(trA != NULL) { |
| 601 |
for(i = 0; i < nseq; i++) |
| 602 |
if(trA[i] != NULL) FreeParsetree(trA[i]); |
| 603 |
free(trA); |
| 604 |
} |
| 605 |
if(name != NULL) free(name); |
| 606 |
if(msa != NULL) esl_msa_Destroy(msa); |
| 607 |
return status; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
static int |
| 614 |
emit_consensus(const ESL_GETOPTS *go, const struct cfg_s *cfg, CM_t *cm) |
| 615 |
{ |
| 616 |
int status; |
| 617 |
ESL_SQ *csq = NULL; |
| 618 |
char *cseqname = NULL; |
| 619 |
|
| 620 |
if(cfg->use_cm) { |
| 621 |
if((status = esl_strdup(cm->name, -1, &cseqname)) != eslOK) cm_Fail("failed to create sequence name"); |
| 622 |
if((status = esl_strcat(&cseqname, -1, "-cmconsensus", -1)) != eslOK) cm_Fail("failed to create sequence name"); |
| 623 |
if((csq = esl_sq_CreateFrom(cseqname, cm->cmcons->cseq, NULL, NULL, NULL)) == NULL) cm_Fail("failed to create consensus sequence"); |
| 624 |
} |
| 625 |
else { |
| 626 |
if ((csq = esl_sq_Create()) == NULL) cm_Fail("Failed to create sequence"); |
| 627 |
if (p7_emit_FancyConsensus(cm->fp7, 0.0, 0.5, csq) != eslOK) cm_Fail("failed to create HMM consensus seq"); |
| 628 |
if (esl_sq_FormatName(csq, "%s-hmmconsensus", cm->name) != eslOK) cm_Fail("failed to set sequence name"); |
| 629 |
} |
| 630 |
if((esl_sqio_Write(cfg->ofp, csq, eslSQFILE_FASTA, FALSE)) != eslOK) cm_Fail("error writing consensus sequence."); |
| 631 |
|
| 632 |
esl_sq_Destroy(csq); |
| 633 |
free(cseqname); |
| 634 |
|
| 635 |
return eslOK; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
static int |
| 644 |
truncate_msa(const ESL_GETOPTS *go, const struct cfg_s *cfg, ESL_MSA *msa, const ESL_ALPHABET *abc, char *errbuf) |
| 645 |
{ |
| 646 |
int status; |
| 647 |
int *useme = NULL; |
| 648 |
int *ct = NULL; |
| 649 |
int apos = 0; |
| 650 |
int cc = 0; |
| 651 |
int clen = 0; |
| 652 |
int swap; |
| 653 |
int spos, epos; |
| 654 |
int set_spos = (esl_opt_IsUsed(go, "--a5p")) ? TRUE : FALSE; |
| 655 |
int set_epos = (esl_opt_IsUsed(go, "--a3p")) ? TRUE : FALSE; |
| 656 |
int rnd_spos = (esl_opt_IsUsed(go, "--a5p") && (esl_opt_GetInteger(go, "--a5p") == 0)) ? TRUE : FALSE; |
| 657 |
int rnd_epos = (esl_opt_IsUsed(go, "--a3p") && (esl_opt_GetInteger(go, "--a3p") == 0)) ? TRUE : FALSE; |
| 658 |
|
| 659 |
ESL_ALLOC(useme, sizeof(int) * (msa->alen+1)); |
| 660 |
ESL_ALLOC(ct, sizeof(int) * (msa->alen+1)); |
| 661 |
|
| 662 |
|
| 663 |
for (apos = 0, cc = 0; apos < msa->alen; apos++) { |
| 664 |
if (!esl_abc_CIsGap(abc, msa->rf[apos])) clen++; |
| 665 |
} |
| 666 |
|
| 667 |
if(set_spos && esl_opt_GetInteger(go, "--a3p") > clen) { |
| 668 |
ESL_XFAIL(eslEINCOMPAT, errbuf, "with --a3p <n> option, <n> must be <= consensus length of CM (%d).\n", clen); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
if(set_spos && set_epos) { |
| 675 |
if(( rnd_spos && (! rnd_epos)) || |
| 676 |
((! rnd_spos) && rnd_epos)) { |
| 677 |
ESL_XFAIL(eslEINCOMPAT, errbuf, "with --a5p <n1> and --a3p <n2>, either <n1> and <n2> must be 0, or neither must be 0"); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
if(set_spos) { |
| 682 |
if(rnd_spos) spos = esl_rnd_Roll(cfg->r, clen) + 1; |
| 683 |
else spos = esl_opt_GetInteger(go, "--a5p"); |
| 684 |
} |
| 685 |
else spos = 1; |
| 686 |
|
| 687 |
|
| 688 |
if(set_epos) { |
| 689 |
if(rnd_epos) epos = esl_rnd_Roll(cfg->r, clen) + 1; |
| 690 |
else epos = esl_opt_GetInteger(go, "--a3p"); |
| 691 |
} |
| 692 |
else epos = clen; |
| 693 |
|
| 694 |
|
| 695 |
if(spos > epos) { |
| 696 |
if(rnd_spos && rnd_epos) { |
| 697 |
swap = spos; spos = epos; epos = swap; |
| 698 |
} |
| 699 |
else { |
| 700 |
assert(esl_opt_IsUsed(go, "--a5p") && esl_opt_IsUsed(go, "--a3p")); |
| 701 |
assert(esl_opt_GetInteger(go, "--a5p") > esl_opt_GetInteger(go, "--a3p")); |
| 702 |
ESL_XFAIL(eslEINCOMPAT, errbuf, "with --a5p <n1> and --a3p <n2>, <n1> must be <= <n2>"); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
if((status = esl_wuss_nopseudo(msa->ss_cons, msa->ss_cons)) != eslOK) goto ERROR; |
| 708 |
|
| 709 |
|
| 710 |
if((status = esl_wuss2ct(msa->ss_cons, msa->alen, ct)) != eslOK) goto ERROR; |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
for (apos = 0, cc = 0; apos < msa->alen; apos++) |
| 715 |
{ |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
if(cc < (spos-1) || cc > epos) { |
| 722 |
useme[apos] = 0; |
| 723 |
if(ct[(apos+1)] != 0) ct[ct[(apos+1)]] = 0; |
| 724 |
ct[(apos+1)] = 0; |
| 725 |
} |
| 726 |
else |
| 727 |
useme[apos] = 1; |
| 728 |
if (!esl_abc_CIsGap(abc, msa->rf[apos])) { |
| 729 |
cc++; |
| 730 |
if(cc == (epos+1)){ |
| 731 |
useme[apos] = 0; |
| 732 |
|
| 733 |
if(ct[(apos+1)] != 0) ct[ct[(apos+1)]] = 0; |
| 734 |
ct[(apos+1)] = 0; |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
if((status = esl_ct2wuss(ct, msa->alen, msa->ss_cons)) != eslOK) goto ERROR; |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
if((status = esl_msa_ColumnSubset(msa, errbuf, useme)) != eslOK) return status; |
| 749 |
free(useme); |
| 750 |
free(ct); |
| 751 |
return eslOK; |
| 752 |
|
| 753 |
ERROR: |
| 754 |
if(useme != NULL) free(useme); |
| 755 |
if(ct != NULL) free(ct); |
| 756 |
return status; |
| 757 |
} |
| 758 |
|
| 759 |
|