@gmod/bbi
Version:
Parser for BigWig/BigBed files
65 lines • 1.77 kB
JavaScript
/**
* Adapted from a combination of Range and _Compound in the
* Dalliance Genome Explorer, (c) Thomas Down 2006-2010.
*/
export default class Range {
constructor(arg1) {
this.ranges = arg1;
}
get min() {
return this.ranges[0].min;
}
get max() {
return this.ranges.at(-1).max;
}
contains(pos) {
for (const r of this.ranges) {
if (r.min <= pos && r.max >= pos) {
return true;
}
}
return false;
}
isContiguous() {
return this.ranges.length > 1;
}
getRanges() {
return this.ranges.map(r => new Range([{ min: r.min, max: r.max }]));
}
toString() {
return this.ranges.map(r => `[${r.min}-${r.max}]`).join(',');
}
union(s1) {
const ranges = [...this.getRanges(), ...s1.getRanges()].sort((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;
}
});
const oranges = [];
let current = ranges[0];
for (const nxt of ranges) {
if (nxt.min > current.max + 1) {
oranges.push(current);
current = nxt;
}
else if (nxt.max > current.max) {
current = new Range([{ min: current.min, max: nxt.max }]);
}
}
oranges.push(current);
return oranges.length === 1 ? oranges[0] : new Range(oranges);
}
}
//# sourceMappingURL=range.js.map