Debian Codegraph @

kmer.go

raw symbols SCIP
1 // Copyright © 2018-2021 Wei Shen <[email protected]>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //b
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 package kmers
22
23 import (
24 "bytes"
25 "errors"
26 )
27
28 // ErrIllegalBase means that base beyond IUPAC symbols are detected.
29 var ErrIllegalBase = errors.New("kmers: illegal base")
30
31 // ErrKOverflow means K > 32.
32 var ErrKOverflow = errors.New("kmers: k-mer size (1-32) overflow")
33
34 // ErrCodeOverflow means the encode interger is bigger than 4^k.
35 var ErrCodeOverflow = errors.New("kmers: code value overflow")
36
37 // ErrKMismatch means K size mismatch.
38 var ErrKMismatch = errors.New("kmers: K mismatch")
39
40 // slice is much faster than switch and map.
41 var base2bit = [256]uint64{
42 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
43 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
44 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
45 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
46 4, 0, 1, 1, 0, 4, 4, 2, 0, 4, 4, 2, 4, 0, 0, 4,
47 4, 4, 0, 1, 3, 3, 0, 0, 4, 1, 4, 4, 4, 4, 4, 4,
48 4, 0, 1, 1, 0, 4, 4, 2, 0, 4, 4, 2, 4, 0, 0, 4,
49 4, 4, 0, 1, 3, 3, 0, 0, 4, 1, 4, 4, 4, 4, 4, 4,
50 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
51 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
52 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
53 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
54 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
55 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
56 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
57 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
58 }
59
60 // var base2bit []uint64
61
62 // MaxCode is the maxinum interger for all Ks.
63 var MaxCode []uint64
64
65 func init() {
66 MaxCode = make([]uint64, 33)
67 for i := 1; i <= 32; i++ {
68 MaxCode[i] = 1<<uint(i*2) - 1
69 }
70 }
71
72 // Encode converts byte slice to bits.
73 //
74 // Codes:
75 //
76 // A 0b00
77 // C 0b01
78 // G 0b10
79 // T 0b11
80 //
81 // For degenerate bases, only the first base is kept.
82 //
83 // M AC A
84 // V ACG A
85 // H ACT A
86 // R AG A
87 // D AGT A
88 // W AT A
89 // S CG C
90 // B CGT C
91 // Y CT C
92 // K GT G
93 // N ACGT A
94 //
95 func Encode(kmer []byte) (code uint64, err error) {
96 if len(kmer) == 0 || len(kmer) > 32 {
97 return 0, ErrKOverflow
98 }
99
100 var v uint64
101 for _, b := range kmer {
102 code <<= 2
103 v = base2bit[b]
104 // if v > 3 {
105 if v == 4 {
106 return code, ErrIllegalBase
107 }
108 code |= v
109 }
110 return code, nil
111 }
112
113 // ErrNotConsecutiveKmers means the two k-mers are not consecutive.
114 var ErrNotConsecutiveKmers = errors.New("kmers: not consecutive k-mers")
115
116 // MustEncodeFromFormerKmer encodes from former the k-mer,
117 // assuming the k-mer and leftKmer are both OK.
118 func MustEncodeFromFormerKmer(kmer []byte, leftKmer []byte, leftCode uint64) (uint64, error) {
119 v := base2bit[kmer[len(kmer)-1]]
120 // if v > 3 {
121 if v == 4 {
122 return leftCode, ErrIllegalBase
123 }
124 // retrieve (k-1)*2 bits and << 2, and then add v
125 return leftCode&((1<<(uint(len(kmer)-1)<<1))-1)<<2 | v, nil
126 }
127
128 // EncodeFromFormerKmer encodes from the former k-mer, inspired by ntHash
129 func EncodeFromFormerKmer(kmer []byte, leftKmer []byte, leftCode uint64) (uint64, error) {
130 if len(kmer) == 0 {
131 return 0, ErrKOverflow
132 }
133 if len(kmer) != len(leftKmer) {
134 return 0, ErrKMismatch
135 }
136 if !bytes.Equal(kmer[0:len(kmer)-1], leftKmer[1:]) {
137 return 0, ErrNotConsecutiveKmers
138 }
139 return MustEncodeFromFormerKmer(kmer, leftKmer, leftCode)
140 }
141
142 // MustEncodeFromLatterKmer encodes from the latter k-mer,
143 // assuming the k-mer and rightKmer are both OK.
144 func MustEncodeFromLatterKmer(kmer []byte, rightKmer []byte, rightCode uint64) (uint64, error) {
145 v := base2bit[kmer[0]]
146 // if v > 3 {
147 if v == 4 {
148 return rightCode, ErrIllegalBase
149 }
150
151 return v<<(uint(len(kmer)-1)<<1) | rightCode>>2, nil
152 }
153
154 // EncodeFromLatterKmer encodes from the former k-mer.
155 func EncodeFromLatterKmer(kmer []byte, rightKmer []byte, rightCode uint64) (uint64, error) {
156 if len(kmer) == 0 {
157 return 0, ErrKOverflow
158 }
159 if len(kmer) != len(rightKmer) {
160 return 0, ErrKMismatch
161 }
162 if !bytes.Equal(rightKmer[0:len(kmer)-1], kmer[1:len(rightKmer)]) {
163 return 0, ErrNotConsecutiveKmers
164 }
165 return MustEncodeFromLatterKmer(kmer, rightKmer, rightCode)
166 }
167
168 // Reverse returns code of the reversed sequence.
169 func Reverse(code uint64, k int) (c uint64) {
170 if k <= 0 || k > 32 {
171 panic(ErrKOverflow)
172 }
173 // for i := 0; i < k; i++ {
174 // c = (c << 2) | (code & 3)
175 // code >>= 2
176 // }
177 // return
178
179 // https: //www.biostars.org/p/113640, with a little modification
180 c = code
181 c = ((c >> 2 & 0x3333333333333333) | (c&0x3333333333333333)<<2)
182 c = ((c >> 4 & 0x0F0F0F0F0F0F0F0F) | (c&0x0F0F0F0F0F0F0F0F)<<4)
183 c = ((c >> 8 & 0x00FF00FF00FF00FF) | (c&0x00FF00FF00FF00FF)<<8)
184 c = ((c >> 16 & 0x0000FFFF0000FFFF) | (c&0x0000FFFF0000FFFF)<<16)
185 c = ((c >> 32 & 0x00000000FFFFFFFF) | (c&0x00000000FFFFFFFF)<<32)
186 return (c >> (2 * (32 - k)))
187 }
188
189 // MustReverse is similar to Reverse, but does not check k.
190 func MustReverse(code uint64, k int) (c uint64) {
191 // for i := 0; i < k; i++ {
192 // c = (c << 2) | (code & 3)
193 // code >>= 2
194 // }
195 // return
196
197 // https: //www.biostars.org/p/113640, with a little modification
198 c = code
199 c = ((c >> 2 & 0x3333333333333333) | (c&0x3333333333333333)<<2)
200 c = ((c >> 4 & 0x0F0F0F0F0F0F0F0F) | (c&0x0F0F0F0F0F0F0F0F)<<4)
201 c = ((c >> 8 & 0x00FF00FF00FF00FF) | (c&0x00FF00FF00FF00FF)<<8)
202 c = ((c >> 16 & 0x0000FFFF0000FFFF) | (c&0x0000FFFF0000FFFF)<<16)
203 c = ((c >> 32 & 0x00000000FFFFFFFF) | (c&0x00000000FFFFFFFF)<<32)
204 return (c >> (2 * (32 - k)))
205 }
206
207 // Complement returns code of complement sequence.
208 func Complement(code uint64, k int) uint64 {
209 if k <= 0 || k > 32 {
210 panic(ErrKOverflow)
211 }
212 return code ^ (1<<uint(k<<1) - 1)
213 }
214
215 // MustComplement is similar to Complement, but does not check k.
216 func MustComplement(code uint64, k int) uint64 {
217 return code ^ (1<<uint(k<<1) - 1)
218 }
219
220 // RevComp returns code of reverse complement sequence.
221 func RevComp(code uint64, k int) (c uint64) {
222 if k <= 0 || k > 32 {
223 panic(ErrKOverflow)
224 }
225 // for i := 0; i < k; i++ {
226 // c = (c << 2) | (code&3 ^ 3)
227 // code >>= 2
228 // }
229 // return
230
231 // https://www.biostars.org/p/113640/#9474334
232 c = ^code
233 c = ((c >> 2 & 0x3333333333333333) | (c&0x3333333333333333)<<2)
234 c = ((c >> 4 & 0x0F0F0F0F0F0F0F0F) | (c&0x0F0F0F0F0F0F0F0F)<<4)
235 c = ((c >> 8 & 0x00FF00FF00FF00FF) | (c&0x00FF00FF00FF00FF)<<8)
236 c = ((c >> 16 & 0x0000FFFF0000FFFF) | (c&0x0000FFFF0000FFFF)<<16)
237 c = ((c >> 32 & 0x00000000FFFFFFFF) | (c&0x00000000FFFFFFFF)<<32)
238 return (c >> (2 * (32 - k)))
239 }
240
241 // MustRevComp is similar to RevComp, but does not check k.
242 func MustRevComp(code uint64, k int) (c uint64) {
243 // for i := 0; i < k; i++ {
244 // c = (c << 2) | (code&3 ^ 3)
245 // code >>= 2
246 // }
247 // return
248
249 // https://www.biostars.org/p/113640/#9474334
250 c = ^code
251 c = ((c >> 2 & 0x3333333333333333) | (c&0x3333333333333333)<<2)
252 c = ((c >> 4 & 0x0F0F0F0F0F0F0F0F) | (c&0x0F0F0F0F0F0F0F0F)<<4)
253 c = ((c >> 8 & 0x00FF00FF00FF00FF) | (c&0x00FF00FF00FF00FF)<<8)
254 c = ((c >> 16 & 0x0000FFFF0000FFFF) | (c&0x0000FFFF0000FFFF)<<16)
255 c = ((c >> 32 & 0x00000000FFFFFFFF) | (c&0x00000000FFFFFFFF)<<32)
256 return (c >> (2 * (32 - k)))
257 }
258
259 // Canonical returns code of its canonical kmer.
260 func Canonical(code uint64, k int) uint64 {
261 if k <= 0 || k > 32 {
262 panic(ErrKOverflow)
263 }
264
265 var rc uint64
266 // c := code
267 // for i := 0; i < k; i++ {
268 // rc = (rc << 2) | (c&3 ^ 3)
269 // c >>= 2
270 // }
271
272 // https://www.biostars.org/p/113640/#9474334
273 c := ^code
274 c = ((c >> 2 & 0x3333333333333333) | (c&0x3333333333333333)<<2)
275 c = ((c >> 4 & 0x0F0F0F0F0F0F0F0F) | (c&0x0F0F0F0F0F0F0F0F)<<4)
276 c = ((c >> 8 & 0x00FF00FF00FF00FF) | (c&0x00FF00FF00FF00FF)<<8)
277 c = ((c >> 16 & 0x0000FFFF0000FFFF) | (c&0x0000FFFF0000FFFF)<<16)
278 c = ((c >> 32 & 0x00000000FFFFFFFF) | (c&0x00000000FFFFFFFF)<<32)
279 rc = (c >> (2 * (32 - k)))
280
281 if rc < code {
282 return rc
283 }
284 return code
285 }
286
287 // MustCanonical is similar to Canonical, but does not check k.
288 func MustCanonical(code uint64, k int) uint64 {
289 var rc uint64
290 // c := code
291 // for i := 0; i < k; i++ {
292 // rc = (rc << 2) | (c&3 ^ 3)
293 // c >>= 2
294 // }
295
296 // https://www.biostars.org/p/113640/#9474334
297 c := ^code
298 c = ((c >> 2 & 0x3333333333333333) | (c&0x3333333333333333)<<2)
299 c = ((c >> 4 & 0x0F0F0F0F0F0F0F0F) | (c&0x0F0F0F0F0F0F0F0F)<<4)
300 c = ((c >> 8 & 0x00FF00FF00FF00FF) | (c&0x00FF00FF00FF00FF)<<8)
301 c = ((c >> 16 & 0x0000FFFF0000FFFF) | (c&0x0000FFFF0000FFFF)<<16)
302 c = ((c >> 32 & 0x00000000FFFFFFFF) | (c&0x00000000FFFFFFFF)<<32)
303 rc = (c >> (2 * (32 - k)))
304
305 if rc < code {
306 return rc
307 }
308 return code
309 }
310
311 // bit2base is for mapping bit to base.
312 var bit2base = [4]byte{'A', 'C', 'G', 'T'}
313
314 // bit2str is for output bits string
315 var bit2str = [4]string{"00", "01", "10", "11"}
316
317 // Decode converts the code to original seq
318 func Decode(code uint64, k int) []byte {
319 if k <= 0 || k > 32 {
320 panic(ErrKOverflow)
321 }
322 if code > MaxCode[k] {
323 panic(ErrCodeOverflow)
324 }
325 kmer := make([]byte, k)
326 for i := 0; i < k; i++ {
327 kmer[k-1-i] = bit2base[code&3]
328 code >>= 2
329 }
330 return kmer
331 }
332
333 // MustDecode is similar to Decode, but does not check k and code.
334 func MustDecode(code uint64, k int) []byte {
335 kmer := make([]byte, k)
336 for i := 0; i < k; i++ {
337 kmer[k-1-i] = bit2base[code&3]
338 code >>= 2
339 }
340 return kmer
341 }
342
343 // KmerCode is a struct representing a k-mer in 64-bits.
344 type KmerCode struct {
345 Code uint64
346 K int
347 }
348
349 // NewKmerCode returns a new KmerCode struct from byte slice.
350 func NewKmerCode(kmer []byte) (KmerCode, error) {
351 code, err := Encode(kmer)
352 if err != nil {
353 return KmerCode{}, err
354 }
355 return KmerCode{code, len(kmer)}, err
356 }
357
358 // NewKmerCodeFromFormerOne computes KmerCode from the Former consecutive k-mer.
359 func NewKmerCodeFromFormerOne(kmer []byte, leftKmer []byte, preKcode KmerCode) (KmerCode, error) {
360 code, err := EncodeFromFormerKmer(kmer, leftKmer, preKcode.Code)
361 if err != nil {
362 return KmerCode{}, err
363 }
364 return KmerCode{code, len(kmer)}, err
365 }
366
367 // NewKmerCodeMustFromFormerOne computes KmerCode from the Former consecutive k-mer,
368 // assuming the k-mer and leftKmer are both OK.
369 func NewKmerCodeMustFromFormerOne(kmer []byte, leftKmer []byte, preKcode KmerCode) (KmerCode, error) {
370 code, err := MustEncodeFromFormerKmer(kmer, leftKmer, preKcode.Code)
371 if err != nil {
372 return KmerCode{}, err
373 }
374 return KmerCode{code, len(kmer)}, err
375 }
376
377 // Equal checks wether two KmerCodes are the same.
378 func (kcode KmerCode) Equal(kcode2 KmerCode) bool {
379 return kcode.K == kcode2.K && kcode.Code == kcode2.Code
380 }
381
382 // Rev returns KmerCode of the reverse sequence.
383 func (kcode KmerCode) Rev() KmerCode {
384 return KmerCode{MustReverse(kcode.Code, kcode.K), kcode.K}
385 }
386
387 // Comp returns KmerCode of the complement sequence.
388 func (kcode KmerCode) Comp() KmerCode {
389 return KmerCode{MustComplement(kcode.Code, kcode.K), kcode.K}
390 }
391
392 // RevComp returns KmerCode of the reverse complement sequence.
393 func (kcode KmerCode) RevComp() KmerCode {
394 return KmerCode{MustRevComp(kcode.Code, kcode.K), kcode.K}
395 }
396
397 // Canonical returns its canonical kmer
398 func (kcode KmerCode) Canonical() KmerCode {
399 rcKcode := kcode.RevComp()
400 if rcKcode.Code < kcode.Code {
401 return rcKcode
402 }
403 return kcode
404 }
405
406 // Bytes returns k-mer in []byte.
407 func (kcode KmerCode) Bytes() []byte {
408 return Decode(kcode.Code, kcode.K)
409 }
410
411 // String returns k-mer in string
412 func (kcode KmerCode) String() string {
413 return string(Decode(kcode.Code, kcode.K))
414 }
415
416 // BitsString returns code to string
417 func (kcode KmerCode) BitsString() string {
418 var buf bytes.Buffer
419 for _, b := range Decode(kcode.Code, kcode.K) {
420 buf.WriteString(bit2str[base2bit[b]])
421 }
422 return buf.String()
423 }