Debian Codegraph @

hmmer/src/seqmodel.c

raw symbols SCIP
1 /* Creating profile HMMs from single sequences.
2 *
3 * Contents:
4 * 1. Routines in the exposed API.
5 * 2. Experiment driver: generating HMMs for hmmsim tests
6 * 3. Unit tests.
7 * 4. Test driver.
8 */
9 #include <p7_config.h>
10
11 #include "easel.h"
12 #include "esl_vectorops.h"
13
14 #include "hmmer.h"
15
16 /*****************************************************************
17 * 1. Routines in the exposed API.
18 *****************************************************************/
19
20
21 /* Function: p7_Seqmodel()
22 * Synopsis: Make a profile HMM from a single sequence.
23 *
24 * Purpose: Make a profile HMM from a single sequence, for
25 * probabilistic Smith/Waterman alignment, HMMER3-style.
26 *
27 * The query is digital sequence <dsq> of length <M>
28 * residues in alphabet <abc>, named <name>.
29 *
30 * The scoring system is given by <Q>, <f>, <popen>, and
31 * <pextend>. <Q> is a $K \times K$ matrix giving
32 * conditional residue probabilities $P(a \mid b)}$; these
33 * are typically obtained by reverse engineering a score
34 * matrix like BLOSUM62. <f> is a vector of $K$ background
35 * frequencies $p_a$. <popen> and <pextend> are the
36 * probabilities assigned to gap-open ($t_{MI}$ and
37 * $t_{MD}$) and gap-extend ($t_{II}$ and $t_{DD}$)
38 * transitions.
39 *
40 * Args:
41 *
42 * Returns: <eslOK> on success, and a newly allocated HMM is returned
43 * in <ret_hmm>.
44 *
45 * Throws: <eslEMEM> on allocation error, and <*ret_hmm> is <NULL>.
46 */
47 int
48 p7_Seqmodel(const ESL_ALPHABET *abc, ESL_DSQ *dsq, int M, char *name,
49 ESL_DMATRIX *Q, float *f, double popen, double pextend,
50 P7_HMM **ret_hmm)
51 {
52 int status;
53 P7_HMM *hmm = NULL;
54 char *logmsg = "[HMM created from a query sequence]";
55 int k;
56
57 if ((hmm = p7_hmm_Create(M, abc)) == NULL) { status = eslEMEM; goto ERROR; }
58
59 for (k = 0; k <= M; k++)
60 {
61 /* Use rows of P matrix as source of match emission vectors */
62 if (k > 0) esl_vec_D2F(Q->mx[(int) dsq[k]], abc->K, hmm->mat[k]);
63
64 /* Set inserts to background for now. This will be improved. */
65 esl_vec_FCopy(f, abc->K, hmm->ins[k]);
66
67 hmm->t[k][p7H_MM] = 1.0 - 2 * popen;
68 hmm->t[k][p7H_MI] = popen;
69 hmm->t[k][p7H_MD] = popen;
70 hmm->t[k][p7H_IM] = 1.0 - pextend;
71 hmm->t[k][p7H_II] = pextend;
72 hmm->t[k][p7H_DM] = 1.0 - pextend;
73 hmm->t[k][p7H_DD] = pextend;
74 }
75
76 /* Deal w/ special stuff at node M, overwriting a little of what we
77 * just did.
78 */
79 hmm->t[M][p7H_MM] = 1.0 - popen;
80 hmm->t[M][p7H_MD] = 0.;
81 hmm->t[M][p7H_DM] = 1.0;
82 hmm->t[M][p7H_DD] = 0.;
83
84 /* Add mandatory annotation
85 */
86 p7_hmm_SetName(hmm, name);
87 p7_hmm_AppendComlog(hmm, 1, &logmsg);
88 hmm->nseq = 1;
89 p7_hmm_SetCtime(hmm);
90 hmm->checksum = 0;
91
92 *ret_hmm = hmm;
93 return eslOK;
94
95 ERROR:
96 if (hmm != NULL) p7_hmm_Destroy(hmm);
97 *ret_hmm = NULL;
98 return status;
99 }
100
101
102 /*****************************************************************
103 * 2. Experiment driver
104 *****************************************************************/
105
106 #ifdef p7EXP_J2_1
107 /* Asking if single sequence queries (probabilistic Smith/Waterman)
108 * still follow expected score distributions. This program creates
109 * HMMs from one or more random sequences, and the HMMs can then
110 * be tested in hmmsim.
111 *
112 * gcc -o seq2hmm -g -Wall -Dp7EXP_J2_1 -L../easel -I ../easel -L. -I. seqmodel.c -lhmmer -leasel -lm
113 * ./seq2hmm <hmmfile> <seqfile>
114 */
115 #include <p7_config.h>
116
117 #include "easel.h"
118 #include "esl_getopts.h"
119 #include "esl_random.h"
120 #include "esl_alphabet.h"
121 #include "esl_sq.h"
122 #include "esl_sqio.h"
123 #include "esl_dmatrix.h"
124 #include "esl_scorematrix.h"
125
126 #include "hmmer.h"
127
128 static ESL_OPTIONS options[] = {
129 /* name type default env range toggles reqs incomp help docgroup*/
130 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
131 { "-m", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, NULL, "use substitution score matrix file from <f>", 0 },
132 { "-q", eslARG_REAL, "0.1", NULL, "0<=x<0.5",NULL, NULL, NULL, "gap open probability", 0 },
133 { "-r", eslARG_REAL, "0.4", NULL, "0<=x<1", NULL, NULL, NULL, "gap extend probability", 0 },
134 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
135 };
136
137 static char usage[] = "[-options] <query FASTA file> <target FASTA file>";
138 static char banner[] = "collect histograms of probabilistic S/W for E-value calculations";
139
140 int
141 main(int argc, char **argv)
142 {
143 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
144 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
145 char *hmmfile = esl_opt_GetArg(go, 1);
146 char *qfile = esl_opt_GetArg(go, 2);
147 ESL_SQ *qsq = esl_sq_CreateDigital(abc);
148 ESL_SQFILE *qfp = NULL;
149 FILE *hmmfp = NULL;
150 ESL_SCOREMATRIX *S = esl_scorematrix_Create(abc);
151 ESL_DMATRIX *Q = NULL;
152 P7_BG *bg = p7_bg_Create(abc);
153 P7_HMM *hmm = NULL;
154 double *fa = NULL;
155 double popen = esl_opt_GetReal (go, "-q");
156 double pextend = esl_opt_GetReal (go, "-r");
157 char *mxfile = esl_opt_GetString(go, "-m");
158 char errbuf[eslERRBUFSIZE];
159 double slambda;
160 int a,b;
161 int status;
162
163 /* Reverse engineer a scoring matrix to obtain conditional prob's
164 * that we'll use for the single-seq query HMM. Because score mx is
165 * symmetric, we can set up P[a][b] = P(b | a), so we can use the
166 * matrix rows as HMM match emission vectors. This means dividing
167 * the joint probs through by f_a.
168 */
169 if (mxfile == NULL) {
170 if (esl_scorematrix_Set("BLOSUM62", S) != eslOK) esl_fatal("failed to set BLOSUM62 scores");
171 } else {
172 ESL_FILEPARSER *efp = NULL;
173
174 if ( esl_fileparser_Open(mxfile, NULL, &efp) != eslOK) esl_fatal("failed to open score file %s", mxfile);
175 if ( esl_scorematrix_Read(efp, abc, &S) != eslOK) esl_fatal("failed to read matrix from %s", mxfile);
176 esl_fileparser_Close(efp);
177 }
178
179 /* A wasteful conversion of the HMMER single-precision background probs to Easel double-prec */
180 ESL_ALLOC(fa, sizeof(double) * bg->abc->K);
181 esl_vec_F2D(bg->f, bg->abc->K, fa);
182
183 /* Backcalculate joint probabilities Q, given score matrix S and background frequencies fa */
184 status = esl_scorematrix_ProbifyGivenBG(S, fa, fa, &slambda, &Q);
185 if (status == eslEINVAL) esl_fatal("built-in score matrix %s has no valid solution for lambda", matrix);
186 else if (status == eslENOHALT) esl_fatal("failed to solve score matrix %s for lambda", matrix);
187 else if (status != eslOK) esl_fatal("unexpected error in solving score matrix %s for probability parameters", matrix);
188
189 esl_scorematrix_JointToConditionalOnQuery(abc, Q);
190
191 /* Open the query sequence file in FASTA format */
192 status = esl_sqfile_Open(qfile, eslSQFILE_FASTA, NULL, &qfp);
193 if (status == eslENOTFOUND) esl_fatal("No such file %s.", qfile);
194 else if (status == eslEFORMAT) esl_fatal("Format of %s unrecognized.", qfile);
195 else if (status == eslEINVAL) esl_fatal("Can't autodetect stdin or .gz.");
196 else if (status != eslOK) esl_fatal("Open of %s failed, code %d.", qfile, status);
197
198 /* Open the output HMM file */
199 if ((hmmfp = fopen(hmmfile, "w")) == NULL) esl_fatal("Failed to open output HMM file %s", hmmfile);
200
201 /* For each sequence, build a model and save it.
202 */
203 while ((status = esl_sqio_Read(qfp, qsq)) == eslOK)
204 {
205 p7_Seqmodel(abc, qsq->dsq, qsq->n, qsq->name, Q, bg->f, popen, pextend, &hmm);
206 if ( p7_hmm_Validate(hmm, errbuf, 1e-5) != eslOK) esl_fatal("HMM validation failed: %s\n", errbuf);
207 if ( p7_hmmfile_WriteASCII(hmmfp, -1, hmm) != eslOK) esl_fatal("HMM save failed");
208
209 p7_hmm_Destroy(hmm);
210 }
211 if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s line %" PRId64 "):\n%s\n",
212 qfp->filename, qfp->linenumber, qfp->errbuf);
213 else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s",
214 status, qfp->filename);
215
216 esl_dmatrix_Destroy(Q);
217 esl_scorematrix_Destroy(S);
218 free(fa);
219 free(fb);
220 esl_sq_Destroy(qsq);
221 esl_sqfile_Close(qfp);
222 fclose(hmmfp);
223 esl_alphabet_Destroy(abc);
224 esl_getopts_Destroy(go);
225 return 0;
226 }
227 #endif /*p7EXP_J2_1*/
228
229
230 /*****************************************************************
231 * x. Unit tests.
232 *****************************************************************/
233 #ifdef p7SEQMODEL_TESTDRIVE
234 #include <string.h>
235
236 static void
237 utest_normalization(ESL_GETOPTS *go)
238 {
239 char *msg = "seqmodel normalization utest failed";
240 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
241 char *seq = "ACDEFGHIKLMNPQRSTVWYBJZOUX";
242 int L = strlen(seq);
243 ESL_DSQ *dsq = NULL;
244 float popen = 0.1;
245 float pextend = 0.4;
246 P7_BUILDER *bld = NULL;
247 P7_BG *bg = p7_bg_Create(abc);
248 P7_HMM *hmm = NULL;
249 char errbuf[eslERRBUFSIZE];
250
251 if ( esl_abc_CreateDsq(abc, seq, &dsq) != eslOK) esl_fatal(msg);
252 if ( (bld = p7_builder_Create(NULL, abc)) == NULL) esl_fatal(msg);
253 if ( p7_builder_LoadScoreSystem(bld, "BLOSUM62", popen, pextend, bg) != eslOK) esl_fatal(msg);
254 if ( p7_Seqmodel(abc, dsq, L, "aatest", bld->Q, bg->f, bld->popen, bld->pextend, &hmm) != eslOK) esl_fatal(msg);
255
256 if (p7_hmm_Validate(hmm, errbuf, 0.0001) != eslOK) esl_fatal("normalization utest failed\n%s\n", errbuf);
257
258 free(dsq);
259 p7_bg_Destroy(bg);
260 p7_hmm_Destroy(hmm);
261 p7_builder_Destroy(bld);
262 esl_alphabet_Destroy(abc);
263 }
264
265 #endif /*p7SEQMODEL_TESTDRIVE*/
266 /*---------------- end, unit tests ------------------------------*/
267
268 /*****************************************************************
269 * x. Test driver
270 *****************************************************************/
271 #ifdef p7SEQMODEL_TESTDRIVE
272
273 #include <p7_config.h>
274 #include "easel.h"
275 #include "hmmer.h"
276
277 static ESL_OPTIONS options[] = {
278 /* name type default env range toggles reqs incomp help docgroup*/
279 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
280 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
281 };
282 static char usage[] = "[-options]";
283 static char banner[] = "unit test driver for seqmodel.c: single sequence query construction";
284
285 int
286 main(int argc, char **argv)
287 {
288 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
289
290 utest_normalization(go);
291
292 esl_getopts_Destroy(go);
293 exit(0); /* success */
294 }
295
296 #endif /*p7SEQMODEL_TESTDRIVE*/
297 /*---------------- end, test driver -----------------------------*/
298
299
300
301
302