Debian Codegraph @

src/truncyk_check.c

raw symbols SCIP
1 /* truncyk_check.c
2 */
3
4 #include "config.h"
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <math.h>
10
11 #include "easel.h"
12 #include "esl_alphabet.h"
13 #include "esl_sqio.h"
14 #include "esl_msa.h"
15 #include "esl_stopwatch.h"
16 #include "esl_getopts.h"
17
18 #include "hmmer.h"
19
20 #include "infernal.h"
21
22 static ESL_OPTIONS options[] = {
23 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help", 0},
24 { "--regress", eslARG_INFILE,NULL, NULL, NULL, NULL, NULL, NULL, "save regression test data to file <f>", 0},
25 { "--scoreonly",eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "do score only for full CYK/inside stage to save memory",0},
26 { "--smallonly",eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "skip full CYK/inside, do divide&conquer only", 0},
27 { "--stringent",eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "require the two parse trees to be indentical", 0},
28 {0,0,0,0,0,0,0,0,0,0},
29 };
30 static char usage[] = "Usage: truncyk_check [-options] <cmfile> <sequence file>";
31
32 int
33 main(int argc, char **argv)
34 {
35 int status;
36 char *cmfile; /* file to read CM from */
37 ESL_ALPHABET *abc;
38 char *seqfile; /* file to read sequences from */
39 int format; /* format of sequence file */
40 CM_FILE *cmfp; /* open CM file for reading */
41 ESL_SQFILE *sqfp; /* open seqfile for reading */
42 CM_t *cm; /* a covariance model */
43 ESL_SQ *seq; /* RNA sequence */
44 ESL_STOPWATCH *watch;
45 float sc1, sc2; /* score of a sequence */
46 Parsetree_t *tr1, *tr2; /* a traceback */
47 float ptsc1, ptsc2; /* scores from interpreting parsetrees */
48 int v, model_len;
49 float bsc;
50
51 int do_local; /* TRUE to align locally w.r.t. model */
52 int do_scoreonly; /* TRUE for score-only (small mem) full CYK */
53 int do_smallonly; /* TRUE to do only d&c, not full CYK/inside */
54 int compare_stringently; /* TRUE to demand identical parse trees */
55 char *regressfile; /* name of regression data file to save */
56 FILE *regressfp; /* open filehandle for writing regressions */
57
58 ESL_GETOPTS *go;
59 char errbuf[eslERRBUFSIZE];
60
61 /***********************************************
62 * Parse command line
63 ***********************************************/
64
65 go = esl_getopts_Create(options);
66 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) esl_fatal("Failed to parse command line: %s\n", go->errbuf);
67 if (esl_opt_VerifyConfig(go) != eslOK) esl_fatal("Failed to parse command line: %s\n", go->errbuf);
68
69 if (esl_opt_GetBoolean(go, "-h") == TRUE) {
70 puts(usage);
71 puts("\n where options are:");
72 esl_opt_DisplayHelp(stdout, go, 0, 2, 80); /* 0=all docgroups; 2=indentation; 80=width */
73 return 0;
74 }
75
76 if (esl_opt_ArgNumber(go) != 2) esl_fatal("Incorrect number of command line arguments.\n%s\n", usage);
77 cmfile = esl_opt_GetArg(go, 1);
78 seqfile = esl_opt_GetArg(go, 2);
79
80
81 abc = NULL;
82 do_local = TRUE;
83 do_scoreonly = esl_opt_GetBoolean(go,"--scoreonly");
84 do_smallonly = esl_opt_GetBoolean(go,"--smallonly");
85 compare_stringently = esl_opt_GetBoolean(go,"--stringent");
86 regressfile = esl_opt_GetString(go,"--regress");
87 format = eslSQFILE_UNKNOWN;
88
89 /***********************************************
90 * Preliminaries: open our files for i/o; get a CM
91 ***********************************************/
92
93 watch = esl_stopwatch_Create();
94
95 if ( esl_sqfile_Open(seqfile, format, NULL, &sqfp) != eslOK )
96 cm_Die("Failed to open sequence database file %s\n%s\n", seqfile, usage);
97
98 if((status = cm_file_Open(cmfile, NULL, FALSE, &cmfp, errbuf)) != eslOK)
99 cm_Die("Failed to open covariance model save file\n");
100 if ((status = cm_file_Read(cmfp, TRUE, &abc, &cm)) != eslOK)
101 cm_Die("Failed to read a CM from cm file\n");
102 if (cm == NULL)
103 cm_Die("CM file empty?\n");
104
105 /* open regression test data file */
106 if (regressfile != NULL) {
107 if ((regressfp = fopen(regressfile, "w")) == NULL)
108 cm_Die("Failed to open regression test file %s", regressfile);
109 }
110
111 /*
112 if (do_local) ConfigLocal(cm, 0.5, 0.5);
113 CMLogoddsify(cm);
114 CMHackInsertScores(cm);*/ /* TEMPORARY: FIXME */
115
116 if (do_local) cm->config_opts |= CM_CONFIG_LOCAL;
117 if ((status = cm_Configure(cm, errbuf, -1)) != eslOK) return status;
118 SetMarginalScores_reproduce_i27(cm);
119
120 /* EPN 11.18.05 Now that know what windowlen is, we need to ensure that
121 * cm->el_selfsc * W >= IMPOSSIBLE (cm->el_selfsc is the score for an EL self transition)
122 * This is done because we are potentially multiply cm->el_selfsc * W, and adding
123 * that to IMPOSSIBLE. To avoid underflow issues this value must be less than
124 * 3 * IMPOSSIBLE. Here we guarantee its less than 2 * IMPOSSIBLE (to be safe).
125 */
126 if((cm->el_selfsc * cm->W) < IMPOSSIBLE)
127 cm->el_selfsc = (IMPOSSIBLE / (cm->W+1));
128
129 seq = esl_sq_Create();
130 while ( esl_sqio_Read(sqfp, seq) == eslOK )
131 {
132 /* CYKDemands(cm, sqinfo.len); */
133
134 if (seq->n == 0) continue; /* silently skip len 0 seqs */
135
136 if (seq->dsq == NULL)
137 esl_sq_Digitize(abc, seq);
138
139 tr1 = tr2 = NULL;
140
141 /* Length correction for Parsetree scores */
142 model_len = 0;
143 for ( v = 0; v < cm->M; v++ )
144 {
145 if ( cm->stid[v] == MATP_MP ) model_len += 2;
146 else if ( cm->stid[v] == MATL_ML ) model_len += 1;
147 else if ( cm->stid[v] == MATR_MR ) model_len += 1;
148 }
149 /* 2.0 instead of 2 to force floating point division, not integer division */
150 bsc = sreLOG2(2.0/(model_len*(model_len+1)));
151
152 if (! do_smallonly) {
153 printf("Full inside algorithm:\n");
154 printf("----------------------\n");
155 esl_stopwatch_Start(watch);
156 if (do_scoreonly) {
157 sc1 = TrCYK_Inside(cm, seq->dsq, seq->n, 0, 1, seq->n, PLI_PASS_5P_AND_3P_ANY, TRUE, FALSE, NULL); /* TRUE: reproduce 1.0 behavior */
158 printf("%-12s : %.2f\n", seq->name, sc1);
159 } else {
160 sc1 = TrCYK_Inside(cm, seq->dsq, seq->n, 0, 1, seq->n, PLI_PASS_5P_AND_3P_ANY, TRUE, FALSE, &tr1); /* TRUE: reproduce 1.0 behavior */
161 ParsetreeDump(stdout, tr1, cm, seq->dsq);
162 //ptsc1 = ParsetreeScore(cm, tr1, seq->dsq, FALSE);
163 ParsetreeScore(cm, NULL, NULL, tr1, seq->dsq, FALSE, &ptsc1, NULL, NULL, NULL, NULL);
164 ptsc1 += bsc;
165 printf("%-12s : %.2f %.2f\n", seq->name, sc1, ptsc1);
166 }
167 esl_stopwatch_Stop(watch);
168 esl_stopwatch_Display(stdout, watch, "CPU time: ");
169 puts("");
170 }
171
172 printf("Divide and conquer algorithm:\n");
173 printf("-------------------------------\n");
174 esl_stopwatch_Start(watch);
175 sc2 = TrCYK_DnC(cm, seq->dsq, seq->n, 0, 1, seq->n, PLI_PASS_5P_AND_3P_ANY, TRUE, &tr2); /* TRUE: reproduce 1.0 behavior */
176 ParsetreeDump(stdout, tr2, cm, seq->dsq);
177 //ptsc2 = ParsetreeScore(cm, tr2, seq->dsq, FALSE);
178 ParsetreeScore(cm, NULL, NULL, tr2, seq->dsq, FALSE, &ptsc2, NULL, NULL, NULL, NULL);
179 ptsc2 += bsc;
180 printf("%-12s : %.2f %.2f\n", seq->name, sc2, ptsc2);
181 esl_stopwatch_Stop(watch);
182 esl_stopwatch_Display(stdout, watch, "CPU time: ");
183 puts("");
184
185 /* Test that the two solutions are identical; or if not identical,
186 * at least they're alternative solutions w/ equal score.
187 * If not, fail w/ non-zero exit status, so qc protocols
188 * can catch the problem.
189 */
190 if (tr1 != NULL && fabs(sc1 - ptsc1) >= 0.01)
191 cm_Die("TrCYKInside score differs from its parse tree's score\n");
192 if (tr2 != NULL && fabs(sc2 - ptsc2) >= 0.01)
193 cm_Die("TrCYKDivideAndConquer score differs from its parse tree's score\n");
194 if (!do_smallonly && fabs(sc1 - sc2) >= 0.01)
195 cm_Die("TrCYKInside score differs from TrCYKDivideAndConquer\n");
196 if (tr1 != NULL && tr2 != NULL &&
197 compare_stringently && !ParsetreeCompare(tr1, tr2))
198 cm_Die("Parse trees for TrCYKInside and TrCYKDivideAndConquer differ\n");
199
200 /* Save regression test data
201 */
202 if (regressfile != NULL) {
203 if (tr1 != NULL) ParsetreeDump(regressfp, tr1, cm, seq->dsq);
204 if (tr2 != NULL) ParsetreeDump(regressfp, tr2, cm, seq->dsq);
205 }
206
207 if (tr1 != NULL) FreeParsetree(tr1);
208 if (tr2 != NULL) FreeParsetree(tr2);
209
210
211 esl_sq_Destroy(seq);
212 seq = esl_sq_Create();
213 }
214 esl_sq_Destroy(seq);
215
216 if (regressfile != NULL) fclose(regressfp);
217 FreeCM(cm);
218 cm_file_Close(cmfp);
219 esl_sqfile_Close(sqfp);
220 esl_stopwatch_Destroy(watch);
221
222 return EXIT_SUCCESS;
223 }