1 /* SQUID - A C function library for biological sequence analysis
2  * Copyright (C) 1992-1996 Sean R. Eddy
3  *
4  *    This source code is distributed under terms of the
5  *    GNU General Public License. See the files COPYING
6  *    and GNULICENSE for further details.
7  *
8  */
9 
10 /* main for shuffle
11  *
12  * shuffle - generate shuffled sequences
13  * Mon Feb 26 16:56:08 1996
14  */
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <time.h>
19 #include "squid.h"
20 
21 struct opt_s OPTIONS[] = {
22   { "-h",     TRUE, ARG_NONE },	    /* help                                  */
23   { "-n",     TRUE, ARG_INT  },     /* number of shuffled seqs per input seq */
24   { "--seed", FALSE, ARG_INT },	    /* set the random number seed            */
25 };
26 #define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))
27 
28 
29 char usage[]  = "Usage: shuffle [-options] <seqfile>\n\
30   Generate shuffled copies of input sequences.\n\
31   Available options:\n\
32   -h         : help; print version and usage info\n\
33   -n <num>   : make <num> shuffles per input seq (default 1)\n\
34   --seed <s> : set random number seed to <s>\n\
35 ";
36 
37 
38 int
main(int argc,char ** argv)39 main(int argc, char **argv)
40 {
41   char  *seqfile;               /* name of sequence file */
42   SQFILE *dbfp;			/* open sequence file */
43   int    fmt;			/* format of seqfile  */
44   char  *seq;			/* sequence */
45   SQINFO sqinfo;                /* additional sequence info */
46   char  *shuff;                 /* shuffled sequence */
47   int    num;			/* number to generate */
48   int    seed;			/* random number generator seed */
49   int    i;
50 
51   char  *optname;               /* option name */
52   char  *optarg;                /* option argument (or NULL) */
53   int    optind;                /* index of next argv[] */
54 
55 
56   /***********************************************
57    * Parse command line
58    ***********************************************/
59 
60   num  = 1;
61   seed = (int) time ((time_t *) NULL);
62 
63   while (Getopt(argc, argv, OPTIONS, NOPTIONS, usage, &optind, &optname, &optarg))
64     {
65       if      (strcmp(optname, "-n")     == 0) { num  = atoi(optarg); }
66       else if (strcmp(optname, "--seed") == 0) { seed = atoi(optarg); }
67       else if (strcmp(optname, "-h")     == 0) {
68 	printf("shuffle %s, %s\n%s\n", squid_version, squid_date, usage);
69 	exit(EXIT_SUCCESS);
70       }
71     }
72 
73   if (argc - optind != 1) Die("%s\n", usage);
74   seqfile = argv[optind];
75 
76   sre_srandom(seed);
77 
78   if (! SeqfileFormat(seqfile, &fmt, NULL))
79     Die("Failed to determine format of file %s", seqfile);
80   if ((dbfp = SeqfileOpen(seqfile, fmt, NULL)) == NULL)
81     Die("Failed to open sequence file %s for reading", seqfile);
82 
83   while (ReadSeq(dbfp, fmt, &seq, &sqinfo))
84     {
85       shuff = (char *) MallocOrDie ((sqinfo.len + 1) * sizeof(char));
86 
87       for (i = 0; i < num; i++)
88 	{
89 	  StrShuffle(shuff, seq);
90 	  WriteSeq(stdout, kPearson, shuff, &sqinfo);
91 	}
92 
93       free(shuff);
94       FreeSequence(seq, &sqinfo);
95     }
96 
97   SeqfileClose(dbfp);
98   return 0;
99 }
100 
101