molstar
Version:
A comprehensive macromolecular library.
351 lines • 16.5 kB
JavaScript
"use strict";
/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.StructureHierarchyManager = void 0;
var tslib_1 = require("tslib");
var state_1 = require("../../../mol-plugin/behavior/static/state");
var commands_1 = require("../../../mol-plugin/commands");
var mol_state_1 = require("../../../mol-state");
var set_1 = require("../../../mol-util/set");
var component_1 = require("../../component");
var hierarchy_state_1 = require("./hierarchy-state");
var StructureHierarchyManager = /** @class */ (function (_super) {
(0, tslib_1.__extends)(StructureHierarchyManager, _super);
function StructureHierarchyManager(plugin) {
var _this = _super.call(this) || this;
_this.plugin = plugin;
_this.state = {
syncedTree: _this.dataState.tree,
notified: false,
hierarchy: (0, hierarchy_state_1.StructureHierarchy)(),
selection: {
trajectories: [],
models: [],
structures: []
}
};
_this.behaviors = {
selection: _this.ev.behavior({
hierarchy: _this.current,
trajectories: _this.selection.trajectories,
models: _this.selection.models,
structures: _this.selection.structures
})
};
_this._currentComponentGroups = void 0;
_this._currentSelectionSet = void 0;
_this.subscribe(plugin.state.data.events.changed, function (e) {
if (e.inTransaction || plugin.behaviors.state.isAnimating.value)
return;
_this.sync(true);
});
_this.subscribe(plugin.behaviors.state.isAnimating, function (isAnimating) {
if (!isAnimating && !plugin.behaviors.state.isUpdating.value)
_this.sync(true);
});
return _this;
}
Object.defineProperty(StructureHierarchyManager.prototype, "dataState", {
get: function () {
return this.plugin.state.data;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StructureHierarchyManager.prototype, "currentComponentGroups", {
get: function () {
if (this._currentComponentGroups)
return this._currentComponentGroups;
this._currentComponentGroups = StructureHierarchyManager.getComponentGroups(this.selection.structures);
return this._currentComponentGroups;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StructureHierarchyManager.prototype, "seletionSet", {
get: function () {
if (this._currentSelectionSet)
return this._currentSelectionSet;
this._currentSelectionSet = new Set();
for (var _i = 0, _a = this.selection.trajectories; _i < _a.length; _i++) {
var r = _a[_i];
this._currentSelectionSet.add(r.cell.transform.ref);
}
for (var _b = 0, _c = this.selection.models; _b < _c.length; _b++) {
var r = _c[_b];
this._currentSelectionSet.add(r.cell.transform.ref);
}
for (var _d = 0, _e = this.selection.structures; _d < _e.length; _d++) {
var r = _e[_d];
this._currentSelectionSet.add(r.cell.transform.ref);
}
return this._currentSelectionSet;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StructureHierarchyManager.prototype, "current", {
get: function () {
this.sync(false);
return this.state.hierarchy;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StructureHierarchyManager.prototype, "selection", {
get: function () {
this.sync(false);
return this.state.selection;
},
enumerable: false,
configurable: true
});
StructureHierarchyManager.prototype.getStructuresWithSelection = function () {
var xs = this.plugin.managers.structure.hierarchy.current.structures;
var ret = [];
for (var _i = 0, xs_1 = xs; _i < xs_1.length; _i++) {
var s = xs_1[_i];
if (this.plugin.managers.structure.selection.structureHasSelection(s)) {
ret.push(s);
}
}
return ret;
};
StructureHierarchyManager.prototype.syncCurrent = function (all, added) {
var current = this.seletionSet;
var newCurrent = [];
for (var _i = 0, all_1 = all; _i < all_1.length; _i++) {
var r = all_1[_i];
var ref = r.cell.transform.ref;
if (current.has(ref) || added.has(ref))
newCurrent.push(r);
}
if (newCurrent.length === 0)
return all.length > 0 ? [all[0]] : [];
return newCurrent;
};
StructureHierarchyManager.prototype.sync = function (notify) {
if (!notify && this.dataState.inUpdate)
return;
if (this.state.syncedTree === this.dataState.tree) {
if (notify && !this.state.notified) {
this.state.notified = true;
this.behaviors.selection.next((0, tslib_1.__assign)({ hierarchy: this.state.hierarchy }, this.state.selection));
}
return;
}
this.state.syncedTree = this.dataState.tree;
var update = (0, hierarchy_state_1.buildStructureHierarchy)(this.plugin.state.data, this.current);
if (!update.changed) {
return;
}
var hierarchy = update.hierarchy;
var trajectories = this.syncCurrent(hierarchy.trajectories, update.added);
var models = this.syncCurrent(hierarchy.models, update.added);
var structures = this.syncCurrent(hierarchy.structures, update.added);
this._currentComponentGroups = void 0;
this._currentSelectionSet = void 0;
this.state.hierarchy = hierarchy;
this.state.selection.trajectories = trajectories;
this.state.selection.models = models;
this.state.selection.structures = structures;
if (notify) {
this.state.notified = true;
this.behaviors.selection.next({ hierarchy: hierarchy, trajectories: trajectories, models: models, structures: structures });
}
else {
this.state.notified = false;
}
};
StructureHierarchyManager.prototype.updateCurrent = function (refs, action) {
var hierarchy = this.current;
var set = action === 'add'
? set_1.SetUtils.union(this.seletionSet, new Set(refs.map(function (r) { return r.cell.transform.ref; })))
: set_1.SetUtils.difference(this.seletionSet, new Set(refs.map(function (r) { return r.cell.transform.ref; })));
var trajectories = [];
var models = [];
var structures = [];
for (var _i = 0, _a = hierarchy.trajectories; _i < _a.length; _i++) {
var t = _a[_i];
if (set.has(t.cell.transform.ref))
trajectories.push(t);
}
for (var _b = 0, _c = hierarchy.models; _b < _c.length; _b++) {
var m = _c[_b];
if (set.has(m.cell.transform.ref))
models.push(m);
}
for (var _d = 0, _e = hierarchy.structures; _d < _e.length; _d++) {
var s = _e[_d];
if (set.has(s.cell.transform.ref))
structures.push(s);
}
this._currentComponentGroups = void 0;
this._currentSelectionSet = void 0;
this.state.selection.trajectories = trajectories;
this.state.selection.models = models;
this.state.selection.structures = structures;
this.behaviors.selection.next({ hierarchy: hierarchy, trajectories: trajectories, models: models, structures: structures });
};
StructureHierarchyManager.prototype.remove = function (refs, canUndo) {
if (refs.length === 0)
return;
var deletes = this.plugin.state.data.build();
for (var _i = 0, refs_1 = refs; _i < refs_1.length; _i++) {
var r = refs_1[_i];
deletes.delete(typeof r === 'string' ? r : r.cell.transform.ref);
}
return deletes.commit({ canUndo: canUndo ? 'Remove' : false });
};
StructureHierarchyManager.prototype.toggleVisibility = function (refs, action) {
if (refs.length === 0)
return;
var isHidden = action !== void 0
? (action === 'show' ? false : true)
: !refs[0].cell.state.isHidden;
for (var _i = 0, refs_2 = refs; _i < refs_2.length; _i++) {
var c = refs_2[_i];
(0, state_1.setSubtreeVisibility)(this.dataState, c.cell.transform.ref, isHidden);
}
};
StructureHierarchyManager.prototype.applyPreset = function (trajectories, provider, params) {
var _this = this;
return this.plugin.dataTransaction(function () { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
var _i, trajectories_1, t;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
_i = 0, trajectories_1 = trajectories;
_a.label = 1;
case 1:
if (!(_i < trajectories_1.length)) return [3 /*break*/, 6];
t = trajectories_1[_i];
if (!(t.models.length > 0)) return [3 /*break*/, 3];
return [4 /*yield*/, this.clearTrajectory(t)];
case 2:
_a.sent();
_a.label = 3;
case 3: return [4 /*yield*/, this.plugin.builders.structure.hierarchy.applyPreset(t.cell, provider, params)];
case 4:
_a.sent();
_a.label = 5;
case 5:
_i++;
return [3 /*break*/, 1];
case 6: return [2 /*return*/];
}
});
}); });
};
StructureHierarchyManager.prototype.updateStructure = function (s, params) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var _this = this;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.plugin.dataTransaction(function () { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
var root, children;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
root = mol_state_1.StateTree.getDecoratorRoot(this.dataState.tree, s.cell.transform.ref);
children = this.dataState.tree.children.get(root).toArray();
return [4 /*yield*/, this.remove(children, false)];
case 1:
_a.sent();
return [4 /*yield*/, this.plugin.state.updateTransform(this.plugin.state.data, s.cell.transform.ref, params, 'Structure Type')];
case 2:
_a.sent();
return [4 /*yield*/, this.plugin.builders.structure.representation.applyPreset(s.cell.transform.ref, 'auto')];
case 3:
_a.sent();
return [2 /*return*/];
}
});
}); }, { canUndo: 'Structure Type' })];
case 1:
_a.sent();
commands_1.PluginCommands.Camera.Reset(this.plugin);
return [2 /*return*/];
}
});
});
};
StructureHierarchyManager.prototype.clearTrajectory = function (trajectory) {
var builder = this.dataState.build();
for (var _i = 0, _a = trajectory.models; _i < _a.length; _i++) {
var m = _a[_i];
builder.delete(m.cell);
}
return builder.commit();
};
return StructureHierarchyManager;
}(component_1.PluginComponent));
exports.StructureHierarchyManager = StructureHierarchyManager;
(function (StructureHierarchyManager) {
function getComponentGroups(structures) {
if (!structures.length)
return [];
if (structures.length === 1)
return structures[0].components.map(function (c) { return [c]; });
var groups = [];
var map = new Map();
for (var _i = 0, structures_1 = structures; _i < structures_1.length; _i++) {
var s = structures_1[_i];
for (var _a = 0, _b = s.components; _a < _b.length; _a++) {
var c = _b[_a];
var key = c.key;
if (!key)
continue;
var component = map.get(key);
if (!component) {
component = [];
map.set(key, component);
groups.push(component);
}
component.push(c);
}
}
return groups;
}
StructureHierarchyManager.getComponentGroups = getComponentGroups;
function getSelectedStructuresDescription(plugin) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
var structures = plugin.managers.structure.hierarchy.selection.structures;
if (structures.length === 0)
return '';
if (structures.length === 1) {
var s = structures[0];
var data = (_a = s.cell.obj) === null || _a === void 0 ? void 0 : _a.data;
if (!data)
return ((_b = s.cell.obj) === null || _b === void 0 ? void 0 : _b.label) || 'Structure';
var model = data.models[0] || data.representativeModel || data.masterModel;
if (!model)
return ((_c = s.cell.obj) === null || _c === void 0 ? void 0 : _c.label) || 'Structure';
var entryId = model.entryId;
if (((_e = (_d = s.model) === null || _d === void 0 ? void 0 : _d.trajectory) === null || _e === void 0 ? void 0 : _e.models) && s.model.trajectory.models.length === 1)
return entryId;
if (s.model)
return ((_f = s.model.cell.obj) === null || _f === void 0 ? void 0 : _f.label) + " | " + entryId;
return entryId;
}
var p = structures[0];
var t = (_g = p === null || p === void 0 ? void 0 : p.model) === null || _g === void 0 ? void 0 : _g.trajectory;
var sameTraj = true;
for (var _i = 0, structures_2 = structures; _i < structures_2.length; _i++) {
var s = structures_2[_i];
if (((_h = s === null || s === void 0 ? void 0 : s.model) === null || _h === void 0 ? void 0 : _h.trajectory) !== t) {
sameTraj = false;
break;
}
}
return sameTraj && t ? ((_j = t.cell.obj) === null || _j === void 0 ? void 0 : _j.label) + " | " + structures.length + " structures" : structures.length + " structures";
}
StructureHierarchyManager.getSelectedStructuresDescription = getSelectedStructuresDescription;
})(StructureHierarchyManager = exports.StructureHierarchyManager || (exports.StructureHierarchyManager = {}));
exports.StructureHierarchyManager = StructureHierarchyManager;
//# sourceMappingURL=hierarchy.js.map