-
Notifications
You must be signed in to change notification settings - Fork 0
/
len.go
120 lines (106 loc) · 2.91 KB
/
len.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"io"
"strconv"
"github.com/bjeight/fastats/fasta"
)
// length() is fastats len in the cli. It writes the appropriate header (which depends on the cli
// arguments), then passes lengthRecords() + 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 length(w io.Writer, filepaths []string, pattern string, file bool, counts bool, description bool, lenFormat string) error {
// print the correct header to stdout, depending on whether the statistics are
// to be calculated per file or per record
if file {
_, err := w.Write([]byte("file\tlength"))
if err != nil {
return err
}
} else {
_, err := w.Write([]byte("record\tlength"))
if err != nil {
return err
}
}
switch lenFormat {
case "kb":
_, err := w.Write([]byte("_kb\n"))
if err != nil {
return err
}
case "mb":
_, err := w.Write([]byte("_mb\n"))
if err != nil {
return err
}
case "gb":
_, err := w.Write([]byte("_gb\n"))
if err != nil {
return err
}
default:
_, err := w.Write([]byte("\n"))
if err != nil {
return err
}
}
// pass lengthRecords + the cli arguments to template() for processesing the fasta file(s)
err := collectCommandLine(w, lengthRecords, filepaths, pattern, file, counts, description, lenFormat)
if err != nil {
return err
}
return nil
}
// lengthRecords does the work of fastats len for one fasta file at a time.
func lengthRecords(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)
// initiate a count for the length of each record
l_total := 0
// 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.
if args.file {
l_total += len(record.Seq)
} else {
s := fmt.Sprintf("%s\t%s\n", return_record_name(record, args.description), return_record_length(len(record.Seq), args.lenFormat))
_, err := w.Write([]byte(s))
if err != nil {
return err
}
}
}
// if the statistic is to be calculated per file, we print the total after all
// the records have been processed
if args.file {
s := fmt.Sprintf("%s\t%s\n", filename, return_record_length(l_total, args.lenFormat))
_, err := w.Write([]byte(s))
if err != nil {
return err
}
}
return nil
}
func return_record_length(l int, unit string) string {
var s string
switch unit {
case "kb":
s = strconv.FormatFloat(float64(l)/float64(1000), 'f', 3, 64)
case "mb":
s = strconv.FormatFloat(float64(l)/float64(1000000), 'f', 6, 64)
case "gb":
s = strconv.FormatFloat(float64(l)/float64(1000000000), 'f', 9, 64)
default:
s = strconv.Itoa(l)
}
return s
}