From ab2031f79bffb19f21fff5f6a74084765cbb3b63 Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Mon, 30 Oct 2017 17:27:23 +0100
Subject: [PATCH] data model (part 2 of ???)

---
 src/mol-base/utils/bit-flags.ts               |  13 ++
 src/mol-data/model.ts                         |   4 +-
 src/mol-data/model/properties/basic.ts        |  28 ----
 src/mol-data/model/properties/conformation.ts |  20 ++-
 src/mol-data/model/properties/hierarchy.ts    |  62 +++++++++
 .../model/properties/macromolecule.ts         |  72 ----------
 .../model/properties/secondary-structure.ts   | 128 ++++++++++++++++++
 src/mol-data/model/properties/symmetry.ts     |   0
 src/mol-data/model/properties/transforms.ts   |   9 ++
 src/mol-data/query.ts                         |   2 +-
 src/mol-data/{ => query}/selection.ts         |   2 +-
 11 files changed, 232 insertions(+), 108 deletions(-)
 create mode 100644 src/mol-base/utils/bit-flags.ts
 delete mode 100644 src/mol-data/model/properties/basic.ts
 create mode 100644 src/mol-data/model/properties/hierarchy.ts
 delete mode 100644 src/mol-data/model/properties/macromolecule.ts
 create mode 100644 src/mol-data/model/properties/secondary-structure.ts
 delete mode 100644 src/mol-data/model/properties/symmetry.ts
 create mode 100644 src/mol-data/model/properties/transforms.ts
 rename src/mol-data/{ => query}/selection.ts (94%)

diff --git a/src/mol-base/utils/bit-flags.ts b/src/mol-base/utils/bit-flags.ts
new file mode 100644
index 000000000..e5cd20290
--- /dev/null
+++ b/src/mol-base/utils/bit-flags.ts
@@ -0,0 +1,13 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+interface BitFlags<Flags> { '@type': Flags }
+
+namespace BitFlags {
+    export function has<F>(flags: BitFlags<F>, flag: F) { return ((flags as any) & (flag as any)) !== 0; }
+}
+
+export default BitFlags
\ No newline at end of file
diff --git a/src/mol-data/model.ts b/src/mol-data/model.ts
index 2d238f801..3572e7618 100644
--- a/src/mol-data/model.ts
+++ b/src/mol-data/model.ts
@@ -6,7 +6,7 @@
 
 import * as Formats from './model/formats'
 //import CommonProperties from './model/properties/common'
-import MacromoleculeProperties from './model/properties/macromolecule'
+import HierarchyProperties from './model/properties/hierarchy'
 import Conformation from './model/properties/conformation'
 import Segmentation from '../mol-base/collections/integer/segmentation'
 
