-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
91 lines (75 loc) · 2.26 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
package main
import (
"fmt"
"os"
"runtime"
"github.com/feliixx/gotranseq/transeq"
"github.com/jessevdk/go-flags"
)
const toolName = "gotranseq"
// Version of gotranseq. Should be linked via ld_flags when compiling
// use this to set version to last known tag:
//
// go build -ldflags "-X main.Version=$(git describe --tags $(git rev-list --tags --max-count=1))"
var Version string
// GlobalOptions struct to store command line args
type GlobalOptions struct {
Required `group:"required"`
transeq.Options `group:"optional"`
General `group:"general"`
}
// Required struct to store required command line args
type Required struct {
Sequence string `short:"s" long:"sequence" value-name:"<filename>" description:"Nucleotide sequence(s) filename"`
Outseq string `short:"o" long:"outseq" value-name:"<filename>" description:"Protein sequence filename"`
}
// General struct to store required command line args
type General struct {
Help bool `short:"h" long:"help" description:"Show this help message"`
Version bool `short:"v" long:"version" description:"Print the tool version and exit"`
}
func run(options GlobalOptions) error {
if options.Sequence == "" {
return fmt.Errorf("missing required parameter -s | -sequence, try %s --help for details", toolName)
}
if options.Outseq == "" {
return fmt.Errorf("missing required parameter -o | -outseq, try %s --help for details", toolName)
}
if options.NumWorker == 0 {
options.NumWorker = runtime.NumCPU()
}
in, err := os.Open(options.Sequence)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(options.Outseq)
if err != nil {
return err
}
defer out.Close()
return transeq.Translate(in, out, options.Options)
}
func main() {
var options GlobalOptions
p := flags.NewParser(&options, flags.Default&^flags.HelpFlag)
p.Usage = "--sequence file.fna --outseq out.faa"
_, err := p.Parse()
if err != nil {
fmt.Printf("wrong arguments: %v, try %s --help for more informations\n", err, toolName)
os.Exit(1)
}
if options.Help {
fmt.Printf("%s %s\n\n", toolName, Version)
p.WriteHelp(os.Stdout)
os.Exit(0)
}
if options.Version {
fmt.Printf("%s %s\n", toolName, Version)
os.Exit(0)
}
err = run(options)
if err != nil {
fmt.Printf("fail to translate file:\n%v", err)
}
}