Debian Codegraph @

hmmer/src/p7_prior.c

raw symbols SCIP
1 /* Mixture Dirichlet priors for profile HMMs.
2 *
3 * SRE, Sat Mar 24 09:12:44 2007 [Janelia]
4 */
5 #include <p7_config.h>
6
7 #include "easel.h"
8 #include "esl_alphabet.h"
9 #include "esl_vectorops.h"
10
11 #include "hmmer.h"
12
13
14 /* Function: p7_prior_CreateAmino()
15 * Incept: SRE, Sat Mar 24 09:35:36 2007 [Janelia]
16 *
17 * Purpose: Creates the default mixture Dirichlet prior for protein
18 * sequences.
19 *
20 * The transition priors (match, insert, delete) are all
21 * single Dirichlets, originally trained by Graeme
22 * Mitchison in the mid-1990's. Notes have been lost, but
23 * we believe they were trained on an early version of
24 * Pfam.
25 *
26 * The match emission prior is a nine-component mixture
27 * from Kimmen Sjolander, who trained it on the Blocks9
28 * database \citep{Sjolander96}.
29 *
30 * The insert emission prior is a single Dirichlet with
31 * high $|\alpha|$, such that insert emission probabilities
32 * are essentially fixed by the prior, regardless of
33 * observed count data. The slightly polar parameterization
34 * was obtained by training on Pfam 1.0.
35 *
36 * Returns: a pointer to the new <P7_PRIOR> structure.
37 */
38 P7_PRIOR *
39 p7_prior_CreateAmino(void)
40 {
41 P7_PRIOR *pri = NULL;
42 int k;
43 int status;
44 /* default match mixture coefficients: [Sjolander96] */
45 static double defmq[9] = { 0.178091, 0.056591, 0.0960191, 0.0781233, 0.0834977, 0.0904123, 0.114468, 0.0682132, 0.234585 };
46
47 /* default match mixture Dirichlet components [Sjolander96] */
48 static double defm[9][20] = {
49 { 0.270671, 0.039848, 0.017576, 0.016415, 0.014268, 0.131916, 0.012391, 0.022599, 0.020358, 0.030727, 0.015315, 0.048298, 0.053803, 0.020662, 0.023612, 0.216147, 0.147226, 0.065438, 0.003758, 0.009621 },
50 { 0.021465, 0.010300, 0.011741, 0.010883, 0.385651, 0.016416, 0.076196, 0.035329, 0.013921, 0.093517, 0.022034, 0.028593, 0.013086, 0.023011, 0.018866, 0.029156, 0.018153, 0.036100, 0.071770, 0.419641 },
51 { 0.561459, 0.045448, 0.438366, 0.764167, 0.087364, 0.259114, 0.214940, 0.145928, 0.762204, 0.247320, 0.118662, 0.441564, 0.174822, 0.530840, 0.465529, 0.583402, 0.445586, 0.227050, 0.029510, 0.121090 },
52 { 0.070143, 0.011140, 0.019479, 0.094657, 0.013162, 0.048038, 0.077000, 0.032939, 0.576639, 0.072293, 0.028240, 0.080372, 0.037661, 0.185037, 0.506783, 0.073732, 0.071587, 0.042532, 0.011254, 0.028723 },
53 { 0.041103, 0.014794, 0.005610, 0.010216, 0.153602, 0.007797, 0.007175, 0.299635, 0.010849, 0.999446, 0.210189, 0.006127, 0.013021, 0.019798, 0.014509, 0.012049, 0.035799, 0.180085, 0.012744, 0.026466 },
54 { 0.115607, 0.037381, 0.012414, 0.018179, 0.051778, 0.017255, 0.004911, 0.796882, 0.017074, 0.285858, 0.075811, 0.014548, 0.015092, 0.011382, 0.012696, 0.027535, 0.088333, 0.944340, 0.004373, 0.016741 },
55 { 0.093461, 0.004737, 0.387252, 0.347841, 0.010822, 0.105877, 0.049776, 0.014963, 0.094276, 0.027761, 0.010040, 0.187869, 0.050018, 0.110039, 0.038668, 0.119471, 0.065802, 0.025430, 0.003215, 0.018742 },
56 { 0.452171, 0.114613, 0.062460, 0.115702, 0.284246, 0.140204, 0.100358, 0.550230, 0.143995, 0.700649, 0.276580, 0.118569, 0.097470, 0.126673, 0.143634, 0.278983, 0.358482, 0.661750, 0.061533, 0.199373 },
57 { 0.005193, 0.004039, 0.006722, 0.006121, 0.003468, 0.016931, 0.003647, 0.002184, 0.005019, 0.005990, 0.001473, 0.004158, 0.009055, 0.003630, 0.006583, 0.003172, 0.003690, 0.002967, 0.002772, 0.002686 },
58 };
59
60 ESL_ALLOC(pri, sizeof(P7_PRIOR));
61 pri->tm = pri->ti = pri->td = pri->em = pri->ei = NULL;
62
63 pri->tm = esl_mixdchlet_Create(1, 3); /* single component; 3 params */
64 pri->ti = esl_mixdchlet_Create(1, 2); /* single component; 2 params */
65 pri->td = esl_mixdchlet_Create(1, 2); /* single component; 2 params */
66 pri->em = esl_mixdchlet_Create(9, 20); /* 9 component; 20 params */
67 pri->ei = esl_mixdchlet_Create(1, 20); /* single component; 20 params */
68
69 if (pri->tm == NULL || pri->ti == NULL || pri->td == NULL || pri->em == NULL || pri->ei == NULL) goto ERROR;
70
71 /* Transition priors: originally from Graeme Mitchison. Notes are lost, but we believe
72 * they were trained on an early version of Pfam.
73 */
74 pri->tm->q[0] = 1.0;
75 pri->tm->alpha[0][0] = 0.7939; /* TMM */
76 pri->tm->alpha[0][1] = 0.0278; /* TMI */ /* Markus suggests ~10x MD, ~0.036; test! */
77 pri->tm->alpha[0][2] = 0.0135; /* TMD */ /* Markus suggests 0.1x MI, ~0.004; test! */
78
79 pri->ti->q[0] = 1.0;
80 pri->ti->alpha[0][0] = 0.1551; /* TIM */
81 pri->ti->alpha[0][1] = 0.1331; /* TII */
82
83 pri->td->q[0] = 1.0;
84 pri->td->alpha[0][0] = 0.9002; /* TDM */
85 pri->td->alpha[0][1] = 0.5630; /* TDD */
86
87 /* Match emission priors are from Kimmen Sjolander, trained
88 * on the Blocks9 database. [Sjolander96]
89 */
90 for (k = 0; k < 9; k++)
91 {
92 pri->em->q[k] = defmq[k];
93 esl_vec_DCopy(defm[k], 20, pri->em->alpha[k]);
94 }
95
96 /* Insert emission priors were trained on Pfam 1.0, 10 Nov 1996;
97 * see ~/projects/plan7/InsertStatistics.
98 * Inserts are slightly biased towards polar residues and away from
99 * hydrophobic residues.
100 */
101 pri->ei->q[0] = 1.0;
102 pri->ei->alpha[0][0] = 681.; /* A */
103 pri->ei->alpha[0][1] = 120.; /* C */
104 pri->ei->alpha[0][2] = 623.; /* D */
105 pri->ei->alpha[0][3] = 651.; /* E */
106 pri->ei->alpha[0][4] = 313.; /* F */
107 pri->ei->alpha[0][5] = 902.; /* G */
108 pri->ei->alpha[0][6] = 241.; /* H */
109 pri->ei->alpha[0][7] = 371.; /* I */
110 pri->ei->alpha[0][8] = 687.; /* K */
111 pri->ei->alpha[0][9] = 676.; /* L */
112 pri->ei->alpha[0][10] = 143.; /* M */
113 pri->ei->alpha[0][11] = 548.; /* N */
114 pri->ei->alpha[0][12] = 647.; /* P */
115 pri->ei->alpha[0][13] = 415.; /* Q */
116 pri->ei->alpha[0][14] = 551.; /* R */
117 pri->ei->alpha[0][15] = 926.; /* S */
118 pri->ei->alpha[0][16] = 623.; /* T */
119 pri->ei->alpha[0][17] = 505.; /* V */
120 pri->ei->alpha[0][18] = 102.; /* W */
121 pri->ei->alpha[0][19] = 269.; /* Y */
122
123 return pri;
124
125 ERROR:
126 if (pri != NULL) p7_prior_Destroy(pri);
127 return NULL;
128 }
129
130 /* Function: p7_prior_CreateNucleic()
131 *
132 * Purpose: Creates the default mixture Dirichlet prior for nucleotide
133 * sequences.
134 *
135 * The transition priors (match, insert, delete) are all
136 * single Dirichlets, trained on a portion of the rmark dataset
137 *
138 * The match emission prior is an eight-component mixture
139 * trained against a portion of the rmark dataset
140 *
141 * The insert emission prior is a single Dirichlet with
142 * high $|\alpha|$, such that insert emission probabilities
143 * are essentially fixed by the prior, regardless of
144 * observed count data.
145 *
146 * Returns: a pointer to the new <P7_PRIOR> structure.
147 */
148 P7_PRIOR *
149 p7_prior_CreateNucleic(void)
150 {
151 int status;
152 P7_PRIOR *pri = NULL;
153 int q;
154
155 /* Plus-1 Laplace prior
156 int num_comp = 1;
157 static double defmq[2] = { 1.0 };
158 static double defm[1][4] = {
159 { 1.0, 1.0, 1.0, 1.0} //
160 };
161 */
162
163 /* Match emission priors are trained on Rmark3 database
164 * Xref: ~wheelert/notebook/2011/0325_nhmmer_new_parameters
165 */
166 int num_comp = 4;
167 static double defmq[4] = { 0.24, 0.26, 0.08, 0.42 };
168 static double defm[4][4] = {
169 { 0.16, 0.45, 0.12, 0.39},
170 { 0.09, 0.03, 0.09, 0.04},
171 { 1.29, 0.40, 6.58, 0.51},
172 { 1.74, 1.49, 1.57, 1.95}
173 };
174
175
176
177 ESL_ALLOC(pri, sizeof(P7_PRIOR));
178 pri->tm = pri->ti = pri->td = pri->em = pri->ei = NULL;
179
180 pri->tm = esl_mixdchlet_Create(1, 3); // match transitions; single component; 3 params
181 pri->ti = esl_mixdchlet_Create(1, 2); // insert transitions; single component; 2 params
182 pri->td = esl_mixdchlet_Create(1, 2); // delete transitions; single component; 2 params
183 pri->em = esl_mixdchlet_Create(num_comp, 4); // match emissions; X component; 4 params
184 pri->ei = esl_mixdchlet_Create(1, 4); // insert emissions; single component; 4 params
185
186 if (pri->tm == NULL || pri->ti == NULL || pri->td == NULL || pri->em == NULL || pri->ei == NULL) goto ERROR;
187
188 /* Transition priors: roughly, learned from rmark benchmark - hand-beautified (trimming overspecified significant digits)
189 */
190 pri->tm->q[0] = 1.0;
191 pri->tm->alpha[0][0] = 2.0; // TMM
192 pri->tm->alpha[0][1] = 0.1; // TMI
193 pri->tm->alpha[0][2] = 0.1; // TMD
194
195 pri->ti->q[0] = 1.0;
196 pri->ti->alpha[0][0] = 0.12; // TIM - was 0.06 (TW changed 3/19/15)
197 pri->ti->alpha[0][1] = 0.4; // TII - was 0.2 (TW changed 3/19/15)
198
199 pri->td->q[0] = 1.0;
200 pri->td->alpha[0][0] = 0.5; // TDM - was 0.1 (TW changed 3/19/15)
201 pri->td->alpha[0][1] = 1.0; // TDD - was 0.2 (TW changed 3/19/15)
202
203
204 /* Match emission priors */
205 for (q = 0; q < num_comp; q++)
206 {
207 pri->em->q[q] = defmq[q];
208 esl_vec_DCopy(defm[q], 4, pri->em->alpha[q]);
209 }
210
211 /* Insert emission priors. Should alphas be lower? higher?
212 */
213 pri->ei->q[0] = 1.0;
214 esl_vec_DSet(pri->ei->alpha[0], 4, 1.0);
215
216 return pri;
217
218 ERROR:
219 if (pri != NULL) p7_prior_Destroy(pri);
220 return NULL;
221 }
222
223
224
225 /* Function: p7_prior_CreateLaplace()
226 * Synopsis: Creates Laplace plus-one prior.
227 * Incept: SRE, Sat Jun 30 09:48:13 2007 [Janelia]
228 *
229 * Purpose: Create a Laplace plus-one prior for alphabet <abc>.
230 */
231 P7_PRIOR *
232 p7_prior_CreateLaplace(const ESL_ALPHABET *abc)
233 {
234 P7_PRIOR *pri = NULL;
235 int status;
236
237 ESL_ALLOC(pri, sizeof(P7_PRIOR));
238 pri->tm = pri->ti = pri->td = pri->em = pri->ei = NULL;
239
240 pri->tm = esl_mixdchlet_Create(1, 3); /* single component; 3 params */
241 pri->ti = esl_mixdchlet_Create(1, 2); /* single component; 2 params */
242 pri->td = esl_mixdchlet_Create(1, 2); /* single component; 2 params */
243 pri->em = esl_mixdchlet_Create(1, abc->K); /* single component; K params */
244 pri->ei = esl_mixdchlet_Create(1, abc->K); /* single component; K params */
245
246 if (pri->tm == NULL || pri->ti == NULL || pri->td == NULL || pri->em == NULL || pri->ei == NULL) goto ERROR;
247
248 pri->tm->q[0] = 1.0; esl_vec_DSet(pri->tm->alpha[0], 3, 1.0); /* match transitions */
249 pri->ti->q[0] = 1.0; esl_vec_DSet(pri->ti->alpha[0], 2, 1.0); /* insert transitions */
250 pri->td->q[0] = 1.0; esl_vec_DSet(pri->td->alpha[0], 2, 1.0); /* delete transitions */
251 pri->em->q[0] = 1.0; esl_vec_DSet(pri->em->alpha[0], abc->K, 1.0); /* match emissions */
252 pri->ei->q[0] = 1.0; esl_vec_DSet(pri->ei->alpha[0], abc->K, 1.0); /* insert emissions */
253 return pri;
254
255 ERROR:
256 p7_prior_Destroy(pri);
257 return NULL;
258 }
259
260
261 /* Function: p7_prior_Destroy()
262 * Incept: SRE, Sat Mar 24 09:55:09 2007 [Janelia]
263 *
264 * Purpose: Frees a mixture Dirichlet prior.
265 */
266 void
267 p7_prior_Destroy(P7_PRIOR *pri)
268 {
269 if (pri == NULL) return;
270 if (pri->tm != NULL) esl_mixdchlet_Destroy(pri->tm);
271 if (pri->ti != NULL) esl_mixdchlet_Destroy(pri->ti);
272 if (pri->td != NULL) esl_mixdchlet_Destroy(pri->td);
273 if (pri->em != NULL) esl_mixdchlet_Destroy(pri->em);
274 if (pri->ei != NULL) esl_mixdchlet_Destroy(pri->ei);
275 free(pri);
276 }
277
278
279
280 /* Function: p7_ParameterEstimation()
281 * Incept: SRE, Sat Mar 24 10:15:37 2007 [Janelia]
282 *
283 * Purpose: Given an <hmm> containing weighted counts, and
284 * a mixture Dirichlet prior <pri>: calculate mean
285 * posterior parameter estimates for all model parameters,
286 * converting the HMM to a parameterized probabilistic
287 * model.
288 *
289 * If <pri> is <NULL>, then model parameters are calculated
290 * as frequencies, by normalization of <hmm>.
291 *
292 * Args: hmm - profile structure, containing counts.
293 * pri - mixture Dirichlet prior structure, or <NULL>.
294 *
295 * Returns: <eslOK> on success.
296 */
297 int
298 p7_ParameterEstimation(P7_HMM *hmm, const P7_PRIOR *pri)
299 {
300 int k;
301 double c[p7_MAXABET];
302 double p[p7_MAXABET];
303
304 // double mix[p7_MAXDCHLET];
305
306 /* Special case of pri=NULL: convert to frequencies.*/
307 if (pri==NULL) return p7_hmm_Renormalize(hmm);
308
309 /* Match transitions 0,1..M: 0 is the B state
310 * TMD at node M is 0.
311 */
312 for (k = 0; k <= hmm->M; k++) {
313 esl_vec_F2D(hmm->t[k], 3, c);
314 esl_mixdchlet_MPParameters(pri->tm, c, p);
315 esl_vec_D2F(p, 3, hmm->t[k]);
316 }
317 hmm->t[hmm->M][p7H_MD] = 0.0;
318 esl_vec_FNorm(hmm->t[hmm->M], 3);
319
320 /* Insert transitions, 0..M
321 */
322 for (k = 0; k <= hmm->M; k++) {
323 esl_vec_F2D(hmm->t[k]+3, 2, c);
324 esl_mixdchlet_MPParameters(pri->ti, c, p);
325 esl_vec_D2F(p, 2, hmm->t[k]+3);
326 }
327
328 /* Delete transitions, 1..M-1
329 * For k=0, which is unused; convention sets TMM=1.0, TMD=0.0
330 * For k=M, TMM = 1.0 (to the E state) and TMD=0.0 (no next D; must go to E).
331 */
332 for (k = 1; k < hmm->M; k++) {
333 esl_vec_F2D(hmm->t[k]+5, 2, c);
334 esl_mixdchlet_MPParameters(pri->td, c, p);
335 esl_vec_D2F(p, 2, hmm->t[k]+5);
336 }
337 hmm->t[0][p7H_DM] = hmm->t[hmm->M][p7H_DM] = 1.0;
338 hmm->t[0][p7H_DD] = hmm->t[hmm->M][p7H_DD] = 0.0;
339
340 /* Match emissions, 1..M
341 * Convention sets mat[0] to a valid pvector: first elem 1, the rest 0.
342 */
343 for (k = 1; k <= hmm->M; k++) {
344 esl_vec_F2D(hmm->mat[k], hmm->abc->K, c);
345 esl_mixdchlet_MPParameters(pri->em, c, p);
346 esl_vec_D2F(p, hmm->abc->K, hmm->mat[k]);
347 }
348 esl_vec_FSet(hmm->mat[0], hmm->abc->K, 0.);
349 hmm->mat[0][0] = 1.0;
350
351 /* Insert emissions 0..M
352 */
353 for (k = 0; k <= hmm->M; k++) {
354 esl_vec_F2D(hmm->ins[k], hmm->abc->K, c);
355 esl_mixdchlet_MPParameters(pri->ei, c, p);
356 esl_vec_D2F(p, hmm->abc->K, hmm->ins[k]);
357 }
358 return eslOK;
359 }
360
361