initial commit
[jalview.git] / forester / archive / RIO / others / hmmer / testsuite / tophits_test.c
1 /* tophits_test.c
2  * SRE, Tue Oct 28 08:03:10 1997 [Newton Institute, Cambridge UK]
3  * 
4  * Test driver for tophits.c. Returns 0 if everything is OK.
5  * 
6  * Options:
7  *    -v   Verbose; print stuff.
8  */
9
10 #include <stdio.h>
11 #include <time.h>
12
13 #include "structs.h"
14 #include "funcs.h"
15 #include "globals.h"
16 #include "squid.h"
17
18 static char banner[] = "\
19 tophits_test : internal verification of tophits.c";
20
21 static char usage[] = "\
22 Usage: tophits_test [-options]\n\
23   Available options are:\n\
24     -h     : help; display this usage info\n\
25     -s <x> : set random seed to <x>\n\
26     -v     : be verbose (default is to simply exit with status 1 or 0)\n\
27 "; 
28
29 static char experts[] = "\
30 \n";
31
32 static struct opt_s OPTIONS[] = {
33   { "-h", TRUE, sqdARG_NONE },
34   { "-s", TRUE, sqdARG_INT  }, 
35   { "-v", TRUE, sqdARG_NONE },
36 };
37 #define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))
38
39 int
40 main(int argc, char **argv)
41 {
42   struct tophit_s *hit;         /* hit list                     */
43   int i,j;                      /* counters                     */
44   int nsamples;                 /* option: # of random "scores" */
45   int be_verbose;               /* option: TRUE to show output  */
46   int seed;                     /* option: random number seed   */
47   int paramH;                   /* option: H parameter          */
48   int paramA;                   /* option: A parameter          */
49   double *list;                 /* list of "scores"             */
50   double tmp;                   /* used for swapping            */
51   float score, score2;
52
53   char *optname;                /* name of option found by Getopt()         */
54   char *optarg;                 /* argument found by Getopt()               */
55   int   optind;                 /* index in argv[]                          */
56
57   /*********************************************** 
58    * Parse command line
59    ***********************************************/
60   be_verbose = FALSE;
61   seed       = (int) time ((time_t *) NULL);
62   paramH     = 100;
63   paramA     = 10;
64   nsamples   = 1000;
65
66   while (Getopt(argc, argv, OPTIONS, NOPTIONS, usage,
67                 &optind, &optname, &optarg))  {
68     if      (strcmp(optname, "-s") == 0) { seed       = atoi(optarg); }
69     else if (strcmp(optname, "-v") == 0) { be_verbose = TRUE;         }
70     else if (strcmp(optname, "-h") == 0) {
71       Banner(stdout, banner);
72       puts(usage);
73       puts(experts);
74       exit(0);
75     }
76   }
77   if (argc - optind != 0)
78     Die("Incorrect number of arguments.\n%s\n", usage);
79
80   sre_srandom(seed);
81   if (be_verbose)
82     printf("%d\tSEED\n", seed);
83
84   /*********************************************** 
85    * Generate three tiers of numbers:
86    *    paramA - really good scores, 1000-2000
87    *    paramH - good scores, 100-200
88    *    nsamples - paramH - paramA: bad scores, 10-20         
89    * then shuffle.
90    ***********************************************/
91
92   list = MallocOrDie (sizeof(double) * nsamples);
93   for (i = 0; i < paramA; i++)
94     list[i] = 1000. + 1000. * sre_random();
95   for (; i < paramA + paramH; i++)
96     list[i] = 100. + 100. * sre_random(); 
97   for (; i < nsamples; i++)
98     list[i] = 10. + 10. * sre_random();
99
100   for (i = 0; i < nsamples; i++)
101     {
102       j = CHOOSE(nsamples);
103       tmp = list[j];
104       list[j] = list[i];
105       list[i] = tmp;
106     }
107
108   if (be_verbose)
109     for (i = 0; i < nsamples; i++)
110       printf("%8.2f\tTest set\n", list[i]);
111
112   /*********************************************** 
113    * Test of FullSortTophits().
114    *     Fill up a hit list with random numbers;
115    *     FullSort it;
116    *     check that all top H are >= 100 and sorted.
117    ***********************************************/
118
119   hit = AllocTophits(100);
120   for (i = 0; i < nsamples; i++)
121     RegisterHit(hit, list[i], 0., (float) list[i], 0., 0., 
122                 NULL, NULL, NULL, /* name, acc, desc */
123                 0,0,0,
124                 0,0,0,
125                 0,0,
126                 NULL);
127   FullSortTophits(hit);
128
129   if (be_verbose)
130     {
131       for (i = 0; i < hit->num; i++)
132         {
133           GetRankedHit(hit, i,  NULL, &score, NULL, NULL, 
134                        NULL, NULL, NULL, /* name, acc, desc */
135                        NULL, NULL, NULL, 
136                        NULL, NULL, NULL, 
137                        NULL, NULL,
138                        NULL);
139           printf("%8.2f  FullSort()\n", score);
140         }
141     }
142
143   for (i = 0; i < hit->num-1; i++)
144     {
145       GetRankedHit(hit, i,  NULL, &score, NULL, NULL, 
146                    NULL, NULL, NULL, /* name, acc, desc */
147                    NULL, NULL, NULL, 
148                    NULL, NULL, NULL,
149                    NULL, NULL,
150                    NULL);
151       GetRankedHit(hit, i+1,NULL, &score2,NULL, NULL, 
152                    NULL, NULL, NULL, /* name, acc, desc */
153                    NULL, NULL, NULL, 
154                    NULL, NULL, NULL, 
155                    NULL, NULL,
156                    NULL);
157       if (score < score2)
158         Die("FullSortTophits() fails test: order wrong");
159       if (i < paramA && score < 1000.)
160         Die("FullSortTophits() fails test: lost a number");
161       if (i < paramA + paramH && score < 100.)
162         Die("FullSortTophits() fails test: lost a number");
163     }
164
165   FreeTophits(hit);
166   free(list);
167   
168   if (be_verbose) printf("tophits_test is OK\n");
169   return 0;
170 }