Browse Source

StructureBuilder.tryCreateComponent* refactoring

David Sehnal 5 years ago
parent
commit
bff07888f9

+ 24 - 32
src/mol-plugin-state/builder/structure.ts

@@ -17,6 +17,7 @@ import { Task } from '../../mol-task';
 import { StructureElement } from '../../mol-model/structure';
 import { ModelSymmetry } from '../../mol-model-formats/structure/property/symmetry';
 import { SpacegroupCell } from '../../mol-math/geometry';
+import Expression from '../../mol-script/language/expression';
 
 export type TrajectoryFormat = 'pdb' | 'cif' | 'gro' | '3dg'
 
@@ -175,58 +176,49 @@ export class StructureBuilder {
         return selector;
     }
 
-    tryCreateStaticComponent(params: { structure: StateObjectRef<SO.Molecule.Structure>, type: StaticStructureComponentType, key: string, label?: string, tags?: string[] }) {
-        return this.tryCreateComponent(params.structure, {
-            type: { name: 'static', params: params.type },
+    tryCreateComponentFromExpression(structure: StateObjectRef<SO.Molecule.Structure>, expression: Expression, key: string, params?: { label?: string, tags?: string[] }) {
+        return this.tryCreateComponent(structure, {
+            type: { name: 'expression', params: expression },
             nullIfEmpty: true,
-            label: ''
-        }, params.key, params.tags);
+            label: (params?.label || '').trim()
+        }, key, params?.tags);
     }
 
-    tryCreateQueryComponent(params: { structure: StateObjectRef<SO.Molecule.Structure>, query: StructureSelectionQuery, key: string, label?: string, tags?: string[] }): Promise<StateObjectRef<SO.Molecule.Structure> | undefined> {
+    tryCreateComponentStatic(structure: StateObjectRef<SO.Molecule.Structure>, type: StaticStructureComponentType, key: string, params?: { label?: string, tags?: string[] }) {
+        return this.tryCreateComponent(structure, {
+            type: { name: 'static', params: type },
+            nullIfEmpty: true,
+            label: (params?.label || '').trim()
+        }, key, params?.tags);
+    }
+
+    tryCreateComponentFromSelection(structure: StateObjectRef<SO.Molecule.Structure>, selection: StructureSelectionQuery, key: string, params?: { label?: string, tags?: string[] }): Promise<StateObjectRef<SO.Molecule.Structure> | undefined> {
         return this.plugin.runTask(Task.create('Query Component', async taskCtx => {
-            let { structure, query, key, label, tags } = params;
+            let { label, tags } = params || { };
             label = (label || '').trim();
 
             const structureData = StateObjectRef.resolveAndCheck(this.dataState, structure)?.obj?.data;
 
             if (!structureData) return;
 
-            const transformParams: StructureComponentParams = query.referencesCurrent
+            const transformParams: StructureComponentParams = selection.referencesCurrent
                 ? {
                     type: {
                         name: 'bundle',
-                        params: StructureElement.Bundle.fromSelection(await query.getSelection(this.plugin, taskCtx, structureData)) },
+                        params: StructureElement.Bundle.fromSelection(await selection.getSelection(this.plugin, taskCtx, structureData)) },
                     nullIfEmpty: true,
-                    label: label || query.label
+                    label: label || selection.label
                 } : {
-                    type: { name: 'expression', params: query.expression },
+                    type: { name: 'expression', params: selection.expression },
                     nullIfEmpty: true,
-                    label: label || query.label
+                    label: label || selection.label
                 };
 
-            if (query.ensureCustomProperties) {
-                await query.ensureCustomProperties({ fetch: this.plugin.fetch, runtime: taskCtx }, structureData);
-            }
-
-            const state = this.dataState;
-            const root = state.build().to(structure);
-            const keyTag = `structure-component-${key}`;
-            const component = root.applyOrUpdateTagged(keyTag, StateTransforms.Model.StructureComponent, transformParams, {
-                tags: tags ? [...tags, StructureBuilderTags.Component, keyTag] : [StructureBuilderTags.Component, keyTag]
-            });
-
-            await this.dataState.updateTree(component).runInContext(taskCtx);
-
-            const selector = component.selector;
-
-            if (!selector.isOk || selector.cell?.obj?.data.elementCount === 0) {
-                const del = state.build().delete(selector.ref);
-                await this.plugin.updateDataState(del);
-                return;
+            if (selection.ensureCustomProperties) {
+                await selection.ensureCustomProperties({ fetch: this.plugin.fetch, runtime: taskCtx }, structureData);
             }
 
-            return selector;
+            return this.tryCreateComponent(structure, transformParams, key, tags);
         }))
     }
 

+ 2 - 10
src/mol-plugin-state/builder/structure/representation-preset.ts

@@ -220,19 +220,11 @@ const atomicDetail = StructureRepresentationPresetProvider({
 });
 
 export function presetStaticComponent(plugin: PluginContext, structure: StateObjectRef<PluginStateObject.Molecule.Structure>, type: StaticStructureComponentType) {
-    return plugin.builders.structure.tryCreateStaticComponent({
-        structure,
-        type,
-        key: `static-${type}`
-    });
+    return plugin.builders.structure.tryCreateComponentStatic(structure, type, `static-${type}`);
 }
 
 export function presetSelectionComponent(plugin: PluginContext, structure: StateObjectRef<PluginStateObject.Molecule.Structure>, query: keyof typeof Q) {
-    return plugin.builders.structure.tryCreateQueryComponent({
-        structure,
-        query: Q[query],
-        key: `selection-${query}`
-    });
+    return plugin.builders.structure.tryCreateComponentFromSelection(structure, Q[query], `selection-${query}`);
 }
 
 export const PresetStructureReprentations = {

+ 1 - 4
src/mol-plugin-state/manager/structure/component.ts

@@ -273,10 +273,7 @@ class StructureComponentManager extends PluginComponent<StructureComponentManage
 
             const componentKey = UUID.create22();
             for (const s of xs) {
-                const component = await this.plugin.builders.structure.tryCreateQueryComponent({
-                    structure: s.childRoot,
-                    query: params.selection,
-                    key: componentKey,
+                const component = await this.plugin.builders.structure.tryCreateComponentFromSelection(s.childRoot, params.selection, componentKey, {
                     label: params.label || (params.selection === StructureSelectionQueries.current ? 'Custom Selection' : ''),
                 });
                 if (params.representation === 'none' || !component) continue;