hmmer-3.3/src/fm_general.c
/* Function:  fm_FM_read()
 * Synopsis:  Read the FM index off disk
 * Purpose:   Read the FM-index as written by fmbuild.
 *            First read the metadata header, then allocate space for the full index,
 *            then read it in.
 */
fm_FM_read()
Synopsis:  Read the FM index off disk
Purpose:   Read the FM-index as written by fmbuild.
First read the metadata header, then allocate space for the full index,
then read it in.
fm_FM_read () Synopsis : Read the FM index off disk Purpose : Read the FM - index as written by fmbuild . First read the metadata header then allocate space for the full index then read it in .
int
fm_FM_read( FM_DATA *fm, FM_METADATA *meta, int getAll )
{
  int64_t *C               = NULL;
  int i;
  uint32_t *occCnts_sb = NULL;
  int32_t compressed_bytes;
  int num_freq_cnts_b;
  int num_freq_cnts_sb;
  int num_SA_samples;
  int64_t prevC;
  int cnt;
  int chars_per_byte = 8/meta->charBits;
  int status;
  if(fread(&(fm->N), sizeof(uint64_t), 1, meta->fp) !=  1)
    esl_fatal( "%s: Error reading block_length in FM index.\n", __FILE__);
  if(fread(&(fm->term_loc), sizeof(uint32_t), 1, meta->fp) !=  1)
    esl_fatal( "%s: Error reading terminal location in FM index.\n", __FILE__);
  if(fread(&(fm->seq_offset), sizeof(uint32_t), 1, meta->fp) !=  1)
    esl_fatal( "%s: Error reading seq_offset in FM index.\n", __FILE__);
  if(fread(&(fm->ambig_offset), sizeof(uint32_t), 1, meta->fp) !=  1)
    esl_fatal( "%s: Error reading ambig_offset in FM index.\n", __FILE__);
  if(fread(&(fm->overlap), sizeof(uint32_t), 1, meta->fp) !=  1)
    esl_fatal( "%s: Error reading overlap in FM index.\n", __FILE__);
  if(fread(&(fm->seq_cnt), sizeof(uint32_t), 1, meta->fp) !=  1)
    esl_fatal( "%s: Error reading seq_cnt in FM index.\n", __FILE__);
  if(fread(&(fm->ambig_cnt), sizeof(uint32_t), 1, meta->fp) !=  1)
    esl_fatal( "%s: Error reading ambig_cnt in FM index.\n", __FILE__);
  compressed_bytes =   ((chars_per_byte-1+fm->N)/chars_per_byte);
  num_freq_cnts_b  = 1+ceil((double)fm->N/meta->freq_cnt_b);
  num_freq_cnts_sb = 1+ceil((double)fm->N/meta->freq_cnt_sb);
  num_SA_samples   = floor((double)fm->N/meta->freq_SA);
  if (getAll) ESL_ALLOC (fm->T, sizeof(uint8_t) * compressed_bytes );
  ESL_ALLOC (fm->BWT_mem,  sizeof(uint8_t) * (compressed_bytes + 31) ); 
     fm->BWT =   (uint8_t *) (((unsigned long int)fm->BWT_mem + 15) & (~0xf));   
  if (getAll) ESL_ALLOC (fm->SA, num_SA_samples * sizeof(uint32_t));
  ESL_ALLOC (fm->C, (1+meta->alph_size) * sizeof(int64_t));
  ESL_ALLOC (fm->occCnts_b,  num_freq_cnts_b *  (meta->alph_size ) * sizeof(uint16_t)); 
  ESL_ALLOC (fm->occCnts_sb,  num_freq_cnts_sb *  (meta->alph_size ) * sizeof(uint32_t)); 
  if(getAll && fread(fm->T, sizeof(uint8_t), compressed_bytes, meta->fp) != compressed_bytes)
    esl_fatal( "%s: Error reading T in FM index.\n", __FILE__);
  if( fread(fm->BWT, sizeof(uint8_t), compressed_bytes, meta->fp)  != compressed_bytes)
    esl_fatal( "%s: Error reading BWT in FM index.\n", __FILE__);
  if(getAll && fread(fm->SA, sizeof(uint32_t), (size_t)num_SA_samples, meta->fp) != (size_t)num_SA_samples)
    esl_fatal( "%s: Error reading SA in FM index.\n", __FILE__);
  if(fread(fm->occCnts_b, sizeof(uint16_t)*(meta->alph_size), (size_t)num_freq_cnts_b, meta->fp) != (size_t)num_freq_cnts_b)
    esl_fatal( "%s: Error reading occCnts_b in FM index.\n", __FILE__);
  if(fread(fm->occCnts_sb, sizeof(uint32_t)*(meta->alph_size), (size_t)num_freq_cnts_sb, meta->fp) != (size_t)num_freq_cnts_sb)
    esl_fatal( "%s: Error reading occCnts_sb in FM index.\n", __FILE__);
  C          = fm->C;
  occCnts_sb = fm->occCnts_sb;
  C[0] = 0;
  for (i=0; i<meta->alph_size; i++) {
    prevC = abs((int)(C[i]));
    cnt = FM_OCC_CNT( sb, num_freq_cnts_sb-1, i);
    if (cnt==0) {
      C[i+1] = prevC;
      C[i] *= -1; 
    } else {
      C[i+1] = prevC + cnt;
    }
  }
  C[meta->alph_size] *= -1;
  C[0] = 1;
  return eslOK;
ERROR:
  fm_FM_destroy(fm, getAll);
  esl_fatal("Error allocating memory in %s\n", "readFM");
  return eslFAIL;
}
int fm_FM_read ( FM_DATA * fm , FM_METADATA * meta , int getAll ) { int64_t * C = NULL ; int i ; uint32_t * occCnts_sb = NULL ; int32_t compressed_bytes ; int num_freq_cnts_b ; int num_freq_cnts_sb ; int num_SA_samples ; int64_t prevC ; int cnt ; int chars_per_byte = 8 / meta -> charBits ; int status ; if ( fread ( & ( fm -> N ) , sizeof ( uint64_t ) , 1 , meta -> fp ) != 1 ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( & ( fm -> term_loc ) , sizeof ( uint32_t ) , 1 , meta -> fp ) != 1 ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( & ( fm -> seq_offset ) , sizeof ( uint32_t ) , 1 , meta -> fp ) != 1 ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( & ( fm -> ambig_offset ) , sizeof ( uint32_t ) , 1 , meta -> fp ) != 1 ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( & ( fm -> overlap ) , sizeof ( uint32_t ) , 1 , meta -> fp ) != 1 ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( & ( fm -> seq_cnt ) , sizeof ( uint32_t ) , 1 , meta -> fp ) != 1 ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( & ( fm -> ambig_cnt ) , sizeof ( uint32_t ) , 1 , meta -> fp ) != 1 ) esl_fatal ( " \n " , __FILE__ ) ; compressed_bytes = ( ( chars_per_byte - 1 + fm -> N ) / chars_per_byte ) ; num_freq_cnts_b = 1 + ceil ( ( double ) fm -> N / meta -> freq_cnt_b ) ; num_freq_cnts_sb = 1 + ceil ( ( double ) fm -> N / meta -> freq_cnt_sb ) ; num_SA_samples = floor ( ( double ) fm -> N / meta -> freq_SA ) ; if ( getAll ) ESL_ALLOC ( fm -> T , sizeof ( uint8_t ) * compressed_bytes ) ; ESL_ALLOC ( fm -> BWT_mem , sizeof ( uint8_t ) * ( compressed_bytes + 31 ) ) ; fm -> BWT = ( uint8_t * ) ( ( ( unsigned long int ) fm -> BWT_mem + 15 ) & ( ~ 0xf ) ) ; if ( getAll ) ESL_ALLOC ( fm -> SA , num_SA_samples * sizeof ( uint32_t ) ) ; ESL_ALLOC ( fm -> C , ( 1 + meta -> alph_size ) * sizeof ( int64_t ) ) ; ESL_ALLOC ( fm -> occCnts_b , num_freq_cnts_b * ( meta -> alph_size ) * sizeof ( uint16_t ) ) ; ESL_ALLOC ( fm -> occCnts_sb , num_freq_cnts_sb * ( meta -> alph_size ) * sizeof ( uint32_t ) ) ; if ( getAll && fread ( fm -> T , sizeof ( uint8_t ) , compressed_bytes , meta -> fp ) != compressed_bytes ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( fm -> BWT , sizeof ( uint8_t ) , compressed_bytes , meta -> fp ) != compressed_bytes ) esl_fatal ( " \n " , __FILE__ ) ; if ( getAll && fread ( fm -> SA , sizeof ( uint32_t ) , ( size_t ) num_SA_samples , meta -> fp ) != ( size_t ) num_SA_samples ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( fm -> occCnts_b , sizeof ( uint16_t ) * ( meta -> alph_size ) , ( size_t ) num_freq_cnts_b , meta -> fp ) != ( size_t ) num_freq_cnts_b ) esl_fatal ( " \n " , __FILE__ ) ; if ( fread ( fm -> occCnts_sb , sizeof ( uint32_t ) * ( meta -> alph_size ) , ( size_t ) num_freq_cnts_sb , meta -> fp ) != ( size_t ) num_freq_cnts_sb ) esl_fatal ( " \n " , __FILE__ ) ; C = fm -> C ; occCnts_sb = fm -> occCnts_sb ; C [ 0 ] = 0 ; for ( i = 0 ; i < meta -> alph_size ; i ++ ) { prevC = abs ( ( int ) ( C [ i ] ) ) ; cnt = FM_OCC_CNT ( sb , num_freq_cnts_sb - 1 , i ) ; if ( cnt == 0 ) { C [ i + 1 ] = prevC ; C [ i ] *= -1 ; } else { C [ i + 1 ] = prevC + cnt ; } } C [ meta -> alph_size ] *= -1 ; C [ 0 ] = 1 ; return eslOK ; ERROR : fm_FM_destroy ( fm , getAll ) ; esl_fatal ( " \n " , " " ) ; return eslFAIL ; }
Function:  fm_FM_read()
 Synopsis:  Read the FM index off disk
 Purpose:   Read the FM-index as written by fmbuild.
Function : fm_FM_read () Synopsis : Read the FM index off disk Purpose : Read the FM - index as written by fmbuild .
//shortcut variables // allocate space, then read the data // +31 for manual 16-byte alignment  ( typically only need +15, but this allows offset in memory, plus offset in case of <16 bytes of characters at the end) // align vector memory on 16-byte boundaries // every freq_cnt positions, store an array of ints // every freq_cnt positions, store an array of ints //shortcut variables /*compute the first position of each letter in the alphabet in a sorted list
  * (with an extra value to simplify lookup of the last position for the last letter).
  * Negative values indicate that there are zero of that character in T, can be
  * used to establish the end of the prior range*/ // none of this character // use negative to indicate that there's no character of this type, the number gives the end point of the previous