Browse Source

wip, strongly typed indexes in Model

David Sehnal 6 years ago
parent
commit
b6e22b7e82

+ 2 - 2
src/mol-data/db/column.ts

@@ -141,8 +141,8 @@ namespace Column {
         return createFirstIndexMapOfColumn(column);
     }
 
-    export function createIndexer<T>(column: Column<T>) {
-        return createIndexerOfColumn(column);
+    export function createIndexer<T, R extends number = number>(column: Column<T>) {
+        return createIndexerOfColumn(column) as ((e: T) => R);
     }
 
     export function mapToArray<T, S>(column: Column<T>, f: (v: T) => S, ctor?: ArrayCtor<S>): ArrayLike<S> {

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

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Model from './model/model'
+import { Model } from './model/model'
 import * as Types from './model/types'
 import Format from './model/format'
 import { ModelSymmetry } from './model/properties/symmetry'

+ 1 - 1
src/mol-model/structure/model/formats/mmcif.ts

@@ -11,7 +11,7 @@ import { Tensor, Vec3 } from 'mol-math/linear-algebra';
 import { Task, RuntimeContext } from 'mol-task';
 import UUID from 'mol-util/uuid';
 import Format from '../format';
-import Model from '../model';
+import { Model } from '../model';
 import { Entities } from '../properties/common';
 import { CustomProperties } from '../properties/custom';
 import { ModelSymmetry } from '../properties/symmetry';

+ 1 - 1
src/mol-model/structure/model/formats/mmcif/atomic.ts

@@ -10,7 +10,7 @@ import { mmCIF_Database } from 'mol-io/reader/cif/schema/mmcif';
 import UUID from 'mol-util/uuid';
 import { Element } from '../../../../structure';
 import Format from '../../format';
-import Model from '../../model';
+import { Model } from '../../model';
 import { AtomicConformation, AtomicData, AtomicHierarchy, AtomicSegments, AtomsSchema, ChainsSchema, ResiduesSchema } from '../../properties/atomic';
 import { getAtomicKeys } from '../../properties/utils/atomic-keys';
 import { ElementSymbol } from '../../types';

+ 1 - 1
src/mol-model/structure/model/formats/mmcif/bonds/comp.ts

@@ -5,7 +5,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import Model from '../../../model'
+import { Model } from '../../../model'
 import { LinkType } from '../../../types'
 import { ModelPropertyDescriptor } from '../../../properties/custom';
 import { mmCIF_Database } from 'mol-io/reader/cif/schema/mmcif';

+ 1 - 1
src/mol-model/structure/model/formats/mmcif/bonds/struct_conn.ts

@@ -5,7 +5,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import Model from '../../../model'
+import { Model } from '../../../model'
 import { Element, Structure } from '../../../../structure'
 import { LinkType } from '../../../types'
 import { findEntityIdByAsymId, findAtomIndexByLabelName } from '../util'

+ 1 - 1
src/mol-model/structure/model/formats/mmcif/pair-restraints/cross-links.ts

@@ -4,7 +4,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import Model from '../../../model'
+import { Model } from '../../../model'
 import { Table } from 'mol-data/db'
 import { mmCIF_Schema } from 'mol-io/reader/cif/schema/mmcif';
 import { findAtomIndexByLabelName } from '../util';

+ 1 - 1
src/mol-model/structure/model/formats/mmcif/util.ts

@@ -4,7 +4,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import Model from '../../model'
+import { Model } from '../../model'
 import { Element } from '../../../structure'
 
 export function findEntityIdByAsymId(model: Model, asymId: string) {

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

@@ -22,7 +22,7 @@ import { ChemicalComponent } from './properties/chemical-component';
  *
  * "Atoms" are integers in the range [0, atomCount).
  */
-interface Model extends Readonly<{
+export interface Model extends Readonly<{
     id: UUID,
     label: string,
 
@@ -64,25 +64,17 @@ interface Model extends Readonly<{
 
 } { }
 
-namespace Model {
+export namespace Model {
     export function create(format: Format) {
         switch (format.kind) {
             // case 'gro': return from_gro(format);
             case 'mmCIF': return from_mmCIF(format);
         }
     }
-
-    // TODO: figure the place to include this?
-    // export interface Property<T, K> { (model: Model, index: number): T, _kind: K }
-    // export interface AtomicProperty<T> extends Property<T, 'atomic'> { }
-    // export interface CoarseProperty<T> extends Property<T, 'coarse'> { }
-    // export interface SphereProperty<T> extends Property<T, 'sphere'> { }
-    // export interface GaussianProperty<T> extends Property<T, 'gaussian'> { }
-
-    // export function atomProp<T>(p: (model: Model, i: number) => T): AtomicProperty<T> { return p as any; }
-    // export function residueProp<T>(p: (model: Model, residueIndex: number) => T): AtomicProperty<T> {
-    //     return p as any;
-    // }
 }
 
-export default Model
+
+export type ElementIndex = { readonly '@type': 'element-index' } & number
+export type ResidueIndex = { readonly '@type': 'residue-index' } & number
+export type ChainIndex = { readonly '@type': 'chain-index' } & number
+export type EntityIndex = { readonly '@type': 'entity-index' } & number

+ 7 - 1
src/mol-model/structure/model/properties/atomic/hierarchy.ts

@@ -80,9 +80,15 @@ export interface AtomicKeys {
     // also index to the Entities table.
     entityKey: ArrayLike<number>,
 
+    /**
+     * @returns index or -1 if not present.
+     */
     findChainKey(entityId: string, label_asym_id: string): number,
 
-    /** Unique number for each of the residue. Also the index of the 1st occurence of this residue. */
+    /**
+     * Unique number for each of the residue. Also the index of the 1st occurence of this residue.
+     * @returns index or -1 if not present.
+     */
     findResidueKey(entityId: string, label_asym_id: string, label_comp_id: string, auth_seq_id: number, pdbx_PDB_ins_code: string): number
 }
 

+ 1 - 1
src/mol-model/structure/model/properties/common.ts

@@ -8,5 +8,5 @@ import { mmCIF_Database as mmCIF } from 'mol-io/reader/cif/schema/mmcif'
 
 export interface Entities {
     data: mmCIF['entity'],
-    getEntityIndex(id: string): number
+    getEntityIndex(id: string): import('../model').EntityIndex
 }