initial commit
[jalview.git] / forester / archive / RIO / others / hmmer / squid / gsi64.c
1 /*****************************************************************
2  * HMMER - Biological sequence analysis with profile HMMs
3  * Copyright (C) 1992-1999 Washington University School of Medicine
4  * All Rights Reserved
5  * 
6  *     This source code is distributed under the terms of the
7  *     GNU General Public License. See the files COPYING and LICENSE
8  *     for details.
9  *****************************************************************/ 
10 #ifdef USE_GSI64
11
12 /* gsi64.c
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.
16  */
17
18 /*    1 + <nfiles> + <nkeys> total records.
19  *    Each record = 42 bytes.
20  *
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)> 
24  * 
25  * CVS $Id: gsi64.c,v 1.1.1.1 2005/03/22 08:34:29 cmzmasek Exp $
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #ifndef SEEK_SET
32 #include <unistd.h>     /* needed for poor crippled SunOS */
33 #endif
34
35 #include "squid.h"
36 #include "gsi64.h"
37
38 /*****************************************************************
39  * GSI64 index file access routines
40  *****************************************************************/
41
42 /* Function: GSI64Open()
43  * 
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.
47  */
48 GSI64FILE *
49 GSI64Open(char *gsifile)
50 {
51   GSI64FILE  *gsi;
52   char        magic[GSI64_KEYSIZE];
53
54   gsi = (GSI64FILE *) MallocOrDie (sizeof(GSI64FILE));
55   if ((gsi->gsifp = fopen(gsifile, "r")) == NULL)
56     { free(gsi); squid_errno = SQERR_NOFILE; return NULL; }
57
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; }
62
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; }
67
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  */
71 #endif
72
73   return gsi;
74 }
75
76 /* Function: GSI64GetRecord()
77  * 
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
81  *           three values.
82  *           
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)
87  *                  
88  * Return:   0 on failure and sets squid_errno.                  
89  */
90 int
91 GSI64GetRecord(GSI64FILE *gsi, char *f1, sqd_uint16 *f2, sqd_uint64 *f3)
92 {
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; }
96
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; }
100
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; }
104
105 #if 0 /* no byteswap yet! HACK! */
106   if (f2 != NULL) *f2 = sre_ntohs(*f2);
107   if (f3 != NULL) *f3 = sre_ntohl(*f3);
108 #endif 
109
110   return 1;
111 }
112
113
114 /* Function: GSI64GetOffset()
115  * 
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.
120  *   
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.         
126  */
127 int
128 GSI64GetOffset(GSI64FILE *gsi, char *key, char *ret_seqfile, 
129                int *ret_format, long long *ret_offset)
130 {
131   sqd_uint64  left, right, mid;
132   int         cmp;
133   char        name[GSI64_KEYSIZE + 1];
134   sqd_uint64  offset;
135   sqd_uint16  filenum;
136   sqd_uint64  fmt;
137
138   name[GSI64_KEYSIZE] = '\0';
139
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);
144
145   while (GSI64GetRecord(gsi, name, &filenum, &offset))
146     {
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);
154     }
155
156   /* Using file number, look up the sequence file and format.
157    */
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;
162   
163   return 1;
164 }
165     
166 /* Function: GSI64Close()
167  * 
168  * Purpose:  Close an open GSI64 sequence index file.
169  */
170 void
171 GSI64Close(GSI64FILE *gsi)
172 {
173   fclose(gsi->gsifp);
174   free(gsi);
175 }
176
177
178 /*****************************************************************
179  * GSI64 index construction routines
180  * SRE, Wed Nov 10 11:49:14 1999 [St. Louis]
181  * 
182  * API:
183  *       g = GSI64AllocIndex();
184  *       
185  *       [foreach filename, <32 char, no directory path]
186  *          GSI64AddFileToIndex(g, filename);
187  *          filenum++;
188  *          [foreach key, <32 char, w/ filenum 1..nfiles, w/ 64bit offset]
189  *             GSI64AddKeyToIndex(g, key, filenum, offset);
190  *            
191  *       GSI64SortIndex(g);
192  *       GSI64WriteIndex(fp, g);
193  *       GSI64FreeIndex(g);
194  *****************************************************************/
195 struct gsi64index_s *
196 GSI64AllocIndex(void)
197 {
198   struct gsi64index_s *g;
199   
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);
204   g->nfiles    = 0;
205   g->nkeys     = 0;
206   return g;
207 }
208 void
209 GSI64FreeIndex(struct gsi64index_s *g)
210 {
211   int i;
212   for (i = 0; i < g->nfiles; i++) free(g->filenames[i]);
213   free(g->filenames);
214   free(g->fmt);
215   free(g->elems);
216   free(g);
217 }
218 void
219 GSI64AddFileToIndex(struct gsi64index_s *g, char *filename, int fmt)
220 {
221   int len;
222
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;
227   g->nfiles++;
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)); 
231   }
232 }
233 void
234 GSI64AddKeyToIndex(struct gsi64index_s *g, char *key, int filenum, long long offset)
235 {
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");
239   
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;
244   g->nkeys++;
245
246   if (g->nkeys % 100 == 0)
247     g->elems = ReallocOrDie(g->elems, sizeof(struct gsi64key_s) * (g->nkeys + 100));
248 }
249 static int 
250 gsi_keysorter(const void *k1, const void *k2)
251 {
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);
257 }
258 void
259 GSI64SortIndex(struct gsi64index_s *g)
260 {
261   qsort((void *) g->elems, g->nkeys, sizeof(struct gsi64key_s), gsi_keysorter); 
262 }
263 void
264 GSI64WriteIndex(FILE *fp, struct gsi64index_s *g)
265 {
266   sqd_uint16 i;
267   sqd_uint64 j;
268
269   /* Range checking.
270    */
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.");
273
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);
279 }
280
281
282
283
284
285 /* Function: GSI64WriteHeader()
286  * Date:     SRE, Wed Aug  5 10:36:02 1998 [St. Louis]
287  *
288  * Purpose:  Write the first record to an open GSI64 file:
289  *           "GSI64" <nfiles> <nkeys>
290  *
291  * Args:     fp      - open file to write to.
292  *           nfiles  - number of files indexed
293  *           nkeys   - number of keys indexed          
294  *
295  * Returns:  void
296  */
297 void
298 GSI64WriteHeader(FILE *fp, int nfiles, long long nkeys)
299 {
300   char       key[GSI64_KEYSIZE];
301   sqd_uint16 f1;
302   sqd_uint64 f2;
303
304   /* beware potential range errors!
305    */
306   if (nfiles > SQD_UINT16_MAX) Die("GSI64: nfiles out of range");
307   if (nkeys > SQD_UINT64_MAX)  Die("GSI64: nkeys out of range");
308
309   f1 = (sqd_uint16) nfiles;
310   f2 = (sqd_uint64) nkeys;
311 #if 0 /* HACK no byteswap */
312   f1 = sre_htons(f1);
313   f2 = sre_htonl(f2);
314 #endif
315   strcpy(key, "GSI64");
316
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;
320 }
321
322
323 /* Function: GSI64WriteFileRecord()
324  * Date:     SRE, Wed Aug  5 10:45:51 1998 [St. Louis]
325  *
326  * Purpose:  Write a file record to an open GSI64 file.
327  *
328  * Args:     fp    - open GSI64 file
329  *           fname - file name (max 31 characters)
330  *           idx   - file number
331  *           fmt   - file format (e.g. kPearson, etc.)
332  *
333  * Returns:  0 on failure. 1 on success.
334  */
335 int
336 GSI64WriteFileRecord(FILE *fp, char *fname, int idx, int fmt)
337 {
338   sqd_uint16 f1;
339   sqd_uint64 f2;
340
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");
344
345   f1 = (sqd_uint16) idx;
346   f2 = (sqd_uint64) fmt;
347 #if 0 /* hack : no byteswap */
348   f1 = sre_htons(f1);
349   f2 = sre_htonl(f2);
350 #endif
351
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;
355   return 1;
356 }
357
358
359 /* Function: GSI64WriteKeyRecord()
360  * Date:     SRE, Wed Aug  5 10:52:30 1998 [St. Louis]
361  *
362  * Purpose:  Write a key record to a GSI64 file.
363  *
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       
368  * 
369  * Returns:  1 on success, else 0.
370  *           will fail if key >= 32 chars, for instance.
371  */
372 int
373 GSI64WriteKeyRecord(FILE *fp, char *key, int fileidx, long long offset)
374 {
375   sqd_uint16 f1;
376   sqd_uint64 f2;
377
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");
381
382   f1 = (sqd_uint16) fileidx;
383   f2 = (sqd_uint64) offset;
384 #if 0 /* HACK! */
385   f1 = sre_htons(f1);
386   f2 = sre_htonl(f2);
387 #endif 
388   
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;
392   return 1;
393 }
394
395 #endif /*USE_GSI64 */