/
main.go
133 lines (113 loc) · 2.97 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"runtime"
"runtime/pprof"
"github.com/TuftsBCB/io/fasta"
"github.com/ndaniels/mica"
)
const interval = 1000
var (
// Flags that affect the higher level operation of compression.
// Flags that control algorithmic parameters are stored in `dbConf`.
flagGoMaxProcs = runtime.NumCPU()
flagQuiet = false
flagCpuProfile = ""
flagMemProfile = ""
)
func init() {
log.SetFlags(0)
flag.IntVar(&flagGoMaxProcs, "p", flagGoMaxProcs,
"The maximum number of CPUs that can be executing simultaneously.")
flag.BoolVar(&flagQuiet, "quiet", flagQuiet,
"When set, the only outputs will be errors echoed to stderr.")
flag.StringVar(&flagCpuProfile, "cpuprofile", flagCpuProfile,
"When set, a CPU profile will be written to the file specified.")
flag.StringVar(&flagMemProfile, "memprofile", flagMemProfile,
"When set, a memory profile will be written to the file specified.")
flag.Usage = usage
flag.Parse()
runtime.GOMAXPROCS(flagGoMaxProcs)
}
func main() {
if flag.NArg() < 2 {
flag.Usage()
}
// If the quiet flag isn't set, enable verbose output.
if !flagQuiet {
mica.Verbose = true
}
// Open the fasta file specified for writing.
outFasta, err := os.Create(flag.Arg(1))
if err != nil {
fatalf("Could not write to '%s': %s\n", flag.Arg(1), err)
}
fastaWriter := fasta.NewWriter(outFasta)
fastaWriter.Asterisk = true
// Create a new database for writing. If we're appending, we load
// the coarse database into memory, and setup the database for writing.
db, err := mica.NewReadDB(flag.Arg(0))
if err != nil {
fatalf("Could not open '%s' database: %s\n", flag.Arg(0), err)
}
mica.Vprintln("")
// Start the CPU profile after all of the data has been read.
if len(flagCpuProfile) > 0 {
f, err := os.Create(flagCpuProfile)
if err != nil {
fatalf("%s\n", err)
}
pprof.StartCPUProfile(f)
}
numSeqs := db.ComDB.NumSequences()
for orgSeqId := 0; orgSeqId < numSeqs; orgSeqId++ {
oseq, err := db.ComDB.ReadSeq(db.CoarseDB, orgSeqId)
if err != nil {
fatalf("Error reading seq id '%d': %s\n", orgSeqId, err)
}
if err := fastaWriter.Write(oseq.FastaSeq()); err != nil {
mica.Vprintf("Error writing seq '%s': %s\n", oseq.Name, err)
}
}
cleanup(db)
if err = fastaWriter.Flush(); err != nil {
fatalf("%s\n", err)
}
if err = outFasta.Close(); err != nil {
fatalf("%s\n", err)
}
}
func cleanup(db *mica.DB) {
if len(flagCpuProfile) > 0 {
pprof.StopCPUProfile()
}
if len(flagMemProfile) > 0 {
writeMemProfile(fmt.Sprintf("%s.last", flagMemProfile))
}
db.ReadClose()
}
func fatalf(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
os.Exit(1)
}
func writeMemProfile(name string) {
f, err := os.Create(name)
if err != nil {
fatalf("%s\n", err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
func usage() {
fmt.Fprintf(os.Stderr,
"\nUsage: %s [flags] "+
"database-directory "+
"out-fasta-file\n",
path.Base(os.Args[0]))
mica.PrintFlagDefaults()
os.Exit(1)
}