| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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" |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
int |
| 123 |
esl_sqncbi_Open(char *filename, int format, ESL_SQFILE *sqfp) |
| 124 |
{ |
| 125 |
int i; |
| 126 |
int status = eslOK; |
| 127 |
|
| 128 |
ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi; |
| 129 |
|
| 130 |
|
| 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 |
|
| 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 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 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; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static int |
| 259 |
sqncbi_DbOpen(ESL_SQNCBI_DATA *ncbi, char *filename, int dbtype) |
| 260 |
{ |
| 261 |
int status = eslOK; |
| 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 |
|
| 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 |
|
| 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 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
static int |
| 314 |
sqncbi_AliasOpen(ESL_SQNCBI_DATA *ncbi, char *filename, int dbtype) |
| 315 |
{ |
| 316 |
int status = eslOK; |
| 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 |
|
| 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 |
|
| 348 |
dbsize = (dbsize > len) ? dbsize : len * 2; |
| 349 |
ESL_ALLOC(dbname, sizeof(char) * dbsize); |
| 350 |
strcpy(dbname, filename); |
| 351 |
|
| 352 |
|
| 353 |
while (len > 0 && dbname[len-1] != eslDIRSLASH) { |
| 354 |
dbname[len-1] = '\0'; |
| 355 |
--len; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 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 |
|
| 374 |
if (!done) { |
| 375 |
newline = 0; |
| 376 |
while (*ptr != '\0') { |
| 377 |
if (*ptr == '\n') newline = 1; |
| 378 |
++ptr; |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
done = 0; |
| 385 |
dblen = len; |
| 386 |
while (!done) { |
| 387 |
|
| 388 |
|
| 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 |
|
| 398 |
if (isspace(*ptr)) { |
| 399 |
if (dblen > len) { |
| 400 |
dbname[dblen++] = '\0'; |
| 401 |
|
| 402 |
|
| 403 |
|
| 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 |
|
| 415 |
reset_db(ncbi); |
| 416 |
|
| 417 |
|
| 418 |
vol = ncbi->volumes++; |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 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 |
|
| 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 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
static int |
| 498 |
sqncbi_Open(ESL_SQNCBI_DATA *ncbi, char *filename) |
| 499 |
{ |
| 500 |
int status = eslOK; |
| 501 |
char *name = NULL; |
| 502 |
|
| 503 |
|
| 504 |
status = sqncbi_DbOpen(ncbi, filename, NCBI_AMINO_DB); |
| 505 |
|
| 506 |
|
| 507 |
if (status == eslENOTFOUND) { |
| 508 |
status = sqncbi_AliasOpen(ncbi, filename, NCBI_AMINO_DB); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
if (status == eslENOTFOUND) { |
| 513 |
status = sqncbi_DbOpen(ncbi, filename, NCBI_DNA_DB); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
if (status == eslENOTFOUND) { |
| 518 |
status = sqncbi_AliasOpen(ncbi, filename, NCBI_DNA_DB); |
| 519 |
} |
| 520 |
|
| 521 |
if (status != eslOK) goto ERROR; |
| 522 |
|
| 523 |
|
| 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 |
|
| 528 |
|
| 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 |
|
| 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 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 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 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 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 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
static int |
| 668 |
sqncbi_SetDigital(ESL_SQFILE *sqfp, const ESL_ALPHABET *abc) |
| 669 |
{ |
| 670 |
return eslOK; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 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 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
static int |
| 706 |
sqncbi_IsRewindable(const ESL_SQFILE *sqfp) |
| 707 |
{ |
| 708 |
return TRUE; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
static const char * |
| 718 |
sqncbi_GetError(const ESL_SQFILE *sqfp) |
| 719 |
{ |
| 720 |
return sqfp->data.ncbi.errbuf; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
static int |
| 750 |
sqncbi_Read(ESL_SQFILE *sqfp, ESL_SQ *sq) |
| 751 |
{ |
| 752 |
|
| 753 |
int status; |
| 754 |
|
| 755 |
ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi; |
| 756 |
|
| 757 |
|
| 758 |
if (ncbi->index >= ncbi->num_seq) return eslEOF; |
| 759 |
|
| 760 |
if ((status = pos_sequence(ncbi, ncbi->index)) != eslOK) return status; |
| 761 |
|
| 762 |
|
| 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 |
|
| 776 |
if ((status = parse_header(ncbi, sq)) != eslOK) return status; |
| 777 |
|
| 778 |
(ncbi->index)++; |
| 779 |
|
| 780 |
return eslOK; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
static int |
| 801 |
sqncbi_ReadInfo(ESL_SQFILE *sqfp, ESL_SQ *sq) |
| 802 |
{ |
| 803 |
|
| 804 |
int status; |
| 805 |
|
| 806 |
ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi; |
| 807 |
|
| 808 |
|
| 809 |
if (ncbi->index >= ncbi->num_seq) return eslEOF; |
| 810 |
|
| 811 |
if ((status = pos_sequence(ncbi, ncbi->index)) != eslOK) return status; |
| 812 |
|
| 813 |
|
| 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 |
|
| 821 |
sq->L = -1; |
| 822 |
|
| 823 |
|
| 824 |
if ((status = parse_header(ncbi, sq)) != eslOK) return status; |
| 825 |
|
| 826 |
(ncbi->index)++; |
| 827 |
|
| 828 |
return eslOK; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
static int |
| 852 |
sqncbi_ReadSequence(ESL_SQFILE *sqfp, ESL_SQ *sq) |
| 853 |
{ |
| 854 |
|
| 855 |
int status; |
| 856 |
|
| 857 |
ESL_SQNCBI_DATA *ncbi = &sqfp->data.ncbi; |
| 858 |
|
| 859 |
|
| 860 |
if (ncbi->index >= ncbi->num_seq) return eslEOF; |
| 861 |
|
| 862 |
if ((status = pos_sequence(ncbi, ncbi->index)) != eslOK) return status; |
| 863 |
|
| 864 |
|
| 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 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 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 |
|
| 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 |
|
| 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 |
{ |
| 998 |
|
| 999 |
|
| 1000 |
sq->start = 0; |
| 1001 |
sq->end = 0; |
| 1002 |
sq->C = 0; |
| 1003 |
sq->W = 0; |
| 1004 |
sq->n = 0; |
| 1005 |
|
| 1006 |
if (sq->dsq != NULL) sq->dsq[1] = eslDSQ_SENTINEL; |
| 1007 |
else sq->seq[0] = '\0'; |
| 1008 |
|
| 1009 |
return eslEOD; |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 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 |
{ |
| 1024 |
sq->C = ESL_MIN(C, sq->L - sq->end + 1); |
| 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 |
|
| 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 |
|
| 1051 |
else |
| 1052 |
{ |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
if (sq->start == 0) |
| 1058 |
{ |
| 1059 |
if (ncbi->index >= ncbi->num_seq) return eslEOF; |
| 1060 |
|
| 1061 |
|
| 1062 |
if ((status = pos_sequence(ncbi, ncbi->index)) != eslOK) return status; |
| 1063 |
|
| 1064 |
|
| 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 |
|
| 1075 |
if ((status = parse_header(ncbi, sq)) != eslOK) return status; |
| 1076 |
|
| 1077 |
sq->start = 1; |
| 1078 |
sq->C = 0; |
| 1079 |
sq->L = -1; |
| 1080 |
ncbi->seq_L = -1; |
| 1081 |
esl_sq_SetSource(sq, sq->name); |
| 1082 |
|
| 1083 |
} |
| 1084 |
else |
| 1085 |
{ |
| 1086 |
sq->C = ESL_MIN(C, sq->n); |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 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; |
| 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; |
| 1114 |
else sq->seq[0] = '\0'; |
| 1115 |
|
| 1116 |
return eslEOD; |
| 1117 |
} |
| 1118 |
else if (status == eslOK) |
| 1119 |
{ |
| 1120 |
sq->end = sq->start + sq->C + nres - 1; |
| 1121 |
sq->W = nres; |
| 1122 |
return eslOK; |
| 1123 |
} |
| 1124 |
else return status; |
| 1125 |
} |
| 1126 |
|
| 1127 |
return eslOK; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 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 |
{ |
| 1186 |
|
| 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 |
{ |
| 1201 |
|
| 1202 |
|
| 1203 |
if (max_residues < 0) |
| 1204 |
max_residues = MAX_RESIDUE_COUNT; |
| 1205 |
|
| 1206 |
tmpsq = esl_sq_CreateDigital(sqBlock->list->abc); |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
if (! sqBlock->complete ) |
| 1211 |
{ |
| 1212 |
|
| 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 |
{ |
| 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); |
| 1228 |
if (status == eslEOD) |
| 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 |
|
| 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; |
| 1249 |
} |
| 1250 |
sqBlock->list->L = tmpsq->L; |
| 1251 |
} |
| 1252 |
} |
| 1253 |
else if (status == eslEOD) |
| 1254 |
{ |
| 1255 |
|
| 1256 |
} |
| 1257 |
else |
| 1258 |
{ |
| 1259 |
if(tmpsq != NULL) esl_sq_Destroy(tmpsq); |
| 1260 |
return status; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
for ( ; i < sqBlock->listSize && size < max_residues; ++i) |
| 1266 |
{ |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 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; |
| 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 |
{ |
| 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); |
| 1292 |
if (status == eslEOD) |
| 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 |
|
| 1307 |
|
| 1308 |
|
| 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 |
|
| 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; |
| 1322 |
} |
| 1323 |
|
| 1324 |
status = eslOK; |
| 1325 |
} |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
|
| 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 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 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 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 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 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 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 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 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; |
| 1418 |
|
| 1419 |
char *name; |
| 1420 |
|
| 1421 |
if (volume < 0 || volume > ncbi->volumes) return eslEINVAL; |
| 1422 |
|
| 1423 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1474 |
fgetc(ncbi->fppsq); |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 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 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 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 |
|
| 1519 |
if (ncbi->index_start == -1 || inx < start || inx > end) { |
| 1520 |
|
| 1521 |
|
| 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 |
|
| 1532 |
if (cnt >= ncbi->volumes) return eslFAIL; |
| 1533 |
|
| 1534 |
if ((status = volume_open(ncbi, cnt)) != eslOK) return status; |
| 1535 |
} |
| 1536 |
} |
| 1537 |
|
| 1538 |
|
| 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 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
if (ncbi->volumes > 0) { |
| 1552 |
cnt = volume->end_seq - inx + 2; |
| 1553 |
start = start - volume->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 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 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 |
|
| 1628 |
if (esl_sq_GrowTo(sq, size) != eslOK) return eslEMEM; |
| 1629 |
|
| 1630 |
|
| 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 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 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 |
|
| 1693 |
|
| 1694 |
|
| 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 |
|
| 1708 |
if (esl_sq_GrowTo(sq, length) != eslOK) return eslEMEM; |
| 1709 |
|
| 1710 |
|
| 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 |
|
| 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 |
|
| 1751 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1770 |
cnt = (ncbi->hdr_buf[ssize] & 0x0f); |
| 1771 |
cnt += 1; |
| 1772 |
|
| 1773 |
|
| 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 |
|
| 1783 |
cnt = (ncbi->hdr_buf[ssize] & 0x0f); |
| 1784 |
cnt = (cnt << 8) | ncbi->hdr_buf[ssize+1]; |
| 1785 |
cnt += 1; |
| 1786 |
|
| 1787 |
|
| 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 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 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 |
|
| 1834 |
if (ncbi->seq_L == -1) ncbi->seq_L = sq->eoff - sq->doff - 1; |
| 1835 |
|
| 1836 |
|
| 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 |
|
| 1844 |
ptr = (sq->dsq != NULL) ? (unsigned char *)sq->dsq + 1 : (unsigned char *) sq->seq; |
| 1845 |
ptr += sq->n; |
| 1846 |
|
| 1847 |
|
| 1848 |
off = sq->doff + sq->start + sq->n - 1; |
| 1849 |
|
| 1850 |
|
| 1851 |
size = ncbi->seq_L - (sq->start + sq->n - 1); |
| 1852 |
size = (size > len) ? len : size; |
| 1853 |
|
| 1854 |
|
| 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 |
|
| 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 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
static int |
| 1881 |
correct_ambiguity(ESL_SQFILE *sqfp, ESL_SQ *sq, int len) |
| 1882 |
{ |
| 1883 |
int64_t alen; |
| 1884 |
int64_t soff; |
| 1885 |
int64_t eoff; |
| 1886 |
int64_t ainx; |
| 1887 |
int64_t size; |
| 1888 |
int64_t cnt; |
| 1889 |
int64_t off; |
| 1890 |
int64_t n; |
| 1891 |
|
| 1892 |
int amb32; |
| 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 |
|
| 1902 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1936 |
cnt = (ncbi->hdr_buf[ainx] & 0x0f); |
| 1937 |
cnt += 1; |
| 1938 |
|
| 1939 |
|
| 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 |
|
| 1947 |
cnt = (ncbi->hdr_buf[ainx] & 0x0f); |
| 1948 |
cnt = (cnt << 8) | ncbi->hdr_buf[ainx+1]; |
| 1949 |
cnt += 1; |
| 1950 |
|
| 1951 |
|
| 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 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2027 |
start = sq->start + sq->n - 1; |
| 2028 |
off = sq->doff + start / 4; |
| 2029 |
|
| 2030 |
|
| 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 |
|
| 2039 |
ssize = (cnt + skip + 3) / 4; |
| 2040 |
|
| 2041 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2100 |
ncnt = (ssize - 1) * 4 + remainder - skip; |
| 2101 |
|
| 2102 |
|
| 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 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 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 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 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 |
|
| 2176 |
|
| 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 |
|
| 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 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 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 |
|
| 2224 |
|
| 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 |
|
| 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 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 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 |
|
| 2318 |
if (ncbi->hdr_ptr + len > limit) return eslEFORMAT; |
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 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 |
|
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 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 |
|
| 2353 |
if (ncbi->hdr_ptr + 1 > limit) return eslEFORMAT; |
| 2354 |
|
| 2355 |
*c = *ncbi->hdr_ptr; |
| 2356 |
|
| 2357 |
return eslOK; |
| 2358 |
} |
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
|
| 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 |
|
| 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 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 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 |
|
| 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 |
|
| 2467 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2468 |
|
| 2469 |
if (parse_peek(ncbi, &c) != eslOK) return eslEFORMAT; |
| 2470 |
|
| 2471 |
|
| 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 |
|
| 2479 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 2480 |
|
| 2481 |
return eslOK; |
| 2482 |
ERROR: |
| 2483 |
return eslEMEM; |
| 2484 |
} |
| 2485 |
|
| 2486 |
|
| 2487 |
|
| 2488 |
|
| 2489 |
|
| 2490 |
|
| 2491 |
|
| 2492 |
|
| 2493 |
|
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 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 |
|
| 2510 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2511 |
|
| 2512 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2550 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 2551 |
|
| 2552 |
|
| 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 |
|
| 2561 |
|
| 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 |
|
| 2574 |
|
| 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 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 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 |
|
| 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 |
|
| 2613 |
|
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 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 |
|
| 2648 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2649 |
|
| 2650 |
if (parse_consume(ncbi, &c, 1) != eslOK) return eslEFORMAT; |
| 2651 |
|
| 2652 |
|
| 2653 |
while (c != 0x00) { |
| 2654 |
if (parse_expect(ncbi, "\x80", 1) != eslOK) return eslEFORMAT; |
| 2655 |
switch (c) { |
| 2656 |
case 0xa0: |
| 2657 |
status = parse_object_id(ncbi); |
| 2658 |
break; |
| 2659 |
case 0xa1: |
| 2660 |
id_ptr = (ncbi->int_id != -1) ? NULL : &ncbi->int_id; |
| 2661 |
status = parse_integer(ncbi, id_ptr); |
| 2662 |
break; |
| 2663 |
case 0xa2: |
| 2664 |
status = parse_integer(ncbi, NULL); |
| 2665 |
break; |
| 2666 |
case 0xa3: |
| 2667 |
status = parse_giimport_id(ncbi); |
| 2668 |
break; |
| 2669 |
case 0xa4: |
| 2670 |
case 0xa5: |
| 2671 |
case 0xa6: |
| 2672 |
case 0xa7: |
| 2673 |
status = parse_textseq_id(ncbi); |
| 2674 |
break; |
| 2675 |
case 0xa8: |
| 2676 |
status = parse_patent_seq_id(ncbi); |
| 2677 |
break; |
| 2678 |
case 0xa9: |
| 2679 |
status = parse_textseq_id(ncbi); |
| 2680 |
break; |
| 2681 |
case 0xaa: |
| 2682 |
status = parse_dbtag(ncbi); |
| 2683 |
break; |
| 2684 |
case 0xab: |
| 2685 |
status = parse_integer(ncbi, NULL); |
| 2686 |
break; |
| 2687 |
case 0xac: |
| 2688 |
case 0xad: |
| 2689 |
status = parse_textseq_id(ncbi); |
| 2690 |
break; |
| 2691 |
case 0xae: |
| 2692 |
status = parse_pdb_seq_id(ncbi); |
| 2693 |
break; |
| 2694 |
case 0xaf: |
| 2695 |
case 0xb0: |
| 2696 |
case 0xb1: |
| 2697 |
case 0xb2: |
| 2698 |
case 0xb3: |
| 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 |
|
| 2711 |
if (c != 0x00 || parse_expect(ncbi, "\x00", 1) != eslOK) return eslEFORMAT; |
| 2712 |
|
| 2713 |
return eslOK; |
| 2714 |
} |
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
|
| 2720 |
|
| 2721 |
|
| 2722 |
|
| 2723 |
|
| 2724 |
|
| 2725 |
|
| 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 |
|
| 2739 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2740 |
|
| 2741 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2766 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
|
| 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 |
|
| 2782 |
|
| 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 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
static int |
| 2808 |
parse_dbtag(ESL_SQNCBI_DATA *ncbi) |
| 2809 |
{ |
| 2810 |
int status; |
| 2811 |
int temp_id; |
| 2812 |
|
| 2813 |
|
| 2814 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2815 |
|
| 2816 |
|
| 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 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
|
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
temp_id = ncbi->int_id; |
| 2830 |
ncbi->int_id = 1; |
| 2831 |
|
| 2832 |
|
| 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 |
|
| 2838 |
ncbi->int_id = temp_id; |
| 2839 |
|
| 2840 |
|
| 2841 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 2842 |
|
| 2843 |
return eslOK; |
| 2844 |
} |
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
|
| 2856 |
|
| 2857 |
static int |
| 2858 |
parse_giimport_id(ESL_SQNCBI_DATA *ncbi) |
| 2859 |
{ |
| 2860 |
int status; |
| 2861 |
int id; |
| 2862 |
|
| 2863 |
|
| 2864 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2865 |
|
| 2866 |
|
| 2867 |
if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT; |
| 2868 |
if ((status = parse_integer(ncbi, &id)) != eslOK) return status; |
| 2869 |
|
| 2870 |
|
| 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 |
|
| 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 |
|
| 2883 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 2884 |
|
| 2885 |
|
| 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 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
|
| 2900 |
|
| 2901 |
|
| 2902 |
|
| 2903 |
static int |
| 2904 |
parse_patent_seq_id(ESL_SQNCBI_DATA *ncbi) |
| 2905 |
{ |
| 2906 |
int status; |
| 2907 |
int id; |
| 2908 |
|
| 2909 |
|
| 2910 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2911 |
|
| 2912 |
|
| 2913 |
if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT; |
| 2914 |
if ((status = parse_integer(ncbi, &id)) != eslOK) return status; |
| 2915 |
|
| 2916 |
|
| 2917 |
if (parse_expect(ncbi, "\xa1\x80", 2) != eslOK) return eslEFORMAT; |
| 2918 |
if ((status = parse_id_pat(ncbi)) != eslOK) return status; |
| 2919 |
|
| 2920 |
|
| 2921 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 2922 |
|
| 2923 |
|
| 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 |
|
| 2933 |
|
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
|
| 2938 |
|
| 2939 |
|
| 2940 |
|
| 2941 |
|
| 2942 |
|
| 2943 |
|
| 2944 |
|
| 2945 |
static int |
| 2946 |
parse_id_pat(ESL_SQNCBI_DATA *ncbi) |
| 2947 |
{ |
| 2948 |
int status; |
| 2949 |
|
| 2950 |
|
| 2951 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2952 |
|
| 2953 |
|
| 2954 |
if (parse_expect(ncbi, "\xa0\x80", 2) != eslOK) return eslEFORMAT; |
| 2955 |
if ((status = parse_string(ncbi, NULL, NULL)) != eslOK) return status; |
| 2956 |
|
| 2957 |
|
| 2958 |
if (parse_expect(ncbi, "\xa1\x80", 2) != eslOK) return eslEFORMAT; |
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 2964 |
|
| 2965 |
|
| 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 |
|
| 2976 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 2977 |
|
| 2978 |
|
| 2979 |
if (parse_accept(ncbi, "\xa3\x80", 2) == eslOK) { |
| 2980 |
if ((status = parse_string(ncbi, NULL, NULL)) != eslOK) return eslEFORMAT; |
| 2981 |
} |
| 2982 |
|
| 2983 |
|
| 2984 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 2985 |
|
| 2986 |
return eslOK; |
| 2987 |
} |
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 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 |
|
| 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 |
|
| 3018 |
if (status == eslOK) { |
| 3019 |
status = parse_expect(ncbi, "\x00\x00", 2); |
| 3020 |
|
| 3021 |
|
| 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 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
|
| 3040 |
|
| 3041 |
|
| 3042 |
|
| 3043 |
|
| 3044 |
|
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 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 |
|
| 3059 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 3060 |
|
| 3061 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 3093 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 3094 |
|
| 3095 |
return eslOK; |
| 3096 |
} |
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
|
| 3104 |
|
| 3105 |
|
| 3106 |
|
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
|
| 3111 |
|
| 3112 |
|
| 3113 |
static int |
| 3114 |
parse_date_std(ESL_SQNCBI_DATA *ncbi) |
| 3115 |
{ |
| 3116 |
int status; |
| 3117 |
|
| 3118 |
|
| 3119 |
if (parse_expect(ncbi, "\x30\x80", 2) != eslOK) return eslEFORMAT; |
| 3120 |
|
| 3121 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 3163 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 3164 |
|
| 3165 |
return eslOK; |
| 3166 |
} |
| 3167 |
|
| 3168 |
|
| 3169 |
|
| 3170 |
|
| 3171 |
|
| 3172 |
|
| 3173 |
|
| 3174 |
|
| 3175 |
|
| 3176 |
|
| 3177 |
|
| 3178 |
|
| 3179 |
|
| 3180 |
|
| 3181 |
|
| 3182 |
|
| 3183 |
|
| 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 |
|
| 3198 |
|
| 3199 |
|
| 3200 |
|
| 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 |
|
| 3218 |
ptr = ncbi->hdr_ptr; |
| 3219 |
if (parse_advance(ncbi, n) != eslOK) return eslEFORMAT; |
| 3220 |
|
| 3221 |
|
| 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 |
|
| 3231 |
|
| 3232 |
|
| 3233 |
|
| 3234 |
|
| 3235 |
|
| 3236 |
|
| 3237 |
|
| 3238 |
|
| 3239 |
|
| 3240 |
|
| 3241 |
|
| 3242 |
|
| 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 |
|
| 3256 |
if (parse_peek(ncbi, &c) != eslOK) return eslEFORMAT; |
| 3257 |
ptr = ncbi->hdr_ptr + 1; |
| 3258 |
|
| 3259 |
|
| 3260 |
|
| 3261 |
|
| 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 |
|
| 3278 |
|
| 3279 |
|
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
|
| 3284 |
|
| 3285 |
|
| 3286 |
|
| 3287 |
static int |
| 3288 |
ignore_sequence_of_integer(ESL_SQNCBI_DATA *ncbi) |
| 3289 |
{ |
| 3290 |
int status; |
| 3291 |
unsigned char c; |
| 3292 |
|
| 3293 |
|
| 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 |
|
| 3303 |
if (parse_expect(ncbi, "\x00\x00", 2) != eslOK) return eslEFORMAT; |
| 3304 |
|
| 3305 |
return eslOK; |
| 3306 |
} |