From d7cc97d4cb2c88f9a73de0454797a4dd247d01cd Mon Sep 17 00:00:00 2001
From: Alexander Rose <alex.rose@rcsb.org>
Date: Mon, 23 Apr 2018 11:44:02 -0700
Subject: [PATCH] wip, coloring

---
 src/apps/render-test/state.ts                 |  8 +--
 src/mol-geo/representation/structure/point.ts |  2 +-
 .../representation/structure/spacefill.ts     |  2 +-
 src/mol-geo/representation/structure/utils.ts | 30 ++++-------
 src/mol-geo/theme/index.ts                    |  2 +-
 .../theme/structure/color/atom-index.ts       | 24 +++++++++
 src/mol-geo/theme/structure/color/chain-id.ts | 51 +++++++++++++++++++
 .../color/{element.ts => element-symbol.ts}   |  0
 src/mol-geo/theme/structure/color/index.ts    |  7 ++-
 .../theme/structure/color/instance-index.ts   | 21 ++++++++
 src/mol-geo/theme/structure/size/index.ts     |  4 +-
 11 files changed, 123 insertions(+), 28 deletions(-)
 create mode 100644 src/mol-geo/theme/structure/color/atom-index.ts
 create mode 100644 src/mol-geo/theme/structure/color/chain-id.ts
 rename src/mol-geo/theme/structure/color/{element.ts => element-symbol.ts} (100%)
 create mode 100644 src/mol-geo/theme/structure/color/instance-index.ts

