-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtips.go
74 lines (63 loc) · 1.58 KB
/
tips.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
package cmd
import (
"fmt"
goio "io"
"os"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)
func tipInfoRecur(t *tree.Tree, f *os.File, id int, cur *tree.Node, prev *tree.Node, prevEdge *tree.Edge, height float64) {
if cur == nil {
cur = t.Root()
}
if cur.Tip() {
f.WriteString(fmt.Sprintf("%d\t%d\t%d\t%s\t%.8f\t%.8f\n", id, cur.Id(), cur.Nneigh(), cur.Name(), prevEdge.Length(), height))
}
for i, n := range cur.Neigh() {
if n != prev {
e := cur.Edges()[i]
tipInfoRecur(t, f, id, n, cur, e, height+e.Length())
}
}
}
// tipsCmd represents the tips command
var tipsCmd = &cobra.Command{
Use: "tips",
Short: "Displays statistics on tips of input tree",
Long: `Displays statistics on tips of input tree
Statistics are displayed in text format (tab separated):
1 - Id of tip
2 - Nb neighbors
3 - Tip Name
Example of usage:
gotree stats tips -i t.mw
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
var treechan <-chan tree.Trees
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f, outtreefile)
f.WriteString("tree\tid\tnneigh\tname\tExternalBranch\tRootToTip\n")
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
}
tipInfoRecur(t.Tree, f, t.Id, nil, nil, nil, 0.0)
}
return
},
}
func init() {
statsCmd.AddCommand(tipsCmd)
}