-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathroot.go
312 lines (270 loc) · 7.45 KB
/
root.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package cmd
import (
"bufio"
"compress/gzip"
"errors"
"fmt"
goio "io"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/evolbioinfo/gotree/io/fileutils"
"github.com/evolbioinfo/gotree/io/utils"
"github.com/evolbioinfo/gotree/tree"
"github.com/fredericlemoine/cobrashell"
"github.com/spf13/cobra"
)
// Variables used in lots of commands
var inalignfile string
var intreefile, intree2file, outtreefile string
var outresfile string
var seed int64 = -1
var inputname string
var mapfile string
var revert bool
var transferdist bool
var deepestedge bool
var edgeformattext bool
var parsimonyAlgo string
var compareTips bool
var tipfile string
var cutoff float64
var replace bool
var treeformat = utils.FORMAT_NEWICK
var cfgFile string
var rootCpus int
var rootInputFormat string
var removeoutgroup bool
var rerootstrict bool
var helptemplate string = `{{with or .Long .Short }}{{. | trim}}
{{end}}Version: ` + Version + `
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}
If you use the Gotree/Goalign toolkit, please cite:
Lemoine F, Gascuel O.
Gotree/Goalign: toolkit and Go API to facilitate the development of phylogenetic workflows.
NAR Genom Bioinform. 2021 Aug 11;3(3):lqab075.
doi: 10.1093/nargab/lqab075. PMID: 34396097; PMCID: PMC8356961.
`
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "gotree",
Short: "gotree: A set of tools to handle phylogenetic trees in go",
Long: `gotree is a set of tools to handle phylogenetic trees in go.
Different usages are implemented:
- Generating random trees
- Transforming trees (renaming tips, pruning/removing tips)
- Comparing trees (computing bootstrap supports, counting common edges)
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
switch rootInputFormat {
case "newick":
treeformat = utils.FORMAT_NEWICK
case "nexus":
treeformat = utils.FORMAT_NEXUS
case "phyloxml":
treeformat = utils.FORMAT_PHYLOXML
case "nextstrain":
treeformat = utils.FORMAT_NEXTSTRAIN
default:
treeformat = utils.FORMAT_NEWICK
}
if seed == -1 {
seed = time.Now().UTC().UnixNano()
}
rand.Seed(seed)
},
Run: func(cmd *cobra.Command, args []string) {
s := cobrashell.New()
// display welcome info.
s.Println(fmt.Sprintf("Welcome to Gotree Console %s", Version))
s.Println("type \"help\" to get a list of available commands")
cobrashell.AddCommands(s, cmd.Root(), nil, cmd.Root().Commands()...)
// We open a gotree console to interactively execute commands
s.Run()
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
},
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
maxcpus := runtime.NumCPU()
RootCmd.PersistentFlags().Int64Var(&seed, "seed", -1, "Random Seed: -1 = nano seconds since 1970/01/01 00:00:00")
RootCmd.PersistentFlags().IntVarP(&rootCpus, "threads", "t", 1, "Number of threads (Max="+strconv.Itoa(maxcpus)+")")
RootCmd.PersistentFlags().StringVar(&rootInputFormat, "format", "newick", "Input tree format (newick, nexus, phyloxml, or nextstrain)")
RootCmd.SetHelpTemplate(helptemplate)
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
}
func openWriteFile(file string) (f *os.File, err error) {
if file == "stdout" || file == "-" {
f = os.Stdout
} else {
f, err = os.Create(file)
}
return
}
func closeWriteFile(f goio.Closer, filename string) {
if filename != "-" && filename != "stdout" {
f.Close()
}
}
// Readln returns a single line (without the ending \n)
// from the input buffered reader.
// An error is returned iff there is an error with the
// buffered reader.
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
/*File in output must be closed by calling function */
func readTrees(infile string) (treefile goio.Closer, treeChannel <-chan tree.Trees, err error) {
// Read Tree
var treereader *bufio.Reader
if treefile, treereader, err = utils.GetReader(infile); err == nil {
treeChannel = utils.ReadMultiTrees(treereader, treeformat)
}
return
}
func readTree(infile string) (t *tree.Tree, err error) {
if infile != "none" {
// Read comp Tree : Only one tree in input
t, err = utils.ReadTree(infile, treeformat)
} else {
err = errors.New("cannot use \"none\" as input file")
}
return
}
// Parse a file with one string per line
func parseStringFile(file string) (s []string, err error) {
var ifilereader *bufio.Reader
var ifile goio.Closer
var line string
var err2 error
s = make([]string, 0, 100)
if ifile, ifilereader, err = utils.GetReader(file); err == nil {
line, err2 = Readln(ifilereader)
for err2 == nil {
s = append(s, strings.Split(line, ",")...)
line, err2 = Readln(ifilereader)
}
ifile.Close()
}
return
}
// Parse a file with one int per line
func parseIntFile(file string) (islice []int, err error) {
var ifilereader *bufio.Reader
var ifile goio.Closer
var line string
var err2 error
var tempi int
islice = make([]int, 0, 100)
if ifile, ifilereader, err = utils.GetReader(file); err == nil {
line, err2 = Readln(ifilereader)
for err2 == nil {
for _, name := range strings.Split(line, ",") {
if tempi, err = strconv.Atoi(name); err != nil {
return
}
islice = append(islice, tempi)
}
line, err2 = Readln(ifilereader)
}
ifile.Close()
}
return
}
// Parse a file with one tip name per line
func parseTipsFile(file string) (tips []string, err error) {
tips, err = parseStringFile(file)
return
}
func readMapFile(file string, revert bool) (map[string]string, error) {
outmap := make(map[string]string, 0)
var mapfile *os.File
var err error
var reader *bufio.Reader
if mapfile, err = os.Open(file); err != nil {
return outmap, err
}
if strings.HasSuffix(file, ".gz") {
if gr, err2 := gzip.NewReader(mapfile); err2 != nil {
return outmap, err2
} else {
reader = bufio.NewReader(gr)
}
} else {
reader = bufio.NewReader(mapfile)
}
line, e := fileutils.Readln(reader)
nl := 1
for e == nil {
cols := strings.Split(line, "\t")
if len(cols) != 2 {
return outmap, errors.New("Map file does not have 2 fields at line: " + fmt.Sprintf("%d", nl))
}
if revert {
outmap[cols[1]] = cols[0]
} else {
outmap[cols[0]] = cols[1]
}
line, e = fileutils.Readln(reader)
nl++
}
if err = mapfile.Close(); err != nil {
return outmap, err
}
return outmap, nil
}
func readIdenticalGroupFile(file string) (groups [][]string, err error) {
var groupfile *os.File
var reader *bufio.Reader
var gr *gzip.Reader
groups = make([][]string, 0)
if groupfile, err = os.Open(file); err != nil {
return
}
if strings.HasSuffix(file, ".gz") {
if gr, err = gzip.NewReader(groupfile); err != nil {
return
}
reader = bufio.NewReader(gr)
defer gr.Close()
} else {
reader = bufio.NewReader(groupfile)
}
line, e := fileutils.Readln(reader)
nl := 1
for e == nil {
cols := strings.Split(line, ",")
if len(cols) == 0 {
err = errors.New("Found empty line in file of group of identical tips")
return
}
groups = append(groups, cols)
line, e = fileutils.Readln(reader)
nl++
}
err = groupfile.Close()
return
}