forked from BenLangmead/bowtie2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ival_list.h
299 lines (265 loc) · 6.54 KB
/
ival_list.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
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
/*
* Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 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.
*
* Bowtie 2 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 Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IVAL_LIST_H_
#define IVAL_LIST_H_
#include "ds.h"
#include "ref_coord.h"
#include <utility>
/**
* Encapsulates the "union" of a collection of intervals. Intervals are stored
* in a sorted list. Intervals can be added but not removed. Supports just
* one type of query for now: locusPresent().
*/
class EIvalMergeList {
public:
static const size_t DEFAULT_UNSORTED_SZ = 16;
explicit EIvalMergeList(int cat = 0) :
sorted_(cat),
sortedLhs_(cat),
unsorted_(cat),
unsortedSz_(DEFAULT_UNSORTED_SZ)
{ }
explicit EIvalMergeList(size_t unsortedSz, int cat = 0) :
sorted_(cat),
sortedLhs_(cat),
unsorted_(cat),
unsortedSz_(unsortedSz)
{ }
/**
* Set the maximum size of the unsorted list.
*/
void setUnsortedSize(size_t usz) {
unsortedSz_ = usz;
}
/**
* Add a new interval to the list.
*/
void add(const Interval& i) {
assert_leq(unsorted_.size(), unsortedSz_);
if(unsorted_.size() < unsortedSz_) {
unsorted_.push_back(i);
}
if(unsorted_.size() == unsortedSz_) {
flush();
}
}
/**
* Move all unsorted interval information into the sorted list and re-sort.
* Merge overlapping intervals.
*/
void flush() {
for(size_t i = 0; i < unsorted_.size(); i++) {
sorted_.push_back(unsorted_[i]);
}
sorted_.sort();
merge();
sortedLhs_.clear();
for(size_t i = 0; i < sorted_.size(); i++) {
sortedLhs_.push_back(sorted_[i].upstream());
}
assert(sortedLhs_.sorted());
unsorted_.clear();
}
#ifndef NDEBUG
/**
* Check that this interval list is internally consistent.
*/
bool repOk() const {
assert_eq(sorted_.size(), sortedLhs_.size());
return true;
}
#endif
/**
* Remove all ranges from the list.
*/
void reset() { clear(); }
/**
* Remove all ranges from the list.
*/
void clear() {
sorted_.clear();
sortedLhs_.clear();
unsorted_.clear();
}
/**
* Return true iff this locus is present in one of the intervals in the
* list.
*/
bool locusPresent(const Coord& loc) const {
return locusPresentUnsorted(loc) || locusPresentSorted(loc);
}
/**
* Return the number of intervals added since the last call to reset() or
* clear().
*/
size_t size() const {
return sorted_.size() + unsorted_.size();
}
/**
* Return true iff list is empty.
*/
bool empty() const {
return sorted_.empty() && unsorted_.empty();
}
protected:
/**
* Go through the sorted interval list and merge adjacent entries that
* overlap.
*/
void merge() {
size_t nmerged = 0;
for(size_t i = 1; i < sorted_.size(); i++) {
if(sorted_[i-1].downstream() >= sorted_[i].upstream()) {
nmerged++;
assert_leq(sorted_[i-1].upstream(), sorted_[i].upstream());
Coord up = std::min(sorted_[i-1].upstream(), sorted_[i].upstream());
Coord dn = std::max(sorted_[i-1].downstream(), sorted_[i].downstream());
sorted_[i].setUpstream(up);
sorted_[i].setLength(dn.off() - up.off());
sorted_[i-1].reset();
}
}
sorted_.sort();
assert_lt(nmerged, sorted_.size());
sorted_.resize(sorted_.size()-nmerged);
#ifndef NDEBUG
for(size_t i = 0; i < sorted_.size(); i++) {
assert(sorted_[i].inited());
}
#endif
}
/**
* Return true iff the given locus is present in one of the intervals in
* the sorted list.
*/
bool locusPresentSorted(const Coord& loc) const {
assert(repOk());
if(sorted_.empty()) {
return false;
}
size_t beg = sortedLhs_.bsearchLoBound(loc);
if(beg == sortedLhs_.size() || sortedLhs_[beg] > loc) {
// Check element before
if(beg == 0) {
return false;
}
return sorted_[beg-1].contains(loc);
} else {
assert_eq(loc, sortedLhs_[beg]);
return true;
}
}
/**
* Return true iff the given locus is present in one of the intervals in
* the unsorted list.
*/
bool locusPresentUnsorted(const Coord& loc) const {
for(size_t i = 0; i < unsorted_.size(); i++) {
if(unsorted_[i].contains(loc)) {
return true;
}
}
return false;
}
EList<Interval> sorted_; // LHS, RHS sorted
EList<Coord> sortedLhs_; // LHS, index into sorted_, sorted
EList<Interval> unsorted_; // unsorted
size_t unsortedSz_; // max allowed size of unsorted_
};
/**
* Binned version of the above. We bin using the low bits of the reference
* sequence.
*/
class EIvalMergeListBinned {
public:
static const size_t NBIN = 7;
explicit EIvalMergeListBinned(int cat = 0) : bins_(1 << NBIN, cat) {
bins_.resize(1 << NBIN);
}
explicit EIvalMergeListBinned(
size_t unsortedSz,
int cat = 0) : bins_(1 << NBIN, cat)
{
bins_.resize(1 << NBIN);
for(size_t i = 0; i < (1 << NBIN); i++) {
bins_[i].setUnsortedSize(unsortedSz);
}
}
/**
* Add a new interval to the list.
*/
void add(const Interval& i) {
size_t bin = i.ref() & ~(0xffffffff << NBIN);
assert_lt(bin, bins_.size());
bins_[bin].add(i);
}
#ifndef NDEBUG
/**
* Check that this interval list is internally consistent.
*/
bool repOk() const {
for(size_t i = 0; i < bins_.size(); i++) {
assert(bins_[i].repOk());
}
return true;
}
#endif
/**
* Remove all ranges from the list.
*/
void reset() { clear(); }
/**
* Remove all ranges from the list.
*/
void clear() {
for(size_t i = 0; i < bins_.size(); i++) {
bins_[i].clear();
}
}
/**
* Return true iff this locus is present in one of the intervals in the
* list.
*/
bool locusPresent(const Coord& loc) const {
size_t bin = loc.ref() & ~(0xffffffff << NBIN);
assert_lt(bin, bins_.size());
return bins_[bin].locusPresent(loc);
}
/**
* Return the number of intervals added since the last call to reset() or
* clear().
*/
size_t size() const {
// TODO: Keep track of size
size_t sz = 0;
for(size_t i = 0; i < bins_.size(); i++) {
sz += bins_[i].size();
}
return sz;
}
/**
* Return true iff list is empty.
*/
bool empty() const {
return size() == 0;
}
protected:
EList<EIvalMergeList> bins_;
};
#endif /*ndef IVAL_LIST_H_*/