Skip to content
Snippets Groups Projects
Commit 2b5e49d2 authored by Alexander Rose's avatar Alexander Rose
Browse files

use LociStructureSelection in plugin structure tools

parent b88bf9bd
No related branches found
No related tags found
No related merge requests found
...@@ -10,11 +10,7 @@ import { StateTransformer, StateSelection, StateObjectCell, StateTransform } fro ...@@ -10,11 +10,7 @@ import { StateTransformer, StateSelection, StateObjectCell, StateTransform } fro
import { StructureElement } from '../../mol-model/structure'; import { StructureElement } from '../../mol-model/structure';
import { isEmptyLoci } from '../../mol-model/loci'; import { isEmptyLoci } from '../../mol-model/loci';
import { PluginContext } from '../context'; import { PluginContext } from '../context';
import { parseMolScript } from '../../mol-script/language/parser';
import { StructureRepresentation3DHelpers } from '../state/transforms/representation'; import { StructureRepresentation3DHelpers } from '../state/transforms/representation';
import Expression from '../../mol-script/language/expression';
import { formatMolScript } from '../../mol-script/language/expression-formatter';
import { MolScriptBuilder as MS } from '../../mol-script/language/builder';
type StructureTransform = StateObjectCell<PluginStateObject.Molecule.Structure, StateTransform<StateTransformer<any, PluginStateObject.Molecule.Structure, any>>> type StructureTransform = StateObjectCell<PluginStateObject.Molecule.Structure, StateTransform<StateTransformer<any, PluginStateObject.Molecule.Structure, any>>>
const RepresentationManagerTag = 'representation-controls' const RepresentationManagerTag = 'representation-controls'
...@@ -23,19 +19,19 @@ function getRepresentationManagerTag(type: string) { ...@@ -23,19 +19,19 @@ function getRepresentationManagerTag(type: string) {
return `${RepresentationManagerTag}-${type}` return `${RepresentationManagerTag}-${type}`
} }
function getCombinedExpression(modifier: SelectionModifier, expression: Expression, currentExpression: Expression): Expression { function getCombinedLoci(mode: SelectionModifier, loci: StructureElement.Loci, currentLoci: StructureElement.Loci): StructureElement.Loci {
switch (modifier) { switch (mode) {
case 'add': return MS.struct.combinator.merge([ currentExpression, expression ]) case 'add': return StructureElement.Loci.union(loci, currentLoci)
case 'remove': return MS.struct.modifier.exceptBy({ 0: currentExpression, by: expression }) case 'remove': return StructureElement.Loci.subtract(currentLoci, loci)
case 'only': return expression case 'only': return loci
case 'all': return MS.struct.generator.all() case 'all': return StructureElement.Loci.all(loci.structure)
} }
} }
type SelectionModifier = 'add' | 'remove' | 'only' | 'all' type SelectionModifier = 'add' | 'remove' | 'only' | 'all'
export class StructureRepresentationHelper { export class StructureRepresentationHelper {
async set(modifier: SelectionModifier, type: string, expression: Expression, structure: StructureTransform) { async set(modifier: SelectionModifier, type: string, loci: StructureElement.Loci, structure: StructureTransform) {
const state = this.plugin.state.dataState const state = this.plugin.state.dataState
const update = state.build(); const update = state.build();
const s = structure.obj!.data const s = structure.obj!.data
...@@ -43,23 +39,21 @@ export class StructureRepresentationHelper { ...@@ -43,23 +39,21 @@ export class StructureRepresentationHelper {
const selections = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure, structure.transform.ref).withTag(getRepresentationManagerTag(type))); const selections = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure, structure.transform.ref).withTag(getRepresentationManagerTag(type)));
if (selections.length > 0) { if (selections.length > 0) {
const parsedExpressions = parseMolScript(selections[0].params!.values.query.expression) const currentLoci = StructureElement.Query.toLoci(selections[0].params!.values.query, s)
if (parsedExpressions.length === 0) return const combinedLoci = getCombinedLoci(modifier, loci, currentLoci)
const currentExpression = parsedExpressions[0]
const combinedExpression = getCombinedExpression(modifier, expression, currentExpression)
update.to(selections[0]).update({ update.to(selections[0]).update({
...selections[0].params!.values, ...selections[0].params!.values,
query: { language: 'mol-script', expression: formatMolScript(combinedExpression) } query: StructureElement.Query.fromLoci(combinedLoci)
}) })
} else { } else {
const combinedExpression = getCombinedExpression(modifier, expression, MS.struct.generator.empty()) const combinedLoci = getCombinedLoci(modifier, loci, StructureElement.Loci(s, []))
update.to(structure.transform.ref) update.to(structure.transform.ref)
.apply( .apply(
StateTransforms.Model.UserStructureSelection, StateTransforms.Model.LociStructureSelection,
{ {
query: { language: 'mol-script', expression: formatMolScript(combinedExpression) }, query: StructureElement.Query.fromLoci(combinedLoci),
label: type label: type
}, },
{ tags: [ RepresentationManagerTag, getRepresentationManagerTag(type) ] } { tags: [ RepresentationManagerTag, getRepresentationManagerTag(type) ] }
...@@ -81,9 +75,8 @@ export class StructureRepresentationHelper { ...@@ -81,9 +75,8 @@ export class StructureRepresentationHelper {
const s = structure.obj!.data const s = structure.obj!.data
const _loci = this.plugin.helpers.structureSelectionManager.get(s) const _loci = this.plugin.helpers.structureSelectionManager.get(s)
const loci = isEmptyLoci(_loci) ? StructureElement.Loci(s, []) : _loci const loci = isEmptyLoci(_loci) ? StructureElement.Loci(s, []) : _loci
const expression = StructureElement.Loci.toScriptExpression(loci)
await this.set(modifier, type, expression, structure) await this.set(modifier, type, loci, structure)
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment