Debian Codegraph @

hmmer/src/hmmlogo.c

raw symbols SCIP
1 /* HMMLOGO, code to print values for building logos, or functions to inline
2 *
3 * Contents:
4 * 1. logo value functions
5 * 2. hmmlogo application
6 */
7 #include "hmmer.h"
8
9 /*****************************************************************
10 * 1. logo value functions
11 *****************************************************************/
12
13 float
14 hmmlogo_maxHeight (P7_BG *bg)
15 {
16 float min_p = 1;
17 int i;
18 for (i=0; i<bg->abc->K; i++)
19 min_p = ESL_MIN(min_p,bg->f[i]);
20
21 return eslCONST_LOG2R * log(1.0/min_p); //bits
22 }
23
24
25 /* assumes rel_ents is allocated with abc->K floats, and heights with hmm->M*abc->K floats*/
26 int
27 hmmlogo_RelativeEntropy_all (P7_HMM *hmm, P7_BG *bg, float *rel_ents, float **probs, float **heights ) {
28
29 int K = hmm->abc->K;
30 int M = hmm->M;
31 int i, j;
32
33 float logodds;
34
35 for (i = 1; i <= M; i++) {
36 // height of column, to be split among the residues
37 rel_ents[i] = 0;
38 for (j=0; j<K; j++) {
39 probs[i][j] = hmm->mat[i][j];
40
41 if ( probs[i][j] > 0 ) {
42 logodds = eslCONST_LOG2R * log(probs[i][j] / bg->f[j]); //bits
43 rel_ents[i] += probs[i][j] * logodds ;
44 }
45 }
46
47 // height of residues
48 for (j=0; j<K; j++) {
49 heights[i][j] = rel_ents[i] * probs[i][j];
50 }
51
52 }
53 return eslOK;
54 }
55
56
57 /* assumes rel_ents is allocated with abc->K floats, and heights with hmm->M*abc->K floats*/
58 int
59 hmmlogo_RelativeEntropy_above_bg (P7_HMM *hmm, P7_BG *bg, float *rel_ents, float **probs, float **heights ) {
60
61 int K = hmm->abc->K;
62 int M = hmm->M;
63 int i, j;
64
65 float logodds;
66 float abovebg_prob_sum;
67
68 for (i = 1; i <= M; i++) {
69
70 // height of column, to be split among the residues; also sum of of positive scores
71 rel_ents[i] = 0;
72 abovebg_prob_sum = 0.0;
73 for (j=0; j<K; j++) {
74 probs[i][j] = hmm->mat[i][j];
75
76 if ( probs[i][j] > 0 ) {
77 logodds = eslCONST_LOG2R * log(probs[i][j] / bg->f[j]); //bits
78 rel_ents[i] += probs[i][j] * logodds ;
79
80 if (logodds > 0)
81 abovebg_prob_sum += probs[i][j];
82 }
83 }
84
85 //height of residues
86 for (j=0; j<K; j++) {
87 heights[i][j] = 0.0;
88 if ( probs[i][j] > 0 ) {
89 logodds = eslCONST_LOG2R * log(probs[i][j] / bg->f[j]); //bits
90 heights[i][j] = logodds<=0 ? 0.0 : (rel_ents[i] * probs[i][j] / abovebg_prob_sum) ;
91 }
92 }
93 }
94 return eslOK;
95 }
96
97 /* assumes heights is allocated with hmm->M floats*/
98 int
99 hmmlogo_ScoreHeights (P7_HMM *hmm, P7_BG *bg, float **heights ) {
100
101 int K = hmm->abc->K;
102 int M = hmm->M;
103 int i, j;
104
105 float p;
106 float logodds;
107
108 for (i = 1; i <= M; i++) {
109
110 // height of column, to be split among the residues; also sum of of positive scores
111 for (j=0; j<K; j++) {
112 p = hmm->mat[i][j];
113 logodds = eslCONST_LOG2R * log(p / bg->f[j]); //bits
114 heights[i][j] = logodds;
115 }
116 }
117 return eslOK;
118 }
119
120
121 /* assumes heights is allocated with hmm->M floats*/
122 int
123 hmmlogo_IndelValues (P7_HMM *hmm, float *insert_P, float *insert_expL, float *occupancy ) {
124
125 int i;
126
127 if (insert_P != NULL) insert_P[1] = hmm->t[1][p7H_MI]; //probability of inserting after this match
128 if (insert_expL != NULL) insert_expL[1] = 1 / (1 - hmm->t[1][p7H_II]) ; //expected length of the insert, if it happens
129 // if (delete_P != NULL) delete_P[1] = 0.0; //1st match state never deleted
130
131 for (i = 2; i < hmm->M; i++) {
132 if (insert_P != NULL) insert_P[i] = hmm->t[i][p7H_MI]; //probability of inserting after this match
133 if (insert_expL != NULL) insert_expL[i] = 1 / (1 - hmm->t[i][p7H_II]) ; //expected length of the insert, if it happens
134 // if (delete_P != NULL) delete_P[i] = ( (1.0-delete_P[i-1]) * hmm->t[i-1][p7H_MD] ) + ( delete_P[i-1] * hmm->t[i-1][p7H_DD]) ; //probability of missing this state, either due to DD or MD from previous position
135 }
136
137 if (insert_P != NULL) insert_P[hmm->M] = 0.0; //no inserts after final position
138 if (insert_expL != NULL) insert_expL[hmm->M] = 0.0; //no inserts after final position
139 // if (delete_P != NULL) delete_P[hmm->M] = ( (1.0-delete_P[hmm->M-1]) * hmm->t[hmm->M-1][p7H_MD] ) + ( delete_P[hmm->M-1] * hmm->t[hmm->M-1][p7H_DD]) ; //probability of missing this state, either due to DD or MD from previous position
140
141 if (occupancy != NULL) p7_hmm_CalculateOccupancy(hmm, occupancy, NULL);
142
143 return eslOK;
144 }
145
146
147
148 /*---------------- end, logo value functions ----------------------*/
149
150
151 /*****************************************************************
152 * 2. hmmlogo application
153 *****************************************************************/
154
155 #define HMMLOGO_OPTS "--height_relent_all,--height_relent_abovebg,--height_score"
156 #define HMMLOGO_RELENT_ALL 1
157 #define HMMLOGO_RELENT_ABOVEBG 2
158 #define HMMLOGO_SCORE 3
159
160
161 static ESL_OPTIONS options[] = {
162 /* name type defaul env range toggles reqs incomp help docgroup*/
163 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
164 /* Control of output */
165 { "--height_relent_all", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, HMMLOGO_OPTS, "total height = relative entropy ; all letters shown (default)", 1 },
166 { "--height_relent_abovebg", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, HMMLOGO_OPTS, "total height = relative entropy ; only letters >bg shown", 1 },
167 { "--height_score", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, HMMLOGO_OPTS, "total height = sums of (pos|neg) scores; residue height = score", 1 },
168 { "--no_indel", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "don't provide indel rate values", 1 },
169 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
170 };
171
172
173 static char usage[] = "[options] <hmmfile>";
174 static char banner[] = "given an hmm, produce data required to build an hmm logo";
175
176 int
177 main(int argc, char **argv)
178 {
179 ESL_GETOPTS *go = NULL;
180 P7_HMMFILE *hfp = NULL; // open input HMM file
181 P7_HMM *hmm = NULL; // one HMM query
182 ESL_ALPHABET *abc = NULL; // digital alphabet
183 P7_BG *bg = NULL;
184 char *hmmfile = NULL;
185 float *rel_ents = NULL;
186 float **heights = NULL;
187 float **probs = NULL;
188 float *ins_P = NULL;
189 float *ins_expL = NULL;
190 float *occupancy = NULL;
191 int mode = HMMLOGO_RELENT_ALL; //default
192 int i, j;
193 int status = eslOK;
194 char errbuf[eslERRBUFSIZE];
195
196 go = esl_getopts_Create(options);
197 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) esl_fatal(argv[0], "Failed to parse command line: %s\n", go->errbuf);
198 if (esl_opt_VerifyConfig(go) != eslOK) esl_fatal(argv[0], "Error in configuration: %s\n", go->errbuf);
199
200 if (esl_opt_GetBoolean(go, "-h") ) {
201 p7_banner (stdout, argv[0], banner);
202 esl_usage (stdout, argv[0], usage);
203 puts("\nOptions:");
204 esl_opt_DisplayHelp(stdout, go, 1, 2, 100);
205 exit(0);
206 }
207
208 if (esl_opt_ArgNumber(go) != 1) esl_fatal(argv[0], "Incorrect number of command line arguments.\n");
209
210 hmmfile = esl_opt_GetArg(go, 1);
211
212 if (esl_opt_IsOn(go, "--height_relent_all")) mode = HMMLOGO_RELENT_ALL;
213 else if (esl_opt_IsOn(go, "--height_relent_abovebg")) mode = HMMLOGO_RELENT_ABOVEBG;
214 else if (esl_opt_IsOn(go, "--height_score")) mode = HMMLOGO_SCORE;
215 else mode = HMMLOGO_RELENT_ALL; //stil default
216
217 /* Open the query profile HMM file */
218 status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf);
219 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
220 else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
221 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf);
222
223 status = p7_hmmfile_Read(hfp, &abc, &hmm);
224 bg = p7_bg_Create(abc);
225
226 ESL_ALLOC(rel_ents, (hmm->M+1) * sizeof(float));
227 ESL_ALLOC(heights, (hmm->M+1) * sizeof(float*));
228 ESL_ALLOC(probs, (hmm->M+1) * sizeof(float*));
229 for (i = 1; i <= hmm->M; i++) {
230 ESL_ALLOC(heights[i], abc->K * sizeof(float));
231 ESL_ALLOC(probs[i], abc->K * sizeof(float));
232 }
233
234 /* residue heights */
235 if (mode == HMMLOGO_RELENT_ALL) {
236 printf ("max expected height = %.2f\n", hmmlogo_maxHeight(bg) );
237 hmmlogo_RelativeEntropy_all(hmm, bg, rel_ents, probs, heights);
238 } else if (mode == HMMLOGO_RELENT_ABOVEBG) {
239 printf ("max expected height = %.2f\n", hmmlogo_maxHeight(bg) );
240 hmmlogo_RelativeEntropy_above_bg(hmm, bg, rel_ents, probs, heights);
241 } else if (mode == HMMLOGO_SCORE) {
242 hmmlogo_ScoreHeights(hmm, bg, heights );
243 }
244
245 printf ("Residue heights\n");
246 for (i = 1; i <= hmm->M; i++) {
247 printf("%d: ", i);
248 for (j=0; j<abc->K; j++)
249 printf("%6.3f ", heights[i][j] );
250
251 if (mode != HMMLOGO_SCORE)
252 printf(" (%6.3f)", rel_ents[i]);
253
254 printf("\n");
255
256 }
257
258 /* indel values */
259 if (! esl_opt_IsOn(go, "--no_indel")) {
260 ESL_ALLOC(ins_P, (hmm->M+1) * sizeof(float));
261 ESL_ALLOC(ins_expL, (hmm->M+1) * sizeof(float));
262 ESL_ALLOC(occupancy, (hmm->M+1) * sizeof(float));
263
264 hmmlogo_IndelValues(hmm, ins_P, ins_expL, occupancy);
265
266 printf ("Indel values\n");
267 for (i = 1; i <= hmm->M; i++)
268 printf("%d: %6.3f %6.3f %6.3f\n", i, ins_P[i], ins_expL[i], occupancy[i] );
269
270 free(ins_P);
271 free(ins_expL);
272 free(occupancy);
273 }
274
275 free(rel_ents);
276 for (i = 1; i <= hmm->M; i++) {
277 free(heights[i]);
278 free(probs[i]);
279 }
280 free(heights);
281 free(probs);
282
283 p7_bg_Destroy(bg);
284 esl_alphabet_Destroy(abc);
285 p7_hmm_Destroy(hmm);
286 p7_hmmfile_Close(hfp);
287 esl_getopts_Destroy(go);
288 return eslOK;
289
290 ERROR:
291 return status;
292 }
293
294 /*---------------- end, hmmlogo application ----------------------*/
295
296
297