-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcomparetrees.go
182 lines (159 loc) · 5 KB
/
comparetrees.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cmd
import (
"errors"
"fmt"
goio "io"
"math"
"runtime"
"github.com/spf13/cobra"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
)
var comparetreeidentical bool
var comparetreerf bool
var comparetreeweighted bool
// compareCmd represents the compare command
var compareTreesCmd = &cobra.Command{
Use: "trees",
Short: "Compare a reference tree with a set of trees",
Long: `Compare a reference tree with a set of trees.
If --binary is given:
For each trees in the compared tree file, it will print tab separated values with:
1) The index of the compared tree in the file
2) "true" if the tree is identical,
"false" otherwise
Otherwise:
For each trees in the compared tree file, it will print tab separated values with:
1) The index of the compared tree in the file
2) The number of branches that are specific to the reference tree
3) The number of branches that are common to both trees
4) The number of branches that are specific to the compared tree
If --rf is given, it only computes the Robinson-Foulds distance, as the sum of
reference + compared specific branches.
If --weighted is given:
For each trees in the compared tree file, it will print tab separated values with:
1) The index of the compared tree in the file
2) The weighted Robinson-Foulds distance (Robinson & Foulds, 1979)
3) The Khuner-Felsenstein branch score (Khuner & Felsenstein, 1994)
If --weighted and --binary are given:
For each trees in the compared tree file, it will print tab separated values with:
1) The index of the compared tree in the file
2) "true" if the tree is identical, both in topology and branch lengths,
"false" otherwise
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var treefile goio.Closer
var treechan <-chan tree.Trees
var refTree *tree.Tree
var stats <-chan tree.BipartitionStats
if intree2file == "none" {
err = errors.New("You must provide a file containing compared trees")
io.LogError(err)
return
}
maxcpus := runtime.NumCPU()
if rootCpus > maxcpus {
rootCpus = maxcpus
}
if refTree, err = readTree(intreefile); err != nil {
io.LogError(err)
return
}
if err = refTree.ReinitIndexes(); err != nil {
io.LogError(err)
}
if treefile, treechan, err = readTrees(intree2file); err != nil {
io.LogError(err)
return
}
defer treefile.Close()
if comparetreeweighted {
if stats2, err := tree.CompareWeighted(refTree, treechan, compareTips, comparetreeidentical, rootCpus); err != nil {
io.LogError(err)
return err
} else {
if comparetreeidentical {
fmt.Printf("tree\tidentical\n")
} else {
fmt.Printf("tree\tweighted_RF\tKF\n")
}
for st := range stats2 {
if st.Err != nil {
/* We empty the channel if needed*/
for range stats {
}
io.LogError(st.Err)
return st.Err
}
if comparetreeidentical {
fmt.Printf("%d\t%v\n", st.Id, st.Sametree)
} else {
// Computing Weighted Robinson-Foulds and Khuner-Felsenstein distances.
wrf := 0.0
kf := 0.0
for _, diff := range st.Common {
wrf += math.Abs(diff)
kf += math.Pow(diff, 2.0)
}
for _, container := range [][]float64{st.Tree1, st.Tree2} {
for _, length := range container {
wrf += length
kf += math.Pow(length, 2.0)
}
}
fmt.Printf("%d\t%E\t%E\n", st.Id, wrf, math.Sqrt(kf))
}
}
}
return
}
if stats, err = tree.Compare(refTree, treechan, compareTips, comparetreeidentical, rootCpus); err != nil {
io.LogError(err)
return
}
if comparetreeidentical {
fmt.Printf("tree\tidentical\n")
for st := range stats {
if st.Err != nil {
/* We empty the channel if needed*/
for range stats {
}
io.LogError(st.Err)
return st.Err
}
fmt.Printf("%d\t%v\n", st.Id, st.Sametree)
}
} else if comparetreerf {
for st := range stats {
if st.Err != nil {
/* We empty the channel if needed*/
for range stats {
}
io.LogError(st.Err)
return st.Err
}
fmt.Printf("%d\n", st.Tree1+st.Tree2)
}
} else {
fmt.Printf("tree\treference\tcommon\tcompared\n")
for st := range stats {
if st.Err != nil {
/* We empty the channel if needed*/
for range stats {
}
io.LogError(st.Err)
return st.Err
}
fmt.Printf("%d\t%d\t%d\t%d\n", st.Id, st.Tree1, st.Common, st.Tree2)
}
}
return
},
}
func init() {
compareCmd.AddCommand(compareTreesCmd)
compareTreesCmd.Flags().BoolVarP(&compareTips, "tips", "l", false, "Include tips in the comparison")
compareTreesCmd.Flags().BoolVar(&comparetreeidentical, "binary", false, "If true, then just print true (identical tree) or false (different tree) for each compared tree")
compareTreesCmd.Flags().BoolVar(&comparetreerf, "rf", false, "If true, outputs Robinson-Foulds distance, as the sum of reference + compared specific branches")
compareTreesCmd.Flags().BoolVar(&comparetreeweighted, "weighted", false, "If true, outputs comparison metrics including branch lengths")
}