hmmer-3.3/easel/esl_sqio_ncbi.c
/* pos_sequence_offsets()
 *
 * Position the sequence and header files for reading at
 * the start of the indexed sequence <inx>.  This routine
 * buffers <INDEX_TABLE_SIZE> offsets from the header and
 * sequence files.  If the index <inx> is not in the
 * currently buffered table, read the the indexes.  If the
 * index is not in the current volume find which volume
 * the indexed sequence is in and open up that database.
 */
pos_sequence_offsets()
Position the sequence and header files for reading at
the start of the indexed sequence .  This routine
buffers  offsets from the header and
sequence files.  If the index  is not in the
currently buffered table, read the the indexes.  If the
index is not in the current volume find which volume
the indexed sequence is in and open up that database.
pos_sequence_offsets () Position the sequence and header files for reading at the start of the indexed sequence . This routine buffers offsets from the header and sequence files . If the index is not in the currently buffered table read the the indexes . If the index is not in the current volume find which volume the indexed sequence is in and open up that database .
static int
pos_sequence(ESL_SQNCBI_DATA *ncbi, int inx)
{
  int        cnt;
  int        status;
  uint32_t   offset;
  uint32_t   start;
  uint32_t   end;
  ESL_SQNCBI_VOLUME *volume;
  if (inx < 0 || inx > ncbi->num_seq) return eslEINVAL;
  start = ncbi->index_start;
  end   = ncbi->index_end;
  if (ncbi->index_start == -1 || inx < start || inx > end) {
    if (ncbi->volumes > 0) {
      volume = ncbi->vols + ncbi->vol_index;
      if (inx < volume->start_seq || inx > volume->end_seq) {
	volume = ncbi->vols;
	for (cnt = 0; cnt < ncbi->volumes; ++cnt) {
	  if (inx < volume->end_seq) break;
	  ++volume;
	}
	if (cnt >= ncbi->volumes) return eslFAIL;
	if ((status = volume_open(ncbi, cnt)) != eslOK) return status;
      }
    }
    if (ncbi->index_start == -1 || inx > end) {
      start = inx;
    } else {
      start = inx + 2;
      start = (start > INDEX_TABLE_SIZE) ? start - INDEX_TABLE_SIZE : 0;
    }
    ncbi->index_start = start;
    if (ncbi->volumes > 0) {
      cnt = volume->end_seq - inx + 2;     
      start = start - volume->start_seq;   
    } else {
      cnt = ncbi->num_seq - inx + 1;
    }
    cnt = (cnt > INDEX_TABLE_SIZE) ? INDEX_TABLE_SIZE : cnt;
    ncbi->index_end = ncbi->index_start + cnt - 2;
    offset = ncbi->hdr_off + (sizeof(uint32_t) * start);
    if (fseek(ncbi->fppin, offset, SEEK_SET) != 0) {
      ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error seeking header index %d\n", offset);
    }
    if (fread(ncbi->hdr_indexes, sizeof(uint32_t), cnt, ncbi->fppin) != cnt) {
      ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error reading header index %d at %d(%d)\n", start, offset, cnt);
    }
    offset = ncbi->seq_off + (sizeof(uint32_t) * start);
    if (fseek(ncbi->fppin, offset, SEEK_SET) != 0) {
      ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error seeking sequence index %d\n", offset);
    }
    if (fread(ncbi->seq_indexes, sizeof(uint32_t), cnt, ncbi->fppin) != cnt) {
      ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error reading sequence index %d at %d(%d)\n", start, offset, cnt);
    }
    if (ncbi->alphatype == eslDNA) {
      offset = ncbi->amb_off + (sizeof(uint32_t) * start);
      if (fseek(ncbi->fppin, offset, SEEK_SET) != 0) {
	ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error seeking ambiguity index %d\n", offset);
      }
      if (fread(ncbi->amb_indexes, sizeof(uint32_t), cnt, ncbi->fppin) != cnt) {
	ESL_FAIL(eslEFORMAT, ncbi->errbuf, "Error reading ambiguity index %d at %d(%d)\n", start, offset, cnt);
      }
    }
  }
  ncbi->index = inx;
  inx -= ncbi->index_start;
  ncbi->roff = htobe32(ncbi->hdr_indexes[inx]);
  ncbi->doff = htobe32(ncbi->seq_indexes[inx]);
  ncbi->hoff = htobe32(ncbi->hdr_indexes[inx+1]);
  ncbi->eoff = htobe32(ncbi->seq_indexes[inx+1]);
  if (ncbi->alphatype == eslDNA) {
    ncbi->seq_apos = htobe32(ncbi->amb_indexes[inx]);
    ncbi->seq_alen = ncbi->seq_apos + htobe32(ncbi->amb_indexes[inx+1]) + 1;
  } else {
    ncbi->seq_apos = 0;
    ncbi->seq_alen = 0;
  }
  if (fseek(ncbi->fpphr, ncbi->roff, SEEK_SET) != 0) return eslESYS;
  if (fseek(ncbi->fppsq, ncbi->doff, SEEK_SET) != 0) return eslESYS;
  return eslOK;
}
static int pos_sequence ( ESL_SQNCBI_DATA * ncbi , int inx ) { int cnt ; int status ; uint32_t offset ; uint32_t start ; uint32_t end ; ESL_SQNCBI_VOLUME * volume ; if ( inx < 0 || inx > ncbi -> num_seq ) return eslEINVAL ; start = ncbi -> index_start ; end = ncbi -> index_end ; if ( ncbi -> index_start == -1 || inx < start || inx > end ) { if ( ncbi -> volumes > 0 ) { volume = ncbi -> vols + ncbi -> vol_index ; if ( inx < volume -> start_seq || inx > volume -> end_seq ) { volume = ncbi -> vols ; for ( cnt = 0 ; cnt < ncbi -> volumes ; ++ cnt ) { if ( inx < volume -> end_seq ) break ; ++ volume ; } if ( cnt >= ncbi -> volumes ) return eslFAIL ; if ( ( status = volume_open ( ncbi , cnt ) ) != eslOK ) return status ; } } if ( ncbi -> index_start == -1 || inx > end ) { start = inx ; } else { start = inx + 2 ; start = ( start > INDEX_TABLE_SIZE ) ? start - INDEX_TABLE_SIZE : 0 ; } ncbi -> index_start = start ; if ( ncbi -> volumes > 0 ) { cnt = volume -> end_seq - inx + 2 ; start = start - volume -> start_seq ; } else { cnt = ncbi -> num_seq - inx + 1 ; } cnt = ( cnt > INDEX_TABLE_SIZE ) ? INDEX_TABLE_SIZE : cnt ; ncbi -> index_end = ncbi -> index_start + cnt - 2 ; offset = ncbi -> hdr_off + ( sizeof ( uint32_t ) * start ) ; if ( fseek ( ncbi -> fppin , offset , SEEK_SET ) != 0 ) { ESL_FAIL ( eslEFORMAT , ncbi -> errbuf , " \n " , offset ) ; } if ( fread ( ncbi -> hdr_indexes , sizeof ( uint32_t ) , cnt , ncbi -> fppin ) != cnt ) { ESL_FAIL ( eslEFORMAT , ncbi -> errbuf , " \n " , start , offset , cnt ) ; } offset = ncbi -> seq_off + ( sizeof ( uint32_t ) * start ) ; if ( fseek ( ncbi -> fppin , offset , SEEK_SET ) != 0 ) { ESL_FAIL ( eslEFORMAT , ncbi -> errbuf , " \n " , offset ) ; } if ( fread ( ncbi -> seq_indexes , sizeof ( uint32_t ) , cnt , ncbi -> fppin ) != cnt ) { ESL_FAIL ( eslEFORMAT , ncbi -> errbuf , " \n " , start , offset , cnt ) ; } if ( ncbi -> alphatype == eslDNA ) { offset = ncbi -> amb_off + ( sizeof ( uint32_t ) * start ) ; if ( fseek ( ncbi -> fppin , offset , SEEK_SET ) != 0 ) { ESL_FAIL ( eslEFORMAT , ncbi -> errbuf , " \n " , offset ) ; } if ( fread ( ncbi -> amb_indexes , sizeof ( uint32_t ) , cnt , ncbi -> fppin ) != cnt ) { ESL_FAIL ( eslEFORMAT , ncbi -> errbuf , " \n " , start , offset , cnt ) ; } } } ncbi -> index = inx ; inx -= ncbi -> index_start ; ncbi -> roff = htobe32 ( ncbi -> hdr_indexes [ inx ] ) ; ncbi -> doff = htobe32 ( ncbi -> seq_indexes [ inx ] ) ; ncbi -> hoff = htobe32 ( ncbi -> hdr_indexes [ inx + 1 ] ) ; ncbi -> eoff = htobe32 ( ncbi -> seq_indexes [ inx + 1 ] ) ; if ( ncbi -> alphatype == eslDNA ) { ncbi -> seq_apos = htobe32 ( ncbi -> amb_indexes [ inx ] ) ; ncbi -> seq_alen = ncbi -> seq_apos + htobe32 ( ncbi -> amb_indexes [ inx + 1 ] ) + 1 ; } else { ncbi -> seq_apos = 0 ; ncbi -> seq_alen = 0 ; } if ( fseek ( ncbi -> fpphr , ncbi -> roff , SEEK_SET ) != 0 ) return eslESYS ; if ( fseek ( ncbi -> fppsq , ncbi -> doff , SEEK_SET ) != 0 ) return eslESYS ; return eslOK ; }
pos_sequence_offsets()
 Position the sequence and header files for reading at
 the start of the indexed sequence <inx>.
pos_sequence_offsets () Position the sequence and header files for reading at the start of the indexed sequence <inx > .
/* get the offsets for the header, sequence and ambiguity table */ /* if the db is broken up into volumes, lets find the correct one to use */ /* check just to make sure we found the volume */ /* adjust where we start reading from if we are reading forwards or backwards */ /* when calculating the count be sure to take into account the fact that the
     * index tables contain one index more that the number of sequences and this
     * last index is used to point to the end of the last header and sequences.
     */ // cppcheck thinks end_seq can be uninitialized here. I think it's wrong. //  .. and ditto for start_seq.