Debian Codegraph @

hmmer/src/cachedb.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 "hmmpgmd.h"
20
21
22 /* sort routines */
23 static int
24 sort_seq(const void *p1, const void *p2)
25 {
26 int cmp;
27
28 cmp = (((HMMER_SEQ *)p1)->idx < ((HMMER_SEQ *)p2)->idx);
29 cmp -= (((HMMER_SEQ *)p1)->idx > ((HMMER_SEQ *)p2)->idx);
30
31 return cmp;
32 }
33
34 int
35 p7_seqcache_Open(char *seqfile, P7_SEQCACHE **ret_cache, char *errbuf)
36 {
37 int i;
38 int inx;
39 int val;
40 int status;
41
42 uint64_t seq_cnt;
43 int32_t db_cnt;
44 int32_t db_inx[32];
45 uint32_t db_key;
46
47 uint64_t res_cnt;
48 uint64_t res_size;
49 uint64_t hdr_size;
50
51 char *hdr_ptr;
52 ESL_DSQ *res_ptr;
53 char *desc_ptr;
54 char *ptr;
55 char buffer[512];
56 off_t offset;
57
58 uint64_t total_mem;
59
60 SEQ_DB *db = NULL;
61 P7_SEQCACHE *cache = NULL;
62
63 ESL_RANDOMNESS *rnd = NULL;
64 ESL_SQFILE *sqfp = NULL;
65 ESL_SQ *sq = NULL;
66 ESL_ALPHABET *abc = NULL;
67 ESL_SQASCII_DATA *ascii = NULL;
68
69 if (errbuf) errbuf[0] = '\0'; /* CURRENTLY UNUSED. FIXME */
70
71 /* Open the target sequence database */
72 if ((status = esl_sqfile_Open(seqfile, eslSQFILE_FASTA, NULL, &sqfp)) != eslOK) return status;
73
74 /* This is a bit of a hack. The first line contains database information.
75 *
76 * #<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>
77 *
78 * The rest of the file is a fasta format. The fasta header is just
79 * sequence number followed by a binary number indicating which
80 * database this sequence occurs in.
81 *
82 * The header line will be read in, parsed and saved. Then the
83 * parser will be repositioned after the line and used normally.
84 */
85 ascii = &sqfp->data.ascii;
86 fseek(ascii->fp, 0L, SEEK_SET);
87 if (fgets(buffer, sizeof(buffer), ascii->fp) == NULL) return eslEFORMAT;
88 if (buffer[0] != '#') return eslEFORMAT;
89
90 ptr = buffer + 1;
91 res_cnt = strtoll(ptr, &ptr, 10);
92 seq_cnt = strtol(ptr, &ptr, 10);
93 db_cnt = strtol(ptr, &ptr, 10);
94
95 if (db_cnt > (sizeof(db_inx)/sizeof(db_inx[0]))) return eslEFORMAT;
96
97 total_mem = sizeof(P7_SEQCACHE);
98 ESL_ALLOC(cache, sizeof(P7_SEQCACHE));
99 memset(cache, 0, sizeof(P7_SEQCACHE));
100
101 if (esl_strdup(seqfile, -1, &cache->name) != eslOK) goto ERROR;
102
103 total_mem += (sizeof(HMMER_SEQ) * seq_cnt);
104 ESL_ALLOC(cache->list, sizeof(HMMER_SEQ) * seq_cnt);
105 memset(cache->list, 0, sizeof(HMMER_SEQ) * seq_cnt);
106
107 total_mem += (sizeof(SEQ_DB) * db_cnt);
108 ESL_ALLOC(db, sizeof(SEQ_DB) * db_cnt);
109 for (i = 0; i < db_cnt; ++i) {
110 db[i].count = strtol(ptr, &ptr, 10);
111 db[i].K = strtol(ptr, &ptr, 10);
112 total_mem += (sizeof(HMMER_SEQ *) * db[i].count);
113 ESL_ALLOC(db[i].list, sizeof(HMMER_SEQ *) * db[i].count);
114 memset(db[i].list, 0, sizeof(HMMER_SEQ *) * db[i].count);
115 }
116
117 /* grab the unique identifier */
118 while (*ptr && isspace(*ptr)) ++ptr;
119 i = strlen(ptr);
120 ESL_ALLOC(cache->id, i+1);
121 strcpy(cache->id, ptr);
122 while (--i > 0 && isspace(cache->id[i])) cache->id[i] = 0;
123
124 res_size = res_cnt + seq_cnt + 1;
125 hdr_size = seq_cnt * 10;
126
127 total_mem += res_size + hdr_size;
128 ESL_ALLOC(cache->residue_mem, res_size);
129 ESL_ALLOC(cache->header_mem, hdr_size);
130
131 /* position the sequence file to the start of the first sequence.
132 * this will force any buffers associated with the file to be reset.
133 */
134 offset = ftell(ascii->fp);
135 if ((status = esl_sqfile_Position(sqfp, offset)) != eslOK) goto ERROR;
136
137 abc = esl_alphabet_Create(eslAMINO);
138 sq = esl_sq_CreateDigital(abc);
139
140 cache->db_cnt = db_cnt;
141 cache->db = db;
142 cache->abc = abc;
143 cache->res_size = res_size;
144 cache->hdr_size = hdr_size;
145 cache->count = seq_cnt;
146
147 hdr_ptr = cache->header_mem;
148 res_ptr = cache->residue_mem;
149 for (i = 0; i < db_cnt; ++i) db_inx[i] = 0;
150
151 strcpy(buffer, "000000001");
152
153 inx = 0;
154 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
155
156 /* sanity checks */
157 if (inx >= seq_cnt) { printf("inx: %d\n", inx); return eslEFORMAT; }
158 if (sq->n + 1 > res_size) { printf("inx: %d size %d %d\n", inx, (int)sq->n + 1, (int)res_size); return eslEFORMAT; }
159 if (hdr_size <= 0) { printf("inx: %d hdr %d\n", inx, (int)hdr_size); return eslEFORMAT; }
160
161 /* generate the database key - modified to take the first word in the desc line.
162 * The remaining part of the desc is then cached as the description. */
163
164 ptr = sq->desc;;
165 desc_ptr = strchr(sq->desc, ' ');
166 if(desc_ptr != NULL) {
167 *desc_ptr= '\0';
168 ++desc_ptr;
169 }
170 val = 1;
171 db_key = 0;
172 while (*ptr) {
173 if (*ptr == '1') db_key += val;
174 val <<= 1;
175 ++ptr;
176 }
177
178
179 if (db_key >= (1 << (db_cnt + 1))) { printf("inx: %d db %d %s\n", inx, db_key, sq->desc); return eslEFORMAT; }
180
181 cache->list[inx].name = hdr_ptr;
182 cache->list[inx].dsq = (ESL_DSQ *)res_ptr;
183 cache->list[inx].n = sq->n;
184 cache->list[inx].idx = inx;
185 cache->list[inx].db_key = db_key;
186 if(desc_ptr != NULL) esl_strdup(desc_ptr, -1, &(cache->list[inx].desc));
187
188 /* copy the digitized sequence */
189 memcpy(res_ptr, sq->dsq, sq->n + 1);
190 res_ptr += (sq->n + 1);
191 res_size -= (sq->n + 1);
192
193 /* copy the index to the header */
194 strcpy(hdr_ptr, buffer);
195 hdr_ptr += 10;
196 hdr_size -= 10;
197
198 /* increment the buffer string */
199 ++buffer[8];
200 for (i = 8; i > 0; --i) {
201 if (buffer[i] > '9') {
202 buffer[i] = '0';
203 buffer[i-1]++;
204 }
205 }
206
207 esl_sq_Reuse(sq);
208 ++inx;
209 }
210 if (status != eslEOF) { printf("Unexpected error %d at %d\n", status, inx); return status; }
211
212 if (inx != seq_cnt) { printf("inx:: %d %" PRIu64 "\n", inx, seq_cnt); return eslEFORMAT; }
213 if (hdr_size != 0) { printf("inx:: %d hdr %d\n", inx, (int)hdr_size); return eslEFORMAT; }
214 if (res_size != 1) { printf("inx:: %d size %d %d\n", inx, (int)sq->n + 1, (int)res_size); return eslEFORMAT; }
215
216 /* copy the final sentinel character */
217 *res_ptr++ = eslDSQ_SENTINEL;
218 --res_size;
219
220 /* sort the order of the database sequences */
221 rnd = esl_randomness_CreateFast(seq_cnt);
222 for (i = 0 ; i < seq_cnt; ++i) {
223 rnd->x = rnd->x * 69069 + 1;
224 cache->list[i].idx = rnd->x;
225 }
226 esl_randomness_Destroy(rnd);
227 qsort(cache->list, seq_cnt, sizeof(HMMER_SEQ), sort_seq);
228
229 /* fill in the different databases and fix the index */
230 for (i = 0 ; i < seq_cnt; ++i) {
231 inx = 0;
232 db_key = cache->list[i].db_key;
233 while (db_key) {
234 if (db_key & 1) {
235 SEQ_DB *db = cache->db + inx;
236 if (db_inx[inx] >= db->count) { printf("sort:: %d %d\n", db_inx[inx], db->count); return eslEFORMAT; }
237 db->list[db_inx[inx]] = &cache->list[i];
238 ++db_inx[inx];
239 }
240 db_key >>= 1;
241 ++inx;
242 }
243 cache->list[i].idx = (cache->list[i].name - cache->header_mem) / 10 + 1;
244 }
245
246 for (i = 0; i < cache->db_cnt; ++i) {
247 printf("sequence database (%d):: %d %d\n", i, cache->db[i].count, db_inx[i]);
248 }
249
250 printf("\nLoaded sequence db file %s; total memory %" PRId64 "\n", seqfile, total_mem);
251
252 esl_sqfile_Close(sqfp);
253 esl_sq_Destroy(sq);
254
255 *ret_cache = cache;
256
257 return eslOK;
258
259 ERROR:
260 if (sq != NULL) esl_sq_Destroy(sq);
261 if (abc != NULL) esl_alphabet_Destroy(abc);
262 if (cache != NULL) {
263 if (cache->header_mem != NULL) free(cache->header_mem);
264 if (cache->residue_mem != NULL) free(cache->residue_mem);
265 if (cache->name != NULL) free(cache->name);
266 if (cache->id != NULL) free(cache->id);
267 free(cache);
268 }
269 for (i = 0; i < db_cnt; ++i) {
270 if (db[i].list != NULL) free(db[i].list);
271 }
272 return eslEMEM;
273 }
274
275 void
276 p7_seqcache_Close(P7_SEQCACHE *cache)
277 {
278 int i;
279
280 if (cache->name) free(cache->name);
281 if (cache->id) free(cache->id);
282 if (cache->db)
283 {
284 for (i = 0; i < cache->db_cnt; ++i) {
285 if (cache->db[i].list != NULL) free(cache->db[i].list);
286 }
287 free(cache->db);
288 }
289 if (cache->abc) esl_alphabet_Destroy(cache->abc);
290 if (cache->list) free(cache->list);
291 if (cache->residue_mem) free(cache->residue_mem);
292 if (cache->header_mem) free(cache->header_mem);
293 free(cache);
294 }
295
296
297
298
299 /*****************************************************************
300 * x. Unit test
301 *****************************************************************/
302
303 #ifdef CACHEDB_UTEST1
304 /*
305 * gcc -O3 -malign-double -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
306 * gcc -g -O -pg -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
307 * gcc -g -Wall -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
308 *
309 * ./evalues-benchmark <hmmfile>
310 *
311 * -malign-double is needed for gcc if the rest of HMMER was compiled w/ -malign-double
312 * (i.e., our default gcc optimization)
313 *
314 * 27 Dec 08 on wanderoo: 24 msec per RRM_1 calibration; 37 msec for Caudal_act
315 * profiling shows 75% in Forward; 12% esl_random(); <3% in MSVFilter.
316 */
317 #include <stdio.h>
318 #include <stdlib.h>
319 #include <string.h>
320 #include <stdint.h>
321 #include <time.h>
322
323 #include "easel.h"
324 #include "esl_alphabet.h"
325 #include "esl_getopts.h"
326 #include "esl_sq.h"
327 #include "esl_sqio.h"
328 #include "esl_stopwatch.h"
329
330 #include "hmmer.h"
331 #include "cachedb.h"
332 #include "hmmpgmd.h"
333
334
335 static ESL_OPTIONS options[] = {
336 /* name type default env range toggles reqs incomp help docgroup*/
337 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
338 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "direct output to file <f>, not stdout", 2 },
339 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
340 };
341
342 typedef struct seq_info_s {
343 ESL_DSQ *dsq; /* digitized sequence [1..n] */
344 int64_t n; /* length of dsq */
345 int32_t inx; /* length of dsq */
346 uint64_t db_key; /* flag for included databases */
347 struct seq_info_s *next;
348 } SEQ_INFO;
349
350 #define HASH_KEY 52807
351 #define SUB_HASH 1531
352
353 int
354 main(int argc, char **argv)
355 {
356 FILE *ofp = stdout;
357
358 int i;
359 int cnt;
360 int inx;
361 int status;
362 int seq_cnt;
363 int db_inx;
364 int db_key;
365 int db_K[32];
366 int db_cnt[32];
367
368 uint64_t res_cnt;
369
370 char *seqfile;
371 char buffer[10];
372
373 ESL_GETOPTS *go = NULL;
374 ESL_ALPHABET *abc = NULL;
375 ESL_SQFILE *sqfp = NULL;
376 ESL_SQ *sq = NULL;
377
378 SEQ_INFO *info;
379 SEQ_INFO **hash;
380
381 time_t timep;
382
383 if ((go = esl_getopts_Create(options)) == NULL) {
384 printf("Failed to create options\n");
385 return 0;
386 }
387 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) {
388 printf("Failed to parse command line: %s\n", go->errbuf);
389 esl_getopts_Destroy(go);
390 return 0;
391 }
392 if (esl_opt_VerifyConfig(go) != eslOK) {
393 printf("Failed to parse command line: %s\n", go->errbuf);
394 esl_getopts_Destroy(go);
395 return 0;
396 }
397
398 if (esl_opt_ArgNumber(go) < 1) {
399 printf("Must specify at least one database\n");
400 esl_getopts_Destroy(go);
401 return 0;
402 }
403
404 if (esl_opt_IsOn(go, "-o")) {
405 if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) {
406 printf("Failed to open output database %s for writing\n", esl_opt_GetString(go, "-o"));
407 return 0;
408 }
409 }
410
411 abc = esl_alphabet_Create(eslAMINO);
412 sq = esl_sq_CreateDigital(abc);
413
414 ESL_ALLOC(hash, sizeof(char *) * HASH_KEY);
415 memset(hash, 0, sizeof(char *) * HASH_KEY);
416
417 memset(db_K, 0, sizeof(db_K));
418 memset(db_cnt, 0, sizeof(db_cnt));
419
420 res_cnt = 0;
421 seq_cnt = 0;
422
423 cnt = 0;
424
425 db_key = 1;
426 db_inx = 1;
427
428 while (db_inx <= esl_opt_ArgNumber(go)) {
429
430 /* the first db just build the list */
431 seqfile = esl_opt_GetArg(go, db_inx);
432 if ((status = esl_sqfile_Open(seqfile, eslSQFILE_FASTA, NULL, &sqfp)) != eslOK) {
433 printf("Unable to open sequence database %s\n", seqfile);
434 return status;
435 }
436 printf("Database %d: %s\n", db_inx, seqfile);
437
438 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
439 int sum = 0;
440
441 inx = 0;
442 for (i = 1; i <= sq->n; ++i) {
443 inx += (sq->dsq[i] * sq->dsq[i]);
444 sum = inx % SUB_HASH;
445 }
446 inx = inx % HASH_KEY;
447
448 info = hash[inx];
449 while (info != NULL) {
450 if (sq->n == info->n && sum == info->inx && memcmp(sq->dsq, info->dsq, sq->n+2) == 0) {
451 break;
452 }
453 info = info->next;
454 }
455
456 if (info == NULL) {
457 ESL_ALLOC(info, sizeof(SEQ_INFO));
458 ESL_ALLOC(info->dsq, sq->n+2);
459 memcpy(info->dsq, sq->dsq, sq->n+2);
460 info->n = sq->n;
461 info->db_key = db_key;
462 info->inx = sum;
463 ++db_cnt[db_inx-1];
464
465 info->next = hash[inx];
466 hash[inx] = info;
467
468 res_cnt += sq->n;
469 seq_cnt++;
470 } else if ((info->db_key & db_key) == 0) {
471 info->db_key |= db_key;
472 ++db_cnt[db_inx-1];
473 }
474
475 ++db_K[db_inx-1];
476
477 esl_sq_Reuse(sq);
478 }
479
480 esl_sqfile_Close(sqfp);
481
482 db_key <<= 1;
483 db_inx++;
484
485 printf("\n");
486 }
487
488 printf("Writing cache %s\n", esl_opt_GetString(go, "-o"));
489
490 timep = time(NULL);
491 fprintf(ofp, "# %" PRId64 " %d %d", res_cnt, seq_cnt, db_inx - 1);
492 for (i = 0; i < db_inx - 1; ++i) fprintf(ofp, " %d %d", db_cnt[i], db_K[i]);
493 fprintf(ofp, " %s", ctime(&timep));
494
495 strcpy(buffer, "000000001");
496
497 for (inx = 0; inx < HASH_KEY; ++inx) {
498 info = hash[inx];
499 while (info) {
500 int pos;
501 char buf[80];
502 SEQ_INFO *next = info->next;
503 fprintf(ofp, ">%s ", buffer);
504
505 while (info->db_key) {
506 fprintf(ofp, "%c", (info->db_key & 1) ? '1' : '0');
507 info->db_key >>= 1;
508 }
509 fprintf(ofp, "\n");
510
511 for (pos = 0; pos < info->n; pos += 60) {
512 esl_abc_TextizeN(abc, info->dsq+pos+1, 60, buf);
513 buf[60] = '\0';
514 fprintf(ofp, "%s\n", buf);
515 }
516
517 free(info->dsq);
518 free(info);
519
520 info = next;
521
522 /* increment the buffer string */
523 ++buffer[8];
524 for (i = 8; i > 0; --i) {
525 if (buffer[i] > '9') {
526 buffer[i] = '0';
527 buffer[i-1]++;
528 }
529 }
530 }
531 }
532
533 free(hash);
534
535 esl_alphabet_Destroy(abc);
536 esl_sq_Destroy(sq);
537
538 if (ofp != stdout) fclose(ofp);
539
540 if (esl_opt_IsOn(go, "-o")) {
541 P7_CACHEDB_SEQS *cache = NULL;
542 printf("Reading cache %s\n", esl_opt_GetString(go, "-o"));
543 if ((status = cache_SeqDb(esl_opt_GetString(go, "-o"), &cache)) != eslOK) {
544 printf("ERROR %d\n", status);
545 return 0;
546 }
547 cache_SeqDestroy(cache);
548 }
549
550 esl_getopts_Destroy(go);
551
552 return 0;
553
554 ERROR:
555 return status;
556 }
557
558 #endif /*CACHEDB_UTEST1*/
559
560
561 #ifdef CACHEDB_UTEST2
562 /*
563 * gcc -O3 -malign-double -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
564 * gcc -g -O -pg -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
565 * gcc -g -Wall -msse2 -o evalues-benchmark -I. -L. -I../easel -L../easel -Dp7EVALUES_BENCHMARK evalues.c -lhmmer -leasel -lm
566 *
567 * ./evalues-benchmark <hmmfile>
568 *
569 * -malign-double is needed for gcc if the rest of HMMER was compiled w/ -malign-double
570 * (i.e., our default gcc optimization)
571 *
572 * 27 Dec 08 on wanderoo: 24 msec per RRM_1 calibration; 37 msec for Caudal_act
573 * profiling shows 75% in Forward; 12% esl_random(); <3% in MSVFilter.
574 */
575 #include <stdio.h>
576 #include <stdlib.h>
577 #include <string.h>
578 #include <stdint.h>
579 #include <time.h>
580
581 #include "easel.h"
582 #include "esl_alphabet.h"
583 #include "esl_getopts.h"
584 #include "esl_sq.h"
585 #include "esl_sqio.h"
586 #include "esl_stopwatch.h"
587
588 #include "hmmer.h"
589 #include "cachedb.h"
590 #include "hmmpgmd.h"
591
592
593 static ESL_OPTIONS options[] = {
594 /* name type default env range toggles reqs incomp help docgroup*/
595 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
596 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
597 };
598 static char usage[] = "[-options] db1 [db2 ...]";
599 static char banner[] = "unit test for cached databases";
600
601 typedef struct seq_info_s {
602 ESL_DSQ *dsq; /* digitized sequence [1..n] */
603 int64_t n; /* length of dsq */
604 uint64_t db_key; /* flag for included databases */
605 struct seq_info_s *next;
606 } SEQ_INFO;
607
608 int
609 main(int argc, char **argv)
610 {
611 int status;
612
613 ESL_GETOPTS *go = NULL;
614 P7_CACHEDB_SEQS *cache = NULL;
615
616
617 if ((go = esl_getopts_Create(options)) == NULL) {
618 printf("Failed to create options\n");
619 return 0;
620 }
621 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) {
622 printf("Failed to parse command line: %s\n", go->errbuf);
623 esl_getopts_Destroy(go);
624 return 0;
625 }
626 if (esl_opt_VerifyConfig(go) != eslOK) {
627 printf("Failed to parse command line: %s\n", go->errbuf);
628 esl_getopts_Destroy(go);
629 return 0;
630 }
631
632 if (esl_opt_ArgNumber(go) < 1) {
633 printf("Must specify at least one database\n");
634 esl_getopts_Destroy(go);
635 return 0;
636 }
637
638 printf("Reading cache %s\n", esl_opt_GetArg(go, 1));
639 if ((status = cache_SeqDb(esl_opt_GetArg(go, 1), &cache)) != eslOK) {
640 printf("ERROR %d\n", status);
641 return 0;
642 }
643 cache_SeqDestroy(cache);
644
645 esl_getopts_Destroy(go);
646
647 return 0;
648 }
649
650 #endif /*CACHEDB_UTEST2*/
651
652