Debian Codegraph @

hmmer/src/p7_scoredata.c

raw symbols SCIP
1 /* The Plan7 SCOREDATA data structure, which holds a compact representation
2 * of substitution scores and maximal extensions, used by nhmmer.
3 *
4 * Contents:
5 * 1. The P7_SCOREDATA object: allocation, initialization, destruction.
6 * 2. Unit tests.
7 * 3. Test driver.
8 */
9 #include <p7_config.h>
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <time.h>
15
16 #include "easel.h"
17 #include "esl_alphabet.h"
18 #include "esl_vectorops.h"
19 #include "esl_random.h"
20 #include "esl_dirichlet.h"
21
22 #include "hmmer.h"
23
24
25 /*********************************************************************
26 *# 1. The P7_SCOREDATA object: allocation, initialization, destruction.
27 *********************************************************************/
28
29 /* Function: scoredata_GetSSVScoreArrays()
30 * Synopsis: Get compact representation of substitution scores and maximal extensions
31 *
32 * Purpose: Extract 8-bit (MSV-style) substitution scores from optimized
33 * matrix. These scores will be used in both standard MSV diagonal
34 * recovery and FM-MSV diagonal scoring.
35 *
36 * Optionally, for each position in the model, capture the maximum
37 * possible score that can be added to a diagonal's score (in both
38 * directions) by extending lengths 1..10. These extension scores
39 * are used in FM-MSV's pruning step.
40 *
41 * Once a hit passes the SSV filter, and the prefix/suffix
42 * values of P7_SCOREDATA are required (to establish windows
43 * around SSV diagonals), p7_hmm_ScoreDataComputeRest()
44 * must be called.
45 *
46 *
47 * Args: om - P7_OPROFILE containing scores used to produce SCOREDATA contents
48 * data - where scores and will be stored
49 * do_opt_ext - boolean, TRUE if optimal-extension scores are required (for FM-MSV)
50 * scale - used to produce 8-bit extracted scores
51 * bias - used to produce 8-bit extracted scores
52 *
53 * Returns: data->scores and possibly ->opt_ext_(fwd|rev) are filled in;
54 * return eslEMEM on allocation failure, eslOK otherwise.
55 */
56 static int
57 scoredata_GetSSVScoreArrays(P7_OPROFILE *om, P7_PROFILE *gm, P7_SCOREDATA *data ) {
58 int i, j, status;
59
60 //gather values from gm->rsc into a succinct 2D array
61 float *max_scores;
62 float sc_fwd, sc_rev;
63 int K = om->abc->Kp;
64 data->M = om->M;
65
66 if (!gm) { // get values for the standard pipeline
67 data->type = p7_sd_std;
68 ESL_ALLOC(data->ssv_scores, (om->M + 1) * K * sizeof(uint8_t));
69 p7_oprofile_GetSSVEmissionScoreArray(om, data->ssv_scores);
70
71 } else {// need float, unscaled scores, and other stuff used in the FMindex-based SSV pipeline,
72 data->type = p7_sd_fm;
73 ESL_ALLOC(data->ssv_scores_f, (om->M + 1) * K * sizeof(float));
74 ESL_ALLOC(max_scores, (om->M + 1) * sizeof(float));
75
76 for (i = 1; i <= om->M; i++) {
77 max_scores[i] = 0;
78 for (j=0; j<K; j++) {
79 if (esl_abc_XIsResidue(om->abc,j)) {
80 data->ssv_scores_f[i*K + j] = gm->rsc[j][(i) * p7P_NR + p7P_MSC];
81 if (data->ssv_scores_f[i*K + j] > max_scores[i]) max_scores[i] = data->ssv_scores_f[i*K + j];
82 }
83 }
84 }
85
86 //for each position in the query, what's the highest possible score achieved by extending X positions, for X=1..10
87 ESL_ALLOC(data->opt_ext_fwd, (om->M + 1) * sizeof(float*));
88 ESL_ALLOC(data->opt_ext_rev, (om->M + 1) * sizeof(float*));
89
90 for (i=1; i<om->M; i++) {
91 ESL_ALLOC(data->opt_ext_fwd[i], 10 * sizeof(float));
92 ESL_ALLOC(data->opt_ext_rev[i], 10 * sizeof(float));
93 }
94 for (i=1; i<om->M; i++) {
95 sc_fwd = 0;
96 sc_rev = 0;
97 for (j=0; j<10 && i+j+1<=om->M; j++) {
98 sc_fwd += max_scores[i+j+1];
99 data->opt_ext_fwd[i][j] = sc_fwd;
100
101 sc_rev += max_scores[om->M-i-j];
102 data->opt_ext_rev[om->M-i][j] = sc_rev;
103
104 }
105 for ( ; j<10; j++) { //fill in empty values
106 data->opt_ext_fwd[i][j] = data->opt_ext_fwd[i][j-1];
107 data->opt_ext_rev[om->M-i][j] = data->opt_ext_rev[om->M-i][j-1];
108 }
109 }
110 free(max_scores);
111 }
112 return eslOK;
113
114 ERROR:
115 return eslEMEM;
116 }
117
118
119 /* Function: p7_hmm_ScoreDataDestroy()
120 *
121 * Synopsis: Destroy a <P7_SCOREDATA> object.
122 */
123 void
124 p7_hmm_ScoreDataDestroy(P7_SCOREDATA *data )
125 {
126 int i;
127
128 if (data != NULL) {
129
130 if (data->ssv_scores != NULL) free( data->ssv_scores);
131 if (data->prefix_lengths != NULL) free( data->prefix_lengths);
132 if (data->suffix_lengths != NULL) free( data->suffix_lengths);
133 if (data->fwd_scores != NULL) free( data->fwd_scores);
134
135 if (data->fwd_transitions != NULL) {
136 for (i=0; i<p7O_NTRANS; i++)
137 free(data->fwd_transitions[i]);
138 free(data->fwd_transitions);
139 }
140 if (data->opt_ext_fwd != NULL) {
141 for (i=1; i<data->M; i++)
142 free(data->opt_ext_fwd[i]);
143 free(data->opt_ext_fwd);
144 }
145 if (data->opt_ext_rev != NULL) {
146 for (i=1; i<data->M; i++)
147 free(data->opt_ext_rev[i]);
148 free( data->opt_ext_rev);
149 }
150
151 free(data);
152 }
153
154 }
155
156 /* Function: p7_hmm_ScoreDataCreate()
157 * Synopsis: Create a <P7_SCOREDATA> model object, based on MSV-filter
158 * part of profile
159 *
160 * Purpose: Allocate a <P7_SCOREDATA> object, then populate
161 * it with data based on the given optimized matrix.
162 *
163 * Once a hit passes the MSV filter, and the prefix/suffix
164 * values of P7_SCOREDATA are required, p7_hmm_ScoreDataComputeRest()
165 * must be called.
166 *
167 * Args: om - P7_OPROFILE containing scores used to produce SCOREDATA contents
168 * do_opt_ext - boolean, TRUE if optimal-extension scores are required (for FM-MSV)
169 *
170 * Returns: a pointer to the new <P7_SCOREDATA> object.
171 *
172 * Throws: <NULL> on allocation failure.
173 */
174 P7_SCOREDATA *
175 p7_hmm_ScoreDataCreate(P7_OPROFILE *om, P7_PROFILE *gm )
176 {
177 P7_SCOREDATA *data = NULL;
178 int status;
179
180 ESL_ALLOC(data, sizeof(P7_SCOREDATA));
181
182 data->ssv_scores = NULL;
183 data->ssv_scores_f = NULL;
184 data->opt_ext_fwd = NULL;
185 data->opt_ext_rev = NULL;
186 data->prefix_lengths = NULL;
187 data->suffix_lengths = NULL;
188 data->fwd_scores = NULL;
189 data->fwd_transitions = NULL;
190
191 scoredata_GetSSVScoreArrays(om, gm, data);
192
193 return data;
194
195 ERROR:
196 p7_hmm_ScoreDataDestroy(data);
197 return NULL;
198 }
199
200
201 /* Function: p7_hmm_ScoreDataClone()
202 * Synopsis: Clone a <P7_SCOREDATA> model object
203 *
204 * Purpose: Allocate a <P7_SCOREDATA> object used in both FM-MSV and
205 * MSV_LongTarget diagonal recovery/extension, then
206 * copy data into it from another populated instance
207 *
208 * Once a hit passes the MSV filter, and the prefix/suffix
209 * values of P7_SCOREDATA are required, p7_hmm_ScoreDataComputeRest()
210 * must be called.
211 *
212 * Args: src - P7_SCOREDATA upon which clone will be based
213 * Kp - alphabet size, including degeneracy codes, gaps
214 *
215 * Returns: a pointer to the new <P7_SCOREDATA> object.
216 *
217 * Throws: <NULL> on allocation failure.
218 */
219 P7_SCOREDATA *
220 p7_hmm_ScoreDataClone(P7_SCOREDATA *src, int Kp) {
221 P7_SCOREDATA *new;
222 int status;
223 int i;
224
225 if (src == NULL)
226 return NULL;
227
228 ESL_ALLOC(new, sizeof(P7_SCOREDATA));
229 new->M = src->M;
230 new->type = src->type;
231 new->ssv_scores = NULL;
232 new->opt_ext_fwd = NULL;
233 new->opt_ext_rev = NULL;
234 new->prefix_lengths = NULL;
235 new->suffix_lengths = NULL;
236 new->fwd_scores = NULL;
237 new->fwd_transitions = NULL;
238
239 if (new->type == p7_sd_std) {
240 ESL_ALLOC(new->ssv_scores, (src->M + 1) * Kp * sizeof(uint8_t));
241 memcpy(new->ssv_scores, src->ssv_scores, (src->M + 1) * Kp * sizeof(uint8_t) );
242 } else {
243 ESL_ALLOC(new->ssv_scores_f, (src->M + 1) * Kp * sizeof(float));
244 memcpy(new->ssv_scores_f, src->ssv_scores_f, (src->M + 1) * Kp * sizeof(float) );
245 }
246
247
248 if (src->prefix_lengths != NULL) {
249 ESL_ALLOC(new->prefix_lengths, (src->M+1) * sizeof(float));
250 memcpy(new->prefix_lengths, src->prefix_lengths, (src->M+1) * sizeof(float));
251 }
252 if (src->suffix_lengths != NULL) {
253 ESL_ALLOC(new->suffix_lengths, (src->M+1) * sizeof(float));
254 memcpy(new->suffix_lengths, src->suffix_lengths, (src->M+1) * sizeof(float));
255 }
256 if (src->fwd_scores != NULL) {
257 ESL_ALLOC(new->fwd_scores, (src->M+1) * sizeof(float));
258 memcpy(new->fwd_scores, src->fwd_scores, (src->M+1) * sizeof(float));
259 }
260
261
262 if (src->opt_ext_fwd != NULL) {
263 ESL_ALLOC(new->opt_ext_fwd, (src->M + 1) * sizeof(float*));
264 for (i=1; i<src->M; i++) {
265 ESL_ALLOC(new->opt_ext_fwd[i], 10 * sizeof(float));
266 memcpy(new->opt_ext_fwd[i], src->opt_ext_fwd[i], 10 * sizeof(float));
267 }
268 }
269 if (src->opt_ext_rev != NULL) {
270 ESL_ALLOC(new->opt_ext_rev, (src->M + 1) * sizeof(float*));
271 for (i=1; i<src->M; i++) {
272 ESL_ALLOC(new->opt_ext_rev[i], 10 * sizeof(float));
273 memcpy(new->opt_ext_rev[i], src->opt_ext_rev[i], 10 * sizeof(float));
274 }
275 }
276 if (src->fwd_transitions != NULL) {
277 ESL_ALLOC(new->fwd_transitions, p7O_NTRANS * sizeof(float*));
278 for (i=0; i<p7O_NTRANS; i++) {
279 ESL_ALLOC(new->fwd_transitions[i], (src->M+1)* sizeof(float));
280 memcpy(new->fwd_transitions[i], src->fwd_transitions[i], (src->M+1) * sizeof(float));
281 }
282 }
283
284 return new;
285
286 ERROR:
287 return NULL;
288 }
289
290 /* Function: p7_hmm_ScoreDataComputeRest()
291 * Synopsis: Using position-specific insert rates, compute MAXL-based prefix and suffix lengths
292 *
293 * Purpose: Using position-specific insert rates, compute MAXL-based prefix
294 * and suffix lengths for each position in the model, used when
295 * establishing windows around SSV diagonals. This fleshes out
296 * the <P7_SCOREDATA> model object that was created by
297 * p7_hmmScoreDataCreate().
298 *
299 * This approach of computing the prefix/suffix length, used
300 * in establishing windows around a seed diagonal, is fast
301 * because it uses a simple closed-form computation of the
302 * length L_i for each position i at which all but
303 * (1-p7_DEFAULT_WINDOW_BETA) of position i's match- and
304 * insert-state emissions are length L_i or shorter.
305 *
306 * Args: om - P7_OPROFILE containing emission/transition probabilities used to for calculations
307 * data - P7_SCOREDATA into which the computed values are placed
308 *
309 * Returns: eslEMEM on failure, else eslOK
310 *
311 * Throws: <NULL> on allocation failure.
312 */
313 int
314 p7_hmm_ScoreDataComputeRest(P7_OPROFILE *om, P7_SCOREDATA *data )
315 {
316 int status;
317 int k;
318 float sum;
319 float *t_mis;
320 float *t_iis;
321
322 ESL_ALLOC(data->fwd_scores, sizeof(float) * om->abc->Kp * (om->M+1));
323 p7_oprofile_GetFwdEmissionScoreArray(om, data->fwd_scores);
324
325 //2D array, holding all the transition scores/costs
326 ESL_ALLOC(data->fwd_transitions, sizeof(float*) * p7O_NTRANS);
327
328 for (k=0; k<p7O_NTRANS; k++) {
329 ESL_ALLOC(data->fwd_transitions[k], sizeof(float) * (om->M+1));
330 p7_oprofile_GetFwdTransitionArray(om, k, data->fwd_transitions[k] );
331 }
332 t_mis = data->fwd_transitions[p7O_MI];
333 t_iis = data->fwd_transitions[p7O_II];
334
335 /*
336 * Elsewhere, we compute the MAXL of a given model, which is the length L
337 * such that only a minute fraction (BETA = 1e-7) of emitted sequence are length > L.
338 *
339 * In the DNA search pipeline, when a high-scoring SSV alignment is identified,
340 * we extract a window around that seed. The length of the window is based on
341 * MAXL, but we need to figure out how much the window should precede the seed
342 * (the prefix of the window) and how much should follow the seed (the suffix).
343 *
344 * Our heuristic strategy is to estimate how much of that MAXL is likely to have
345 * come from each position. Using these relative contributions of each position,
346 * we can compute the length of extracted window prior to the seed as
347 * MAXL * (sum of the expected fractional contributions of preceding positions),
348 * and do the same for the extracted window following the seed (suffix sum). This
349 * code supports that process, by computing prefix-sum and suffix-sum values for
350 * these relative contributions.
351 *
352 * We estimate the relative contributions of a position k by computing for position
353 * k, the minimum length l_k at which the tail probability mass for sequences
354 * emitted by position k (match and insert states) P(L>l_k) < BETA.
355 * These per-position lengths are summed, and the relative contribution of a
356 * position is it's l_k normalized by the sum of all lengths
357 */
358 ESL_ALLOC(data->prefix_lengths, (om->M+1) * sizeof(float));
359 ESL_ALLOC(data->suffix_lengths, (om->M+1) * sizeof(float));
360
361 sum = 0;
362 for (k=1; k < om->M; k++) {
363 if (t_mis[k] == 0)
364 data->prefix_lengths[k] = 1;
365 else
366 data->prefix_lengths[k] = 1 + (int)(log(p7_DEFAULT_WINDOW_BETA / t_mis[k] )/log(t_iis[k]));
367
368 sum += data->prefix_lengths[k];
369 }
370 data->prefix_lengths[0] = data->prefix_lengths[om->M] = 0;
371
372 for (k=1; k < om->M; k++)
373 data->prefix_lengths[k] /= sum;
374
375 data->suffix_lengths[om->M] = data->prefix_lengths[om->M-1];
376 for (k=om->M - 1; k >= 1; k--)
377 data->suffix_lengths[k] = data->suffix_lengths[k+1] + data->prefix_lengths[k-1];
378 for (k=2; k < om->M; k++)
379 data->prefix_lengths[k] += data->prefix_lengths[k-1];
380
381 return eslOK;
382
383 ERROR:
384 p7_hmm_ScoreDataDestroy(data);
385 return eslEMEM;
386 }
387
388
389 /*****************************************************************
390 * 2. Unit tests
391 *****************************************************************/
392 #ifdef p7SCOREDATA_TESTDRIVE
393
394 static void
395 utest_createScoreData(ESL_GETOPTS *go, ESL_RANDOMNESS *r )
396 {
397 char msg[] = "createScoreData unit test failed";
398 P7_HMM *hmm = NULL;
399 ESL_ALPHABET *abc = NULL;
400 P7_PROFILE *gm = NULL;
401 P7_OPROFILE *om = NULL;
402 P7_SCOREDATA *scoredata = NULL;
403 uint8_t scale = 3.0 / eslCONST_LOG2; /* scores in units of third-bits */
404 float max = 0.0;
405 int x;
406
407 if ( (abc = esl_alphabet_Create(eslDNA)) == NULL) esl_fatal(msg);
408
409 if ( p7_hmm_Sample(r, 100, abc, &hmm) != eslOK) esl_fatal(msg);
410 if ( (gm = p7_profile_Create (hmm->M, abc)) == NULL ) esl_fatal(msg);
411 if ( (om = p7_oprofile_Create(hmm->M, abc)) == NULL ) esl_fatal(msg);
412
413 for (x = 0; x < gm->abc->K; x++) max = ESL_MAX(max, esl_vec_FMax(gm->rsc[x], (gm->M+1)*2));
414 //based on unbiased_byteify
415 max = -1.0f * roundf(scale * max);
416
417 if ( (scoredata = p7_hmm_ScoreDataCreate(om, FALSE)) == NULL ) esl_fatal(msg);
418
419 p7_hmm_ScoreDataDestroy(scoredata);
420 p7_oprofile_Destroy(om);
421 p7_profile_Destroy(gm);
422 p7_hmm_Destroy(hmm);
423 esl_alphabet_Destroy(abc);
424 }
425 #endif /*p7SCOREDATA_TESTDRIVE*/
426
427
428 /*****************************************************************
429 * 3. Test driver
430 *****************************************************************/
431
432 #ifdef p7SCOREDATA_TESTDRIVE
433 #include <esl_config.h>
434
435 #include <stdio.h>
436
437 #include "easel.h"
438 #include "esl_getopts.h"
439 #include "esl_random.h"
440
441 #include "hmmer.h"
442
443 static ESL_OPTIONS options[] = {
444 /* name type default env range togs reqs incomp help docgrp */
445 {"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
446 {"-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0},
447 {"-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show verbose commentary/output", 0},
448 { 0,0,0,0,0,0,0,0,0,0},
449 };
450 static char usage[] = "[-options]";
451 static char banner[] = "test driver for p7_bg";
452
453 int
454 main(int argc, char **argv)
455 {
456 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
457 ESL_RANDOMNESS *rng = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
458 int be_verbose = esl_opt_GetBoolean(go, "-v");
459
460 if (be_verbose) printf("p7_scoredata unit test: rng seed %" PRIu32 "\n", esl_randomness_GetSeed(rng));
461
462 utest_createScoreData(go, rng);
463
464 esl_randomness_Destroy(rng);
465 esl_getopts_Destroy(go);
466 return 0;
467 }
468 #endif /* p7BG_TESTDRIVE */
469
470
471
472