forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuccinct_dbg.h
195 lines (165 loc) · 5.64 KB
/
succinct_dbg.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
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
/*
* succinct_dbg.h
* This file is a part of 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 SUCCINCT_DBG_H_
#define SUCCINCT_DBG_H_
#include <assert.h>
#include <vector>
#include "rank_and_select.h"
using std::vector;
class SuccinctDBG {
public:
typedef uint16_t multi_t;
// constants
static const int kAlphabetSize = 4;
static const int kWAlphabetSize = 9;
static const int kWBitsPerChar = 4;
static const int kWCharsPerWord = sizeof(unsigned long long) * kBitsPerByte / kWBitsPerChar;
static const int kWCharMask = 0xF;
static const int kMaxKmerK = 128;
static const int kCharsPerUint32 = 16;
static const int kBitsPerChar = 2;
int64_t size;
int kmer_k;
public:
SuccinctDBG(): need_to_free_(false) { }
~SuccinctDBG() {
if (need_to_free_) {
free(last_);
free(w_);
free(invalid_);
free(is_dollar_);
free(dollar_node_seq_);
}
if (need_to_free_mul_) {
free(edge_multiplicities_);
}
}
void LoadFromFile(const char *dbg_name);
void init(unsigned long long *w, unsigned long long *last, long long *f, int64_t size, int kmer_k) {
w_ = w;
last_ = last;
this->size = size;
this->kmer_k = kmer_k;
for (int i = 0; i < kAlphabetSize + 2; ++i) {
f_[i] = f[i];
}
rs_w_.Build(w_, size);
rs_last_.Build(last_, size);
}
uint8_t GetW(int64_t x) {
return (*(w_ + x / kWCharsPerWord) >> (x % kWCharsPerWord * kWBitsPerChar)) & kWCharMask;
}
bool IsLast(int64_t x) {
return (*(last_ + x / 64) >> (x % 64)) & 1;
}
bool IsLastOrDollar(int64_t x) {
return ((last_[x / 64] | is_dollar_[x / 64]) >> (x % 64)) & 1;
}
int64_t GetLastIndex(int64_t x) {
return rs_last_.Succ(x);
}
uint8_t GetNodeLastChar(int64_t x) {
for (int i = 1; ; ++i) {
if (f_[i] > x) {
return i - 1;
}
}
}
bool IsValidNode(int64_t x) {
return !((*(invalid_ + x / 64) >> (x % 64)) & 1);
}
bool IsDollarNode(int64_t x) {
return (*(is_dollar_ + x / 64) >> (x % 64)) & 1;
}
void SetValid(int64_t x) {
x = rs_last_.Succ(x);
do {
__sync_fetch_and_and(invalid_ + x / 64, ~(1ULL << (x % 64)));
--x;
} while (x >= 0 && IsLastOrDollar(x) == 0);
}
void SetInvalid(int64_t x) {
x = rs_last_.Succ(x);
do {
__sync_fetch_and_or(invalid_ + x / 64, 1ULL << (x % 64));
--x;
} while (x >= 0 && IsLastOrDollar(x) == 0);
}
int EdgeMultiplicity(int64_t x) {
return edge_multiplicities_[x];
}
int NodeMultiplicity(int64_t x);
int64_t Forward(int64_t x) { // the last node x points to
uint8_t a = GetW(x);
if (a > 4) {
a -= 4;
}
int64_t count_a = rs_w_.Rank(a, x);
return rs_last_.Select(rs_last_.Rank(f_[a] - 1) + count_a - 1);
}
int64_t Backward(int64_t x) { // the first node points to x
uint8_t a = GetNodeLastChar(x);
int64_t count_a = rs_last_.Rank(x - 1) - rs_last_.Rank(f_[a] - 1);
return rs_w_.Select(a, count_a);
}
int Indegree(int64_t x);
int Outdegree(int64_t x);
int Incomings(int64_t x, int64_t *incomings);
int Outgoings(int64_t x, int64_t *outgoings);
int Incomings(int64_t x, int64_t *incomings, int *edge_countings);
int Outgoings(int64_t x, int64_t *outgoings, int *edge_countings);
bool IndegreeZero(int64_t x);
bool OutdegreeZero(int64_t x);
int64_t UniqueIncoming(int64_t x); // whether there is a unique valid node y s.t. y->x, return y if exist, else return -1
int64_t UniqueOutgoing(int64_t x); // whether there is a unique valid node y s.t. x->y, return y if exist, else return -1
int64_t Outgoing(uint8_t c, int64_t x); // not implement
int64_t Incoming(uint8_t c, int64_t x); // not implement
int64_t Index(uint8_t *seq);
int64_t IndexBinarySearch(uint8_t *seq);
int Label(int64_t x, uint8_t *seq);
int64_t ReverseComplement(int64_t x);
// WARNING: use this with cautions
// After that NodeMultiplicity() and EdgeMultiplicty() are invalid
void FreeMul() {
if (need_to_free_mul_) {
free(edge_multiplicities_);
}
need_to_free_mul_ = false;
}
private:
// main memory
unsigned long long *w_;
unsigned long long *last_;
unsigned long long *is_dollar_;
unsigned long long *invalid_;
uint32_t *dollar_node_seq_;
multi_t *edge_multiplicities_;
long long f_[kAlphabetSize + 2];
unsigned int num_dollar_nodes_;
int uint32_per_dollar_nodes_;
// auxiliary memory
RankAndSelect4Bits rs_w_;
RankAndSelect1Bit rs_last_;
RankAndSelect1Bit rs_is_dollar_;
bool need_to_free_;
bool need_to_free_mul_;
void PrefixRangeSearch_(uint8_t c, int64_t &l, int64_t &r);
};
#endif // SUCCINCT_DBG_H_