-
Notifications
You must be signed in to change notification settings - Fork 0
/
num.go
57 lines (46 loc) · 1.31 KB
/
num.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"io"
"github.com/bjeight/fastats/fasta"
)
// num() is fastats num in the cli. It writes the header, then passes numRecords() + the
// cli arguments + the writer to collectCommandLine, which processes the fasta file(s)
// from the command line or stdin, depending on what is provided by the user.
func num(w io.Writer, filepaths []string, pattern string, file bool, counts bool, description bool, lenFormat string) error {
_, err := w.Write([]byte("file\tn_records\n"))
if err != nil {
return err
}
err = collectCommandLine(w, numRecords, filepaths, pattern, file, counts, description, lenFormat)
if err != nil {
return err
}
return nil
}
// numRecords does the work of fastats num for one fasta file at a time.
func numRecords(r *fasta.Reader, args arguments, w io.Writer) error {
// get the file name for when we need to print it
filename := filenameFromFullPath(args.filepath)
// initiate a count for the number of records
c_total := 0
// iterate over every record in the fasta file
for {
_, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
// for every record, += the count
c_total += 1
}
// print the count
s := fmt.Sprintf("%s\t%d\n", filename, c_total)
_, err := w.Write([]byte(s))
if err != nil {
return err
}
return nil
}