| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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; |
| 55 |
P7_OPROFILE *om = NULL; |
| 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; |
| 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; |
| 69 |
|
| 70 |
while ((status = p7_oprofile_ReadMSV(hfp, &(cache->abc), &om)) == eslOK) |
| 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 |
|
| 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 |
|
| 99 |
|
| 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; |
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
int |
| 132 |
p7_hmmcache_SetNumericNames(P7_HMMCACHE *cache) |
| 133 |
{ |
| 134 |
int namelen = 9; |
| 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 |
|
| 150 |
|
| 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 |
|
| 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 |
|
| 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 |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|