Pārlūkot izejas kodu

mol-plugin-state: do not create empty unit cells

David Sehnal 5 gadi atpakaļ
vecāks
revīzija
9b2181667d

+ 2 - 1
src/mol-math/geometry/spacegroup/construction.ts

@@ -35,7 +35,8 @@ namespace SpacegroupCell {
     export const Zero: SpacegroupCell = create('P 1', Vec3.create(1, 1, 1), Vec3.create(Math.PI / 2, Math.PI / 2, Math.PI / 2));
 
     /** True if 'P 1' with cellsize [1, 1, 1] */
-    export function isZero(cell: SpacegroupCell) {
+    export function isZero(cell?: SpacegroupCell) {
+        if (!cell) return true;
         return cell.index === 0 && cell.size[0] === 1 && cell.size[1] === 1 && cell.size[1] === 1;
     }
 

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

@@ -37,7 +37,7 @@ export const MmcifProvider: DataFormatProvider<PluginStateObject.Data.String | P
             const { structure, model } = await ctx.builders.structure.parseStructure({ data, dataFormat: 'mmcif' });
             if (options.visuals) {
                 await ctx.builders.structure.representation.applyPreset(structure, 'auto');
-                await ctx.builders.structure.createUnitcell(model, undefined, { isHidden: true })
+                await ctx.builders.structure.tryCreateUnitcell(model, undefined, { isHidden: true })
             }
         })
     }
@@ -56,7 +56,7 @@ export const PdbProvider: DataFormatProvider<any> = {
             const { structure, model } = await ctx.builders.structure.parseStructure({ data, dataFormat: 'pdb' });
             if (options.visuals) {
                 await ctx.builders.structure.representation.applyPreset(structure, 'auto');
-                await ctx.builders.structure.createUnitcell(model, undefined, { isHidden: true })
+                await ctx.builders.structure.tryCreateUnitcell(model, undefined, { isHidden: true })
             }
         })
     }
@@ -75,7 +75,7 @@ export const GroProvider: DataFormatProvider<any> = {
             const { structure, model } = await ctx.builders.structure.parseStructure({ data, dataFormat: 'gro' });
             if (options.visuals) {
                 await ctx.builders.structure.representation.applyPreset(structure, 'auto');
-                await ctx.builders.structure.createUnitcell(model, undefined, { isHidden: true })
+                await ctx.builders.structure.tryCreateUnitcell(model, undefined, { isHidden: true })
             }
         })
     }
@@ -94,7 +94,7 @@ export const Provider3dg: DataFormatProvider<any> = {
             const { structure, model } = await ctx.builders.structure.parseStructure({ data, dataFormat: '3dg' });
             if (options.visuals) {
                 await ctx.builders.structure.representation.applyPreset(structure, 'auto');
-                await ctx.builders.structure.createUnitcell(model, undefined, { isHidden: true })
+                await ctx.builders.structure.tryCreateUnitcell(model, undefined, { isHidden: true })
             }
         })
     }
@@ -253,7 +253,7 @@ const DownloadStructure = StateAction.build({
             });
             if (createRepr) {
                 await plugin.builders.structure.representation.applyPreset(structure, 'auto');
-                await plugin.builders.structure.createUnitcell(model, undefined, { isHidden: true })
+                await plugin.builders.structure.tryCreateUnitcell(model, undefined, { isHidden: true })
             }
         } else {
             for (const download of downloadParams) {
@@ -266,7 +266,7 @@ const DownloadStructure = StateAction.build({
                 });
                 if (createRepr) {
                     await plugin.builders.structure.representation.applyPreset(structure, 'auto');
-                    await plugin.builders.structure.createUnitcell(model, undefined, { isHidden: true })
+                    await plugin.builders.structure.tryCreateUnitcell(model, undefined, { isHidden: true })
                 }
             }
         }

+ 9 - 3
src/mol-plugin-state/builder/structure.ts

@@ -15,6 +15,8 @@ import { StructureRepresentationBuilder } from './structure/representation';
 import { StructureSelectionQuery } from '../helpers/structure-selection-query';
 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';
 
 export type TrajectoryFormat = 'pdb' | 'cif' | 'gro' | '3dg'
 
@@ -115,11 +117,15 @@ export class StructureBuilder {
         return props.selector;
     }
 
-    async createUnitcell(model: StateObjectRef<SO.Molecule.Model>, params?: StateTransformer.Params<StateTransforms['Representation']['ModelUnitcell3D']>, initialState?: Partial<StateTransform.State>) {
-        const state = this.plugin.state.data;
+    async tryCreateUnitcell(model: StateObjectRef<SO.Molecule.Model>, params?: StateTransformer.Params<StateTransforms['Representation']['ModelUnitcell3D']>, initialState?: Partial<StateTransform.State>) {
+        const state = this.dataState;
+        const m = StateObjectRef.resolveAndCheck(state, model)?.obj?.data;
+        if (!m) return;
+        const cell = ModelSymmetry.Provider.get(m)?.spacegroup.cell;
+        if (SpacegroupCell.isZero(cell)) return;
+
         const unitcell = state.build().to(model)
             .apply(StateTransforms.Representation.ModelUnitcell3D, params, { tags: StructureBuilderTags.ModelUnitcell, state: initialState });
-
         await this.plugin.updateDataState(unitcell, { revertOnError: true });
         return unitcell.selector;
     }

+ 1 - 1
src/mol-plugin-ui/structure/unitcell.tsx

@@ -57,7 +57,7 @@ export class UnitcellEntry extends PurePluginUIComponent<{ cell: UnitcellCell },
         if (!obj) return null;
 
         return <>
-            <div className='msp-btn-row-group' style={{ marginTop: '3px' }}>
+            <div className='msp-btn-row-group' style={{ marginTop: '6px' }}>
                 <button className='msp-form-control msp-control-button-label' title={`Unitcell. Click to focus.`} onClick={this.focus} onMouseEnter={this.highlight} onMouseLeave={this.clearHighlight} style={{ textAlign: 'left' }}>
                     Unitcell
                 </button>