forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmer_uint32.h
155 lines (136 loc) · 5.25 KB
/
kmer_uint32.h
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
/*
* MEGAHIT
* Copyright (C) 2014 The University of Hong Kong
*
* 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/>.
*/
#ifndef KMER_UINT32_H_
#define KMER_UINT32_H_
#include "definitions.h"
#include "helper_functions-inl.h"
/**
* @brief a kmer struct for sdbg_builder (for efficiency). Not well OO.
*/
struct KmerUint32 {
static const int kNumWords = 6;
edge_word_t data_[kNumWords];
char kmer_k;
KmerUint32() {
clean();
}
KmerUint32(edge_word_t *read_ptr, int k) {
init(read_ptr, k);
}
void init(edge_word_t *read_ptr, int k) {
this->kmer_k = k;
int num_words = DivCeiling(k, kCharsPerEdgeWord);
int bits_to_clean = (kCharsPerEdgeWord - k % kCharsPerEdgeWord) * kBitsPerEdgeChar;
for (int i = 0; i < num_words; ++i) {
data_[i] = read_ptr[i];
}
if (bits_to_clean > 0) {
data_[num_words - 1] >>= bits_to_clean;
data_[num_words - 1] <<= bits_to_clean;
}
for (int i = num_words; i < kNumWords; ++i) {
data_[i] = 0;
}
}
void init(edge_word_t *read_ptr, int k, int spacing) {
this->kmer_k = k;
int num_words = DivCeiling(k, kCharsPerEdgeWord);
int bits_to_clean = (kCharsPerEdgeWord - k % kCharsPerEdgeWord) * kBitsPerEdgeChar;
for (int i = 0; i < num_words; ++i) {
data_[i] = read_ptr[i * spacing];
}
if (bits_to_clean != kBitsPerEdgeWord) {
data_[num_words - 1] >>= bits_to_clean;
data_[num_words - 1] <<= bits_to_clean;
}
for (int i = num_words; i < kNumWords; ++i) {
data_[i] = 0;
}
}
void clean() {
kmer_k = 0;
memset(data_, 0, sizeof(data_));
}
int cmp(const KmerUint32 &rhs) const {
if (kmer_k != rhs.kmer_k) return kmer_k - rhs.kmer_k;
for (int i = 0; i < kNumWords; ++i) {
if (data_[i] != rhs.data_[i]) {
return data_[i] < rhs.data_[i] ? -1 : 1;
}
}
return 0;
}
bool operator< (const KmerUint32 &rhs) const {
return cmp(rhs) < 0;
}
bool operator== (const KmerUint32 &rhs) const {
return cmp(rhs) == 0;
}
void ShiftLeft() {
for (int i = 0; i < kNumWords - 1; ++i) {
data_[i] = (data_[i] << kBitsPerEdgeChar) | (data_[i + 1] >> (kCharsPerEdgeWord - 1) * kBitsPerEdgeChar);
}
data_[kNumWords - 1] <<= kBitsPerEdgeChar;
kmer_k--;
}
void ShiftLeftAppend(int x) {
int num_words = DivCeiling(kmer_k, kCharsPerEdgeWord);
for (int i = 0; i < num_words - 1; ++i) {
data_[i] = (data_[i] << kBitsPerEdgeChar) | (data_[i + 1] >> (kCharsPerEdgeWord - 1) * kBitsPerEdgeChar);
}
data_[num_words - 1] <<= kBitsPerEdgeChar;
data_[num_words - 1] |= x << (kCharsPerEdgeWord - 1 - (kmer_k - 1) % kCharsPerEdgeWord) * kBitsPerEdgeChar;
}
void ShiftRightAppend(int x) {
for (int i = kNumWords - 1; i > 0; --i) {
data_[i] = (data_[i] >> kBitsPerEdgeChar) | ((data_[i - 1] & 3) << (kCharsPerEdgeWord - 1) * kBitsPerEdgeChar);
}
data_[0] = (data_[0] >> kBitsPerEdgeChar) | (x << (kCharsPerEdgeWord - 1) * kBitsPerEdgeChar);
// clean the last char
data_[kmer_k / kCharsPerEdgeWord] &= ~(3 << (kCharsPerEdgeWord - 1 - kmer_k % kCharsPerEdgeWord) * kBitsPerEdgeChar);
}
const KmerUint32 &ReverseComplement() {
int which_word = (kmer_k - 1) / kCharsPerEdgeWord;
int word_offset = (kmer_k - 1) % kCharsPerEdgeWord;
edge_word_t new_data[kNumWords];
if (word_offset == kCharsPerEdgeWord - 1) {
// aligned
for (int i = 0; i <= which_word; ++i) {
new_data[i] = ~ mirror(data_[which_word - i]);
}
} else {
// not aligned
int bit_offset = (kCharsPerEdgeWord - 1 - word_offset) * kBitsPerEdgeChar;
for (int i = 0; i < which_word; ++i) {
new_data[i] = (data_[which_word - i] >> bit_offset) | (data_[which_word - i - 1] << (kBitsPerEdgeWord - bit_offset));
new_data[i] = ~ mirror(new_data[i]);
}
new_data[which_word] = ~ mirror(data_[0] >> bit_offset);
}
init(new_data, kmer_k);
return *this;
}
void Append(int x) {
data_[kmer_k / kCharsPerEdgeWord] |= x << (kCharsPerEdgeWord - 1 - kmer_k % kCharsPerEdgeWord) * kBitsPerEdgeChar;
kmer_k++;
}
int operator[] (int i) {
return (data_[i / kCharsPerEdgeWord] >> (kCharsPerEdgeWord - 1 - i % kCharsPerEdgeWord) * kBitsPerEdgeChar) & 3;
}
};
#endif // KMER_UINT32_H_