Debian Codegraph @

hmmer/src/cachedb_shard.c

raw symbols SCIP
1 /* Sequence and profile caches, used by the hmmpgmd daemon.
2 */
3 #include <p7_config.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9
10 #include "easel.h"
11 #include "esl_alphabet.h"
12 #include "esl_getopts.h"
13 #include "esl_sq.h"
14 #include "esl_sqio.h"
15 #include "esl_stopwatch.h"
16
17 #include "hmmer.h"
18 #include "cachedb.h"
19 #include "cachedb_shard.h"
20 #include "hmmpgmd.h"
21
22
23 /* sort routines */
24 static int
25 sort_seq(const void *p1, const void *p2)
26 {
27 int cmp;
28
29 cmp = (((HMMER_SEQ *)p1)->idx < ((HMMER_SEQ *)p2)->idx);
30 cmp -= (((HMMER_SEQ *)p1)->idx > ((HMMER_SEQ *)p2)->idx);
31
32 return cmp;
33 }
34
35
36 /* Modified version of p7_seqcache_Open that doesn't actually cache the sequences and their descriptions,
37 just records the index of each sequence, as that's all the master node really needs.
38 We could save a bit more memory by modifying some of the data structures to not include pointers to
39 fields that the master doesn't need, but just removing the residues and descriptions should reduce the memory
40 footprint enough to meet our goal of keeping EBI working until H4 ships */
41 int
42 p7_seqcache_Open_master(char *seqfile, P7_SEQCACHE **ret_cache, char *errbuf)
43 {
44 int i;
45 int inx;
46 int val;
47 int status;
48
49 uint64_t seq_cnt;
50 uint32_t db_cnt;
51 uint32_t db_inx[32];
52 uint32_t db_key;
53
54 //uint64_t res_cnt;
55 //uint64_t res_size;
56 uint64_t hdr_size;
57
58 char *hdr_ptr;
59 //ESL_DSQ *res_ptr;
60 char *desc_ptr;
61 char *ptr;
62 char buffer[512];
63 off_t offset;
64
65 uint64_t total_mem;
66
67 SEQ_DB *db = NULL;
68 P7_SEQCACHE *cache = NULL;
69
70 ESL_RANDOMNESS *rnd = NULL;
71 ESL_SQFILE *sqfp = NULL;
72 ESL_SQ *sq = NULL;
73 ESL_ALPHABET *abc = NULL;
74 ESL_SQASCII_DATA *ascii = NULL;
75
76 if (errbuf) errbuf[0] = '\0'; /* CURRENTLY UNUSED. FIXME */
77
78 /* Open the target sequence database */
79 if ((status = esl_sqfile_Open(seqfile, eslSQFILE_FASTA, NULL, &sqfp)) != eslOK) return status;
80
81 /* This is a bit of a hack. The first line contains database information.
82 *
83 * #<res_count> <seq_count> <db_count> <db_sequences_1> <db_sequences_before_removing_duplicates_1> <db_sequences_2> <db_sequences_before_removing_duplicates_2> ... <date_stamp>
84 *
85 * The rest of the file is a fasta format. The fasta header is just
86 * sequence number followed by a binary number indicating which
87 * database this sequence occurs in.
88 *
89 * The header line will be read in, parsed and saved. Then the
90 * parser will be repositioned after the line and used normally.
91 */
92 ascii = &sqfp->data.ascii;
93 fseek(ascii->fp, 0L, SEEK_SET);
94 if (fgets(buffer, sizeof(buffer), ascii->fp) == NULL) return eslEFORMAT;
95 if (buffer[0] != '#') return eslEFORMAT;
96
97 ptr = buffer + 1;
98 //res_cnt = strtoll(ptr, &ptr, 10);
99 strtoll(ptr, &ptr, 10); // SRE: res_cnt unused, but parsing <ptr> still needs to be advanced
100 seq_cnt = strtol(ptr, &ptr, 10);
101 db_cnt = strtol(ptr, &ptr, 10);
102
103 if (db_cnt > (sizeof(db_inx)/sizeof(db_inx[0]))) return eslEFORMAT;
104
105 total_mem = sizeof(P7_SEQCACHE);
106 ESL_ALLOC(cache, sizeof(P7_SEQCACHE));
107 memset(cache, 0, sizeof(P7_SEQCACHE));
108
109 if (esl_strdup(seqfile, -1, &cache->name) != eslOK) goto ERROR;
110
111 total_mem += (sizeof(HMMER_SEQ) * seq_cnt);
112 ESL_ALLOC(cache->list, sizeof(HMMER_SEQ) * seq_cnt);
113 memset(cache->list, 0, sizeof(HMMER_SEQ) * seq_cnt);
114
115 total_mem += (sizeof(SEQ_DB) * db_cnt);
116 ESL_ALLOC(db, sizeof(SEQ_DB) * db_cnt);
117 for (i = 0; i < db_cnt; ++i) {
118 db[i].count = strtol(ptr, &ptr, 10);
119 db[i].K = strtol(ptr, &ptr, 10);
120 total_mem += (sizeof(HMMER_SEQ *) * db[i].count);
121 ESL_ALLOC(db[i].list, sizeof(HMMER_SEQ *) * db[i].count);
122 memset(db[i].list, 0, sizeof(HMMER_SEQ *) * db[i].count);
123 }
124
125 /* grab the unique identifier */
126 while (*ptr && isspace(*ptr)) ++ptr;
127 i = strlen(ptr);
128 ESL_ALLOC(cache->id, i+1);
129 strcpy(cache->id, ptr);
130 while (--i > 0 && isspace(cache->id[i])) cache->id[i] = 0;
131
132 //res_size = res_cnt + seq_cnt + 1;
133 hdr_size = seq_cnt * 10;
134
135 //total_mem += res_size + hdr_size;
136 total_mem += hdr_size;
137 // ESL_ALLOC(cache->residue_mem, res_size);
138 ESL_ALLOC(cache->header_mem, hdr_size);
139
140 /* position the sequence file to the start of the first sequence.
141 * this will force any buffers associated with the file to be reset.
142 */
143 offset = ftell(ascii->fp);
144 if ((status = esl_sqfile_Position(sqfp, offset)) != eslOK) goto ERROR;
145
146 abc = esl_alphabet_Create(eslAMINO);
147 sq = esl_sq_CreateDigital(abc);
148
149 cache->db_cnt = db_cnt;
150 cache->db = db;
151 cache->abc = abc;
152 // cache->res_size = res_size;
153 cache->res_size = 0;
154 cache->hdr_size = hdr_size;
155 cache->count = seq_cnt;
156
157 hdr_ptr = cache->header_mem;
158 //res_ptr = cache->residue_mem;
159 for (i = 0; i < db_cnt; ++i) db_inx[i] = 0;
160
161 strcpy(buffer, "000000001");
162
163 inx = 0;
164 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
165
166 /* sanity checks */
167 if (inx >= seq_cnt) { printf("inx: %d\n", inx); return eslEFORMAT; }
168 // if (sq->n + 1 > res_size) { printf("inx: %d size %d %d\n", inx, (int)sq->n + 1, (int)res_size); return eslEFORMAT; }
169 // This check no longer needed since we aren't caching the sequences themselves
170
171 if (hdr_size <= 0) { printf("inx: %d hdr %d\n", inx, (int)hdr_size); return eslEFORMAT; }
172
173 /* generate the database key - modified to take the first word in the desc line.
174 * The remaining part of the desc is then cached as the description. */
175
176 ptr = sq->desc;;
177 desc_ptr = strchr(sq->desc, ' ');
178 if(desc_ptr != NULL) {
179 *desc_ptr= '\0';
180 ++desc_ptr;
181 } // need this to keep the db_key computation from running off the rails
182 val = 1;
183 db_key = 0;
184 while (*ptr) {
185 if (*ptr == '1') db_key += val;
186 val <<= 1;
187 ++ptr;
188 }
189
190
191 if (db_key >= (1 << (db_cnt + 1))) { printf("inx: %d db_cnt %d db %d %s\n", inx, db_cnt, db_key, sq->desc); return eslEFORMAT; }
192
193 cache->list[inx].name = hdr_ptr;
194 cache->list[inx].dsq = NULL;
195 //cache->list[inx].dsq = (ESL_DSQ *)res_ptr;
196 cache->list[inx].n = sq->n;
197 cache->list[inx].idx = inx;
198 cache->list[inx].db_key = db_key;
199 //if(desc_ptr != NULL) esl_strdup(desc_ptr, -1, &(cache->list[inx].desc));
200
201 /* copy the digitized sequence */
202 //memcpy(res_ptr, sq->dsq, sq->n + 1);
203 //res_ptr += (sq->n + 1);
204 //res_size -= (sq->n + 1);
205
206 /* copy the index to the header */
207 strcpy(hdr_ptr, buffer);
208 hdr_ptr += 10;
209 hdr_size -= 10;
210
211 /* increment the buffer string */
212 ++buffer[8];
213 for (i = 8; i > 0; --i) {
214 if (buffer[i] > '9') {
215 buffer[i] = '0';
216 buffer[i-1]++;
217 }
218 }
219
220 esl_sq_Reuse(sq);
221 ++inx;
222 }
223 if (status != eslEOF) { printf("Unexpected error %d at %d\n", status, inx); return status; }
224
225 if (inx != seq_cnt) { printf("inx:: %d %" PRIu64 "\n", inx, seq_cnt); return eslEFORMAT; }
226 if (hdr_size != 0) { printf("inx:: %d hdr %d\n", inx, (int)hdr_size); return eslEFORMAT; }
227 //if (res_size != 1) { printf("inx:: %d size %d %d\n", inx, (int)sq->n + 1, (int)res_size); return eslEFORMAT; }
228
229 /* copy the final sentinel character */
230 //*res_ptr++ = eslDSQ_SENTINEL;
231 //--res_size;
232
233 /* sort the order of the database sequences */
234 rnd = esl_randomness_CreateFast(seq_cnt);
235 for (i = 0 ; i < seq_cnt; ++i) {
236 rnd->x = rnd->x * 69069 + 1;
237 cache->list[i].idx = rnd->x;
238 }
239 esl_randomness_Destroy(rnd);
240 qsort(cache->list, seq_cnt, sizeof(HMMER_SEQ), sort_seq);
241
242 /* fill in the different databases and fix the index */
243 for (i = 0 ; i < seq_cnt; ++i) {
244 inx = 0;
245 db_key = cache->list[i].db_key;
246 while (db_key) {
247 if (db_key & 1) {
248 SEQ_DB *db = cache->db + inx;
249 if (db_inx[inx] >= db->count) { printf("sort:: %d %d\n", db_inx[inx], db->count); return eslEFORMAT; }
250 db->list[db_inx[inx]] = &cache->list[i];
251 ++db_inx[inx];
252 }
253 db_key >>= 1;
254 ++inx;
255 }
256 cache->list[i].idx = (cache->list[i].name - cache->header_mem) / 10 + 1;
257 }
258
259 for (i = 0; i < cache->db_cnt; ++i) {
260 printf("sequence database (%d):: %d %d\n", i, cache->db[i].count, db_inx[i]);
261 }
262
263 printf("\nLoaded sequence db file %s; total memory %" PRId64 "\n", seqfile, total_mem);
264
265 esl_sqfile_Close(sqfp);
266 esl_sq_Destroy(sq);
267
268 *ret_cache = cache;
269
270 return eslOK;
271
272 ERROR:
273 if (sq != NULL) esl_sq_Destroy(sq);
274 if (abc != NULL) esl_alphabet_Destroy(abc);
275 if (cache != NULL) {
276 if (cache->header_mem != NULL) free(cache->header_mem);
277 if (cache->residue_mem != NULL) free(cache->residue_mem);
278 if (cache->name != NULL) free(cache->name);
279 if (cache->id != NULL) free(cache->id);
280 free(cache);
281 }
282 for (i = 0; i < db_cnt; ++i) {
283 if (db[i].list != NULL) free(db[i].list);
284 }
285 return eslEMEM;
286 }
287
288 void
289 p7_seqcache_Close_master(P7_SEQCACHE *cache)
290 {
291 int i;
292
293 if (cache->name) free(cache->name);
294 if (cache->id) free(cache->id);
295 if (cache->db)
296 {
297 for (i = 0; i < cache->db_cnt; ++i) {
298 if (cache->db[i].list != NULL) free(cache->db[i].list);
299 }
300 free(cache->db);
301 }
302 if (cache->abc) esl_alphabet_Destroy(cache->abc);
303 if (cache->list) free(cache->list);
304 //if (cache->residue_mem) free(cache->residue_mem);
305 if (cache->header_mem) free(cache->header_mem);
306 free(cache);
307 }
308
309 int
310 p7_seqcache_Open_shard(char *seqfile, P7_SEQCACHE **ret_cache, char *errbuf, int my_shard, uint64_t num_shards)
311 {
312
313 //note: num_shards is declared here to be 64-bit because it's used in computations with other 64-bit values,
314 // not because we anticipate needing anywhere near that many shards
315 int i;
316 int inx;
317 int val;
318 int status;
319 uint32_t sequence_number; // Count the total number of sequences seen so that we get the correct sequence IDs
320 uint64_t seq_cnt;
321 uint32_t db_cnt;
322 uint32_t db_inx[32];
323 uint32_t db_key;
324
325 uint64_t res_cnt;
326 uint64_t res_size;
327 uint64_t hdr_size;
328
329 char *hdr_ptr;
330 ESL_DSQ *res_ptr;
331 char *desc_ptr;
332 char *ptr;
333 char buffer[512];
334 off_t offset;
335
336 uint64_t total_mem;
337 uint64_t res_mem_used, hdr_mem_used;
338
339 SEQ_DB *db = NULL;
340 P7_SEQCACHE *cache = NULL;
341
342 ESL_RANDOMNESS *rnd = NULL;
343 ESL_SQFILE *sqfp = NULL;
344 ESL_SQ *sq = NULL;
345 ESL_ALPHABET *abc = NULL;
346 ESL_SQASCII_DATA *ascii = NULL;
347 uint64_t *db_seq_count, *seq_in_db;
348 if (errbuf) errbuf[0] = '\0'; /* CURRENTLY UNUSED. FIXME */
349
350 /* Open the target sequence database */
351 if ((status = esl_sqfile_Open(seqfile, eslSQFILE_FASTA, NULL, &sqfp)) != eslOK) return status;
352
353 /* This is a bit of a hack. The first line contains database information.
354 *
355 * #<res_count> <seq_count> <db_count> <db_sequences_1> <db_sequences_before_removing_duplicates_1> <db_sequences_2> <db_sequences_before_removing_duplicates_2> ... <date_stamp>
356 *
357 * The rest of the file is a fasta format. The fasta header is just
358 * sequence number followed by a binary number indicating which
359 * database this sequence occurs in.
360 *
361 * The header line will be read in, parsed and saved. Then the
362 * parser will be repositioned after the line and used normally.
363 */
364 ascii = &sqfp->data.ascii;
365 fseek(ascii->fp, 0L, SEEK_SET);
366 if (fgets(buffer, sizeof(buffer), ascii->fp) == NULL) return eslEFORMAT;
367 if (buffer[0] != '#') return eslEFORMAT;
368
369 ptr = buffer + 1;
370 res_cnt = strtoll(ptr, &ptr, 10);
371 seq_cnt = strtol(ptr, &ptr, 10);
372 db_cnt = strtol(ptr, &ptr, 10);
373
374 if (db_cnt > (sizeof(db_inx)/sizeof(db_inx[0]))) return eslEFORMAT;
375
376 total_mem = sizeof(P7_SEQCACHE);
377 ESL_ALLOC(cache, sizeof(P7_SEQCACHE));
378 memset(cache, 0, sizeof(P7_SEQCACHE));
379
380 if (esl_strdup(seqfile, -1, &cache->name) != eslOK) goto ERROR;
381
382 total_mem += (sizeof(HMMER_SEQ) * seq_cnt);
383 ESL_ALLOC(cache->list, sizeof(HMMER_SEQ) * seq_cnt);
384 memset(cache->list, 0, sizeof(HMMER_SEQ) * seq_cnt);
385
386 total_mem += (sizeof(SEQ_DB) * db_cnt);
387 ESL_ALLOC(db, sizeof(SEQ_DB) * db_cnt);
388 for (i = 0; i < db_cnt; ++i) {
389 db[i].count = strtol(ptr, &ptr, 10); // this will get overwritten later
390 db[i].K = strtol(ptr, &ptr, 10);
391 total_mem += (sizeof(HMMER_SEQ *) * db[i].count);
392 ESL_ALLOC(db[i].list, sizeof(HMMER_SEQ *) * db[i].count);
393 memset(db[i].list, 0, sizeof(HMMER_SEQ *) * db[i].count);
394 }
395
396 ESL_ALLOC(db_seq_count, db_cnt *sizeof(uint64_t));
397 ESL_ALLOC(seq_in_db, db_cnt *sizeof(uint64_t));
398 for (i = 0; i < db_cnt; i++){
399 db_seq_count[i] = 0;
400 seq_in_db[i] = 0;
401 }
402 /* grab the unique identifier */
403 while (*ptr && isspace(*ptr)) ++ptr;
404 i = strlen(ptr);
405 ESL_ALLOC(cache->id, i+1);
406 strcpy(cache->id, ptr);
407 while (--i > 0 && isspace(cache->id[i])) cache->id[i] = 0;
408
409 res_size = (res_cnt + seq_cnt + 1)/num_shards; // starting guesses about how much memory we'll need
410 hdr_size = (seq_cnt * 10) / num_shards; // starting guesses about how much memory we'll need
411
412 // total_mem += res_size + hdr_size; (we'll update this later, once we know how much memory we're really using)
413 ESL_ALLOC(cache->residue_mem, res_size);
414 ESL_ALLOC(cache->header_mem, hdr_size);
415
416 /* position the sequence file to the start of the first sequence.
417 * this will force any buffers associated with the file to be reset.
418 */
419 offset = ftell(ascii->fp);
420 if ((status = esl_sqfile_Position(sqfp, offset)) != eslOK) goto ERROR;
421
422 abc = esl_alphabet_Create(eslAMINO);
423 sq = esl_sq_CreateDigital(abc);
424
425 cache->db_cnt = db_cnt;
426 cache->db = db;
427 cache->abc = abc;
428 cache->res_size = res_size;
429 cache->hdr_size = hdr_size;
430 cache->count = seq_cnt;
431
432 hdr_ptr = cache->header_mem;
433 res_ptr = cache->residue_mem;
434 for (i = 0; i < db_cnt; ++i) db_inx[i] = 0;
435
436 strcpy(buffer, "000000001");
437 //printf("Worker starting to read DB\n");
438 inx = 0;
439 sequence_number = 1;
440 res_mem_used = 0;
441 hdr_mem_used = 0;
442 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
443
444 /* sanity checks */
445 if (inx >= seq_cnt) { printf("inx: %d\n", inx); return eslEFORMAT; }
446 if (sq->n + 1 > res_size) { printf("inx: %d size %d %d\n", inx, (int)sq->n + 1, (int)res_size); return eslEFORMAT; }
447 if (hdr_size <= 0) { printf("inx: %d hdr %d\n", inx, (int)hdr_size); return eslEFORMAT; }
448
449 /* generate the database key - modified to take the first word in the desc line.
450 * The remaining part of the desc is then cached as the description. */
451
452 ptr = sq->desc;;
453 desc_ptr = strchr(sq->desc, ' ');
454 if(desc_ptr != NULL) {
455 *desc_ptr= '\0';
456 ++desc_ptr;
457 }
458 val = 1;
459 db_key = 0;
460 i = 0;
461 while (*ptr) {
462 if (*ptr == '1'){
463 if(db_seq_count[i] % num_shards == my_shard){ //this sequence is part of database i and belongs to this node's shard
464 db_key += val;
465 seq_in_db[i]++;
466 }
467 db_seq_count[i]++; // increase the number of sequences we've seen that belong to database i
468 }
469 val <<= 1;
470 i++;
471 ++ptr;
472 }
473
474
475 if (db_key >= (1 << (db_cnt + 1))) { printf("inx: %d db %d %s\n", inx, db_key, sq->desc); return eslEFORMAT; }
476
477 if(db_key != 0){ // This sequence appears in the appropriate shard of at least one database, so put it in the cache
478 cache->list[inx].name = hdr_ptr;
479 cache->list[inx].dsq = (ESL_DSQ *)res_ptr;
480 cache->list[inx].n = sq->n;
481 cache->list[inx].idx = sequence_number;
482 cache->list[inx].db_key = db_key;
483 if(desc_ptr != NULL) esl_strdup(desc_ptr, -1, &(cache->list[inx].desc));
484 ++inx;
485
486 /* copy the digitized sequence */
487 if(res_size < res_mem_used + sq->n +1){
488 // fprintf(stderr, "Reallocing residue_mem to size %llu\n", res_mem_used+ sq->n + 1);
489 ESL_REALLOC(cache->residue_mem, res_mem_used + (1024 * 1024)); // add a megabyte of space at a time to reduce
490 // the number of reallocations we do
491 res_ptr = cache->residue_mem + res_mem_used;
492 res_size = res_mem_used + (1024 * 1024);
493 }
494 // printf("Copying %llu bytes into residue array of size %llu with %llu bytes already used\n", (sq->n +1), res_size, res_mem_used);
495 memcpy(res_ptr, sq->dsq, sq->n + 1);
496 res_ptr += (sq->n + 1);
497 res_mem_used += (sq->n +1);
498
499 /* copy the index to the header */
500 if(hdr_size < hdr_mem_used + 10){
501 // fprintf(stderr, "Reallocing hdr_mem to size %llu\n", hdr_mem_used +10);
502 ESL_REALLOC(cache->header_mem, hdr_mem_used + (100 * 1024)); // add 100k at a time to reduces the number of reallocs
503 hdr_ptr = cache->header_mem + hdr_mem_used;
504 hdr_size = hdr_mem_used + (100 * 1024);
505 }
506 strcpy(hdr_ptr, buffer);
507 hdr_ptr += 10;
508 hdr_mem_used += 10;
509 }
510 /* increment the buffer string */
511 ++buffer[8];
512 for (i = 8; i > 0; --i) {
513 if (buffer[i] > '9') {
514 buffer[i] = '0';
515 buffer[i-1]++;
516 }
517 }
518
519 esl_sq_Reuse(sq);
520 sequence_number++;
521 /* if(strtol(buffer, NULL, 10) != (long)sequence_number){
522 printf("Miss-match between sequence name of %s and number of %d\n", buffer, sequence_number);
523 } */
524 }
525 //printf("Starting final reallocs\n");
526 ESL_REALLOC(cache->header_mem, hdr_mem_used); // trim any unused memory in this array
527 ESL_REALLOC(cache->residue_mem, res_mem_used+1); // ditto
528
529 //printf("Worker done reading DB\n");
530
531 // Need to patch all of the header and sequence pointers in the cache list because they may have gotten mangled by reallocs
532 hdr_ptr = cache->header_mem; // reset to base
533 res_ptr = cache->residue_mem; // ditto
534
535 for(i = 0; i < inx; i++){
536 cache->list[i].name = hdr_ptr;
537 hdr_ptr+= 10;
538 cache->list[i].dsq = (ESL_DSQ *) res_ptr;
539 res_ptr += cache->list[i].n + 1;
540 }
541
542 if (status != eslEOF) { printf("Unexpected error %d at %d\n", status, inx); return status; }
543
544 for(i = 0; i < db_cnt; i++){
545 // reset database sequence counts based on number of sequences in shard
546 db[i].count = seq_in_db[i];
547
548 }
549 //if (inx != seq_cnt) { printf("inx:: %d %d\n", inx, seq_cnt); return eslEFORMAT; }
550 //if (hdr_size != 0) { printf("inx:: %d hdr %d\n", inx, (int)hdr_size); return eslEFORMAT; }
551 //if (res_size != 1) { printf("inx:: %d size %d %d\n", inx, (int)sq->n + 1, (int)res_size); return eslEFORMAT; }
552
553 /* copy the final sentinel character */
554 *res_ptr++ = eslDSQ_SENTINEL;
555 --res_size;
556
557 seq_cnt = inx; //Record number of sequences actually loaded into memory with sharding
558 /* sort the order of the database sequences */
559 rnd = esl_randomness_CreateFast(seq_cnt);
560 for (i = 0 ; i < seq_cnt; ++i) {
561 rnd->x = rnd->x * 69069 + 1;
562 cache->list[i].idx = rnd->x;
563 }
564 esl_randomness_Destroy(rnd);
565 qsort(cache->list, seq_cnt, sizeof(HMMER_SEQ), sort_seq);
566
567 /* fill in the different databases and fix the index */
568 for (i = 0 ; i < seq_cnt; ++i) {
569 inx = 0;
570 db_key = cache->list[i].db_key;
571 while (db_key) {
572 if (db_key & 1) {
573 SEQ_DB *db = cache->db + inx;
574 if (db_inx[inx] >= db->count) { printf("sort:: %d %d\n", db_inx[inx], db->count); return eslEFORMAT; }
575 db->list[db_inx[inx]] = &cache->list[i];
576 ++db_inx[inx];
577 }
578 db_key >>= 1;
579 ++inx;
580 }
581 //cache->list[i].idx = (cache->list[i].name - cache->header_mem) / 10 + 1;
582 cache->list[i].idx = strtol(cache->list[i].name, NULL, 10);
583 }
584
585 for (i = 0; i < cache->db_cnt; ++i) {
586 printf("sequence database (%d):: %d %d\n", i, cache->db[i].count, db_inx[i]);
587 }
588 // re-update these with the computed values
589 cache->res_size = res_mem_used;
590 cache->hdr_size = hdr_mem_used;
591 //cache->count = inx;
592
593 total_mem += (res_mem_used + hdr_mem_used);
594 printf("\nLoaded sequence db file %s; total memory %" PRId64 "\n", seqfile, total_mem);
595
596 esl_sqfile_Close(sqfp);
597 esl_sq_Destroy(sq);
598
599
600 *ret_cache = cache;
601 free(db_seq_count);
602 free(seq_in_db);
603 return eslOK;
604
605 ERROR:
606 if (sq != NULL) esl_sq_Destroy(sq);
607 if (abc != NULL) esl_alphabet_Destroy(abc);
608 if (cache != NULL) {
609 if (cache->header_mem != NULL) free(cache->header_mem);
610 if (cache->residue_mem != NULL) free(cache->residue_mem);
611 if (cache->name != NULL) free(cache->name);
612 if (cache->id != NULL) free(cache->id);
613 free(cache);
614 }
615 for (i = 0; i < db_cnt; ++i) {
616 if (db[i].list != NULL) free(db[i].list);
617 }
618 return eslEMEM;
619 }
620 /*
621 void
622 p7_seqcache_Close(P7_SEQCACHE *cache)
623 {
624 int i;
625
626 if (cache->name) free(cache->name);
627 if (cache->id) free(cache->id);
628 if (cache->db)
629 {
630 for (i = 0; i < cache->db_cnt; ++i) {
631 if (cache->db[i].list != NULL) free(cache->db[i].list);
632 }
633 free(cache->db);
634 }
635 if (cache->abc) esl_alphabet_Destroy(cache->abc);
636 if (cache->list) free(cache->list);
637 if (cache->residue_mem) free(cache->residue_mem);
638 if (cache->header_mem) free(cache->header_mem);
639 free(cache);
640 }
641 */
642
643 /*****************************************************************
644 * x. Unit test
645 *****************************************************************/
646
647 #ifdef CACHEDB_UTEST1
648 /*
649 * gcc -O3 -malign-double -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
650 * gcc -g -O -pg -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
651 * gcc -g -Wall -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
652 *
653 * ./evalues-benchmark <hmmfile>
654 *
655 * -malign-double is needed for gcc if the rest of HMMER was compiled w/ -malign-double
656 * (i.e., our default gcc optimization)
657 *
658 * 27 Dec 08 on wanderoo: 24 msec per RRM_1 calibration; 37 msec for Caudal_act
659 * profiling shows 75% in Forward; 12% esl_random(); <3% in MSVFilter.
660 */
661 #include <stdio.h>
662 #include <stdlib.h>
663 #include <string.h>
664 #include <stdint.h>
665 #include <time.h>
666
667 #include "easel.h"
668 #include "esl_alphabet.h"
669 #include "esl_getopts.h"
670 #include "esl_sq.h"
671 #include "esl_sqio.h"
672 #include "esl_stopwatch.h"
673
674 #include "hmmer.h"
675 #include "cachedb.h"
676 #include "hmmpgmd.h"
677
678
679 static ESL_OPTIONS options[] = {
680 /* name type default env range toggles reqs incomp help docgroup*/
681 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
682 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "direct output to file <f>, not stdout", 2 },
683 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
684 };
685
686 typedef struct seq_info_s {
687 ESL_DSQ *dsq; /* digitized sequence [1..n] */
688 int64_t n; /* length of dsq */
689 int32_t inx; /* length of dsq */
690 uint64_t db_key; /* flag for included databases */
691 struct seq_info_s *next;
692 } SEQ_INFO;
693
694 #define HASH_KEY 52807
695 #define SUB_HASH 1531
696
697 int
698 main(int argc, char **argv)
699 {
700 FILE *ofp = stdout;
701
702 int i;
703 int cnt;
704 int inx;
705 int status;
706 int seq_cnt;
707 int db_inx;
708 int db_key;
709 int db_K[32];
710 int db_cnt[32];
711
712 uint64_t res_cnt;
713
714 char *seqfile;
715 char buffer[10];
716
717 ESL_GETOPTS *go = NULL;
718 ESL_ALPHABET *abc = NULL;
719 ESL_SQFILE *sqfp = NULL;
720 ESL_SQ *sq = NULL;
721
722 SEQ_INFO *info;
723 SEQ_INFO **hash;
724
725 time_t timep;
726
727 if ((go = esl_getopts_Create(options)) == NULL) {
728 printf("Failed to create options\n");
729 return 0;
730 }
731 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) {
732 printf("Failed to parse command line: %s\n", go->errbuf);
733 esl_getopts_Destroy(go);
734 return 0;
735 }
736 if (esl_opt_VerifyConfig(go) != eslOK) {
737 printf("Failed to parse command line: %s\n", go->errbuf);
738 esl_getopts_Destroy(go);
739 return 0;
740 }
741
742 if (esl_opt_ArgNumber(go) < 1) {
743 printf("Must specify at least one database\n");
744 esl_getopts_Destroy(go);
745 return 0;
746 }
747
748 if (esl_opt_IsOn(go, "-o")) {
749 if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) {
750 printf("Failed to open output database %s for writing\n", esl_opt_GetString(go, "-o"));
751 return 0;
752 }
753 }
754
755 abc = esl_alphabet_Create(eslAMINO);
756 sq = esl_sq_CreateDigital(abc);
757
758 ESL_ALLOC(hash, sizeof(char *) * HASH_KEY);
759 memset(hash, 0, sizeof(char *) * HASH_KEY);
760
761 memset(db_K, 0, sizeof(db_K));
762 memset(db_cnt, 0, sizeof(db_cnt));
763
764 res_cnt = 0;
765 seq_cnt = 0;
766
767 cnt = 0;
768
769 db_key = 1;
770 db_inx = 1;
771
772 while (db_inx <= esl_opt_ArgNumber(go)) {
773
774 /* the first db just build the list */
775 seqfile = esl_opt_GetArg(go, db_inx);
776 if ((status = esl_sqfile_Open(seqfile, eslSQFILE_FASTA, NULL, &sqfp)) != eslOK) {
777 printf("Unable to open sequence database %s\n", seqfile);
778 return status;
779 }
780 printf("Database %d: %s\n", db_inx, seqfile);
781
782 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
783 int sum = 0;
784
785 inx = 0;
786 for (i = 1; i <= sq->n; ++i) {
787 inx += (sq->dsq[i] * sq->dsq[i]);
788 sum = inx % SUB_HASH;
789 }
790 inx = inx % HASH_KEY;
791
792 info = hash[inx];
793 while (info != NULL) {
794 if (sq->n == info->n && sum == info->inx && memcmp(sq->dsq, info->dsq, sq->n+2) == 0) {
795 break;
796 }
797 info = info->next;
798 }
799
800 if (info == NULL) {
801 ESL_ALLOC(info, sizeof(SEQ_INFO));
802 ESL_ALLOC(info->dsq, sq->n+2);
803 memcpy(info->dsq, sq->dsq, sq->n+2);
804 info->n = sq->n;
805 info->db_key = db_key;
806 info->inx = sum;
807 ++db_cnt[db_inx-1];
808
809 info->next = hash[inx];
810 hash[inx] = info;
811
812 res_cnt += sq->n;
813 seq_cnt++;
814 } else if ((info->db_key & db_key) == 0) {
815 info->db_key |= db_key;
816 ++db_cnt[db_inx-1];
817 }
818
819 ++db_K[db_inx-1];
820
821 esl_sq_Reuse(sq);
822 }
823
824 esl_sqfile_Close(sqfp);
825
826 db_key <<= 1;
827 db_inx++;
828
829 printf("\n");
830 }
831
832 printf("Writing cache %s\n", esl_opt_GetString(go, "-o"));
833
834 timep = time(NULL);
835 fprintf(ofp, "# %" PRId64 " %d %d", res_cnt, seq_cnt, db_inx - 1);
836 for (i = 0; i < db_inx - 1; ++i) fprintf(ofp, " %d %d", db_cnt[i], db_K[i]);
837 fprintf(ofp, " %s", ctime(&timep));
838
839 strcpy(buffer, "000000001");
840
841 for (inx = 0; inx < HASH_KEY; ++inx) {
842 info = hash[inx];
843 while (info) {
844 int pos;
845 char buf[80];
846 SEQ_INFO *next = info->next;
847 fprintf(ofp, ">%s ", buffer);
848
849 while (info->db_key) {
850 fprintf(ofp, "%c", (info->db_key & 1) ? '1' : '0');
851 info->db_key >>= 1;
852 }
853 fprintf(ofp, "\n");
854
855 for (pos = 0; pos < info->n; pos += 60) {
856 esl_abc_TextizeN(abc, info->dsq+pos+1, 60, buf);
857 buf[60] = '\0';
858 fprintf(ofp, "%s\n", buf);
859 }
860
861 free(info->dsq);
862 free(info);
863
864 info = next;
865
866 /* increment the buffer string */
867 ++buffer[8];
868 for (i = 8; i > 0; --i) {
869 if (buffer[i] > '9') {
870 buffer[i] = '0';
871 buffer[i-1]++;
872 }
873 }
874 }
875 }
876
877 free(hash);
878
879 esl_alphabet_Destroy(abc);
880 esl_sq_Destroy(sq);
881
882 if (ofp != stdout) fclose(ofp);
883
884 if (esl_opt_IsOn(go, "-o")) {
885 P7_CACHEDB_SEQS *cache = NULL;
886 printf("Reading cache %s\n", esl_opt_GetString(go, "-o"));
887 if ((status = cache_SeqDb(esl_opt_GetString(go, "-o"), &cache)) != eslOK) {
888 printf("ERROR %d\n", status);
889 return 0;
890 }
891 cache_SeqDestroy(cache);
892 }
893
894 esl_getopts_Destroy(go);
895
896 return 0;
897
898 ERROR:
899 return status;
900 }
901
902 #endif /*CACHEDB_UTEST1*/
903
904
905 #ifdef CACHEDB_UTEST2
906 /*
907 * gcc -O3 -malign-double -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
908 * gcc -g -O -pg -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
909 * gcc -g -Wall -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
910 *
911 * ./evalues-benchmark <hmmfile>
912 *
913 * -malign-double is needed for gcc if the rest of HMMER was compiled w/ -malign-double
914 * (i.e., our default gcc optimization)
915 *
916 * 27 Dec 08 on wanderoo: 24 msec per RRM_1 calibration; 37 msec for Caudal_act
917 * profiling shows 75% in Forward; 12% esl_random(); <3% in MSVFilter.
918 */
919 #include <stdio.h>
920 #include <stdlib.h>
921 #include <string.h>
922 #include <stdint.h>
923 #include <time.h>
924
925 #include "easel.h"
926 #include "esl_alphabet.h"
927 #include "esl_getopts.h"
928 #include "esl_sq.h"
929 #include "esl_sqio.h"
930 #include "esl_stopwatch.h"
931
932 #include "hmmer.h"
933 #include "cachedb.h"
934 #include "hmmpgmd.h"
935
936
937 static ESL_OPTIONS options[] = {
938 /* name type default env range toggles reqs incomp help docgroup*/
939 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
940 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
941 };
942 static char usage[] = "[-options] db1 [db2 ...]";
943 static char banner[] = "unit test for cached databases";
944
945 typedef struct seq_info_s {
946 ESL_DSQ *dsq; /* digitized sequence [1..n] */
947 int64_t n; /* length of dsq */
948 uint64_t db_key; /* flag for included databases */
949 struct seq_info_s *next;
950 } SEQ_INFO;
951
952 int
953 main(int argc, char **argv)
954 {
955 int status;
956
957 ESL_GETOPTS *go = NULL;
958 P7_CACHEDB_SEQS *cache = NULL;
959
960
961 if ((go = esl_getopts_Create(options)) == NULL) {
962 printf("Failed to create options\n");
963 return 0;
964 }
965 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) {
966 printf("Failed to parse command line: %s\n", go->errbuf);
967 esl_getopts_Destroy(go);
968 return 0;
969 }
970 if (esl_opt_VerifyConfig(go) != eslOK) {
971 printf("Failed to parse command line: %s\n", go->errbuf);
972 esl_getopts_Destroy(go);
973 return 0;
974 }
975
976 if (esl_opt_ArgNumber(go) < 1) {
977 printf("Must specify at least one database\n");
978 esl_getopts_Destroy(go);
979 return 0;
980 }
981
982 printf("Reading cache %s\n", esl_opt_GetArg(go, 1));
983 if ((status = cache_SeqDb(esl_opt_GetArg(go, 1), &cache)) != eslOK) {
984 printf("ERROR %d\n", status);
985 return 0;
986 }
987 cache_SeqDestroy(cache);
988
989 esl_getopts_Destroy(go);
990
991 return 0;
992 }
993
994 #endif /*CACHEDB_UTEST2*/
995
996