Debian Codegraph @

easel/esl_ssi.c

raw symbols SCIP
1 /* Sequence/subsequence indices: fast lookup in large sequence files by keyword.
2 *
3 * Contents:
4 * 1. Using (reading) an SSI index.
5 * 2. Creating (writing) new SSI files.
6 * 3. Portable binary i/o.
7 * 4. Unit tests.
8 * 5. Test driver.
9 * 6. Example code.
10 */
11 #include <esl_config.h>
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "easel.h"
17 #include "esl_ssi.h"
18
19 static uint32_t v30magic = 0xd3d3c9b3; /* SSI 3.0: "ssi3" + 0x80808080 */
20 static uint32_t v30swap = 0xb3c9d3d3; /* byteswapped */
21
22
23 /*****************************************************************
24 *# 1. Using (reading) an SSI index.
25 *****************************************************************/
26
27 static int binary_search(ESL_SSI *ssi, const char *key, uint32_t klen, off_t base,
28 uint32_t recsize, uint64_t maxidx);
29
30 /* Function: esl_ssi_Open()
31 * Synopsis: Open an SSI index as an <ESL_SSI>.
32 *
33 * Purpose: Open the SSI index file <filename>, and returns a pointer
34 * to the new <ESL_SSI> object in <ret_ssi>.
35 *
36 * Caller is responsible for closing the SSI file with
37 * <esl_ssi_Close()>.
38 *
39 * Args: <filename> - name of SSI index file to open.
40 * <ret_ssi> - RETURN: the new <ESL_SSI>.
41 *
42 * Returns: <eslOK> on success;
43 * <eslENOTFOUND> if <filename> cannot be opened for reading;
44 * <eslEFORMAT> if it's not in correct SSI file format;
45 * <eslERANGE> if it uses 64-bit file offsets, and we're on a system
46 * that doesn't support 64-bit file offsets.
47 *
48 * Throws: <eslEMEM> on allocation error.
49 */
50 int
51 esl_ssi_Open(const char *filename, ESL_SSI **ret_ssi)
52 {
53 ESL_SSI *ssi = NULL;
54 int status;
55 uint32_t magic; /* magic number that starts the SSI file */
56 uint16_t i; /* counter over files */
57
58 /* Initialize the SSI structure, null'ing so we can autocleanup.
59 */
60 ESL_ALLOC(ssi, sizeof(ESL_SSI));
61 ssi->fp = NULL;
62 ssi->filename = NULL;
63 ssi->fileformat = NULL;
64 ssi->fileflags = NULL;
65 ssi->bpl = NULL;
66 ssi->rpl = NULL;
67 ssi->nfiles = 0;
68
69 /* Open the file.
70 */
71 status = eslENOTFOUND;
72 if ((ssi->fp = fopen(filename, "rb")) == NULL) goto ERROR;
73
74 /* Read the magic number: make sure it's an SSI file, and determine
75 * whether it's byteswapped.
76 */
77 status = eslEFORMAT;
78 if (esl_fread_u32(ssi->fp, &magic) != eslOK) goto ERROR;
79 if (magic != v30magic && magic != v30swap) goto ERROR;
80 if (esl_fread_u32(ssi->fp, &(ssi->flags)) != eslOK) goto ERROR;
81 if (esl_fread_u32(ssi->fp, &(ssi->offsz)) != eslOK) goto ERROR;
82
83 status = eslERANGE;
84 if (ssi->offsz != 4 && ssi->offsz != 8) goto ERROR;
85 if (ssi->offsz > sizeof(off_t)) goto ERROR;
86
87 /* The header data. */
88 status = eslEFORMAT;
89 if (esl_fread_u16(ssi->fp, &(ssi->nfiles)) != eslOK) goto ERROR;
90 if (esl_fread_u64(ssi->fp, &(ssi->nprimary)) != eslOK) goto ERROR;
91 if (esl_fread_u64(ssi->fp, &(ssi->nsecondary)) != eslOK) goto ERROR;
92 if (esl_fread_u32(ssi->fp, &(ssi->flen)) != eslOK) goto ERROR;
93 if (esl_fread_u32(ssi->fp, &(ssi->plen)) != eslOK) goto ERROR;
94 if (esl_fread_u32(ssi->fp, &(ssi->slen)) != eslOK) goto ERROR;
95 if (esl_fread_u32(ssi->fp, &(ssi->frecsize)) != eslOK) goto ERROR;
96 if (esl_fread_u32(ssi->fp, &(ssi->precsize)) != eslOK) goto ERROR;
97 if (esl_fread_u32(ssi->fp, &(ssi->srecsize)) != eslOK) goto ERROR;
98
99 if (esl_fread_offset(ssi->fp, ssi->offsz, &(ssi->foffset)) != eslOK) goto ERROR;
100 if (esl_fread_offset(ssi->fp, ssi->offsz, &(ssi->poffset)) != eslOK) goto ERROR;
101 if (esl_fread_offset(ssi->fp, ssi->offsz, &(ssi->soffset)) != eslOK) goto ERROR;
102
103 /* The file information.
104 * We expect the number of files to be small, so reading it once
105 * should be advantageous overall. If SSI ever had to deal with
106 * large numbers of files, you'd probably want to read file
107 * information on demand.
108 */
109 status = eslEFORMAT;
110 if (ssi->nfiles == 0) goto ERROR;
111
112 ESL_ALLOC(ssi->filename, sizeof(char *) * ssi->nfiles);
113 for (i = 0; i < ssi->nfiles; i++) ssi->filename[i] = NULL;
114 ESL_ALLOC(ssi->fileformat, sizeof(uint32_t) * ssi->nfiles);
115 ESL_ALLOC(ssi->fileflags, sizeof(uint32_t) * ssi->nfiles);
116 ESL_ALLOC(ssi->bpl, sizeof(uint32_t) * ssi->nfiles);
117 ESL_ALLOC(ssi->rpl, sizeof(uint32_t) * ssi->nfiles);
118
119 /* (most) allocations done, now we read. */
120 for (i = 0; i < ssi->nfiles; i++)
121 {
122 ESL_ALLOC(ssi->filename[i], sizeof(char)* ssi->flen);
123 /* We do have to explicitly position, because header and file
124 * records may expand in the future; frecsize and foffset
125 * give us forwards compatibility.
126 */
127 status = eslEFORMAT;
128 if (fseeko(ssi->fp, ssi->foffset + (i * ssi->frecsize), SEEK_SET) != 0) goto ERROR;
129 if (fread(ssi->filename[i],sizeof(char),ssi->flen, ssi->fp)!=ssi->flen) goto ERROR;
130 if (esl_fread_u32(ssi->fp, &(ssi->fileformat[i]))) goto ERROR;
131 if (esl_fread_u32(ssi->fp, &(ssi->fileflags[i]))) goto ERROR;
132 if (esl_fread_u32(ssi->fp, &(ssi->bpl[i]))) goto ERROR;
133 if (esl_fread_u32(ssi->fp, &(ssi->rpl[i]))) goto ERROR;
134 }
135 *ret_ssi = ssi;
136 return eslOK;
137
138 ERROR:
139 if (ssi != NULL) esl_ssi_Close(ssi);
140 *ret_ssi = NULL;
141 return status;
142 }
143
144
145 /* Function: esl_ssi_FindName()
146 * Synopsis: Look up a primary or secondary key.
147 *
148 * Purpose: Looks up the string <key> in index <ssi>.
149 * <key> can be either a primary or secondary key. If <key>
150 * is found, <ret_fh> contains a unique handle on
151 * the file that contains <key> (suitable for an <esl_ssi_FileInfo()>
152 * call, or for comparison to the handle of the last file
153 * that was opened for retrieval), and <ret_offset> contains
154 * the offset of the sequence record in that file.
155 *
156 * Args: <ssi> - open index file
157 * <key> - name to search for
158 * <ret_fh> - RETURN: handle on file that key is in
159 * <ret_roff> - RETURN: offset of the start of that key's record
160 * <opt_doff> - optRETURN: data offset (may be 0 if unset)
161 * <opt_L> - optRETURN: length of data record (may be 0 if unset)
162 *
163 * Returns: <eslOK> on success;
164 * <eslENOTFOUND> if no such key is in the index;
165 * <eslEFORMAT> if an fread() or fseeko() fails, which almost
166 * certainly reflects some kind of misformatting of
167 * the index.
168 *
169 * Throws: <eslEMEM> on allocation error.
170 */
171 int
172 esl_ssi_FindName(ESL_SSI *ssi, const char *key, uint16_t *ret_fh, off_t *ret_roff, off_t *opt_doff, int64_t *opt_L)
173 {
174 int status;
175 off_t doff;
176 int64_t L;
177 char *pkey = NULL;
178
179 /* Look in the primary keys.
180 */
181 status = binary_search(ssi, key, ssi->plen, ssi->poffset, ssi->precsize,
182 ssi->nprimary);
183
184 if (status == eslOK)
185 { /* We found it as a primary key; get our data & return. */
186 status = eslEFORMAT;
187 if (esl_fread_u16(ssi->fp, ret_fh) != eslOK) goto ERROR;
188 if (esl_fread_offset(ssi->fp, ssi->offsz, ret_roff) != eslOK) goto ERROR;
189 if (esl_fread_offset(ssi->fp, ssi->offsz, &doff) != eslOK) goto ERROR;
190 if (esl_fread_i64 (ssi->fp, &L) != eslOK) goto ERROR;
191 }
192 else if (status == eslENOTFOUND)
193 { /* Not in the primary keys? OK, try the secondary keys. */
194 if (ssi->nsecondary > 0) {
195 if ((status = binary_search(ssi, key, ssi->slen, ssi->soffset, ssi->srecsize, ssi->nsecondary)) != eslOK) goto ERROR;
196
197 /* We have the secondary key; flip to its primary key, then look that up. */
198 ESL_ALLOC(pkey, sizeof(char) * ssi->plen);
199 status = eslEFORMAT;
200 if (fread(pkey, sizeof(char), ssi->plen, ssi->fp) != ssi->plen) goto ERROR;
201 if ((status = esl_ssi_FindName(ssi, pkey, ret_fh, ret_roff, &doff, &L)) != eslOK) goto ERROR;
202 } else goto ERROR; /* no secondary keys? pass along the ENOTFOUND error. */
203 } else goto ERROR; /* status from binary search was an error code. */
204
205 if (pkey != NULL) free(pkey);
206 if (opt_doff != NULL) *opt_doff = doff;
207 if (opt_L != NULL) *opt_L = L;
208 return eslOK;
209
210 ERROR:
211 if (pkey != NULL) free(pkey);
212 *ret_fh = 0;
213 *ret_roff = 0;
214 if (opt_doff != NULL) *opt_doff = 0;
215 if (opt_L != NULL) *opt_L = 0;
216 return status;
217 }
218
219
220
221 /* Function: esl_ssi_FindNumber()
222 * Synopsis: Look up the n'th primary key.
223 *
224 * Purpose: Looks up primary key number <nkey> in the open index
225 * <ssi>. <nkey> ranges from <0..ssi->nprimary-1>. When
226 * key <nkey> is found, any/all of several optional
227 * arguments point to results. <*opt_fh> contains a unique
228 * handle on the file that contains that key (suitable for
229 * an <esl_ssi_FileInfo()> call, or for comparison to the
230 * handle of the last file that was opened for retrieval).
231 * <*opt_roff> contains the record offset; <*opt_doff>
232 * contains the data offset; <*opt_L> contains the record
233 * length; and <*opt_pkey> points to the primary key name
234 * (a string, allocated here, that the caller becomes
235 * responsible for free'ing).
236 *
237 * Args: <ssi> - open index file
238 * <nkey> - primary key number to retrieve (0..nprimary-1)
239 * <opt_fh> - optRETURN: handle on file that key is in
240 * <opt_roff> - optRETURN: offset of the start of that key's record
241 * <opt_doff> - optRETURN: data offset (may be 0 if unset)
242 * <opt_L> - optRETURN: length of data record (may be 0 if unset)
243 * <opt_pkey> - optRETURN: primary key name (allocated here; caller must free)
244 *
245 * Returns: <eslOK> on success;
246 * <eslENOTFOUND> if there is no sequence record <nkey>;
247 * <eslEFORMAT> if a read or a seek fails, probably indicating
248 * some kind of file misformatting.
249 *
250 * Throws: <eslEMEM> on allocation error.
251 */
252 int
253 esl_ssi_FindNumber(ESL_SSI *ssi, int64_t nkey, uint16_t *opt_fh, off_t *opt_roff, off_t *opt_doff, int64_t *opt_L, char **opt_pkey)
254 {
255 int status;
256 uint16_t fh;
257 off_t doff, roff;
258 uint64_t L;
259 char *pkey = NULL;
260
261 if (nkey >= ssi->nprimary) { status = eslENOTFOUND; goto ERROR; }
262 ESL_ALLOC(pkey, sizeof(char) * ssi->plen);
263
264 status = eslEFORMAT;
265 if (fseeko(ssi->fp, ssi->poffset+ssi->precsize*nkey, SEEK_SET)!= 0) goto ERROR;
266 if (fread(pkey, sizeof(char), ssi->plen, ssi->fp) != ssi->plen) goto ERROR;
267 if (esl_fread_u16(ssi->fp, &fh) != eslOK) goto ERROR;
268 if (esl_fread_offset(ssi->fp, ssi->offsz, &roff) != eslOK) goto ERROR;
269 if (esl_fread_offset(ssi->fp, ssi->offsz, &doff) != eslOK) goto ERROR;
270 if (esl_fread_u64 (ssi->fp, &L) != eslOK) goto ERROR;
271
272 if (opt_fh != NULL) *opt_fh = fh;
273 if (opt_roff != NULL) *opt_roff = roff;
274 if (opt_doff != NULL) *opt_doff = doff;
275 if (opt_L != NULL) *opt_L = L;
276 if (opt_pkey != NULL) *opt_pkey = pkey; else free(pkey);
277 return eslOK;
278
279 ERROR:
280 if (pkey != NULL) free(pkey);
281 if (opt_fh != NULL) *opt_fh = 0;
282 if (opt_roff != NULL) *opt_roff = 0;
283 if (opt_doff != NULL) *opt_doff = 0;
284 if (opt_L != NULL) *opt_L = 0;
285 if (opt_pkey != NULL) *opt_pkey = NULL;
286 return status;
287 }
288
289
290 /* Function: esl_ssi_FindSubseq()
291 * Synopsis: Look up a specific subsequence's start.
292 * Date: SRE, Mon Jan 1 19:49:31 2001 [St. Louis]
293 *
294 * Purpose: Fast subsequence retrieval: look up a primary or secondary
295 * <key> in the open index <ssi>, and ask for the nearest data
296 * offset to a subsequence starting at residue
297 * <requested_start> in the sequence (numbering the sequence
298 * <1..L>). If <key> is found, on return, <ret_fh> contains
299 * a unique handle on the file that contains <key>;
300 * <ret_roff> contains the disk offset to the start of the
301 * sequence record; <ret_doff> contains the disk offset
302 * (see below); and <ret_actual_start) contains the coordinate
303 * (1..L) of the first valid residue at or after
304 * <data_offset>. <ret_actual_start> is $\leq$
305 * <requested_start>.
306 *
307 * Depending on the file's characteristics, there are four
308 * possible outcomes.
309 *
310 * If the file has the <eslSSI_FASTSUBSEQ> flag set, a data
311 * offset was indexed for this key, and the data can be
312 * indexed at single residue resolution (because the file's
313 * lines contain only residues, no spaces), then <ret_doff>
314 * is exactly the position of residue <requested_start> on
315 * disk, and <ret_actual_start> is <requested_start>.
316 *
317 * If the file has the <eslSSI_FASTSUBSEQ> flag set, a data
318 * offset was indexed for this key, but the data can only be
319 * indexed at line resolution (because at least some of the
320 * file's lines contain spaces), then <ret_doff> is the
321 * position of the start of the line that <requested_start>
322 * is on, and <ret_actual_start> is the coord <1..L> of the
323 * first residue on that line.
324 *
325 * If the file does not have the <eslSSI_FASTSUBSEQ> flag
326 * set (because lines contain a variable number of residues
327 * and/or bytes), but a data offset was indexed for this
328 * key, then we can still at least return that data offset,
329 * but the caller is going to have to start from the
330 * beginning of the data and read residues until it reaches
331 * the desired <requested_start>. Now <ret_doff> is the
332 * offset to the start of the first line of the sequence
333 * data, and <ret_actual_start> is 1.
334 *
335 * If the key does not have a data offset indexed at all,
336 * then regardless of the file's <eslSSI_FASTSUBSEQ>
337 * setting, we can't calculate even the position of the
338 * first line. In this case, <ret_doff> is 0 (for
339 * unset/unknown), and <ret_actual_start> is <1>.
340 *
341 * A caller that's going to position the disk and read a
342 * subseq must check for all four possible outcomes (pardon
343 * redundancy with the above, but just to be clear, from the
344 * caller's perspective now):
345 *
346 * If <ret_doff> is 0, no data offset information can be
347 * calculated; the caller can still use <ret_roff> to
348 * position the disk to the start of <key>'s record, but it
349 * will need to parse the header to find the start of the
350 * sequence data; then it will need to parse the sequence
351 * data, skipping to residue <requested start>.
352 *
353 * If <ret_doff> is valid ($>0$), and <ret_actual_start> is
354 * 1, then caller may use <ret_doff> to position the disk to
355 * the start of the first sequence data line, but will still
356 * need to parse all the sequence data, counting and
357 * skipping to residue <requested start>. This is equivalent
358 * to (and in practice, not much more efficient than)
359 * positioning to the record start and parsing the header to
360 * locate the sequence data start.
361 *
362 * If <ret_doff> is valid ($>0$), and <ret_actual_start> is
363 * $>1$ but $<$ <requested_start>, then <ret_doff> is the
364 * offset to the first byte of a line on which the
365 * subsequence begins. The caller can position the disk
366 * there, then start parsing, skipping <requested_start -
367 * *ret_actual_start> residues to reach the
368 * <requested_start>. (In the case where the subsequence
369 * begins on the first line, then <ret_actual_start> will be
370 * 1, and the caller will have to handle this as the case
371 * above.)
372 *
373 * If <<ret_doff> is valid ($>0$), and <ret_actual_start> is
374 * $=$ <requested_start>, then <ret_doff> is the offset to a
375 * byte in the file, such that the requested subsequence
376 * starts at the next valid residue at or after that
377 * position. (The <ret_doff> would usually be exactly the
378 * first residue of the subsequence, because we used single
379 * residue resolution arithmetic to find it, but there's a
380 * case where <requested_start> happens to be the first
381 * residue of a line and we calculated <ret_doff> using
382 * line-resolution arithmetic; in this latter case,
383 * <ret_doff> could be pointing at a space before the first
384 * subseq residue.) The caller may position the disk there
385 * and start parsing immediately; the first valid residue
386 * will be the start of the subsequence.
387 *
388 * Args: <ssi> - open index file
389 * <key> - primary or secondary key to find
390 * <requested_start> - residue we'd like to start at (1..L)
391 * <ret_fh> - RETURN: handle for file the key is in
392 * <ret_roff> - RETURN: offset to start of sequence record
393 * <ret_doff> - RETURN: offset to closest start of subseq data, or 0.
394 * <ret_L> - RETURN: length of <key> in residues (may be 0 if unset)
395 * <ret_actual_start>- RETURN: coord (1..L) of residue at <ret_doff>
396 *
397 * Returns: <eslOK> on any of the four successful outcomes.
398 * <eslENOTFOUND> if no such key is found in the index;
399 * <eslEFORMAT> on a read or seek failure, presumably meaning that
400 * the file is misformatted somehow;
401 * <eslERANGE> if <requested_start> isn't somewhere in the range
402 * <1..len> for the target sequence.
403 *
404 * Throws: <eslEMEM> on allocation error.
405 */
406 int
407 esl_ssi_FindSubseq(ESL_SSI *ssi, const char *key, int64_t requested_start,
408 uint16_t *ret_fh, off_t *ret_roff, off_t *ret_doff, int64_t *ret_L, int64_t *ret_actual_start)
409 {
410 int status;
411 uint64_t r, b, i, l; /* tmp variables for "clarity", to match docs */
412
413 /* Look up the key by name.
414 */
415 if ((status = esl_ssi_FindName(ssi, key, ret_fh, ret_roff, ret_doff, ret_L)) != eslOK) goto ERROR;
416 if (requested_start < 0 || requested_start > *ret_L) { status = eslERANGE; goto ERROR; }
417
418 /* Do we have a data offset for this key? If not, we're case 4. */
419 /* Can we do fast subseq lookup on this file? If no, we're case 3. */
420 if (*ret_doff == 0 || ! (ssi->fileflags[*ret_fh] & eslSSI_FASTSUBSEQ))
421 {
422 *ret_actual_start = 1;
423 return eslOK;
424 }
425
426 /* Set up tmp variables for clarity of equations below,
427 * and to make them match tex documentation
428 */
429 r = ssi->rpl[*ret_fh]; /* residues per line */
430 b = ssi->bpl[*ret_fh]; /* bytes per line */
431 i = requested_start; /* start position 1..L */
432 l = (i-1)/r; /* data line # (0..) that the residue is on */
433 if (r == 0 || b == 0) { status = eslEINVAL; goto ERROR; }
434
435 /* When b = r+1, there's nothing but sequence on each data line (and the \0).
436 * In this case, we know we can find each residue precisely: outcome #1.
437 */
438 if (b == r+1)
439 {
440 *ret_doff += l*b + (i-1)%r;
441 *ret_actual_start = requested_start;
442 }
443 /* else, there's other stuff on seq lines - probably spaces - so the best
444 * we can do (without figuring out the spacing pattern and checking that
445 * it's consistent everywhere) is to position at start of relevant line.
446 */
447 else
448 {
449 *ret_doff += l*b;
450 *ret_actual_start = 1 + l*r;
451 }
452 return eslOK;
453
454 ERROR:
455 *ret_fh = 0;
456 *ret_roff = 0;
457 *ret_doff = 0;
458 *ret_L = 0;
459 *ret_actual_start = 0;
460 return status;
461 }
462
463
464 /* Function: esl_ssi_FileInfo()
465 * Synopsis: Retrieve a file name and format code.
466 * Date: SRE, Tue Jan 2 10:31:01 2001 [St. Louis]
467 *
468 * Purpose: Given a file number <fh> in an open index file
469 * <ssi>, retrieve file name <ret_filename> and
470 * the file format <ret_format>.
471 *
472 * <ret_filename> is a pointer to a string maintained
473 * internally by <ssi>. It should not be free'd;
474 * <esl_ssi_Close(ssi)> will take care of it.
475 *
476 * Args: <ssi> - open index file
477 * <fh> - handle on file to look up
478 * <ret_filename> - RETURN: name of file n
479 * <ret_format> - RETURN: format code for file n
480 *
481 * Returns: <eslOK> on success.
482 *
483 * Throws: <eslEINVAL> if there is no such file number <fh>.
484 */
485 int
486 esl_ssi_FileInfo(ESL_SSI *ssi, uint16_t fh, char **ret_filename, int *ret_format)
487 {
488 int status;
489
490 if (fh >= ssi->nfiles) ESL_XEXCEPTION(eslEINVAL, "no such file number");
491 *ret_filename = ssi->filename[fh];
492 *ret_format = ssi->fileformat[fh];
493 return eslOK;
494
495 ERROR:
496 *ret_filename = NULL;
497 *ret_format = 0;
498 return status;
499 }
500
501
502 /* Function: esl_ssi_Close()
503 * Synopsis: Close an SSI index.
504 *
505 * Purpose: Close an open SSI index <ssi>.
506 *
507 * Args: <ssi> - an open SSI index file.
508 */
509 void
510 esl_ssi_Close(ESL_SSI *ssi)
511 {
512 int i;
513
514 if (ssi == NULL) return;
515
516 if (ssi->fp != NULL) fclose(ssi->fp);
517 if (ssi->filename != NULL) {
518 for (i = 0; i < ssi->nfiles; i++)
519 if (ssi->filename[i] != NULL) free(ssi->filename[i]);
520 free(ssi->filename);
521 }
522 if (ssi->fileformat != NULL) free(ssi->fileformat);
523 if (ssi->fileflags != NULL) free(ssi->fileflags);
524 if (ssi->bpl != NULL) free(ssi->bpl);
525 if (ssi->rpl != NULL) free(ssi->rpl);
526 free(ssi);
527 }
528
529
530 /* binary_search()
531 * Date: SRE, Sun Dec 31 16:05:03 2000 [St. Louis]
532 *
533 * Purpose: Find <key> in an SSI index, by a binary search
534 * in an alphabetically sorted list of keys. If successful,
535 * return <eslOK>, and the index file is positioned to read
536 * the rest of the data for that key. If unsuccessful,
537 * return <eslFAIL>, and the positioning of the index file
538 * is left in an undefined state.
539 *
540 * Args: <ssi> - an open ESL_SSI
541 * <key> - key to find
542 * <klen> - key length to allocate (plen or slen from ssi)
543 * <base> - base offset (poffset or soffset)
544 * <recsize> - size of each key record in bytes (precsize or srecsize)
545 * <maxidx> - # of keys (nprimary or nsecondary)
546 *
547 * Returns: <eslOK> on success, and leaves file positioned for reading remaining
548 * data for the key.
549 *
550 * <eslENOTFOUND> if <key> is not found.
551 * <eslEFORMAT> if an fread() or fseeko() fails, probably indicating
552 * some kind of misformatting of the index file.
553 *
554 * Throws: <eslEMEM> on allocation failure.
555 *
556 */
557 static int
558 binary_search(ESL_SSI *ssi, const char *key, uint32_t klen, off_t base,
559 uint32_t recsize, uint64_t maxidx)
560 {
561 char *name;
562 uint64_t left, right, mid;
563 int cmp;
564 int status;
565
566 if (maxidx == 0) return eslENOTFOUND; /* special case: empty index */
567
568 ESL_ALLOC(name, (sizeof(char)*klen));
569
570 left = 0;
571 right = maxidx-1;
572 while (1) { /* A binary search: */
573 mid = (left+right) / 2; /* careful here. left+right potentially overflows if
574 we didn't limit unsigned vars to signed ranges. */
575 status = eslEFORMAT;
576 if (fseeko(ssi->fp, base + recsize*mid, SEEK_SET) != 0) goto ERROR;
577 if (fread(name, sizeof(char), klen, ssi->fp) != klen) goto ERROR;
578
579 status = eslENOTFOUND;
580 cmp = strcmp(name, key);
581 if (cmp == 0) break; /* found it! */
582 else if (left >= right) goto ERROR; /* no such key */
583 else if (cmp < 0) left = mid+1; /* it's still right of mid */
584 else if (cmp > 0) {
585 if (mid == 0) goto ERROR; /* beware left edge case */
586 else right = mid-1; /* it's left of mid */
587 }
588 }
589
590 if (name != NULL) free(name);
591 return eslOK; /* and ssi->fp is positioned to read the record. */
592
593 ERROR:
594 if (name != NULL) free(name);
595 return status;
596 }
597
598
599 /*****************************************************************
600 *# 2. Creating (writing) new SSI files.
601 *****************************************************************/
602 static int current_newssi_size(const ESL_NEWSSI *ns);
603 static int activate_external_sort(ESL_NEWSSI *ns);
604 static int parse_pkey(char *buf, ESL_PKEY *pkey);
605 static int parse_skey(char *buf, ESL_SKEY *skey);
606 static int pkeysort(const void *k1, const void *k2);
607 static int skeysort(const void *k1, const void *k2);
608
609 /* Function: esl_newssi_Open()
610 * Synopsis: Create a new <ESL_NEWSSI>.
611 *
612 * Purpose: Creates and returns a <ESL_NEWSSI>, in order to create a
613 * new SSI index file.
614 *
615 * Returns: <eslOK> on success, and <*ret_newssi> is a pointer to a
616 * new <ESL_NEWSSI> structure.
617 *
618 * Returns <eslENOTFOUND> if <ssifile> can't be opened.
619 *
620 * Returns <eslEOVERWRITE> if <allow_overwrite> is <FALSE>
621 * and <ssifile> (or any necessary tmp files) already
622 * exist, to block overwriting of an existing SSI file.
623 *
624 * Throws: <eslEMEM> on allocation error.
625 */
626 int
627 esl_newssi_Open(const char *ssifile, int allow_overwrite, ESL_NEWSSI **ret_newssi)
628 {
629 ESL_NEWSSI *ns = NULL;
630 int i;
631 int status;
632
633 ESL_ALLOC(ns, sizeof(ESL_NEWSSI));
634 ns->ssifile = NULL;
635 ns->ssifp = NULL;
636 ns->external = FALSE; /* we'll switch to external sort if... */
637 ns->max_ram = eslSSI_MAXRAM; /* ... if we exceed this memory limit in MB. */
638 ns->filenames = NULL;
639 ns->fileformat = NULL;
640 ns->bpl = NULL;
641 ns->rpl = NULL;
642 ns->flen = 0;
643 ns->nfiles = 0;
644 ns->pkeys = NULL;
645 ns->plen = 0;
646 ns->nprimary = 0;
647 ns->ptmpfile = NULL;
648 ns->ptmp = NULL;
649 ns->skeys = NULL;
650 ns->slen = 0;
651 ns->nsecondary = 0;
652 ns->stmpfile = NULL;
653 ns->stmp = NULL;
654 ns->errbuf[0] = '\0';
655
656 if ((status = esl_strdup(ssifile, -1, &(ns->ssifile))) != eslOK) goto ERROR;
657 if ((status = esl_strdup(ssifile, -1, &(ns->ptmpfile))) != eslOK) goto ERROR;
658 if ((status = esl_strdup(ssifile, -1, &(ns->stmpfile))) != eslOK) goto ERROR;
659 if ((status = esl_strcat(&ns->ptmpfile, -1, ".1", 2)) != eslOK) goto ERROR;
660 if ((status = esl_strcat(&ns->stmpfile, -1, ".2", 2)) != eslOK) goto ERROR;
661
662 if (! allow_overwrite)
663 {
664 if (esl_FileExists(ssifile) ||
665 esl_FileExists(ns->ptmpfile) ||
666 esl_FileExists(ns->stmpfile))
667 { status = eslEOVERWRITE; goto ERROR; }
668 }
669
670 if ((ns->ssifp = fopen(ssifile, "w")) == NULL) { status = eslENOTFOUND; goto ERROR; }
671
672 ESL_ALLOC(ns->filenames, sizeof(char *) * eslSSI_FCHUNK);
673 for (i = 0; i < eslSSI_FCHUNK; i++)
674 ns->filenames[i] = NULL;
675 ESL_ALLOC(ns->fileformat, sizeof(uint32_t) * eslSSI_FCHUNK);
676 ESL_ALLOC(ns->bpl, sizeof(uint32_t) * eslSSI_FCHUNK);
677 ESL_ALLOC(ns->rpl, sizeof(uint32_t) * eslSSI_FCHUNK);
678 ESL_ALLOC(ns->pkeys, sizeof(ESL_PKEY) * eslSSI_KCHUNK);
679 for (i = 0; i < eslSSI_KCHUNK; i++)
680 ns->pkeys[i].key = NULL;
681 ESL_ALLOC(ns->skeys, sizeof(ESL_SKEY) * eslSSI_KCHUNK);
682 for (i = 0; i < eslSSI_KCHUNK; i++) {
683 ns->skeys[i].key = NULL;
684 ns->skeys[i].pkey = NULL;
685 }
686 *ret_newssi = ns;
687 return eslOK;
688
689 ERROR:
690 esl_newssi_Close(ns); /* free the damaged structure */
691 return status;
692 }
693
694
695 /* Function: esl_newssi_AddFile()
696 * Synopsis: Add a filename to a growing index.
697 *
698 * Purpose: Registers the file <filename> into the new index <ns>,
699 * along with its format code <fmt>. The index assigns it
700 * a unique handle, which it returns in <ret_fh>. This
701 * handle is needed when registering primary keys.
702 *
703 * Caller should make sure that the same file isn't registered
704 * twice; this function doesn't check.
705 *
706 * Args: <ns> - new ssi index under construction.
707 * <filename> - filename to add to the index.
708 * <fmt> - format code to associate with <filename> (or 0)
709 * <ret_fh> - RETURN: filehandle associated with <filename>
710 *
711 * Returns: <eslOK> on success;
712 * <eslERANGE> if registering this file would exceed the
713 * maximum number of indexed files.
714 *
715 * Throws: <eslEMEM> on allocation or reallocation error.
716 */
717 int
718 esl_newssi_AddFile(ESL_NEWSSI *ns, const char *filename, int fmt, uint16_t *ret_fh)
719 {
720 int status;
721 uint16_t fh;
722 int i;
723 int n;
724
725 if (ns->nfiles >= eslSSI_MAXFILES) ESL_XFAIL(eslERANGE, ns->errbuf, "exceeded the maximum number of files an SSI index can store");
726
727 n = strlen(filename);
728 if ((n+1) > ns->flen) ns->flen = n+1;
729
730 if ((status = esl_FileTail(filename, FALSE, &(ns->filenames[ns->nfiles]))) != eslOK) goto ERROR;
731
732 ns->fileformat[ns->nfiles] = fmt;
733 ns->bpl[ns->nfiles] = 0;
734 ns->rpl[ns->nfiles] = 0;
735 fh = ns->nfiles; /* handle is simply = file number */
736 ns->nfiles++;
737
738 if (ns->nfiles % eslSSI_FCHUNK == 0) {
739 ESL_REALLOC(ns->filenames, sizeof(char *) * (ns->nfiles+eslSSI_FCHUNK));
740 for (i = ns->nfiles; i < ns->nfiles+eslSSI_FCHUNK; i++) ns->filenames[i] = NULL;
741 ESL_REALLOC(ns->fileformat, sizeof(uint32_t) * (ns->nfiles+eslSSI_FCHUNK));
742 ESL_REALLOC(ns->bpl, sizeof(uint32_t) * (ns->nfiles+eslSSI_FCHUNK));
743 ESL_REALLOC(ns->rpl, sizeof(uint32_t) * (ns->nfiles+eslSSI_FCHUNK));
744 }
745
746 *ret_fh = fh;
747 return eslOK;
748
749 ERROR:
750 *ret_fh = 0;
751 return status;
752 }
753
754
755
756 /* Function: esl_newssi_SetSubseq()
757 * Synopsis: Declare that file is suitable for fast subseq lookup.
758 *
759 * Purpose: Declare that the file associated with handle <fh> is
760 * suitable for fast subsequence lookup, because it has
761 * a constant number of residues and bytes per (nonterminal)
762 * data line, <rpl> and <bpl>, respectively.
763 *
764 * Caller is responsible for this being true: <rpl> and
765 * <bpl> must be constant for every nonterminal line of
766 * every sequence in this file.
767 *
768 * Args: <ns> - ssi index under construction
769 * <fh> - handle on file to set fast subseq lookup on
770 * <bpl> - constant bytes per nonterminal line in <fh>
771 * <rpl> - constant residues per nonterminal line in <fh>
772 *
773 * Returns: <eslOK> on success.
774 *
775 * Throws: <eslEINVAL> on invalid argument(s).
776 */
777 int
778 esl_newssi_SetSubseq(ESL_NEWSSI *ns, uint16_t fh, uint32_t bpl, uint32_t rpl)
779 {
780 int status;
781
782 if (fh >= ns->nfiles) ESL_XEXCEPTION(eslEINVAL, "invalid file number");
783 if (bpl <= 0 || rpl <= 0) ESL_XEXCEPTION(eslEINVAL, "invalid bpl or rpl");
784 ns->bpl[fh] = bpl;
785 ns->rpl[fh] = rpl;
786 return eslOK;
787
788 ERROR:
789 return status;
790 }
791
792
793 /* Function: esl_newssi_AddKey()
794 * Synopsis: Add a primary key to a growing index.
795 * Date: SRE, Tue Jan 2 11:50:54 2001 [St. Louis]
796 *
797 * Purpose: Register primary key <key> in new index <ns>, while telling
798 * the index that this primary key is in the file associated
799 * with filehandle <fh> (the handle returned by a previous call
800 * to <esl_newssi_AddFile()>); that its record starts at
801 * offset <r_off> in the file; that its data (usually
802 * sequence data) starts at offset <d_off> in the file (i.e.
803 * after any record header); and that the record's data is
804 * of length <L> (usually, the record is a sequence, and <L>
805 * is its length in residues).
806 *
807 * The data length <L> is technically optional as far as SSI
808 * is concerned; <L> may be passed as 0 to leave it
809 * unset. However, functions in the <sqio> module that use
810 * SSI indices will assume that <L> is available.
811 *
812 * <d_off> is also optional; it may be passed as <0> to
813 * leave it unset. If provided, <d_off> gives an offset to
814 * the data portion of the record. The interpretation of
815 * this data offset may be implementation-defined and may
816 * depend on the format of the datafile; for example, in how
817 * <sqio> uses SSI indices, <d_off> is the offset to the
818 * start of the first sequence line.
819 *
820 * Both <d_off> and <L> must be provided, and additionally
821 * <eslSSI_FASTSUBSEQ> must be set for this file, for fast
822 * subsequence lookup to work.
823 *
824 * Args: <ns> - active index
825 * <key> - primary key to add
826 * <fh> - handle on file that this key's in
827 * <r_off> - offset to start of record
828 * <d_off> - offset to start of sequence data, or 0
829 * <L> - length of sequence, or 0
830 *
831 * Returns: <eslOK> on success;
832 * <eslERANGE> if registering this key would exceed the maximum
833 * number of primary keys;
834 * <eslENOTFOUND> if we needed to open external tmp files, but
835 * the attempt to open them failed.
836 *
837 * Throws: <eslEINVAL> on an invalid argument;
838 * <eslEMEM> on allocation failure;
839 * <eslEWRITE> on any system error writing to tmp file, such
840 * as filling the filesystem.
841 */
842 int
843 esl_newssi_AddKey(ESL_NEWSSI *ns, const char *key, uint16_t fh,
844 off_t r_off, off_t d_off, int64_t L)
845 {
846 int status;
847 int i;
848 int n; /* a string length */
849
850 if (fh >= eslSSI_MAXFILES) ESL_XEXCEPTION(eslEINVAL, "invalid fh");
851 if (ns->nprimary >= eslSSI_MAXKEYS) ESL_XFAIL(eslERANGE, ns->errbuf, "exceeded maximum number of primary keys allowed");
852
853 /* Before adding the key: check how big our index is.
854 * If it's getting too large, switch to external mode.
855 */
856 if (!ns->external && current_newssi_size(ns) >= ns->max_ram)
857 if ((status = activate_external_sort(ns)) != eslOK) goto ERROR;
858
859 /* Update maximum pkey length, if needed. (Inclusive of '\0').
860 */
861 n = strlen(key)+1;
862 if (n > ns->plen) ns->plen = n;
863
864 /* External mode? Simply append to disk...
865 */
866 if (ns->external)
867 {
868 if (sizeof(off_t) == 4) {
869 if (fprintf(ns->ptmp, "%s\t%d\t%" PRIu32 "\t%" PRIu32 "\t%" PRIi64 "\n",
870 key, fh, (uint32_t) r_off, (uint32_t) d_off, L) <= 0)
871 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi key tmp file write failed");
872 } else {
873 if (fprintf(ns->ptmp, "%s\t%d\t%" PRIu64 "\t%" PRIu64 "\t%" PRIi64 "\n",
874 key, fh, (uint64_t) r_off, (uint64_t) d_off, L) <= 0)
875 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi key tmp file write failed");
876 }
877 ns->nprimary++;
878 }
879 else
880 {
881 /* Else: internal mode, keep keys in memory...
882 */
883 if ((status = esl_strdup(key, n, &(ns->pkeys[ns->nprimary].key))) != eslOK) goto ERROR;
884 ns->pkeys[ns->nprimary].fnum = fh;
885 ns->pkeys[ns->nprimary].r_off = r_off;
886 ns->pkeys[ns->nprimary].d_off = d_off;
887 ns->pkeys[ns->nprimary].len = L;
888 ns->nprimary++;
889
890 /* Reallocate as needed. */
891 if (ns->nprimary % eslSSI_KCHUNK == 0) {
892 ESL_REALLOC(ns->pkeys, sizeof(ESL_PKEY) * (ns->nprimary+eslSSI_KCHUNK));
893 for (i = ns->nprimary; i < ns->nprimary + eslSSI_KCHUNK; i++)
894 ns->pkeys[i].key = NULL;
895 }
896 }
897 return eslOK;
898
899 ERROR:
900 return status;
901 }
902
903 /* Function: esl_newssi_AddAlias()
904 * Synopsis: Add a secondary key (alias) to a growing index.
905 *
906 * Purpose: Registers secondary key <alias> in index <ns>, and
907 * map it to the primary key <key>. <key> must already
908 * have been registered. That is, when someone looks up <alias>,
909 * we'll retrieve record <key>.
910 *
911 * Args: <ns> - ssi index being constructed
912 * <alias> - secondary key to register
913 * <key> - primary key to associate with <skey>.
914 *
915 * Returns: <eslOK> on success;
916 * <eslERANGE> if registering this key would exceed the maximum
917 * number of secondary keys that can be stored;
918 * <eslENOTFOUND> if we needed to open external tmp files, but
919 * the attempt to open them failed.
920 *
921 * Throws: <eslEWRITE> on any system error writing to tmp file, such
922 * as running out of space on the device.
923 */
924 int
925 esl_newssi_AddAlias(ESL_NEWSSI *ns, const char *alias, const char *key)
926 {
927 int status;
928 int i;
929 int n; /* a string length */
930
931 if (ns->nsecondary >= eslSSI_MAXKEYS) ESL_XFAIL(eslERANGE, ns->errbuf, "exceeded maximum number of secondary keys allowed");
932
933 /* Before adding the key: check how big our index is.
934 * If it's getting too large, switch to external mode.
935 */
936 if (!ns->external && current_newssi_size(ns) >= ns->max_ram)
937 if ((status = activate_external_sort(ns)) != eslOK) goto ERROR;
938
939 /* Update maximum secondary key length, if necessary. */
940 n = strlen(alias)+1;
941 if (n > ns->slen) ns->slen = n;
942
943 /* if external mode: write info to disk. */
944 if (ns->external)
945 {
946 if (fprintf(ns->stmp, "%s\t%s\n", alias, key) <= 0) ESL_XEXCEPTION_SYS(eslEWRITE, "ssi alias tmp file write failed");
947 ns->nsecondary++;
948 }
949 else
950 { /* else, internal mode... store info in memory. */
951 if ((status = esl_strdup(alias, n, &(ns->skeys[ns->nsecondary].key))) != eslOK) goto ERROR;
952 if ((status = esl_strdup(key, -1, &(ns->skeys[ns->nsecondary].pkey))) != eslOK) goto ERROR;
953 ns->nsecondary++;
954
955 if (ns->nsecondary % eslSSI_KCHUNK == 0) {
956 ESL_REALLOC(ns->skeys, sizeof(ESL_SKEY) * (ns->nsecondary+eslSSI_KCHUNK));
957 for (i = ns->nsecondary; i < ns->nsecondary+eslSSI_KCHUNK; i++) {
958 ns->skeys[i].key = NULL;
959 ns->skeys[i].pkey = NULL;
960 }
961 }
962 }
963 return eslOK;
964
965 ERROR:
966 return status;
967 }
968
969
970 /* Function: esl_newssi_Write()
971 * Synopsis: Save a new index to an SSI file.
972 *
973 * Purpose: Writes the complete index <ns> in SSI format to its file,
974 * and closes the file.
975 *
976 * Handles all necessary overhead of sorting the primary and
977 * secondary keys, including any externally sorted tmpfiles that
978 * may have been needed for large indices.
979 *
980 * You only <_Write()> once. The open SSI file is closed.
981 * After calling <_Write()>, you should <_Close()> the
982 * <ESL_NEWSSI>.
983 *
984 * Verifies that all primary and secondary keys are unique.
985 * If not, returns <eslEDUP>.
986 *
987 * On any error, the SSI file <ns->ssifile> is deleted.
988 *
989 * Args: <ns> - new SSI index to write
990 *
991 * Returns: <eslOK> on success;
992 * <eslEDUP> if primary or secondary keys aren't all unique
993 * <eslERANGE> if index size exceeds system's maximum file size;
994 * <eslESYS> if any of the steps of an external sort fail.
995 *
996 * Throws: <eslEINVAL> on invalid argument, including too-long tmpfile names,
997 * or trying to _Write() the <ESL_NEWSSI> more than once;
998 * <eslEMEM> on buffer allocation failure;
999 * <eslEWRITE> on any system write failure, including filled disk.
1000 *
1001 * Note: It's O(1) memory to check for key duplications
1002 * here, after keys are sorted, compared to O(N) in
1003 * <esl_newssi_AddKey()>, where we would have to maintain a
1004 * hash of all previous N keys in memory.
1005 */
1006 int
1007 esl_newssi_Write(ESL_NEWSSI *ns)
1008 {
1009 int status, /* convention */
1010 i; /* counter over files, keys */
1011 uint32_t header_flags, /* bitflags in the header */
1012 file_flags, /* bitflags for a file record */
1013 frecsize, /* size of a file record (bytes) */
1014 precsize, /* size of a primary key record (bytes) */
1015 srecsize; /* size of a secondary key record (bytes) */
1016 off_t foffset, /* offset to file section */
1017 poffset, /* offset to primary key section */
1018 soffset; /* offset to secondary key section */
1019 char *fk = NULL, /* fixed-width (flen) file name */
1020 *pk = NULL, /* fixed-width (plen) primary key string */
1021 *sk = NULL, /* fixed-width (slen) secondary key string */
1022 *buf = NULL; /* esl_fgets() growable buffer */
1023 int n = 0; /* esl_fgets() buffer size */
1024 ESL_PKEY pkey; /* primary key info from external tmpfile */
1025 ESL_SKEY skey; /* secondary key info from external tmpfile */
1026
1027 if (ns->nsecondary > 0 && ns->slen == 0)
1028 ESL_EXCEPTION(eslEINVAL, "zero secondary key length: shouldn't happen");
1029 if (ns->ssifp == NULL)
1030 ESL_EXCEPTION(eslEINVAL, "SSI data were already written.");
1031
1032 /* We need fixed-width buffers to get our keys fwrite()'ten in their
1033 * full binary lengths; pkey->key (for instance) is not guaranteed
1034 * to be allocated for the final maximum plen. We use strncpy(), not
1035 * strcpy(), to fill these buffers, because strncpy() pads unused
1036 * bytes as NUL's, and valgrind will flag you if you attempt to
1037 * write uninitialized bytes from these buffers.
1038 */
1039 ESL_ALLOC(fk, sizeof(char) * ns->flen);
1040 ESL_ALLOC(pk, ESL_MAX(1, sizeof(char) * ns->plen));
1041 if (ns->nsecondary > 0) ESL_ALLOC(sk, sizeof(char) * ns->slen);
1042
1043 /* How big is the index? If it's going to be > 2GB, we better have
1044 * 64-bit offsets. (2047 (instead of 2048) gives us
1045 * some slop room.) If not, abort here.
1046 *
1047 * aborting here is pretty brutal - we've processed hundreds of
1048 * millions of keys for nothing. Ah well.
1049 */
1050 if (current_newssi_size(ns) >= 2047 && sizeof(off_t) != 8)
1051 ESL_XFAIL(eslERANGE, ns->errbuf, "SSI index file file would be > 2G; your filesystem isn't capable of handling it");
1052
1053 /* Magic-looking numbers come from adding up sizes
1054 * of things in bytes: they match current_newssi_size().
1055 */
1056 frecsize = 4*sizeof(uint32_t) + ns->flen;
1057 precsize = 2*sizeof(off_t) + sizeof(uint16_t) + sizeof(uint64_t) + ns->plen;
1058 srecsize = ns->slen + ns->plen;
1059 header_flags = 0;
1060
1061 /* Magic-looking numbers again come from adding up sizes
1062 * of things in bytes: matches current_newssi_size()
1063 */
1064 foffset = 9*sizeof(uint32_t)+2*sizeof(uint64_t)+sizeof(uint16_t)+3*sizeof(off_t);
1065 poffset = foffset + frecsize*ns->nfiles;
1066 soffset = poffset + precsize*ns->nprimary;
1067
1068 /* Sort the keys.
1069 * If external mode, make system calls to UNIX/POSIX "sort" in place, then
1070 * open new sorted files for reading thru ptmp and stmp handles.
1071 * If internal mode, call qsort.
1072 *
1073 * Note that you'd better force a POSIX locale for the sort; else,
1074 * some silly distro (e.g. Mandrake Linux >=8.1) may have specified
1075 * LC_COLLATE=en_US, and this'll give a sort "bug" in which it doesn't
1076 * sort by byte order.
1077 */
1078 if (ns->external)
1079 {
1080 char cmd[1024];
1081
1082 if (strlen(ns->ptmpfile) > 256 || strlen(ns->ptmpfile) > 256)
1083 ESL_XEXCEPTION(eslEINVAL, "tmpfile name too long");
1084
1085 fclose(ns->ptmp);
1086 ns->ptmp = NULL;
1087 snprintf(cmd, 1024, "env LC_ALL=POSIX sort -o %s %s\n", ns->ptmpfile, ns->ptmpfile);
1088 if (system(cmd) != 0) ESL_XFAIL(eslESYS, ns->errbuf, "external sort of primary keys failed");
1089 if ((ns->ptmp = fopen(ns->ptmpfile, "r")) == NULL) ESL_XFAIL(eslESYS, ns->errbuf, "failed to reopen primary key tmp file after sort");
1090
1091 fclose(ns->stmp);
1092 ns->stmp = NULL;
1093 snprintf(cmd, 1024, "env LC_ALL=POSIX sort -o %s %s\n", ns->stmpfile, ns->stmpfile);
1094 if (system(cmd) != 0) ESL_XFAIL(eslESYS, ns->errbuf, "external sort of secondary keys failed");
1095 if ((ns->stmp = fopen(ns->stmpfile, "r")) == NULL) ESL_XFAIL(eslESYS, ns->errbuf, "failed to reopen secondary key tmp file after sort");
1096 }
1097 else
1098 {
1099 qsort((void *) ns->pkeys, ns->nprimary, sizeof(ESL_PKEY), pkeysort);
1100 qsort((void *) ns->skeys, ns->nsecondary, sizeof(ESL_SKEY), skeysort);
1101 }
1102
1103 /* Write the header
1104 */
1105 if (esl_fwrite_u32(ns->ssifp, v30magic) != eslOK ||
1106 esl_fwrite_u32(ns->ssifp, header_flags) != eslOK ||
1107 esl_fwrite_u32(ns->ssifp, sizeof(off_t)) != eslOK ||
1108 esl_fwrite_u16(ns->ssifp, ns->nfiles) != eslOK ||
1109 esl_fwrite_u64(ns->ssifp, ns->nprimary) != eslOK ||
1110 esl_fwrite_u64(ns->ssifp, ns->nsecondary)!= eslOK ||
1111 esl_fwrite_u32(ns->ssifp, ns->flen) != eslOK ||
1112 esl_fwrite_u32(ns->ssifp, ns->plen) != eslOK ||
1113 esl_fwrite_u32(ns->ssifp, ns->slen) != eslOK ||
1114 esl_fwrite_u32(ns->ssifp, frecsize) != eslOK ||
1115 esl_fwrite_u32(ns->ssifp, precsize) != eslOK ||
1116 esl_fwrite_u32(ns->ssifp, srecsize) != eslOK ||
1117 esl_fwrite_offset(ns->ssifp, foffset) != eslOK ||
1118 esl_fwrite_offset(ns->ssifp, poffset) != eslOK ||
1119 esl_fwrite_offset(ns->ssifp, soffset) != eslOK)
1120 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed");
1121
1122 /* Write the file section
1123 */
1124 for (i = 0; i < ns->nfiles; i++)
1125 {
1126 file_flags = 0;
1127 if (ns->bpl[i] > 0 && ns->rpl[i] > 0) file_flags |= eslSSI_FASTSUBSEQ;
1128 strncpy(fk, ns->filenames[i], ns->flen);
1129
1130 if (fwrite(fk, sizeof(char), ns->flen, ns->ssifp) != ns->flen ||
1131 esl_fwrite_u32(ns->ssifp, ns->fileformat[i]) != eslOK ||
1132 esl_fwrite_u32(ns->ssifp, file_flags) != eslOK ||
1133 esl_fwrite_u32(ns->ssifp, ns->bpl[i]) != eslOK ||
1134 esl_fwrite_u32(ns->ssifp, ns->rpl[i]) != eslOK)
1135 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed");
1136 }
1137
1138 /* Write the primary key section
1139 */
1140 if (ns->external)
1141 {
1142 if (ns->nprimary) strncpy(pk, "", ns->plen);
1143 for (i = 0; i < ns->nprimary; i++)
1144 {
1145 if (esl_fgets(&buf, &n, ns->ptmp) != eslOK) ESL_XFAIL(eslESYS, ns->errbuf, "read from sorted primary key tmpfile failed");
1146 if (parse_pkey(buf, &pkey) != eslOK) ESL_XFAIL(eslESYS, ns->errbuf, "parse failed for a line of sorted primary key tmpfile failed");
1147 if (strcmp(pk, pkey.key) == 0) ESL_XFAIL(eslEDUP, ns->errbuf, "primary keys not unique: '%s' occurs more than once", pkey.key);
1148 strncpy(pk, pkey.key, ns->plen); // strncpy() pads w/ nulls, and we count on that behavior.
1149
1150 if (fwrite(pk,sizeof(char),ns->plen,ns->ssifp) != ns->plen ||
1151 esl_fwrite_u16( ns->ssifp, pkey.fnum) != eslOK ||
1152 esl_fwrite_offset(ns->ssifp, pkey.r_off) != eslOK ||
1153 esl_fwrite_offset(ns->ssifp, pkey.d_off) != eslOK ||
1154 esl_fwrite_i64( ns->ssifp, pkey.len) != eslOK)
1155 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed");
1156 }
1157 }
1158 else
1159 {
1160 if (ns->nprimary) strncpy(pk, "", ns->plen);
1161 for (i = 0; i < ns->nprimary; i++)
1162 {
1163 if (strcmp(pk, ns->pkeys[i].key) == 0) ESL_XFAIL(eslEDUP, ns->errbuf, "primary keys not unique: '%s' occurs more than once", ns->pkeys[i].key);
1164 strncpy(pk, ns->pkeys[i].key, ns->plen);
1165
1166 if (fwrite(pk,sizeof(char),ns->plen,ns->ssifp) != ns->plen ||
1167 esl_fwrite_u16( ns->ssifp, ns->pkeys[i].fnum) != eslOK ||
1168 esl_fwrite_offset(ns->ssifp, ns->pkeys[i].r_off) != eslOK ||
1169 esl_fwrite_offset(ns->ssifp, ns->pkeys[i].d_off) != eslOK ||
1170 esl_fwrite_i64( ns->ssifp, ns->pkeys[i].len) != eslOK)
1171 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed");
1172 }
1173 }
1174
1175
1176 /* Write the secondary key section
1177 */
1178 if (ns->external)
1179 {
1180 if (ns->nsecondary) strncpy(sk, "", ns->slen);
1181 for (i = 0; i < ns->nsecondary; i++)
1182 {
1183 if (esl_fgets(&buf, &n, ns->stmp) != eslOK) ESL_XFAIL(eslESYS, ns->errbuf, "read from sorted secondary key tmpfile failed");
1184 if (parse_skey(buf, &skey) != eslOK) ESL_XFAIL(eslESYS, ns->errbuf, "parse failed for a line of sorted secondary key tmpfile failed");
1185 if (strcmp(sk, skey.key) == 0) ESL_XFAIL(eslEDUP, ns->errbuf, "secondary keys not unique: '%s' occurs more than once", skey.key);
1186 strncpy(sk, skey.key, ns->slen); // slen > 0 if there are any secondary keys.
1187 strncpy(pk, skey.pkey, ns->plen);
1188
1189 if (fwrite(sk, sizeof(char), ns->slen, ns->ssifp) != ns->slen ||
1190 fwrite(pk, sizeof(char), ns->plen, ns->ssifp) != ns->plen)
1191 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed");
1192 }
1193 }
1194 else
1195 {
1196 /* if ns->nsecondary=0, ns->slen=0 and sk=NULL */
1197 if (ns->nsecondary) strncpy(sk, "", ns->slen);
1198 for (i = 0; i < ns->nsecondary; i++)
1199 {
1200 if (strcmp(sk, ns->skeys[i].key) == 0) ESL_XFAIL(eslEDUP, ns->errbuf, "secondary keys not unique: '%s' occurs more than once", ns->skeys[i].key);
1201 strncpy(sk, ns->skeys[i].key, ns->slen);
1202 strncpy(pk, ns->skeys[i].pkey, ns->plen);
1203
1204 if (fwrite(sk, sizeof(char), ns->slen, ns->ssifp) != ns->slen ||
1205 fwrite(pk, sizeof(char), ns->plen, ns->ssifp) != ns->plen)
1206 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed");
1207 }
1208 }
1209
1210 fclose(ns->ssifp); // Closing <ssifp> makes it so we can only _Write() once.
1211 ns->ssifp = NULL;
1212 if (fk) free(fk);
1213 if (pk) free(pk);
1214 if (sk) free(sk);
1215 if (buf) free(buf);
1216 if (ns->ptmp) { fclose(ns->ptmp); ns->ptmp = NULL; }
1217 if (ns->stmp) { fclose(ns->stmp); ns->stmp = NULL; }
1218 return eslOK;
1219
1220 ERROR:
1221 remove(ns->ssifile); // Cleanup: delete failed <ssifile> on any error.
1222 if (ns->ssifp) { fclose(ns->ssifp); ns->ssifp = NULL; }
1223 if (fk) free(fk);
1224 if (pk) free(pk);
1225 if (sk) free(sk);
1226 if (buf) free(buf);
1227 if (ns->ptmp) { fclose(ns->ptmp); ns->ptmp = NULL; }
1228 if (ns->stmp) { fclose(ns->stmp); ns->stmp = NULL; }
1229 return status;
1230 }
1231
1232 /* Function: esl_newssi_Close()
1233 * Synopsis: Free an <ESL_NEWSSI>.
1234 *
1235 * Purpose: Frees a <ESL_NEWSSI>.
1236 */
1237 void
1238 esl_newssi_Close(ESL_NEWSSI *ns)
1239 {
1240 int i;
1241 if (ns == NULL) return;
1242
1243 if (ns->external == FALSE)
1244 {
1245 if (ns->pkeys != NULL)
1246 {
1247 for (i = 0; i < ns->nprimary; i++)
1248 if (ns->pkeys[i].key != NULL) free(ns->pkeys[i].key);
1249 free(ns->pkeys);
1250 }
1251 if (ns->skeys != NULL)
1252 {
1253 for (i = 0; i < ns->nsecondary; i++)
1254 {
1255 if (ns->skeys[i].key != NULL) free(ns->skeys[i].key);
1256 if (ns->skeys[i].pkey != NULL) free(ns->skeys[i].pkey);
1257 }
1258 free(ns->skeys);
1259 }
1260 }
1261 else
1262 {
1263 remove(ns->ptmpfile);
1264 remove(ns->stmpfile);
1265 }
1266
1267 if (ns->filenames != NULL)
1268 {
1269 for (i = 0; i < ns->nfiles; i++)
1270 if (ns->filenames[i] != NULL) free(ns->filenames[i]);
1271 free(ns->filenames);
1272 }
1273
1274 if (ns->stmp) fclose(ns->stmp);
1275 if (ns->stmpfile) free(ns->stmpfile);
1276 if (ns->ptmp) fclose(ns->ptmp);
1277 if (ns->ptmpfile) free(ns->ptmpfile);
1278 if (ns->fileformat) free(ns->fileformat);
1279 if (ns->bpl) free(ns->bpl);
1280 if (ns->rpl) free(ns->rpl);
1281 if (ns->ssifile) free(ns->ssifile);
1282 if (ns->ssifp) fclose(ns->ssifp);
1283 free(ns);
1284 }
1285
1286
1287
1288
1289 /* current_newssi_size()
1290 *
1291 * Calculates the size of the current index, in megabytes, in
1292 * its disk version (which is essentially the same as the
1293 * RAM it takes, modulo some small overhead for the structures
1294 * and ptrs).
1295 *
1296 * The header costs 10 uint32, 1 uint16, and 3 off_t's: 42 + (12 | 24).
1297 * Each file record costs 4 uint32 and flen chars;
1298 * each primary key costs us 2 off_t, 1 uint16, 1 uint32, and plen chars;
1299 * each sec key costs us plen+slen chars.
1300 */
1301 static int
1302 current_newssi_size(const ESL_NEWSSI *ns)
1303 {
1304 uint64_t frecsize, precsize, srecsize;
1305 uint64_t total;
1306
1307 /* Magic-looking numbers come from adding up sizes
1308 * of things in bytes
1309 */
1310 frecsize = 4*sizeof(uint32_t) + ns->flen;
1311 precsize = 2*sizeof(off_t) + sizeof(uint16_t) + sizeof(uint64_t) + ns->plen;
1312 srecsize = ns->slen + ns->plen;
1313 total = (9*sizeof(uint32_t)+2*sizeof(uint64_t)+sizeof(uint16_t)+3*sizeof(off_t)+
1314 frecsize * ns->nfiles + /* file section size */
1315 precsize * ns->nprimary + /* primary key section size */
1316 srecsize * ns->nsecondary) / /* secondary key section size */
1317 1048576L;
1318 return (int) total;
1319 }
1320
1321 /* activate_external_sort()
1322 *
1323 * Switch to external sort mode.
1324 * Open file handles for external index files (ptmp, stmp).
1325 * Flush current index information to these files.
1326 * Free current memory, turn over control to the tmpfiles.
1327 *
1328 * Return <eslOK> on success;
1329 * <eslENOTFOUND> if we can't open a tmpfile for writing.
1330 *
1331 * Throw <eslEWRITE> if a write fails.
1332 */
1333 static int
1334 activate_external_sort(ESL_NEWSSI *ns)
1335 {
1336 int status;
1337 int i;
1338
1339 if (ns->external) return eslOK; /* we already are external, fool */
1340
1341 if ((ns->ptmp = fopen(ns->ptmpfile, "w")) == NULL) ESL_XFAIL(eslENOTFOUND, ns->errbuf, "Failed to open primary key tmpfile for external sort");
1342 if ((ns->stmp = fopen(ns->stmpfile, "w")) == NULL) ESL_XFAIL(eslENOTFOUND, ns->errbuf, "Failed to open secondary key tmpfile for external sort");
1343
1344 /* Flush the current indices. */
1345 for (i = 0; i < ns->nprimary; i++) {
1346 if (sizeof(off_t) == 4) {
1347 if (fprintf(ns->ptmp, "%s\t%u\t%lu\t%lu\t%lu\n",
1348 ns->pkeys[i].key,
1349 (unsigned int) ns->pkeys[i].fnum,
1350 (unsigned long) ns->pkeys[i].r_off,
1351 (unsigned long) ns->pkeys[i].d_off,
1352 (unsigned long) ns->pkeys[i].len) <= 0)
1353 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi key tmp file write failed");
1354 } else {
1355 if (fprintf(ns->ptmp, "%s\t%u\t%llu\t%llu\t%lu\n",
1356 ns->pkeys[i].key,
1357 (unsigned int) ns->pkeys[i].fnum,
1358 (unsigned long long) ns->pkeys[i].r_off,
1359 (unsigned long long) ns->pkeys[i].d_off,
1360 (unsigned long) ns->pkeys[i].len) <= 0)
1361 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi key tmp file write failed");
1362 }
1363 }
1364 for (i = 0; i < ns->nsecondary; i++)
1365 if (fprintf(ns->stmp, "%s\t%s\n", ns->skeys[i].key, ns->skeys[i].pkey) <= 0)
1366 ESL_XEXCEPTION_SYS(eslEWRITE, "ssi alias tmp file write failed");
1367
1368 /* Free the memory now that we've flushed our lists to disk
1369 */
1370 for (i = 0; i < ns->nprimary; i++) free(ns->pkeys[i].key);
1371 for (i = 0; i < ns->nsecondary; i++) free(ns->skeys[i].key);
1372 for (i = 0; i < ns->nsecondary; i++) free(ns->skeys[i].pkey);
1373 if (ns->pkeys != NULL) free(ns->pkeys);
1374 if (ns->skeys != NULL) free(ns->skeys);
1375 ns->pkeys = NULL;
1376 ns->skeys = NULL;
1377 ns->external = TRUE;
1378 return eslOK;
1379
1380 ERROR:
1381 if (ns->ptmp != NULL) { fclose(ns->ptmp); ns->ptmp = NULL; }
1382 if (ns->stmp != NULL) { fclose(ns->stmp); ns->stmp = NULL; }
1383 return status;
1384 }
1385
1386 /* parse_pkey(), parse_skey()
1387 *
1388 * Given a <buf> containing a line read from the external
1389 * primary-key or secondary-key tmpfile; parse it, and fill in the fields of
1390 * <pkey> or <skey>
1391 *
1392 * <?key> is a ptr to a structure on the stack. It is assumed
1393 * to be in use only transiently.
1394 * <?key>'s strings become ptrs into <buf>'s space, so we don't have to
1395 * allocate new space for them. This means that the transient <?key> structure
1396 * is only usable until <buf> is modified or free'd.
1397 *
1398 * Returns <eslOK> on success.
1399 *
1400 * Throws <eslEFORMAT> on parse error (shouldn't happen; we created it!)
1401 * <eslEINCONCEIVABLE> if we can't deal with off_t's size.
1402 */
1403 static int
1404 parse_pkey(char *buf, ESL_PKEY *pkey)
1405 {
1406 int status;
1407 char *s, *tok;
1408
1409 s = buf;
1410 if (esl_strtok(&s, "\t\n", &(pkey->key)) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed");
1411 if (esl_strtok(&s, "\t\n", &tok) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed");
1412
1413 pkey->fnum = (uint16_t) atoi(tok);
1414 if (esl_strtok(&s, "\t\n", &tok) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed");
1415 if (sizeof(off_t) == 4) pkey->r_off = (off_t) strtoul (tok, NULL, 10);
1416 else if (sizeof(off_t) == 8) pkey->r_off = (off_t) strtoull(tok, NULL, 10);
1417 else ESL_XEXCEPTION(eslEINCONCEIVABLE, "whoa - weird off_t");
1418
1419 if (esl_strtok(&s, "\t\n", &tok) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed");
1420 if (sizeof(off_t) == 4) pkey->d_off = (off_t) strtoul (tok, NULL, 10);
1421 else if (sizeof(off_t) == 8) pkey->d_off = (off_t) strtoull(tok, NULL, 10);
1422 else ESL_XEXCEPTION(eslEINCONCEIVABLE, "whoa - weird off_t");
1423
1424 if (esl_strtok(&s, "\t\n", &tok) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed");
1425 pkey->len = (uint64_t) strtoull(tok, NULL, 10);
1426 return eslOK;
1427
1428 ERROR:
1429 return status;
1430 }
1431 static int
1432 parse_skey(char *buf, ESL_SKEY *skey)
1433 {
1434 int status;
1435 char *s;
1436
1437 s = buf;
1438 if (esl_strtok(&s, "\t\n", &(skey->key)) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed");
1439 if (esl_strtok(&s, "\t\n", &(skey->pkey)) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed");
1440 return eslOK;
1441
1442 ERROR:
1443 return status;
1444 }
1445
1446 /* ordering functions needed for qsort() */
1447 static int
1448 pkeysort(const void *k1, const void *k2)
1449 {
1450 ESL_PKEY *key1;
1451 ESL_PKEY *key2;
1452 key1 = (ESL_PKEY *) k1;
1453 key2 = (ESL_PKEY *) k2;
1454 return strcmp(key1->key, key2->key);
1455 }
1456 static int
1457 skeysort(const void *k1, const void *k2)
1458 {
1459 ESL_SKEY *key1;
1460 ESL_SKEY *key2;
1461 key1 = (ESL_SKEY *) k1;
1462 key2 = (ESL_SKEY *) k2;
1463 return strcmp(key1->key, key2->key);
1464 }
1465
1466
1467 /*****************************************************************
1468 *# 3. Portable binary i/o
1469 *****************************************************************/
1470
1471 /* Function: esl_byteswap()
1472 * Synopsis: Swap between big-endian and little-endian, in place.
1473 *
1474 * Purpose: Swap between big-endian and little-endian, in place.
1475 */
1476 void
1477 esl_byteswap(char *swap, int nbytes)
1478 {
1479 int x;
1480 char byte;
1481
1482 for (x = 0; x < nbytes / 2; x++)
1483 {
1484 byte = swap[nbytes - x - 1];
1485 swap[nbytes - x - 1] = swap[x];
1486 swap[x] = byte;
1487 }
1488 }
1489
1490 /* Function: esl_ntoh16()
1491 * Synopsis: Convert 2-byte integer from network-order to host-order.
1492 *
1493 * Purpose: Convert a 2-byte integer from network-order to host-order,
1494 * and return it.
1495 *
1496 * <esl_ntoh32()> and <esl_ntoh64()> do the same, but for 4-byte
1497 * and 8-byte integers, respectively.
1498 */
1499 uint16_t
1500 esl_ntoh16(uint16_t netshort)
1501 {
1502 #ifdef WORDS_BIGENDIAN
1503 return netshort;
1504 #else
1505 esl_byteswap((char *) &netshort, 2);
1506 return netshort;
1507 #endif
1508 }
1509 uint32_t
1510 esl_ntoh32(uint32_t netlong)
1511 {
1512 #ifdef WORDS_BIGENDIAN
1513 return netlong;
1514 #else
1515 esl_byteswap((char *) &netlong, 4);
1516 return netlong;
1517 #endif
1518 }
1519 uint64_t
1520 esl_ntoh64(uint64_t net_int64)
1521 {
1522 #ifdef WORDS_BIGENDIAN
1523 return net_int64;
1524 #else
1525 esl_byteswap((char *) &net_int64, 8);
1526 return net_int64;
1527 #endif
1528 }
1529
1530 /* Function: esl_hton16()
1531 * Synopsis: Convert 2-byte integer from host-order to network-order.
1532 *
1533 * Purpose: Convert a 2-byte integer from host-order to network-order, and
1534 * return it.
1535 *
1536 * <esl_hton32()> and <esl_hton64()> do the same, but for 4-byte
1537 * and 8-byte integers, respectively.
1538 */
1539 uint16_t
1540 esl_hton16(uint16_t hostshort)
1541 {
1542 #ifdef WORDS_BIGENDIAN
1543 return hostshort;
1544 #else
1545 esl_byteswap((char *) &hostshort, 2);
1546 return hostshort;
1547 #endif
1548 }
1549 uint32_t
1550 esl_hton32(uint32_t hostlong)
1551 {
1552 #ifdef WORDS_BIGENDIAN
1553 return hostlong;
1554 #else
1555 esl_byteswap((char *) &hostlong, 4);
1556 return hostlong;
1557 #endif
1558 }
1559 uint64_t
1560 esl_hton64(uint64_t host_int64)
1561 {
1562 #ifdef WORDS_BIGENDIAN
1563 return host_int64;
1564 #else
1565 esl_byteswap((char *) &host_int64, 8);
1566 return host_int64;
1567 #endif
1568 }
1569
1570
1571 /* Function: esl_fread_u16()
1572 * Synopsis: Read network-order integer from a stream.
1573 *
1574 * Purpose: Read a 2-byte network-order integer from <fp>, convert to
1575 * host order, leave it in <ret_result>.
1576 *
1577 * <esl_fread_u32()> and <esl_fread_u64()> do the same, but
1578 * for 4-byte and 8-byte integers, respectively.
1579 *
1580 * Returns: <eslOK> on success, and <eslFAIL> on <fread()> failure.
1581 */
1582 int
1583 esl_fread_u16(FILE *fp, uint16_t *ret_result)
1584 {
1585 uint16_t result;
1586 if (fread(&result, sizeof(uint16_t), 1, fp) != 1) return eslFAIL;
1587 *ret_result = esl_ntoh16(result);
1588 return eslOK;
1589 }
1590 int
1591 esl_fread_u32(FILE *fp, uint32_t *ret_result)
1592 {
1593 uint32_t result;
1594 if (fread(&result, sizeof(uint32_t), 1, fp) != 1) return eslFAIL;
1595 *ret_result = esl_ntoh32(result);
1596 return eslOK;
1597 }
1598 int
1599 esl_fread_u64(FILE *fp, uint64_t *ret_result)
1600 {
1601 uint64_t result;
1602 if (fread(&result, sizeof(uint64_t), 1, fp) != 1) return eslFAIL;
1603 *ret_result = esl_ntoh64(result);
1604 return eslOK;
1605 }
1606 int
1607 esl_fread_i16(FILE *fp, int16_t *ret_result)
1608 {
1609 int16_t result;
1610 if (fread(&result, sizeof(int16_t), 1, fp) != 1) return eslFAIL;
1611 *ret_result = (int16_t) esl_ntoh16((uint16_t) result);
1612 return eslOK;
1613 }
1614 int
1615 esl_fread_i32(FILE *fp, int32_t *ret_result)
1616 {
1617 int32_t result;
1618 if (fread(&result, sizeof(int32_t), 1, fp) != 1) return eslFAIL;
1619 *ret_result = (int32_t) esl_ntoh32((uint32_t) result);
1620 return eslOK;
1621 }
1622 int
1623 esl_fread_i64(FILE *fp, int64_t *ret_result)
1624 {
1625 int64_t result;
1626 if (fread(&result, sizeof(int64_t), 1, fp) != 1) return eslFAIL;
1627 *ret_result = (int64_t) esl_ntoh64((uint64_t) result);
1628 return eslOK;
1629 }
1630
1631
1632 /* Function: esl_fwrite_u16()
1633 * Synopsis: Write an integer to a stream in network-order.
1634 *
1635 * Purpose: Write a 2-byte host-order integer <n> to stream <fp>
1636 * in network order.
1637 *
1638 * <esl_fwrite_u32()> and <esl_fwrite_u64()> do the same, but
1639 * for 4-byte and 8-byte integers, respectively.
1640 *
1641 * Returns: <eslOK> on success, and <eslFAIL> on <fwrite()> failure.
1642 */
1643 int
1644 esl_fwrite_u16(FILE *fp, uint16_t n)
1645 {
1646 n = esl_hton16(n);
1647 if (fwrite(&n, sizeof(uint16_t), 1, fp) != 1) return eslFAIL;
1648 return eslOK;
1649 }
1650 int
1651 esl_fwrite_u32(FILE *fp, uint32_t n)
1652 {
1653 n = esl_hton32(n);
1654 if (fwrite(&n, sizeof(uint32_t), 1, fp) != 1) return eslFAIL;
1655 return eslOK;
1656 }
1657 int
1658 esl_fwrite_u64(FILE *fp, uint64_t n)
1659 {
1660 n = esl_hton64(n);
1661 if (fwrite(&n, sizeof(uint64_t), 1, fp) != 1) return eslFAIL;
1662 return eslOK;
1663 }
1664 int
1665 esl_fwrite_i16(FILE *fp, int16_t n)
1666 {
1667 n = (int16_t) esl_hton16((uint16_t) n);
1668 if (fwrite(&n, sizeof(int16_t), 1, fp) != 1) return eslFAIL;
1669 return eslOK;
1670 }
1671 int
1672 esl_fwrite_i32(FILE *fp, int32_t n)
1673 {
1674 n = (int32_t) esl_hton32((uint32_t) n);
1675 if (fwrite(&n, sizeof(int32_t), 1, fp) != 1) return eslFAIL;
1676 return eslOK;
1677 }
1678 int
1679 esl_fwrite_i64(FILE *fp, int64_t n)
1680 {
1681 n = (int64_t) esl_hton64((uint64_t) n);
1682 if (fwrite(&n, sizeof(int64_t), 1, fp) != 1) return eslFAIL;
1683 return eslOK;
1684 }
1685
1686
1687 /* Function: esl_fread_offset()
1688 * Synopsis: Read an offset portably.
1689 *
1690 * Purpose: Read a file offset from the stream <fp> (which would usually
1691 * be a save file), and store it in <ret_offset>.
1692 *
1693 * Offsets may have been saved by a different machine
1694 * than the machine that reads them. The writer and the reader
1695 * may differ in byte order and in width (<sizeof(off_t)>).
1696 *
1697 * Byte order is dealt with by saving offsets in
1698 * network byte order, and converting them to host byte order
1699 * when they are read (if necessary).
1700 *
1701 * Width is dealt with by the <sz> argument, which must be
1702 * either 4 or 8, specifying that the saved offset is a
1703 * 32-bit versus 64-bit <off_t>. If the reading host
1704 * <off_t> width matches the <sz> of the writer, no
1705 * problem. If <sz> is 4 but the reading host has 64-bit
1706 * <off_t>'s, this is also no problem; the conversion
1707 * always works. If <sz> is 64 but the reading host has
1708 * only 32-bit <off_t>, we cannot guarantee that we have
1709 * sufficient dynamic range to represent the offset; if
1710 * the stored offset is too large to represent in a 32-bit
1711 * offset, we throw a fatal <eslEINCOMPAT> error.
1712 *
1713 * Returns: <eslOK> on success; <eslFAIL> on a read failure.
1714 *
1715 * Throws: <eslEINVAL> if <sz> is something other than 4 or 8;
1716 * <eslEINCOMPAT> if the stored offset is too large for
1717 * the reader to represent (the machine that wrote the
1718 * SSI file used 64 bit offsets, the reader uses 32
1719 * bit offsets, and this offset is too large to represent
1720 * in a 32 bit offset).
1721 */
1722 int
1723 esl_fread_offset(FILE *fp, int sz, off_t *ret_offset)
1724 {
1725 int status;
1726 uint32_t x32;
1727 uint64_t x64;
1728
1729 if (sz == 8)
1730 {
1731 if (esl_fread_u64(fp, &x64) != eslOK) { status = eslFAIL; goto ERROR; }
1732 if (sizeof(off_t) == 4 && x64 > INT32_MAX)
1733 ESL_XEXCEPTION(eslEINCOMPAT, "can't read 64-bit off_t on this 32-bit host");
1734 *ret_offset = (off_t) x64;
1735 }
1736 else if (sz == 4)
1737 {
1738 if (esl_fread_u32(fp, &x32) != eslOK) { status = eslFAIL; goto ERROR; }
1739 *ret_offset = (off_t) x32;
1740 }
1741 else ESL_XEXCEPTION(eslEINVAL, "offsets must be 32 or 64 bits");
1742 return eslOK;
1743
1744 ERROR:
1745 *ret_offset = 0;
1746 return status;
1747 }
1748
1749 /* Function: esl_fwrite_offset()
1750 * Synopsis: Write an offset portably.
1751 *
1752 * Purpose: Portably write (save) <offset> to the stream <fp>, in network
1753 * byte order.
1754 *
1755 * Returns: <eslOK> on success; <eslFAIL> on write failure.
1756 *
1757 * Throws: <eslEINVAL> if <off_t> is something other than a 32-bit or
1758 * 64-bit integer on this machine, in which case we don't know
1759 * how to deal with it portably.
1760 */
1761 int
1762 esl_fwrite_offset(FILE *fp, off_t offset)
1763 {
1764 if (sizeof(off_t) == 4) return esl_fwrite_u32(fp, offset);
1765 else if (sizeof(off_t) == 8) return esl_fwrite_u64(fp, offset);
1766 else ESL_EXCEPTION(eslEINVAL, "off_t is neither 32-bit nor 64-bit");
1767 /*UNREACHED*/
1768 return eslEINCONCEIVABLE;
1769 }
1770
1771
1772 /*****************************************************************
1773 * 5. Unit tests
1774 *****************************************************************/
1775 #ifdef eslSSI_TESTDRIVE
1776
1777 #include "esl_arr2.h"
1778 #include "esl_getopts.h"
1779 #include "esl_sq.h"
1780 #include "esl_sqio.h"
1781 #include "esl_random.h"
1782 #include "esl_randomseq.h"
1783
1784 struct ssi_testdata {
1785 int nfiles; // generate this many files...
1786 int nseq; // ... with this many sequences per file ...
1787 int maxL; // ... with sequence lengths 1..maxL.
1788
1789 char **sqfile; // seq file names, [0..nfiles-1]
1790 char **seqname; // seq names, [0..nfiles*nseq-1] = "seq%d-file%d"
1791 char **seqdesc; // seq descriptions, [0..nfiles*nseq-1] = "desc%d-file%d", used as secondary keys
1792 int *seqlen; // seq lengths, [0..nfiles*nseq-1]
1793 char **seq;
1794 };
1795
1796 static struct ssi_testdata *
1797 ssi_testdata_create(ESL_RANDOMNESS *rng, int max_nfiles, int max_nseq, int maxL, int do_dupname)
1798 {
1799 char msg[] = "esl_ssi test data creation failed";
1800 struct ssi_testdata *td;
1801 double p[4] = { 0.25, 0.25, 0.25, 0.25 }; // composition of random generated DNA seqs
1802 FILE *fp;
1803 ESL_SQ *sq;
1804 int i,j;
1805
1806 if ( (td = malloc(sizeof(struct ssi_testdata))) == NULL) esl_fatal(msg);
1807 td->nfiles = 1 + esl_rnd_Roll(rng, max_nfiles); // 1..max_nfiles
1808 td->nseq = 2 + esl_rnd_Roll(rng, max_nseq-1); // 2..max_nseq Need at least 2 for dup test to always be valid
1809 td->maxL = maxL;
1810
1811 /* Create <td->nfiles> sequence tmpfile names. */
1812 if ( (td->sqfile = malloc(sizeof(char *) * td->nfiles)) == NULL) esl_fatal(msg);
1813 for (j = 0; j < td->nfiles; j++)
1814 if ( esl_sprintf(&(td->sqfile[j]), "esltmpXXXXXX" ) != eslOK) esl_fatal(msg);
1815
1816 /* Create <td->nfiles*td->nseq> sequences with random lengths up to <maxL> */
1817 if ( (td->seq = malloc(sizeof(char *) * td->nseq * td->nfiles)) == NULL) esl_fatal(msg);
1818 if ( (td->seqname = malloc(sizeof(char *) * td->nseq * td->nfiles)) == NULL) esl_fatal(msg);
1819 if ( (td->seqlen = malloc(sizeof(int) * td->nseq * td->nfiles)) == NULL) esl_fatal(msg);
1820 if ( (td->seqdesc = malloc(sizeof(char *) * td->nseq * td->nfiles)) == NULL) esl_fatal(msg);
1821 for (i = 0; i < td->nseq*td->nfiles; i++)
1822 {
1823 td->seqlen[i] = 1 + esl_rnd_Roll(rng, td->maxL); /* 1..maxL */
1824 if ( (td->seq[i] = malloc(sizeof(char) * (td->seqlen[i]+1))) == NULL) esl_fatal(msg);
1825 if ( esl_rsq_IID(rng, "ACGT", p, 4, td->seqlen[i], td->seq[i]) != eslOK) esl_fatal(msg);
1826 if ( esl_sprintf(&(td->seqname[i]), "seq%d-file%d", i, i/td->nseq) != eslOK) esl_fatal(msg);
1827 if ( esl_sprintf(&(td->seqdesc[i]), "desc%d-file%d", i, i/td->nseq) != eslOK) esl_fatal(msg);
1828 }
1829
1830 /* If we were asked to poison with a duplicate key, do it (randomly duplicating a primary or secondary key) */
1831 if (do_dupname)
1832 {
1833 i = esl_rnd_Roll(rng, td->nseq * td->nfiles - 1); // 0..n-2
1834 j = i + 1 + esl_rnd_Roll(rng, td->nseq * td->nfiles - i - 1); // i+1..n-1
1835 if (esl_rnd_Roll(rng, 2) == 0) {
1836 strcpy(td->seqname[i], "DUP"); // Allocated space is guaranteed to be enough,
1837 strcpy(td->seqname[j], "DUP"); // because the original name was "seq%d-file%d"
1838 } else {
1839 strcpy(td->seqdesc[i], "DUP");
1840 strcpy(td->seqdesc[j], "DUP");
1841 }
1842 }
1843
1844 /* Save them to files. */
1845 for (j = 0; j < td->nfiles; j++)
1846 {
1847 if (esl_tmpfile_named(td->sqfile[j], &fp) != eslOK) esl_fatal(msg);
1848 for (i = j*td->nseq; i < (j+1)*td->nseq; i++)
1849 {
1850 if ( (sq = esl_sq_CreateFrom(td->seqname[i], td->seq[i], td->seqdesc[i], NULL, NULL)) == NULL) esl_fatal(msg);
1851 if ( esl_sqio_Write(fp, sq, eslSQFILE_FASTA, FALSE) != eslOK) esl_fatal(msg);
1852 esl_sq_Destroy(sq);
1853 }
1854 fclose(fp);
1855 }
1856
1857 return td;
1858 }
1859
1860 static void
1861 ssi_testdata_destroy(struct ssi_testdata *td)
1862 {
1863 int j;
1864 for (j = 0; j < td->nfiles; j++) remove(td->sqfile[j]);
1865 esl_arr2_Destroy((void **) td->sqfile, td->nfiles);
1866 esl_arr2_Destroy((void **) td->seqname, td->nseq*td->nfiles);
1867 esl_arr2_Destroy((void **) td->seqdesc, td->nseq*td->nfiles);
1868 esl_arr2_Destroy((void **) td->seq, td->nseq*td->nfiles);
1869 free(td->seqlen);
1870 free(td);
1871 }
1872
1873 static void
1874 utest_enchilada(ESL_GETOPTS *go, ESL_RANDOMNESS *rng, int do_external, int do_dupkeys)
1875 {
1876 char msg[] = "esl_ssi whole enchilada test failed";
1877 struct ssi_testdata *td = NULL;
1878 int nq = 10; // number of SSI-based retrievals to test
1879 char *ssifile = NULL; // Index creation: ssifile to create.
1880 ESL_NEWSSI *ns = NULL; // new SSI index data
1881 ESL_SQFILE *sqfp = NULL; // open FASTA file that we're indexing
1882 ESL_SQ *sq = NULL; // a sequence from <sqfp>
1883 uint16_t fh; // handle on an indexed fasta file, from _AddFile, for _AddKey
1884 ESL_SSI *ssi = NULL; // Retrieval testing: open SSI index to use
1885 char query[32]; // name of sequence to retrieve. The 32 is also used as a size by sprintf() calls below.
1886 char *qfile; // retrieved name of file it's in
1887 int qfmt; // retrieved format of that file (fasta)
1888 off_t roff; // retrieved record offset of it
1889 int i,j;
1890 int status;
1891
1892 td = ssi_testdata_create(rng,
1893 esl_opt_GetInteger(go, "-F"), // max # of seq files
1894 esl_opt_GetInteger(go, "-N"), // max # of seqs per file
1895 esl_opt_GetInteger(go, "-L"), // max seq length
1896 do_dupkeys); // if you poison w/ dup keys, _Write should fail.
1897
1898 /* Create an ssi index of all the FASTA files. */
1899 if (esl_strdup(td->sqfile[0], -1, &ssifile) != eslOK) esl_fatal(msg);
1900 if (esl_strcat(&ssifile, -1, ".ssi", 4) != eslOK) esl_fatal(msg);
1901 if (esl_newssi_Open(ssifile, TRUE, &ns) != eslOK) esl_fatal(msg);
1902 if ((sq = esl_sq_Create()) == NULL) esl_fatal(msg);
1903 if (do_external)
1904 if (activate_external_sort(ns) != eslOK) esl_fatal(msg);
1905 for (j = 0; j < td->nfiles; j++)
1906 {
1907 if (esl_sqfile_Open(td->sqfile[j], eslSQFILE_UNKNOWN, NULL, &sqfp) != eslOK) esl_fatal(msg);
1908 if (esl_newssi_AddFile(ns, td->sqfile[j], sqfp->format, &fh) != eslOK) esl_fatal(msg);
1909 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK)
1910 {
1911 if (esl_newssi_AddKey (ns, sq->name, fh, sq->roff, sq->doff, sq->L) != eslOK) esl_fatal(msg);
1912 if (esl_newssi_AddAlias(ns, sq->desc, sq->name) != eslOK) esl_fatal(msg);
1913 esl_sq_Reuse(sq);
1914 }
1915 if (status != eslEOF) esl_fatal(msg);
1916 esl_sqfile_Close(sqfp);
1917 }
1918 esl_sq_Destroy(sq);
1919
1920 /* Save the SSI index to a file. */
1921 status = esl_newssi_Write(ns);
1922 if ( do_dupkeys && status != eslEDUP) esl_fatal(msg);
1923 if (! do_dupkeys && status != eslOK) esl_fatal(msg);
1924 esl_newssi_Close(ns);
1925
1926 /* Open the SSI index - now we'll use it to retrieve <nq> random sequences. */
1927 if (! do_dupkeys)
1928 {
1929 if (esl_ssi_Open(ssifile, &ssi) != eslOK) esl_fatal(msg);
1930 sq = esl_sq_Create();
1931 while (nq--)
1932 {
1933 /* Choose a seq and file */
1934 i = esl_rnd_Roll(rng, td->nseq*td->nfiles);
1935 j = i/td->nseq;
1936 if (esl_rnd_Roll(rng, 2) == 0) snprintf(query, 32, "seq%d-file%d", i, j); // by primary key. 32 is the alloc size for query[]
1937 else snprintf(query, 32, "desc%d-file%d", i, j); // by secondary key
1938
1939 /* Retrieve it */
1940 if ( esl_ssi_FindName(ssi, query, &fh, &roff, NULL, NULL) != eslOK) esl_fatal(msg);
1941 if ( esl_ssi_FileInfo(ssi, fh, &qfile, &qfmt) != eslOK) esl_fatal(msg);
1942 if ( esl_sqfile_Open(qfile, qfmt, NULL, &sqfp) != eslOK) esl_fatal(msg);
1943 if ( esl_sqfile_Position(sqfp, roff) != eslOK) esl_fatal(msg);
1944 if ( esl_sqio_Read(sqfp, sq) != eslOK) esl_fatal(msg);
1945
1946 /* Check that it's the right one */
1947 if (strcmp(sq->name, query) != 0 && strcmp(sq->desc, query) != 0) esl_fatal(msg);
1948 if (sq->n != td->seqlen[i]) esl_fatal(msg);
1949 if (strcmp(sq->seq, td->seq[i]) != 0) esl_fatal(msg);
1950 if (strcmp(qfile, td->sqfile[j]) != 0) esl_fatal(msg);
1951
1952 esl_sq_Reuse(sq);
1953 esl_sqfile_Close(sqfp);
1954 }
1955 remove(ssifile); // in the dup keys test, ssifile is removed by _Write().
1956 esl_sq_Destroy(sq);
1957 esl_ssi_Close(ssi);
1958 }
1959
1960 ssi_testdata_destroy(td);
1961 free(ssifile);
1962 }
1963 #endif /*eslSSI_TESTDRIVE*/
1964
1965
1966 /*****************************************************************
1967 * 5. Test driver
1968 *****************************************************************/
1969
1970 #ifdef eslSSI_TESTDRIVE
1971 #include <esl_config.h>
1972
1973 #include <stdlib.h>
1974 #include <stdio.h>
1975 #include <string.h>
1976
1977 #include "easel.h"
1978 #include "esl_getopts.h"
1979 #include "esl_sq.h"
1980 #include "esl_sqio.h"
1981 #include "esl_ssi.h"
1982 #include "esl_random.h"
1983 #include "esl_randomseq.h"
1984
1985 static ESL_OPTIONS options[] = {
1986 /* name type default env range toggles reqs incomp help docgroup*/
1987 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
1988 { "-F", eslARG_INT, "3", NULL, NULL, NULL, NULL, NULL, "max number of test files", 0 },
1989 { "-L", eslARG_INT, "1000", NULL, NULL, NULL, NULL, NULL, "max length of test sequences", 0 },
1990 { "-N", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "max number of test sequences per file", 0 },
1991 { "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
1992 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1993 };
1994 static char usage[] = "[-options]";
1995 static char banner[] = "test driver for ssi module";
1996
1997 int
1998 main(int argc, char **argv)
1999 {
2000 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
2001 ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));
2002
2003 fprintf(stderr, "## %s\n", argv[0]);
2004 fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng));
2005
2006 /* do_external do_dupkeys */
2007 utest_enchilada(go, rng, FALSE, FALSE);
2008 utest_enchilada(go, rng, TRUE, FALSE);
2009 utest_enchilada(go, rng, FALSE, TRUE);
2010 utest_enchilada(go, rng, TRUE, TRUE);
2011
2012 esl_randomness_Destroy(rng);
2013 esl_getopts_Destroy(go);
2014
2015 fprintf(stderr, "# status = ok\n");
2016 return eslOK;
2017 }
2018 #endif /*eslSSI_TESTDRIVE*/
2019
2020
2021
2022 /*****************************************************************
2023 * 5. Example code.
2024 ****************************************************************/
2025 #ifdef eslSSI_EXAMPLE
2026 /* gcc -o example -g -Wall -DeslSSI_EXAMPLE esl_ssi.c easel.c
2027 * esl-shuffle -o foo.fa -N 1000 -G --amino -L 400
2028 * ./example foo.fa
2029 */
2030 /*::cexcerpt::ssi_example::begin::*/
2031 #include <stdio.h>
2032 #include "easel.h"
2033 #include "esl_ssi.h"
2034
2035 int
2036 main(int argc, char **argv)
2037 {
2038 ESL_NEWSSI *ns;
2039 char *fafile; /* name of FASTA file */
2040 FILE *fp; /* opened FASTA file for reading */
2041 char *ssifile; /* name of SSI file */
2042 uint16_t fh; /* file handle SSI associates w/ fafile */
2043 char *buf = NULL; /* growable buffer for esl_fgets() */
2044 int n = 0; /* length of buf */
2045 char *s, *seqname;
2046 off_t seq_offset;
2047 int status;
2048
2049 /* Open a new SSI index named <fafile>.ssi */
2050 fafile = argv[1];
2051 esl_strdup(fafile, -1, &ssifile);
2052 esl_strcat(&ssifile, -1, ".ssi", 4);
2053 status = esl_newssi_Open(ssifile, FALSE, &ns);
2054 if (status == eslENOTFOUND) esl_fatal("failed to open SSI index %s", ssifile);
2055 else if (status == eslEOVERWRITE) esl_fatal("SSI index %s already exists; delete or rename it", ssifile);
2056 else if (status != eslOK) esl_fatal("failed to create a new SSI index");
2057
2058 /* Collect the sequence names from a FASTA file into an index */
2059 if ((fp = fopen(fafile, "r")) == NULL) esl_fatal("failed to open %s", fafile);
2060 if (esl_newssi_AddFile(ns, fafile, 1, &fh) != eslOK) esl_fatal("failed to add %s to index: %s", fafile, ns->errbuf);
2061 seq_offset = ftello(fp);
2062 while (esl_fgets(&buf, &n, fp) == eslOK)
2063 {
2064 if (*buf == '>') {
2065 s = buf+1; /* skip past > */
2066 esl_strtok(&s, " \t\n", &seqname); /* name = 1st token on > line */
2067 if (esl_newssi_AddKey(ns, seqname, fh, seq_offset, 0, 0) != eslOK)
2068 esl_fatal("failed to add key %s to index: %s", seqname, ns->errbuf);
2069 }
2070 seq_offset = ftello(fp);
2071 }
2072 free(buf);
2073 fclose(fp);
2074
2075 /* Save the index to disk */
2076 status = esl_newssi_Write(ns);
2077 if (status == eslEDUP) esl_fatal("SSI index construction failed:\n %s", ns->errbuf);
2078 else if (status == eslERANGE) esl_fatal("SSI index file size exceeds maximum allowed by your filesystem");
2079 else if (status == eslESYS) esl_fatal("SSI index sort failed:\n %s", ns->errbuf);
2080 else if (status != eslOK) esl_fatal("SSI indexing failed:\n %s", ns->errbuf);
2081 esl_newssi_Close(ns);
2082 free(ssifile);
2083 return 0;
2084 }
2085 /*::cexcerpt::ssi_example::end::*/
2086 #endif /*eslSSI_EXAMPLE*/
2087
2088
2089 #ifdef eslSSI_EXAMPLE2
2090 /* gcc -o example2 -g -Wall -DeslSSI_EXAMPLE2 esl_ssi.c easel.c
2091 * ./example2 random77 foo.fa.ssi
2092 */
2093 /*::cexcerpt::ssi_example2::begin::*/
2094 #include <stdio.h>
2095 #include "easel.h"
2096 #include "esl_ssi.h"
2097
2098 int main(int argc, char **argv)
2099 {
2100 ESL_SSI *ssi;
2101 char *seqname; /* name of sequence to retrieve */
2102 char *ssifile; /* name of SSI file */
2103 uint16_t fh; /* file handle SSI associates w/ fafile */
2104 char *fafile; /* name of FASTA file */
2105 int fmt; /* format code (1, in this example) */
2106 off_t offset; /* disk offset of seqname in fafile */
2107 FILE *fp; /* opened FASTA file for reading */
2108 char *buf = NULL; /* growable buffer for esl_fgets() */
2109 int n = 0; /* size of buffer */
2110
2111 seqname = argv[1];
2112 ssifile = argv[2];
2113
2114 if (esl_ssi_Open(ssifile, &ssi) != eslOK) esl_fatal("open failed");
2115 if (esl_ssi_FindName(ssi, seqname, &fh, &offset, NULL, NULL) != eslOK) esl_fatal("find failed");
2116 if (esl_ssi_FileInfo(ssi, fh, &fafile, &fmt) != eslOK) esl_fatal("info failed");
2117 /* you can't close the ssi file yet - fafile is pointing into it! */
2118
2119 if ((fp = fopen(fafile, "r")) == NULL) esl_fatal("failed to open %s", fafile);
2120 if (fseeko(fp, offset, SEEK_SET) != 0) esl_fatal("failed to position %s", fafile);
2121 if (esl_fgets(&buf, &n, fp) != eslOK) esl_fatal("failed to get name/desc line");
2122 do {
2123 printf("%s", buf);
2124 } while (esl_fgets(&buf, &n, fp) == eslOK && *buf != '>');
2125
2126 esl_ssi_Close(ssi);
2127 fclose(fp);
2128 free(buf);
2129 return 0;
2130 }
2131 /*::cexcerpt::ssi_example2::end::*/
2132 #endif /*eslSSI_EXAMPLE2*/
2133