From b6e22b7e821335672168bb907b8d750b05f0bf81 Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Wed, 11 Jul 2018 11:30:01 +0200
Subject: [PATCH] wip, strongly typed indexes in Model

---
 src/mol-data/db/column.ts                     |  4 ++--
 src/mol-model/structure/model.ts              |  2 +-
 .../structure/model/formats/mmcif.ts          |  2 +-
 .../structure/model/formats/mmcif/atomic.ts   |  2 +-
 .../model/formats/mmcif/bonds/comp.ts         |  2 +-
 .../model/formats/mmcif/bonds/struct_conn.ts  |  2 +-
 .../mmcif/pair-restraints/cross-links.ts      |  2 +-
 .../structure/model/formats/mmcif/util.ts     |  2 +-
 src/mol-model/structure/model/model.ts        | 22 ++++++-------------
 .../model/properties/atomic/hierarchy.ts      |  8 ++++++-
 .../structure/model/properties/common.ts      |  2 +-
 11 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/src/mol-data/db/column.ts b/src/mol-data/db/column.ts
index d1c183b4c..55243135f 100644
--- a/src/mol-data/db/column.ts
+++ b/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> {
diff --git a/src/mol-model/structure/model.ts b/src/mol-model/structure/model.ts
index 0551a4c20..af5b8c7bb 100644
--- a/src/mol-model/structure/model.ts
+++ b/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'
diff --git a/src/mol-model/structure/model/formats/mmcif.ts b/src/mol-model/structure/model/formats/mmcif.ts
index 17b7ee9cf..46fd62b3e 100644
--- a/src/mol-model/structure/model/formats/mmcif.ts
+++ b/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';
diff --git a/src/mol-model/structure/model/formats/mmcif/atomic.ts b/src/mol-model/structure/model/formats/mmcif/atomic.ts
index 5ab6ded5d..d9ddcd2b0 100644
--- a/src/mol-model/structure/model/formats/mmcif/atomic.ts
+++ b/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';
diff --git a/src/mol-model/structure/model/formats/mmcif/bonds/comp.ts b/src/mol-model/structure/model/formats/mmcif/bonds/comp.ts
index 6a3f12ffe..77ab1f084 100644
--- a/src/mol-model/structure/model/formats/mmcif/bonds/comp.ts
+++ b/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';
diff --git a/src/mol-model/structure/model/formats/mmcif/bonds/struct_conn.ts b/src/mol-model/structure/model/formats/mmcif/bonds/struct_conn.ts
index f2ea4703a..2d79a18b9 100644
--- a/src/mol-model/structure/model/formats/mmcif/bonds/struct_conn.ts
+++ b/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'
diff --git a/src/mol-model/structure/model/formats/mmcif/pair-restraints/cross-links.ts b/src/mol-model/structure/model/formats/mmcif/pair-restraints/cross-links.ts
index 584a429cc..835b4c440 100644
--- a/src/mol-model/structure/model/formats/mmcif/pair-restraints/cross-links.ts
+++ b/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';
diff --git a/src/mol-model/structure/model/formats/mmcif/util.ts b/src/mol-model/structure/model/formats/mmcif/util.ts
index 7c4806cc1..5747eb166 100644
--- a/src/mol-model/structure/model/formats/mmcif/util.ts
+++ b/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) {
diff --git a/src/mol-model/structure/model/model.ts b/src/mol-model/structure/model/model.ts
index 24e5a5875..412531f73 100644
--- a/src/mol-model/structure/model/model.ts
+++ b/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
\ No newline at end of file
+
+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
\ No newline at end of file
diff --git a/src/mol-model/structure/model/properties/atomic/hierarchy.ts b/src/mol-model/structure/model/properties/atomic/hierarchy.ts
index 2d2612a75..a4aeaacad 100644
--- a/src/mol-model/structure/model/properties/atomic/hierarchy.ts
+++ b/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
 }
 
diff --git a/src/mol-model/structure/model/properties/common.ts b/src/mol-model/structure/model/properties/common.ts
index 9dadbc95a..35d9143ae 100644
--- a/src/mol-model/structure/model/properties/common.ts
+++ b/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
 }
\ No newline at end of file
-- 
GitLab