@@ -23,7 +23,7 @@ interface Model extends Readonly<{
     sourceData: Formats.RawData,
 
     //common: CommonProperties,
-    macromolecule: MacromoleculeProperties,
+    macromolecule: HierarchyProperties,
     conformation: Conformation,
 
     // used for diffing.
diff --git a/src/mol-data/model/properties/basic.ts b/src/mol-data/model/properties/basic.ts
deleted file mode 100644
index 1837912b6..000000000
--- a/src/mol-data/model/properties/basic.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author David Sehnal <david.sehnal@gmail.com>
- */
-
-import Column from '../../../mol-base/collections/column'
-
-export type Table<Data> = { [E in keyof Data]: Column<Data[E]> }
-
-export interface ElementSymbol extends String { '@type': 'element-symbol' }
-export function ElementSymbol(s: string): ElementSymbol {
-    // TODO: optimize?
-    return s.toUpperCase() as any;
-}
-
-
-export interface Atoms extends Table<{
-    name: string,
-    elementSymbol: ElementSymbol,
-    
-}> { }
- 
-interface Common {
-
-}
-
-export default Common
\ No newline at end of file
diff --git a/src/mol-data/model/properties/conformation.ts b/src/mol-data/model/properties/conformation.ts
index 7c18e8ab5..ac90abac4 100644
--- a/src/mol-data/model/properties/conformation.ts
+++ b/src/mol-data/model/properties/conformation.ts
@@ -4,13 +4,25 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-interface Conformation {
+import { SecondaryStructureFlag as SSF } from './secondary-structure'
+import BitFlags from '../../../mol-base/utils/bit-flags'
+import Segmentation from '../../../mol-base/collections/integer/segmentation'
+
+interface Positions {
     x: ArrayLike<number>,
     y: ArrayLike<number>,
-    z: ArrayLike<number>,
+    z: ArrayLike<number>
+}
 
-    // Assign a secondary structure type to each residue.
-    secondaryStructureType: ArrayLike<any>
+interface SecondaryStructure {
+    ofResidue: ArrayLike<BitFlags<SSF>>,
+    // atom segmentation
+    segments: Segmentation
+}
+
+interface Conformation {
+    positions: Positions,
+    secondaryStructureType: SecondaryStructure
 }
 
 export default Conformation
diff --git a/src/mol-data/model/properties/hierarchy.ts b/src/mol-data/model/properties/hierarchy.ts
new file mode 100644
index 000000000..427b2a1bc
--- /dev/null
+++ b/src/mol-data/model/properties/hierarchy.ts
@@ -0,0 +1,62 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import Column from '../../../mol-base/collections/column'
+import { Shape as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
+
+export interface ElementSymbol extends String { '@type': 'element-symbol' }
+export function ElementSymbol(s: string): ElementSymbol {
+    // TODO: optimize?
+    return s.toUpperCase() as any;
+}
+
+type Key = { key: Column<number> }
+
+type _Atoms = Pick<mmCIF['atom_site'],
+    | 'type_symbol'
+    | 'label_atom_id'
+    | 'auth_atom_id'
+    | 'label_alt_id'
+    | 'pdbx_formal_charge'
+    | 'occupancy'
+    | 'B_iso_or_equiv'>
+    & Key
+export interface Atoms extends _Atoms {
+    source_row: Column<number>
+}
+
+type _Residues = Pick<mmCIF['atom_site'],
+    | 'group_PDB'
+    | 'label_comp_id'
+    | 'auth_comp_id'
+    | 'label_seq_id'
+    | 'auth_seq_id'
+    | 'pdbx_PDB_ins_code'>
+    & Key
+export interface Residues extends _Residues { }
+
+type _Chains = Pick<mmCIF['atom_site'],
+    | 'label_asym_id'
+    | 'auth_asym_id'
+    | 'auth_comp_id'
+    | 'label_entity_id'
+    | 'pdbx_PDB_model_num'>
+    & Key
+export interface Chains extends _Chains {
+    enityDataIndex: Column<number>
+}
+
+type _EntityData = mmCIF['entity']
+export interface EntityData extends _EntityData { }
+
+export interface Macromolecule {
+    atoms: Atoms,
+    residues: Residues,
+    chains: Chains,
+    entityData: EntityData
+}
+
+export default Macromolecule
\ No newline at end of file
diff --git a/src/mol-data/model/properties/macromolecule.ts b/src/mol-data/model/properties/macromolecule.ts
deleted file mode 100644
index 1285ba4d3..000000000
--- a/src/mol-data/model/properties/macromolecule.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-/**
- * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author David Sehnal <david.sehnal@gmail.com>
- */
-
-import Column from '../../../mol-base/collections/column'
-import { Shape as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
-
-export type Table<Data> = { [E in keyof Data]: Column<Data[E]> }
-
-export interface ElementSymbol extends String { '@type': 'element-symbol' }
-export function ElementSymbol(s: string): ElementSymbol {
-    // TODO: optimize?
-    return s.toUpperCase() as any;
-}
-
-export interface Atoms extends Table<{
-    // unique number for each atom
-    key: number,
-
-    id: number,
-    type_symbol: ElementSymbol,
-    label_atom_id: string,
-    auth_atom_id: string,
-    label_alt_id: string,
-    auth_alt_id: string,
-    pdbx_formal_charge: string,
-    occupancy: number,
-    B_iso_or_equiv: number
-}> { }
-
-export interface Residues extends Table<{
-    // unique number for each residue
-    key: number,
-
-    group_PDB: string,
-
-    label_comp_id: string,
-    auth_comp_id: string,
-    label_seq_id: number,
-    auth_seq_id: number,
-    pdbx_PDB_ins_code: string
-}> { }
-
-export interface Chains extends Table<{
-    // unique number for each chain
-    key: number,
-
-    label_asym_id: string,
-    auth_asym_id: string
-}> { }
-
-export interface Entities extends Table<{
-    // unique number for each entity
-    // row index to the EntityData table
-    key: number,
-    label_entity_id: string,
-    pdbx_PDB_model_num: number
-}> { }
-
-type _EntityData = mmCIF['entity']
-export interface EntityData extends _EntityData { }
-
-export interface Macromolecule {
-    atoms: Atoms,
-    residues: Residues,
-    chains: Chains,
-    entities: Entities
-}
-
-export default Macromolecule
\ No newline at end of file
diff --git a/src/mol-data/model/properties/secondary-structure.ts b/src/mol-data/model/properties/secondary-structure.ts
new file mode 100644
index 000000000..75d1f4b89
--- /dev/null
+++ b/src/mol-data/model/properties/secondary-structure.ts
@@ -0,0 +1,128 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ */
+
+const enum SSF {
+    None = 0x0,
+
+    // category
+    DoubleHelix = 0x1,
+    Helix = 0x2,
+    Beta = 0x4,
+    Turn = 0x8,
+
+    // category variant
+    LeftHanded = 0x10,  // helix
+    RightHanded = 0x20,
+
+    ClassicTurn = 0x40,  // turn
+    InverseTurn = 0x80,
+
+    // sub-category
+    HelixOther = 0x100,  // protein
+    Helix27 = 0x200,
+    Helix3Ten = 0x400,
+    HelixAlpha = 0x800,
+    HelixGamma = 0x1000,
+    HelixOmega = 0x2000,
+    HelixPi = 0x4000,
+    HelixPolyproline = 0x8000,
+
+    DoubleHelixOther = 0x10000,  // nucleic
+    DoubleHelixZ = 0x20000,
+    DoubleHelixA = 0x40000,
+    DoubleHelixB = 0x80000,
+
+    BetaOther = 0x100000,  // protein
+    BetaStrand = 0x200000,  // single strand
+    BetaSheet = 0x400000,  // multiple hydrogen bonded strands
+    BetaBarell = 0x800000,  // closed series of sheets
+
+    TurnOther = 0x1000000,  // protein
+    Turn1 = 0x2000000,
+    Turn2 = 0x4000000,
+    Turn3 = 0x8000000,
+
+    NA = 0x10000000,  // not applicable/available
+}
+export { SSF as SecondaryStructureFlag }
+
+export const SecondaryStructureMmcif: { [value: string]: number } = {
+    HELX_LH_27_P: SSF.Helix | SSF.LeftHanded | SSF.Helix27,  // left-handed 2-7 helix (protein)
+    HELX_LH_3T_P: SSF.Helix | SSF.LeftHanded | SSF.Helix3Ten,  // left-handed 3-10 helix (protein)
+    HELX_LH_AL_P: SSF.Helix | SSF.LeftHanded | SSF.HelixAlpha,  // left-handed alpha helix (protein)
+    HELX_LH_A_N: SSF.DoubleHelix | SSF.LeftHanded | SSF.DoubleHelixA,  // left-handed A helix (nucleic acid)
+    HELX_LH_B_N: SSF.DoubleHelix | SSF.LeftHanded | SSF.DoubleHelixB,  // left-handed B helix (nucleic acid)
+    HELX_LH_GA_P: SSF.Helix | SSF.LeftHanded | SSF.HelixGamma,  // left-handed gamma helix (protein)
+    HELX_LH_N: SSF.DoubleHelix | SSF.LeftHanded,  // left-handed helix with type not specified (nucleic acid)
+    HELX_LH_OM_P: SSF.Helix | SSF.LeftHanded | SSF.HelixOmega,  // left-handed omega helix (protein)
+    HELX_LH_OT_N: SSF.DoubleHelix | SSF.LeftHanded | SSF.DoubleHelixOther,  // left-handed helix with type that does not conform to an accepted category (nucleic acid)
+    HELX_LH_OT_P: SSF.Helix | SSF.LeftHanded | SSF.HelixOther,  // left-handed helix with type that does not conform to an accepted category (protein)
+    HELX_LH_P: SSF.Helix | SSF.LeftHanded,  // left-handed helix with type not specified (protein)
+    HELX_LH_PI_P: SSF.Helix | SSF.LeftHanded | SSF.HelixPi,  // left-handed pi helix (protein)
+    HELX_LH_PP_P: SSF.Helix | SSF.LeftHanded | SSF.HelixPolyproline,  // left-handed polyproline helix (protein)
+    HELX_LH_Z_N: SSF.DoubleHelix | SSF.LeftHanded | SSF.DoubleHelixZ,  // left-handed Z helix (nucleic acid)
+    HELX_N: SSF.DoubleHelix,  // helix with handedness and type not specified (nucleic acid)
+    HELX_OT_N: SSF.DoubleHelix,  // helix with handedness and type that do not conform to an accepted category (nucleic acid)
+    HELX_OT_P: SSF.Helix,  // helix with handedness and type that do not conform to an accepted category (protein)
+    HELX_P: SSF.Helix,  // helix with handedness and type not specified (protein)
+    HELX_RH_27_P: SSF.Helix | SSF.RightHanded | SSF.Helix27,  // right-handed 2-7 helix (protein)
+    HELX_RH_3T_P: SSF.Helix | SSF.RightHanded | SSF.Helix3Ten,  // right-handed 3-10 helix (protein)
+    HELX_RH_AL_P: SSF.Helix | SSF.RightHanded | SSF.HelixAlpha,  // right-handed alpha helix (protein)
+    HELX_RH_A_N: SSF.DoubleHelix | SSF.RightHanded | SSF.DoubleHelixA,  // right-handed A helix (nucleic acid)
+    HELX_RH_B_N: SSF.DoubleHelix | SSF.RightHanded | SSF.DoubleHelixB,  // right-handed B helix (nucleic acid)
+    HELX_RH_GA_P: SSF.Helix | SSF.RightHanded | SSF.HelixGamma,  // right-handed gamma helix (protein)
+    HELX_RH_N: SSF.DoubleHelix | SSF.RightHanded,  // right-handed helix with type not specified (nucleic acid)
+    HELX_RH_OM_P: SSF.Helix | SSF.RightHanded | SSF.HelixOmega,  // right-handed omega helix (protein)
+    HELX_RH_OT_N: SSF.DoubleHelix | SSF.RightHanded | SSF.DoubleHelixOther,  // right-handed helix with type that does not conform to an accepted category (nucleic acid)
+    HELX_RH_OT_P: SSF.Helix | SSF.RightHanded | SSF.HelixOther,  // right-handed helix with type that does not conform to an accepted category (protein)
+    HELX_RH_P: SSF.Helix | SSF.RightHanded,  // right-handed helix with type not specified (protein)
+    HELX_RH_PI_P: SSF.Helix | SSF.RightHanded | SSF.HelixPi,  // right-handed pi helix (protein)
+    HELX_RH_PP_P: SSF.Helix | SSF.RightHanded | SSF.HelixPolyproline,  // right-handed polyproline helix (protein)
+    HELX_RH_Z_N: SSF.DoubleHelix | SSF.RightHanded | SSF.DoubleHelixZ,  // right-handed Z helix (nucleic acid)
+    STRN: SSF.Beta | SSF.BetaStrand,  // beta strand (protein)
+    TURN_OT_P: SSF.Turn | SSF.TurnOther,  // turn with type that does not conform to an accepted category (protein)
+    TURN_P: SSF.Turn,  // turn with type not specified (protein)
+    TURN_TY1P_P: SSF.Turn | SSF.InverseTurn | SSF.Turn1,  // type I prime turn (protein)
+    TURN_TY1_P: SSF.Turn | SSF.ClassicTurn | SSF.Turn1,  // type I turn (protein)
+    TURN_TY2P_P: SSF.Turn | SSF.InverseTurn | SSF.Turn2,  // type II prime turn (protein)
+    TURN_TY2_P: SSF.Turn | SSF.ClassicTurn | SSF.Turn2,  // type II turn (protein)
+    TURN_TY3P_P: SSF.Turn | SSF.InverseTurn | SSF.Turn3,  // type III prime turn (protein)
+    TURN_TY3_P: SSF.Turn | SSF.ClassicTurn | SSF.Turn3,  // type III turn (protein)
+}
+
+export const SecondaryStructurePdb: { [value: string]: number } = {
+    1: SSF.Helix | SSF.RightHanded | SSF.HelixAlpha,  // Right-handed alpha (default)
+    2: SSF.Helix | SSF.RightHanded | SSF.HelixOmega,  // Right-handed omega
+    3: SSF.Helix | SSF.RightHanded | SSF.HelixPi,  // Right-handed pi
+    4: SSF.Helix | SSF.RightHanded | SSF.HelixGamma,  // Right-handed gamma
+    5: SSF.Helix | SSF.RightHanded | SSF.Helix3Ten,  // Right-handed 310
+    6: SSF.Helix | SSF.LeftHanded | SSF.HelixAlpha,  // Left-handed alpha
+    7: SSF.Helix | SSF.LeftHanded | SSF.HelixOmega,  // Left-handed omega
+    8: SSF.Helix | SSF.LeftHanded | SSF.HelixGamma,  // Left-handed gamma
+    9: SSF.Helix | SSF.Helix27,  // 27 ribbon/helix
+    10: SSF.Helix | SSF.HelixPolyproline,  // Polyproline
+}
+
+export const SecondaryStructureStride: { [value: string]: number } = {
+    H: SSF.Helix | SSF.HelixAlpha,  // Alpha helix
+    G: SSF.Helix | SSF.Helix3Ten,  // 3-10 helix
+    I: SSF.Helix | SSF.HelixPi,  // PI-helix
+    E: SSF.Beta | SSF.BetaSheet,  // Extended conformation
+    B: SSF.Beta | SSF.BetaStrand,  // Isolated bridge
+    b: SSF.Beta | SSF.BetaStrand,  // Isolated bridge
+    T: SSF.Turn,  // Turn
+    C: SSF.NA,  // Coil (none of the above)
+}
+
+export const SecondaryStructureDssp: { [value: string]: number } = {
+    H: SSF.Helix | SSF.HelixAlpha,  // alpha-helix
+    B: SSF.Beta | SSF.BetaStrand,  // residue in isolated beta-bridge
+    E: SSF.Beta | SSF.BetaSheet,  // extended strand, participates in beta ladder
+    G: SSF.Helix | SSF.Helix3Ten,  // 3-helix (310 helix)
+    I: SSF.Helix | SSF.HelixPi,  // 5 helix (pi-helix)
+    T: SSF.Turn,  // hydrogen bonded turn
+    S: SSF.Turn,  // bend
+}
\ No newline at end of file
diff --git a/src/mol-data/model/properties/symmetry.ts b/src/mol-data/model/properties/symmetry.ts
deleted file mode 100644
index e69de29bb..000000000
diff --git a/src/mol-data/model/properties/transforms.ts b/src/mol-data/model/properties/transforms.ts
new file mode 100644
index 000000000..f43641255
--- /dev/null
+++ b/src/mol-data/model/properties/transforms.ts
@@ -0,0 +1,9 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+// TODO: symmetry and assebmlies descriptors
+
+//import Column from '../../../mol-base/collections/column'
\ No newline at end of file
diff --git a/src/mol-data/query.ts b/src/mol-data/query.ts
index d99fbd941..89aaef1c2 100644
--- a/src/mol-data/query.ts
+++ b/src/mol-data/query.ts
@@ -5,7 +5,7 @@
  */
 
 import Structure from './structure'
-import Selection from './selection'
+import Selection from './query/selection'
 
 interface Query { (s: Structure): Selection }
 
diff --git a/src/mol-data/selection.ts b/src/mol-data/query/selection.ts
similarity index 94%
rename from src/mol-data/selection.ts
rename to src/mol-data/query/selection.ts
index 6364aa263..a509c40fe 100644
--- a/src/mol-data/selection.ts
+++ b/src/mol-data/query/selection.ts
@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Structure from './structure'
+import Structure from './../structure'
 
 type Selection =
     | Structure // each atom is interpreted as a singleton structure
-- 
GitLab