forked from evolbioinfo/gotree
/
rotate_sort.go
62 lines (52 loc) · 1.38 KB
/
rotate_sort.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
package cmd
import (
goio "io"
"os"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)
// rotateCmd represents the shuffletips command
var rotateSortCmd = &cobra.Command{
Use: "sort",
Short: "Sorts children of internal nodes by number of tips",
Long: `Sorts children of internal nodes by number of tips.
It does not change the topology, but just the order of neighbors
of all node and thus the newick representation.
------C ------A
x |z x |z
A---------*ROOT => B---------*ROOT
|t |t
------B ------C
Example of usage:
gotree rotate sort -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.SortNeighborsByTips()
f.WriteString(t.Tree.Newick() + "\n")
}
return
},
}
func init() {
rotateCmd.AddCommand(rotateSortCmd)
}