Debian Codegraph @

hmmer/src/p7_null3.c

raw symbols SCIP
1 /* "null3" model: biased composition correction
2 *
3 * Contents:
4 * 1. Null3 estimation algorithms.
5 * 2. Benchmark driver.
6 * 3. Unit tests.
7 * 4. Test driver.
8 *
9 * See p7_domaindef.c -- null3 correction of per-seq and per-domain
10 * scores is embedded within p7_domaindef's logic; we split it out
11 * to a separate file because it's so important.
12 *
13 * Approach is based heavily on the null3 approach used in Infernal,
14 * and described in its user guide, specifically based on
15 * ScoreCorrectionNull3CompUnknown()
16 */
17 #include <p7_config.h>
18
19 #include "easel.h"
20 #include "esl_vectorops.h"
21
22 #include "hmmer.h"
23
24
25 /*****************************************************************
26 * 1. Null3 estimation algorithms.
27 *****************************************************************/
28
29
30 /* Function: p7_null3_score()
31 *
32 * Purpose: Calculate a correction (in log_2 odds) to be applied
33 * to a sequence, using a null model based on the
34 * composition of the target sequence.
35 * The null model is constructed /post hoc/ as the
36 * distribution of the target sequence; if the target
37 * sequence is 40% A, 5% C, 5% G, 40% T, then the null
38 * model is (0.4, 0.05, 0.05, 0.4). This function is
39 * based heavily on Infernal's ScoreCorrectionNull3(),
40 * with two important changes:
41 * - it leaves the log2 conversion from NATS to BITS
42 * for the calling function.
43 * - it doesn't include the omega score modifier
44 * (based on prior probability of using the null3
45 * model), again leaving this to the calling function.
46 *
47 * Args: abc - alphabet for hit (only used to get alphabet size)
48 * dsq - the sequence the hit resides in
49 * tr - trace of the alignment, used to find the match states
50 * (non-match chars are ignored in computing freq, not used if NULL)
51 * start - start position of hit in dsq
52 * stop - end position of hit in dsq
53 * bg - background, used for the default null model's emission freq
54 * ret_sc - RETURN: the correction to the score (in NATS);
55 * caller subtracts this from hit score to get
56 * corrected score.
57 * Return: void, ret_sc: the log-odds score correction (in NATS).
58 */
59 void
60 p7_null3_score(const ESL_ALPHABET *abc, const ESL_DSQ *dsq, P7_TRACE *tr, int start, int stop, P7_BG *bg, float *ret_sc)
61 {
62 float score = 0.;
63 int status;
64 int i;
65 float *freq;
66 int dir;
67 int tr_pos;
68
69 ESL_ALLOC(freq, sizeof(float) * abc->K);
70 esl_vec_FSet(freq, abc->K, 0.0);
71
72 /* contract check */
73 if(abc == NULL) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "p7_null3_score() alphabet is NULL.%s\n", "");
74 if(dsq == NULL) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "p7_null3_score() dsq alphabet is NULL.%s\n", "");
75 if(abc->type != eslRNA && abc->type != eslDNA) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "p7_null3_score() expects alphabet of RNA or DNA.%s\n", "");
76
77 dir = start < stop ? 1 : -1;
78
79 if (tr != NULL) {
80 /* skip the parts of the trace that precede the first match state */
81 tr_pos = 2;
82 i = start;
83 while (tr->st[tr_pos] != p7T_M) {
84 if (tr->st[tr_pos] == p7T_N)
85 i += dir;
86 tr_pos++;
87 }
88
89 /* tally frequencies from characters hitting match state*/
90 while (tr->st[tr_pos] != p7T_E) {
91 if (tr->st[tr_pos] == p7T_M) {
92 if(esl_abc_XIsGap(abc, dsq[i])) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "in p7_null3_score(), res %d is a gap!%s\n", "");
93 esl_abc_FCount(abc, freq, dsq[i], 1.);
94 }
95 if (tr->st[tr_pos] != p7T_D )
96 i += dir;
97 tr_pos++;
98 }
99 } else {
100 /* tally frequencies from the full envelope */
101 for (i=ESL_MIN(start,stop); i <= ESL_MAX(start,stop); i++)
102 {
103 if(esl_abc_XIsGap(abc, dsq[i])) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "in p7_null3_score(), res %d is a gap!%s\n", "");
104 esl_abc_FCount(abc, freq, dsq[i], 1.);
105 }
106 }
107
108 esl_vec_FNorm(freq, abc->K);
109
110
111 /* now compute score modifier (nats) - note: even with tr!=NULL, this includes the unmatched characters*/
112 for (i = 0; i < abc->K; i++)
113 score += freq[i]==0 ? 0.0 : esl_logf( freq[i]/bg->f[i] ) * freq[i] * ( (stop-start)*dir +1) ;
114
115 /* Return the correction to the bit score. */
116 score = p7_FLogsum(0., score);
117 *ret_sc = score;
118
119 return;
120
121 ERROR:
122 esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "p7_null3_score() memory allocation error.%s\n", "");
123 return; /* never reached */
124
125 }
126
127
128
129 /*****************************************************************
130 * 2. Benchmark driver
131 *****************************************************************/
132 #ifdef p7_NULL3_BENCHMARK
133 /*
134 icc -O3 -static -o generic_null2_benchmark -I. -L. -I../easel -L../easel -Dp7GENERIC_NULL2_BENCHMARK generic_null2.c -lhmmer -leasel -lm
135 ./benchmark-generic-null2 <hmmfile>
136 RRM_1 (M=72) Caudal_act (M=136) SMC_N (M=1151)
137 ----------------- ------------------ -------------------
138 21 Aug 2008 7.77u (185 Mc/s) 14.13u (192 Mc/s) 139.03u (165.6 Mc/s)
139 */
140 #include <p7_config.h>
141
142 #include "easel.h"
143 #include "esl_alphabet.h"
144 #include "esl_getopts.h"
145 #include "esl_random.h"
146 #include "esl_randomseq.h"
147 #include "esl_stopwatch.h"
148
149 #include "hmmer.h"
150
151 static ESL_OPTIONS options[] = {
152 /* name type default env range toggles reqs incomp help docgroup*/
153 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
154 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
155 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
156 { "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
157 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
158 };
159 static char usage[] = "[-options] <hmmfile>";
160 static char banner[] = "benchmark driver for posterior residue null2, generic version";
161
162 int
163 main(int argc, char **argv)
164 {
165 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
166 char *hmmfile = esl_opt_GetArg(go, 1);
167 ESL_STOPWATCH *w = esl_stopwatch_Create();
168 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
169 ESL_ALPHABET *abc = NULL;
170 P7_HMMFILE *hfp = NULL;
171 P7_HMM *hmm = NULL;
172 P7_BG *bg = NULL;
173 P7_PROFILE *gm = NULL;
174 P7_GMX *gx1 = NULL;
175 P7_GMX *gx2 = NULL;
176 int L = esl_opt_GetInteger(go, "-L");
177 int N = esl_opt_GetInteger(go, "-N");
178 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
179 float null2[p7_MAXCODE];
180 int i;
181 float fsc, bsc;
182 double Mcs;
183
184 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
185 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
186
187 bg = p7_bg_Create(abc); p7_bg_SetLength(bg, L);
188 gm = p7_profile_Create(hmm->M, abc); p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
189 gx1 = p7_gmx_Create(gm->M, L);
190 gx2 = p7_gmx_Create(gm->M, L);
191
192 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
193 p7_GForward (dsq, L, gm, gx1, &fsc);
194 p7_GBackward(dsq, L, gm, gx2, &bsc);
195 p7_GDecoding(gm, gx1, gx2, gx2);
196
197 esl_stopwatch_Start(w);
198 for (i = 0; i < N; i++)
199 p7_GNull2_ByExpectation(gm, gx2, null2);
200 esl_stopwatch_Stop(w);
201
202 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / w->user;
203 esl_stopwatch_Display(stdout, w, "# CPU time: ");
204 printf("# M = %d\n", gm->M);
205 printf("# %.1f Mc/s\n", Mcs);
206
207 free(dsq);
208 p7_gmx_Destroy(gx1);
209 p7_gmx_Destroy(gx2);
210 p7_profile_Destroy(gm);
211 p7_bg_Destroy(bg);
212 p7_hmm_Destroy(hmm);
213 p7_hmmfile_Close(hfp);
214 esl_alphabet_Destroy(abc);
215 esl_stopwatch_Destroy(w);
216 esl_randomness_Destroy(r);
217 esl_getopts_Destroy(go);
218 return 0;
219 }
220 #endif /*p7_NULL3_BENCHMARK*/
221 /*------------------ end, benchmark driver ----------------------*/
222
223
224
225
226 /*****************************************************************
227 * 3. Unit tests
228 *****************************************************************/
229 #ifdef p7_NULL3_TESTDRIVE
230
231 static void
232 utest_correct_normalization(ESL_RANDOMNESS *r, P7_PROFILE *gm, P7_BG *bg, ESL_DSQ *dsq, int L, P7_GMX *fwd, P7_GMX *bck)
233 {
234 char *msg = "normalization unit test failed";
235 float null2[p7_MAXABET];
236 float sum;
237 int x;
238
239 esl_rsq_xfIID(r, bg->f, gm->abc->K, L, dsq); /* sample a random digital seq of length L */
240
241 p7_GForward (dsq, L, gm, fwd, NULL);
242 p7_GBackward(dsq, L, gm, bck, NULL);
243 p7_PosteriorNull2(L, gm, fwd, bck, bck); /* <bck> now contains posterior probs */
244 p7_Null2Corrections(gm, dsq, L, 0, bck, fwd, null2, NULL, NULL); /* use <fwd> as workspace */
245
246 /* Convert null2 from lod score to frequencies f_d */
247 for (x = 0; x < gm->abc->K; x++)
248 null2[x] = exp(null2[x]) * bg->f[x];
249
250 sum = esl_vec_FSum(null2, gm->abc->K);
251 if (sum < 0.99 || sum > 1.01) esl_fatal(msg);
252 }
253
254
255 #endif /*p7_NULL3_TESTDRIVE*/
256 /*--------------------- end, unit tests -------------------------*/
257
258
259
260
261 /*****************************************************************
262 * 4. Test driver
263 *****************************************************************/
264 #ifdef p7_NULL3_TESTDRIVE
265 /* gcc -o null2_utest -g -Wall -I../easel -L../easel -I. -L. -Dp7NULL2_TESTDRIVE null2.c -lhmmer -leasel -lm
266 * ./null2_utest
267 */
268 #include <p7_config.h>
269
270 #include <stdio.h>
271 #include <stdlib.h>
272
273 #include "easel.h"
274 #include "esl_getopts.h"
275 #include "esl_random.h"
276 #include "esl_alphabet.h"
277 #include "hmmer.h"
278
279 static ESL_OPTIONS options[] = {
280 /* name type default env range toggles reqs incomp help docgroup*/
281 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
282 { "-L", eslARG_INT, "200", NULL, NULL, NULL, NULL, NULL, "length of sampled sequences", 0 },
283 { "-M", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "length of sampled HMM", 0 },
284 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
285 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
286 };
287
288 static char usage[] = "[-options]";
289 static char banner[] = "unit test driver for the null2 correction calculation";
290
291 int
292 main(int argc, char **argv)
293 {
294 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
295 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
296 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
297 P7_HMM *hmm = NULL;
298 P7_BG *bg = NULL;
299 P7_PROFILE *gm = NULL;
300 P7_GMX *fwd = NULL;
301 P7_GMX *bck = NULL;
302 ESL_DSQ *dsq = NULL;
303 int M = esl_opt_GetInteger(go, "-M");
304 int L = esl_opt_GetInteger(go, "-L");
305
306 /* Sample a random HMM */
307 p7_hmm_Sample(r, M, abc, &hmm);
308
309 /* Configure a profile from the sampled HMM */
310 bg = p7_bg_Create(abc);
311 p7_bg_SetLength(bg, L);
312 gm = p7_profile_Create(hmm->M, abc);
313 p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
314
315 /* Other initial allocations */
316 dsq = malloc(sizeof(ESL_DSQ) * (L+2));
317 fwd = p7_gmx_Create(gm->M, L);
318 bck = p7_gmx_Create(gm->M, L);
319 p7_FLogsumInit();
320
321 utest_correct_normalization(r, gm, bg, dsq, L, fwd, bck);
322
323 free(dsq);
324 p7_gmx_Destroy(fwd);
325 p7_gmx_Destroy(bck);
326 p7_profile_Destroy(gm);
327 p7_bg_Destroy(bg);
328 p7_hmm_Destroy(hmm);
329 esl_alphabet_Destroy(abc);
330 esl_randomness_Destroy(r);
331 esl_getopts_Destroy(go);
332 return 0;
333 }
334 #endif /*p7_NULL3_TESTDRIVE*/
335 /*-------------------- end, test driver -------------------------*/
336
337
338