Skip to content

Instantly share code, notes, and snippets.

@bsipos
Created March 29, 2020 12:13
Show Gist options
  • Save bsipos/e55625a8873d57255748f6edd71608f3 to your computer and use it in GitHub Desktop.
Save bsipos/e55625a8873d57255748f6edd71608f3 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 := "+\n" // Pattern to look for.
// Create new buffered reader:
reader := bufio.NewReaderSize(fh, buffSize)
// Variable for counting records:
var records int
for {
// Read next line:
line, err := reader.ReadString('\n')
// Are we at the end of the file?
if err == io.EOF {
break
}
// Anything else went wrong?
checkError(err)
// Is this a fastq separator line?
if line == pattern {
records++
}
}
// 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