-
Notifications
You must be signed in to change notification settings - Fork 26
/
genomeLoci.h
288 lines (261 loc) · 7.47 KB
/
genomeLoci.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
#ifndef __GENOME_LOCI_H
#define __GENOME_LOCI_H
#include <vector>
#include <set>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include "Error.h"
// A single genomic int32_terval
class genomeLocus {
public:
std::string chrom; // chromosome name
int32_t beg1; // includes 1-based, excludes 0-based
int32_t end0; // excludes 1-based, includes 0-based
char buf[255];
genomeLocus(const char* c, int32_t b, int32_t e) : chrom(c), beg1(b), end0(e) {
sprintf(buf,"%s:%d-%d",c,b,e);
}
// convert [chr]:[beg1]-[end0] string int32_to int32_terval
// 20:100-110 means [100,110] in 1-based [100,111) in 1-based [99,110) in 0-based
genomeLocus(const char* region) {
strcpy(buf,region);
const char* pcolon = strchr(region,':');
const char* pminus = strchr(pcolon+1,'-');
//if ( ( pcolon == NULL ) || ( pminus == NULL ) )
if ( pcolon == NULL )
error("Cannot parse %s in genomeLocus::genomeLocus()");
chrom = std::string(region,0,pcolon-region);
beg1 = atoi(pcolon+1);
if ( pminus == NULL ) end0 = INT_MAX;
else {
end0 = atoi(pminus+1);
if ( end0 == 0 ) end0 = INT_MAX;
}
}
const char* toString() const {
return buf;
}
// compare between genomeLocus
bool operator< (const genomeLocus& l) const {
if ( chrom == l.chrom ) {
if ( beg1 == l.beg1 ) {
return ( end0 < l.end0 );
}
else {
return ( beg1 < l.beg1 );
}
}
else {
int32_t n1 = atoi(chrom.c_str());
int32_t n2 = atoi(l.chrom.c_str());
if ( ( n1 == 0 ) && ( n2 == 0 ) ) {
return chrom < l.chrom;
}
else if ( ( n1 > 0 ) && ( n2 > 0 ) ) {
return n1 < n2;
}
else { // treat n1 == 0 as infinite
return ( n1 > 0 ) ? true : false;
}
}
}
// length
unsigned long length() const { return end0-beg1+1; }
bool overlaps(const char* _chrom, int32_t _beg1, int32_t _end0) const {
if ( chrom == _chrom ) {
if ( ( beg1 <= _end0 ) && ( _beg1 <= end0 ) ) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
// check overlap with other locus
bool overlaps (const genomeLocus& l) const {
if ( chrom == l.chrom ) {
if ( ( beg1 <= l.end0 ) && ( l.beg1 <= end0 ) ) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
// merge two locus if possible
bool merge (const genomeLocus& l) {
if ( chrom == l.chrom ) {
if ( ( beg1-1 <= l.end0 ) && ( l.beg1-1 <= end0 ) ) {
if ( l.beg1 < beg1 ) beg1 = l.beg1;
if ( l.end0 > end0 ) end0 = l.end0;
return true;
}
else {
return false;
}
}
else {
return false;
}
}
// check if it contains pos
bool contains0(const char* chr, int32_t pos0) const { return contains1(chr,pos0+1); }
bool contains1(const char* chr = NULL, int32_t pos1 = INT_MAX) const {
if ( ( chr == NULL ) || ( chrom == chr ) ) {
return ( ( pos1 >= beg1 ) && ( pos1 <= end0 ) );
}
else {
return false;
}
}
};
// Collection of genomic locus
class genomeLoci {
public:
std::set<genomeLocus> loci;
std::set<genomeLocus>::iterator it;
bool overlapResolved;
int32_t maxLength;
genomeLoci() : overlapResolved(false), maxLength(0) {}
genomeLoci(const char* reg) : overlapResolved(false), maxLength(0) {
add(reg);
resolveOverlaps();
}
// functions for iterating each locus
void rewind() { it = loci.begin(); }
bool next() { ++it; return ( it != loci.end() ); }
bool isend() { return ( it == loci.end() ); }
const genomeLocus& currentLocus() { return (*it); }
// check the size
bool empty() { return loci.empty(); }
int32_t numLocus() const { return (int32_t)loci.size(); }
// add a locus
bool add(const char* chr, int32_t beg1, int32_t end0) {
overlapResolved = false;
if ( end0-beg1+1 > maxLength ) maxLength = end0-beg1+1;
return loci.insert(genomeLocus(chr,beg1,end0)).second;
}
// add a locus
bool add(const char* region) {
overlapResolved = false;
std::pair<std::set<genomeLocus>::iterator, bool> ret = loci.insert(genomeLocus(region));
int32_t l = ret.first->end0 - ret.first->beg1 + 1;
if ( l > maxLength ) maxLength = l;
return ret.second;
}
// Resolve overlapping int32_tervals
int32_t resolveOverlaps() {
if ( !overlapResolved ) {
std::set<genomeLocus>::iterator it;
std::set<genomeLocus>::iterator prev;
int32_t numMerged = 0;
for(it = loci.begin(); it != loci.end(); ++it) {
if ( it != loci.begin() ) {
if ( prev->overlaps(*it) ) {
// if overlaps, erase both and insert merged one
genomeLocus locus = *prev;
locus.merge(*it);
if ( (int32_t)locus.length() > maxLength ) maxLength = locus.length();
loci.erase(it);
loci.erase(prev);
prev = it = loci.insert(locus).first;
++numMerged;
}
else {
prev = it;
}
}
else {
prev = it;
}
}
overlapResolved = true;
return numMerged;
}
else {
return 0;
}
return 0;
}
unsigned long totalLength() const {
//resolveOverlaps();
unsigned long sz = 0;
std::set<genomeLocus>::iterator it2;
for(it2 = loci.begin(); it2 != loci.end(); ++it2) {
sz += it2->length();
}
return sz;
}
bool moveTo(const char* chr = NULL, int32_t pos1 = INT_MAX) {
if ( it->contains1(chr, pos1) ) return true;
chr = it->chrom.c_str();
genomeLocus locus(chr, pos1, pos1);
it = loci.lower_bound(locus);
if ( it == loci.begin() ) { // do nothing
return (it->contains1(chr,pos1));
}
else if ( it == loci.end() ) {
std::set<genomeLocus>::iterator i = it;
--i;
if ( i->contains1(chr,pos1) ) { it = i; return true; }
else { return false; }
}
else {
if ( it->contains1(chr,pos1) ) return true;
else {
std::set<genomeLocus>::iterator i = it;
--i;
if ( i->contains1(chr,pos1) ) { it = i; return true; }
else { return false; }
}
}
}
bool contains1(const char* chr, int32_t pos1) {
genomeLocus locus(chr, pos1, pos1);
std::set<genomeLocus>::iterator it2 = loci.lower_bound(locus);
if ( it2 != loci.begin() ) --it2;
if ( it2->chrom != chr ) ++it2;
while( it2 != loci.end() && ( it2->chrom == chr ) && ( it2->beg1 <= pos1 ) ) {
if ( it2->end0 >= pos1 ) return true;
++it2;
}
return false;
}
bool overlaps(const char* chr, int32_t beg1, int32_t end0) {
genomeLocus locus(chr, overlapResolved ? beg1 : beg1-maxLength, overlapResolved ? beg1 : beg1-maxLength);
if ( loci.empty() ) return false;
std::set<genomeLocus>::iterator it2 = loci.lower_bound(locus);
if ( it2 != loci.begin() ) --it2;
if ( it2->chrom != chr ) ++it2;
while( it2 != loci.end() && ( it2->chrom == chr ) && ( it2->beg1 <= end0 ) ) {
if ( ( it2->beg1 <= end0 ) && ( beg1 <= it2->end0 ) )
return true;
++it2;
}
//notice("%s:%d-%d",it2->chrom.c_str(),it2->beg1,it2->end0);
return false;
}
bool contains(const char* chr, int32_t beg1, int32_t end0) {
if ( loci.empty() ) return false;
resolveOverlaps();
genomeLocus locus(chr, beg1-maxLength, beg1-maxLength);
std::set<genomeLocus>::iterator it2 = loci.lower_bound(locus);
if ( it2 != loci.begin() ) --it2;
if ( it2->chrom != chr ) ++it2;
while( it2 != loci.end() && ( it2->chrom == chr ) && ( it2->beg1 <= end0 ) ) {
if ( ( it2->beg1 <= beg1 ) && ( end0 <= it2->end0 ) )
return true;
++it2;
}
return false;
}
};
#endif