-
Notifications
You must be signed in to change notification settings - Fork 17
/
BedReader.h
209 lines (192 loc) · 6.1 KB
/
BedReader.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
#ifndef _BEDREADER_H_
#define _BEDREADER_H_
#include "IO.h"
#include "TypeConversion.h"
#include "StringUtil.h"
#include <map>
#include <string>
#include <vector>
#include <algorithm>
struct Region{
int beg; // inclusive
int end; // exclusive
std::string label;
};
struct RegionIndex{
int beg;
int end;
std::vector<int> overlap; // store index of regions that overlaps
};
bool RegionComparator(const Region& a, const Region& b) {
if (a.beg == b.beg)
return (a.end < b.end);
return (a.beg < b.beg);
};
bool RegionIndexComparator(const RegionIndex& a, const RegionIndex& b) {
if (a.beg == b.beg)
return (a.end < b.end);
return (a.beg < b.beg);
};
bool inRegion(const int pos, const Region& r) {
if ( r.beg <= pos && pos < r.end)
return true;
return false;
};
class BedReader {
public:
int open(const char* fn){
LineReader lr(fn);
std::string line;
std::vector<std::string> fd;
int totalRegion = 0;
Region region;
int& beg = region.beg;
int& end = region.end;
while (lr.readLine(&line)) {
stringNaturalTokenize(line, "\t ", &fd);
if (fd[0][0] == '#' || fd.size() < 3) // at least: chrom beg end [optional_label]
continue;
std::string& chrom = fd[0];
if (!str2int(fd[1].c_str(), &beg)){
fprintf(stderr, "Cannot convert [ %s ]\n", fd[1].c_str());
continue;
}
if (!str2int(fd[2].c_str(), &end)){
fprintf(stderr, "Cannot convert [ %s ]\n", fd[2].c_str());
continue;
}
if (fd.size() >= 4) // default label is empty.
region.label = fd[3];
this->data[chrom].push_back(region);
totalRegion ++;
};
this->createIndex();
return totalRegion;
};
/**
* @return >=1 if find; or 0 if not find.
* @param ret_label: store all non-empty labels
* NOTE: Here the BED boundary is defined as left-close, right-open.
*/
int find(const char* chrom, int pos,
std::vector<std::string>* ret_label) const {
int nFound = 0;
ret_label->clear();
ConstIndexIter iterChrom = this->index.find(chrom);
if (iterChrom == this->index.end()) {
return 0;
}
RegionIndex temp;
temp.beg = temp.end = pos;
ConstRegionIndexIter iter = std::lower_bound(iterChrom->second.begin(), iterChrom->second.end(), temp, RegionIndexComparator);
// iter always points to the region on the right of the region
// e.g. find 100, but points to (200, 300) region
// so we need to rewind to the left (subtract 1)
if (iter == iterChrom->second.begin())
return 0;
iter--;
// int idx;
// if (iter == iterChrom->second.end()) {
// idx = 0;
// } else {
// idx = iter - iterChrom->second.begin() - 1;
// };
// printf("check %d\n", iter - iterChrom->second.begin());
const std::vector<int>& index = iter->overlap;
ConstDataIter iterRegion = this->data.find(chrom);
for (size_t i = 0; i < index.size(); ++i ) {
const Region& r = (iterRegion->second)[index[i]];
if (inRegion(pos, r)) {
++ nFound;
// printf("i = %zu, pos = %d, beg = %d, end = %d\n", i, pos, r.beg, r.end);
if (!r.label.empty())
ret_label->push_back ( r.label);
}
}
return nFound;
};
void dump() {
puts("--------------------");
dumpData();
puts("--------------------");
dumpIndex();
};
void dumpData(){
for (ConstDataIter iter = this->data.begin();
iter != this->data.end();
++iter) {
// int idx = 0;
for (ConstRegionIter iter2 = iter->second.begin();
iter2 != iter->second.end();
++iter2) {
// fprintf(stderr, " [ %d ] %s:%d-%d -> %s \n", idx++, iter->first.c_str(), iter2->beg, iter2->end, iter2->label.c_str());
};
};
};
void dumpIndex(){
for (ConstIndexIter iter = this->index.begin();
iter != this->index.end();
++iter) {
int idx = 0;
for (ConstRegionIndexIter iter2 = iter->second.begin();
iter2 != iter->second.end();
++iter2) {
std::string overlap;
for (size_t i = 0; i < iter2->overlap.size(); ++i) {
overlap += toStr(iter2->overlap[i]);
overlap += ',';
}
fprintf(stderr, " [ %d ] %s:%d-%d -> %s \n", idx++, iter->first.c_str(), iter2->beg, iter2->end, overlap.c_str());
};
};
};
private:
// sort each region by region start position
// and create index
void createIndex() {
DataIter iter = this->data.begin();
for (; iter != this->data.end(); ++iter) {
// sort
std::vector<Region>& region = iter->second;
std::sort(region.begin(), region.end(), RegionComparator);
// create index
size_t n = region.size();
Region r = region[0];
RegionIndex ri;
ri.beg = r.beg;
ri.end = r.end;
ri.overlap.push_back(0);
for (size_t i = 1; i < n; ++i) {
// printf("%zu\t", i);
if (inRegion(region[i].beg, r)) {// merge region
r.end = std::max(r.end, region[i].end);
ri.overlap.push_back(i);
// printf("pushed in %zu\t", i);
} else { //
this->index[iter->first].push_back(ri);
r = region[i];
ri.beg = r.beg;
ri.end = r.end;
ri.overlap.clear();
ri.overlap.push_back(i);
// printf("skip %zu\t", i);
};
// printf("\n");
}
if (ri.overlap.size()){
this->index[iter->first].push_back(ri);
}
};
/* dumpData(); */
/* dumpIndex(); */
};
static const char SEPARATOR = ',';
std::map<std::string, std::vector< Region > > data;
std::map<std::string, std::vector< RegionIndex> > index;
typedef std::map<std::string, std::vector< Region > >::const_iterator ConstDataIter;
typedef std::map<std::string, std::vector< Region > >::iterator DataIter;
typedef std::map<std::string, std::vector< RegionIndex> >::const_iterator ConstIndexIter;
typedef std::vector< Region >::const_iterator ConstRegionIter;
typedef std::vector< RegionIndex >::const_iterator ConstRegionIndexIter;
}; // BedReader
#endif /* _BEDREADER_H_ */