123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- /**
- * Copyright (c) 2019 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>
- */
- import { ParamDefinition as PD } from '../../../../mol-util/param-definition';
- import { PluginStateObject } from '../../../state/objects';
- import { VolumeIsoValue, VolumeData } from '../../../../mol-model/volume';
- import { createIsoValueParam } from '../../../../mol-repr/volume/isosurface';
- import { VolumeServerHeader, VolumeServerInfo } from './model';
- import { Box3D } from '../../../../mol-math/geometry';
- import { Vec3 } from '../../../../mol-math/linear-algebra';
- import { Color } from '../../../../mol-util/color';
- import { PluginBehavior } from '../../behavior';
- import { LRUCache } from '../../../../mol-util/lru-cache';
- import { urlCombine } from '../../../../mol-util/url';
- import { CIF } from '../../../../mol-io/reader/cif';
- import { volumeFromDensityServerData } from '../../../../mol-model-formats/volume/density-server';
- import { PluginCommands } from '../../../command';
- import { StateSelection } from '../../../../mol-state';
- import { Representation } from '../../../../mol-repr/representation';
- import { ButtonsType, ModifiersKeys } from '../../../../mol-util/input/input-observer';
- import { StructureElement, Link, Structure } from '../../../../mol-model/structure';
- import { PluginContext } from '../../../context';
- import { Binding } from '../../../../mol-util/binding';
- import { EmptyLoci, Loci, isEmptyLoci } from '../../../../mol-model/loci';
- const B = ButtonsType
- const M = ModifiersKeys
- const Trigger = Binding.Trigger
- export class VolumeStreaming extends PluginStateObject.CreateBehavior<VolumeStreaming.Behavior>({ name: 'Volume Streaming' }) { }
- export namespace VolumeStreaming {
- function channelParam(label: string, color: Color, defaultValue: VolumeIsoValue, stats: VolumeData['dataStats']) {
- return PD.Group({
- isoValue: createIsoValueParam(defaultValue, stats),
- color: PD.Color(color),
- wireframe: PD.Boolean(false),
- opacity: PD.Numeric(0.3, { min: 0, max: 1, step: 0.01 })
- }, { label, isExpanded: true });
- }
- const fakeSampling: VolumeServerHeader.Sampling = {
- byteOffset: 0,
- rate: 1,
- sampleCount: [1, 1, 1],
- valuesInfo: [{ mean: 0, min: -1, max: 1, sigma: 0.1 }, { mean: 0, min: -1, max: 1, sigma: 0.1 }]
- };
- export const DefaultBindings = {
- clickVolumeAroundOnly: Binding(Trigger(B.Flag.Secondary, M.create()), 'Show the volume around only the clicked element using ${trigger}.'),
- }
- export function createParams(data?: VolumeServerInfo.Data, defaultView?: ViewTypes, binding?: typeof DefaultBindings) {
- // fake the info
- const info = data || { kind: 'em', header: { sampling: [fakeSampling], availablePrecisions: [{ precision: 0, maxVoxels: 0 }] }, emDefaultContourLevel: VolumeIsoValue.relative(0) };
- const box = (data && data.structure.boundary.box) || Box3D.empty();
- return {
- view: PD.MappedStatic(defaultView || (info.kind === 'em' ? 'cell' : 'selection-box'), {
- 'off': PD.Group({}),
- 'box': PD.Group({
- bottomLeft: PD.Vec3(box.min),
- topRight: PD.Vec3(box.max),
- }, { description: 'Static box defined by cartesian coords.', isFlat: true }),
- 'selection-box': PD.Group({
- radius: PD.Numeric(5, { min: 0, max: 50, step: 0.5 }),
- bottomLeft: PD.Vec3(Vec3.create(0, 0, 0), { isHidden: true }),
- topRight: PD.Vec3(Vec3.create(0, 0, 0), { isHidden: true }),
- }, { description: 'Box around last-interacted element.', isFlat: true }),
- 'cell': PD.Group({}),
- // 'auto': PD.Group({ }), // TODO based on camera distance/active selection/whatever, show whole structure or slice.
- }, { options: ViewTypeOptions as any }),
- detailLevel: PD.Select<number>(Math.min(3, info.header.availablePrecisions.length - 1),
- info.header.availablePrecisions.map((p, i) => [i, `${i + 1} [ ${Math.pow(p.maxVoxels, 1 / 3) | 0}^3 cells ]`] as [number, string])),
- channels: info.kind === 'em'
- ? PD.Group({
- 'em': channelParam('EM', Color(0x638F8F), info.emDefaultContourLevel || VolumeIsoValue.relative(1), info.header.sampling[0].valuesInfo[0])
- }, { isFlat: true })
- : PD.Group({
- '2fo-fc': channelParam('2Fo-Fc', Color(0x3362B2), VolumeIsoValue.relative(1.5), info.header.sampling[0].valuesInfo[0]),
- 'fo-fc(+ve)': channelParam('Fo-Fc(+ve)', Color(0x33BB33), VolumeIsoValue.relative(3), info.header.sampling[0].valuesInfo[1]),
- 'fo-fc(-ve)': channelParam('Fo-Fc(-ve)', Color(0xBB3333), VolumeIsoValue.relative(-3), info.header.sampling[0].valuesInfo[1]),
- }, { isFlat: true }),
- bindings: PD.Value(binding || DefaultBindings, { isHidden: true }),
- };
- }
- export const ViewTypeOptions = [['off', 'Off'], ['box', 'Bounded Box'], ['selection-box', 'Surroundings'], ['cell', 'Whole Structure']];
- export type ViewTypes = 'off' | 'box' | 'selection-box' | 'cell'
- export type ParamDefinition = typeof createParams extends (...args: any[]) => (infer T) ? T : never
- export type Params = ParamDefinition extends PD.Params ? PD.Values<ParamDefinition> : {}
- type CT = typeof channelParam extends (...args: any[]) => (infer T) ? T : never
- export type ChannelParams = CT extends PD.Group<infer T> ? T : {}
- type ChannelsInfo = { [name in ChannelType]?: { isoValue: VolumeIsoValue, color: Color, wireframe: boolean, opacity: number } }
- type ChannelsData = { [name in 'EM' | '2FO-FC' | 'FO-FC']?: VolumeData }
- export type ChannelType = 'em' | '2fo-fc' | 'fo-fc(+ve)' | 'fo-fc(-ve)'
- export const ChannelTypeOptions: [ChannelType, string][] = [['em', 'em'], ['2fo-fc', '2fo-fc'], ['fo-fc(+ve)', 'fo-fc(+ve)'], ['fo-fc(-ve)', 'fo-fc(-ve)']]
- export interface ChannelInfo {
- data: VolumeData,
- color: Color,
- wireframe: boolean,
- isoValue: VolumeIsoValue.Relative,
- opacity: number
- }
- export type Channels = { [name in ChannelType]?: ChannelInfo }
- export class Behavior extends PluginBehavior.WithSubscribers<Params> {
- private cache = LRUCache.create<ChannelsData>(25);
- public params: Params = {} as any;
- private lastLoci: StructureElement.Loci | EmptyLoci = EmptyLoci;
- private ref: string = '';
- channels: Channels = {}
- private async queryData(box?: Box3D) {
- let url = urlCombine(this.info.serverUrl, `${this.info.kind}/${this.info.dataId.toLowerCase()}`);
- if (box) {
- const { min: a, max: b } = box;
- url += `/box`
- + `/${a.map(v => Math.round(1000 * v) / 1000).join(',')}`
- + `/${b.map(v => Math.round(1000 * v) / 1000).join(',')}`;
- } else {
- url += `/cell`;
- }
- url += `?detail=${this.params.detailLevel}`;
- let data = LRUCache.get(this.cache, url);
- if (data) {
- return data;
- }
- const cif = await this.plugin.runTask(this.plugin.fetch({ url, type: 'binary' }));
- data = await this.parseCif(cif as Uint8Array);
- if (!data) {
- return;
- }
- LRUCache.set(this.cache, url, data);
- return data;
- }
- private async parseCif(data: Uint8Array): Promise<ChannelsData | undefined> {
- const parsed = await this.plugin.runTask(CIF.parseBinary(data));
- if (parsed.isError) {
- this.plugin.log.error('VolumeStreaming, parsing CIF: ' + parsed.toString());
- return;
- }
- if (parsed.result.blocks.length < 2) {
- this.plugin.log.error('VolumeStreaming: Invalid data.');
- return;
- }
- const ret: ChannelsData = {};
- for (let i = 1; i < parsed.result.blocks.length; i++) {
- const block = parsed.result.blocks[i];
- const densityServerCif = CIF.schema.densityServer(block);
- const volume = await this.plugin.runTask(await volumeFromDensityServerData(densityServerCif));
- (ret as any)[block.header as any] = volume;
- }
- return ret;
- }
- private updateDynamicBox(box: Box3D) {
- if (this.params.view.name !== 'selection-box') return;
- const state = this.plugin.state.dataState;
- const newParams: Params = {
- ...this.params,
- view: {
- name: 'selection-box' as 'selection-box',
- params: {
- radius: this.params.view.params.radius,
- bottomLeft: box.min,
- topRight: box.max
- }
- }
- };
- const update = state.build().to(this.ref).update(newParams);
- PluginCommands.State.Update.dispatch(this.plugin, { state, tree: update, options: { doNotUpdateCurrent: true } });
- }
- private getStructureRoot() {
- return this.plugin.state.dataState.select(StateSelection.Generators.byRef(this.ref).rootOfType([PluginStateObject.Molecule.Structure]))[0];
- }
- register(ref: string): void {
- this.ref = ref;
- this.subscribeObservable(this.plugin.events.state.object.removed, o => {
- if (!PluginStateObject.Molecule.Structure.is(o.obj) || !StructureElement.Loci.is(this.lastLoci)) return;
- if (this.lastLoci.structure === o.obj.data) {
- this.lastLoci = EmptyLoci;
- }
- });
- this.subscribeObservable(this.plugin.events.state.object.updated, o => {
- if (!PluginStateObject.Molecule.Structure.is(o.oldObj) || !StructureElement.Loci.is(this.lastLoci)) return;
- if (this.lastLoci.structure === o.oldObj.data) {
- this.lastLoci = EmptyLoci;
- }
- });
- this.subscribeObservable(this.plugin.behaviors.interaction.click, ({ current, buttons, modifiers }) => {
- if (!Binding.match(this.params.bindings.clickVolumeAroundOnly || DefaultBindings.clickVolumeAroundOnly, buttons, modifiers)) return;
- if (this.params.view.name !== 'selection-box') {
- this.lastLoci = this.getNormalizedLoci(current.loci);
- } else {
- this.updateInteraction(current);
- }
- });
- }
- private getNormalizedLoci(loci: Loci): StructureElement.Loci | EmptyLoci {
- if (StructureElement.Loci.is(loci)) {
- return loci;
- } else if (Link.isLoci(loci)) {
- return Link.toStructureElementLoci(loci);
- } else if (Structure.isLoci(loci)) {
- return Structure.toStructureElementLoci(loci);
- } else {
- return EmptyLoci;
- }
- }
- private getBoxFromLoci(loci: StructureElement.Loci | EmptyLoci): Box3D {
- if (isEmptyLoci(loci) || StructureElement.Loci.isEmpty(loci)) {
- return Box3D.empty();
- }
- const parent = this.plugin.helpers.substructureParent.get(loci.structure);
- if (!parent) return Box3D.empty();
- const root = this.getStructureRoot();
- if (!root || !root.obj || root.obj !== parent.obj) return Box3D.empty();
- const extendedLoci = StructureElement.Loci.extendToWholeResidues(loci)
- const box = StructureElement.Loci.getBoundary(extendedLoci).box
- if (StructureElement.Loci.size(extendedLoci) === 1) {
- Box3D.expand(box, box, Vec3.create(1, 1, 1))
- }
- return box;
- }
- private updateInteraction(current: Representation.Loci) {
- const loci = this.getNormalizedLoci(current.loci)
- if (Loci.areEqual(this.lastLoci, loci)) {
- this.lastLoci = EmptyLoci;
- this.updateDynamicBox(Box3D.empty());
- return;
- }
- this.lastLoci = loci;
- if (isEmptyLoci(loci)) {
- this.updateDynamicBox(Box3D.empty());
- return;
- }
- const box = this.getBoxFromLoci(loci);
- this.updateDynamicBox(box);
- }
- async update(params: Params) {
- const switchedToSelection = params.view.name === 'selection-box' && this.params && this.params.view && this.params.view.name !== 'selection-box';
- this.params = params;
- let box: Box3D | undefined = void 0, emptyData = false;
- switch (params.view.name) {
- case 'off':
- emptyData = true;
- break;
- case 'box':
- box = Box3D.create(params.view.params.bottomLeft, params.view.params.topRight);
- emptyData = Box3D.volume(box) < 0.0001;
- break;
- case 'selection-box': {
- if (switchedToSelection) {
- box = this.getBoxFromLoci(this.lastLoci) || Box3D.empty();
- } else {
- box = Box3D.create(Vec3.clone(params.view.params.bottomLeft), Vec3.clone(params.view.params.topRight));
- }
- const r = params.view.params.radius;
- emptyData = Box3D.volume(box) < 0.0001;
- Box3D.expand(box, box, Vec3.create(r, r, r));
- break;
- }
- case 'cell':
- box = this.info.kind === 'x-ray'
- ? this.info.structure.boundary.box
- : void 0;
- break;
- }
- const data = emptyData ? {} : await this.queryData(box);
- if (!data) return false;
- const info = params.channels as ChannelsInfo;
- if (this.info.kind === 'x-ray') {
- this.channels['2fo-fc'] = this.createChannel(data['2FO-FC'] || VolumeData.One, info['2fo-fc'], this.info.header.sampling[0].valuesInfo[0]);
- this.channels['fo-fc(+ve)'] = this.createChannel(data['FO-FC'] || VolumeData.One, info['fo-fc(+ve)'], this.info.header.sampling[0].valuesInfo[1]);
- this.channels['fo-fc(-ve)'] = this.createChannel(data['FO-FC'] || VolumeData.One, info['fo-fc(-ve)'], this.info.header.sampling[0].valuesInfo[1]);
- } else {
- this.channels['em'] = this.createChannel(data['EM'] || VolumeData.One, info['em'], this.info.header.sampling[0].valuesInfo[0]);
- }
- return true;
- }
- private createChannel(data: VolumeData, info: ChannelsInfo['em'], stats: VolumeData['dataStats']): ChannelInfo {
- const i = info!;
- return {
- data,
- color: i.color,
- wireframe: i.wireframe,
- opacity: i.opacity,
- isoValue: i.isoValue.kind === 'relative' ? i.isoValue : VolumeIsoValue.toRelative(i.isoValue, stats)
- };
- }
- getDescription() {
- if (this.params.view.name === 'selection-box') return 'Selection';
- if (this.params.view.name === 'box') return 'Static Box';
- if (this.params.view.name === 'cell') return 'Cell';
- return '';
- }
- constructor(public plugin: PluginContext, public info: VolumeServerInfo.Data) {
- super(plugin, {} as any);
- }
- }
- }
|