-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbalancedtree.go
54 lines (46 loc) · 1.15 KB
/
balancedtree.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
package cmd
import (
"os"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)
func balancedTree(nbtrees int, depth int, output string, rooted bool) error {
var f *os.File
var err error
var t *tree.Tree
if output != "stdout" && output != "-" {
f, err = os.Create(output)
defer f.Close()
} else {
f = os.Stdout
}
if err != nil {
return err
}
for i := 0; i < nbtrees; i++ {
t, err = tree.RandomBalancedBinaryTree(depth, rooted)
if err != nil {
return err
}
f.WriteString(t.Newick() + "\n")
}
return nil
}
// binarytreeCmd represents the binarytree command
var balancedtreeCmd = &cobra.Command{
Use: "balancedtree",
Short: "Generates a random balanced binary tree",
Long: `Generates a random balanced binary tree
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if err = balancedTree(generateNbTrees, generateDepth, generateOutputfile, generateRooted); err != nil {
io.LogError(err)
}
return
},
}
func init() {
generateCmd.AddCommand(balancedtreeCmd)
balancedtreeCmd.PersistentFlags().IntVarP(&generateDepth, "depth", "d", 3, "Depth of the balanced binary tree")
}