-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnames.go
61 lines (51 loc) · 1.46 KB
/
names.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
58
59
60
61
package main
import (
"fmt"
"io"
"github.com/bjeight/fastats/fasta"
)
// names() is fastats names in the cli. It writes the IDs / descriptions of the records
func names(w io.Writer, filepaths []string, pattern string, file bool, counts bool, description bool, lenFormat string) error {
// Write the correct header for the output
var err error
if description {
_, err = w.Write([]byte("file\tdescription\n"))
if err != nil {
return err
}
} else {
_, err = w.Write([]byte("file\tid\n"))
if err != nil {
return err
}
}
// pass namesRecords + the cli arguments to collectCommandLine() for processing the fasta file(s)
err = collectCommandLine(w, namesRecords, filepaths, pattern, file, counts, description, lenFormat)
if err != nil {
return err
}
return nil
}
// namesRecords does the work of fastats names for one fasta file at a time.
func namesRecords(r *fasta.Reader, args arguments, w io.Writer) error {
// get the file name in case we need to print it to stdout
filename := filenameFromFullPath(args.filepath)
// iterate over every record in the fasta file
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return (err)
}
// if the statistic is to be calculated per file, add this record's length
// to the total, else just write it.
s := fmt.Sprintf("%s\t%s\n", filename, returnRecordName(record, args.description))
_, err = w.Write([]byte(s))
if err != nil {
return err
}
}
return nil
}