-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
189 lines (164 loc) · 4.86 KB
/
cli.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
package main
import (
"errors"
"os"
"github.com/spf13/cobra"
)
var (
rootCmd = &cobra.Command{
Use: "fastats {command}",
Short: "Very simple statistics from fasta files",
Long: ``,
Version: "0.7.0",
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
}
)
func main() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
var f bool
var c bool
var d bool
var p string
var kb bool
var mb bool
var gb bool
var lenFormat string
func init() {
rootCmd.PersistentFlags().BoolVarP(&f, "file", "f", false, "calculate statistics per file (default is per record)")
rootCmd.PersistentFlags().BoolVarP(&c, "count", "c", false, "print counts (default is proportions)")
rootCmd.PersistentFlags().BoolVarP(&d, "description", "d", false, "write record descriptions (default is IDs)")
rootCmd.PersistentFlags().Lookup("file").NoOptDefVal = "true"
rootCmd.PersistentFlags().Lookup("count").NoOptDefVal = "true"
rootCmd.PersistentFlags().Lookup("description").NoOptDefVal = "true"
rootCmd.AddCommand(atCmd)
rootCmd.AddCommand(gcCmd)
rootCmd.AddCommand(atgcCmd)
rootCmd.AddCommand(nCmd)
rootCmd.AddCommand(gapCmd)
rootCmd.AddCommand(lenCmd)
rootCmd.AddCommand(softCmd)
rootCmd.AddCommand(patternCmd)
rootCmd.AddCommand(numCmd)
rootCmd.AddCommand(nameCmd)
patternCmd.Flags().StringVarP(&p, "pattern", "p", "", "arbitrary pattern to parse")
lenCmd.Flags().BoolVarP(&kb, "kb", "", false, "print length in kilobases")
lenCmd.Flags().BoolVarP(&mb, "mb", "", false, "print length in megabases")
lenCmd.Flags().BoolVarP(&gb, "gb", "", false, "print length in gigabases")
lenCmd.Flags().Lookup("kb").NoOptDefVal = "true"
lenCmd.Flags().Lookup("mb").NoOptDefVal = "true"
lenCmd.Flags().Lookup("gb").NoOptDefVal = "true"
}
var atCmd = &cobra.Command{
Use: "at <infile[s]>",
Short: "AT content",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = pattern(os.Stdout, args, "ATat", f, c, d, lenFormat)
return err
},
}
var gcCmd = &cobra.Command{
Use: "gc <infile[s]>",
Short: "GC content",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = pattern(os.Stdout, args, "GCgc", f, c, d, lenFormat)
return err
},
}
var atgcCmd = &cobra.Command{
Use: "atgc <infile[s]>",
Short: "ATGC content",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = pattern(os.Stdout, args, "ATGCatgc", f, c, d, lenFormat)
return err
},
}
var nCmd = &cobra.Command{
Use: "n <infile[s]>",
Short: "N content",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = pattern(os.Stdout, args, "Nn", f, c, d, lenFormat)
return err
},
}
var gapCmd = &cobra.Command{
Use: "gaps <infile[s]>",
Short: "Gap content",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = pattern(os.Stdout, args, "-", f, c, d, lenFormat)
return err
},
}
var softCmd = &cobra.Command{
Use: "soft <infile[s]>",
Short: "Softmasked content",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = pattern(os.Stdout, args, "atgcn", f, c, d, lenFormat)
return err
},
}
var patternCmd = &cobra.Command{
Use: "pattern -p PATTERN <infile[s]>",
Long: `e.g. fastats pattern -p AG <infile[s]>
`,
Short: "Arbitrary PATTERN content",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = pattern(os.Stdout, args, p, f, c, d, lenFormat)
return err
},
}
var lenCmd = &cobra.Command{
Use: "len <infile[s]>",
Short: "Sequence length",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var (
formatCount int
)
if kb {
formatCount += 1
lenFormat = "kb"
}
if mb {
formatCount += 1
lenFormat = "mb"
}
if gb {
formatCount += 1
lenFormat = "gb"
}
if formatCount > 1 {
return errors.New("Choose one of --kb, --mb, or --gb")
}
err = length(os.Stdout, args, p, f, c, d, lenFormat)
return err
},
}
var numCmd = &cobra.Command{
Use: "num <infile[s]>",
Short: "Number of records",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = num(os.Stdout, args, p, f, c, d, lenFormat)
return err
},
}
var nameCmd = &cobra.Command{
Use: "names <infile[s]>",
Short: "Record names",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = names(os.Stdout, args, p, f, c, d, lenFormat)
return err
},
}