deps/kb_meme/meme-5.0.1/src/seq-reader-from-fasta.c
/****************************************************************************
 * Read raw sequence until a new sequence is encountered or too many letters
 * are read.
 *
 * Return: Was the sequence read completely?
 ****************************************************************************/
Read raw sequence until a new sequence is encountered or too many letters
are read.


Read raw sequence until a new sequence is encountered or too many letters are read .
bool read_raw_sequence_from_reader(
   DATA_BLOCK_READER_T *reader, 
   unsigned int max_chars, 
   char* raw_sequence 
) {
  SEQ_READER_FROM_FASTA_T *fasta_reader 
    = (SEQ_READER_FROM_FASTA_T *) get_data_block_reader_data(reader);
  char buffer[max_chars];
  long start_file_pos = ftell(fasta_reader->fasta_file);
  size_t seq_index = 0;
  size_t total_read = 0;
  while (seq_index < max_chars) {
    size_t num_char_read = fread(
      buffer,
      sizeof(char), 
      max_chars - seq_index,
      fasta_reader->fasta_file
    );
    fasta_reader->current_position += num_char_read;
    total_read += num_char_read;
    if (feof(fasta_reader->fasta_file)) {
       fasta_reader->at_end_of_file = true;
    }
    else if (num_char_read < (max_chars - seq_index)) {
      die(
        "Error while reading sequence from file:%s.\nError message: %s\n", 
        fasta_reader->filename,
        strerror(ferror(fasta_reader->fasta_file))
      );
    }
    size_t i;
    for(i = 0; i < num_char_read; ++i) {
      char c = buffer[i];
      assert(c != 0);
      if (isspace(c)) {
        fasta_reader->at_start_of_line = (c == '\n');
      }
      else if (c == '>' && fasta_reader->at_start_of_line == true) {
        fseek(fasta_reader->fasta_file, start_file_pos + i - 1, SEEK_SET);
        fasta_reader->current_position = start_file_pos + i - 1;
        fasta_reader->at_end_of_seq = true;
        fasta_reader->at_start_of_line = false;
        fasta_reader->at_end_of_file = false;
        break;
      }
      else {
        fasta_reader->at_start_of_line = false;
        if (alph_is_known(fasta_reader->alphabet, c)) {
          raw_sequence[seq_index] = c;
        }
        else {
          raw_sequence[seq_index] = alph_wildcard(fasta_reader->alphabet);
          fprintf(
            stderr, 
            "Warning: %c is not a valid character in %s alphabet.\n"
            "         Converting %c to %c.\n",
            c,
            alph_name(fasta_reader->alphabet),
            c,
            raw_sequence[i]
          );
        }
        ++seq_index;
      }
    }
    if (fasta_reader->at_end_of_seq | fasta_reader->at_end_of_file) {
      break;
    }
  }
  raw_sequence[seq_index] = '\0';
  return(fasta_reader->at_end_of_seq | fasta_reader->at_end_of_file);
}
bool read_raw_sequence_from_reader ( DATA_BLOCK_READER_T * reader , unsigned int max_chars , char * raw_sequence ) { SEQ_READER_FROM_FASTA_T * fasta_reader = ( SEQ_READER_FROM_FASTA_T * ) get_data_block_reader_data ( reader ) ; char buffer [ max_chars ] ; long start_file_pos = ftell ( fasta_reader -> fasta_file ) ; size_t seq_index = 0 ; size_t total_read = 0 ; while ( seq_index < max_chars ) { size_t num_char_read = fread ( buffer , sizeof ( char ) , max_chars - seq_index , fasta_reader -> fasta_file ) ; fasta_reader -> current_position += num_char_read ; total_read += num_char_read ; if ( feof ( fasta_reader -> fasta_file ) ) { fasta_reader -> at_end_of_file = true ; } else if ( num_char_read < ( max_chars - seq_index ) ) { die ( " \n \n " , fasta_reader -> filename , strerror ( ferror ( fasta_reader -> fasta_file ) ) ) ; } size_t i ; for ( i = 0 ; i < num_char_read ; ++ i ) { char c = buffer [ i ] ; assert ( c != 0 ) ; if ( isspace ( c ) ) { fasta_reader -> at_start_of_line = ( c == ' \n ' ) ; } else if ( c == ' ' && fasta_reader -> at_start_of_line == true ) { fseek ( fasta_reader -> fasta_file , start_file_pos + i - 1 , SEEK_SET ) ; fasta_reader -> current_position = start_file_pos + i - 1 ; fasta_reader -> at_end_of_seq = true ; fasta_reader -> at_start_of_line = false ; fasta_reader -> at_end_of_file = false ; break ; } else { fasta_reader -> at_start_of_line = false ; if ( alph_is_known ( fasta_reader -> alphabet , c ) ) { raw_sequence [ seq_index ] = c ; } else { raw_sequence [ seq_index ] = alph_wildcard ( fasta_reader -> alphabet ) ; fprintf ( stderr , " \n " " \n " , c , alph_name ( fasta_reader -> alphabet ) , c , raw_sequence [ i ] ) ; } ++ seq_index ; } } if ( fasta_reader -> at_end_of_seq | fasta_reader -> at_end_of_file ) { break ; } } raw_sequence [ seq_index ] = ' \0 ' ; return ( fasta_reader -> at_end_of_seq | fasta_reader -> at_end_of_file ) ; }
Read raw sequence until a new sequence is encountered or too many letters
 are read.
Read raw sequence until a new sequence is encountered or too many letters are read .
// Sequence source // Maximum chars in raw_sequence. // Pre-allocated sequence buffer. // Read sequence into temp. buffer from the sequence file. // Skip over white space // We found the start of a new sequence while trying // to fill the buffer. Leave the buffer incomplete. // and wind back the file // Check that character is legal in alphabet.  // If not, replace with wild card character.