-
Notifications
You must be signed in to change notification settings - Fork 1
/
quartet.go
210 lines (203 loc) · 3.91 KB
/
quartet.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package gophy
import (
"errors"
"strconv"
)
// Quartet are represented as map[int]bools, one for the left and one for the right
type Quartet struct {
Lt map[int]bool
Rt map[int]bool
Lts []map[int]bool
Rts []map[int]bool
Len float64
Index int
}
// ConflictsWith checks whether two biparts conflict
func (b Quartet) ConflictsWith(ib Quartet) (con bool) {
con = false
if IntMapIntersects(ib.Rt, b.Rt) && IntMapIntersects(ib.Rt, b.Lt) {
if IntMapIntersects(ib.Lt, b.Rt) && IntMapIntersects(ib.Lt, b.Lt) {
con = true
return
}
}
return
}
// Match for matching quartets
func (b Quartet) Match(inq Quartet) (mat bool) {
mat = true
//need to match at least 2 for each
if b.ConflictsWith(inq) {
mat = false
return
}
lmatchedl := map[int]bool{}
lmatchedr := map[int]bool{}
for _, i := range b.Lts {
tl := 0
for j := range inq.Lts {
if IntMapIntersects(i, inq.Lts[j]) {
tl++
lmatchedl[j] = true
}
}
for j := range inq.Rts {
if IntMapIntersects(i, inq.Rts[j]) {
tl++
lmatchedr[j] = true
}
}
if tl > 1 {
mat = false
return
}
}
samed := true
lmatched := lmatchedl
if len(lmatchedl) > 0 && len(lmatchedr) > 0 {
mat = false
return
}
if len(lmatchedr) > 0 {
samed = false
lmatched = lmatchedr
}
if len(lmatched) < 2 {
mat = false
return
}
rmatchedl := map[int]bool{}
rmatchedr := map[int]bool{}
for _, i := range b.Rts {
tl := 0
for j := range inq.Lts {
if IntMapIntersects(i, inq.Lts[j]) {
tl++
rmatchedl[j] = true
}
}
for j := range inq.Rts {
if IntMapIntersects(i, inq.Rts[j]) {
tl++
rmatchedr[j] = true
}
}
if tl > 1 {
mat = false
return
}
}
if len(rmatchedl) > 0 && len(rmatchedr) > 0 {
mat = false
return
}
rmatched := rmatchedr
if samed && len(rmatchedl) > 0 {
mat = false
return
}
if samed == false && len(rmatchedr) > 0 {
mat = false
return
}
if samed == false {
rmatched = rmatchedl
}
if len(rmatched) < 2 {
mat = false
return
}
mat = true
return
}
// StringWithNames converts the ints to the the strings from nmmap
func (b Quartet) StringWithNames(nmmap map[int]string) (ret string) {
for cb, i := range b.Lts {
c := 0
for j := range i {
ret += nmmap[j]
if c < len(i)-1 {
ret += ","
}
c++
}
if cb < len(b.Lts)-1 {
ret += " | "
}
}
ret += " === "
for cb, i := range b.Rts {
c := 0
for j := range i {
ret += nmmap[j]
if c < len(i)-1 {
ret += ","
}
c++
}
if cb < len(b.Lts)-1 {
ret += " | "
}
}
s := strconv.FormatFloat(b.Len, 'f', 1, 64)
ret += " (" + s + ")"
return
}
//GetQuartet for node
func GetQuartet(nd *Node, tree Tree, maptips map[string]int) (Quartet, error) {
if len(nd.Chs) == 0 || nd == tree.Rt {
bp := Quartet{}
return bp, errors.New("root or child")
}
rights := []map[int]bool{}
lefts := []map[int]bool{}
right := map[int]bool{}
for _, i := range nd.Chs {
r := map[int]bool{}
for _, j := range i.GetTipNames() {
r[maptips[j]] = true
right[maptips[j]] = true
}
rights = append(rights, r)
}
p := nd.Par
for _, i := range p.Chs {
ilvs := map[int]bool{}
for _, j := range i.GetTipNames() {
ilvs[maptips[j]] = true
}
if IntMapIntersects(ilvs, right) {
continue
} else {
lefts = append(lefts, ilvs)
}
}
if p != tree.Rt {
out := map[int]bool{}
plvs := map[int]bool{}
for _, j := range tree.Rt.GetTipNames() {
out[maptips[j]] = true
}
for _, j := range p.GetTipNames() {
plvs[maptips[j]] = true
}
inter := IntMapDifferenceRet(out, plvs)
interm := map[int]bool{}
for _, j := range inter {
interm[j] = true
}
lefts = append(lefts, interm)
}
bp := Quartet{Lts: lefts, Rts: rights, Len: nd.Len}
return bp, nil
}
//GetQuartets return slice of quartets
func GetQuartets(tree Tree, maptips map[string]int) (bps []Quartet) {
for _, i := range tree.Pre {
q, err := GetQuartet(i, tree, maptips)
if err == nil {
bps = append(bps, q)
}
}
return bps
}