UNPKG

@rcsb/rcsb-saguaro-3d

Version:
158 lines 6.87 kB
/* * Copyright (c) 2021 RCSB PDB and contributors, licensed under MIT, See LICENSE file for more info. * @author Joan Segura Mora <joan.segura@rcsb.org> */ import { __awaiter } from "tslib"; import { asyncScheduler } from "rxjs"; export class AssemblyBehaviourObserver { constructor(structureLoader) { this.structureLoader = structureLoader; } observe(structureViewer, stateManager) { this.structureBehaviour = new AssemblyBehaviour(structureViewer, stateManager); this.structureLoader.load(structureViewer).then(() => { console.info("Assembly load complete"); }); } unsubscribe() { this.structureBehaviour.unsubscribe(); } } class AssemblyBehaviour { constructor(structureViewer, stateManager) { this.CREATE_COMPONENT_THR = 3; this.structureViewer = structureViewer; this.stateManager = stateManager; this.subscription = this.subscribe(); } modelChange() { throw new Error("Method not implemented."); } subscribe() { return this.stateManager.subscribe((o) => __awaiter(this, void 0, void 0, function* () { if (o.type == "selection-change" && o.view == "1d-view") this.selectionChange(); if (o.type == "hover-change" && o.view == "1d-view") this.hoverChange(); if (o.type == "feature-click" && o.view == "1d-view") yield this.featureClick(); if (o.type == "selection-change" && o.view == "3d-view") yield this.isSelectionEmpty(); if (o.type == "pfv-change" && o.view == "1d-view") this.resetPluginView(); if (o.type == "visibility-change" && o.view == "1d-view" && o.data) this.visibilityChange(o.data); if (o.type == "component-info" && o.view == "1d-view" && o.data) this.componentInfo(o.data); })); } selectionChange() { this.select("select", "set"); } hoverChange() { this.select("hover", "set"); } featureClick() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; yield this.removeComponent(); const { modelId, labelAsymId, operatorName, regions } = (_a = this.stateManager.selectionState.getLastSelection()) !== null && _a !== void 0 ? _a : {}; if (modelId && labelAsymId && operatorName && Array.isArray(regions) && regions.length > 0) { const residues = regions.map(r => r.begin == r.end ? [r.begin] : [r.begin, r.end]).flat().filter(r => r != null); if (residues.length == 0) return; const ranges = regions.map(r => ({ modelId, labelAsymId, operatorName, begin: r.begin, end: r.end })); this.structureViewer.cameraFocus(modelId, labelAsymId, residues, operatorName); const nRes = ranges.map(r => r.end - r.begin + 1).reduce((prev, curr) => curr + prev, 0); if (nRes <= this.CREATE_COMPONENT_THR) asyncScheduler.schedule(() => __awaiter(this, void 0, void 0, function* () { const x = residues[0]; const y = residues[residues.length - 1]; this.selectedComponentId = labelAsymId + ":" + ((x === y) ? x.toString() : x.toString() + "," + y.toString()); yield this.structureViewer.createComponent(this.selectedComponentId, ranges, "ball-and-stick"); if (nRes == 1) this.structureViewer.setFocus(modelId, labelAsymId, residues[0], residues[0], operatorName); })); else this.selectedComponentId = undefined; } else { const modelId = this.stateManager.assemblyModelSate.getString("modelId"); const labelAsymId = this.stateManager.assemblyModelSate.getString("labelAsymId"); const operatorName = (_b = this.stateManager.assemblyModelSate.getOperator()) === null || _b === void 0 ? void 0 : _b.name; this.structureViewer.clearSelection("select", { modelId, labelAsymId, operatorName }); this.resetPluginView(); } }); } reprChange() { } unsubscribe() { this.subscription.unsubscribe(); } select(mode, operator) { var _a; const modelId = this.stateManager.assemblyModelSate.getString("modelId"); const labelAsymId = this.stateManager.assemblyModelSate.getString("labelAsymId"); const operatorName = (_a = this.stateManager.assemblyModelSate.getOperator()) === null || _a === void 0 ? void 0 : _a.name; const selection = this.stateManager.selectionState.getSelectionWithCondition(modelId, labelAsymId, mode, operatorName); if (operator == "set") this.structureViewer.clearSelection(mode, { modelId, labelAsymId, operatorName }); if (selection && Array.isArray(selection.regions) && selection.regions.length > 0) { this.structureViewer.select(selection.regions.map(r => ({ modelId, labelAsymId, operatorName, begin: r.begin, end: r.end })), mode, "add"); } else { if (mode == "select") this.resetPluginView(); } } isSelectionEmpty() { return __awaiter(this, void 0, void 0, function* () { if (this.stateManager.selectionState.getLastSelection() == null) { yield this.removeComponent(); this.resetPluginView(); } }); } removeComponent() { return __awaiter(this, void 0, void 0, function* () { if (typeof this.selectedComponentId === "string") yield this.structureViewer.removeComponent(this.selectedComponentId); }); } resetPluginView() { this.structureViewer.clearFocus(); this.structureViewer.resetCamera(); } visibilityChange(data) { if (data.display === 'visible') { this.structureViewer.displayComponent(data.label, true); } else { this.structureViewer.displayComponent(data.label, false); } } componentInfo(data) { this.stateManager.next({ type: "component-info", view: "3d-view", data: { label: data.label, display: this.structureViewer.displayComponent(data.label) ? "visible" : "hidden" } }); } } //# sourceMappingURL=AssemblyBehaviour.js.map