Browse Source

mol-plugin: structure transform3d

David Sehnal 6 years ago
parent
commit
52a02fa922

+ 15 - 1
src/mol-model/structure/structure/structure.ts

@@ -21,7 +21,7 @@ import StructureProperties from './properties';
 import { ResidueIndex, ChainIndex, EntityIndex } from '../model/indexing';
 import { Carbohydrates } from './carbohydrates/data';
 import { computeCarbohydrates } from './carbohydrates/compute';
-import { Vec3 } from 'mol-math/linear-algebra';
+import { Vec3, Mat4 } from 'mol-math/linear-algebra';
 import { idFactory } from 'mol-util/id-factory';
 import { GridLookup3D } from 'mol-math/geometry';
 import { UUID } from 'mol-util';
@@ -344,6 +344,20 @@ namespace Structure {
         }
     }
 
+    export function transform(s: Structure, transform: Mat4) {
+        if (Mat4.isIdentity(transform)) return s;
+        if (!Mat4.isRotationAndTranslation(transform)) throw new Error('Only rotation/translation combination can be applied.');
+
+        const units: Unit[] = [];
+        for (const u of s.units) {
+            const old = u.conformation.operator;
+            const op = SymmetryOperator.create(old.name, transform, old.hkl);
+            units.push(u.applyOperator(u.id, op));
+        }
+
+        return new Structure(units);
+    }
+
     export class StructureBuilder {
         private units: Unit[] = [];
         private invariantId = idFactory()

+ 5 - 2
src/mol-plugin/state/transforms/model.ts

@@ -11,6 +11,7 @@ import { Model, Format, Structure, ModelSymmetry, StructureSymmetry, QueryContex
 import { ParamDefinition as PD } from 'mol-util/param-definition';
 import Expression from 'mol-script/language/expression';
 import { compile } from 'mol-script/runtime/query/compiler';
+import { Mat4 } from 'mol-math/linear-algebra';
 
 export { ParseModelsFromMmCif }
 namespace ParseModelsFromMmCif { export interface Params { blockHeader?: string } }
@@ -46,7 +47,7 @@ const ParseModelsFromMmCif = PluginStateTransform.Create<SO.Data.Cif, SO.Models,
 });
 
 export { CreateStructureFromModel }
-namespace CreateStructureFromModel { export interface Params { modelIndex: number } }
+namespace CreateStructureFromModel { export interface Params { modelIndex: number, transform3d?: Mat4 } }
 const CreateStructureFromModel = PluginStateTransform.Create<SO.Models, SO.Structure, CreateStructureFromModel.Params>({
     name: 'create-structure-from-model',
     display: {
@@ -59,9 +60,11 @@ const CreateStructureFromModel = PluginStateTransform.Create<SO.Models, SO.Struc
         default: () => ({ modelIndex: 0 }),
         controls: a => ({ modelIndex: PD.Range('Model Index', 'Model Index', 0, 0, Math.max(0, a.data.length - 1), 1) })
     },
+    isApplicable: a => a.data.length > 0,
     apply({ a, params }) {
         if (params.modelIndex < 0 || params.modelIndex >= a.data.length) throw new Error(`Invalid modelIndex ${params.modelIndex}`);
-        const s = Structure.ofModel(a.data[params.modelIndex]);
+        let s = Structure.ofModel(a.data[params.modelIndex]);
+        if (params.transform3d) s = Structure.transform(s, params.transform3d);
         return new SO.Structure({ label: `Model ${s.models[0].modelNum}`, description: s.elementCount === 1 ? '1 element' : `${s.elementCount} elements` }, s);
     }
 });