Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,27 @@ func (args assembly) writeHeader(w io.Writer) error {

func (args assembly) writeBody(w io.Writer) error {
for _, input := range args.inputs {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = assemblyRecords(input, reader, args, w)
err := writeBodyLineAssembly(w, input, args)
if err != nil {
return err
}
}
return nil
}

func writeBodyLineAssembly(w io.Writer, input string, args assembly) error {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = assemblyRecords(input, reader, args, w)
if err != nil {
return err
}
return nil
}

func assemblyRecords(inputPath string, r *Reader, args assembly, w io.Writer) error {
contigLengths := make([]int64, 0)
var totalLength int64 = 0
Expand Down
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (
Use: "fastats {command}",
Short: "Very simple statistics from fasta files",
Long: ``,
Version: "0.13.0",
Version: "0.13.1",
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
Run: func(cmd *cobra.Command, args []string) {
if licences {
Expand Down
22 changes: 15 additions & 7 deletions content.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,27 @@ func (args content) writeHeader(w io.Writer) error {

func (args content) writeBody(w io.Writer) error {
for _, input := range args.inputs {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = contentRecords(input, reader, args, w)
err := writeBodyLineContent(w, input, args)
if err != nil {
return err
}
}
return nil
}

func writeBodyLineContent(w io.Writer, input string, args content) error {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = contentRecords(input, reader, args, w)
if err != nil {
return err
}
return nil
}

func contentRecords(inputPath string, r *Reader, args content, w io.Writer) error {

// initiate a count for the total length of the file
Expand All @@ -92,7 +100,7 @@ func contentRecords(inputPath string, r *Reader, args content, w io.Writer) erro

// iterate over every record in the fasta file
for {
record, err := r.Read()
record, err := r.ReadCalcBaseCounts()
if err == io.EOF {
break
}
Expand Down
19 changes: 15 additions & 4 deletions fasta.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@ func NewZReader(f io.Reader) *Reader {
return &Reader{bufio.NewReader(rz)}
}

// Read reads one fasta record's info from the underlying reader. The final record is returned with error = nil,
// and the next call to Read() returns an empty Record struct and error = io.EOF.
func (r *Reader) Read() (Record, error) {
return r.read(false)
}

func (r *Reader) ReadCalcBaseCounts() (Record, error) {
return r.read(true)
}

// read reads one fasta record's info from the underlying reader. The final record is returned with error = nil,
// and the next call to read() returns an empty Record struct and error = io.EOF.
// Base content is optionally counted
func (r *Reader) read(calcBaseCounts bool) (Record, error) {

var (
line, peek []byte
Expand Down Expand Up @@ -120,8 +129,10 @@ func (r *Reader) Read() (Record, error) {
line = line[:len(line)-drop]
}

for _, b := range line {
bases[b]++
if calcBaseCounts {
for _, b := range line {
bases[b]++
}
}
seqLength += int64(len(line))
}
Expand Down
8 changes: 4 additions & 4 deletions fasta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ AT
BaseCounts: bc,
}

record, err := r.Read()
record, err := r.ReadCalcBaseCounts()
if err != nil {
t.Error(err)
}
Expand All @@ -53,7 +53,7 @@ AT
Len: 20,
BaseCounts: bc,
}
record, err = r.Read()
record, err = r.ReadCalcBaseCounts()
if err != nil {
t.Error(err)
}
Expand All @@ -72,7 +72,7 @@ AT
Len: 2,
BaseCounts: bc,
}
record, err = r.Read()
record, err = r.ReadCalcBaseCounts()
if err != nil {
t.Error(err)
}
Expand All @@ -82,7 +82,7 @@ AT
}

expected = Record{}
record, err = r.Read()
record, err = r.ReadCalcBaseCounts()
if !errors.Is(err, io.EOF) {
t.Error(err)
}
Expand Down
20 changes: 14 additions & 6 deletions len.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,27 @@ func (args length) writeHeader(w io.Writer) error {

func (args length) writeBody(w io.Writer) error {
for _, input := range args.inputs {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = lengthRecords(input, reader, args, w)
err := writeBodyLineLen(w, input, args)
if err != nil {
return err
}
}
return nil
}

func writeBodyLineLen(w io.Writer, input string, args length) error {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = lengthRecords(input, reader, args, w)
if err != nil {
return err
}
return nil
}

func lengthRecords(inputPath string, r *Reader, args length, w io.Writer) error {

// initiate a count for the length of each record
Expand Down
20 changes: 14 additions & 6 deletions names.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@ func (args names) writeHeader(w io.Writer) error {

func (args names) writeBody(w io.Writer) error {
for _, input := range args.inputs {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = namesRecords(input, reader, args, w)
err := writeBodyLineNames(w, input, args)
if err != nil {
return err
}
}
return nil
}

func writeBodyLineNames(w io.Writer, input string, args names) error {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = namesRecords(input, reader, args, w)
if err != nil {
return err
}
return nil
}

func namesRecords(inputPath string, r *Reader, args names, w io.Writer) error {

// iterate over every record in the fasta file
Expand Down
20 changes: 14 additions & 6 deletions num.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ func (args num) writeHeader(w io.Writer) error {

func (args num) writeBody(w io.Writer) error {
for _, input := range args.inputs {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = numRecords(input, reader, args, w)
err := writeBodyLineNum(w, input, args)
if err != nil {
return err
}
}
return nil
}

func writeBodyLineNum(w io.Writer, input string, args num) error {
reader, file, err := getReaderFile(input)
if err != nil {
return err
}
defer file.Close()
err = numRecords(input, reader, args, w)
if err != nil {
return err
}
return nil
}

func numRecords(inputPath string, r *Reader, args num, w io.Writer) error {
// initiate a count for the number of records
var c_total int64 = 0
Expand Down