Skip to content

Instantly share code, notes, and snippets.

@bsipos
Created March 29, 2020 12:49
Show Gist options
  • Save bsipos/f55b612ef661f82f8e639ad2ea01bc14 to your computer and use it in GitHub Desktop.
Save bsipos/f55b612ef661f82f8e639ad2ea01bc14 to your computer and use it in GitHub Desktop.
// CountFastxRecords counts the number of records in a fastx file.
func CountFastxRecords(fh *os.File, buffSize int, format string) int {
// Start the timer:
start := time.Now()
pattern := []byte("\n+\n") // Pattern to look for.
// Create new buffered reader:
reader := bufio.NewReaderSize(fh, buffSize)
// Byte buffer:
buffer := make([]byte, buffSize)
// Variable for counting records:
var records int
for {
// Read next line:
_, err := reader.Read(buffer)
// Are we at the end of the file?
if err == io.EOF {
break
}
// Anything else went wrong?
checkError(err)
// Count the number of fastq separator lines in the buffer:
records += bytes.Count(buffer, pattern) // FIXME: records will be missed!
}
// Print out the time needed for counting:
fmt.Fprintf(os.Stderr, "%d\n", time.Since(start).Microseconds())
// Return the number of records:
return records
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment