idoneal - nucleotides.go
		
     1  package meta
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  )
     7  
     8  // CountNucleotides returns the number of Nucleotides in the passed FASTQ
     9  // formatted Reader.
    10  //
    11  // NB: No validation is performed on the data being read.
    12  func CountNucleotides(r io.Reader) (int, error) {
    13  	lr := cleaner(r)
    14  	var buf [1024]byte
    15  	line := 0
    16  	count := 0
    17  	for {
    18  		n, err := lr.Read(buf[:])
    19  		for _, b := range buf[:n] {
    20  			if b == '\n' {
    21  				line = (line + 1) % 4 // what sequence line are we on?
    22  			} else if line == 1 { // second line
    23  				count++
    24  			}
    25  		}
    26  		if err == io.EOF {
    27  			break
    28  		} else if err != nil {
    29  			return 0, fmt.Errorf("error while counting nucleotides: %w", err)
    30  		}
    31  	}
    32  	return count, nil
    33  }
    34