Debian Codegraph @

hmmer/src/p7_hmmcache.c

raw symbols SCIP
1 /* A cached profile database. Used by the hmmpgmd daemon.
2 *
3 * Contents:
4 * 1. P7_HMMCACHE : a daemon's cached profile database.
5 * 2. Benchmark driver.
6 * 3. Unit tests.
7 */
8 #include <p7_config.h>
9
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "easel.h"
14
15 #include "hmmer.h"
16 #include "p7_hmmcache.h"
17
18 /*****************************************************************
19 * 1. P7_HMMCACHE: a daemon's cached profile database
20 *****************************************************************/
21
22 /* Function: p7_hmmcache_Open()
23 * Synopsis: Cache a profile database.
24 *
25 * Purpose: Open <hmmfile> and read all of its contents, creating
26 * a cached profile database in memory. Return a ptr to the
27 * cached profile database in <*ret_cache>.
28 *
29 * Caller may optionally provide an <errbuf> ptr to
30 * at least <eslERRBUFSIZE> bytes, to capture an
31 * informative error message on failure.
32 *
33 * Args: hmmfile - (base) name of profile file to open
34 * ret_cache - RETURN: cached profile database
35 * errbuf - optRETURN: error message for a failure
36 *
37 * Returns: <eslOK> on success. <*ret_cache> points to the
38 * cached db. <errbuf> is unchanged.
39 *
40 * Failure codes:
41 * <eslENOTFOUND> : <hmmfile> couldn't be opened for reading
42 * <eslEFORMAT> : <hmmfile> isn't in recognized HMMER file format
43 * <eslEINCOMPAT> : profiles in <hmmfile> have different alphabets
44 *
45 * On any failure, <*ret_cache> is <NULL> and <errbuf> contains
46 * an informative error message for the user.
47 *
48 * Throws: <eslEMEM> : memory allocation error.
49 */
50 int
51 p7_hmmcache_Open(char *hmmfile, P7_HMMCACHE **ret_cache, char *errbuf)
52 {
53 P7_HMMCACHE *cache = NULL;
54 P7_HMMFILE *hfp = NULL; /* open HMM database file */
55 P7_OPROFILE *om = NULL; /* target profile */
56 int status;
57
58 ESL_ALLOC(cache, sizeof(P7_HMMCACHE));
59 cache->name = NULL;
60 cache->abc = NULL;
61 cache->list = NULL;
62 cache->lalloc = 4096; /* allocation chunk size for <list> of ptrs */
63 cache->n = 0;
64
65 if ( ( status = esl_strdup(hmmfile, -1, &cache->name) != eslOK)) goto ERROR;
66 ESL_ALLOC(cache->list, sizeof(P7_OPROFILE *) * cache->lalloc);
67
68 if ( (status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf)) != eslOK) goto ERROR; // eslENOTFOUND | eslEFORMAT
69
70 while ((status = p7_oprofile_ReadMSV(hfp, &(cache->abc), &om)) == eslOK) /* eslEFORMAT | eslEINCOMPAT */
71 {
72 if (( status = p7_oprofile_ReadRest(hfp, om)) != eslOK)
73 { strncpy(errbuf, hfp->rr_errbuf, eslERRBUFSIZE); goto ERROR; }
74
75 if (cache->n >= cache->lalloc) {
76 ESL_REALLOC(cache->list, sizeof(char *) * cache->lalloc * 2);
77 cache->lalloc *= 2;
78 }
79
80 cache->list[cache->n++] = om;
81 om = NULL;
82 }
83 if (status != eslEOF) { strncpy(errbuf, hfp->errbuf, eslERRBUFSIZE); goto ERROR; }
84
85 //printf("\nfinal:: %d memory %" PRId64 "\n", inx, total_mem);
86 p7_hmmfile_Close(hfp);
87 *ret_cache = cache;
88 return eslOK;
89
90 ERROR:
91 if (cache) p7_hmmcache_Close(cache);
92 if (om) p7_oprofile_Destroy(om);
93 if (hfp) p7_hmmfile_Close(hfp);
94 return status;
95 }
96
97
98 /* Function: p7_hmmcache_Sizeof()
99 * Synopsis: Returns total size of a profile cache, in bytes.
100 */
101 size_t
102 p7_hmmcache_Sizeof(P7_HMMCACHE *cache)
103 {
104 size_t n = sizeof(P7_HMMCACHE);
105 int i;
106
107 n += sizeof(char) * (strlen(cache->name) + 1);
108 n += esl_alphabet_Sizeof(cache->abc);
109 n += sizeof(P7_OPROFILE *) * cache->lalloc; /* cache->list */
110
111 for (i = 0; i < cache->n; i++)
112 n += p7_oprofile_Sizeof(cache->list[i]);
113
114 return n;
115 }
116
117
118 /* Function: p7_hmmcache_SetNumericNames()
119 * Synopsis: Rename each profile in cache with a numeric name.
120 *
121 * Purpose: Rename every profile in profile cache <cache>
122 * with a numeric code, starting from "000000001".
123 *
124 * The code is nine digits long, left padded with
125 * 0's.
126 *
127 * Returns: <eslOK> on success.
128 *
129 * Throws: <eslEMEM> on allocation failure.
130 */
131 int
132 p7_hmmcache_SetNumericNames(P7_HMMCACHE *cache)
133 {
134 int namelen = 9; /* 9 digit numeric code: 000000001, 000000002... */
135 P7_OPROFILE *om;
136 int i;
137 int status;
138
139 for (i = 0; i < cache->n; i++)
140 {
141 om = cache->list[i];
142 if (om->name) free(om->name);
143 if (( status = esl_sprintf(&(om->name), "%0*d", namelen, i+1)) != eslOK) return status;
144 }
145 return eslOK;
146 }
147
148
149 /* Function: p7_hmmcache_Close()
150 * Synopsis: Free a profile cache.
151 */
152 void
153 p7_hmmcache_Close(P7_HMMCACHE *cache)
154 {
155 int i;
156
157 if (! cache) return;
158 if (cache->name) free(cache->name);
159 if (cache->abc) esl_alphabet_Destroy(cache->abc);
160 if (cache->list)
161 {
162 for (i = 0; i < cache->n; i++)
163 p7_oprofile_Destroy(cache->list[i]);
164 free(cache->list);
165 }
166 free(cache);
167 }
168
169 /*****************************************************************
170 * 2. Benchmark driver
171 *****************************************************************/
172 #ifdef p7HMMCACHE_BENCHMARK
173
174 #include <p7_config.h>
175
176 #include "easel.h"
177 #include "esl_getopts.h"
178 #include "esl_stopwatch.h"
179
180 #include "hmmer.h"
181 #include "p7_hmmcache.h"
182
183 static ESL_OPTIONS options[] = {
184 /* name type default env range toggles reqs incomp help docgroup*/
185 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
186 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
187 };
188 static char usage[] = "[-options] <HMM file>";
189 static char banner[] = "benchmark driver for profile database cache";
190
191 int
192 main(int argc, char **argv)
193 {
194 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
195 ESL_STOPWATCH *w = esl_stopwatch_Create();
196 char *hmmfile = esl_opt_GetArg(go, 1);
197 P7_HMMCACHE *hcache = NULL;
198 char errbuf[eslERRBUFSIZE];
199 size_t tot_mem;
200 int status;
201
202 esl_stopwatch_Start(w);
203
204 status = p7_hmmcache_Open(hmmfile, &hcache, errbuf);
205 if (status == eslENOTFOUND) p7_Fail("Failed to read %s\n %s\n", hmmfile, errbuf);
206 else if (status == eslEFORMAT) p7_Fail("Failed to parse %s\n %s\n", hmmfile, errbuf);
207 else if (status == eslEINCOMPAT) p7_Fail("Mixed profile types in %s\n %s\n", hmmfile, errbuf);
208 else if (status != eslOK) p7_Fail("Failed to cache %s: error code %d\n", hmmfile, status);
209
210 p7_hmmcache_SetNumericNames(hcache);
211 tot_mem = p7_hmmcache_Sizeof(hcache);
212
213 esl_stopwatch_Stop(w);
214 esl_stopwatch_Display(stdout, w, "# CPU time: ");
215 printf("tot memory = %" PRIu64 "\n", (uint64_t) tot_mem);
216
217 p7_hmmcache_Close(hcache);
218 esl_getopts_Destroy(go);
219 esl_stopwatch_Destroy(w);
220 return 0;
221 }
222 #endif /*p7HMMCACHE_BENCHMARK*/
223 /*--------------- end, benchmark driver -------------------------*/
224
225
226