-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathKmer.cpp
More file actions
367 lines (298 loc) · 9.24 KB
/
Kmer.cpp
File metadata and controls
367 lines (298 loc) · 9.24 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "Kmer.hpp"
// use: int2bin(a, buffer, buf_size);
// pre: buf_size >= 8 and buffer has space for buf_size elements
// post: buffer[0,...,7] is the binary representation of a
void int2bin(uint32_t a, char *buffer, int buf_size) {
//buffer += (buf_size - 1);
for (int i = 7; i >= 0; i--) {
*buffer++ = (a & 1) + '0';
a >>= 1;
}
}
static const uint8_t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0x2e, 0x6e, 0xae, 0xee, 0x3e, 0x7e, 0xbe, 0xfe,
0x03, 0x43, 0x83, 0xc3, 0x13, 0x53, 0x93, 0xd3,
0x23, 0x63, 0xa3, 0xe3, 0x33, 0x73, 0xb3, 0xf3,
0x07, 0x47, 0x87, 0xc7, 0x17, 0x57, 0x97, 0xd7,
0x27, 0x67, 0xa7, 0xe7, 0x37, 0x77, 0xb7, 0xf7,
0x0b, 0x4b, 0x8b, 0xcb, 0x1b, 0x5b, 0x9b, 0xdb,
0x2b, 0x6b, 0xab, 0xeb, 0x3b, 0x7b, 0xbb, 0xfb,
0x0f, 0x4f, 0x8f, 0xcf, 0x1f, 0x5f, 0x9f, 0xdf,
0x2f, 0x6f, 0xaf, 0xef, 0x3f, 0x7f, 0xbf, 0xff
};
// use: km = Kmer();
// pre:
// post: the DNA string in km is AA....AAA (k times A)
Kmer::Kmer() {
memset(bytes,0,MAX_K/4);
}
// use: _km = Kmer(km);
// pre: s[0],...,s[k] are all equal to 'A','C','G' or 'T'
// post: the DNA string in _km and is the same as in km
Kmer::Kmer(const Kmer& o) {
memcpy(bytes,o.bytes,MAX_K/4);
}
// use: km = Kmer(s);
// pre: s[0],...,s[k] are all equal to 'A','C','G' or 'T'
// post: the DNA string in km is now the same as s
Kmer::Kmer(const char *s) {
set_kmer(s);
}
// use: _km = km;
// pre:
// post: the DNA string in _km and is the same as in km
Kmer& Kmer::operator=(const Kmer& o) {
if (this != &o) {
memcpy(bytes, o.bytes, MAX_K/4);
}
return *this;
}
// use: km = Kmer();
// pre:
// post: The last bit in the bit array which stores the DNA string has been set to 1
// which indicates that the km is invalid
void Kmer::set_deleted() {
memset(bytes,0xff,MAX_K/4);
}
// use: b = (km1 < km2);
// pre:
// post: b is true <==> the DNA strings in km1 is alphabetically smaller than
// the DNA string in km2
bool Kmer::operator<(const Kmer& o) const {
for (size_t i = 0; i < k_bytes-1; ++i) {
if (base_swap[bytes[i]] > base_swap[o.bytes[i]]) {
return false;
} else if (base_swap[bytes[i]] < base_swap[o.bytes[i]]) {
return true;
}
}
if (base_swap[bytes[k_bytes-1] & k_modmask] >= base_swap[o.bytes[k_bytes-1] & k_modmask]) {
return false;
}
return true;
}
// use: b = (km1 == km2);
// pre:
// post: b is true <==> the DNA strings in km1 and km2 are equal
bool Kmer::operator==(const Kmer& o) const {
return memcmp(bytes,o.bytes,MAX_K/4)==0;
}
// use: km.set_kmer(s);
// pre: s[0],...,s[k-1] are all 'A','C','G' or 'T'
// post: The DNA string in km is now equal to s
void Kmer::set_kmer(const char *s) {
size_t i,j,l;
memset(bytes,0,MAX_K/4);
for (i = 0; i < k; ++i) {
j = i % 4;
l = i/4;
assert(*s != '\0');
switch(*s) {
case 'A': break;
case 'C': bytes[l] |= (0x01 << (2*j)); break;
case 'G': bytes[l] |= (0x02 << (2*j)); break;
case 'T': bytes[l] |= (0x03 << (2*j)); break;
}
s++;
}
}
// use: i = km.hash();
// pre:
// post: i is the hash value of km
uint64_t Kmer::hash() const {
uint64_t ret;
MurmurHash3_x64_64((const void *)bytes,k_bytes,0,&ret);
return ret;
}
// use: rep = km.rep();
// pre:
// post: rep is km.twin() if the DNA string in km.twin() is alphabetically smaller than
// the DNA string in km, else rep is km
Kmer Kmer::rep() const {
Kmer tw = twin();
return (tw < *this) ? tw : *this;
}
// use: tw = km.twin();
// pre:
// post: tw is the twin kmer with respect to km,
// i.e. if the DNA string in km is 'GTCA'
// then the DNA string in tw is 'TGAC'
Kmer Kmer::twin() const {
Kmer km(*this);
for (size_t i = 0; i < k_bytes; i++) {
km.bytes[i] = ~bytes[i];
}
km.bytes[k_bytes-1] ^= ~k_modmask;
km.shiftForward(8*k_bytes-2*k);
uint8_t tmp;
for (size_t i = 0; i < k_bytes/2; ++i) {
tmp = km.bytes[i];
km.bytes[i] = base_swap[km.bytes[k_bytes-1-i]];
km.bytes[k_bytes-1-i] = base_swap[tmp];
}
if ((k_bytes %2) == 1) {
km.bytes[k_bytes/2] = base_swap[km.bytes[k_bytes/2]];
}
return km;
}
// use: link = km.getLink(index);
// pre: 0 <= index < 8
// post: gives the forward kmer with the (index % 4) character in 'A','C','G' or 'T' if index < 4
// else the backward kmer with the (index % 4) character in 'A','C','G' or 'T'
Kmer Kmer::getLink(const size_t index) const {
assert(index >= 0 && index < 8);
char c;
switch (index % 4) {
case 0: c = 'A'; break;
case 1: c = 'C'; break;
case 2: c = 'G'; break;
case 3: c = 'T'; break;
}
return (index < 4) ? forwardBase(c) : backwardBase(c);
}
// use: fw = km.forwardBase(c)
// pre:
// post: fw is the forward kmer from km with last character c,
// i.e. if the DNA string in km is 'ACGT' and c equals 'T' then
// the DNA string in fw is 'CGTT'
Kmer Kmer::forwardBase(const char b) const {
int s = 2*((k+3) % 4);
Kmer km(*this);
km.shiftBackward(2);
km.bytes[k_bytes-1] &= Kmer::k_modmask;
switch(b) {
case 'A': km.bytes[k_bytes-1] |= 0x00 << s; break;
case 'C': km.bytes[k_bytes-1] |= 0x01 << s; break;
case 'G': km.bytes[k_bytes-1] |= 0x02 << s; break;
case 'T': km.bytes[k_bytes-1] |= 0x03 << s; break;
}
return km;
}
// use: bw = km.backwardBase(c)
// pre:
// post: bw is the backward kmer from km with first character c,
// i.e. if the DNA string in km is 'ACGT' and c equals 'T' then
// the DNA string in bw is 'TACG'
Kmer Kmer::backwardBase(const char b) const {
Kmer km(*this);
km.shiftForward(2);
km.bytes[k_bytes-1] &= Kmer::k_modmask;
if (k%4 == 0 and k_bytes < MAX_K/4) {
km.bytes[k_bytes] = 0x00;
}
switch(b) {
case 'A': km.bytes[0] |= 0x00; break;
case 'C': km.bytes[0] |= 0x01; break;
case 'G': km.bytes[0] |= 0x02; break;
case 'T': km.bytes[0] |= 0x03; break;
}
return km;
}
// use: km.printBinary();
// pre:
// post: The bits in the binary representation of the
// DNA string for km has been printed to stdout
void Kmer::printBinary() const {
char buff[9]; buff[8] = '\0';
printf("binary:");
for (size_t i = 0; i < Kmer::k_bytes; i++) {
int2bin(bytes[i],buff,8);
printf("%s",buff);
}
printf("\n");
}
// use: km.toString(s);
// pre: s has space for k+1 elements
// post: s[0,...,k-1] is the DNA string for the Kmer km and s[k] = '\0'
void Kmer::toString(char *s) const {
size_t i,j,l;
for (i = 0; i < k; i++) {
j = i % 4;
l = i / 4;
switch(((bytes[l]) >> (2*j) )& 0x03 ) {
case 0x00: *s = 'A'; ++s; break;
case 0x01: *s = 'C'; ++s; break;
case 0x02: *s = 'G'; ++s; break;
case 0x03: *s = 'T'; ++s; break;
}
}
*s = '\0';
}
std::string Kmer::toString() const {
char buf[MAX_K];
toString(buf);
return std::string(buf);
}
// use: km.shiftForward(i);
// pre: i = 2,4,6
// post: The DNA string in km has been shifted i/2 positions forward i.e.
// if i=2 then ACGT becomes XACG and X is A,C,G or T
void Kmer::shiftForward(int shift) {
if (shift>0) {
if (shift < 8 ) {
for (size_t i = Kmer::k_bytes-1; i > 0; i--) {
bytes[i] <<= shift;
bytes[i] |= (uint8_t) (bytes[i-1] >> (8-shift));
}
bytes[0] <<= shift;
} else {
assert(0); // we should never need this!
}
}
}
// use: km.shiftBackward(i);
// pre: i = 2,4,6
// post: The DNA string in km has been shifted i/2 positions backward i.e.
// if i=2 then ACGT becomes CGTX and X is A,C,G or T
void Kmer::shiftBackward(int shift) {
if (shift > 0) {
if (shift < 8) {
for (size_t i = 0; i < Kmer::k_bytes-1; i++) {
bytes[i] >>= shift;
bytes[i] |= (uint8_t) ( bytes[i+1] << (8-shift));
}
bytes[Kmer::k_bytes-1] >>= shift;
} else {
assert(0); // bad
}
}
}
// use: set_k(k);
// pre: this method has not been called before and 0 < k < MAX_K
// post: The Kmer size has been set to k
void Kmer::set_k(unsigned int _k) {
assert(_k < MAX_K);
assert(_k > 0);
assert(k_bytes == 0); // we can only call this once
k = _k;
k_bytes = (_k+3)/4;
// k_longs = (_k+15)/16;
k_modmask = (1 << (2*((k%4)?k%4:4)) )-1;
}
unsigned int Kmer::k = 0;
unsigned int Kmer::k_bytes = 0;
//unsigned int Kmer::k_longs = 0;
unsigned int Kmer::k_modmask = 0;