-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmer_equivalence_relation.go
218 lines (200 loc) · 5.5 KB
/
kmer_equivalence_relation.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
211
212
213
214
215
216
217
218
/* Copyright (C) 2019 Philipp Benner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gonetics
/* -------------------------------------------------------------------------- */
import "fmt"
/* -------------------------------------------------------------------------- */
type KmerEquivalence struct {
N, M int // min and max kmer size
Complement bool
Reverse bool
Revcomp bool
MaxAmbiguous []int // maximum number of ambiguous letters
Alphabet ComplementableAlphabet
}
/* -------------------------------------------------------------------------- */
func NewKmerEquivalence(n, m int, comp, rev, rc bool, maxAmbiguous []int, al ComplementableAlphabet) (KmerEquivalence, error) {
if len(maxAmbiguous) == 0 {
maxAmbiguous = make([]int, m-n+1)
for i := 0; i < m-n+1; i++ {
maxAmbiguous[i] = -1
}
} else
if len(maxAmbiguous) == 1 {
x := maxAmbiguous[0]
maxAmbiguous = make([]int, m-n+1)
for i := 0; i < m-n+1; i++ {
maxAmbiguous[i] = x
}
} else
if len(maxAmbiguous) != m-n+1 {
return KmerEquivalence{}, fmt.Errorf("parameter `maxAmbiguous' has invalid length")
}
r := KmerEquivalence{}
r.N = n
r.M = m
r.Complement = comp
r.Reverse = rev
r.Revcomp = rc
r.MaxAmbiguous = maxAmbiguous
r.Alphabet = al
return r, nil
}
/* -------------------------------------------------------------------------- */
func (a KmerEquivalence) Equals(b KmerEquivalence) bool {
if a.M != b.M {
return false
}
if a.N != b.N {
return false
}
if a.Complement != b.Complement {
return false
}
if a.Reverse != b.Reverse {
return false
}
if a.Revcomp != b.Revcomp {
return false
}
if a.Alphabet.String() != b.Alphabet.String() {
return false
}
if len(a.MaxAmbiguous) != len(b.MaxAmbiguous) {
return false
}
for i := 0; i < len(a.MaxAmbiguous); i++ {
if a.MaxAmbiguous[i] != b.MaxAmbiguous[i] {
return false
}
}
return true
}
/* -------------------------------------------------------------------------- */
type KmerEquivalenceRelation struct {
KmerEquivalence
p []int // pre-evaluated powers
}
/* -------------------------------------------------------------------------- */
func NewKmerEquivalenceRelation(n, m int, comp, rev, rc bool, maxAmbiguous []int, al ComplementableAlphabet) (KmerEquivalenceRelation, error) {
r := KmerEquivalenceRelation{}
p := make([]int, m+1)
for k := 0; k <= m; k++ {
p[k] = iPow(al.Length(), k)
}
t, err := NewKmerEquivalence(n, m, comp, rev, rc, maxAmbiguous, al)
if err != nil {
return r, err
}
r.KmerEquivalence = t
r.p = p
return r, nil
}
/* -------------------------------------------------------------------------- */
func (obj KmerEquivalenceRelation) EquivalenceClass(kmer string) KmerClass {
k := len(kmer)
c1 := []byte(kmer)
c2 := make([]byte, k)
c3 := make([]byte, k)
c4 := make([]byte, k)
// compute indices
i := 0 // id
i_c := 0 // id of complement
i_r := 0 // id of reverse
i_rc := 0 // id of reverse complement
for j := 0; j < k; j++ {
x1, _ := obj.Alphabet.Code(c1[ j])
x2, _ := obj.Alphabet.Code(c1[k-j-1])
y1, _ := obj.Alphabet.ComplementCoded(x1)
y2, _ := obj.Alphabet.ComplementCoded(x2)
i += int(x2) * obj.p[j]
i_c += int(y2) * obj.p[j]
i_r += int(x1) * obj.p[j]
i_rc += int(y1) * obj.p[j]
}
// find minimum
if obj.Complement && i > i_c {
i = i_c
} else {
i_c = -1
}
if obj.Reverse && i > i_r {
i = i_r
} else {
i_r = -1
}
if obj.Revcomp && i > i_rc {
i = i_rc
} else {
i_rc = -1
}
// compute name
b_c2 := false
b_c3 := false
b_c4 := false
switch i {
case i_c:
obj.comp(c2, c1)
c1, c2 = c2, c1
b_c2 = true
case i_r:
obj.rev (c3, c1)
c1, c3 = c3, c1
b_c3 = true
case i_rc:
obj.rev (c2, c1)
obj.comp(c4, c2)
c1, c4 = c4, c1
b_c2 = true
b_c4 = true
}
if !b_c2 && obj.Complement || obj.Revcomp {
obj.comp(c2, c1)
}
if !b_c3 && obj.Reverse || obj.Revcomp {
obj.rev (c3, c1)
}
if !b_c4 && obj.Revcomp {
obj.rev (c4, c2)
}
elements := []string{string(c1)}
if obj.Complement {
elements = append(elements, string(c2))
}
if obj.Reverse {
elements = append(elements, string(c3))
}
if obj.Revcomp {
elements = append(elements, string(c4))
}
return NewKmerClass(k, i, elements)
}
/* -------------------------------------------------------------------------- */
func (obj KmerEquivalenceRelation) comp(dest, src []byte) error {
for j := 0; j < len(src); j++ {
if x, err := obj.Alphabet.Complement(src[j]); err != nil {
return err
} else {
dest[j] = x
}
}
return nil
}
func (obj KmerEquivalenceRelation) rev(dest, src []byte) {
for i, j := 0, len(src)-1; i <= j; i, j = i+1, j-1 {
dest[i], dest[j] = src[j], src[i]
}
}