-
Notifications
You must be signed in to change notification settings - Fork 16
/
nodes.go
87 lines (76 loc) · 1.79 KB
/
nodes.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
package cmd
import (
"fmt"
goio "io"
"os"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)
// nodesCmd represents the nodes command
var nodesCmd = &cobra.Command{
Use: "nodes",
Short: "Displays statistics on nodes of input tree",
Long: `Displays statistics on nodes of input tree.
Statistics are displayed in text format (tab separated):
1 - Id of node
2 - Nb neighbors
3 - Name of node
4 - depth of node (shortest path to a tip)
Example of usage:
gotree stats nodes -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
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f, outtreefile)
f.WriteString("tree\tnid\tnneigh\tname\tdepth\tcomments\tupnames\tdownnames\n")
var depth int
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.ComputeDepths()
for i, n := range t.Tree.Nodes() {
if depth, err = n.Depth(); err != nil {
io.LogError(err)
return
}
p, _ := n.Parent()
parentName := ""
childName := ""
if p != nil {
parentName = p.Name()
} else {
parentName = "-"
}
j := 0
for _, n := range n.Neigh() {
if n != p && n.Name() != "" {
if j > 0 {
childName += ","
}
j++
childName += n.Name()
}
}
f.WriteString(fmt.Sprintf("%d\t%d\t%d\t%s\t%d\t%s\t%s\t%s\n", t.Id, i, n.Nneigh(), n.Name(), depth, n.CommentsString(), parentName, childName))
}
}
return
},
}
func init() {
statsCmd.AddCommand(nodesCmd)
}