idoneal - sequences.go
1 // The meta package provides some simple convenience functions for dealing with
2 // FASTQ files.
3 package meta // import "vimagination.zapto.org/idoneal/pkg/meta"
4
5 import (
6 "fmt"
7 "io"
8 )
9
10 // CountSequences returns the number of Sequences in the passed FASTQ formatted
11 // Reader.
12 //
13 // NB: This function acts naively, simply counting the non-blank lines in the
14 // Reader, and diving by 4.
15 func CountSequences(r io.Reader) (int, error) {
16 lr := cleaner(r)
17 var buf [1024]byte
18 lineCount := 1
19 for {
20 n, err := lr.Read(buf[:])
21 for _, b := range buf[:n] {
22 if b == '\n' {
23 lineCount++
24 }
25 }
26 if err == io.EOF {
27 break
28 } else if err != nil {
29 return 0, fmt.Errorf("error while counting sequences: %w", err)
30 }
31 }
32 return lineCount >> 2, nil
33 }
34