Ver código fonte

mol-plugin-state: StructureComponent

David Sehnal 5 anos atrás
pai
commit
5db134f34f

+ 1 - 1
src/mol-plugin-state/actions/structure.ts

@@ -225,7 +225,7 @@ const DownloadStructure = StateAction.build({
 
     const createRepr = !params.source.params.structure.noRepresentation;
 
-    state.transaction(async () => {
+    await state.transaction(async () => {
         if (downloadParams.length > 0 && asTrajectory) {
             const traj = await createSingleTrajectoryModel(plugin, state, downloadParams);
             const struct = createStructure(state.build().to(traj), supportProps, src.params.structure.type);

+ 13 - 6
src/mol-plugin-state/builder/structure/preset.ts

@@ -5,13 +5,13 @@
  */
 
 import { StateTransforms } from '../../transforms';
-import { StructureComplexElementTypes } from '../../transforms/model';
 import { StructureRepresentation3DHelpers } from '../../transforms/representation';
 import { StructureSelectionQueries as Q } from '../../../mol-plugin/util/structure-selection-helper';
 import { BuiltInStructureRepresentations } from '../../../mol-repr/structure/registry';
 import { StructureRepresentationProvider, RepresentationProviderTags } from './provider';
 import { StateBuilder } from '../../../mol-state';
 import { PluginStateObject } from '../../objects';
+import { StaticStructureComponentType } from '../../helpers/structure-component';
 
 const auto = StructureRepresentationProvider({
     id: 'preset-structure-representation-auto',
@@ -169,14 +169,21 @@ const cartoon = StructureRepresentationProvider({
     }
 });
 
-function applyComplex(to: StateBuilder.To<PluginStateObject.Molecule.Structure>, type: keyof typeof StructureComplexElementTypes) {
-    return to.applyOrUpdateTagged(type, StateTransforms.Model.StructureComplexElement, { type }, { tags: RepresentationProviderTags.Selection });
+function applyComplex(to: StateBuilder.To<PluginStateObject.Molecule.Structure>, type: StaticStructureComponentType) {
+    return to.applyOrUpdateTagged(type, StateTransforms.Model.StructureComponent, { 
+        type: { name: 'static', params: type },
+        nullIfEmpty: true,
+        label: ''
+    }, { tags: RepresentationProviderTags.Selection });
 }
 
 function applySelection(to: StateBuilder.To<PluginStateObject.Molecule.Structure>, query: keyof typeof Q) {
-    return to.applyOrUpdateTagged(query, StateTransforms.Model.StructureSelectionFromExpression,
-        { expression: Q[query].expression, label: Q[query].label },
-        { tags: RepresentationProviderTags.Selection });
+    return to.applyOrUpdateTagged(query, StateTransforms.Model.StructureComponent, { 
+        type: { name: 'expression', params: Q[query].expression },
+        nullIfEmpty: true,
+        label: Q[query].label
+    },
+    { tags: RepresentationProviderTags.Selection });
 }
 
 export const PresetStructureReprentations = {

+ 158 - 0
src/mol-plugin-state/helpers/structure-component.ts

@@ -0,0 +1,158 @@
+/**
+ * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import { ParamDefinition as PD } from '../../mol-util/param-definition';
+import Expression from '../../mol-script/language/expression';
+import { MolScriptBuilder } from '../../mol-script/language/builder';
+import { StructureElement, Structure, StructureSelection as Sel, StructureQuery, Queries, QueryContext } from '../../mol-model/structure';
+import { StructureQueryHelper } from '../../mol-plugin/util/structure-query';
+import { PluginStateObject as SO } from '../objects';
+import { StructureSelectionQueries } from '../../mol-plugin/util/structure-selection-helper';
+import { StateTransformer, StateObject } from '../../mol-state';
+import { Script } from '../../mol-script/script';
+
+export const StaticStructureComponentTypes = [
+    'all',
+
+    'protein-or-nucleic',
+
+    'protein',
+    'nucleic',
+    'water',
+
+    'branched',
+    'ligand',
+    'non-standard',
+
+    'coarse'
+] as const;
+
+export type StaticStructureComponentType = (typeof StaticStructureComponentTypes)[number]
+
+export const StructureComponentParams = {
+    type: PD.MappedStatic('static', {
+        static: PD.Text<StaticStructureComponentType>('protein-or-nucleic'),
+        expression: PD.Value<Expression>(MolScriptBuilder.struct.generator.all),
+        bundle: PD.Value<StructureElement.Bundle>(StructureElement.Bundle.Empty),
+        script: PD.Script({ language: 'mol-script', expression: '(sel.atom.all)' }),
+    }, { isHidden: true }),
+    nullIfEmpty: PD.Boolean(true, { isHidden: true }),
+    label: PD.Text('', { isHidden: true })
+};
+export type StructureComponentParams = PD.Values<typeof StructureComponentParams>
+
+export function createStructureComponent(a: Structure, params: StructureComponentParams, cache: { source: Structure, entry?: StructureQueryHelper.CacheEntry }) {
+    cache.source = a;
+
+    let component: Structure = Structure.Empty;
+    let label: string | undefined = void 0;
+    switch (params.type.name) {
+        case 'static': {
+            let query: StructureQuery;
+            switch (params.type.params) {
+                case 'all': query = StructureSelectionQueries.all.query; label = 'Sequence'; break;
+
+                case 'protein-or-nucleic': query = StructureSelectionQueries.proteinOrNucleic.query; label = 'Sequence'; break;
+    
+                case 'protein': query = StructureSelectionQueries.protein.query; label = 'Protein'; break;
+                case 'nucleic': query = StructureSelectionQueries.nucleic.query; label = 'Nucleic'; break;
+                case 'water': query = Queries.internal.water(); label = 'Water'; break;
+    
+                case 'branched': query = StructureSelectionQueries.branchedPlusConnected.query; label = 'Branched'; break;
+                case 'ligand': query = StructureSelectionQueries.ligandPlusConnected.query; label = 'Ligand'; break;
+    
+                case 'non-standard': query = StructureSelectionQueries.nonStandardPolymer.query; label = 'Non-standard'; break;
+    
+                case 'coarse': query = StructureSelectionQueries.coarse.query; label = 'Coarse'; break;
+        
+                default: throw new Error(`${params.type} is a not valid complex element.`);
+            }
+            const result = query(new QueryContext(a));
+            component = Sel.unionStructure(result);
+            break;
+        }
+        case 'script':
+        case 'expression': {
+            const { selection, entry } = StructureQueryHelper.createAndRun(a, params.type.params);
+            cache.entry = entry;
+            component = Sel.unionStructure(selection);
+            break;
+        }
+        case 'bundle': {
+            if (params.type.params.hash !== a.hashCode) break;
+            component = StructureElement.Bundle.toStructure(params.type.params, a);
+            break;
+        }
+    }
+
+    if (params.nullIfEmpty && component.elementCount === 0) return StateObject.Null;
+
+    const props = { label: `${params.label || label || 'Component'}`, description: Structure.elementDescription(component) };
+    return new SO.Molecule.Structure(component, props);
+}
+
+export function updateStructureComponent(a: Structure, b: SO.Molecule.Structure, oldParams: StructureComponentParams, newParams: StructureComponentParams, cache: { source: Structure, entry?: StructureQueryHelper.CacheEntry }) {
+    if (oldParams.type.name !== newParams.type.name) return StateTransformer.UpdateResult.Recreate;
+    
+    let updated = false;
+
+    switch (newParams.type.name) {
+        case 'static': {
+            if (a !== cache.source || oldParams.type.params !== newParams.type.params) {
+                return StateTransformer.UpdateResult.Recreate;
+            }
+            break;
+        }
+        case 'script':
+            if (!Script.areEqual(oldParams.type.params as Script, newParams.type.params)) {
+                return StateTransformer.UpdateResult.Recreate;
+            }
+        case 'expression': {
+            if ((oldParams.type.params as Expression) !== newParams.type.params) {
+                return StateTransformer.UpdateResult.Recreate;
+            }
+
+            if (a !== cache.source) return StateTransformer.UpdateResult.Unchanged;
+
+            const entry = (cache as { entry: StructureQueryHelper.CacheEntry }).entry;
+
+            const selection = StructureQueryHelper.updateStructure(entry, a);
+            cache.source = a;
+            b.data = Sel.unionStructure(selection);
+            StructureQueryHelper.updateStructureObject(b, selection, newParams.label);
+            updated = true;
+            break;
+        }
+        case 'bundle': {
+            if (a === cache.source && StructureElement.Bundle.areEqual(oldParams.type.params as StructureElement.Bundle, newParams.type.params)) {
+                break;
+            }
+
+            cache.source = a;
+            if (newParams.type.params.hash !== a.hashCode) {
+                updated = b.data.elementCount !== 0;
+                b.data = b.data.elementCount === 0 ? b.data : Structure.Empty;
+            } else {
+                updated = true;
+                b.data = StructureElement.Bundle.toStructure(newParams.type.params, a);
+            }
+            break;
+        }
+    }
+
+    if (updated) {
+        if (newParams.nullIfEmpty && b.data.elementCount === 0) return StateTransformer.UpdateResult.Null;
+
+        b.description = Structure.elementDescription(b.data);
+    }
+
+    if (oldParams.label !== newParams.label) {
+        updated = true;
+        b.label = `${newParams.label || b.label}`;
+    }
+
+    return updated ? StateTransformer.UpdateResult.Updated : StateTransformer.UpdateResult.Unchanged;
+}

+ 18 - 0
src/mol-plugin-state/transforms/model.ts

@@ -31,6 +31,7 @@ import { parseDcd } from '../../mol-io/reader/dcd/parser';
 import { coordinatesFromDcd } from '../../mol-model-formats/structure/dcd';
 import { topologyFromPsf } from '../../mol-model-formats/structure/psf';
 import { deepEqual } from '../../mol-util';
+import { StructureComponentParams, createStructureComponent, updateStructureComponent } from '../helpers/structure-component';
 
 export { CoordinatesFromDcd };
 export { TopologyFromPsf };
@@ -50,6 +51,7 @@ export { MultiStructureSelectionFromExpression }
 export { StructureSelectionFromScript };
 export { StructureSelectionFromBundle };
 export { StructureComplexElement };
+export { StructureComponent }
 export { CustomModelProperties };
 export { CustomStructureProperties };
 
@@ -668,6 +670,22 @@ const StructureComplexElement = PluginStateTransform.BuiltIn({
     }
 });
 
+type StructureComponent = typeof StructureComponent
+const StructureComponent = PluginStateTransform.BuiltIn({
+    name: 'structure-component',
+    display: { name: 'Component', description: 'A molecular structure component.' },
+    from: SO.Molecule.Structure,
+    to: SO.Molecule.Structure,
+    params: StructureComponentParams
+})({
+    apply({ a, params, cache }) {
+        return createStructureComponent(a.data, params, cache as any);
+    },
+    update: ({ a, b, oldParams, newParams, cache }) => {
+        return updateStructureComponent(a.data, b, oldParams, newParams, cache as any);
+    }
+});
+
 type CustomModelProperties = typeof CustomModelProperties
 const CustomModelProperties = PluginStateTransform.BuiltIn({
     name: 'custom-model-properties',