Debian Codegraph @

hmmer/easel/miniapps/esl-shuffle.c

raw symbols SCIP
1 /* Shuffling or generating random sequences.
2 *
3 * SRE, Wed Jan 16 15:30:05 2008 [UA5230 to New York]
4 * from squid's shuffle (1995)
5 */
6 #include <esl_config.h>
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 #include "easel.h"
13 #include "esl_composition.h"
14 #include "esl_getopts.h"
15 #include "esl_msa.h"
16 #include "esl_msashuffle.h"
17 #include "esl_random.h"
18 #include "esl_randomseq.h"
19 #include "esl_sq.h"
20 #include "esl_sqio.h"
21 #include "esl_vectorops.h"
22
23 static char banner[] = "shuffling or generating random sequences";
24 static char usage1[] = " [options] <seqfile> (shuffles individual sequences)";
25 static char usage2[] = "-A [options] <msafile> (shuffles msa columnwise)";
26 static char usage3[] = "-G [options] (generates random sequences)";
27
28
29 #define MODE_OPTS "-S,-A,-G" /* toggle group, modes (seqfile, msafile, none) */
30 #define SHUF_OPTS "-m,-d,-k,-0,-1,-r,-w" /* toggle group, seq shuffling options */
31 #define ALPH_OPTS "--rna,--dna,--amino" /* toggle group, alphabet type options */
32
33 static ESL_OPTIONS options[] = {
34 /* name type default env range togs reqs incomp help docgroup */
35 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; show brief info on version and usage", 1 },
36 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "direct output data to file <f>", 1 },
37 { "-N", eslARG_INT, "1", NULL,"n>0", NULL, NULL, NULL, "generate <n> samples (per input seq/msa)", 1 },
38 { "-L", eslARG_INT, "0", NULL,"n>=0", NULL, NULL, NULL, "truncate outputs to length <n>", 1 },
39
40 /* Options for shuffling/generating based on input sequences */
41 { "-m", eslARG_NONE,"default", NULL, NULL, SHUF_OPTS, "-S", NULL, "shuffle preserving monoresidue composition", 2 },
42 { "-d", eslARG_NONE, FALSE, NULL, NULL, SHUF_OPTS, "-S", NULL, "shuffle preserving mono- and di-residue composition", 2 },
43 { "-k", eslARG_INT, FALSE, NULL,"n>0", SHUF_OPTS, "-S", NULL, "shuffle nonoverlapping <n>-mers", 2 },
44 { "-0", eslARG_NONE, FALSE, NULL, NULL, SHUF_OPTS, "-S", NULL, "generate with 0th order Markov properties per input", 2 },
45 { "-1", eslARG_NONE, FALSE, NULL, NULL, SHUF_OPTS, "-S", NULL, "generate with 1st order Markov properties per input", 2 },
46 { "-r", eslARG_NONE, FALSE, NULL, NULL, SHUF_OPTS, "-S", NULL, "reverse each input", 2 },
47 { "-w", eslARG_INT, FALSE, NULL,"n>0", SHUF_OPTS, "-S", NULL, "regionally shuffle inputs in window size <n>", 2 },
48
49 /* Options for shuffling multiple alignments (default w/ -A is to shuffle columns) */
50 { "-b", eslARG_NONE, FALSE, NULL, NULL, NULL, "-A", NULL, "take bootstrapping samples", 3 },
51 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, "-A", NULL, "shuffle residues in each column independently", 3 },
52
53 /* Options for generating sequences de novo */
54 { "--rna", eslARG_NONE,"default", NULL, NULL, ALPH_OPTS, "-G", NULL, "generate RNA sequence", 4 },
55 { "--dna", eslARG_NONE, FALSE, NULL, NULL, ALPH_OPTS, "-G", NULL, "generate DNA sequence", 4 },
56 { "--amino", eslARG_NONE, FALSE, NULL, NULL, ALPH_OPTS, "-G", NULL, "generate protein sequence", 4 },
57
58 /* Other "expert" options */
59 { "--seed", eslARG_INT, "0", NULL,"n>=0", NULL, NULL, NULL, "set random number generator's seed to <n>", 5 },
60 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that input file is in format <s>", 5 },
61
62 /* "undocumented" options (these are documented w/ command line usage, and implemented as options) */
63 { "-S", eslARG_NONE,"default", NULL, NULL, MODE_OPTS, NULL, NULL, "shuffle individual input sequences", 99 },
64 { "-A", eslARG_NONE, FALSE, NULL, NULL, MODE_OPTS, NULL, NULL, "input is an <msafile> to be shuffled by columns", 99 },
65 { "-G", eslARG_NONE, FALSE, NULL, NULL, MODE_OPTS, "-L", NULL, "generate de novo random sequences", 99 },
66
67 { 0,0,0,0,0,0,0,0,0,0 },
68 };
69
70
71 static void
72 cmdline_failure(char *argv0, char *format, ...)
73 {
74 va_list argp;
75
76 va_start(argp, format);
77 vfprintf(stderr, format, argp);
78 va_end(argp);
79 esl_usage(stdout, argv0, usage1);
80 esl_usage(stdout, argv0, usage2);
81 esl_usage(stdout, argv0, usage3);
82 printf("\nTo see more help on available options, do %s -h\n\n", argv0);
83 exit(1);
84 }
85
86 static void
87 cmdline_help(char *argv0, ESL_GETOPTS *go)
88 {
89 esl_banner(stdout, argv0, banner);
90 esl_usage (stdout, argv0, usage1);
91 esl_usage (stdout, argv0, usage2);
92 esl_usage (stdout, argv0, usage3);
93 puts("\n where general options are:");
94 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
95 puts("\n options for shuffling input sequences (default mode):");
96 esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
97 puts("\n options for shuffling alignments (-A mode):");
98 esl_opt_DisplayHelp(stdout, go, 3, 2, 80);
99 puts("\n options for generating sequences de novo (w/ -G option):");
100 esl_opt_DisplayHelp(stdout, go, 4, 2, 80);
101 puts("\n other infrequently used options:");
102 esl_opt_DisplayHelp(stdout, go, 5, 2, 80);
103 exit(0);
104 }
105
106
107 /* msa_shuffling()
108 *
109 * Shuffling multiple sequence alignments
110 */
111 static int
112 msa_shuffling(ESL_GETOPTS *go, ESL_RANDOMNESS *r, FILE *ofp, int outfmt)
113 {
114 char *msafile = esl_opt_GetArg(go, 1);
115 int infmt = eslMSAFILE_UNKNOWN;
116 ESL_ALPHABET *abc = NULL;
117 ESL_MSAFILE *afp = NULL;
118 ESL_MSA *msa = NULL;
119 ESL_MSA *shuf = NULL;
120 int N = esl_opt_GetInteger(go, "-N");
121 int i;
122 int status;
123
124 if (esl_opt_IsOn(go, "--informat") &&
125 (infmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--informat"))) == eslMSAFILE_UNKNOWN)
126 esl_fatal("%s is not a valid MSA file format for --informat", esl_opt_GetString(go, "--informat"));
127
128 status = esl_msafile_Open(&abc, msafile, NULL, infmt, NULL, &afp);
129 if (status != eslOK) esl_msafile_OpenFailure(afp, status);
130
131 while ((status = esl_msafile_Read(afp, &msa)) != eslEOF)
132 {
133 if (status != eslOK) esl_msafile_ReadFailure(afp, status);
134
135 shuf = esl_msa_Clone(msa);
136
137 for (i = 0; i < N; i++)
138 {
139 if (esl_opt_GetBoolean(go, "-v")) esl_msashuffle_VShuffle (r, msa, shuf);
140 else if (esl_opt_GetBoolean(go, "-b")) esl_msashuffle_Bootstrap(r, msa, shuf);
141 else esl_msashuffle_Shuffle (r, msa, shuf);
142
143 /* Set the name of the shuffled alignment */
144 if (msa->name != NULL) {
145 if (esl_opt_GetBoolean(go, "-b")) {
146 if (N > 1) esl_msa_FormatName(shuf, "%s-sample-%d", msa->name, i);
147 else esl_msa_FormatName(shuf, "%s-sample", msa->name);
148 } else {
149 if (N > 1) esl_msa_FormatName(shuf, "%s-shuffle-%d", msa->name, i);
150 else esl_msa_FormatName(shuf, "%s-shuffle", msa->name);
151 }
152 } else {
153 if (esl_opt_GetBoolean(go, "-b")) {
154 if (N > 1) esl_msa_FormatName(shuf, "sample-%d", i);
155 else esl_msa_FormatName(shuf, "sample");
156 } else {
157 if (N > 1) esl_msa_FormatName(shuf, "shuffle-%d", i);
158 else esl_msa_FormatName(shuf, "shuffle");
159 }
160 }
161
162 esl_msafile_Write(ofp, shuf, afp->format);
163 }
164
165 esl_msa_Destroy(shuf);
166 esl_msa_Destroy(msa);
167 }
168 if(abc != NULL){
169 esl_alphabet_Destroy(abc);
170 }
171 esl_msafile_Close(afp);
172 return eslOK;
173 }
174
175
176 /* seq_generation()
177 *
178 * Generating sequences.
179 */
180 static int
181 seq_generation(ESL_GETOPTS *go, ESL_RANDOMNESS *r, FILE *ofp, int outfmt)
182 {
183 ESL_ALPHABET *abc = NULL;
184 ESL_SQ *sq = NULL;
185 double *fq = NULL;
186 int alphatype = eslUNKNOWN; // static checkers can't see that 1 of --rna, --dna, --amino must be true
187 int N = esl_opt_GetInteger(go, "-N");
188 int L = esl_opt_GetInteger(go, "-L");
189 int i;
190 int status;
191
192 if (L <= 0) esl_fatal("To generate sequences, set -L option (length of generated seqs) > 0 ");
193 if (esl_opt_GetBoolean(go, "--rna")) alphatype = eslRNA;
194 if (esl_opt_GetBoolean(go, "--dna")) alphatype = eslDNA;
195 if (esl_opt_GetBoolean(go, "--amino")) alphatype = eslAMINO;
196 abc = esl_alphabet_Create(alphatype);
197 sq = esl_sq_CreateDigital(abc);
198 esl_sq_GrowTo(sq, L);
199
200 /* Pick the iid frequency distribution to use */
201 ESL_ALLOC(fq, sizeof(double) * abc->K);
202 switch (alphatype) {
203 case eslRNA:
204 case eslDNA: esl_vec_DSet(fq, 4, 0.25); break;
205 case eslAMINO: esl_composition_SW34(fq); break;
206 default: esl_vec_DSet(fq, abc->K, 1.0 / (double) abc->K); break;
207 }
208
209 /* generate */
210 for (i = 0; i < N; i++)
211 {
212 esl_rsq_xIID(r, fq, abc->K, L, sq->dsq);
213 if (N > 1) esl_sq_FormatName(sq, "random%d", i);
214 else esl_sq_SetName(sq, "random");
215 sq->n = L;
216 esl_sqio_Write(ofp, sq, outfmt, FALSE);
217 }
218
219 free(fq);
220 esl_alphabet_Destroy(abc);
221 esl_sq_Destroy(sq);
222 return eslOK;
223
224 ERROR:
225 if (fq != NULL) free(fq);
226 esl_alphabet_Destroy(abc);
227 esl_sq_Destroy(sq);
228 return status;
229 }
230
231
232 /* seq_shuffling()
233 * SRE, Tue Jan 22 08:35:51 2008 [Market Street Cafe, Leesburg]
234 *
235 * Shuffling of input sequences.
236 *
237 * Fixed-length (L>0) vs. full-length (L=0) modes handled differently.
238 * In fixed-length mode:
239 * <shuff->seq> only needs to be allocated once, for L
240 * <targ> is an allocated copy of a random subseq of length L
241 * sequences < L residues long can't be shuffled
242 * In full-length mode:
243 * <shuff->seq> is grown to length <sq->n> for each input seq
244 * <targ> just points to <sq->seq>
245 */
246 static int
247 seq_shuffling(ESL_GETOPTS *go, ESL_RANDOMNESS *r, FILE *ofp, int outfmt)
248 {
249 char *seqfile = esl_opt_GetArg(go, 1);
250 int infmt = eslSQFILE_UNKNOWN;
251 ESL_SQFILE *sqfp = NULL;
252 ESL_SQ *sq = esl_sq_Create();
253 ESL_SQ *shuff = esl_sq_Create();
254 char *targ = NULL;
255 int N = esl_opt_GetInteger(go, "-N");
256 int L = esl_opt_GetInteger(go, "-L"); /* L>0 means select random fixed-len subseqs */
257 int kmers = 0;
258 int i;
259 int status;
260
261 if (esl_opt_GetString(go, "--informat") != NULL) {
262 infmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--informat"));
263 if (infmt == eslSQFILE_UNKNOWN) esl_fatal("%s is not a valid input sequence file format for --informat");
264 }
265
266 if (esl_opt_IsOn(go, "-k")) kmers = esl_opt_GetInteger(go, "-k");
267
268
269 status = esl_sqfile_Open(seqfile, infmt, NULL, &sqfp);
270 if (status == eslENOTFOUND) esl_fatal("No such file %s", seqfile);
271 else if (status == eslEFORMAT) esl_fatal("Format of seqfile %s unrecognized.", seqfile);
272 else if (status == eslEINVAL) esl_fatal("Can't autodetect stdin or .gz.");
273 else if (status != eslOK) esl_fatal("Open failed, code %d.", status);
274
275 if (L>0) {
276 esl_sq_GrowTo(shuff, L);
277 shuff->n = L;
278 ESL_ALLOC(targ, sizeof(char) * (L+1));
279 }
280
281 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK)
282 {
283 if (L == 0) { /* shuffling entire sequence */
284 esl_sq_GrowTo(shuff, sq->n); /* make sure shuff can hold sq */
285 shuff->n = sq->n;
286 targ = sq->seq;
287 } else {
288 if (sq->n < L) continue; /* reject seqs < L long */
289 }
290
291 for (i = 0; i < N; i++)
292 {
293 if (L > 0) { /* fixed-len mode: copy a random subseq */
294 int pos = esl_rnd_Roll(r, sq->n - L + 1);
295 strncpy(targ, sq->seq + pos, L);
296 targ[L] = '\0';
297 }
298
299 /* Do the requested kind of shuffling */
300 if (esl_opt_GetBoolean(go, "-m")) esl_rsq_CShuffle (r, targ, shuff->seq); /* monoresidue shuffling */
301 else if (esl_opt_GetBoolean(go, "-d")) esl_rsq_CShuffleDP (r, targ, shuff->seq); /* diresidue shuffling */
302 else if (esl_opt_IsOn (go, "-k")) esl_rsq_CShuffleKmers(r, targ, kmers, shuff->seq); /* diresidue shuffling */
303 else if (esl_opt_GetBoolean(go, "-0")) esl_rsq_CMarkov0 (r, targ, shuff->seq); /* 0th order Markov */
304 else if (esl_opt_GetBoolean(go, "-1")) esl_rsq_CMarkov1 (r, targ, shuff->seq); /* 1st order Markov */
305 else if (esl_opt_GetBoolean(go, "-r")) esl_rsq_CReverse ( targ, shuff->seq); /* reverse */
306 else if (esl_opt_IsOn (go, "-w")) { /* regionally shuffle */
307 int W= esl_opt_GetInteger(go, "-w"); esl_rsq_CShuffleWindows(r, targ, W, shuff->seq);
308 }
309
310 /* Set the name of the shuffled sequence */
311 if (N > 1) esl_sq_FormatName(shuff, "%s-shuffled-%d", sq->name, i);
312 else esl_sq_FormatName(shuff, "%s-shuffled", sq->name);
313
314 /* Output the resulting sequence */
315 esl_sqio_Write(ofp, shuff, outfmt, FALSE);
316
317 /* don't need to reuse the shuffled sequence: we will use exactly the same memory */
318 }
319 esl_sq_Reuse(sq);
320 }
321 if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n",
322 sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
323 else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s",
324 status, sqfp->filename);
325
326 if (L>0) free(targ);
327 esl_sq_Destroy(shuff);
328 esl_sq_Destroy(sq);
329 esl_sqfile_Close(sqfp);
330 return eslOK;
331
332 ERROR:
333 if (targ != NULL) free(targ);
334 esl_sq_Destroy(shuff);
335 esl_sq_Destroy(sq);
336 esl_sqfile_Close(sqfp);
337 return status;
338 }
339
340
341 int
342 main(int argc, char **argv)
343 {
344 ESL_GETOPTS *go = NULL; /* application configuration */
345 ESL_RANDOMNESS *r = NULL; /* random number generator */
346 FILE *ofp = NULL; /* data output stream */
347 int outfmt = eslSQFILE_FASTA;
348
349 /* Parse command line */
350 go = esl_getopts_Create(options);
351 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK)
352 cmdline_failure(argv[0], "Failed to parse command line: %s\n", go->errbuf);
353 if (esl_opt_VerifyConfig(go) != eslOK)
354 cmdline_failure(argv[0], "Error in app configuration: %s\n", go->errbuf);
355 if (esl_opt_GetBoolean(go, "-h") )
356 cmdline_help(argv[0], go);
357
358 /* Open the output data file, if any */
359 if (esl_opt_GetString(go, "-o") != NULL)
360 {
361 if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL)
362 esl_fatal("Failed to open output file %s\n", esl_opt_GetString(go, "-o"));
363 }
364 else ofp = stdout;
365
366 /* Initialize */
367 r = esl_randomness_Create(esl_opt_GetInteger(go, "--seed"));
368
369 /* Hand off execution to one of the three modes */
370 if (esl_opt_GetBoolean(go, "-A")) /* Column shuffling ("horizontal") */
371 {
372 if (esl_opt_ArgNumber(go) != 1) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
373 msa_shuffling(go, r, ofp, outfmt);
374 }
375 else if (esl_opt_GetBoolean(go, "-G")) /* Sequence generation */
376 {
377 if (esl_opt_ArgNumber(go) != 0) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
378 seq_generation(go, r, ofp, outfmt);
379 }
380 else if (esl_opt_GetBoolean(go, "-S")) /* Sequence shuffling */
381 {
382 if (esl_opt_ArgNumber(go) != 1) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
383 seq_shuffling(go, r, ofp, outfmt);
384 }
385
386 if (esl_opt_GetString(go, "-o") != NULL) fclose(ofp);
387 esl_randomness_Destroy(r);
388 esl_getopts_Destroy(go);
389 return 0;
390 }
391
392
393