From 727a75ee6a61075b24857b828e7c7b7944f30088 Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Tue, 7 Nov 2017 23:49:00 +0100
Subject: [PATCH] ability to specify default value in column schema

---
 src/mol-data/db/column.ts                      | 18 +++++++++++++-----
 src/mol-io/reader/cif/schema/mmcif.ts          | 16 ++++++++--------
 src/mol-model/structure/model/formats/mmcif.ts |  2 +-
 .../structure/model/properties/hierarchy.ts    |  2 +-
 4 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/src/mol-data/db/column.ts b/src/mol-data/db/column.ts
index b65e45d0c..54fe666de 100644
--- a/src/mol-data/db/column.ts
+++ b/src/mol-data/db/column.ts
@@ -25,6 +25,8 @@ namespace Column {
     export type Schema<T = any> = Schema.Str | Schema.Int | Schema.Float | Schema.Coordinate | Schema.Aliased<T> | Schema.Tensor
 
     export namespace Schema {
+        // T also serves as a default value for undefined columns
+
         type Base<T extends string> = { valueType: T }
         export type Str = { '@type': 'str', T: string } & Base<'str'>
         export type Int = { '@type': 'int', T: number } & Base<'int'>
@@ -39,11 +41,17 @@ namespace Column {
         export const coord: Coordinate = { '@type': 'coord', T: 0, valueType: 'float' };
         export const float: Float = { '@type': 'float', T: 0, valueType: 'float' };
 
-        export function tensor(space: Tensors.Space): Tensor { return { '@type': 'tensor', T: space.create(), space, valueType: 'tensor' }; }
-        export function vector(dim: number): Tensor { return tensor(Tensors.Vector(dim)); }
-        export function matrix(rows: number, cols: number): Tensor { return tensor(Tensors.ColumnMajorMatrix(rows, cols)); }
-
-        export function aliased<T>(t: Str | Int): Aliased<T> { return t as any as Aliased<T>; }
+        export function Str(defaultValue = ''): Str { return { '@type': 'str', T: defaultValue, valueType: 'str' } };
+        export function Int(defaultValue = 0): Int { return { '@type': 'int', T: defaultValue, valueType: 'int' } };
+        export function Float(defaultValue = 0): Float { return { '@type': 'float', T: defaultValue, valueType: 'float' } };
+        export function Tensor(space: Tensors.Space): Tensor { return { '@type': 'tensor', T: space.create(), space, valueType: 'tensor' }; }
+        export function Vector(dim: number): Tensor { return Tensor(Tensors.Vector(dim)); }
+        export function Matrix(rows: number, cols: number): Tensor { return Tensor(Tensors.ColumnMajorMatrix(rows, cols)); }
+
+        export function Aliased<T>(t: Str | Int, defaultValue?: T): Aliased<T> {
+            if (typeof defaultValue !== 'undefined') return { ...t, T: defaultValue } as any as Aliased<T>;
+            return t as any as Aliased<T>;
+        }
     }
 
     export interface ToArrayParams<T> {
diff --git a/src/mol-io/reader/cif/schema/mmcif.ts b/src/mol-io/reader/cif/schema/mmcif.ts
index 13ff7ade9..62f3a395c 100644
--- a/src/mol-io/reader/cif/schema/mmcif.ts
+++ b/src/mol-io/reader/cif/schema/mmcif.ts
@@ -20,7 +20,7 @@ type EntityType = 'polymer' | 'non-polymer' | 'water' | 'macrolide'
 
 const entity = {
     id: str,
-    type: Schema.aliased<EntityType>(str),
+    type: Schema.Aliased<EntityType>(str),
     src_method: str,
     pdbx_description: str,
     formula_weight: float,
@@ -119,7 +119,7 @@ type BondValueOrder =
 
 const struct_conn = {
     id: str,
-    conn_type_id: Schema.aliased<StructConnTypeId>(str),
+    conn_type_id: Schema.Aliased<StructConnTypeId>(str),
     pdbx_PDB_id: str,
     ptnr1_label_asym_id: str,
     ptnr1_label_comp_id: str,
@@ -150,11 +150,11 @@ const struct_conn = {
     pdbx_ptnr3_PDB_ins_code: str,
     details: str,
     pdbx_dist_value: float,
-    pdbx_value_order: Schema.aliased<BondValueOrder>(str)
+    pdbx_value_order: Schema.Aliased<BondValueOrder>(str)
 }
 
 const struct_conn_type = {
-    id: Schema.aliased<StructConnTypeId>(str),
+    id: Schema.Aliased<StructConnTypeId>(str),
     criteria: str,
     reference: str
 }
@@ -163,10 +163,10 @@ const chem_comp_bond = {
     comp_id: str,
     pdbx_stereo_config: str,
     pdbx_ordinal: int,
-    pdbx_aromatic_flag: Schema.aliased<'Y' | 'N'>(str),
+    pdbx_aromatic_flag: Schema.Aliased<'Y' | 'N'>(str),
     atom_id_1: str,
     atom_id_2: str,
-    value_order: Schema.aliased<BondValueOrder>(str)
+    value_order: Schema.Aliased<BondValueOrder>(str)
 }
 
 const pdbx_struct_assembly = {
@@ -188,8 +188,8 @@ const pdbx_struct_oper_list = {
     type: str,
     name: str,
     symmetry_operation: str,
-    matrix: Schema.matrix(3, 3),
-    vector: Schema.vector(3)
+    matrix: Schema.Matrix(3, 3),
+    vector: Schema.Vector(3)
 }
 
 const pdbx_struct_mod_residue = {
diff --git a/src/mol-model/structure/model/formats/mmcif.ts b/src/mol-model/structure/model/formats/mmcif.ts
index aca855e52..b0f421ca8 100644
--- a/src/mol-model/structure/model/formats/mmcif.ts
+++ b/src/mol-model/structure/model/formats/mmcif.ts
@@ -48,7 +48,7 @@ function createHierarchyData({ data }: mmCIF_Format, bounds: Interval, offsets:
     const { atom_site } = data;
     const start = Interval.start(bounds), end = Interval.end(bounds);
     const atoms = Table.ofColumns(Hierarchy.AtomsSchema, {
-        type_symbol: Column.ofArray({ array: Column.mapToArray(Column.window(atom_site.type_symbol, start, end), ElementSymbol), schema: Column.Schema.aliased<ElementSymbol>(Column.Schema.str) }),
+        type_symbol: Column.ofArray({ array: Column.mapToArray(Column.window(atom_site.type_symbol, start, end), ElementSymbol), schema: Column.Schema.Aliased<ElementSymbol>(Column.Schema.str) }),
         label_atom_id: Column.window(atom_site.label_atom_id, start, end),
         auth_atom_id: Column.window(atom_site.auth_atom_id, start, end),
         label_alt_id: Column.window(atom_site.label_alt_id, start, end),
diff --git a/src/mol-model/structure/model/properties/hierarchy.ts b/src/mol-model/structure/model/properties/hierarchy.ts
index f3bec187d..92d5f006a 100644
--- a/src/mol-model/structure/model/properties/hierarchy.ts
+++ b/src/mol-model/structure/model/properties/hierarchy.ts
@@ -10,7 +10,7 @@ import { mmCIF_Schema as mmCIF } from 'mol-io/reader/cif/schema/mmcif'
 import { ElementSymbol} from '../types'
 
 export const AtomsSchema = {
-    type_symbol: Column.Schema.aliased<ElementSymbol>(mmCIF.atom_site.type_symbol),
+    type_symbol: Column.Schema.Aliased<ElementSymbol>(mmCIF.atom_site.type_symbol),
     label_atom_id: mmCIF.atom_site.label_atom_id,
     auth_atom_id: mmCIF.atom_site.auth_atom_id,
     label_alt_id: mmCIF.atom_site.label_alt_id,
-- 
GitLab