molstar
Version:
A comprehensive macromolecular library.
478 lines • 28 kB
JavaScript
/**
* Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __assign, __awaiter, __generator } from "tslib";
import * as ReactDOM from 'react-dom';
import { DefaultCanvas3DParams } from '../../mol-canvas3d/canvas3d';
import { AnimateModelIndex } from '../../mol-plugin-state/animation/built-in/model-index';
import { createStructureRepresentationParams } from '../../mol-plugin-state/helpers/structure-representation-params';
import { StateTransforms } from '../../mol-plugin-state/transforms';
import { createPlugin } from '../../mol-plugin-ui';
import { DefaultPluginUISpec } from '../../mol-plugin-ui/spec';
import { CreateVolumeStreamingInfo, InitVolumeStreaming } from '../../mol-plugin/behavior/dynamic/volume-streaming/transformers';
import { PluginCommands } from '../../mol-plugin/commands';
import { MolScriptBuilder as MS } from '../../mol-script/language/builder';
import { StateSelection } from '../../mol-state';
import { Asset } from '../../mol-util/assets';
import { Color } from '../../mol-util/color';
import { ColorNames } from '../../mol-util/color/names';
import { getFormattedTime } from '../../mol-util/date';
import { download } from '../../mol-util/download';
import { RxEventHelper } from '../../mol-util/rx-event-helper';
import { EvolutionaryConservation } from './annotation';
import { createProteopediaCustomTheme } from './coloring';
import { ModelInfo, StateElements } from './helpers';
import './index.html';
import { volumeStreamingControls } from './ui/controls';
require('../../mol-plugin-ui/skin/light.scss');
var MolStarProteopediaWrapper = /** @class */ (function () {
function MolStarProteopediaWrapper() {
var _this = this;
this._ev = RxEventHelper.create();
this.events = {
modelInfo: this._ev()
};
this.emptyLoadedParams = { url: '', format: 'cif', isBinary: false, assemblyId: '' };
this.loadedParams = { url: '', format: 'cif', isBinary: false, assemblyId: '' };
this.viewport = {
setSettings: function (settings) {
PluginCommands.Canvas3D.SetSettings(_this.plugin, {
settings: settings || DefaultCanvas3DParams
});
}
};
this.camera = {
toggleSpin: function () { return _this.toggleSpin(); },
resetPosition: function () { return PluginCommands.Camera.Reset(_this.plugin, {}); }
};
this.animate = {
modelIndex: {
targetFps: 8,
onceForward: function () { _this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: _this.animateModelIndexTargetFps() } }, mode: { name: 'once', params: { direction: 'forward' } } }); },
onceBackward: function () { _this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: _this.animateModelIndexTargetFps() } }, mode: { name: 'once', params: { direction: 'backward' } } }); },
palindrome: function () { _this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: _this.animateModelIndexTargetFps() } }, mode: { name: 'palindrome', params: {} } }); },
loop: function () { _this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: _this.animateModelIndexTargetFps() } }, mode: { name: 'loop', params: { direction: 'forward' } } }); },
stop: function () { return _this.plugin.managers.animation.stop(); }
}
};
this.coloring = {
evolutionaryConservation: function (params) { return __awaiter(_this, void 0, void 0, function () {
var state, tree, colorTheme;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(!params || !params.keepStyle)) return [3 /*break*/, 2];
return [4 /*yield*/, this.updateStyle({ sequence: { kind: 'spacefill' } }, true)];
case 1:
_a.sent();
_a.label = 2;
case 2:
state = this.state;
tree = state.build();
colorTheme = { name: EvolutionaryConservation.propertyProvider.descriptor.name, params: this.plugin.representation.structure.themes.colorThemeRegistry.get(EvolutionaryConservation.propertyProvider.descriptor.name).defaultValues };
if (!params || !!params.sequence) {
tree.to(StateElements.SequenceVisual).update(StateTransforms.Representation.StructureRepresentation3D, function (old) { return (__assign(__assign({}, old), { colorTheme: colorTheme })); });
}
if (params && !!params.het) {
tree.to(StateElements.HetVisual).update(StateTransforms.Representation.StructureRepresentation3D, function (old) { return (__assign(__assign({}, old), { colorTheme: colorTheme })); });
}
return [4 /*yield*/, PluginCommands.State.Update(this.plugin, { state: state, tree: tree })];
case 3:
_a.sent();
return [2 /*return*/];
}
});
}); }
};
this.experimentalDataElement = void 0;
this.experimentalData = {
init: function (parent) { return __awaiter(_this, void 0, void 0, function () {
var asm, params;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
asm = this.state.select(StateElements.Assembly)[0].obj;
params = InitVolumeStreaming.createDefaultParams(asm, this.plugin);
params.options.behaviorRef = StateElements.VolumeStreaming;
params.defaultView = 'box';
params.options.channelParams['fo-fc(+ve)'] = { wireframe: true };
params.options.channelParams['fo-fc(-ve)'] = { wireframe: true };
return [4 /*yield*/, this.plugin.runTask(this.state.applyAction(InitVolumeStreaming, params, StateElements.Assembly))];
case 1:
_a.sent();
this.experimentalDataElement = parent;
volumeStreamingControls(this.plugin, parent);
return [2 /*return*/];
}
});
}); },
remove: function () {
var r = _this.state.select(StateSelection.Generators.ofTransformer(CreateVolumeStreamingInfo))[0];
if (!r)
return;
PluginCommands.State.RemoveObject(_this.plugin, { state: _this.state, ref: r.transform.ref });
if (_this.experimentalDataElement) {
ReactDOM.unmountComponentAtNode(_this.experimentalDataElement);
_this.experimentalDataElement = void 0;
}
}
};
this.hetGroups = {
reset: function () {
var update = _this.state.build().delete(StateElements.HetGroupFocusGroup);
PluginCommands.State.Update(_this.plugin, { state: _this.state, tree: update });
PluginCommands.Camera.Reset(_this.plugin, {});
},
focusFirst: function (compId, options) { return __awaiter(_this, void 0, void 0, function () {
var update, core, surroundings, group, asm, coreSel, waters, exclude, onlySurroundings, focus, sphere, radius, snapshot;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.state.transforms.has(StateElements.Assembly))
return [2 /*return*/];
return [4 /*yield*/, PluginCommands.Camera.Reset(this.plugin, {})];
case 1:
_a.sent();
update = this.state.build();
update.delete(StateElements.HetGroupFocusGroup);
core = MS.struct.filter.first([
MS.struct.generator.atomGroups({
'residue-test': MS.core.rel.eq([MS.struct.atomProperty.macromolecular.label_comp_id(), compId]),
'group-by': MS.core.str.concat([MS.struct.atomProperty.core.operatorName(), MS.struct.atomProperty.macromolecular.residueKey()])
})
]);
surroundings = MS.struct.modifier.includeSurroundings({ 0: core, radius: 5, 'as-whole-residues': true });
group = update.to(StateElements.Assembly).group(StateTransforms.Misc.CreateGroup, { label: compId }, { ref: StateElements.HetGroupFocusGroup });
asm = this.state.select(StateElements.Assembly)[0].obj;
coreSel = group.apply(StateTransforms.Model.StructureSelectionFromExpression, { label: 'Core', expression: core }, { ref: StateElements.HetGroupFocus });
coreSel.apply(StateTransforms.Representation.StructureRepresentation3D, createStructureRepresentationParams(this.plugin, asm.data, {
type: 'ball-and-stick'
}));
coreSel.apply(StateTransforms.Representation.StructureRepresentation3D, createStructureRepresentationParams(this.plugin, asm.data, {
type: 'label',
typeParams: { level: 'element' }
}), { tags: ['proteopedia-labels'] });
group.apply(StateTransforms.Model.StructureSelectionFromExpression, { label: 'Surroundings', expression: surroundings })
.apply(StateTransforms.Representation.StructureRepresentation3D, createStructureRepresentationParams(this.plugin, asm.data, {
type: 'ball-and-stick',
color: 'uniform', colorParams: { value: ColorNames.gray },
size: 'uniform', sizeParams: { value: 0.33 }
}));
if (!(options === null || options === void 0 ? void 0 : options.hideLabels)) {
waters = MS.struct.generator.atomGroups({
'entity-test': MS.core.rel.eq([MS.struct.atomProperty.macromolecular.entityType(), 'water']),
});
exclude = (options === null || options === void 0 ? void 0 : options.doNotLabelWaters) ? MS.struct.combinator.merge([core, waters]) : core;
onlySurroundings = MS.struct.modifier.exceptBy({ 0: surroundings, by: exclude });
group.apply(StateTransforms.Model.StructureSelectionFromExpression, { label: 'Surroundings (only)', expression: onlySurroundings })
.apply(StateTransforms.Representation.StructureRepresentation3D, createStructureRepresentationParams(this.plugin, asm.data, {
type: 'label',
typeParams: { level: 'residue' }
}), { tags: ['proteopedia-labels'] }); // the tag can later be used to toggle the labels
}
return [4 /*yield*/, PluginCommands.State.Update(this.plugin, { state: this.state, tree: update })];
case 2:
_a.sent();
focus = this.state.select(StateElements.HetGroupFocus)[0].obj.data;
sphere = focus.boundary.sphere;
radius = Math.max(sphere.radius, 5);
snapshot = this.plugin.canvas3d.camera.getFocus(sphere.center, radius);
PluginCommands.Camera.SetSnapshot(this.plugin, { snapshot: snapshot, durationMs: 250 });
return [2 /*return*/];
}
});
}); }
};
this.snapshot = {
get: function (params) {
return _this.plugin.state.getSnapshot(params);
},
set: function (snapshot) {
return _this.plugin.state.setSnapshot(snapshot);
},
download: function (type, params) {
if (type === void 0) { type = 'molj'; }
return __awaiter(_this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.plugin.managers.snapshot.serialize({ type: type, params: params })];
case 1:
data = _a.sent();
download(data, "mol-star_state_" + getFormattedTime() + "." + type);
return [2 /*return*/];
}
});
});
},
fetch: function (url, type) {
if (type === void 0) { type = 'molj'; }
return __awaiter(_this, void 0, void 0, function () {
var data, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
return [4 /*yield*/, this.plugin.runTask(this.plugin.fetch({ url: url, type: 'binary' }))];
case 1:
data = _a.sent();
this.loadedParams = __assign({}, this.emptyLoadedParams);
return [4 /*yield*/, this.plugin.managers.snapshot.open(new File([data], "state." + type))];
case 2: return [2 /*return*/, _a.sent()];
case 3:
e_1 = _a.sent();
console.log(e_1);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
}
};
}
MolStarProteopediaWrapper.prototype.init = function (target, options) {
this.plugin = createPlugin(typeof target === 'string' ? document.getElementById(target) : target, __assign(__assign({}, DefaultPluginUISpec()), { animations: [
AnimateModelIndex
], layout: {
initial: {
isExpanded: false,
showControls: false
}
}, components: {
remoteState: 'none'
} }));
var customColoring = createProteopediaCustomTheme((options && options.customColorList) || []);
this.plugin.representation.structure.themes.colorThemeRegistry.add(customColoring);
this.plugin.representation.structure.themes.colorThemeRegistry.add(EvolutionaryConservation.colorThemeProvider);
this.plugin.managers.lociLabels.addProvider(EvolutionaryConservation.labelProvider);
this.plugin.customModelProperties.register(EvolutionaryConservation.propertyProvider, true);
};
Object.defineProperty(MolStarProteopediaWrapper.prototype, "state", {
get: function () {
return this.plugin.state.data;
},
enumerable: false,
configurable: true
});
MolStarProteopediaWrapper.prototype.download = function (b, url, isBinary) {
return b.apply(StateTransforms.Data.Download, { url: Asset.Url(url), isBinary: isBinary });
};
MolStarProteopediaWrapper.prototype.model = function (b, format) {
var parsed = format === 'cif'
? b.apply(StateTransforms.Data.ParseCif).apply(StateTransforms.Model.TrajectoryFromMmCif)
: b.apply(StateTransforms.Model.TrajectoryFromPDB);
return parsed
.apply(StateTransforms.Model.ModelFromTrajectory, { modelIndex: 0 }, { ref: StateElements.Model });
};
MolStarProteopediaWrapper.prototype.structure = function (assemblyId) {
var model = this.state.build().to(StateElements.Model);
var props = {
type: assemblyId ? {
name: 'assembly',
params: { id: assemblyId }
} : {
name: 'model',
params: {}
}
};
var s = model
.apply(StateTransforms.Model.StructureFromModel, props, { ref: StateElements.Assembly });
s.apply(StateTransforms.Model.StructureComplexElement, { type: 'atomic-sequence' }, { ref: StateElements.Sequence });
s.apply(StateTransforms.Model.StructureComplexElement, { type: 'atomic-het' }, { ref: StateElements.Het });
s.apply(StateTransforms.Model.StructureComplexElement, { type: 'water' }, { ref: StateElements.Water });
return s;
};
MolStarProteopediaWrapper.prototype.visual = function (_style, partial) {
var structure = this.getObj(StateElements.Assembly);
if (!structure)
return;
var style = _style || {};
var update = this.state.build();
if (!partial || (partial && style.sequence)) {
var root = update.to(StateElements.Sequence);
if (style.sequence && style.sequence.hide) {
root.delete(StateElements.SequenceVisual);
}
else {
root.applyOrUpdate(StateElements.SequenceVisual, StateTransforms.Representation.StructureRepresentation3D, createStructureRepresentationParams(this.plugin, structure, {
type: (style.sequence && style.sequence.kind) || 'cartoon',
color: (style.sequence && style.sequence.coloring) || 'unit-index'
}));
}
}
if (!partial || (partial && style.hetGroups)) {
var root = update.to(StateElements.Het);
if (style.hetGroups && style.hetGroups.hide) {
root.delete(StateElements.HetVisual);
}
else {
if (style.hetGroups && style.hetGroups.hide) {
root.delete(StateElements.HetVisual);
}
else {
root.applyOrUpdate(StateElements.HetVisual, StateTransforms.Representation.StructureRepresentation3D, createStructureRepresentationParams(this.plugin, structure, {
type: (style.hetGroups && style.hetGroups.kind) || 'ball-and-stick',
color: style.hetGroups && style.hetGroups.coloring
}));
}
}
}
if (!partial || (partial && style.snfg3d)) {
var root = update.to(StateElements.Het);
if (style.hetGroups && style.hetGroups.hide) {
root.delete(StateElements.HetVisual);
}
else {
if (style.snfg3d && style.snfg3d.hide) {
root.delete(StateElements.Het3DSNFG);
}
else {
root.applyOrUpdate(StateElements.Het3DSNFG, StateTransforms.Representation.StructureRepresentation3D, createStructureRepresentationParams(this.plugin, structure, { type: 'carbohydrate' }));
}
}
}
if (!partial || (partial && style.water)) {
var root = update.to(StateElements.Water);
if (style.water && style.water.hide) {
root.delete(StateElements.WaterVisual);
}
else {
root.applyOrUpdate(StateElements.WaterVisual, StateTransforms.Representation.StructureRepresentation3D, createStructureRepresentationParams(this.plugin, structure, {
type: (style.water && style.water.kind) || 'ball-and-stick',
typeParams: { alpha: 0.51 },
color: style.water && style.water.coloring
}));
}
}
return update;
};
MolStarProteopediaWrapper.prototype.getObj = function (ref) {
var state = this.state;
var cell = state.select(ref)[0];
if (!cell || !cell.obj)
return void 0;
return cell.obj.data;
};
MolStarProteopediaWrapper.prototype.doInfo = function (checkPreferredAssembly) {
return __awaiter(this, void 0, void 0, function () {
var model, info;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
model = this.getObj('model');
if (!model)
return [2 /*return*/];
return [4 /*yield*/, ModelInfo.get(this.plugin, model, checkPreferredAssembly)];
case 1:
info = _a.sent();
this.events.modelInfo.next(info);
return [2 /*return*/, info];
}
});
});
};
MolStarProteopediaWrapper.prototype.applyState = function (tree) {
return PluginCommands.State.Update(this.plugin, { state: this.plugin.state.data, tree: tree });
};
MolStarProteopediaWrapper.prototype.load = function (_a) {
var url = _a.url, _b = _a.format, format = _b === void 0 ? 'cif' : _b, _c = _a.assemblyId, assemblyId = _c === void 0 ? '' : _c, _d = _a.isBinary, isBinary = _d === void 0 ? false : _d, representationStyle = _a.representationStyle;
return __awaiter(this, void 0, void 0, function () {
var loadType, state, modelTree, info, asmId, structureTree, tree, info, asmId, props_1;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
loadType = 'full';
state = this.plugin.state.data;
if (this.loadedParams.url !== url || this.loadedParams.format !== format) {
loadType = 'full';
}
else if (this.loadedParams.url === url) {
if (state.select(StateElements.Assembly).length > 0)
loadType = 'update';
}
if (!(loadType === 'full')) return [3 /*break*/, 5];
return [4 /*yield*/, PluginCommands.State.RemoveObject(this.plugin, { state: state, ref: state.tree.root.ref })];
case 1:
_e.sent();
modelTree = this.model(this.download(state.build().toRoot(), url, isBinary), format);
return [4 /*yield*/, this.applyState(modelTree)];
case 2:
_e.sent();
return [4 /*yield*/, this.doInfo(true)];
case 3:
info = _e.sent();
asmId = (assemblyId === 'preferred' && info && info.preferredAssemblyId) || assemblyId;
structureTree = this.structure(asmId);
return [4 /*yield*/, this.applyState(structureTree)];
case 4:
_e.sent();
return [3 /*break*/, 8];
case 5:
tree = state.build();
return [4 /*yield*/, this.doInfo(true)];
case 6:
info = _e.sent();
asmId = (assemblyId === 'preferred' && info && info.preferredAssemblyId) || assemblyId;
props_1 = {
type: assemblyId ? {
name: 'assembly',
params: { id: asmId }
} : {
name: 'model',
params: {}
}
};
tree.to(StateElements.Assembly).update(StateTransforms.Model.StructureFromModel, function (p) { return (__assign(__assign({}, p), props_1)); });
return [4 /*yield*/, this.applyState(tree)];
case 7:
_e.sent();
_e.label = 8;
case 8: return [4 /*yield*/, this.updateStyle(representationStyle)];
case 9:
_e.sent();
this.loadedParams = { url: url, format: format, assemblyId: assemblyId };
return [2 /*return*/];
}
});
});
};
MolStarProteopediaWrapper.prototype.updateStyle = function (style, partial) {
return __awaiter(this, void 0, void 0, function () {
var tree;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
tree = this.visual(style, partial);
if (!tree)
return [2 /*return*/];
return [4 /*yield*/, PluginCommands.State.Update(this.plugin, { state: this.plugin.state.data, tree: tree })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
MolStarProteopediaWrapper.prototype.setBackground = function (color) {
if (!this.plugin.canvas3d)
return;
var renderer = this.plugin.canvas3d.props.renderer;
PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { renderer: __assign(__assign({}, renderer), { backgroundColor: Color(color) }) } });
};
MolStarProteopediaWrapper.prototype.toggleSpin = function () {
if (!this.plugin.canvas3d)
return;
var trackball = this.plugin.canvas3d.props.trackball;
PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { trackball: __assign(__assign({}, trackball), { spin: !trackball.spin }) } });
};
MolStarProteopediaWrapper.prototype.animateModelIndexTargetFps = function () {
return Math.max(1, this.animate.modelIndex.targetFps | 0);
};
MolStarProteopediaWrapper.VERSION_MAJOR = 5;
MolStarProteopediaWrapper.VERSION_MINOR = 5;
return MolStarProteopediaWrapper;
}());
window.MolStarProteopediaWrapper = MolStarProteopediaWrapper;
//# sourceMappingURL=index.js.map