diff --git a/src/apps/render-test/state.ts b/src/apps/render-test/state.ts
index 212a941cb..e7cb9fab2 100644
--- a/src/apps/render-test/state.ts
+++ b/src/apps/render-test/state.ts
@@ -49,7 +49,8 @@ export default class State {
 
         const structPointRepr = StructureRepresentation(Point)
         const pointProps: PointProps = {
-            colorTheme: { name: 'uniform', value: 0xFF4411 },
+            // colorTheme: { name: 'uniform', value: 0xFF4411 },
+            colorTheme: { name: 'chain-id' },
             sizeTheme: { name: 'uniform', value: 0.1 }
         }
         await Run(structPointRepr.create(struct, pointProps), log, 100)
@@ -59,9 +60,10 @@ export default class State {
         const spacefillProps: SpacefillProps = {
             detail: 1,
             // colorTheme: { name: 'uniform', value: 0xFF4411 },
-            // colorTheme: { name: 'instance-id' },
+            // colorTheme: { name: 'instance-index' },
             // colorTheme: { name: 'element-symbol' },
-            colorTheme: { name: 'atom-id' },
+            // colorTheme: { name: 'atom-index' },
+            colorTheme: { name: 'chain-id' },
         }
         await Run(structSpacefillRepr.create(struct, spacefillProps), log, 100)
         structSpacefillRepr.renderObjects.forEach(viewer.add)
diff --git a/src/mol-geo/representation/structure/point.ts b/src/mol-geo/representation/structure/point.ts
index 0555750a0..8735c23d8 100644
--- a/src/mol-geo/representation/structure/point.ts
+++ b/src/mol-geo/representation/structure/point.ts
@@ -19,7 +19,7 @@ import { ColorTheme, SizeTheme } from '../../theme';
 import { createTransforms, createColors, createSizes } from './utils';
 
 export const DefaultPointProps = {
-    colorTheme: { name: 'instance-id' } as ColorTheme,
+    colorTheme: { name: 'instance-index' } as ColorTheme,
     sizeTheme: { name: 'vdw' } as SizeTheme
 }
 export type PointProps = Partial<typeof DefaultPointProps>
diff --git a/src/mol-geo/representation/structure/spacefill.ts b/src/mol-geo/representation/structure/spacefill.ts
index 2cdc6d5e8..5778ecee6 100644
--- a/src/mol-geo/representation/structure/spacefill.ts
+++ b/src/mol-geo/representation/structure/spacefill.ts
@@ -21,7 +21,7 @@ import VertexMap from '../../shape/vertex-map';
 
 export const DefaultSpacefillProps = {
     detail: 0,
-    colorTheme: { name: 'instance-id' } as ColorTheme,
+    colorTheme: { name: 'instance-index' } as ColorTheme,
 }
 export type SpacefillProps = Partial<typeof DefaultSpacefillProps>
 
diff --git a/src/mol-geo/representation/structure/utils.ts b/src/mol-geo/representation/structure/utils.ts
index a0eb47202..a7a1d1587 100644
--- a/src/mol-geo/representation/structure/utils.ts
+++ b/src/mol-geo/representation/structure/utils.ts
@@ -7,14 +7,12 @@
 import { Unit, ElementGroup } from 'mol-model/structure';
 import { Mat4 } from 'mol-math/linear-algebra'
 
-import { ColorScale } from 'mol-util/color';
-import { createUniformColor, createInstanceColor, createElementInstanceColor } from '../../util/color-data';
+import { createUniformColor } from '../../util/color-data';
 import { createUniformSize } from '../../util/size-data';
 import { vdwSizeData } from '../../theme/structure/size/vdw';
 import VertexMap from '../../shape/vertex-map';
 import { ColorTheme, SizeTheme } from '../../theme';
-import { elementSymbolColorData } from '../../theme/structure/color/element';
-import { OrderedSet } from 'mol-data/int';
+import { atomIndexColorData, elementSymbolColorData, instanceIndexColorData, chainIdColorData } from '../../theme/structure/color';
 
 export function createTransforms(units: ReadonlyArray<Unit>) {
     const unitCount = units.length
@@ -26,25 +24,17 @@ export function createTransforms(units: ReadonlyArray<Unit>) {
 }
 
 export function createColors(units: ReadonlyArray<Unit>, elementGroup: ElementGroup, vertexMap: VertexMap, props: ColorTheme) {
-    const instanceCount = units.length
-    const elementCount = OrderedSet.size(elementGroup.elements)
     switch (props.name) {
-        case 'uniform':
-            return createUniformColor(props)
-        case 'instance-id':
-            const instanceDomain = props.domain ? props.domain : [ 0, instanceCount - 1 ]
-            const instanceScale = ColorScale.create({ domain: instanceDomain })
-            return createInstanceColor({ colorFn: instanceScale.color, instanceCount })
-        case 'atom-id':
-            const atomDomain = props.domain ? props.domain : [ 0, instanceCount * elementCount - 1 ]
-            const atomScale = ColorScale.create({ domain: atomDomain })
-            return createElementInstanceColor({
-                colorFn: (unitIdx, elementIdx) => atomScale.color(unitIdx * elementCount + elementIdx),
-                instanceCount,
-                vertexMap
-            })
+        case 'atom-index':
+            return atomIndexColorData({ units, elementGroup, vertexMap })
+        case 'chain-id':
+            return chainIdColorData({ units, elementGroup, vertexMap })
         case 'element-symbol':
             return elementSymbolColorData({ units, elementGroup, vertexMap })
+        case 'instance-index':
+            return instanceIndexColorData({ units, elementGroup, vertexMap })
+        case 'uniform':
+            return createUniformColor(props)
     }
 }
 
diff --git a/src/mol-geo/theme/index.ts b/src/mol-geo/theme/index.ts
index 70eaa22d9..452ff76ed 100644
--- a/src/mol-geo/theme/index.ts
+++ b/src/mol-geo/theme/index.ts
@@ -12,7 +12,7 @@ export interface UniformColorTheme {
 }
 
 export interface ScaleColorTheme {
-    name: 'instance-id' | 'element-symbol' | 'atom-id'
+    name:  'atom-index' | 'chain-id' | 'element-symbol' | 'instance-index'
     domain?: [number, number]
 }
 
diff --git a/src/mol-geo/theme/structure/color/atom-index.ts b/src/mol-geo/theme/structure/color/atom-index.ts
new file mode 100644
index 000000000..1bd61c99a
--- /dev/null
+++ b/src/mol-geo/theme/structure/color/atom-index.ts
@@ -0,0 +1,24 @@
+/**
+ * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ */
+
+import { ColorScale } from 'mol-util/color';
+import { StructureColorDataProps } from '.';
+import { OrderedSet } from 'mol-data/int';
+import { createElementInstanceColor } from '../../../util/color-data';
+
+export function atomIndexColorData(props: StructureColorDataProps) {
+    const { units, elementGroup, vertexMap } = props
+    const instanceCount = units.length
+    const elementCount = OrderedSet.size(elementGroup.elements)
+
+    const domain = [ 0, instanceCount * elementCount - 1 ]
+    const scale = ColorScale.create({ domain })
+    return createElementInstanceColor({
+        colorFn: (instanceIdx, elementIdx) => scale.color(instanceIdx * elementCount + elementIdx),
+        instanceCount,
+        vertexMap
+    })
+}
\ No newline at end of file
diff --git a/src/mol-geo/theme/structure/color/chain-id.ts b/src/mol-geo/theme/structure/color/chain-id.ts
new file mode 100644
index 000000000..a6bd36b1a
--- /dev/null
+++ b/src/mol-geo/theme/structure/color/chain-id.ts
@@ -0,0 +1,51 @@
+/**
+ * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ */
+
+import { ElementGroup, Model } from 'mol-model/structure';
+
+import { StructureColorDataProps } from '.';
+import { createAttributeOrElementColor } from '../../../util/color-data';
+import { ColorScale } from 'mol-util/color';
+
+function createChainIdMap(model: Model) {
+    const { chains } = model.hierarchy
+    const { label_asym_id } = chains
+
+    const map = new Map<string, number>()
+    let index = 0
+
+    for (let i = 0, il = chains._rowCount; i < il; ++i) {
+        const chainId = label_asym_id.value(i)
+        if (map.get(chainId) === undefined) {
+            map.set(chainId, index)
+            index += 1
+        }
+    }
+    return { map, count: index }
+}
+
+export function chainIdColorData(props: StructureColorDataProps) {
+    const { units, elementGroup, vertexMap } = props
+    const unit = units[0]
+
+    const { chains, chainSegments } = unit.model.hierarchy
+    const { label_asym_id } = chains
+    const { map, count } = createChainIdMap(unit.model)
+
+    const domain = [ 0, count - 1 ]
+    const scale = ColorScale.create({ domain })
+
+    return createAttributeOrElementColor(vertexMap, {
+        colorFn: (elementIdx: number) => {
+            const aI = ElementGroup.getAt(elementGroup, elementIdx);
+            const cI = chainSegments.segmentMap[aI]
+            const chainId = label_asym_id.value(cI)
+
+            return scale.color(map.get(chainId) || 0)
+        },
+        vertexMap
+    })
+}
\ No newline at end of file
diff --git a/src/mol-geo/theme/structure/color/element.ts b/src/mol-geo/theme/structure/color/element-symbol.ts
similarity index 100%
rename from src/mol-geo/theme/structure/color/element.ts
rename to src/mol-geo/theme/structure/color/element-symbol.ts
diff --git a/src/mol-geo/theme/structure/color/index.ts b/src/mol-geo/theme/structure/color/index.ts
index 35b994ba9..435874109 100644
--- a/src/mol-geo/theme/structure/color/index.ts
+++ b/src/mol-geo/theme/structure/color/index.ts
@@ -11,4 +11,9 @@ export interface StructureColorDataProps {
     units: ReadonlyArray<Unit>,
     elementGroup: ElementGroup,
     vertexMap: VertexMap
-}
\ No newline at end of file
+}
+
+export { atomIndexColorData } from './atom-index'
+export { chainIdColorData } from './chain-id'
+export { elementSymbolColorData } from './element-symbol'
+export { instanceIndexColorData } from './instance-index'
\ No newline at end of file
diff --git a/src/mol-geo/theme/structure/color/instance-index.ts b/src/mol-geo/theme/structure/color/instance-index.ts
new file mode 100644
index 000000000..bd7d6f577
--- /dev/null
+++ b/src/mol-geo/theme/structure/color/instance-index.ts
@@ -0,0 +1,21 @@
+/**
+ * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ */
+
+import { ColorScale } from 'mol-util/color';
+import { StructureColorDataProps } from '.';
+import { createInstanceColor } from '../../../util/color-data';
+
+export function instanceIndexColorData(props: StructureColorDataProps) {
+    const { units } = props
+    const instanceCount = units.length
+
+    const domain = [ 0, instanceCount - 1 ]
+    const scale = ColorScale.create({ domain })
+    return createInstanceColor({
+        colorFn: scale.color,
+        instanceCount
+    })
+}
\ No newline at end of file
diff --git a/src/mol-geo/theme/structure/size/index.ts b/src/mol-geo/theme/structure/size/index.ts
index 775c3c0d7..b178ad42b 100644
--- a/src/mol-geo/theme/structure/size/index.ts
+++ b/src/mol-geo/theme/structure/size/index.ts
@@ -11,4 +11,6 @@ export interface StructureSizeDataProps {
     units: ReadonlyArray<Unit>,
     elementGroup: ElementGroup,
     vertexMap: VertexMap
-}
\ No newline at end of file
+}
+
+export { vdwSizeData } from './vdw'
\ No newline at end of file
-- 
GitLab