1 /*****************************************************************
2 * HMMER - Biological sequence analysis with profile HMMs
3 * Copyright (C) 1992-1999 Washington University School of Medicine
6 * This source code is distributed under the terms of the
7 * GNU General Public License. See the files COPYING and LICENSE
9 *****************************************************************/
13 * Updated interfaces for GSI64 64-bit "generic sequence index" files.
14 * See gsi.c for old interfaces.
15 * This is a temporary hack! Needed for human genome project.
18 /* 1 + <nfiles> + <nkeys> total records.
19 * Each record = 42 bytes.
21 * one header record : <"GSI64" (32)> <nfiles (2)> <nkeys (8)>
22 * <nfiles> file records : <filename (32)> <fileno (2)> <fmt (8)>
23 * <nkeys> key records : <key (32)> <fileno (2)> <offset(8)>
25 * CVS $Id: gsi64.c,v 1.1.1.1 2005/03/22 08:34:29 cmzmasek Exp $
32 #include <unistd.h> /* needed for poor crippled SunOS */
38 /*****************************************************************
39 * GSI64 index file access routines
40 *****************************************************************/
42 /* Function: GSI64Open()
44 * Purpose: Open a GSI64 file. Returns the number of records in
45 * the file and a file pointer. Returns NULL on failure.
46 * The file pointer should be fclose()'d normally.
49 GSI64Open(char *gsifile)
52 char magic[GSI64_KEYSIZE];
54 gsi = (GSI64FILE *) MallocOrDie (sizeof(GSI64FILE));
55 if ((gsi->gsifp = fopen(gsifile, "r")) == NULL)
56 { free(gsi); squid_errno = SQERR_NOFILE; return NULL; }
58 if (! fread(magic, sizeof(char), GSI64_KEYSIZE, gsi->gsifp))
59 { free(gsi); squid_errno = SQERR_NODATA; return NULL; }
60 if (strcmp(magic, "GSI64") != 0)
61 { free(gsi); squid_errno = SQERR_FORMAT; return NULL; }
63 if (! fread(&(gsi->nfiles), sizeof(sqd_uint16), 1, gsi->gsifp))
64 { free(gsi); squid_errno = SQERR_NODATA; return NULL; }
65 if (! fread(&(gsi->recnum), sizeof(sqd_uint64), 1, gsi->gsifp))
66 { free(gsi); squid_errno = SQERR_NODATA; return NULL; }
68 #if 0 /* HACK! we don't byteswap */
69 gsi->nfiles = sre_ntohs(gsi->nfiles); /* convert from network short */
70 gsi->recnum = sre_ntohl(gsi->recnum); /* convert from network long */
76 /* Function: GSI64GetRecord()
78 * Purpose: Each non-header record of a GSI64 index file consists
79 * of 42 bytes: 32 bytes of character string, a 2 byte
80 * short, and an 8 byte long long. This function returns the
83 * Args: gsi - open GSI64 index file, correctly positioned at a record
84 * f1 - char[32], allocated by caller (or NULL if unwanted)
85 * f2 - pointer to short (or NULL if unwanted)
86 * f3 - pointer to long long (or NULL if unwanted)
88 * Return: 0 on failure and sets squid_errno.
91 GSI64GetRecord(GSI64FILE *gsi, char *f1, sqd_uint16 *f2, sqd_uint64 *f3)
93 if (f1 == NULL) fseek64(gsi->gsifp, GSI64_KEYSIZE, SEEK_CUR);
94 else if (! fread(f1, GSI64_KEYSIZE, 1, gsi->gsifp))
95 { squid_errno = SQERR_NODATA; return 0; }
97 if (f2 == NULL) fseek64(gsi->gsifp, sizeof(sqd_uint16), SEEK_CUR);
98 else if (! fread(f2, sizeof(sqd_uint16), 1, gsi->gsifp))
99 { squid_errno = SQERR_NODATA; return 0; }
101 if (f3 == NULL) fseek64(gsi->gsifp, sizeof(sqd_uint64), SEEK_CUR);
102 else if (! fread(f3, sizeof(sqd_uint64), 1, gsi->gsifp))
103 { squid_errno = SQERR_NODATA; return 0; }
105 #if 0 /* no byteswap yet! HACK! */
106 if (f2 != NULL) *f2 = sre_ntohs(*f2);
107 if (f3 != NULL) *f3 = sre_ntohl(*f3);
114 /* Function: GSI64GetOffset()
116 * Purpose: From a key (sequence name), find a disk offset
117 * in an open general sequence index file by binary
118 * search. Presumably GSI64 indexing could be even faster
119 * if we used hashing.
121 * Args: gsi - GSI64 index file, opened by GSI64Open()
122 * key - name of key to retrieve indices for
123 * ret_seqfile - pre-alloced char[32] array for seqfile name
124 * ret_fmt - format of seqfile
125 * ret_offset - return: disk offset in seqfile.
128 GSI64GetOffset(GSI64FILE *gsi, char *key, char *ret_seqfile,
129 int *ret_format, long long *ret_offset)
131 sqd_uint64 left, right, mid;
133 char name[GSI64_KEYSIZE + 1];
138 name[GSI64_KEYSIZE] = '\0';
140 left = gsi->nfiles + 1;
141 right = gsi->nfiles + gsi->recnum;
142 mid = (left + right) / 2;
143 fseek64(gsi->gsifp, mid * GSI64_RECSIZE, SEEK_SET);
145 while (GSI64GetRecord(gsi, name, &filenum, &offset))
147 cmp = strcmp(name, key);
148 if (cmp == 0) break; /* found it! */
149 else if (left >= right) return 0; /* oops, missed it; fail. */
150 else if (cmp < 0) left = mid + 1; /* it's right of mid */
151 else if (cmp > 0) right = mid - 1; /* it's left of mid */
152 mid = (left + right) / 2;
153 fseek64(gsi->gsifp, mid * GSI64_RECSIZE, SEEK_SET);
156 /* Using file number, look up the sequence file and format.
158 fseek64(gsi->gsifp, filenum * GSI64_RECSIZE, SEEK_SET);
159 GSI64GetRecord(gsi, ret_seqfile, NULL, &fmt);
160 *ret_format = (int) fmt;
161 *ret_offset = (long long) offset;
166 /* Function: GSI64Close()
168 * Purpose: Close an open GSI64 sequence index file.
171 GSI64Close(GSI64FILE *gsi)
178 /*****************************************************************
179 * GSI64 index construction routines
180 * SRE, Wed Nov 10 11:49:14 1999 [St. Louis]
183 * g = GSI64AllocIndex();
185 * [foreach filename, <32 char, no directory path]
186 * GSI64AddFileToIndex(g, filename);
188 * [foreach key, <32 char, w/ filenum 1..nfiles, w/ 64bit offset]
189 * GSI64AddKeyToIndex(g, key, filenum, offset);
192 * GSI64WriteIndex(fp, g);
194 *****************************************************************/
195 struct gsi64index_s *
196 GSI64AllocIndex(void)
198 struct gsi64index_s *g;
200 g = MallocOrDie(sizeof(struct gsi64index_s));
201 g->filenames = MallocOrDie(sizeof(char *) * 10);
202 g->fmt = MallocOrDie(sizeof(int) * 10);
203 g->elems = MallocOrDie(sizeof(struct gsi64key_s) * 100);
209 GSI64FreeIndex(struct gsi64index_s *g)
212 for (i = 0; i < g->nfiles; i++) free(g->filenames[i]);
219 GSI64AddFileToIndex(struct gsi64index_s *g, char *filename, int fmt)
223 len = strlen(filename);
224 if (len >= GSI64_KEYSIZE) Die("File name too long to be indexed.");
225 g->filenames[g->nfiles] = sre_strdup(filename, len);
226 g->fmt[g->nfiles] = fmt;
228 if (g->nfiles % 10 == 0) {
229 g->filenames = ReallocOrDie(g->filenames, sizeof(char *) * (g->nfiles + 10));
230 g->fmt = ReallocOrDie(g->fmt, sizeof(int) * (g->nfiles + 10));
234 GSI64AddKeyToIndex(struct gsi64index_s *g, char *key, int filenum, long long offset)
236 if (strlen(key) >= GSI64_KEYSIZE) Die("key too long in GSI64 index");
237 if (filenum > SQD_UINT16_MAX) Die("too many files in GSI64 index");
238 if (offset > SQD_UINT64_MAX) Die("offset too big in GSI64 index");
240 strncpy(g->elems[g->nkeys].key, key, GSI64_KEYSIZE-1);
241 g->elems[g->nkeys].key[GSI64_KEYSIZE-1] = '\0';
242 g->elems[g->nkeys].filenum = (sqd_uint16) filenum;
243 g->elems[g->nkeys].offset = (sqd_uint64) offset;
246 if (g->nkeys % 100 == 0)
247 g->elems = ReallocOrDie(g->elems, sizeof(struct gsi64key_s) * (g->nkeys + 100));
250 gsi_keysorter(const void *k1, const void *k2)
252 struct gsi64key_s *key1;
253 struct gsi64key_s *key2;
254 key1 = (struct gsi64key_s *) k1;
255 key2 = (struct gsi64key_s *) k2;
256 return strcmp(key1->key, key2->key);
259 GSI64SortIndex(struct gsi64index_s *g)
261 qsort((void *) g->elems, g->nkeys, sizeof(struct gsi64key_s), gsi_keysorter);
264 GSI64WriteIndex(FILE *fp, struct gsi64index_s *g)
271 if (g->nfiles > SQD_UINT16_MAX) Die("Too many files in GSI64 index.");
272 if (g->nkeys > SQD_UINT64_MAX) Die("Too many keys in GSI64 index.");
274 GSI64WriteHeader(fp, g->nfiles, g->nkeys);
275 for (i = 0; i < g->nfiles; i++)
276 GSI64WriteFileRecord(fp, g->filenames[i], i+1, g->fmt[i]);
277 for (j = 0; j < g->nkeys; j++)
278 GSI64WriteKeyRecord(fp, g->elems[j].key, g->elems[j].filenum, g->elems[j].offset);
285 /* Function: GSI64WriteHeader()
286 * Date: SRE, Wed Aug 5 10:36:02 1998 [St. Louis]
288 * Purpose: Write the first record to an open GSI64 file:
289 * "GSI64" <nfiles> <nkeys>
291 * Args: fp - open file to write to.
292 * nfiles - number of files indexed
293 * nkeys - number of keys indexed
298 GSI64WriteHeader(FILE *fp, int nfiles, long long nkeys)
300 char key[GSI64_KEYSIZE];
304 /* beware potential range errors!
306 if (nfiles > SQD_UINT16_MAX) Die("GSI64: nfiles out of range");
307 if (nkeys > SQD_UINT64_MAX) Die("GSI64: nkeys out of range");
309 f1 = (sqd_uint16) nfiles;
310 f2 = (sqd_uint64) nkeys;
311 #if 0 /* HACK no byteswap */
315 strcpy(key, "GSI64");
317 if (fwrite(key, 1, GSI64_KEYSIZE, fp) < GSI64_KEYSIZE) PANIC;
318 if (fwrite(&f1, 2, 1, fp) < 1) PANIC;
319 if (fwrite(&f2, 8, 1, fp) < 1) PANIC;
323 /* Function: GSI64WriteFileRecord()
324 * Date: SRE, Wed Aug 5 10:45:51 1998 [St. Louis]
326 * Purpose: Write a file record to an open GSI64 file.
328 * Args: fp - open GSI64 file
329 * fname - file name (max 31 characters)
331 * fmt - file format (e.g. kPearson, etc.)
333 * Returns: 0 on failure. 1 on success.
336 GSI64WriteFileRecord(FILE *fp, char *fname, int idx, int fmt)
341 if (strlen(fname) >= GSI64_KEYSIZE) return 0;
342 if (idx > SQD_UINT16_MAX) Die("GSI64: file index out of range");
343 if (fmt > SQD_UINT64_MAX) Die("GSI64: format index out of range");
345 f1 = (sqd_uint16) idx;
346 f2 = (sqd_uint64) fmt;
347 #if 0 /* hack : no byteswap */
352 if (fwrite(fname, 1, GSI64_KEYSIZE, fp) < GSI64_KEYSIZE) PANIC;
353 if (fwrite(&f1, 2, 1, fp) < 1) PANIC;
354 if (fwrite(&f2, 8, 1, fp) < 1) PANIC;
359 /* Function: GSI64WriteKeyRecord()
360 * Date: SRE, Wed Aug 5 10:52:30 1998 [St. Louis]
362 * Purpose: Write a key record to a GSI64 file.
364 * Args: fp - open GSI64 file for writing
365 * key - key (max 31 char + \0)
366 * fileidx - which file number to find this key in
367 * offset - offset for this key
369 * Returns: 1 on success, else 0.
370 * will fail if key >= 32 chars, for instance.
373 GSI64WriteKeyRecord(FILE *fp, char *key, int fileidx, long long offset)
378 if (strlen(key) >= GSI64_KEYSIZE) return 0;
379 if (fileidx > SQD_UINT16_MAX) Die("GSI64: file index out of range");
380 if (offset > SQD_UINT64_MAX) Die("GSI64: offset out of range");
382 f1 = (sqd_uint16) fileidx;
383 f2 = (sqd_uint64) offset;
389 if (fwrite(key, 1, GSI64_KEYSIZE, fp) < GSI64_KEYSIZE) PANIC;
390 if (fwrite(&f1, 2, 1, fp) < 1) PANIC;
391 if (fwrite(&f2, 8, 1, fp) < 1) PANIC;
395 #endif /*USE_GSI64 */