forked from evolbioinfo/gotree
/
shuffletips.go
63 lines (52 loc) · 1.44 KB
/
shuffletips.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
package cmd
import (
goio "io"
"os"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)
// shuffletipsCmd represents the shuffletips command
var shuffletipsCmd = &cobra.Command{
Use: "shuffletips",
Short: "Shuffle tip names of an input tree",
Long: `Shuffle tip names of an input tree.
------C ------A
x |z x |z
A---------*ROOT => B---------*ROOT
|t |t
------B ------C
Example of usage:
gotree shuffletips -i t.nw
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
var treechan <-chan tree.Trees
// Read Tree
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f, outtreefile)
if treefile, treechan, err = readTrees(intreefile); err != nil {
io.LogError(err)
return
}
defer treefile.Close()
for t := range treechan {
if t.Err != nil {
io.LogError(t.Err)
return t.Err
}
t.Tree.ShuffleTips()
f.WriteString(t.Tree.Newick() + "\n")
}
return
},
}
func init() {
RootCmd.AddCommand(shuffletipsCmd)
shuffletipsCmd.PersistentFlags().StringVarP(&intreefile, "input", "i", "stdin", "Input tree")
shuffletipsCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "Shuffled tree output file")
}