Debian Codegraph @

hmmer/easel/esl_sqio_ncbi.c

raw symbols SCIP
1 /* Unaligned ncbi sequence file i/o.
2 *
3 * Contents:
4 * 1. An <ESL_SQFILE> object, in text mode.
5 * 2. An <ESL_SQFILE> object, in digital mode. [with <alphabet>]
6 * 3. Miscellaneous routines.
7 * 4. Sequence reading (sequential).
8 * 5. Parsing routines
9 */
10 #include <esl_config.h>
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
19 #endif
20
21 #ifdef HAVE_ENDIAN_H
22 #include <endian.h>
23 #endif
24
25 #include "easel.h"
26 #include "esl_alphabet.h" /* alphabet aug adds digital sequences */
27 #include "esl_sq.h"
28 #include "esl_sqio.h"
29
30 #ifndef htobe32
31 #ifdef WORDS_BIGENDIAN
32 #define htobe32(x) (x)
33 #else
34 #define htobe32(x) \
35 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
36 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
37 #endif
38 #endif
39
40 /* format specific routines */
41 static int sqncbi_Position (ESL_SQFILE *sqfp, off_t offset);
42 static void sqncbi_Close (ESL_SQFILE *sqfp);
43 static int sqncbi_SetDigital (ESL_SQFILE *sqfp, const ESL_ALPHABET *abc);
44 static int sqncbi_GuessAlphabet (ESL_SQFILE *sqfp, int *ret_type);
45 static int sqncbi_Read (ESL_SQFILE *sqfp, ESL_SQ *sq);
46 static int sqncbi_ReadInfo (ESL_SQFILE *sqfp, ESL_SQ *sq);
47 static int sqncbi_ReadSequence (ESL_SQFILE *sqfp, ESL_SQ *sq);
48 static int sqncbi_ReadWindow (ESL_SQFILE *sqfp, int C, int W, ESL_SQ *sq);
49 static int sqncbi_ReadBlock (ESL_SQFILE *sqfp, ESL_SQ_BLOCK *sqBlock, int max_residues, int max_sequences, int max_init_window, int long_target);
50 static int sqncbi_Echo (ESL_SQFILE *sqfp, const ESL_SQ *sq, FILE *ofp);
51
52 static int sqncbi_IsRewindable (const ESL_SQFILE *sqfp);
53 static const char *sqncbi_GetError (const ESL_SQFILE *sqfp);
54
55 /* common routines for processing ncbi database */
56 static int sqncbi_Open (ESL_SQNCBI_DATA *ncbi, char *filename);
57
58 static void reset_db (ESL_SQNCBI_DATA *ncbi);
59 static int pos_sequence (ESL_SQNCBI_DATA *ncbi, int inx);
60 static int volume_open (ESL_SQNCBI_DATA *ncbi, int volume);
61 static void reset_header_values (ESL_SQNCBI_DATA *ncbi);
62
63 static int read_amino (ESL_SQFILE *sqfp, ESL_SQ *sq);
64 static int read_dna (ESL_SQFILE *sqfp, ESL_SQ *sq);
65 static int read_nres_amino (ESL_SQFILE *sqfp, ESL_SQ *sq, int len, uint64_t *nres);
66 static int read_nres_dna (ESL_SQFILE *sqfp, ESL_SQ *sq, int len, uint64_t *nres);
67
68 static int inmap_ncbi (ESL_SQFILE *sqfp);
69 static int inmap_ncbi_amino (ESL_SQFILE *sqfp);
70 static int inmap_ncbi_dna (ESL_SQFILE *sqfp);
71
72 /* parsing routines */
73 static int parse_header (ESL_SQNCBI_DATA *ncbi, ESL_SQ *sq);
74 static int parse_def_line (ESL_SQNCBI_DATA *ncbi, ESL_SQ *sq);
75 static int parse_seq_id (ESL_SQNCBI_DATA *ncbi);
76 static int parse_textseq_id (ESL_SQNCBI_DATA *ncbi);
77 static int parse_object_id (ESL_SQNCBI_DATA *ncbi);
78 static int parse_dbtag (ESL_SQNCBI_DATA *ncbi);
79 static int parse_patent_seq_id (ESL_SQNCBI_DATA *ncbi);
80 static int parse_giimport_id (ESL_SQNCBI_DATA *ncbi);
81 static int parse_id_pat (ESL_SQNCBI_DATA *ncbi);
82 static int parse_pdb_seq_id (ESL_SQNCBI_DATA *ncbi);
83 static int parse_date_std (ESL_SQNCBI_DATA *ncbi);
84 static int parse_string (ESL_SQNCBI_DATA *ncbi, char **str, int *len);
85 static int parse_integer (ESL_SQNCBI_DATA *ncbi, int *value);
86 static int ignore_sequence_of_integer(ESL_SQNCBI_DATA *ncbi);
87
88 #define INDEX_TABLE_SIZE 1024
89 #define INIT_HDR_BUFFER_SIZE 2048
90
91 #define NCBI_VERSION_4 4
92 #define NCBI_DNA_DB 0
93 #define NCBI_AMINO_DB 1
94
95 /*****************************************************************
96 *# 1. An <ESL_SQFILE> object, in text mode.
97 *****************************************************************/
98
99 /* Function: esl_sqncbi_Open()
100 * Synopsis: Open a sequence file for reading.
101 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
102 *
103 * Purpose: Open a sequence file <filename> for reading.
104 * The opened <ESL_SQFILE> is returned through <ret_sqfp>.
105 *
106 * The .pin, .phr and .psq files are required for the
107 * open function to succeed. Only ncbi version 4
108 * databases are currently supported.
109 *
110 * Returns: <eslOK> on success, and <*ret_sqfp> points to a new
111 * open <ESL_SQFILE>. Caller deallocates this object with
112 * <esl_sqfile_Close()>.
113 *
114 * Returns <eslENOTFOUND> if <filename> can't be found or
115 * opened. Returns <eslEFORMAT> if the file is empty, or
116 * if autodetection is attempted and the format can't be
117 * determined. On any error condition, <*ret_sqfp> is
118 * returned NULL.
119 *
120 * Throws: <eslEMEM> on allocation failure.
121 */
122 int
123 esl_sqncbi_Open(char *filename, int format, ESL_SQFILE *sqfp)
124 {
125 int i;
126 int status = eslOK; /* return status from an ESL call */
127
128 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
129
130 /* before we go any further, make sure we can handle the format */
131 if (format != eslSQFILE_NCBI && format != eslSQFILE_UNKNOWN) return eslENOTFOUND;
132
133 ncbi->fppin = NULL;
134 ncbi->fpphr = NULL;
135 ncbi->fppsq = NULL;
136
137 ncbi->title = NULL;
138 ncbi->timestamp = NULL;
139
140 ncbi->index = 0;
141
142 ncbi->hdr_off = -1;
143 ncbi->seq_off = -1;
144 ncbi->amb_off = -1;
145
146 ncbi->index_start = -1;
147 ncbi->index_end = -1;
148 ncbi->hdr_indexes = NULL;
149 ncbi->seq_indexes = NULL;
150 ncbi->amb_indexes = NULL;
151
152 ncbi->hdr_buf = NULL;
153 reset_header_values(ncbi);
154
155 ncbi->amb_off = 0;
156
157 ncbi->alphatype = eslUNKNOWN;
158 ncbi->alphasym = NULL;
159
160 ncbi->vol_index = -1;
161 ncbi->volumes = 0;
162
163 for (i = 0; i < MAX_DB_VOLUMES; ++i) {
164 ncbi->vols[i].name = NULL;
165 ncbi->vols[i].start_seq = -1;
166 ncbi->vols[i].end_seq = -1;
167 }
168
169 if ((status = sqncbi_Open(ncbi, filename)) != eslOK) goto ERROR;
170
171 sqfp->format = eslSQFILE_NCBI;
172 if ((status = inmap_ncbi(sqfp)) != eslOK) goto ERROR;
173
174 /* initialize the function pointers for the ncbi routines */
175 sqfp->position = &sqncbi_Position;
176 sqfp->close = &sqncbi_Close;
177
178 sqfp->set_digital = &sqncbi_SetDigital;
179 sqfp->guess_alphabet = &sqncbi_GuessAlphabet;
180
181 sqfp->is_rewindable = &sqncbi_IsRewindable;
182
183 sqfp->read = &sqncbi_Read;
184 sqfp->read_info = &sqncbi_ReadInfo;
185 sqfp->read_seq = &sqncbi_ReadSequence;
186 sqfp->read_window = &sqncbi_ReadWindow;
187 sqfp->echo = &sqncbi_Echo;
188
189 sqfp->read_block = &sqncbi_ReadBlock;
190
191 sqfp->get_error = &sqncbi_GetError;
192
193 return eslOK;
194
195 ERROR:
196 sqncbi_Close(sqfp);
197 return status;
198 }
199
200 /* sqncbi_ParseIndexFile()
201 *
202 * Parse an open index file verifying database type and version
203 * if handled. Read in the entries, i.e. name, number of sequences,
204 * etc. Keep track of the offsets where the header and sequence
205 * tables begin.
206 */
207 static int
208 sqncbi_ParseIndexFile(ESL_SQNCBI_DATA *ncbi, int dbtype)
209 {
210 int len;
211 uint32_t info[4];
212 int status = eslOK; /* return status from an ESL call */
213
214 if (fread(&info[0], sizeof(uint32_t), 3, ncbi->fppin) != 3) status = eslFAIL;
215 if (htobe32(info[0]) != NCBI_VERSION_4) status = eslEFORMAT;
216 if (htobe32(info[1]) != dbtype) status = eslEUNIMPLEMENTED;
217
218 if (status != eslOK) goto ERROR;
219 ncbi->version = htobe32(info[0]);
220 ncbi->alphatype = (dbtype == NCBI_DNA_DB) ? eslDNA : eslAMINO;
221 ncbi->index = 0;
222
223 /* read the database title */
224 len = htobe32(info[2]);
225 ESL_ALLOC(ncbi->title, sizeof(char) * (len + 1));
226 if (fread(ncbi->title, sizeof(char), len, ncbi->fppin) != len) { status = eslFAIL; goto ERROR; }
227 ncbi->title[len] = 0;
228
229 /* read the database time stamp */
230 if (fread(&info[0], sizeof(uint32_t), 1, ncbi->fppin) != 1) { status = eslFAIL; goto ERROR; }
231 len = htobe32(info[0]);
232 ESL_ALLOC(ncbi->timestamp, sizeof(char) * (len + 1));
233 if (fread(ncbi->timestamp, sizeof(char), len, ncbi->fppin) != len) { status = eslFAIL; goto ERROR; }
234 ncbi->timestamp[len] = 0;
235
236 /* read in database stats */
237 if (fread(&info[0], sizeof(uint32_t), 4, ncbi->fppin) != 4) { status = eslFAIL; goto ERROR; }
238 ncbi->num_seq = htobe32(info[0]);
239 memcpy(&ncbi->total_res, info+1, sizeof(uint64_t));
240 ncbi->max_seq = htobe32(info[3]);
241
242 /* save the offsets to the index tables */
243 ncbi->hdr_off = ftell(ncbi->fppin);
244 ncbi->seq_off = ncbi->hdr_off + sizeof(uint32_t) * (ncbi->num_seq + 1);
245
246 return eslOK;
247
248 ERROR:
249 return status;
250 }
251
252 /* sqncbi_DbOpen()
253 *
254 * Try to open the database files. On successful opening of
255 * the files, index, header and sequence, the index file is
256 * parsed for validity.
257 */
258 static int
259 sqncbi_DbOpen(ESL_SQNCBI_DATA *ncbi, char *filename, int dbtype)
260 {
261 int status = eslOK; /* return status from an ESL call */
262 int len;
263
264 char *name = NULL;
265
266 len = strlen(filename);
267 ESL_ALLOC(name, sizeof(char) * (len+5));
268 strcpy(name, filename);
269
270 /* Check for basic database first */
271 strcpy(name+len, ".Xin");
272 name[len+1] = (dbtype == NCBI_DNA_DB) ? 'n' : 'p';
273 if ((ncbi->fppin = fopen(name, "rb")) == NULL) {
274 status = eslENOTFOUND;
275 goto ERROR;
276 }
277 strcpy(name+len, ".Xhr");
278 name[len+1] = (dbtype == NCBI_DNA_DB) ? 'n' : 'p';
279 if ((ncbi->fpphr = fopen(name, "rb")) == NULL) {
280 status = eslENOTFOUND;
281 goto ERROR;
282 }
283 strcpy(name+len, ".Xsq");
284 name[len+1] = (dbtype == NCBI_DNA_DB) ? 'n' : 'p';
285 if ((ncbi->fppsq = fopen(name, "rb")) == NULL) {
286 status = eslENOTFOUND;
287 goto ERROR;
288 }
289
290 /* parse the header make sure we are looking at a version 4 db. */
291 if ((status = sqncbi_ParseIndexFile(ncbi, dbtype)) != eslOK) goto ERROR;
292
293 if (name != NULL) free(name);
294
295 return eslOK;
296
297 ERROR:
298
299 reset_db(ncbi);
300
301 if (name != NULL) free(name);
302 return status;
303 }
304
305
306 /* sqncbi_AliasOpen()
307 *
308 * Opens an alias file parsing the DBLIST directive building
309 * a list of all the volumes makeing up this database. As each
310 * volume is successfully parsed, the name of the volume, number
311 * of sequences and offsets are kept for quick reference.
312 */
313 static int
314 sqncbi_AliasOpen(ESL_SQNCBI_DATA *ncbi, char *filename, int dbtype)
315 {
316 int status = eslOK; /* return status from an ESL call */
317 int newline = 1;
318 int seqcnt = 0;
319 int rescnt = 0;
320 int done = 0;
321 int vol;
322 int len;
323
324 char *ptr = NULL;
325 char *name = NULL;
326 char buffer[80];
327
328 char *dbptr = NULL;
329 char *dbname = NULL;
330 int dbsize = 512;
331 int dblen = 0;
332
333 FILE *fp = NULL;
334
335 len = strlen(filename);
336 ESL_ALLOC(name, sizeof(char) * (len+5));
337 strcpy(name, filename);
338
339 /* Check for alias file */
340 strcpy(name+len, ".Xal");
341 name[len+1] = (dbtype == NCBI_DNA_DB) ? 'n' : 'p';
342 if ((fp = fopen(name, "r")) == NULL) {
343 status = eslENOTFOUND;
344 goto ERROR;
345 }
346
347 /* copy the filename */
348 dbsize = (dbsize > len) ? dbsize : len * 2;
349 ESL_ALLOC(dbname, sizeof(char) * dbsize);
350 strcpy(dbname, filename);
351
352 /* remove the filename keeping the path */
353 while (len > 0 && dbname[len-1] != eslDIRSLASH) {
354 dbname[len-1] = '\0';
355 --len;
356 }
357
358 /* find the DBLIST directive */
359 while (!done) {
360 ptr = buffer;
361 if (fgets(buffer, sizeof(buffer), fp) == NULL) {
362 status = eslEFORMAT;
363 goto ERROR;
364 }
365
366 if (newline) {
367 if (strncmp(buffer, "DBLIST", 6) == 0 && isspace(buffer[6])) {
368 done = 1;
369 ptr = buffer + 6;
370 }
371 }
372
373 /* skip to the end of the line */
374 if (!done) {
375 newline = 0;
376 while (*ptr != '\0') {
377 if (*ptr == '\n') newline = 1;
378 ++ptr;
379 }
380 }
381 }
382
383 /* parse the DBLIST line */
384 done = 0;
385 dblen = len;
386 while (!done) {
387
388 /* check if we hit the end of the buffer */
389 if (*ptr == '\0') {
390 ptr = buffer;
391 if (fgets(buffer, sizeof(buffer), fp) == NULL) {
392 status = eslEFORMAT;
393 goto ERROR;
394 }
395 }
396
397 /* skip spaces */
398 if (isspace(*ptr)) {
399 if (dblen > len) {
400 dbname[dblen++] = '\0';
401
402 /* if the name in the DBLIST directive was ablsolute, do not
403 * use the working directory as a prefix.
404 */
405 if (dbname[len] == eslDIRSLASH) {
406 dbptr = dbname + len;
407 } else {
408 dbptr = dbname;
409 }
410
411 status = sqncbi_DbOpen(ncbi, dbptr, dbtype);
412 if (status != eslOK) goto ERROR;
413
414 /* close any open files and free up allocations */
415 reset_db(ncbi);
416
417 /* if successful, copy the db information */
418 vol = ncbi->volumes++;
419
420 /* allocate the name of the string big enought so the buffer can
421 * handle an extension, i.e. ".pin" or ".nsq" tacked on to the end
422 * of it without reallocating.
423 */
424 ncbi->vols[vol].name = NULL;
425 ESL_ALLOC(ncbi->vols[vol].name, sizeof(char) * strlen(dbptr) + 5);
426 strcpy(ncbi->vols[vol].name, dbptr);
427
428 ncbi->vols[vol].start_seq = seqcnt;
429 ncbi->vols[vol].end_seq = seqcnt + ncbi->num_seq - 1;
430
431 ncbi->vols[vol].hdr_off = ncbi->hdr_off;
432 ncbi->vols[vol].seq_off = ncbi->seq_off;
433 ncbi->vols[vol].amb_off = ncbi->amb_off;
434
435 seqcnt += ncbi->num_seq;
436 rescnt += ncbi->total_res;
437
438 dblen = len;
439 }
440
441 done = *ptr == '\n' || *ptr == '\r';
442
443 ptr++;
444 } else {
445 dbname[dblen++] = *ptr++;
446 if (dblen >= dbsize - 1) {
447 char *t;
448 dbsize += dbsize;
449 ESL_RALLOC(dbname, t, dbsize);
450 }
451 }
452 }
453
454 /* reopen the first volume for processing */
455 if ((status = volume_open(ncbi, 0)) != eslOK) goto ERROR;
456
457 ncbi->num_seq = seqcnt;
458 ncbi->total_res = rescnt;
459
460 if (name != NULL) free(name);
461 if (dbname != NULL) free(dbname);
462
463 if (fp != NULL) fclose(fp);
464
465 return eslOK;
466
467 ERROR:
468
469 reset_db(ncbi);
470
471 if (fp != NULL) fclose(fp);
472
473 if (dbname != NULL) free(dbname);
474 if (name != NULL) free(name);
475
476 return status;
477 }
478
479
480 /* Function: sqncbi_Open()
481 * Synopsis: Open an ncbi database.
482 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
483 *
484 * Purpose: Open an ncbi database making sure all the necessry
485 * files are present. Parse the index file for database
486 * information filling in the ncbi data structure.
487 *
488 * Returns: <eslOK> on success, and the ncbi data structre is filled
489 * in with the database information.
490 *
491 * Returns <eslENOTFOUND> if <filename> can't be found or
492 * opened. Returns <eslEFORMAT> if the index file is an
493 * upsupported version.
494 *
495 * Throws: <eslEMEM> on allocation failure.
496 */
497 static int
498 sqncbi_Open(ESL_SQNCBI_DATA *ncbi, char *filename)
499 {
500 int status = eslOK; /* return status from an ESL call */
501 char *name = NULL;
502
503 /* first try to open a single protein database */
504 status = sqncbi_DbOpen(ncbi, filename, NCBI_AMINO_DB);
505
506 /* if the database was not found, look for protein volume */
507 if (status == eslENOTFOUND) {
508 status = sqncbi_AliasOpen(ncbi, filename, NCBI_AMINO_DB);
509 }
510
511 /* if nothing so far, try a dna database */
512 if (status == eslENOTFOUND) {
513 status = sqncbi_DbOpen(ncbi, filename, NCBI_DNA_DB);
514 }
515
516 /* still nothing, look for dna volume */
517 if (status == eslENOTFOUND) {
518 status = sqncbi_AliasOpen(ncbi, filename, NCBI_DNA_DB);
519 }
520
521 if (status != eslOK) goto ERROR;
522
523 /* allocate buffers used in parsing the database files */
524 ESL_ALLOC(ncbi->hdr_indexes, sizeof(uint32_t) * INDEX_TABLE_SIZE);
525 ESL_ALLOC(ncbi->seq_indexes, sizeof(uint32_t) * INDEX_TABLE_SIZE);
526
527 /* if this is a dna database we need to allocate space for the
528 * ambiguity offsets.
529 */
530 if (ncbi->alphatype == eslDNA) {
531 ncbi->amb_off = ncbi->seq_off + sizeof(uint32_t) * (ncbi->num_seq + 1);
532 ESL_ALLOC(ncbi->amb_indexes, sizeof(uint32_t) * INDEX_TABLE_SIZE);
533 }
534
535 ncbi->hdr_alloced = INIT_HDR_BUFFER_SIZE;
536 ESL_ALLOC(ncbi->hdr_buf, sizeof(unsigned char) * INIT_HDR_BUFFER_SIZE);
537
538 /* skip the first sentinal byte in the .psq file */
539 fgetc(ncbi->fppsq);
540
541 if (name != NULL) free(name);
542 return eslOK;
543
544 ERROR:
545 if (name != NULL) free(name);
546 return status;
547 }
548
549
550 /* Function: sqncbi_Position()
551 * Synopsis: Reposition an open sequence file to an offset.
552 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
553 *
554 * Purpose: Reposition an open <sqfp> to offset <offset>.
555 * <offset> for the ncbi db format specified the sequence
556 * index, not file offset. Both the sequence and header
557 * files are repositioned.
558 *
559 * Returns: <eslOK> on success;
560 *
561 * Throws: <eslESYS> if the fseeko() or fread() call fails.
562 * On errors, the state of <sqfp> is indeterminate, and
563 * it should not be used again.
564 */
565 static int
566 sqncbi_Position(ESL_SQFILE *sqfp, off_t offset)
567 {
568 int status;
569
570 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
571
572 status = pos_sequence(ncbi, offset);
573 return status;
574 }
575
576 /* Function: sqncbi_Close()
577 * Synopsis: Close a sequence file.
578 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
579 *
580 * Purpose: Closes an open <sqfp>.
581 *
582 * Returns: (void).
583 */
584 static void
585 sqncbi_Close(ESL_SQFILE *sqfp)
586 {
587 int i;
588
589 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
590
591 if (ncbi->title != NULL) free(ncbi->title);
592 if (ncbi->timestamp != NULL) free(ncbi->timestamp);
593
594 if (ncbi->hdr_buf != NULL) free(ncbi->hdr_buf);
595
596 if (ncbi->hdr_indexes != NULL) free(ncbi->hdr_indexes);
597 if (ncbi->seq_indexes != NULL) free(ncbi->seq_indexes);
598 if (ncbi->amb_indexes != NULL) free(ncbi->amb_indexes);
599
600 if (ncbi->alphasym != NULL) free(ncbi->alphasym);
601
602 if (ncbi->fppin != NULL) fclose(ncbi->fppin);
603 if (ncbi->fpphr != NULL) fclose(ncbi->fpphr);
604 if (ncbi->fppsq != NULL) fclose(ncbi->fppsq);
605
606 ncbi->vol_index = -1;
607 ncbi->volumes = 0;
608
609 for (i = 0; i < MAX_DB_VOLUMES; ++i) {
610 if (ncbi->vols[i].name != NULL) free(ncbi->vols[i].name);
611
612 ncbi->vols[i].name = NULL;
613 ncbi->vols[i].start_seq = -1;
614 ncbi->vols[i].end_seq = -1;
615 }
616
617 ncbi->fppin = NULL;
618 ncbi->fpphr = NULL;
619 ncbi->fppsq = NULL;
620
621 ncbi->title = NULL;
622 ncbi->timestamp = NULL;
623
624 ncbi->index = 0;
625
626 ncbi->hdr_off = -1;
627 ncbi->seq_off = -1;
628 ncbi->amb_off = -1;
629
630 ncbi->index_start = -1;
631 ncbi->index_end = -1;
632 ncbi->hdr_indexes = NULL;
633 ncbi->seq_indexes = NULL;
634 ncbi->amb_indexes = NULL;
635
636 ncbi->hdr_buf = NULL;
637
638 ncbi->alphatype = eslUNKNOWN;
639 ncbi->alphasym = NULL;
640
641 return;
642 }
643 /*------------------- SQNCBI open/close -----------------------*/
644
645
646 /*****************************************************************
647 *# 2. An <ESL_SQFILE> object, in digital mode [with <alphabet>]
648 *****************************************************************/
649
650 /* Function: sqncbi_SetDigital()
651 * Synopsis: Set an open <ESL_SQFILE> to read in digital mode.
652 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
653 *
654 * Purpose: Given an <ESL_SQFILE> that's already been opened,
655 * configure it to expect subsequent input to conform
656 * to the digital alphabet <abc>.
657 *
658 * Calling <esl_sqfile_Open(); esl_sqfile_SetDigital()> is
659 * equivalent to <esl_sqfile_OpenDigital()>. The two-step
660 * version is useful when you need a
661 * <esl_sqfile_GuessAlphabet()> call in between, guessing
662 * the file's alphabet in text mode before you set it to
663 * digital mode.
664 *
665 * Returns: <eslOK> on success.
666 */
667 static int
668 sqncbi_SetDigital(ESL_SQFILE *sqfp, const ESL_ALPHABET *abc)
669 {
670 return eslOK;
671 }
672
673 /* Function: sqncbi_GuessAlphabet()
674 * Synopsis: Guess the alphabet of an open <ESL_SQFILE>.
675 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
676 *
677 * Purpose: The only ncbi db format supported is protein.
678 *
679 * Returns: <eslOK> on success, and <*ret_type> is set to <eslAMINO>.
680 */
681 static int
682 sqncbi_GuessAlphabet(ESL_SQFILE *sqfp, int *ret_type)
683 {
684 *ret_type = sqfp->data.ncbi.alphatype;
685 return eslOK;
686 }
687
688 /*-------------- end, digital mode SQNCBI -------------------*/
689
690
691
692
693 /*****************************************************************
694 *# 3. Miscellaneous routines
695 *****************************************************************/
696
697 /* Function: sqncbi_IsRewindable()
698 * Synopsis: Return <TRUE> if <sqfp> can be rewound.
699 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
700 *
701 * Purpose: Returns <TRUE> if <sqfp> can be rewound (positioned
702 * to an offset of zero), in order to read it a second
703 * time.
704 */
705 static int
706 sqncbi_IsRewindable(const ESL_SQFILE *sqfp)
707 {
708 return TRUE;
709 }
710
711 /* Function: sqncbi_GetError()
712 * Synopsis: Returns error buffer
713 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
714 *
715 * Purpose: Return a pointer to the error buffer.
716 */
717 static const char *
718 sqncbi_GetError(const ESL_SQFILE *sqfp)
719 {
720 return sqfp->data.ncbi.errbuf;
721 }
722
723
724 /*****************************************************************
725 *# 4. Sequence reading (sequential)
726 *****************************************************************/
727
728 /* Function: sqncbi_Read()
729 * Synopsis: Read the next sequence from a file.
730 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
731 *
732 * Purpose: Reads the next sequence from open sequence file <sqfp> into
733 * <sq>. Caller provides an allocated and initialized <s>, which
734 * will be internally reallocated if its space is insufficient.
735 *
736 * Returns: <eslOK> on success; the new sequence is stored in <s>.
737 *
738 * Returns <eslEOF> when there is no sequence left in the
739 * file (including first attempt to read an empty file).
740 *
741 * Returns <eslEFORMAT> if there's a problem with the format,
742 * such as an illegal character; the line number that the parse
743 * error occurs on is in <sqfp->linenumber>, and an informative
744 * error message is placed in <sqfp->errbuf>.
745 *
746 * Throws: <eslEMEM> on allocation failure;
747 * <eslEINCONCEIVABLE> on internal error.
748 */
749 static int
750 sqncbi_Read(ESL_SQFILE *sqfp, ESL_SQ *sq)
751 {
752 //int index;
753 int status;
754
755 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
756
757 //index = ncbi->index + 1;
758 if (ncbi->index >= ncbi->num_seq) return eslEOF;
759
760 if ((status = pos_sequence(ncbi, ncbi->index)) != eslOK) return status;
761
762 /* Disk offset bookkeeping */
763 sq->idx = ncbi->index;
764 sq->roff = ncbi->roff;
765 sq->doff = ncbi->doff;
766 sq->hoff = ncbi->hoff;
767 sq->eoff = ncbi->eoff;
768
769 if (ncbi->alphatype == eslAMINO)
770 status = read_amino(sqfp, sq);
771 else
772 status = read_dna(sqfp, sq);
773 if (status != eslOK) return status;
774
775 /* read and parse the ncbi header */
776 if ((status = parse_header(ncbi, sq)) != eslOK) return status;
777
778 (ncbi->index)++;
779
780 return eslOK;
781 }
782
783
784 /* Function: sqncbi_ReadInfo()
785 * Synopsis: Read sequence info, but not the sequence itself.
786 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
787 *
788 * Purpose: Read the next sequence from open sequence file <sqfp>,
789 * but don't store the sequence (or secondary structure).
790 * Upon successful return, <s> holds all the available
791 * information about the sequence -- its name, accession,
792 * description, and overall length <sq->L>.
793 *
794 * This is useful for indexing sequence files, where
795 * individual sequences might be ginormous, and we'd rather
796 * avoid reading complete seqs into memory.
797 *
798 * Returns: <eslOK> on success.
799 */
800 static int
801 sqncbi_ReadInfo(ESL_SQFILE *sqfp, ESL_SQ *sq)
802 {
803 //int index;
804 int status;
805
806 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
807
808 //index = ncbi->index + 1;
809 if (ncbi->index >= ncbi->num_seq) return eslEOF;
810
811 if ((status = pos_sequence(ncbi, ncbi->index)) != eslOK) return status;
812
813 /* Disk offset bookkeeping */
814 sq->idx = ncbi->index;
815 sq->roff = ncbi->roff;
816 sq->doff = ncbi->doff;
817 sq->hoff = ncbi->hoff;
818 sq->eoff = ncbi->eoff;
819
820 /* figure out the sequence length */
821 sq->L = -1;
822
823 /* read and parse the ncbi header */
824 if ((status = parse_header(ncbi, sq)) != eslOK) return status;
825
826 (ncbi->index)++;
827
828 return eslOK;
829 }
830
831
832 /* Function: sqncbi_ReadSequence()
833 * Synopsis: Read the sequence, not the sequence header.
834 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
835 *
836 * Purpose: Read the next sequence from open sequence file <sqfp>,
837 * but not the header information. Upon successful return,
838 * <s> holds all the sequence.
839 *
840 * This is useful reading binary formats and delaying the
841 * over heads of reading the sequence name until needed by
842 * the report generator.
843 *
844 * Returns: <eslOK> on success; the new sequence is stored in <s>.
845 *
846 * Returns <eslEOF> when there is no sequence left in the
847 * file (including first attempt to read an empty file).
848 *
849 * Throws: <eslEMEM> on allocation failure;
850 */
851 static int
852 sqncbi_ReadSequence(ESL_SQFILE *sqfp, ESL_SQ *sq)
853 {
854 //int index;
855 int status;
856
857 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
858
859 //index = ncbi->index + 1;
860 if (ncbi->index >= ncbi->num_seq) return eslEOF;
861
862 if ((status = pos_sequence(ncbi, ncbi->index)) != eslOK) return status;
863
864 /* Disk offset bookkeeping */
865 sq->idx = ncbi->index;
866 sq->roff = ncbi->roff;
867 sq->doff = ncbi->doff;
868 sq->hoff = ncbi->hoff;
869 sq->eoff = ncbi->eoff;
870
871 reset_header_values(ncbi);
872
873 if (ncbi->alphatype == eslAMINO)
874 status = read_amino(sqfp, sq);
875 else
876 status = read_dna(sqfp, sq);
877 if (status != eslOK) return status;
878
879 (ncbi->index)++;
880 return eslOK;
881 }
882
883
884 /* Function: sqncbi_ReadWindow()
885 * Synopsis: Read next window of sequence.
886 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
887 *
888 * Purpose: Read a next window of <W> residues from open file <sqfp>,
889 * keeping <C> residues from the previous window as
890 * context, and keeping previous annotation in the <sq>
891 * as before.
892 *
893 * If this is the first window of a new sequence record,
894 * <C> is ignored (there's no previous context yet), and
895 * the annotation fields of the <sq> (name, accession, and
896 * description) are initialized by reading the sequence
897 * record's header. This is the only time the annotation
898 * fields are initialized.
899 *
900 * On return, <sq->dsq[]> contains the window and its
901 * context; residues <1..sq->C> are the previous context,
902 * and residues <sq->C+1..sq->n> are the new window. The
903 * start and end coordinates of the whole <dsq[1..n]>
904 * (including context) in the original source sequence are
905 * <sq->start..sq->end>. (Or, for text mode sequences,
906 * <sq->seq[0..sq->C-1,sq->C..sq->n-1]>, while <start> and
907 * <end> coords are still <1..L>.)
908 *
909 * When a sequence record is completed and no more data
910 * remain, <eslEOD> is returned, with an ``info'' <sq>
911 * structure (containing the annotation and the total
912 * sequence length <L>, but no sequence). (The total
913 * sequence length <L> is unknown in <sq> until this
914 * <eslEOD> return.)
915 *
916 * The caller may then do one of two things before calling
917 * <esl_sq_ReadWindow()> again; it can reset the sequence
918 * with <esl_sq_Reuse()> to continue reading the next
919 * sequence in the file, or it can set a negative <W> as a
920 * signal to read windows from the reverse complement
921 * (Crick) strand. Reverse complement reading only works
922 * for nucleic acid sequence.
923 *
924 * If you read the reverse complement strand, you must read
925 * the whole thing, calling <esl_sqio_ReadWindow()> with
926 * negative <W> windows until <eslEOD> is returned again
927 * with an empty (info-only) <sq> structure. When that
928 * <EOD> is reached, the <sqfp> is repositioned at the
929 * start of the next sequence record; the caller should now
930 * <Reuse()> the <sq>, and the next <esl_sqio_ReadWindow()>
931 * call must have a positive <W>, corresponding to starting
932 * to read the Watson strand of the next sequence.
933 *
934 * Note that the <ReadWindow()> interface is designed for
935 * an idiom of sequential reading of complete sequences in
936 * overlapping windows, possibly on both strands; if you
937 * want more freedom to move around in the sequence
938 * grabbing windows in another order, you can use the
939 * <FetchSubseq()> interface.
940 *
941 * Reading the reverse complement strand requires file
942 * repositioning, so it will not work on non-repositionable
943 * streams like gzipped files or a stdin pipe. Moreover,
944 * for reverse complement input to be efficient, the
945 * sequence file should have consistent line lengths,
946 * suitable for SSI's fast subsequence indexing.
947 *
948 * Returns: <eslOK> on success; <sq> now contains next window of
949 * sequence, with at least 1 new residue. The number
950 * of new residues is <sq->W>; <sq->C> residues are
951 * saved from the previous window. Caller may now
952 * process residues <sq->dsq[sq->C+1]..sq->dsq[sq->n]>.
953 *
954 * <eslEOD> if no new residues were read for this sequence
955 * and strand, and <sq> now contains an empty info-only
956 * structure (annotation and <L> are valid). Before calling
957 * <esl_sqio_ReadWindow()> again, caller will either want
958 * to make <W> negative (to start reading the Crick strand
959 * of the current sequence), or it will want to reset the
960 * <sq> (with <esl_sq_Reuse()>) to go on the next sequence.
961 *
962 * <eslEOF> if we've already returned <eslEOD> before to
963 * signal the end of the previous seq record, and moreover,
964 * there's no more sequence records in the file.
965 *
966 * <eslEINVAL> if an invalid residue is found in the
967 * sequence, or if you attempt to take the reverse
968 * complement of a sequence that can't be reverse
969 * complemented.
970 *
971 * Throws: <eslESYNTAX> if you try to read a reverse window before
972 * you've read forward strand.
973 *
974 * <eslECORRUPT> if something goes awry internally in the
975 * coordinate system.
976 *
977 * <eslEMEM> on allocation error.
978 */
979 static int
980 sqncbi_ReadWindow(ESL_SQFILE *sqfp, int C, int W, ESL_SQ *sq)
981 {
982 uint64_t nres;
983 int status;
984
985 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
986
987 /* Negative W indicates reverse complement direction */
988 if (W < 0)
989 {
990 if (sq->L == -1) ESL_EXCEPTION(eslESYNTAX, "Can't read reverse complement until you've read forward strand");
991
992 /* update the sequence index */
993 if ((status = sqncbi_Position(sqfp, sq->idx)) != eslOK)
994 ESL_FAIL(eslEINVAL, ncbi->errbuf, "Unexpected error positioning database to sequence %" PRId64, sq->idx);
995
996 if (sq->end == 1)
997 { /* last end == 1 means last window was the final one on reverse strand,
998 * so we're EOD; jump back to last forward position.
999 */
1000 sq->start = 0;
1001 sq->end = 0;
1002 sq->C = 0;
1003 sq->W = 0;
1004 sq->n = 0;
1005 /* sq->L stays as it is */
1006 if (sq->dsq != NULL) sq->dsq[1] = eslDSQ_SENTINEL;
1007 else sq->seq[0] = '\0';
1008
1009 return eslEOD;
1010 }
1011
1012 /* If s == 0, we haven't read any reverse windows yet;
1013 * init reading from sq->L
1014 */
1015 W = -W;
1016 if (sq->start == 0)
1017 {
1018 sq->start = ESL_MAX(1, (sq->L - W + 1));
1019 sq->end = sq->start;
1020 sq->C = 0;
1021 }
1022 else
1023 { /* Else, we're continuing to next window; prv was <end>..<start> */
1024 sq->C = ESL_MIN(C, sq->L - sq->end + 1); /* based on prev window's end */
1025 sq->start = ESL_MAX(1, (sq->end - W));
1026 W = sq->end - sq->start + sq->C;
1027 sq->end = sq->start;
1028 }
1029
1030 /* grab the subseq and rev comp it */
1031 if ((status = esl_sq_GrowTo(sq, W)) != eslOK) return status;
1032 sq->n = 0;
1033 if (ncbi->alphatype == eslAMINO) status = read_nres_amino(sqfp, sq, W, &nres);
1034 else status = read_nres_dna(sqfp, sq, W, &nres);
1035
1036 if (status != eslOK || nres != W) {
1037 ESL_EXCEPTION(eslECORRUPT, "Failed to extract %d..%d", sq->start, sq->end);
1038 } else {
1039 sq->end = sq->start + nres - 1;
1040 sq->W = nres - sq->C;
1041 }
1042
1043 status = esl_sq_ReverseComplement(sq);
1044 if (status == eslEINVAL) ESL_FAIL(eslEINVAL, ncbi->errbuf, "can't reverse complement that seq - it's not DNA/RNA");
1045 else if (status != eslOK) return status;
1046
1047 return eslOK;
1048 }
1049
1050 /* Else, we're reading the forward strand */
1051 else
1052 { /* sq->start == 0 means we haven't read any windows on this sequence yet...
1053 * it's a new record, and we need to initialize with the header and
1054 * the first window. This is the only case that we're allowed to return
1055 * EOF from.
1056 */
1057 if (sq->start == 0)
1058 {
1059 if (ncbi->index >= ncbi->num_seq) return eslEOF;
1060
1061 /* get the sequence and header offsets */
1062 if ((status = pos_sequence(ncbi, ncbi->index)) != eslOK) return status;
1063
1064 /* Disk offset bookkeeping */
1065 sq->idx = ncbi->index;
1066 sq->roff = ncbi->roff;
1067 sq->doff = ncbi->doff;
1068 sq->hoff = ncbi->hoff;
1069 sq->eoff = ncbi->eoff;
1070
1071 ncbi->seq_cpos = -1;
1072 ncbi->seq_L = -1;
1073
1074 /* read and parse the ncbi header */
1075 if ((status = parse_header(ncbi, sq)) != eslOK) return status;
1076
1077 sq->start = 1;
1078 sq->C = 0; /* no context in first window */
1079 sq->L = -1; /* won't be known 'til EOD. */
1080 ncbi->seq_L = -1; /* init to 0, so we can count residues as we go */
1081 esl_sq_SetSource(sq, sq->name);
1082
1083 }
1084 else
1085 { /* else we're reading a window other than first; slide context over. */
1086 sq->C = ESL_MIN(C, sq->n);
1087
1088 /* if the case where the window is smaller than the context and the
1089 * context is not full, it is not necessary to move the context part
1090 * of the sequence that has been read in.
1091 */
1092 if (sq->C >= C) {
1093 if (sq->seq != NULL) memmove(sq->seq, sq->seq + sq->n - sq->C, sq->C);
1094 else memmove(sq->dsq+1, sq->dsq + sq->n - sq->C + 1, sq->C);
1095 sq->start = sq->end - sq->C + 1;
1096 sq->n = C;
1097 }
1098 }
1099
1100 if ((status = esl_sq_GrowTo(sq, C+W)) != eslOK) return status; /* EMEM */
1101 if (ncbi->alphatype == eslAMINO) status = read_nres_amino(sqfp, sq, W, &nres);
1102 else status = read_nres_dna(sqfp, sq, W, &nres);
1103
1104 if (status == eslEOD)
1105 {
1106 (ncbi->index)++;
1107 sq->start = 0;
1108 sq->end = 0;
1109 sq->C = 0;
1110 sq->W = 0;
1111 sq->n = 0;
1112
1113 if (sq->dsq != NULL) sq->dsq[1] = eslDSQ_SENTINEL; /* erase the saved context */
1114 else sq->seq[0] = '\0';
1115
1116 return eslEOD;
1117 }
1118 else if (status == eslOK)
1119 { /* Forward strand is still in progress. <= W residues were read. Return eslOK. */
1120 sq->end = sq->start + sq->C + nres - 1;
1121 sq->W = nres;
1122 return eslOK;
1123 }
1124 else return status; /* EFORMAT,EMEM */
1125 }
1126 /*NOTREACHED*/
1127 return eslOK;
1128 }
1129
1130 /* Function: sqncbi_ReadBlock()
1131 * Synopsis: Read the next block of sequences from a file.
1132 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
1133 *
1134 * Purpose: Reads a block of sequences from open sequence file <sqfp> into
1135 * <sqBlock>.
1136 *
1137 * In the case that <long_target> is false, the sequences are
1138 * expected to be protein - individual sequences won't be long
1139 * so read them in one-whole-sequence at a time. If <max_sequences>
1140 * is set to a number > 0 read <max_sequences> sequences.
1141 *
1142 * If <long_target> is true, the sequences are expected to be DNA.
1143 * Because sequences in a DNA database can exceed MAX_RESIDUE_COUNT,
1144 * this function uses ReadWindow to read chunks of sequence no
1145 * larger than <max_residues>, and must allow for the possibility that
1146 * a request will be made to continue reading a partly-read
1147 * sequence. This case also respects the <max_sequences> limit.
1148 *
1149 * If <long_target> is true and <max_init_window> is TRUE,
1150 * the first window read from each sequence (of length L)
1151 * is always min(L, <max_residues>). If <max_init_window>
1152 * is FALSE, then the length of the first window read from
1153 * each sequence is calculated differently as
1154 * max(<max_residues> - <size>, <max_residues> * .05);
1155 * where <size> is total number of residues already existing
1156 * in the block. <max_init_window> == TRUE mode was added
1157 * to ensure that the window boundaries read are not dependent
1158 * on the order of the sequence in the file, thus ensuring
1159 * reproducibility if (for example) a user extracts one
1160 * sequence from a file and reruns a program on it (and all
1161 * else remains equal).
1162 *
1163 * Returns: <eslOK> on success; the new sequence is stored in <sqBlock>.
1164 *
1165 * Returns <eslEOF> when there is no sequence left in the
1166 * file (including first attempt to read an empty file).
1167 *
1168 * Returns <eslEFORMAT> if there's a problem with the format,
1169 * such as an illegal character;
1170 *
1171 * Throws: <eslEMEM> on allocation failure;
1172 * <eslEINCONCEIVABLE> on internal error.
1173 */
1174 static int
1175 sqncbi_ReadBlock(ESL_SQFILE *sqfp, ESL_SQ_BLOCK *sqBlock, int max_residues, int max_sequences, int max_init_window, int long_target)
1176 {
1177 int i = 0;
1178 int size = 0;
1179 int status = eslOK;
1180 ESL_SQ *tmpsq = NULL;
1181
1182 sqBlock->count = 0;
1183
1184 if ( !long_target )
1185 { /* in these cases, an individual sequence won't ever be really long,
1186 so just read in a sequence at a time */
1187
1188 if (max_sequences < 1 || max_sequences > sqBlock->listSize)
1189 max_sequences = sqBlock->listSize;
1190
1191 for (i = 0; i < max_sequences && size < MAX_RESIDUE_COUNT; ++i)
1192 {
1193 status = sqncbi_Read(sqfp, sqBlock->list + i);
1194 if (status != eslOK) break;
1195 size += sqBlock->list[i].n;
1196 ++sqBlock->count;
1197 }
1198 }
1199 else
1200 { /* DNA, not an alignment. Might be really long sequences */
1201
1202 /*this variable was used instead of the MAX_RESIDUE_COUNT macro because old H3 impl_dummy could've required shorter sequences to fit in memory*/
1203 if (max_residues < 0)
1204 max_residues = MAX_RESIDUE_COUNT;
1205
1206 tmpsq = esl_sq_CreateDigital(sqBlock->list->abc);
1207
1208 //if complete flag set to FALSE, then the prior block must have ended with a window that was a possibly
1209 //incomplete part of it's full sequence. Read another overlapping window.
1210 if (! sqBlock->complete )
1211 {
1212 //overloading C as indicator of how big C should be for this window reading action
1213 status = sqncbi_ReadWindow(sqfp, sqBlock->list->C, max_residues, sqBlock->list);
1214 if (status == eslOK)
1215 {
1216 sqBlock->count = i = 1;
1217 size = sqBlock->list->n;
1218 sqBlock->list[i].L = sqfp->data.ncbi.seq_L;
1219 if (sqBlock->list->n >= max_residues)
1220 { // Filled the block with a single very long window.
1221
1222 if ( sqBlock->list->n == sqfp->data.ncbi.seq_L) {
1223 sqBlock->complete = TRUE;
1224 esl_sq_Reuse(tmpsq);
1225 tmpsq->start = sqBlock->list->start ;
1226 tmpsq->C = 0;
1227 status = sqncbi_ReadWindow(sqfp, 0, max_residues, tmpsq); // burn off the EOD
1228 if (status == eslEOD) // otherwise, the unexpected status will be returned
1229 status = eslOK;
1230
1231 } else {
1232 sqBlock->complete = FALSE;
1233 }
1234
1235 if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
1236 return status;
1237
1238 }
1239 else
1240 {
1241 // Burn off EOD (see notes for similar entry ~25 lines below), then go fetch the next sequence
1242 esl_sq_Reuse(tmpsq);
1243 tmpsq->start = sqBlock->list->start ;
1244 tmpsq->C = 0;
1245 status = sqncbi_ReadWindow(sqfp, 0, max_residues, tmpsq);
1246 if (status != eslEOD) {
1247 if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
1248 return status; //surprising
1249 }
1250 sqBlock->list->L = tmpsq->L;
1251 }
1252 }
1253 else if (status == eslEOD)
1254 { // turns out there isn't any more of the sequence to read, after all
1255
1256 }
1257 else
1258 {
1259 if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
1260 return status;
1261 }
1262 } // otherwise, just start at the beginning
1263
1264
1265 for ( ; i < sqBlock->listSize && size < max_residues; ++i)
1266 {
1267 /* restricted request_size is used to ensure that all blocks are pretty close to the
1268 * same size. Without it, we may either naively keep asking for max_residue windows,
1269 * which can result in a window with ~2*max_residues ... or we can end up with absurdly
1270 * short fragments at the end of blocks
1271 */
1272 int request_size = (max_init_window) ? max_residues : ESL_MAX(max_residues-size, max_residues * .05);
1273
1274 esl_sq_Reuse(tmpsq);
1275 esl_sq_Reuse(sqBlock->list + i);
1276 status = sqncbi_ReadWindow(sqfp, 0, request_size, tmpsq);
1277 esl_sq_Copy(tmpsq, sqBlock->list +i);
1278 if (status != eslOK) break; // end of sequences
1279
1280 size += sqBlock->list[i].n - sqBlock->list[i].C;
1281 sqBlock->list[i].L = sqfp->data.ncbi.seq_L;
1282 ++(sqBlock->count);
1283 if (size >= max_residues)
1284 { // a full window worth of sequence was read
1285
1286 if ( sqBlock->list[i].n == sqfp->data.ncbi.seq_L) {
1287 sqBlock->complete = TRUE;
1288 esl_sq_Reuse(tmpsq);
1289 tmpsq->start = sqBlock->list->start ;
1290 tmpsq->C = 0;
1291 status = sqncbi_ReadWindow(sqfp, 0, max_residues, tmpsq); // burn off the EOD
1292 if (status == eslEOD) // otherwise, the unexpected status will be returned
1293 status = eslOK;
1294
1295 } else {
1296 sqBlock->complete = FALSE;
1297 }
1298
1299
1300 if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
1301 return status;
1302
1303 }
1304 else
1305 {
1306 /* Sequence was finished before filling a full window. Need to burn off the EOD value that will be
1307 returned by the next ReadWindow call. Can just use a tmp sq, after setting a couple
1308 values ReadWindow needs to see for correct processing.
1309 */
1310 esl_sq_Reuse(tmpsq);
1311 tmpsq->start = sqBlock->list[i].start ;
1312 tmpsq->end = sqBlock->list[i].end ;
1313 tmpsq->n = sqBlock->list[i].n ;
1314 //tmpsq->doff = sqBlock->list[i].doff ;
1315 tmpsq->idx = sqBlock->list[i].idx ;
1316
1317 tmpsq->C = 0;
1318 status = sqncbi_ReadWindow(sqfp, 0, max_residues, tmpsq);
1319 if (status != eslEOD) {
1320 if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
1321 return status; //surprising
1322 }
1323 //sqBlock->list[i].L = tmpsq->L;
1324 status = eslOK;
1325 }
1326 }
1327 }
1328
1329 /* EOF will be returned only in the case were no sequences were read */
1330 if (status == eslEOF && i > 0) status = eslOK;
1331
1332 sqBlock->complete = TRUE;
1333
1334 if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
1335
1336 return status;
1337 }
1338
1339 /* Function: sqncbi_Echo()
1340 * Synopsis: Echo a sequence's record onto output stream.
1341 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
1342 *
1343 * Returns: <eslEUNIMPLEMENTED>.
1344 */
1345 static int
1346 sqncbi_Echo(ESL_SQFILE *sqfp, const ESL_SQ *sq, FILE *ofp)
1347 {
1348 ESL_EXCEPTION(eslEINVAL, "can't Echo() a sequence from NCBI database");
1349 return eslEUNIMPLEMENTED;
1350 }
1351 /*------------------ end, sequential sequence input -------------*/
1352
1353
1354
1355 /* Function: reset_db()
1356 * Synopsis: Resets a sequence file.
1357 * Incept: MSF, Wed Mar 17, 2010 [Janelia]
1358 *
1359 * Purpose: Closes just the currently opened db and frees
1360 * up the memory associated with that db. This is
1361 * used primarily with multi-volume databases where
1362 * the current db is closed so the next one can be
1363 * opened.
1364 *
1365 * Returns: void.
1366 */
1367 static void
1368 reset_db(ESL_SQNCBI_DATA *ncbi)
1369 {
1370 if (ncbi->title != NULL) free(ncbi->title);
1371 if (ncbi->timestamp != NULL) free(ncbi->timestamp);
1372
1373 if (ncbi->fppin != NULL) fclose(ncbi->fppin);
1374 if (ncbi->fpphr != NULL) fclose(ncbi->fpphr);
1375 if (ncbi->fppsq != NULL) fclose(ncbi->fppsq);
1376
1377 ncbi->fppin = NULL;
1378 ncbi->fpphr = NULL;
1379 ncbi->fppsq = NULL;
1380
1381 ncbi->title = NULL;
1382 ncbi->timestamp = NULL;
1383
1384 return;
1385 }
1386
1387
1388 /* reset_header_values()
1389 *
1390 * Clear the header values so it is clear which values
1391 * have been set by the current header.
1392 */
1393 static void
1394 reset_header_values(ESL_SQNCBI_DATA *ncbi)
1395 {
1396 ncbi->name_ptr = NULL;
1397 ncbi->name_size = 0;
1398 ncbi->acc_ptr = NULL;
1399 ncbi->acc_size = 0;
1400 ncbi->int_id = -1;
1401 ncbi->str_id_ptr = NULL;
1402 ncbi->str_id_size = 0;
1403 }
1404
1405 /* volume_open()
1406 *
1407 * Open up the index, head and sequence files for a particular
1408 * volume. Parse the first three fields in the index file,
1409 * one more time, just to make sure nothing funny is going on.
1410 */
1411 static int
1412 volume_open(ESL_SQNCBI_DATA *ncbi, int volume)
1413 {
1414 int len;
1415 uint32_t info[4];
1416 int dbtype;
1417 int status = eslOK; /* return status from an ESL call */
1418
1419 char *name;
1420
1421 if (volume < 0 || volume > ncbi->volumes) return eslEINVAL;
1422
1423 /* if the db has no volumes return */
1424 if (ncbi->volumes == 0) return eslOK;
1425
1426 if (ncbi->fppin != NULL) fclose(ncbi->fppin);
1427 if (ncbi->fpphr != NULL) fclose(ncbi->fpphr);
1428 if (ncbi->fppsq != NULL) fclose(ncbi->fppsq);
1429
1430 name = ncbi->vols[volume].name;
1431 len = strlen(name);
1432
1433 dbtype = (ncbi->alphatype == eslDNA) ? NCBI_DNA_DB : NCBI_AMINO_DB;
1434
1435 /* Check for basic database first */
1436 strcpy(name+len, ".Xin");
1437 name[len+1] = (dbtype == NCBI_DNA_DB) ? 'n' : 'p';
1438 if ((ncbi->fppin = fopen(name, "rb")) == NULL) {
1439 status = eslFAIL;
1440 goto ERROR;
1441 }
1442 strcpy(name+len, ".Xhr");
1443 name[len+1] = (dbtype == NCBI_DNA_DB) ? 'n' : 'p';
1444 if ((ncbi->fpphr = fopen(name, "rb")) == NULL) {
1445 status = eslFAIL;
1446 goto ERROR;
1447 }
1448 strcpy(name+len, ".Xsq");
1449 name[len+1] = (dbtype == NCBI_DNA_DB) ? 'n' : 'p';
1450 if ((ncbi->fppsq = fopen(name, "rb")) == NULL) {
1451 status = eslFAIL;
1452 goto ERROR;
1453 }
1454
1455 /* quickly parse the header make sure we are sane. */
1456 if (fread(&info[0], sizeof(uint32_t), 3, ncbi->fppin) != 3) status = eslFAIL;
1457 if (htobe32(info[0]) != NCBI_VERSION_4) status = eslEFORMAT;
1458 if (htobe32(info[1]) != dbtype) status = eslEFORMAT;
1459
1460 if (status != eslOK) goto ERROR;
1461
1462 /* save the offsets to the index tables */
1463 ncbi->hdr_off = ncbi->vols[volume].hdr_off;
1464 ncbi->seq_off = ncbi->vols[volume].seq_off;
1465 if (dbtype == NCBI_DNA_DB) {
1466 ncbi->amb_off = ncbi->vols[volume].amb_off;
1467 }
1468
1469 ncbi->vol_index = volume;
1470 ncbi->index_start = -1;
1471 ncbi->index_end = -1;
1472
1473 /* skip the first sentinal byte in the .psq file */
1474 fgetc(ncbi->fppsq);
1475
1476 /* zero terminate the name other functions can just
1477 * tack on any extension without a lot of testing.
1478 */
1479 name[len] = '\0';
1480
1481 return eslOK;
1482
1483 ERROR:
1484
1485 reset_db(ncbi);
1486
1487 return status;
1488 }
1489
1490
1491 /* pos_sequence_offsets()
1492 *
1493 * Position the sequence and header files for reading at
1494 * the start of the indexed sequence <inx>. This routine
1495 * buffers <INDEX_TABLE_SIZE> offsets from the header and
1496 * sequence files. If the index <inx> is not in the
1497 * currently buffered table, read the the indexes. If the
1498 * index is not in the current volume find which volume
1499 * the indexed sequence is in and open up that database.
1500 */
1501 static int
1502 pos_sequence(ESL_SQNCBI_DATA *ncbi, int inx)
1503 {
1504 int cnt;
1505 int status;
1506
1507 uint32_t offset;
1508 uint32_t start;
1509 uint32_t end;
1510
1511 ESL_SQNCBI_VOLUME *volume;
1512
1513 if (inx < 0 || inx > ncbi->num_seq) return eslEINVAL;
1514
1515 start = ncbi->index_start;
1516 end = ncbi->index_end;
1517
1518 /* get the offsets for the header, sequence and ambiguity table */
1519 if (ncbi->index_start == -1 || inx < start || inx > end) {
1520
1521 /* if the db is broken up into volumes, lets find the correct one to use */
1522 if (ncbi->volumes > 0) {
1523 volume = ncbi->vols + ncbi->vol_index;
1524 if (inx < volume->start_seq || inx > volume->end_seq) {
1525 volume = ncbi->vols;
1526 for (cnt = 0; cnt < ncbi->volumes; ++cnt) {
1527 if (inx < volume->end_seq) break;
1528 ++volume;
1529 }
1530
1531 /* check just to make sure we found the volume */
1532 if (cnt >= ncbi->volumes) return eslFAIL;
1533
1534 if ((status = volume_open(ncbi, cnt)) != eslOK) return status;
1535 }
1536 }
1537
1538 /* adjust where we start reading from if we are reading forwards or backwards */
1539 if (ncbi->index_start == -1 || inx > end) {
1540 start = inx;
1541 } else {
1542 start = inx + 2;
1543 start = (start > INDEX_TABLE_SIZE) ? start - INDEX_TABLE_SIZE : 0;
1544 }
1545 ncbi->index_start = start;
1546
1547 /* when calculating the count be sure to take into account the fact that the
1548 * index tables contain one index more that the number of sequences and this
1549 * last index is used to point to the end of the last header and sequences.
1550 */
1551 if (ncbi->volumes > 0) {
1552 cnt = volume->end_seq - inx + 2; // cppcheck thinks end_seq can be uninitialized here. I think it's wrong.
1553 start = start - volume->start_seq; // .. and ditto for start_seq.
1554 } else {
1555 cnt = ncbi->num_seq - inx + 1;
1556 }
1557 cnt = (cnt > INDEX_TABLE_SIZE) ? INDEX_TABLE_SIZE : cnt;
1558 ncbi->index_end = ncbi->index_start + cnt - 2;
1559
1560 offset = ncbi->hdr_off + (sizeof(uint32_t) * start);
1561 if (fseek(ncbi->fppin, offset, SEEK_SET) != 0) {
1562 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error seeking header index %d\n", offset);
1563 }
1564 if (fread(ncbi->hdr_indexes, sizeof(uint32_t), cnt, ncbi->fppin) != cnt) {
1565 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error reading header index %d at %d(%d)\n", start, offset, cnt);
1566 }
1567
1568 offset = ncbi->seq_off + (sizeof(uint32_t) * start);
1569 if (fseek(ncbi->fppin, offset, SEEK_SET) != 0) {
1570 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error seeking sequence index %d\n", offset);
1571 }
1572 if (fread(ncbi->seq_indexes, sizeof(uint32_t), cnt, ncbi->fppin) != cnt) {
1573 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error reading sequence index %d at %d(%d)\n", start, offset, cnt);
1574 }
1575
1576 if (ncbi->alphatype == eslDNA) {
1577 offset = ncbi->amb_off + (sizeof(uint32_t) * start);
1578 if (fseek(ncbi->fppin, offset, SEEK_SET) != 0) {
1579 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error seeking ambiguity index %d\n", offset);
1580 }
1581 if (fread(ncbi->amb_indexes, sizeof(uint32_t), cnt, ncbi->fppin) != cnt) {
1582 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error reading ambiguity index %d at %d(%d)\n", start, offset, cnt);
1583 }
1584 }
1585 }
1586
1587 ncbi->index = inx;
1588
1589 inx -= ncbi->index_start;
1590 ncbi->roff = htobe32(ncbi->hdr_indexes[inx]);
1591 ncbi->doff = htobe32(ncbi->seq_indexes[inx]);
1592 ncbi->hoff = htobe32(ncbi->hdr_indexes[inx+1]);
1593 ncbi->eoff = htobe32(ncbi->seq_indexes[inx+1]);
1594
1595 if (ncbi->alphatype == eslDNA) {
1596 ncbi->seq_apos = htobe32(ncbi->amb_indexes[inx]);
1597 ncbi->seq_alen = ncbi->seq_apos + htobe32(ncbi->amb_indexes[inx+1]) + 1;
1598 } else {
1599 ncbi->seq_apos = 0;
1600 ncbi->seq_alen = 0;
1601 }
1602
1603 if (fseek(ncbi->fpphr, ncbi->roff, SEEK_SET) != 0) return eslESYS;
1604 if (fseek(ncbi->fppsq, ncbi->doff, SEEK_SET) != 0) return eslESYS;
1605
1606 return eslOK;
1607 }
1608
1609 /* Function: read_amino()
1610 * Synopsis: Read in the amino sequence
1611 * Incept: MSF, Wed Jan 27, 2010 [Janelia]
1612 *
1613 * Purpose: Read and translate the amino acid sequence.
1614 */
1615 static int
1616 read_amino(ESL_SQFILE *sqfp, ESL_SQ *sq)
1617 {
1618 int inx;
1619 int size;
1620
1621 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
1622
1623 if (ncbi->index >= ncbi->num_seq) return eslEOF;
1624
1625 size = sq->eoff - sq->doff;
1626
1627 /* figure out the sequence length */
1628 if (esl_sq_GrowTo(sq, size) != eslOK) return eslEMEM;
1629
1630 /* figure out if the sequence is in digital mode or not */
1631 if (sq->dsq != NULL) {
1632 ESL_DSQ *ptr = sq->dsq + 1;
1633 if (fread(ptr, sizeof(char), size, ncbi->fppsq) != size) return eslEFORMAT;
1634 for (inx = 0; inx < size - 1; ++inx) {
1635 *ptr = sqfp->inmap[(int) *ptr];
1636 ++ptr;
1637 }
1638 *ptr = eslDSQ_SENTINEL;
1639 } else {
1640 char *ptr = sq->seq;
1641 if (fread(ptr, sizeof(char), size, ncbi->fppsq) != size) return eslEFORMAT;
1642 for (inx = 0; inx < size - 1; ++inx) {
1643 *ptr = sqfp->inmap[(int) *ptr];
1644 *ptr = ncbi->alphasym[(int) *ptr];
1645 ++ptr;
1646 }
1647 *ptr = '\0';
1648 }
1649
1650 sq->start = 1;
1651 sq->end = size - 1;
1652 sq->C = 0;
1653 sq->W = size - 1;
1654 sq->L = size - 1;
1655 sq->n = size - 1;
1656
1657 return eslOK;
1658 }
1659
1660
1661 /* Function: read_dna()
1662 * Synopsis: Read in the dna sequence
1663 * Incept: MSF, Wed Jan 27, 2010 [Janelia]
1664 *
1665 * Purpose: Read and translate the dna sequence.
1666 */
1667 static int
1668 read_dna(ESL_SQFILE *sqfp, ESL_SQ *sq)
1669 {
1670 int64_t inx;
1671 int64_t cnt;
1672 int64_t off;
1673 int size;
1674 int text;
1675 int status;
1676 int amb32;
1677
1678 int remainder;
1679 int length;
1680 int ssize;
1681 int n;
1682
1683 unsigned char *ptr;
1684 void *t;
1685
1686 unsigned char c;
1687
1688 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
1689
1690 if (ncbi->index >= ncbi->num_seq) return eslEOF;
1691
1692 /* calculate the max size of the sequence. It is most likely
1693 * a bit smaller, but we need to read the sequence in first
1694 * before the real size can be figured out.
1695 */
1696 size = sq->eoff - sq->doff;
1697 if (ncbi->hdr_alloced < size) {
1698 while (ncbi->hdr_alloced < size) ncbi->hdr_alloced += ncbi->hdr_alloced;
1699 ESL_RALLOC(ncbi->hdr_buf, t, sizeof(char) * ncbi->hdr_alloced);
1700 }
1701 if (fread(ncbi->hdr_buf, sizeof(char), size, ncbi->fppsq) != size) return eslEFORMAT;
1702
1703 ssize = ncbi->seq_apos - sq->doff - 1;
1704 remainder = *(ncbi->hdr_buf + ssize) & 0x03;
1705 length = ssize * 4 + remainder;
1706
1707 /* figure out the sequence length */
1708 if (esl_sq_GrowTo(sq, length) != eslOK) return eslEMEM;
1709
1710 /* figure out if the sequence is in digital mode or not */
1711 if (sq->dsq != NULL) {
1712 text = FALSE;
1713 ptr = (unsigned char *) sq->dsq + 1;
1714 } else {
1715 text = TRUE;
1716 ptr = (unsigned char *) sq->seq;
1717 }
1718
1719 for (inx = 0; inx < ssize; ++inx) {
1720 c = ncbi->hdr_buf[inx];
1721 n = 1 << ((c >> 6) & 0x03);
1722 *ptr = sqfp->inmap[n];
1723 if (text) *ptr = ncbi->alphasym[(int) *ptr];
1724 ++ptr;
1725 n = 1 << ((c >> 4) & 0x03);
1726 *ptr = sqfp->inmap[n];
1727 if (text) *ptr = ncbi->alphasym[(int) *ptr];
1728 ++ptr;
1729 n = 1 << ((c >> 2) & 0x03);
1730 *ptr = sqfp->inmap[n];
1731 if (text) *ptr = ncbi->alphasym[(int) *ptr];
1732 ++ptr;
1733 n = 1 << ((c >> 0) & 0x03);
1734 *ptr = sqfp->inmap[n];
1735 if (text) *ptr = ncbi->alphasym[(int) *ptr];
1736 ++ptr;
1737 }
1738
1739 /* handle the remainder */
1740 c = ncbi->hdr_buf[inx];
1741 for (inx = 0; inx < remainder; ++inx) {
1742 n = 1 << ((c >> (6 - inx * 2)) & 0x03);
1743 *ptr = sqfp->inmap[n];
1744 if (text) *ptr = ncbi->alphasym[(int) *ptr];
1745 ++ptr;
1746 }
1747
1748 *ptr = (text) ? '\0' : eslDSQ_SENTINEL;
1749
1750 /* we need to look that the first by the the ambiguity table
1751 * to see if the entries are 32 or 64 bit entries.
1752 */
1753 amb32 = 0;
1754 if (ncbi->seq_apos - sq->doff < size) {
1755 amb32 = ((ncbi->hdr_buf[ncbi->seq_apos - sq->doff] & 0x80) == 0);
1756 }
1757
1758 /* skip past the count and start processing the abmiguity table */
1759 ssize = ncbi->seq_apos - sq->doff + 4;
1760 ptr = (text) ? (unsigned char *)sq->seq : (unsigned char *)sq->dsq + 1;
1761
1762 while (ssize < size) {
1763 /* get the ambiguity character */
1764 n = ((ncbi->hdr_buf[ssize] >> 4) & 0x0f);
1765 c = sqfp->inmap[n];
1766 if (text) c = ncbi->alphasym[(int) c];
1767
1768 if (amb32) {
1769 /* get the repeat count 4 bits */
1770 cnt = (ncbi->hdr_buf[ssize] & 0x0f);
1771 cnt += 1;
1772
1773 /* get the offset 24 bits */
1774 off = ncbi->hdr_buf[ssize+1];
1775 off = (off << 8) | ncbi->hdr_buf[ssize+2];
1776 off = (off << 8) | ncbi->hdr_buf[ssize+3];
1777
1778 for (inx = 0; inx < cnt; ++inx) ptr[off+inx] = c;
1779
1780 ssize += 4;
1781 } else {
1782 /* get the repeat count 12 bits */
1783 cnt = (ncbi->hdr_buf[ssize] & 0x0f);
1784 cnt = (cnt << 8) | ncbi->hdr_buf[ssize+1];
1785 cnt += 1;
1786
1787 /* get the offset 48 bits*/
1788 off = ncbi->hdr_buf[ssize+2];
1789 off = (off << 8) | ncbi->hdr_buf[ssize+3];
1790 off = (off << 8) | ncbi->hdr_buf[ssize+4];
1791 off = (off << 8) | ncbi->hdr_buf[ssize+5];
1792 off = (off << 8) | ncbi->hdr_buf[ssize+6];
1793 off = (off << 8) | ncbi->hdr_buf[ssize+7];
1794
1795 for (inx = 0; inx < cnt; ++inx) ptr[off+inx] = c;
1796
1797 ssize += 8;
1798 }
1799 }
1800
1801 sq->start = 1;
1802 sq->end = length;
1803 sq->C = 0;
1804 sq->W = length;
1805 sq->L = length;
1806 sq->n = length;
1807
1808 return eslOK;
1809
1810 ERROR:
1811 return eslEMEM;
1812 }
1813
1814
1815 /* Function: read_nres_amino()
1816 * Synopsis: Read in the amino sequence
1817 * Incept: MSF, Wed Jan 27, 2010 [Janelia]
1818 *
1819 * Purpose: Read and translate the dna sequence.
1820 */
1821 static int
1822 read_nres_amino(ESL_SQFILE *sqfp, ESL_SQ *sq, int len, uint64_t *nres)
1823 {
1824 int inx;
1825 int off;
1826 int size;
1827
1828 unsigned char *ptr;
1829 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
1830
1831 if (ncbi->index >= ncbi->num_seq) return eslEOF;
1832
1833 /* if we don't know the sequence length, figure it out */
1834 if (ncbi->seq_L == -1) ncbi->seq_L = sq->eoff - sq->doff - 1;
1835
1836 /* check if we are at the end */
1837 if (sq->start + sq->n > ncbi->seq_L) {
1838 if (nres != NULL) *nres = 0;
1839 sq->L = ncbi->seq_L;
1840 return eslEOD;
1841 }
1842
1843 /* figure out if the sequence is in digital mode or not */
1844 ptr = (sq->dsq != NULL) ? (unsigned char *)sq->dsq + 1 : (unsigned char *) sq->seq;
1845 ptr += sq->n;
1846
1847 /* calculate where to start reading from */
1848 off = sq->doff + sq->start + sq->n - 1;
1849
1850 /* calculate the size to read */
1851 size = ncbi->seq_L - (sq->start + sq->n - 1);
1852 size = (size > len) ? len : size;
1853
1854 /* seek to the windows location and read into the buffer */
1855 if (fseek(ncbi->fppsq, off, SEEK_SET) != 0) return eslESYS;
1856 if (fread(ptr, sizeof(char), size, ncbi->fppsq) != size) return eslEFORMAT;
1857
1858 /* figure out if the sequence is in digital mode or not */
1859 for (inx = 0; inx < size; ++inx) {
1860 *ptr = sqfp->inmap[(int) *ptr];
1861 if (sq->dsq == NULL) *ptr = ncbi->alphasym[(int) *ptr];
1862 ++ptr;
1863 }
1864
1865 *ptr = (sq->dsq == NULL) ? '\0' : eslDSQ_SENTINEL;
1866
1867 sq->n = sq->n + size;
1868
1869 if (nres != NULL) *nres = size;
1870
1871 return eslOK;
1872 }
1873
1874 /* Function: correct_ambiguity()
1875 * Synopsis: Read in the dna sequence
1876 * Incept: MSF, Thu Feb 4, 2010 [Janelia]
1877 *
1878 * Purpose: Correct any ambiguity characters.
1879 */
1880 static int
1881 correct_ambiguity(ESL_SQFILE *sqfp, ESL_SQ *sq, int len)
1882 {
1883 int64_t alen; /* ambiguity length */
1884 int64_t soff; /* starting offset */
1885 int64_t eoff; /* ending offset */
1886 int64_t ainx; /* ambiguity index */
1887 int64_t size; /* size of table read in */
1888 int64_t cnt; /* repeat count */
1889 int64_t off;
1890 int64_t n;
1891
1892 int amb32; /* flag for 32 or 64 bits */
1893 char *ptr;
1894
1895 unsigned char c;
1896
1897 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
1898
1899 if (ncbi->seq_alen == 0) return eslOK;
1900
1901 /* go to the start of the ambiguity table and see if the table
1902 * is in 32 or 64 bit entries.
1903 */
1904 if (fseek(ncbi->fppsq, ncbi->seq_apos, SEEK_SET) != 0) return eslESYS;
1905 if (fread(ncbi->hdr_buf, sizeof(char), 4, ncbi->fppsq) != 4) return eslEFORMAT;
1906 amb32 = ((ncbi->hdr_buf[0] & 0x80) == 0);
1907
1908 ptr = (sq->dsq != NULL) ? (char *)sq->dsq + 1 : sq->seq;
1909 ptr += sq->n;
1910
1911 /* calculate the starting and ending offsets */
1912 soff = sq->start + sq->n - 1;
1913 eoff = soff + len;
1914
1915 off = 0;
1916 ainx = 0;
1917 size = 0;
1918 alen = ncbi->seq_alen - 4;
1919 while (off < eoff) {
1920 /* check if we need to read in more of the abmiguity table */
1921 if (ainx == size) {
1922 size = alen;
1923 size = (size > INIT_HDR_BUFFER_SIZE) ? INIT_HDR_BUFFER_SIZE : size;
1924 if (fread(ncbi->hdr_buf, sizeof(char), size, ncbi->fppsq) != size) return eslEFORMAT;
1925 alen -= size;
1926 ainx = 0;
1927 }
1928
1929 /* get the ambiguity character */
1930 n = ((ncbi->hdr_buf[ainx] >> 4) & 0x0f);
1931 c = sqfp->inmap[n];
1932 if (sq->dsq == NULL) c = ncbi->alphasym[(int) c];
1933
1934 if (amb32) {
1935 /* get the repeat count 4 bits */
1936 cnt = (ncbi->hdr_buf[ainx] & 0x0f);
1937 cnt += 1;
1938
1939 /* get the offset 24 bits */
1940 off = ncbi->hdr_buf[ainx+1];
1941 off = (off << 8) | ncbi->hdr_buf[ainx+2];
1942 off = (off << 8) | ncbi->hdr_buf[ainx+3];
1943
1944 ainx += 4;
1945 } else {
1946 /* get the repeat count 12 bits */
1947 cnt = (ncbi->hdr_buf[ainx] & 0x0f);
1948 cnt = (cnt << 8) | ncbi->hdr_buf[ainx+1];
1949 cnt += 1;
1950
1951 /* get the offset 48 bits*/
1952 off = ncbi->hdr_buf[ainx+2];
1953 off = (off << 8) | ncbi->hdr_buf[ainx+3];
1954 off = (off << 8) | ncbi->hdr_buf[ainx+4];
1955 off = (off << 8) | ncbi->hdr_buf[ainx+5];
1956 off = (off << 8) | ncbi->hdr_buf[ainx+6];
1957 off = (off << 8) | ncbi->hdr_buf[ainx+7];
1958
1959 ainx += 8;
1960 }
1961
1962 if (off + cnt >= soff && off < eoff) {
1963 int inx;
1964 int start = (off > soff) ? off - soff : 0;
1965 int end = (off + cnt > eoff) ? eoff : off - soff + cnt;
1966 for (inx = start; inx < end; ++inx) ptr[inx] = c;
1967 }
1968
1969 off += cnt;
1970 }
1971
1972 return eslOK;
1973 }
1974
1975 /* Function: read_nres_dna()
1976 * Synopsis: Read in the dna sequence
1977 * Incept: MSF, Wed Jan 27, 2010 [Janelia]
1978 *
1979 * Purpose: Read and translate the dna sequence.
1980 */
1981 static int
1982 read_nres_dna(ESL_SQFILE *sqfp, ESL_SQ *sq, int len, uint64_t *nres)
1983 {
1984 int inx;
1985 int off;
1986 int cnt;
1987 int ncnt;
1988 int start;
1989 int skip;
1990 int text;
1991 int status;
1992
1993 int remainder;
1994 int length;
1995 int ssize;
1996 int n;
1997
1998 unsigned char *ptr;
1999 void *t;
2000
2001 unsigned char c;
2002
2003 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
2004
2005 if (ncbi->index >= ncbi->num_seq) return eslEOF;
2006
2007 /* if we don't know the sequence length, figure it out */
2008 if (ncbi->seq_L == -1) {
2009 if (fseek(ncbi->fppsq, ncbi->seq_apos - 1, SEEK_SET) != 0) return eslESYS;
2010 if (fread(&c, sizeof(char), 1, ncbi->fppsq) != 1) return eslEFORMAT;
2011
2012 ssize = ncbi->seq_apos - sq->doff - 1;
2013 remainder = c & 0x03;
2014 length = ssize * 4 + remainder;
2015
2016 ncbi->seq_L = length;
2017 }
2018
2019 /* check if we are at the end */
2020 if (sq->start + sq->n > ncbi->seq_L) {
2021 if (nres != NULL) *nres = 0;
2022 sq->L = ncbi->seq_L;
2023 return eslEOD;
2024 }
2025
2026 /* calculate where to start reading from */
2027 start = sq->start + sq->n - 1;
2028 off = sq->doff + start / 4;
2029
2030 /* calculate bits to skip at the beginning and end */
2031 cnt = ncbi->seq_L - (sq->start + sq->n - 1);
2032 cnt = (cnt > len) ? len : cnt;
2033
2034 skip = start & 0x03;
2035 remainder = skip + cnt;
2036 remainder = (remainder & 0x03) ? (remainder & 0x03) : 4;
2037
2038 /* calculate bytes need to read in the window */
2039 ssize = (cnt + skip + 3) / 4;
2040
2041 /* seek to the windows location and read into the buffer */
2042 if (fseek(ncbi->fppsq, off, SEEK_SET) != 0) return eslESYS;
2043 if (ncbi->hdr_alloced < ssize) {
2044 while (ncbi->hdr_alloced < ssize) ncbi->hdr_alloced += ncbi->hdr_alloced;
2045 ESL_RALLOC(ncbi->hdr_buf, t, sizeof(char) * ncbi->hdr_alloced);
2046 }
2047 if (fread(ncbi->hdr_buf, sizeof(char), ssize, ncbi->fppsq) != ssize) return eslEFORMAT;
2048
2049 /* figure out if the sequence is in digital mode or not */
2050 if (sq->dsq != NULL) {
2051 text = FALSE;
2052 ptr = (unsigned char *)sq->dsq + 1;
2053 } else {
2054 text = TRUE;
2055 ptr = (unsigned char *)sq->seq;
2056 }
2057 ptr += sq->n;
2058
2059 inx = 0;
2060 c = ncbi->hdr_buf[inx];
2061 for (inx = skip; inx < 4; ++inx) {
2062 n = 1 << ((c >> (6 - inx * 2)) & 0x03);
2063 *ptr = sqfp->inmap[n];
2064 if (text) *ptr = ncbi->alphasym[(int) *ptr];
2065 ++ptr;
2066 }
2067
2068 for (inx = 1; inx < ssize - 1; ++inx) {
2069 c = ncbi->hdr_buf[inx];
2070 n = 1 << ((c >> 6) & 0x03);
2071 *ptr = sqfp->inmap[n];
2072 if (text) *ptr = ncbi->alphasym[(int) *ptr];
2073 ++ptr;
2074 n = 1 << ((c >> 4) & 0x03);
2075 *ptr = sqfp->inmap[n];
2076 if (text) *ptr = ncbi->alphasym[(int) *ptr];
2077 ++ptr;
2078 n = 1 << ((c >> 2) & 0x03);
2079 *ptr = sqfp->inmap[n];
2080 if (text) *ptr = ncbi->alphasym[(int) *ptr];
2081 ++ptr;
2082 n = 1 << ((c >> 0) & 0x03);
2083 *ptr = sqfp->inmap[n];
2084 if (text) *ptr = ncbi->alphasym[(int) *ptr];
2085 ++ptr;
2086 }
2087
2088 /* handle the remainder */
2089 c = ncbi->hdr_buf[inx];
2090 for (inx = 0; inx < remainder; ++inx) {
2091 n = 1 << ((c >> (6 - inx * 2)) & 0x03);
2092 *ptr = sqfp->inmap[n];
2093 if (text) *ptr = ncbi->alphasym[(int) *ptr];
2094 ++ptr;
2095 }
2096
2097 *ptr = (text) ? '\0' : eslDSQ_SENTINEL;
2098
2099 /* calculate the number of residues processed */
2100 ncnt = (ssize - 1) * 4 + remainder - skip;
2101
2102 /* start processing the abmiguity table if there is one */
2103 if (ncbi->seq_alen > 0) {
2104 correct_ambiguity(sqfp, sq, ncnt);
2105 }
2106
2107 sq->n = sq->n + ncnt;
2108
2109 if (nres != NULL) *nres = ncnt;
2110
2111 return eslOK;
2112
2113 ERROR:
2114 return eslEMEM;
2115 }
2116
2117
2118 /* Function: inmap_ncbi()
2119 * Synopsis: Set up a translation map
2120 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2121 *
2122 * Purpose: Initialize the translation map used to translate a ncbi
2123 * sequences to the internal representation used in hmmer.
2124 *
2125 * Returns: <eslOK> on success;
2126 *
2127 * Throws: <eslEMEM> on allocation failure;
2128 */
2129 static int
2130 inmap_ncbi(ESL_SQFILE *sqfp)
2131 {
2132 int status = eslOK;
2133
2134 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
2135
2136 switch(ncbi->alphatype) {
2137 case eslDNA:
2138 status = inmap_ncbi_dna(sqfp);
2139 break;
2140 case eslAMINO:
2141 status = inmap_ncbi_amino(sqfp);
2142 break;
2143 default:
2144 ESL_EXCEPTION(eslEINVAL, "bad alphabet type: unrecognized");
2145 }
2146
2147 return status;
2148 }
2149
2150 /* Function: inmap_ncbi_dna()
2151 * Synopsis: Set up a translation map
2152 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2153 *
2154 * Purpose: Initialize the translation map used to translate a ncbi
2155 * protein sequence to the internal representation used in
2156 * hmmer.
2157 *
2158 * Returns: <eslOK> on success;
2159 *
2160 * Throws: <eslEMEM> on allocation failure;
2161 */
2162 static int
2163 inmap_ncbi_dna(ESL_SQFILE *sqfp)
2164 {
2165 int x, y;
2166 const char *ncbisym = "-ACMGRSVTWYHKDBN";
2167
2168 ESL_ALPHABET *abc = NULL;
2169 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
2170
2171 if ((abc = esl_alphabet_Create(eslDNA)) == NULL) return eslEMEM;
2172
2173 for (x = 0; x < 128; x++) sqfp->inmap[x] = eslDSQ_ILLEGAL;
2174
2175 /* for each letter in the ncbi alphabet, find that letter in the
2176 * hmmer alphabet and map the translation.
2177 */
2178 for (x = 0; x < strlen(ncbisym); ++x) {
2179 for (y = 0; y < strlen(abc->sym); ++y) {
2180 if (ncbisym[x] == abc->sym[y]) {
2181 sqfp->inmap[x] = y;
2182 break;
2183 }
2184 }
2185
2186 /* there is a problem if a translation does not exist */
2187 if (y >= strlen(abc->sym)) return eslEFORMAT;
2188 }
2189
2190 if (ncbi->alphasym == NULL) esl_strdup(abc->sym, -1, &ncbi->alphasym);
2191
2192 esl_alphabet_Destroy(abc);
2193
2194 return eslOK;
2195 }
2196
2197
2198 /* Function: inmap_ncbi_amino()
2199 * Synopsis: Set up a translation map
2200 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2201 *
2202 * Purpose: Initialize the translation map used to translate a ncbi
2203 * protein sequence to the internal representation used in
2204 * hmmer.
2205 *
2206 * Returns: <eslOK> on success;
2207 *
2208 * Throws: <eslEMEM> on allocation failure;
2209 */
2210 static int
2211 inmap_ncbi_amino(ESL_SQFILE *sqfp)
2212 {
2213 int x, y;
2214 const char *ncbisym = "-ABCDEFGHIKLMNPQRSTVWXYZU*OJ";
2215
2216 ESL_ALPHABET *abc = NULL;
2217 ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi;
2218
2219 if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) return eslEMEM;
2220
2221 for (x = 0; x < 128; x++) sqfp->inmap[x] = eslDSQ_ILLEGAL;
2222
2223 /* for each letter in the ncbi alphabet, find that letter in the
2224 * hmmer alphabet and map the translation.
2225 */
2226 for (x = 0; x < strlen(ncbisym); ++x) {
2227 for (y = 0; y < strlen(abc->sym); ++y) {
2228 if (ncbisym[x] == abc->sym[y]) {
2229 sqfp->inmap[x] = y;
2230 break;
2231 }
2232 }
2233
2234 /* there is a problem if a translation does not exist */
2235 if (y >= strlen(abc->sym)) return eslEFORMAT;
2236 }
2237
2238 if (ncbi->alphasym == NULL) esl_strdup(abc->sym, -1, &ncbi->alphasym);
2239
2240 esl_alphabet_Destroy(abc);
2241
2242 return eslOK;
2243 }
2244
2245
2246
2247 /*****************************************************************
2248 *# 5. Parsing routines
2249 *****************************************************************/
2250
2251 /* Function: parse_expect()
2252 * Synopsis: Expect the next bytes to parse match
2253 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2254 *
2255 * Purpose: Match if the next <len> bytes to parse match the bytes
2256 * in <str>. If the bytes do not match, throw <eslEFORMAT>
2257 * error. Advance the parsers pointer.
2258 *
2259 * Returns: <eslOK> on success
2260 *
2261 * Throws: <eslEFORMAT> if there are insufficient bytes remaining
2262 * in the header or if the data to parse does not match
2263 * what is expected.
2264 */
2265 static int
2266 parse_expect(ESL_SQNCBI_DATA *ncbi, void *str, int len)
2267 {
2268 int size;
2269 unsigned char *c;
2270 unsigned char *limit;
2271
2272 size = ncbi->hoff - ncbi->roff;
2273 limit = ncbi->hdr_buf + size;
2274
2275 /* verify the buffer has atleast len bytes remaining */
2276 if (ncbi->hdr_ptr + len > limit) {
2277 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Expecting %d bytes at %d : 0x%X(%d)\n",
2278 len, (uint32_t) (ncbi->hdr_ptr - ncbi->hdr_buf), ncbi->roff, size);
2279 }
2280
2281 /* check the buffer matches the token string */
2282 c = (unsigned char *) str;
2283 while (len--) {
2284 if (*ncbi->hdr_ptr != *c) {
2285 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Expecting 0x%X found 0x%X at %d : 0x%X(%d)\n",
2286 *ncbi->hdr_ptr, *c, (uint32_t) (ncbi->hdr_ptr - ncbi->hdr_buf), ncbi->roff, size);
2287 }
2288 ncbi->hdr_ptr++;
2289 c++;
2290 }
2291
2292 return eslOK;
2293 }
2294
2295 /* Function: parse_accept()
2296 * Synopsis: Check if the next bytes to parse match
2297 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2298 *
2299 * Purpose: Check if the next <len> bytes to parse match the bytes
2300 * in <str>. If the bytes match, they are consumed and the
2301 * parsers pointer is advanced.
2302 *
2303 * Returns: <eslOK> on success
2304 * <eslEFORMAT> if the bytes to not match.
2305 */
2306 static int
2307 parse_accept(ESL_SQNCBI_DATA *ncbi, void *str, int len)
2308 {
2309 int i;
2310 int size;
2311 unsigned char *c;
2312 unsigned char *limit;
2313
2314 size = ncbi->hoff - ncbi->roff;
2315 limit = ncbi->hdr_buf + size;
2316
2317 /* check the buffer matches the token string */
2318 if (ncbi->hdr_ptr + len > limit) return eslEFORMAT;
2319
2320 /* verify the buffer matches the token string without advancing
2321 * the buffer pointers until we have a complete match.
2322 */
2323 c = (unsigned char *) str;
2324 for (i = 0; i < len; ++i) {
2325 if (ncbi->hdr_ptr[i] != c[i]) return eslEFORMAT;
2326 }
2327 ncbi->hdr_ptr += len;
2328
2329 return eslOK;
2330 }
2331
2332 /* Function: parse_peek()
2333 * Synopsis: Peek at the next byte to parse
2334 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2335 *
2336 * Purpose: Return the next characer to be parsed without advancing the
2337 * parsers pointer.
2338 *
2339 * Returns: <eslOK> on success
2340 * <eslEFORMAT> if there are insufficient bytes remaining
2341 * in the header.
2342 */
2343 static int
2344 parse_peek(ESL_SQNCBI_DATA *ncbi, unsigned char *c)
2345 {
2346 int size;
2347 unsigned char *limit;
2348
2349 size = ncbi->hoff - ncbi->roff;
2350 limit = ncbi->hdr_buf + size;
2351
2352 /* verify the buffer has atleast len bytes remaining */
2353 if (ncbi->hdr_ptr + 1 > limit) return eslEFORMAT;
2354
2355 *c = *ncbi->hdr_ptr;
2356
2357 return eslOK;
2358 }
2359
2360 /* Function: parse_consume()
2361 * Synopsis: Copies bytes from the header
2362 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2363 *
2364 * Purpose: Copies <len> bytes from the header to the buffer supplied by
2365 * <str> if non-null. Adcance the parser pointer.
2366 *
2367 * Returns: <eslOK> on success
2368 *
2369 * Throws: <eslEFORMAT> if there are insufficient bytes remaining
2370 * in the header.
2371 */
2372 static int
2373 parse_consume(ESL_SQNCBI_DATA *ncbi, void *str, int len)
2374 {
2375 int i;
2376 int size;
2377 unsigned char *c;
2378 unsigned char *limit;
2379
2380 size = ncbi->hoff - ncbi->roff;
2381 limit = ncbi->hdr_buf + size;
2382
2383 /* verify the buffer has atleast len bytes remaining */
2384 if (ncbi->hdr_ptr + len > limit) {
2385 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Expecting %d bytes at %d : 0x%X(%d)\n",
2386 len, (uint32_t) (ncbi->hdr_ptr - ncbi->hdr_buf), ncbi->roff, size);
2387 }
2388
2389 /* copy the characters in the buffer to <str> */
2390 c = (unsigned char *) str;
2391 for (i = 0; i < len; ++i) {
2392 if (c != NULL) *c++ = *ncbi->hdr_ptr++;
2393 }
2394
2395 return eslOK;
2396 }
2397
2398 /* Function: parse_advance()
2399 * Synopsis: Advance the parser pointer
2400 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2401 *
2402 * Purpose: Advance the parser pointer <len> bytes.
2403 *
2404 * Returns: <eslOK> on success
2405 *
2406 * Throws: <eslEFORMAT> if there are insufficient bytes remaining
2407 * in the header.
2408 */
2409 static int
2410 parse_advance(ESL_SQNCBI_DATA *ncbi, int len)
2411 {
2412 int size;
2413 unsigned char *limit;
2414
2415 size = ncbi->hoff - ncbi->roff;
2416 limit = ncbi->hdr_buf + size;
2417
2418 /* verify the buffer has atleast len bytes remaining */
2419 if (ncbi->hdr_ptr + len > limit) {
2420 ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Expecting %d bytes at %d : 0x%X(%d)\n",
2421 len, (uint32_t) (ncbi->hdr_ptr - ncbi->hdr_buf), ncbi->roff, size);
2422 }
2423
2424 ncbi->hdr_ptr += len;
2425
2426 return eslOK;
2427 }
2428
2429 /* Function: parse_header()
2430 * Synopsis: Parse the ncbi db header
2431 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2432 *
2433 * Purpose: Parse a ncbi database header. This routine implements
2434 * a recursive descent parser for the ASN.1 definition of
2435 * a blast database header filling in <sq>.
2436 *
2437 * The blast db header can have multiple definitions defined
2438 * within it. Only the information from the first usable
2439 * defition will be used.
2440 *
2441 * Returns: <eslOK> on success; the new sequence is stored in <s>.
2442 *
2443 * Returns <eslEFORMAT> if there's a problem with the format,
2444 * such as an illegal character.
2445 */
2446 static int
2447 parse_header(ESL_SQNCBI_DATA *ncbi, ESL_SQ *sq)
2448 {
2449 int size;
2450 int status;
2451 void *tmp;
2452
2453 unsigned char c;
2454
2455 reset_header_values(ncbi);
2456 size = ncbi->hoff - ncbi->roff;
2457
2458 /* read in the header data */
2459 if (ncbi->hdr_alloced < size) {
2460 while (ncbi->hdr_alloced < size) ncbi->hdr_alloced += ncbi->hdr_alloced;
2461 ESL_RALLOC(ncbi->hdr_buf, tmp, sizeof(char) * ncbi->hdr_alloced);
2462 }
2463 if (fread(ncbi->hdr_buf, sizeof(char), size, ncbi->fpphr) != size) return eslEFORMAT;
2464 ncbi->hdr_ptr = ncbi->hdr_buf;
2465
2466 /* verify we are at the beginning of a structure */
2467 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2468
2469 if (parse_peek(ncbi, &c) != eslOK) return eslEFORMAT;
2470
2471 /* parse the different seq id structures */
2472 while (c != 0x00) {
2473 if ((status = parse_def_line(ncbi, sq)) != eslOK) return status;
2474
2475 if (parse_peek(ncbi, &c) != eslOK) return eslEFORMAT;
2476 }
2477
2478 /* verify we are at the end of the structure */
2479 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2480
2481 return eslOK;
2482 ERROR:
2483 return eslEMEM;
2484 }
2485
2486 /* Function: parse_def_line()
2487 * Synopsis: Parse the Blast-def-line definition
2488 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2489 *
2490 * Blast-def-line ::= SEQUENCE {
2491 * title VisibleString OPTIONAL, -- simple title
2492 * seqid SEQUENCE OF Seq-id, -- Regular NCBI Seq-Id
2493 * taxid INTEGER OPTIONAL, -- taxonomy id
2494 * memberships SEQUENCE OF INTEGER OPTIONAL, -- bit arrays
2495 * links SEQUENCE OF INTEGER OPTIONAL, -- bit arrays
2496 * other-info SEQUENCE OF INTEGER OPTIONAL -- for future use (probably genomic sequences)
2497 * }
2498 */
2499 static int
2500 parse_def_line(ESL_SQNCBI_DATA *ncbi, ESL_SQ *sq)
2501 {
2502 int status;
2503
2504 int i;
2505 int len = 0;
2506 int taxid = -1;
2507 char *title = NULL;
2508
2509 /* verify we are at the beginning of a structure */
2510 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2511
2512 /* look for an optional title */
2513 sq->desc[0] = 0;
2514 if (parse_accept(ncbi, "\xa0\x80", 2) == eslOK) {
2515 if ((status = parse_string(ncbi, &title, &len)) != eslOK) return status;
2516 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2517 }
2518
2519 /* look for sequence id structure */
2520 if (parse_expect(ncbi, "\xa1\x80", 2) != eslOK) return eslEFORMAT;
2521 if ((status = parse_seq_id(ncbi)) != eslOK) return status;
2522 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2523
2524 /* look for an optional taxonomy id */
2525 sq->tax_id = -1;
2526 if (parse_accept(ncbi, "\xa2\x80", 2) == eslOK) {
2527 if ((status = parse_integer(ncbi, &taxid)) != eslOK) return status;
2528 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2529 }
2530
2531 /* look for an optional memberships */
2532 if (parse_accept(ncbi, "\xa3\x80", 2) == eslOK) {
2533 if ((status = ignore_sequence_of_integer(ncbi)) != eslOK) return status;
2534 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2535 }
2536
2537 /* look for an optional links */
2538 if (parse_accept(ncbi, "\xa4\x80", 2) == eslOK) {
2539 if ((status = ignore_sequence_of_integer(ncbi)) != eslOK) return status;
2540 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2541 }
2542
2543 /* look for an optional other info */
2544 if (parse_accept(ncbi, "\xa5\x80", 2) == eslOK) {
2545 if ((status = ignore_sequence_of_integer(ncbi)) != eslOK) return status;
2546 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2547 }
2548
2549 /* verify we are at the end of the structure */
2550 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2551
2552 /* zero terminate any saved string */
2553 if (ncbi->name_ptr != NULL) ncbi->name_ptr[ncbi->name_size] = '\0';
2554 if (ncbi->acc_ptr != NULL) ncbi->acc_ptr[ncbi->acc_size] = '\0';
2555 if (ncbi->str_id_ptr != NULL) ncbi->str_id_ptr[ncbi->str_id_size] = '\0';
2556
2557 if (title != NULL) title[len] = '\0';
2558
2559 if (ncbi->name_ptr != NULL || ncbi->acc_ptr != NULL) {
2560 /* if we have both a name and accession, set both fields.
2561 * if we have only one, set the name to that one field.
2562 */
2563 if (ncbi->name_ptr != NULL) {
2564 esl_sq_SetName(sq, ncbi->name_ptr);
2565 if (ncbi->acc_ptr != NULL) {
2566 esl_sq_SetAccession(sq, ncbi->acc_ptr);
2567 }
2568 } else {
2569 esl_sq_SetName(sq, ncbi->acc_ptr);
2570 }
2571 if (title != NULL) esl_sq_SetDesc(sq, title);
2572 } else if (ncbi->str_id_ptr != NULL || ncbi->int_id != -1) {
2573 /* since we don't have a name or accession, use the id
2574 * as the name.
2575 */
2576 if (ncbi->str_id_ptr != NULL) {
2577 esl_sq_SetName(sq, ncbi->str_id_ptr);
2578 } else {
2579 char id[32];
2580 snprintf(id, 32, "%d", ncbi->int_id);
2581 esl_sq_SetName(sq, id);
2582 }
2583 if (title != NULL) esl_sq_SetDesc(sq, title);
2584 } else if (title != NULL) {
2585 /* lastly we don't have anything, so lets just use the
2586 * title. take the first word of the title and use that
2587 * for the name. the remaining portion of the title will
2588 * be used for the description.
2589 */
2590 for (i = 0; i < len; ++i) {
2591 if (isspace(title[i])) {
2592 title[i] = '\0';
2593 break;
2594 }
2595 }
2596 esl_sq_SetName(sq, title);
2597 ++i;
2598
2599 /* skip over multiple spaces till the next word */
2600 for ( ; i < len; ++i) {
2601 if (!isspace(title[i])) break;
2602 }
2603 if (i < len) esl_sq_SetDesc(sq, title + i);
2604 }
2605
2606 if (taxid != -1) sq->tax_id = taxid;
2607
2608 return eslOK;
2609 }
2610
2611
2612 /* Function: parse_seq_id()
2613 * Synopsis: Parse the Blast-def-line definition
2614 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2615 *
2616 * Seq-id ::= CHOICE {
2617 * local Object-id , -- local use
2618 * gibbsq INTEGER , -- Geninfo backbone seqid
2619 * gibbmt INTEGER , -- Geninfo backbone moltype
2620 * giim Giimport-id , -- Geninfo import id
2621 * genbank Textseq-id ,
2622 * embl Textseq-id ,
2623 * pir Textseq-id ,
2624 * swissprot Textseq-id ,
2625 * patent Patent-seq-id ,
2626 * other Textseq-id , -- for historical reasons, 'other' = 'refseq'
2627 * general Dbtag , -- for other databases
2628 * gi INTEGER , -- GenInfo Integrated Database
2629 * ddbj Textseq-id , -- DDBJ
2630 * prf Textseq-id , -- PRF SEQDB
2631 * pdb PDB-seq-id , -- PDB sequence
2632 * tpg Textseq-id , -- Third Party Annot/Seq GenBank
2633 * tpe Textseq-id , -- Third Party Annot/Seq EMBL
2634 * tpd Textseq-id , -- Third Party Annot/Seq DDBJ
2635 * gpipe Textseq-id , -- Internal NCBI genome pipeline processing ID
2636 * named-annot-track Textseq-id -- Internal named annotation tracking ID
2637 * }
2638 */
2639 static int
2640 parse_seq_id(ESL_SQNCBI_DATA *ncbi)
2641 {
2642 int status;
2643 int *id_ptr;
2644
2645 unsigned char c;
2646
2647 /* verify we are at the beginning of a structure */
2648 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2649
2650 if (parse_consume(ncbi, &c, 1) != eslOK) return eslEFORMAT;
2651
2652 /* parse the different seq id structures */
2653 while (c != 0x00) {
2654 if (parse_expect(ncbi, "\x80", 1) != eslOK) return eslEFORMAT;
2655 switch (c) {
2656 case 0xa0: /* LOCAL */
2657 status = parse_object_id(ncbi);
2658 break;
2659 case 0xa1: /* GIBBSQ */
2660 id_ptr = (ncbi->int_id != -1) ? NULL : &ncbi->int_id;
2661 status = parse_integer(ncbi, id_ptr);
2662 break;
2663 case 0xa2: /* GIBBMT */
2664 status = parse_integer(ncbi, NULL);
2665 break;
2666 case 0xa3: /* GIIM */
2667 status = parse_giimport_id(ncbi);
2668 break;
2669 case 0xa4: /* GENBANK */
2670 case 0xa5: /* EMBL */
2671 case 0xa6: /* PIR */
2672 case 0xa7: /* SWISSPROT */
2673 status = parse_textseq_id(ncbi);
2674 break;
2675 case 0xa8: /* PATENT */
2676 status = parse_patent_seq_id(ncbi);
2677 break;
2678 case 0xa9: /* OTHER */
2679 status = parse_textseq_id(ncbi);
2680 break;
2681 case 0xaa: /* GENERAL */
2682 status = parse_dbtag(ncbi);
2683 break;
2684 case 0xab: /* GI */
2685 status = parse_integer(ncbi, NULL);
2686 break;
2687 case 0xac: /* DDBJ */
2688 case 0xad: /* PRF */
2689 status = parse_textseq_id(ncbi);
2690 break;
2691 case 0xae: /* PDB */
2692 status = parse_pdb_seq_id(ncbi);
2693 break;
2694 case 0xaf: /* TPG */
2695 case 0xb0: /* TPE */
2696 case 0xb1: /* TPD */
2697 case 0xb2: /* GPIPE */
2698 case 0xb3: /* NAMED ANNOT TRACK */
2699 status = parse_textseq_id(ncbi);
2700 break;
2701 default:
2702 status = eslEFORMAT;
2703 }
2704
2705 if (status != eslOK) return status;
2706 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2707 if (parse_consume(ncbi, &c, 1) != eslOK) return eslEFORMAT;
2708 }
2709
2710 /* verify we are at the end of the structure */
2711 if (c != 0x00 || parse_expect(ncbi, "\x00", 1) != eslOK) return eslEFORMAT;
2712
2713 return eslOK;
2714 }
2715
2716
2717 /* Function: parse_textseq_id()
2718 * Synopsis: Parse the general text header
2719 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2720 *
2721 * Textseq-id ::= SEQUENCE {
2722 * name VisibleString OPTIONAL ,
2723 * accession VisibleString OPTIONAL ,
2724 * release VisibleString OPTIONAL ,
2725 * version INTEGER OPTIONAL
2726 * }
2727 */
2728 static int
2729 parse_textseq_id(ESL_SQNCBI_DATA *ncbi)
2730 {
2731 char *acc = NULL;
2732 int alen = 0;
2733 char *name = NULL;
2734 int nlen = 0;
2735
2736 int status;
2737
2738 /* verify we are at the beginning of a structure */
2739 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2740
2741 /* look for an optional name */
2742 if (parse_accept(ncbi, "\xa0\x80", 2) == eslOK) {
2743 if ((status = parse_string(ncbi, &name, &nlen)) != eslOK) return status;
2744 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2745 }
2746
2747 /* look for an optional accession */
2748 if (parse_accept(ncbi, "\xa1\x80", 2) == eslOK) {
2749 if ((status = parse_string(ncbi, &acc, &alen)) != eslOK) return status;
2750 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2751 }
2752
2753 /* look for an optional release */
2754 if (parse_accept(ncbi, "\xa2\x80", 2) == eslOK) {
2755 if ((status = parse_string(ncbi, NULL, NULL)) != eslOK) return status;
2756 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2757 }
2758
2759 /* look for an optional version */
2760 if (parse_accept(ncbi, "\xa3\x80", 2) == eslOK) {
2761 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
2762 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2763 }
2764
2765 /* verify we are at the end of the structure */
2766 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2767
2768 /* if we found both the accession and name and so far
2769 * we have only come across incomplete headers, save
2770 * this one off.
2771 */
2772 if (acc != NULL && name != NULL) {
2773 if (ncbi->name_ptr == NULL || ncbi->acc_ptr == NULL) {
2774 ncbi->name_ptr = name;
2775 ncbi->name_size = nlen;
2776 ncbi->acc_ptr = acc;
2777 ncbi->acc_size = alen;
2778 }
2779
2780 } else if (ncbi->name_ptr == NULL && ncbi->acc_ptr == NULL) {
2781 /* if neither the accession or name have been set, and the
2782 * header supplied one, save it off.
2783 */
2784 if (acc != NULL) {
2785 ncbi->acc_ptr = acc;
2786 ncbi->acc_size = alen;
2787 }
2788 if (name != NULL) {
2789 ncbi->name_ptr = name;
2790 ncbi->name_size = nlen;
2791 }
2792 }
2793
2794 return eslOK;
2795 }
2796
2797
2798 /* Function: parse_dbtag()
2799 * Synopsis: Parse the a general db tag
2800 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2801 *
2802 * Dbtag ::= SEQUENCE {
2803 * db VisibleString , -- name of database or system
2804 * tag Object-id -- appropriate tag
2805 * }
2806 */
2807 static int
2808 parse_dbtag(ESL_SQNCBI_DATA *ncbi)
2809 {
2810 int status;
2811 int temp_id;
2812
2813 /* verify we are at the beginning of a structure */
2814 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2815
2816 /* look for an db name */
2817 if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT;
2818 if ((status = parse_string(ncbi, 0, NULL)) != eslOK) return status;
2819
2820 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2821
2822 /* it looks like the dbtag is used when formatdb is run
2823 * without parsing sequence ids (ie -o F). if that is
2824 * the case, the id is equal to the sequence number in
2825 * the database. so for dbtag headers, nothing will be
2826 * saved. to do this lets create a bogus id value and
2827 * restore it after dbtag is parsed.
2828 */
2829 temp_id = ncbi->int_id;
2830 ncbi->int_id = 1;
2831
2832 /* look for a tag object */
2833 if (parse_expect(ncbi, "\xa1\x80", 2) != eslOK) return eslEFORMAT;
2834 if ((status = parse_object_id(ncbi)) != eslOK) return status;
2835 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2836
2837 /* restore the id value */
2838 ncbi->int_id = temp_id;
2839
2840 /* verify we are at the end of the structure */
2841 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2842
2843 return eslOK;
2844 }
2845
2846
2847 /* Function: parse_giimport_id()
2848 * Synopsis: Parse the a giimport id
2849 * Incept: MSF, Thu Mar 25, 2010 [Janelia]
2850 *
2851 * Giimport-id ::= SEQUENCE {
2852 * id INTEGER, -- the id to use here
2853 * db VisibleString OPTIONAL, -- dbase used in
2854 * release VisibleString OPTIONAL } -- the release
2855 * }
2856 */
2857 static int
2858 parse_giimport_id(ESL_SQNCBI_DATA *ncbi)
2859 {
2860 int status;
2861 int id;
2862
2863 /* verify we are at the beginning of a structure */
2864 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2865
2866 /* look for an id */
2867 if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT;
2868 if ((status = parse_integer(ncbi, &id)) != eslOK) return status;
2869
2870 /* look for an optional database */
2871 if (parse_accept(ncbi, "\xa1\x80", 2) == eslOK) {
2872 if ((status = parse_string(ncbi, NULL, NULL)) != eslOK) return status;
2873 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2874 }
2875
2876 /* look for an optional release */
2877 if (parse_accept(ncbi, "\xa2\x80", 2) == eslOK) {
2878 if ((status = parse_string(ncbi, NULL, NULL)) != eslOK) return status;
2879 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2880 }
2881
2882 /* verify we are at the end of the structure */
2883 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2884
2885 /* if there is not already a saved seq id, save it */
2886 if (ncbi->int_id == -1 && ncbi->str_id_ptr == NULL) {
2887 ncbi->int_id = id;
2888 }
2889
2890 return eslOK;
2891 }
2892
2893
2894 /* Function: parse_patent_seq_id()
2895 * Synopsis: Parse the patent header
2896 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2897 *
2898 * Patent-seq-id ::= SEQUENCE {
2899 * seqid INTEGER , -- number of sequence in patent
2900 * cit Id-pat -- patent citation
2901 * }
2902 */
2903 static int
2904 parse_patent_seq_id(ESL_SQNCBI_DATA *ncbi)
2905 {
2906 int status;
2907 int id;
2908
2909 /* verify we are at the beginning of a structure */
2910 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2911
2912 /* look for a seqid */
2913 if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT;
2914 if ((status = parse_integer(ncbi, &id)) != eslOK) return status;
2915
2916 /* look for a patent citation object */
2917 if (parse_expect(ncbi, "\xa1\x80", 2) != eslOK) return eslEFORMAT;
2918 if ((status = parse_id_pat(ncbi)) != eslOK) return status;
2919
2920 /* verify we are at the end of the structure */
2921 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2922
2923 /* if there is not already a saved seq id, save it */
2924 if (ncbi->int_id == -1 && ncbi->str_id_ptr == NULL) {
2925 ncbi->int_id = id;
2926 }
2927
2928 return eslOK;
2929 }
2930
2931
2932 /* Function: parse_id_pat()
2933 * Synopsis: Parse the patent citation
2934 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2935 *
2936 * Id-pat ::= SEQUENCE { -- just to identify a patent
2937 * country VisibleString , -- Patent Document Country
2938 * id CHOICE {
2939 * number VisibleString , -- Patent Document Number
2940 * app-number VisibleString -- Patent Doc Appl Number
2941 * } ,
2942 * doc-type VisibleString OPTIONAL -- Patent Doc Type
2943 * }
2944 */
2945 static int
2946 parse_id_pat(ESL_SQNCBI_DATA *ncbi)
2947 {
2948 int status;
2949
2950 /* verify we are at the beginning of a structure */
2951 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2952
2953 /* look for a country */
2954 if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT;
2955 if ((status = parse_string(ncbi, NULL, NULL)) != eslOK) return status;
2956
2957 /* look for an id */
2958 if (parse_expect(ncbi, "\xa1\x80", 2) != eslOK) return eslEFORMAT;
2959
2960 /* the id is a choice of two strings */
2961
2962 /* verify we are at the beginning of a structure */
2963 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
2964
2965 /* look for an optional taxonomy id */
2966 if (parse_accept(ncbi, "\xa0\x80", 2) == eslOK) {
2967 status = parse_string(ncbi, NULL, NULL);
2968 } else if (parse_accept(ncbi, "\xa1\x80", 2) == eslOK) {
2969 status = parse_string(ncbi, NULL, NULL);
2970 } else {
2971 status = eslEFORMAT;
2972 }
2973 if (status != eslOK) return status;
2974
2975 /* verify we are at the end of the structure */
2976 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2977
2978 /* look for a doc type */
2979 if (parse_accept(ncbi, "\xa3\x80", 2) == eslOK) {
2980 if ((status = parse_string(ncbi, NULL, NULL)) != eslOK) return eslEFORMAT;
2981 }
2982
2983 /* verify we are at the end of the structure */
2984 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
2985
2986 return eslOK;
2987 }
2988
2989
2990 /* Function: parse_object_id()
2991 * Synopsis: Parse a generic sequence id
2992 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
2993 *
2994 * Object-id ::= CHOICE {
2995 * id INTEGER ,
2996 * str VisibleString
2997 * }
2998 */
2999 static int
3000 parse_object_id(ESL_SQNCBI_DATA *ncbi)
3001 {
3002 int status;
3003
3004 char *id_str = NULL;
3005 int id_len = 0;
3006 int id = -1;
3007
3008 /* look for an optional taxonomy id */
3009 if (parse_accept(ncbi, "\xa0\x80", 2) == eslOK) {
3010 status = parse_integer(ncbi, &id);
3011 } else if (parse_accept(ncbi, "\xa1\x80", 2) == eslOK) {
3012 status = parse_string(ncbi, &id_str, &id_len);
3013 } else {
3014 status = eslEFORMAT;
3015 }
3016
3017 /* verify we are at the end of the structure */
3018 if (status == eslOK) {
3019 status = parse_expect(ncbi, "\x00\x00", 2);
3020
3021 /* if there is not already a saved seq id, save it */
3022 if (ncbi->int_id == -1 && ncbi->str_id_ptr == NULL) {
3023 if (id_str != NULL) {
3024 ncbi->str_id_ptr = id_str;
3025 ncbi->str_id_size = id_len;
3026 } else if (id != -1) {
3027 ncbi->int_id = id;
3028 }
3029 }
3030 }
3031
3032 return status;
3033 }
3034
3035
3036 /* Function: parse_pdb_seq_id()
3037 * Synopsis: Parse a PDB sequence
3038 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
3039 *
3040 * PDB-seq-id ::= SEQUENCE {
3041 * mol PDB-mol-id , -- the molecule name
3042 * chain INTEGER , -- a single ASCII character, chain id
3043 * rel Date OPTIONAL } -- release date, month and year
3044 *
3045 * Date ::= CHOICE {
3046 * str VisibleString , -- for those unparsed dates
3047 * std Date-std -- use this if you can
3048 * }
3049 */
3050 static int
3051 parse_pdb_seq_id(ESL_SQNCBI_DATA *ncbi)
3052 {
3053 int status;
3054
3055 char *id;
3056 int len;
3057
3058 /* verify we are at the beginning of a structure */
3059 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
3060
3061 /* look for an pdb mol id */
3062 if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT;
3063 if ((status = parse_string(ncbi, &id, &len)) != eslOK) return status;
3064 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3065
3066 /* look for chain */
3067 if (parse_accept(ncbi, "\xa1\x80", 2) == eslOK) {
3068 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
3069 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3070 }
3071
3072 /* look for an optional date */
3073 if (parse_accept(ncbi, "\xa2\x80", 2) == eslOK) {
3074 if (parse_accept(ncbi, "\xa0\x80", 2) == eslOK) {
3075 status = parse_string(ncbi, NULL, NULL);
3076 } else if (parse_accept(ncbi, "\xa1\x80", 2) == eslOK) {
3077 status = parse_date_std(ncbi);
3078 } else {
3079 status = eslEFORMAT;
3080 }
3081 if (status != eslOK) return status;
3082 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3083 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3084 }
3085
3086 /* if there is not already a saved seq id, save it */
3087 if (ncbi->int_id == -1 && ncbi->str_id_ptr == NULL) {
3088 ncbi->str_id_ptr = id;
3089 ncbi->str_id_size = len;
3090 }
3091
3092 /* verify we are at the end of the structure */
3093 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3094
3095 return eslOK;
3096 }
3097
3098
3099 /* Function: parse_date_std()
3100 * Synopsis: Parse the data structure
3101 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
3102 *
3103 * Date-std ::= SEQUENCE { -- NOTE: this is NOT a unix tm struct
3104 * year INTEGER , -- full year (including 1900)
3105 * month INTEGER OPTIONAL , -- month (1-12)
3106 * day INTEGER OPTIONAL , -- day of month (1-31)
3107 * season VisibleString OPTIONAL , -- for "spring", "may-june", etc
3108 * hour INTEGER OPTIONAL , -- hour of day (0-23)
3109 * minute INTEGER OPTIONAL , -- minute of hour (0-59)
3110 * second INTEGER OPTIONAL -- second of minute (0-59)
3111 * }
3112 */
3113 static int
3114 parse_date_std(ESL_SQNCBI_DATA *ncbi)
3115 {
3116 int status;
3117
3118 /* verify we are at the beginning of a structure */
3119 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
3120
3121 /* look for a year */
3122 if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT;
3123 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
3124 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3125
3126 /* look for an optional month */
3127 if (parse_accept(ncbi, "\xa1\x80", 2) == eslOK) {
3128 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
3129 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3130 }
3131
3132 /* look for an optional day */
3133 if (parse_accept(ncbi, "\xa2\x80", 2) == eslOK) {
3134 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
3135 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3136 }
3137
3138 /* look for an optional season */
3139 if (parse_accept(ncbi, "\xa3\x80", 2) == eslOK) {
3140 if ((status = parse_string(ncbi, NULL, NULL)) != eslOK) return status;
3141 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3142 }
3143
3144 /* look for an optional hour */
3145 if (parse_accept(ncbi, "\xa4\x80", 2) == eslOK) {
3146 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
3147 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3148 }
3149
3150 /* look for an optional minute */
3151 if (parse_accept(ncbi, "\xa5\x80", 2) == eslOK) {
3152 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
3153 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3154 }
3155
3156 /* look for an optional second */
3157 if (parse_accept(ncbi, "\xa6\x80", 2) == eslOK) {
3158 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
3159 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3160 }
3161
3162 /* verify we are at the end of the structure */
3163 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3164
3165 return eslOK;
3166 }
3167
3168
3169 /* Function: parse_string()
3170 * Synopsis: Parse a visible string
3171 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
3172 *
3173 * Purpose: Parses a string from the header stream.
3174 *
3175 * If <str> is non null, the location of the string in
3176 * the header will be saved. If <len> is non null, the
3177 * length of the string will be filled in. If <str> is
3178 * non null, then <len> must be non null since the strings
3179 * are not zero terminated.
3180 *
3181 * Returns: <eslOK> on success.
3182 * <eslEFORMAT> if there's a problem with the format.
3183 * <eslEINCOMPAT> if <str> is non null and <len> is null.
3184 *
3185 */
3186 static int
3187 parse_string(ESL_SQNCBI_DATA *ncbi, char **str, int *len)
3188 {
3189 int n;
3190
3191 unsigned char x;
3192 unsigned char c;
3193 unsigned char *ptr;
3194
3195 if (parse_expect(ncbi, "\x1a", 1) != eslOK) return eslEFORMAT;
3196
3197 /* the next byte is the length of the string. if the length is
3198 * less than 128, then this is the true length; otherwise this
3199 * length describes the number of bytes necessary to hold the
3200 * true length of the string in the lower 7 bits.
3201 */
3202 if (parse_consume(ncbi, &c, 1) != eslOK) return eslEFORMAT;
3203 if (c < 128) {
3204 n = c;
3205 } else {
3206 c = c & 0x7f;
3207 if (c > sizeof(n)) return eslEFORMAT;
3208
3209 n = 0;
3210 while (c > 0) {
3211 if (parse_consume(ncbi, &x, 1) != eslOK) return eslEFORMAT;
3212 n = (n << 8) + (unsigned int) x;
3213 --c;
3214 }
3215 }
3216
3217 /* validate the length of the string */
3218 ptr = ncbi->hdr_ptr;
3219 if (parse_advance(ncbi, n) != eslOK) return eslEFORMAT;
3220
3221 /* fill in the values */
3222 if (str != NULL && len == NULL) return eslEINCOMPAT;
3223 if (len != NULL) *len = n;
3224 if (str != NULL) *str = (char *)ptr;
3225
3226 return eslOK;
3227 }
3228
3229
3230 /* Function: parse_integer()
3231 * Synopsis: Parse an integer
3232 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
3233 *
3234 * Purpose: Reads an integer from the header stream. If the integer is
3235 * more bytes than the native int format, the most significant
3236 * bytes will be lost.
3237 *
3238 * If <value> is non null, the parsed integer will be placed
3239 * in the pointer.
3240 *
3241 * Returns: <eslOK> on success.
3242 * <eslEFORMAT> if there's a problem with the format.
3243 *
3244 */
3245 static int
3246 parse_integer(ESL_SQNCBI_DATA *ncbi, int *value)
3247 {
3248 int n;
3249
3250 unsigned char c;
3251 unsigned char *ptr;
3252
3253 if (parse_expect(ncbi, "\x02", 1) != eslOK) return eslEFORMAT;
3254
3255 /* get the length of the integer */
3256 if (parse_peek(ncbi, &c) != eslOK) return eslEFORMAT;
3257 ptr = ncbi->hdr_ptr + 1;
3258
3259 /* advance past the integer to make sure the buffer holds all
3260 * of the integer. the pointer <ptr> points the the start of
3261 * the integer.
3262 */
3263 if (parse_advance(ncbi, c + 1) != eslOK) return eslEFORMAT;
3264
3265 n = 0;
3266 while (c > 0) {
3267 n = (n << 8) + (unsigned int) *ptr++;
3268 --c;
3269 }
3270
3271 if (value != NULL) *value = n;
3272
3273 return eslOK;
3274 }
3275
3276
3277 /* Function: ignore_sequence_of_integer()
3278 * Synopsis: Skip over the sequence of integers
3279 * Incept: MSF, Mon Dec 10, 2009 [Janelia]
3280 *
3281 * Purpose: Skip over a sequence of integers.
3282 *
3283 * Returns: <eslOK> on success.
3284 * <eslEFORMAT> if there's a problem with the format.
3285 *
3286 */
3287 static int
3288 ignore_sequence_of_integer(ESL_SQNCBI_DATA *ncbi)
3289 {
3290 int status;
3291 unsigned char c;
3292
3293 /* verify we are at the beginning of a structure */
3294 if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT;
3295
3296 if (parse_peek(ncbi, &c) != eslOK) return eslEFORMAT;
3297 while (c == 0x02) {
3298 if ((status = parse_integer(ncbi, NULL)) != eslOK) return status;
3299 if (parse_peek(ncbi, &c) != eslOK) return eslEFORMAT;
3300 }
3301
3302 /* verify we are at the end of the structure */
3303 if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT;
3304
3305 return eslOK;
3306 }