drugflow-molstar
Version:
Molstar implementation for DrugFlow
174 lines (173 loc) • 10.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SuperpositionFocusRepresentation = exports.SuperpositionFocusRepresentationTags = void 0;
var tslib_1 = require("tslib");
var structure_1 = require("Molstar/mol-model/structure");
var structure_representation_params_1 = require("Molstar/mol-plugin-state/helpers/structure-representation-params");
var transforms_1 = require("Molstar/mol-plugin-state/transforms");
var behavior_1 = require("Molstar/mol-plugin/behavior");
var builder_1 = require("Molstar/mol-script/language/builder");
var mol_state_1 = require("Molstar/mol-state");
var param_definition_1 = require("Molstar/mol-util/param-definition");
var commands_1 = require("Molstar/mol-plugin/commands");
var loci_details_1 = require("./loci-details");
var SuperpositionFocusRepresentationParams = function (plugin) {
var reprParams = transforms_1.StateTransforms.Representation.StructureRepresentation3D.definition.params(void 0, plugin);
return {
expandRadius: param_definition_1.ParamDefinition.Numeric(5, { min: 1, max: 10, step: 1 }),
surroundingsParams: param_definition_1.ParamDefinition.Group(reprParams, {
label: 'Surroundings',
customDefault: (0, structure_representation_params_1.createStructureRepresentationParams)(plugin, void 0, { type: 'ball-and-stick', size: 'physical', typeParams: { sizeFactor: 0.16 }, sizeParams: { scale: 0.3 } })
})
};
};
var SuperpositionFocusRepresentationTags;
(function (SuperpositionFocusRepresentationTags) {
SuperpositionFocusRepresentationTags["SurrSel"] = "superposition-focus-surr-sel";
SuperpositionFocusRepresentationTags["SurrRepr"] = "superposition-focus-surr-repr";
})(SuperpositionFocusRepresentationTags || (exports.SuperpositionFocusRepresentationTags = SuperpositionFocusRepresentationTags = {}));
var TagSet = new Set([SuperpositionFocusRepresentationTags.SurrSel, SuperpositionFocusRepresentationTags.SurrRepr]);
var SuperpositionFocusRepresentationBehavior = /** @class */ (function (_super) {
tslib_1.__extends(SuperpositionFocusRepresentationBehavior, _super);
function SuperpositionFocusRepresentationBehavior() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(SuperpositionFocusRepresentationBehavior.prototype, "surrLabel", {
get: function () { return "[Focus] Surroundings (".concat(this.params.expandRadius, " \u00C5)"); },
enumerable: false,
configurable: true
});
SuperpositionFocusRepresentationBehavior.prototype.ensureShape = function (cell) {
var state = this.plugin.state.data, tree = state.tree;
var builder = state.build();
var refs = mol_state_1.StateSelection.findUniqueTagsInSubtree(tree, cell.transform.ref, TagSet);
// Selections
if (!refs[SuperpositionFocusRepresentationTags.SurrSel]) {
refs[SuperpositionFocusRepresentationTags.SurrSel] = builder
.to(cell)
.apply(transforms_1.StateTransforms.Model.StructureSelectionFromExpression, { expression: builder_1.MolScriptBuilder.struct.generator.empty(), label: this.surrLabel }, { tags: SuperpositionFocusRepresentationTags.SurrSel }).ref;
}
// Representations
if (!refs[SuperpositionFocusRepresentationTags.SurrRepr]) {
refs[SuperpositionFocusRepresentationTags.SurrRepr] = builder
.to(refs[SuperpositionFocusRepresentationTags.SurrSel])
.apply(transforms_1.StateTransforms.Representation.StructureRepresentation3D, this.params.surroundingsParams, { tags: SuperpositionFocusRepresentationTags.SurrRepr }).ref;
}
return { state: state, builder: builder, refs: refs };
};
SuperpositionFocusRepresentationBehavior.prototype.clear = function (root) {
var e_1, _a;
var state = this.plugin.state.data;
var surrs = state.select(mol_state_1.StateSelection.Generators.byRef(root).subtree().withTag(SuperpositionFocusRepresentationTags.SurrSel));
if (surrs.length === 0)
return;
var update = state.build();
var expression = builder_1.MolScriptBuilder.struct.generator.empty();
try {
for (var surrs_1 = tslib_1.__values(surrs), surrs_1_1 = surrs_1.next(); !surrs_1_1.done; surrs_1_1 = surrs_1.next()) {
var s = surrs_1_1.value;
update.to(s).update(transforms_1.StateTransforms.Model.StructureSelectionFromExpression, function (old) { return (tslib_1.__assign(tslib_1.__assign({}, old), { expression: expression })); });
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (surrs_1_1 && !surrs_1_1.done && (_a = surrs_1.return)) _a.call(surrs_1);
}
finally { if (e_1) throw e_1.error; }
}
return commands_1.PluginCommands.State.Update(this.plugin, { state: state, tree: update, options: { doNotLogTiming: true, doNotUpdateCurrent: true } });
};
SuperpositionFocusRepresentationBehavior.prototype.focus = function (sourceLoci) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var parent, loci, residueLoci, residueBundle, target, surroundings, lociDeatils, _a, state, builder, refs;
var _this = this;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
parent = this.plugin.helpers.substructureParent.get(sourceLoci.structure);
if (!parent || !parent.obj)
return [2 /*return*/];
loci = structure_1.StructureElement.Loci.remap(sourceLoci, parent.obj.data);
residueLoci = structure_1.StructureElement.Loci.extendToWholeResidues(loci);
residueBundle = structure_1.StructureElement.Bundle.fromLoci(residueLoci);
target = structure_1.StructureElement.Bundle.toExpression(residueBundle);
surroundings = builder_1.MolScriptBuilder.struct.modifier.includeSurroundings({
0: target,
radius: this.params.expandRadius,
'as-whole-residues': true
});
lociDeatils = (0, loci_details_1.lociDetails)(sourceLoci);
if (!lociDeatils) {
surroundings = builder_1.MolScriptBuilder.struct.modifier.exceptBy({
0: surroundings,
by: target
});
}
_a = this.ensureShape(parent), state = _a.state, builder = _a.builder, refs = _a.refs;
builder.to(refs[SuperpositionFocusRepresentationTags.SurrSel]).update(transforms_1.StateTransforms.Model.StructureSelectionFromExpression, function (old) { return (tslib_1.__assign(tslib_1.__assign({}, old), { expression: surroundings, label: _this.surrLabel })); });
return [4 /*yield*/, commands_1.PluginCommands.State.Update(this.plugin, { state: state, tree: builder, options: { doNotLogTiming: true, doNotUpdateCurrent: true } })];
case 1:
_b.sent();
return [2 /*return*/];
}
});
});
};
SuperpositionFocusRepresentationBehavior.prototype.register = function (ref) {
var _this = this;
this.subscribeObservable(this.plugin.managers.structure.focus.behaviors.current, function (entry) {
// if (entry) this.focus(entry.loci);
// else this.clear(StateTransform.RootRef);
_this.clear(mol_state_1.StateTransform.RootRef);
if (entry)
_this.focus(entry.loci);
});
};
SuperpositionFocusRepresentationBehavior.prototype.update = function (params) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var old, state, builder, all, _a, _b, repr;
var e_2, _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
old = this.params;
this.params = params;
state = this.plugin.state.data;
builder = state.build();
all = mol_state_1.StateSelection.Generators.root.subtree();
try {
for (_a = tslib_1.__values(state.select(all.withTag(SuperpositionFocusRepresentationTags.SurrRepr))), _b = _a.next(); !_b.done; _b = _a.next()) {
repr = _b.value;
builder.to(repr).update(this.params.surroundingsParams);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_2) throw e_2.error; }
}
return [4 /*yield*/, commands_1.PluginCommands.State.Update(this.plugin, { state: state, tree: builder, options: { doNotLogTiming: true, doNotUpdateCurrent: true } })];
case 1:
_d.sent();
if (!(params.expandRadius !== old.expandRadius)) return [3 /*break*/, 3];
return [4 /*yield*/, this.clear(mol_state_1.StateTransform.RootRef)];
case 2:
_d.sent();
_d.label = 3;
case 3: return [2 /*return*/, true];
}
});
});
};
return SuperpositionFocusRepresentationBehavior;
}(behavior_1.PluginBehavior.WithSubscribers));
exports.SuperpositionFocusRepresentation = behavior_1.PluginBehavior.create({
name: 'create-superposition-focus-representation',
display: { name: 'Superposition Focus Representation' },
category: 'interaction',
ctor: SuperpositionFocusRepresentationBehavior,
params: function (_, plugin) { return SuperpositionFocusRepresentationParams(plugin); }
});