-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
230 lines (197 loc) · 6.31 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"code.cloudfoundry.org/bytefmt"
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
fastq "squish/fastq"
_io "squish/io"
_sort "squish/sort"
"time"
)
// overwrite this at build time ;
// -ldflags="-X 'main.Version=someversion'"
var Version = "foo-version"
const fastqHeaderChar byte = '@'
const delim byte = '\n'
const defaultSortMethod string = "alpha"
const defaultSortDescrption string = "Alphabetical sort on sequence"
// const defaultSortFunc func() = _sort.SortReadsSequence
const defaultCpuProfileFilename string = "cpu.prof"
const defaultMemProfileFilename string = "mem.prof"
const defaultOrderFilename string = "order.txt"
func Profiling(cpuFilename string, memFilename string) (*os.File, *os.File) {
// start CPU and Memory profiling
//
// NOTES:
// https://pkg.go.dev/runtime/pprof
// https://github.com/google/pprof/blob/main/doc/README.md
// $ go tool pprof cpu.prof
// $ go tool pprof mem.prof
// (pprof) top
//
// caller must also run these;
// defer cpuFile.Close()
// defer memFile.Close()
// defer pprof.StopCPUProfile()
// defer pprof.WriteHeapProfile(memFile)
//
// see also; https://pkg.go.dev/net/http/pprof
// Start CPU profiling
cpuFile, err := os.Create(cpuFilename)
if err != nil {
log.Fatal(err)
}
log.Printf("CPU profile will be saved to %v\n", cpuFilename)
pprof.StartCPUProfile(cpuFile)
// Start memory profiling file
memFile, err := os.Create(memFilename)
if err != nil {
log.Fatal(err)
}
log.Printf("Memory profile will be saved to %v\n", memFilename)
return cpuFile, memFile
}
func GetFileSize(filepath string) (int64, error) {
fi, err := os.Stat(filepath)
if err != nil {
return 0, err
}
return fi.Size(), nil
}
func LogFileSize(filepath string, filetype string) int64 {
inputFileSize, err := GetFileSize(filepath)
if err != nil {
log.Printf("WARNING: could not get size for file %v\n", filepath)
}
inputFileSizeBytes := bytefmt.ByteSize(uint64(inputFileSize))
log.Printf("%v file %v of size %v Bytes\n", filetype, filepath, inputFileSizeBytes)
return inputFileSize
}
func PrintVersionAndQuit() {
fmt.Println(Version)
os.Exit(0)
}
func MinCliPosArgs(args []string, n int) {
if len(args) < n {
log.Fatalf("Not enough cli args provided, %v args required, or use -h for help\n", n)
}
}
func RunSort(config Config) {
// run the chosen sorting method on the fastq file
// input
reader := _io.GetReader(config.InputFilepath)
defer reader.Close()
// output
writer := _io.GetWriter(config.OutputFilepath)
defer writer.Close()
// hold all the reads from the file in here
reads := []fastq.FastqRead{}
// load all reads from file
totalByteSize := fastq.LoadReads(&reads, reader, &config.RecordDelim)
log.Printf("%v reads loaded (%v)\n", len(reads), bytefmt.ByteSize(uint64(totalByteSize)))
// sort the fastq reads
log.Printf("starting read sort")
config.SortMethod.Func(&reads)
log.Printf("%v reads after sorting\n", len(reads))
// write the fastq reads
log.Printf("Writing to output file %v\n", config.OutputFilepath)
fastq.WriteReads(&reads, writer)
// save the order of the sorted reads to file
fastq.SaveOrder(&reads, config.OrderFilename)
}
func GetSortingMethods() (map[string]SortMethod, string) {
// parse the available sorting methods
sortMethodMap := map[string]SortMethod{
defaultSortMethod: SortMethod{defaultSortMethod, defaultSortDescrption, _sort.SortReadsSequence}, // alpha
"gc": SortMethod{"gc", "GC Content Sort", _sort.SortReadsGC},
"qual": SortMethod{"qual", "Quality score sort", _sort.SortReadsQual},
"alpha-heap": SortMethod{"alpha-heap", "Sequence alpha heap sort", _sort.HeapSortSequence},
}
// minimal map for help text printing
sortMethodsDescr := map[string]string{}
for key, value := range sortMethodMap {
sortMethodsDescr[key] = value.Description
}
// help text
sortMethodOptionStr := fmt.Sprintf("Options: %v", sortMethodsDescr)
return sortMethodMap, sortMethodOptionStr
}
type SortMethod struct {
CLIArg string
Description string
Func func(*[]fastq.FastqRead)
}
type Config struct {
SortMethod SortMethod
InputFilepath string
InputFileSize int64
OutputFilepath string
RecordDelim byte
RecordHeaderChar byte
TimeStart time.Time
OrderFilename string
}
func main() {
timeStart := time.Now()
sortMethodMap, sortMethodOptionStr := GetSortingMethods()
// get command line args
printVersion := flag.Bool("v", false, "print version information")
sortMethodArg := flag.String("m", defaultSortMethod, "Fastq read sorting method. "+sortMethodOptionStr)
cpuProfileFilename := flag.String("cpuProf", defaultCpuProfileFilename, "CPU profile filename")
memProfileFilename := flag.String("memProf", defaultMemProfileFilename, "Memory profile filename")
orderFilename := flag.String("orderFile", defaultOrderFilename, "File to record the order of sorted fastq reads")
flag.Parse()
cliArgs := flag.Args() // all positional args passed
if *printVersion {
PrintVersionAndQuit()
}
MinCliPosArgs(cliArgs, 2)
// get positional cli args
inputFilepath := cliArgs[0]
outputFilepath := cliArgs[1]
inputFileSize := LogFileSize(inputFilepath, "Input")
// initialize config
_, ok := sortMethodMap[*sortMethodArg]
if !ok {
log.Fatalf("ERROR: Unknown sort method: %v\n", *sortMethodArg)
}
config := Config{
SortMethod: sortMethodMap[*sortMethodArg],
InputFilepath: inputFilepath,
InputFileSize: inputFileSize,
OutputFilepath: outputFilepath,
RecordDelim: delim,
RecordHeaderChar: fastqHeaderChar,
TimeStart: timeStart,
OrderFilename: *orderFilename,
}
//
//
//
// start profiler
cpuFile, memFile := Profiling(*cpuProfileFilename, *memProfileFilename)
defer cpuFile.Close()
defer memFile.Close()
defer pprof.StopCPUProfile()
defer pprof.WriteHeapProfile(memFile)
// insert sort methods here
log.Printf("Using sort method: %v\n", config.SortMethod.CLIArg)
RunSort(config)
//
//
//
// print some stuff to the console log
timeStop := time.Now()
timeDuration := timeStop.Sub(config.TimeStart)
outputFileSize := LogFileSize(config.OutputFilepath, "Output")
sizeDifference := config.InputFileSize - outputFileSize
sizeDifferenceBytes := bytefmt.ByteSize(uint64(sizeDifference))
log.Printf("Size reduced by %v Bytes (%.4f) in %v\n",
sizeDifferenceBytes,
float64(sizeDifference)/float64(inputFileSize),
timeDuration,
)
}