@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
72 lines (71 loc) • 2.04 kB
JavaScript
import Flatbush from "../flatbush/index.js";
export default class PrecomputedLayout {
rectangles;
totalHeight;
maxHeightReached;
index;
indexData = [];
constructor({ rectangles, totalHeight, maxHeightReached }) {
this.rectangles = new Map(Object.entries(rectangles));
this.totalHeight = totalHeight;
this.maxHeightReached = maxHeightReached;
this.buildIndex();
}
buildIndex() {
if (this.rectangles.size === 0) {
return;
}
this.index = new Flatbush(this.rectangles.size);
this.indexData = [];
for (const [name, rect] of this.rectangles) {
this.index.add(rect[0], rect[1], rect[2], rect[3]);
this.indexData.push({ name, rect });
}
this.index.finish();
}
addRect(id) {
const rect = this.rectangles.get(id);
if (!rect) {
throw new Error(`id ${id} not found in precomputed feature layout`);
}
return rect[1];
}
getRectangles() {
return this.rectangles;
}
getTotalHeight() {
return this.totalHeight;
}
collides(_rect, _top) {
throw new Error('Method not implemented.');
}
getByCoord(x, y) {
if (!this.index) {
return undefined;
}
const results = this.index.search(x, y, x + 1, y + 1);
if (results.length > 0) {
return this.indexData[results[0]]?.name;
}
return undefined;
}
getByID(id) {
return this.rectangles.get(id);
}
addRectToBitmap(_rect, _data) {
throw new Error('Method not implemented.');
}
discardRange(_left, _right) {
throw new Error('Method not implemented.');
}
serializeRegion(_region) {
throw new Error('Method not implemented.');
}
toJSON() {
return {
rectangles: Object.fromEntries(this.rectangles),
totalHeight: this.totalHeight,
maxHeightReached: false,
};
}
}