Browse Source

refactoring

David Sehnal 7 years ago
parent
commit
35d7c19649

+ 4 - 33
src/mol-data/model.ts

@@ -4,37 +4,8 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import * as Formats from './model/formats'
-import HierarchyProperties from './model/properties/hierarchy'
-import ConformationProperties from './model/properties/conformation'
-import UUID from '../mol-base/utils/uuid'
-import buildMmCIF from './model/builders/mmcif'
+import Model from './model/model'
+import ModelDataFormat from './model/data-format'
+import * as ModelConstants from './model/constants'
 
-export interface Version {
-    hierarchy: UUID,
-    conformation: UUID
-}
-
-/**
- * Interface to the "source data" of the molecule.
- *
- * "Atoms" are integers in the range [0, atomCount).
- */
-interface Model extends Readonly<{
-    id: UUID,
-
-    modelNum: number,
-
-    sourceData: Formats.RawData,
-
-    hierarchy: HierarchyProperties,
-    conformation: ConformationProperties,
-
-    atomCount: number
-}> { }
-
-namespace Model {
-    export const ofMmCIF = buildMmCIF;
-}
-
-export default Model
+export { Model, ModelConstants, ModelDataFormat }

+ 18 - 19
src/mol-data/model/builders/mmcif.ts

@@ -4,9 +4,9 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import { RawData } from '../formats'
-import { Frame as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
-import Model from '../../model'
+import { mmCIF } from '../data-format'
+//import { Frame as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
+import Model from '../model'
 import Column from '../../../mol-base/collections/column'
 import Table from '../../../mol-base/collections/table'
 import { Interval, Segmentation } from '../../../mol-base/collections/integer'
@@ -15,7 +15,7 @@ import * as Hierarchy from '../properties/hierarchy'
 import Conformation from '../properties/conformation'
 import findHierarchyKeys from '../utils/hierarchy-keys'
 
-function findModelBounds(data: mmCIF, startIndex: number) {
+function findModelBounds({ data }: mmCIF, startIndex: number) {
     const num = data.atom_site.pdbx_PDB_model_num;
     const atomCount = num.rowCount;
     if (!num.isDefined) return Interval.ofBounds(startIndex, atomCount);
@@ -24,7 +24,7 @@ function findModelBounds(data: mmCIF, startIndex: number) {
     return Interval.ofBounds(startIndex, endIndex);
 }
 
-function findHierarchyOffsets(data: mmCIF, bounds: Interval) {
+function findHierarchyOffsets({ data }: mmCIF, bounds: Interval) {
     const start = Interval.start(bounds), end = Interval.end(bounds);
     const residues = [start], chains = [start];
 
@@ -44,7 +44,7 @@ function findHierarchyOffsets(data: mmCIF, bounds: Interval) {
     return { residues, chains };
 }
 
-function createHierarchyData(data: mmCIF, bounds: Interval, offsets: { residues: ArrayLike<number>, chains: ArrayLike<number> }): Hierarchy.Data {
+function createHierarchyData({ data }: mmCIF, bounds: Interval, offsets: { residues: ArrayLike<number>, chains: ArrayLike<number> }): Hierarchy.Data {
     const { atom_site } = data;
     const start = Interval.start(bounds), end = Interval.end(bounds);
     const atoms = Table.ofColumns<Hierarchy.AtomsSchema>({
@@ -62,7 +62,7 @@ function createHierarchyData(data: mmCIF, bounds: Interval, offsets: { residues:
     return { atoms, residues, chains, entities: data.entity };
 }
 
-function getConformation(data: mmCIF, bounds: Interval): Conformation {
+function getConformation({ data }: mmCIF, bounds: Interval): Conformation {
     const start = Interval.start(bounds), end = Interval.end(bounds);
     const { atom_site } = data;
     return {
@@ -83,14 +83,14 @@ function isHierarchyDataEqual(a: Hierarchy.Hierarchy, b: Hierarchy.Data) {
         && Table.areEqual(a.atoms as Table<Hierarchy.AtomsSchema>, b.atoms as Table<Hierarchy.AtomsSchema>)
 }
 
-function createModel(raw: RawData, data: mmCIF, bounds: Interval, previous?: Model): Model {
-    const hierarchyOffsets = findHierarchyOffsets(data, bounds);
-    const hierarchyData = createHierarchyData(data, bounds, hierarchyOffsets);
+function createModel(format: mmCIF, bounds: Interval, previous?: Model): Model {
+    const hierarchyOffsets = findHierarchyOffsets(format, bounds);
+    const hierarchyData = createHierarchyData(format, bounds, hierarchyOffsets);
 
     if (previous && isHierarchyDataEqual(previous.hierarchy, hierarchyData)) {
         return {
             ...previous,
-            conformation: getConformation(data, bounds)
+            conformation: getConformation(format, bounds)
         };
     }
 
@@ -102,25 +102,24 @@ function createModel(raw: RawData, data: mmCIF, bounds: Interval, previous?: Mod
 
     return {
         id: newUUID(),
-        sourceData: raw,
-        modelNum: data.atom_site.pdbx_PDB_model_num.value(Interval.start(bounds)),
+        sourceData: format,
+        modelNum: format.data.atom_site.pdbx_PDB_model_num.value(Interval.start(bounds)),
         hierarchy: { ...hierarchyData, ...hierarchyKeys, ...hierarchySegments },
-        conformation: getConformation(data, bounds),
+        conformation: getConformation(format, bounds),
         atomCount: Interval.size(bounds)
     };
 }
 
-function buildModels(data: mmCIF): ReadonlyArray<Model> {
-    const raw: RawData = { source: 'mmCIF', data };
+function buildModels(format: mmCIF): ReadonlyArray<Model> {
     const models: Model[] = [];
-    const atomCount = data.atom_site._rowCount;
+    const atomCount = format.data.atom_site._rowCount;
 
     if (atomCount === 0) return models;
 
     let modelStart = 0;
     while (modelStart < atomCount) {
-        const bounds = findModelBounds(data, modelStart);
-        const model = createModel(raw, data, bounds, models.length > 0 ? models[models.length - 1] : void 0);
+        const bounds = findModelBounds(format, modelStart);
+        const model = createModel(format, bounds, models.length > 0 ? models[models.length - 1] : void 0);
         models.push(model);
         modelStart = Interval.end(bounds);
     }

+ 6 - 2
src/mol-data/model/formats.ts → src/mol-data/model/data-format.ts

@@ -6,5 +6,9 @@
 
 import { Frame as mmCIF_Frame } from '../../mol-io/reader/cif/schema/mmcif'
 
-export type RawData =
-    | { source: 'mmCIF', data: mmCIF_Frame }
+export interface mmCIF { kind: 'mmCIF', data: mmCIF_Frame }
+
+type Format =
+    | mmCIF
+
+export default Format

+ 40 - 0
src/mol-data/model/model.ts

@@ -0,0 +1,40 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import DataFormat from './data-format'
+import HierarchyProperties from './properties/hierarchy'
+import ConformationProperties from './properties/conformation'
+import UUID from '../../mol-base/utils/uuid'
+
+import buildMmCIF from './builders/mmcif'
+
+/**
+ * Interface to the "source data" of the molecule.
+ *
+ * "Atoms" are integers in the range [0, atomCount).
+ */
+interface Model extends Readonly<{
+    id: UUID,
+
+    modelNum: number,
+
+    sourceData: DataFormat,
+
+    hierarchy: HierarchyProperties,
+    conformation: ConformationProperties,
+
+    atomCount: number
+}> { }
+
+namespace Model {
+    export function create(format: DataFormat) {
+        switch (format.kind) {
+            case 'mmCIF': return buildMmCIF(format);
+        }
+    }
+}
+
+export default Model

+ 1 - 1
src/mol-data/structure/structure.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Model from '../model'
+import { Model } from '../model'
 import Unit from './unit'
 import Operator from './operator'
 import AtomSet from './atom/set'

+ 1 - 1
src/mol-data/structure/unit.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Model from '../model'
+import { Model } from '../model'
 import Operator from './operator'
 
 interface Unit extends Readonly<{

+ 2 - 2
src/perf-tests/structure.ts

@@ -10,7 +10,7 @@ import * as util from 'util'
 import * as fs from 'fs'
 import CIF from '../mol-io/reader/cif'
 
-import Model from '../mol-data/Model'
+import { Model } from '../mol-data/Model'
 import { Structure, Atom, AtomSet } from '../mol-data/structure'
 import * as Q from '../mol-data/query'
 import { OrderedSet as OrdSet, Segmentation } from '../mol-base/collections/integer'
@@ -47,7 +47,7 @@ export async function readCIF(path: string) {
     const mmcif = CIF.schema.mmCIF(data);
     console.timeEnd('schema')
     console.time('buildModels')
-    const models = Model.ofMmCIF(mmcif);
+    const models = Model.create({ kind: 'mmCIF', data: mmcif });
     console.timeEnd('buildModels')
     const structures = models.map(Structure.ofModel);
 

+ 1 - 1
src/script.ts

@@ -115,7 +115,7 @@ async function runCIF(input: string | Uint8Array) {
     console.log(mmcif.pdbx_struct_oper_list.matrix.value(0));
 
     console.time('createModels');
-    const models = buildModels(mmcif);
+    const models = buildModels({ kind: 'mmCIF', data: mmcif });
     console.timeEnd('createModels');
 
     for (let i = 0; i < models.length; i++) {