2 * SRE, Mon Mar 9 07:55:47 1998 [St. Louis]
4 * Exercise the various Viterbi algorithms, big and small.
6 * RCS $Id: viterbi_exercise.c,v 1.1.1.1 2005/03/22 08:34:50 cmzmasek Exp $
20 static char banner[] = "\
21 viterbi_exercise : testing of Plan7 Viterbi code";
23 static char usage[] = "\
24 Usage: testdriver [-options]\n\
25 Available options are:\n\
26 -h : help; display this usage info\n\
30 static char experts[] = "\
31 --hmm <f> : use HMM in file <f>\n\
34 static struct opt_s OPTIONS[] = {
35 { "-h", TRUE, sqdARG_NONE },
36 { "-v", TRUE, sqdARG_NONE },
37 { "--hmm", FALSE, sqdARG_STRING },
39 #define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))
42 main(int argc, char **argv)
44 char *hmmfile; /* file to read HMM(s) from */
45 HMMFILE *hmmfp; /* opened hmmfile for reading */
46 struct plan7_s *hmm; /* the HMM to search with */
47 char *dsq; /* digitized target sequence */
50 int L; /* length of dsq */
51 struct p7trace_s *tr1; /* traceback */
52 struct p7trace_s *tr2; /* another traceback */
54 float sc1, sc2; /* scores */
60 char *optname; /* name of option found by Getopt() */
61 char *optarg; /* argument found by Getopt() */
62 int optind; /* index in argv[] */
65 /***********************************************
67 ***********************************************/
73 while (Getopt(argc, argv, OPTIONS, NOPTIONS, usage,
74 &optind, &optname, &optarg)) {
75 if (strcmp(optname, "-v") == 0) be_verbose = TRUE;
76 else if (strcmp(optname, "--hmm") == 0) hmmfile = optarg;
77 else if (strcmp(optname, "-h") == 0) {
78 Banner(stdout, banner);
84 if (argc - optind != 0)
85 Die("Incorrect number of arguments.\n%s\n", usage);
87 /***********************************************
89 * Read a single HMM from it.
90 ***********************************************/
92 if ((hmmfp = HMMFileOpen(hmmfile, NULL)) == NULL)
93 Die("Failed to open HMM file %s\n%s", hmmfile, usage);
94 if (!HMMFileRead(hmmfp, &hmm))
95 Die("Failed to read any HMMs from %s\n", hmmfile);
97 Die("HMM file %s corrupt or in incorrect format? Parse failed", hmmfile);
98 Plan7Renormalize(hmm);
100 /***********************************************
101 * We cycle through different model configurations.
102 * For each configuration, we repeat 100 times:
103 * - generate a sequence
104 * - score it by Viterbi and by SmallViterbi
105 * - make sure they give OK and identical results
106 ***********************************************/
108 for (config = 1; config <= 5; config++)
111 case 1: Plan7NakedConfig(hmm); break;
112 case 2: Plan7GlobalConfig(hmm); break;
113 case 3: Plan7LSConfig(hmm); break;
114 case 4: Plan7FSConfig(hmm, 0.5, 0.5); break;
115 case 5: Plan7SWConfig(hmm, 0.5, 0.5); break;
116 default: Die("never happens");
118 P7Logoddsify(hmm, TRUE);
120 for (i = 0; i < nseq; i++)
122 EmitSequence(hmm, &dsq, &L, NULL);
123 sprintf(sqinfo.name, "seq%d", i+1);
125 sqinfo.flags = SQINFO_NAME | SQINFO_LEN;
127 sc1 = P7Viterbi(dsq, L, hmm, &tr1);
128 sc2 = P7SmallViterbi(dsq, L, hmm, &tr2);
132 printf("Viterbi score: %.1f SmallViterbi: %.1f\n", sc1, sc2);
133 P7PrintTrace(stdout, tr1, hmm, dsq);
134 P7PrintTrace(stdout, tr2, hmm, dsq);
136 seq = DedigitizeSequence(dsq, L);
137 WriteSeq(stdout, SQFILE_FASTA, seq, &sqinfo);
142 Die("Different scores from normal/small Viterbi");
144 if (fabs(sc1 - P7TraceScore(hmm, dsq, tr1)) > 0.1)
145 Die("P7Viterbi score doesn't match its TraceScore");
146 if (fabs(sc2 - P7TraceScore(hmm, dsq, tr2)) > 0.1)
147 Die("P7SmallViterbi score doesn't match its TraceScore");
149 if (! TraceVerify(tr1, hmm->M, L))
150 Die("TraceVerify() failed for a P7Viterbi trace");
151 if (! TraceVerify(tr2, hmm->M, L))
152 Die("TraceVerify() failed for a P7SmallViterbi trace");
154 if (tr1->tlen != tr2->tlen)
155 Die("Trace lengths differ for normal/small Viterbi");
156 if (! TraceCompare(tr1, tr2))
157 Die("Different traces from normal/small Viterbi");