molstar
Version:
A comprehensive macromolecular library.
341 lines (340 loc) • 15.1 kB
JavaScript
/**
* Copyright (c) 2018-2026 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>
* @author Yakov Pechersky <ffxen158@gmail.com>
*/
import { CIF } from '../../mol-io/reader/cif.js';
import { Mat4, Vec3 } from '../../mol-math/linear-algebra.js';
import { volumeFromCcp4 } from '../../mol-model-formats/volume/ccp4.js';
import { volumeFromDensityServerData } from '../../mol-model-formats/volume/density-server.js';
import { volumeFromDsn6 } from '../../mol-model-formats/volume/dsn6.js';
import { Task } from '../../mol-task/index.js';
import { ParamDefinition as PD } from '../../mol-util/param-definition.js';
import { PluginStateObject as SO, PluginStateTransform } from '../objects.js';
import { volumeFromCube } from '../../mol-model-formats/volume/cube.js';
import { volumeFromDx } from '../../mol-model-formats/volume/dx.js';
import { Grid } from '../../mol-model/volume.js';
import { StateSelection, StateTransformer } from '../../mol-state/index.js';
import { volumeFromSegmentationData } from '../../mol-model-formats/volume/segmentation.js';
import { getTransformFromParams, TransformParam, transformParamsNeedCentroid } from './helpers.js';
export { VolumeFromCcp4 };
export { VolumeFromDsn6 };
export { VolumeFromCube };
export { VolumeFromDx };
export { AssignColorVolume };
export { VolumeFromDensityServerCif };
export { VolumeFromSegmentationCif };
export { VolumeTransform };
export { VolumeInstances };
export { CustomVolumeProperties };
const VolumeFromCcp4 = PluginStateTransform.BuiltIn({
name: 'volume-from-ccp4',
display: { name: 'Volume from CCP4/MRC/MAP', description: 'Create Volume from CCP4/MRC/MAP data' },
from: SO.Format.Ccp4,
to: SO.Volume.Data,
params(a) {
return {
voxelSize: PD.Vec3(Vec3.create(1, 1, 1)),
offset: PD.Vec3(Vec3.create(0, 0, 0)),
entryId: PD.Text(''),
};
}
})({
apply({ a, params }) {
return Task.create('Create volume from CCP4/MRC/MAP', async (ctx) => {
const volume = await volumeFromCcp4(a.data, { ...params, label: a.data.name || a.label }).runInContext(ctx);
const props = { label: volume.label || 'Volume', description: `Volume ${a.data.header.NX}\u00D7${a.data.header.NX}\u00D7${a.data.header.NX}` };
return new SO.Volume.Data(volume, props);
});
},
dispose({ b }) {
b === null || b === void 0 ? void 0 : b.data.customProperties.dispose();
}
});
const VolumeFromDsn6 = PluginStateTransform.BuiltIn({
name: 'volume-from-dsn6',
display: { name: 'Volume from DSN6/BRIX', description: 'Create Volume from DSN6/BRIX data' },
from: SO.Format.Dsn6,
to: SO.Volume.Data,
params(a) {
return {
voxelSize: PD.Vec3(Vec3.create(1, 1, 1)),
entryId: PD.Text(''),
};
}
})({
apply({ a, params }) {
return Task.create('Create volume from DSN6/BRIX', async (ctx) => {
const volume = await volumeFromDsn6(a.data, { ...params, label: a.data.name || a.label }).runInContext(ctx);
const props = { label: volume.label || 'Volume', description: `Volume ${a.data.header.xExtent}\u00D7${a.data.header.yExtent}\u00D7${a.data.header.zExtent}` };
return new SO.Volume.Data(volume, props);
});
},
dispose({ b }) {
b === null || b === void 0 ? void 0 : b.data.customProperties.dispose();
}
});
const VolumeFromCube = PluginStateTransform.BuiltIn({
name: 'volume-from-cube',
display: { name: 'Volume from Cube', description: 'Create Volume from Cube data' },
from: SO.Format.Cube,
to: SO.Volume.Data,
params(a) {
const dataIndex = a ? PD.Select(0, a.data.header.dataSetIds.map((id, i) => [i, `${id}`])) : PD.Numeric(0);
return {
dataIndex,
entryId: PD.Text(''),
};
}
})({
apply({ a, params }) {
return Task.create('Create volume from Cube', async (ctx) => {
const volume = await volumeFromCube(a.data, { ...params, label: a.data.name || a.label }).runInContext(ctx);
const props = { label: volume.label || 'Volume', description: `Volume ${a.data.header.dim[0]}\u00D7${a.data.header.dim[1]}\u00D7${a.data.header.dim[2]}` };
return new SO.Volume.Data(volume, props);
});
},
dispose({ b }) {
b === null || b === void 0 ? void 0 : b.data.customProperties.dispose();
}
});
const VolumeFromDx = PluginStateTransform.BuiltIn({
name: 'volume-from-dx',
display: { name: 'Parse DX', description: 'Create volume from DX data.' },
from: SO.Format.Dx,
to: SO.Volume.Data
})({
apply({ a }) {
return Task.create('Parse DX', async (ctx) => {
const volume = await volumeFromDx(a.data, { label: a.data.name || a.label }).runInContext(ctx);
const props = { label: volume.label || 'Volume', description: `Volume ${a.data.header.dim[0]}\u00D7${a.data.header.dim[1]}\u00D7${a.data.header.dim[2]}` };
return new SO.Volume.Data(volume, props);
});
},
dispose({ b }) {
b === null || b === void 0 ? void 0 : b.data.customProperties.dispose();
}
});
const VolumeFromDensityServerCif = PluginStateTransform.BuiltIn({
name: 'volume-from-density-server-cif',
display: { name: 'Volume from density-server CIF', description: 'Identify and create all separate models in the specified CIF data block' },
from: SO.Format.Cif,
to: SO.Volume.Data,
params(a) {
if (!a) {
return {
blockHeader: PD.Optional(PD.Text(void 0, { description: 'Header of the block to parse. If none is specifed, the 1st data block in the file is used.' })),
entryId: PD.Text(''),
};
}
const blocks = a.data.blocks.slice(1); // zero block contains query meta-data
return {
blockHeader: PD.Optional(PD.Select(blocks[0] && blocks[0].header, blocks.map(b => [b.header, b.header]), { description: 'Header of the block to parse' })),
entryId: PD.Text(''),
};
}
})({
isApplicable: a => a.data.blocks.length > 0,
apply({ a, params }) {
return Task.create('Parse density-server CIF', async (ctx) => {
var _a;
const header = params.blockHeader || a.data.blocks[1].header; // zero block contains query meta-data
const block = a.data.blocks.find(b => b.header === header);
if (!block)
throw new Error(`Data block '${[header]}' not found.`);
const densityServerCif = CIF.schema.densityServer(block);
const volume = await volumeFromDensityServerData(densityServerCif, { entryId: params.entryId }).runInContext(ctx);
const [x, y, z] = volume.grid.cells.space.dimensions;
const props = { label: (_a = params.entryId) !== null && _a !== void 0 ? _a : densityServerCif.volume_data_3d_info.name.value(0), description: `Volume ${x}\u00D7${y}\u00D7${z}` };
return new SO.Volume.Data(volume, props);
});
},
dispose({ b }) {
b === null || b === void 0 ? void 0 : b.data.customProperties.dispose();
}
});
const VolumeFromSegmentationCif = PluginStateTransform.BuiltIn({
name: 'volume-from-segmentation-cif',
display: { name: 'Volume from Segmentation CIF' },
from: SO.Format.Cif,
to: SO.Volume.Data,
params(a) {
const blocks = a === null || a === void 0 ? void 0 : a.data.blocks.slice(1);
const blockHeaderParam = blocks ?
PD.Optional(PD.Select(blocks[0] && blocks[0].header, blocks.map(b => [b.header, b.header]), { description: 'Header of the block to parse' }))
: PD.Optional(PD.Text(void 0, { description: 'Header of the block to parse. If none is specifed, the 1st data block in the file is used.' }));
return {
blockHeader: blockHeaderParam,
segmentLabels: PD.ObjectList({ id: PD.Numeric(-1), label: PD.Text('') }, s => `${s.id} = ${s.label}`, { description: 'Mapping of segment IDs to segment labels' }),
ownerId: PD.Text('', { isHidden: true, description: 'Reference to the object which manages this volume' }),
};
}
})({
isApplicable: a => a.data.blocks.length > 0,
apply({ a, params }) {
return Task.create('Parse segmentation CIF', async (ctx) => {
const header = params.blockHeader || a.data.blocks[1].header; // zero block contains query meta-data
const block = a.data.blocks.find(b => b.header === header);
if (!block)
throw new Error(`Data block '${[header]}' not found.`);
const segmentationCif = CIF.schema.segmentation(block);
const segmentLabels = {};
for (const segment of params.segmentLabels)
segmentLabels[segment.id] = segment.label;
const volume = await volumeFromSegmentationData(segmentationCif, { segmentLabels, ownerId: params.ownerId }).runInContext(ctx);
const [x, y, z] = volume.grid.cells.space.dimensions;
const props = { label: segmentationCif.volume_data_3d_info.name.value(0), description: `Segmentation ${x}\u00D7${y}\u00D7${z}` };
return new SO.Volume.Data(volume, props);
});
},
dispose({ b }) {
b === null || b === void 0 ? void 0 : b.data.customProperties.dispose();
}
});
const AssignColorVolume = PluginStateTransform.BuiltIn({
name: 'assign-color-volume',
display: { name: 'Assign Color Volume', description: 'Assigns another volume to be available for coloring.' },
from: SO.Volume.Data,
to: SO.Volume.Data,
isDecorator: true,
params(a, plugin) {
if (!a)
return { ref: PD.Text() };
const cells = plugin.state.data.select(StateSelection.Generators.root.subtree().ofType(SO.Volume.Data).filter(cell => { var _a; return !!cell.obj && !((_a = cell.obj) === null || _a === void 0 ? void 0 : _a.data.colorVolume) && cell.obj !== a; }));
if (cells.length === 0)
return { ref: PD.Text('', { isHidden: true }) };
return { ref: PD.Select(cells[0].transform.ref, cells.map(c => [c.transform.ref, c.obj.label])) };
}
})({
apply({ a, params, dependencies }) {
return Task.create('Assign Color Volume', async (ctx) => {
if (!dependencies || !dependencies[params.ref]) {
throw new Error('Dependency not available.');
}
const colorVolume = dependencies[params.ref].data;
const volume = {
...a.data,
colorVolume
};
const props = { label: a.label, description: 'Volume + Colors' };
return new SO.Volume.Data(volume, props);
});
}
});
const VolumeTransform = PluginStateTransform.BuiltIn({
name: 'volume-transform',
display: { name: 'Transform Volume' },
isDecorator: true,
from: SO.Volume.Data,
to: SO.Volume.Data,
params: {
transform: TransformParam,
},
})({
canAutoUpdate() {
return false;
},
apply({ a, params }) {
// similar to StateTransforms.Model.TransformStructureConformation;
const center = transformParamsNeedCentroid(params.transform) ? Grid.getBoundingSphere(a.data.grid).center : Vec3.unit;
const transform = getTransformFromParams(params.transform, center);
const gridTransform = {
kind: 'matrix',
matrix: Mat4.mul(Mat4(), transform, Grid.getGridToCartesianTransform(a.data.grid)),
};
return new SO.Volume.Data({
...a.data,
grid: {
...a.data.grid,
transform: gridTransform,
},
}, {
label: a.label,
description: `${a.description} [Transformed]`,
});
},
});
const VolumeInstances = PluginStateTransform.BuiltIn({
name: 'volume-instances',
display: { name: 'Volume Instances' },
isDecorator: true,
from: SO.Volume.Data,
to: SO.Volume.Data,
params: {
transforms: PD.ObjectList({ transform: TransformParam }, () => 'Transform')
},
})({
canAutoUpdate() {
return true;
},
apply({ a, params }) {
const center = params.transforms.some(t => transformParamsNeedCentroid(t.transform)) ? Grid.getBoundingSphere(a.data.grid).center : Vec3.unit;
const instances = params.transforms.map(t => ({ transform: getTransformFromParams(t.transform, center) }));
if (!instances.length) {
return a;
}
return new SO.Volume.Data({
...a.data,
instances,
}, {
label: a.label,
description: `${a.description} [Instanced]`,
});
},
});
const CustomVolumeProperties = PluginStateTransform.BuiltIn({
name: 'custom-volume-properties',
display: { name: 'Custom Volume Properties' },
isDecorator: true,
from: SO.Volume.Data,
to: SO.Volume.Data,
params: (a, ctx) => {
return ctx.customVolumeProperties.getParams(a === null || a === void 0 ? void 0 : a.data);
}
})({
apply({ a, params }, ctx) {
return Task.create('Custom Volume Props', async (taskCtx) => {
await attachVolumeProps(a.data, ctx, taskCtx, params);
return new SO.Volume.Data(a.data, { label: a.label, description: a.description });
});
},
update({ a, b, oldParams, newParams }, ctx) {
return Task.create('Custom Volume Props', async (taskCtx) => {
b.data = a.data;
b.label = a.label;
b.description = a.description;
for (const name of oldParams.autoAttach) {
const property = ctx.customVolumeProperties.get(name);
if (!property)
continue;
a.data.customProperties.reference(property.descriptor, false);
}
await attachVolumeProps(a.data, ctx, taskCtx, newParams);
return StateTransformer.UpdateResult.Updated;
});
},
dispose({ b }) {
b === null || b === void 0 ? void 0 : b.data.customProperties.dispose();
}
});
async function attachVolumeProps(volume, ctx, taskCtx, params) {
const propertyCtx = { runtime: taskCtx, assetManager: ctx.managers.asset, errorContext: ctx.errorContext };
const { autoAttach, properties } = params;
for (const name of Object.keys(properties)) {
const property = ctx.customVolumeProperties.get(name);
const props = properties[name];
if (autoAttach.includes(name) || property.isHidden) {
try {
await property.attach(propertyCtx, volume, props, true);
}
catch (e) {
ctx.log.warn(`Error attaching volume prop '${name}': ${e}`);
}
}
else {
property.set(volume, props);
}
}
}