|
@@ -6,35 +6,9 @@
|
|
|
|
|
|
import * as React from 'react';
|
|
|
import { PluginUIComponent } from '../base';
|
|
|
-import { PluginStateObject } from '../../../mol-plugin/state/objects';
|
|
|
-import { StateTransforms } from '../../../mol-plugin/state/transforms';
|
|
|
-import { StateTransformer, StateSelection, StateObjectCell, StateTransform, StateBuilder } from '../../../mol-state';
|
|
|
import { ParamDefinition as PD} from '../../../mol-util/param-definition';
|
|
|
import { ParameterControls } from '../controls/parameters';
|
|
|
-import { StructureElement, QueryContext, StructureSelection } from '../../../mol-model/structure';
|
|
|
-import { isEmptyLoci } from '../../../mol-model/loci';
|
|
|
import { PluginContext } from '../../context';
|
|
|
-import { getExpression } from './util';
|
|
|
-import { parseMolScript } from '../../../mol-script/language/parser';
|
|
|
-import { transpileMolScript } from '../../../mol-script/script/mol-script/symbols';
|
|
|
-import { compile } from '../../../mol-script/runtime/query/compiler';
|
|
|
-import { StructureRepresentation3DHelpers } from '../../state/transforms/representation';
|
|
|
-
|
|
|
-type RepresentationEachStructureCallback = (update: StateBuilder.Root, structure: StateObjectCell<PluginStateObject.Molecule.Structure, StateTransform<StateTransformer<any, PluginStateObject.Molecule.Structure, any>>>) => void
|
|
|
-const RepresentationManagerTag = 'representation-controls'
|
|
|
-
|
|
|
-function getRepresentationManagerTag(type: string) {
|
|
|
- return `${RepresentationManagerTag}-${type}`
|
|
|
-}
|
|
|
-
|
|
|
-function getCombinedLoci(mode: 'add' | 'remove' | 'only' | 'all', loci: StructureElement.Loci, currentLoci: StructureElement.Loci): StructureElement.Loci {
|
|
|
- switch (mode) {
|
|
|
- case 'add': return StructureElement.Loci.union(loci, currentLoci)
|
|
|
- case 'remove': return StructureElement.Loci.subtract(currentLoci, loci)
|
|
|
- case 'only': return loci
|
|
|
- case 'all': return StructureElement.Loci.all(loci.structure)
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
export class StructureRepresentationControls extends PluginUIComponent<{}, { params: PD.Values<ReturnType<typeof StructureRepresentationControls.getParams>> }> {
|
|
|
state = { params: PD.getDefaultValues(StructureRepresentationControls.getParams(this.plugin)) }
|
|
@@ -50,77 +24,17 @@ export class StructureRepresentationControls extends PluginUIComponent<{}, { par
|
|
|
|
|
|
}
|
|
|
|
|
|
- private async eachStructure(callback: RepresentationEachStructureCallback) {
|
|
|
- const state = this.plugin.state.dataState;
|
|
|
- const structures = state.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Structure));
|
|
|
-
|
|
|
- const update = state.build();
|
|
|
- for (const s of structures) {
|
|
|
- callback(update, s)
|
|
|
- }
|
|
|
-
|
|
|
- await this.plugin.runTask(state.updateTree(update, { doNotUpdateCurrent: true }));
|
|
|
- }
|
|
|
-
|
|
|
- set = async (mode: 'add' | 'remove' | 'only' | 'all') => {
|
|
|
- const state = this.plugin.state.dataState
|
|
|
- const { type } = this.state.params
|
|
|
-
|
|
|
- await this.eachStructure((update, structure) => {
|
|
|
- const s = structure.obj!.data
|
|
|
- const _loci = this.plugin.helpers.structureSelection.get(s)
|
|
|
- const loci = isEmptyLoci(_loci) ? StructureElement.Loci(s, []) : _loci
|
|
|
-
|
|
|
- const selections = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure, structure.transform.ref).withTag(getRepresentationManagerTag(type)));
|
|
|
-
|
|
|
- if (selections.length > 0) {
|
|
|
- const parsed = parseMolScript(selections[0].params!.values.query.expression)
|
|
|
- if (parsed.length === 0) return
|
|
|
-
|
|
|
- const query = transpileMolScript(parsed[0])
|
|
|
- const compiled = compile(query)
|
|
|
- const result = compiled(new QueryContext(structure.obj!.data))
|
|
|
- const currentLoci = StructureSelection.toLoci2(result)
|
|
|
-
|
|
|
- const combinedLoci = getCombinedLoci(mode, loci, currentLoci)
|
|
|
-
|
|
|
- update.to(selections[0]).update({
|
|
|
- ...selections[0].params!.values,
|
|
|
- query: { language: 'mol-script', expression: getExpression(combinedLoci) }
|
|
|
- })
|
|
|
- } else {
|
|
|
- const combinedLoci = getCombinedLoci(mode, loci, StructureElement.Loci(loci.structure, []))
|
|
|
-
|
|
|
- update.to(structure.transform.ref)
|
|
|
- .apply(
|
|
|
- StateTransforms.Model.UserStructureSelection,
|
|
|
- {
|
|
|
- query: { language: 'mol-script', expression: getExpression(combinedLoci) },
|
|
|
- label: type
|
|
|
- },
|
|
|
- { tags: [ RepresentationManagerTag, getRepresentationManagerTag(type) ] }
|
|
|
- )
|
|
|
- .apply(
|
|
|
- StateTransforms.Representation.StructureRepresentation3D,
|
|
|
- StructureRepresentation3DHelpers.getDefaultParams(this.plugin, type as any, s)
|
|
|
- )
|
|
|
- }
|
|
|
- })
|
|
|
+ set = (mode: 'add' | 'remove' | 'only' | 'all') => {
|
|
|
+ this.plugin.helpers.structureRepresentation.setSelected(mode, this.state.params.type)
|
|
|
}
|
|
|
|
|
|
- show = async () => { this.set('add') }
|
|
|
- hide = async () => { this.set('remove') }
|
|
|
- only = async () => { this.set('only') }
|
|
|
- showAll = async () => { this.set('all') }
|
|
|
-
|
|
|
- hideAll = async () => {
|
|
|
- const { type } = this.state.params
|
|
|
- const state = this.plugin.state.dataState;
|
|
|
- const update = state.build();
|
|
|
-
|
|
|
- state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure).withTag(getRepresentationManagerTag(type))).forEach(structure => update.delete(structure.transform.ref));
|
|
|
+ show = () => { this.set('add') }
|
|
|
+ hide = () => { this.set('remove') }
|
|
|
+ only = () => { this.set('only') }
|
|
|
+ showAll = () => { this.set('all') }
|
|
|
|
|
|
- await this.plugin.runTask(state.updateTree(update, { doNotUpdateCurrent: true }));
|
|
|
+ hideAll = () => {
|
|
|
+ this.plugin.helpers.structureRepresentation.hideAll(this.state.params.type)
|
|
|
}
|
|
|
|
|
|
render() {
|