forked from remimarenco/bbi-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspans.js
257 lines (220 loc) · 5.5 KB
/
spans.js
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
/* -*- mode: javascript; c-basic-offset: 4; indent-tabs-mode: nil -*- */
//
// Dalliance Genome Explorer
// (c) Thomas Down 2006-2010
//
// spans.js: JavaScript Intset/Location port.
//
define([], function() {
"use strict";
function Range(min, max)
{
if (typeof(min) != 'number' || typeof(max) != 'number')
throw 'Bad range ' + min + ',' + max;
this._min = min;
this._max = max;
}
Range.prototype.min = function() {
return this._min;
}
Range.prototype.max = function() {
return this._max;
}
Range.prototype.contains = function(pos) {
return pos >= this._min && pos <= this._max;
}
Range.prototype.isContiguous = function() {
return true;
}
Range.prototype.ranges = function() {
return [this];
}
Range.prototype._pushRanges = function(ranges) {
ranges.push(this);
}
Range.prototype.toString = function() {
return '[' + this._min + '-' + this._max + ']';
}
function _Compound(ranges) {
// given: a set of unsorted possibly overlapping ranges
// sort the input ranges
var sorted = ranges.sort(_rangeOrder);
// merge overlaps between adjacent ranges
var merged = [];
var current = sorted.shift();
sorted.forEach(function(range) {
if (range._min <= current._max) {
if (range._max > current._max) {
current._max = range._max;
}
}
else {
merged.push(current);
current = range;
}
});
merged.push(current);
this._ranges = merged;
}
_Compound.prototype.min = function() {
return this._ranges[0].min();
}
_Compound.prototype.max = function() {
return this._ranges[this._ranges.length - 1].max();
}
// returns the index of the first range that is not less than pos
_Compound.prototype.lower_bound = function(pos) {
// first check if pos is out of range
var r = this.ranges();
if (pos > this.max()) return r.length;
if (pos < this.min()) return 0;
// do a binary search
var a=0, b=r.length - 1;
while (a <= b) {
var m = Math.floor((a+b)/2);
if (pos > r[m]._max) {
a = m+1;
}
else if (pos < r[m]._min) {
b = m-1;
}
else {
return m;
}
}
return a;
}
_Compound.prototype.contains = function(pos) {
var lb = this.lower_bound(pos);
if (lb < this._ranges.length && this._ranges[lb].contains(pos)) {
return true;
}
return false;
}
_Compound.prototype.insertRange = function(range) {
var lb = this.lower_bound(range._min);
if (lb === this._ranges.length) { // range follows this
this._ranges.push(range);
return;
}
var r = this.ranges();
if (range._max < r[lb]._min) { // range preceeds lb
this._ranges.splice(lb,0,range);
return;
}
// range overlaps lb (at least)
if (r[lb]._min < range._min) range._min = r[lb]._min;
var ub = lb+1;
while (ub < r.length && r[ub]._min <= range._max) {
ub++;
}
ub--;
// ub is the upper bound of the new range
if (r[ub]._max > range._max) range._max = r[ub]._max;
// splice range into this._ranges
this._ranges.splice(lb,ub-lb+1,range);
return;
}
_Compound.prototype.isContiguous = function() {
return this._ranges.length > 1;
}
_Compound.prototype.ranges = function() {
return this._ranges;
}
_Compound.prototype._pushRanges = function(ranges) {
for (var ri = 0; ri < this._ranges.length; ++ri)
ranges.push(this._ranges[ri]);
}
_Compound.prototype.toString = function() {
var s = '';
for (var r = 0; r < this._ranges.length; ++r) {
if (r>0) {
s = s + ',';
}
s = s + this._ranges[r].toString();
}
return s;
}
function union(s0, s1) {
if (! (s0 instanceof _Compound)) {
if (! (s0 instanceof Array))
s0 = [s0];
s0 = new _Compound(s0);
}
if (s1)
s0.insertRange(s1);
return s0;
}
function intersection(s0, s1) {
var r0 = s0.ranges();
var r1 = s1.ranges();
var l0 = r0.length, l1 = r1.length;
var i0 = 0, i1 = 0;
var or = [];
while (i0 < l0 && i1 < l1) {
var s0 = r0[i0], s1 = r1[i1];
var lapMin = Math.max(s0.min(), s1.min());
var lapMax = Math.min(s0.max(), s1.max());
if (lapMax >= lapMin) {
or.push(new Range(lapMin, lapMax));
}
if (s0.max() > s1.max()) {
++i1;
} else {
++i0;
}
}
if (or.length == 0) {
return null; // FIXME
} else if (or.length == 1) {
return or[0];
} else {
return new _Compound(or);
}
}
function coverage(s) {
var tot = 0;
var rl = s.ranges();
for (var ri = 0; ri < rl.length; ++ri) {
var r = rl[ri];
tot += (r.max() - r.min() + 1);
}
return tot;
}
function rangeOrder(a, b)
{
if (a.min() < b.min()) {
return -1;
} else if (a.min() > b.min()) {
return 1;
} else if (a.max() < b.max()) {
return -1;
} else if (b.max() > a.max()) {
return 1;
} else {
return 0;
}
}
function _rangeOrder(a, b)
{
if (a._min < b._min) {
return -1;
} else if (a._min > b._min) {
return 1;
} else if (a._max < b._max) {
return -1;
} else if (b._max > a._max) {
return 1;
} else {
return 0;
}
}
return {
Range: Range,
union: union,
intersection: intersection,
coverage: coverage,
rangeOver: rangeOrder,
_rangeOrder: _rangeOrder
};
});