UNPKG

@gmod/bbi

Version:

Parser for BigWig/BigBed files

68 lines 1.86 kB
"use strict"; /** * Adapted from a combination of Range and _Compound in the * Dalliance Genome Explorer, (c) Thomas Down 2006-2010. */ Object.defineProperty(exports, "__esModule", { value: true }); 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); } } exports.default = Range; //# sourceMappingURL=range.js.map