deps/kb_meme/meme-5.0.1/src/seq-reader-from-fasta.c
/******************************************************************************
 * This function reads the entire sequence header at the start of a new sequence.
 * The current position is assumed to be start of a new sequence.
 * Read from the current position to the end of the current line.
 *
 * Returns true if it was able to read the sequence text, false if 
 * EOF reached before the terminal newline was found. Dies if other errors
 * are encountered.
 *****************************************************************************/
This function reads the entire sequence header at the start of a new sequence.
The current position is assumed to be start of a new sequence.
Read from the current position to the end of the current line.

Returns true if it was able to read the sequence text, false if
EOF reached before the terminal newline was found. Dies if other errors
are encountered.
This function reads the entire sequence header at the start of a new sequence . The current position is assumed to be start of a new sequence . Read from the current position to the end of the current line . Returns true if it was able to read the sequence text false if EOF reached before the terminal newline was found . Dies if other errors are encountered .
bool read_seq_header_from_seq_reader_from_fasta(
  SEQ_READER_FROM_FASTA_T *fasta_reader
) {
  int result = false;
  const size_t initial_buffer_len = 100;
  if (fasta_reader->seq_header == NULL) {
     fasta_reader->seq_header = mm_malloc(sizeof(char) * initial_buffer_len);
     fasta_reader->header_buffer_len = initial_buffer_len;
  }
  int c = 0;
  int seq_index = 0;
  while((c = fgetc(fasta_reader->fasta_file)) != EOF) {
    if (seq_index >= fasta_reader->header_buffer_len) {
      fasta_reader->seq_header
        = mm_realloc(
            fasta_reader->seq_header, 
            2 * fasta_reader->header_buffer_len
          );
      fasta_reader->header_buffer_len = 2 * fasta_reader->header_buffer_len;
    }
    if (c == '\n') {
      fasta_reader->seq_header[seq_index] = '\0';
      fasta_reader->seq_header_len = seq_index + 1;
      fasta_reader->at_start_of_line = true;
      result = true;
      break;
    }
    else {
      fasta_reader->seq_header[seq_index] = c;
      ++seq_index;
    }
  }
  if (c == EOF) {
    if (ferror(fasta_reader->fasta_file)) {
      die(
        "Error reading file:%s.\nError message: %s\n", 
        fasta_reader->filename,
        strerror(ferror(fasta_reader->fasta_file))
      );
    }
    else if (feof(fasta_reader->fasta_file)) {
        fasta_reader->seq_header[0] = '\0';
        fasta_reader->seq_header_len = 0;
    }
  }
  return result;
}
bool read_seq_header_from_seq_reader_from_fasta ( SEQ_READER_FROM_FASTA_T * fasta_reader ) { int result = false ; const size_t initial_buffer_len = 100 ; if ( fasta_reader -> seq_header == NULL ) { fasta_reader -> seq_header = mm_malloc ( sizeof ( char ) * initial_buffer_len ) ; fasta_reader -> header_buffer_len = initial_buffer_len ; } int c = 0 ; int seq_index = 0 ; while ( ( c = fgetc ( fasta_reader -> fasta_file ) ) != EOF ) { if ( seq_index >= fasta_reader -> header_buffer_len ) { fasta_reader -> seq_header = mm_realloc ( fasta_reader -> seq_header , 2 * fasta_reader -> header_buffer_len ) ; fasta_reader -> header_buffer_len = 2 * fasta_reader -> header_buffer_len ; } if ( c == ' \n ' ) { fasta_reader -> seq_header [ seq_index ] = ' \0 ' ; fasta_reader -> seq_header_len = seq_index + 1 ; fasta_reader -> at_start_of_line = true ; result = true ; break ; } else { fasta_reader -> seq_header [ seq_index ] = c ; ++ seq_index ; } } if ( c == EOF ) { if ( ferror ( fasta_reader -> fasta_file ) ) { die ( " \n \n " , fasta_reader -> filename , strerror ( ferror ( fasta_reader -> fasta_file ) ) ) ; } else if ( feof ( fasta_reader -> fasta_file ) ) { fasta_reader -> seq_header [ 0 ] = ' \0 ' ; fasta_reader -> seq_header_len = 0 ; } } return result ; }
This function reads the entire sequence header at the start of a new sequence.
This function reads the entire sequence header at the start of a new sequence .
// Initial allocation of sequence buffer // Look for EOL // Need to grow buffer // Found EOL // Keep looking for EOL // At this point c is EOL or EOF // EOF could actually indicate an error. // Reached EOF before reaching EOL for the sequence.