From 8eb43a95427fb6213c30adaa9e01b599e9ecb9d1 Mon Sep 17 00:00:00 2001
From: Alexander Rose <alex.rose@rcsb.org>
Date: Tue, 26 Jun 2018 20:18:45 -0700
Subject: [PATCH] added CoarsedElementKeys.findSequenceKey

---
 .../model/properties/coarse/hierarchy.ts      |  3 ++
 .../model/properties/utils/atomic-keys.ts     |  8 +---
 .../model/properties/utils/coarse-keys.ts     | 46 ++++++++++++++-----
 3 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/src/mol-model/structure/model/properties/coarse/hierarchy.ts b/src/mol-model/structure/model/properties/coarse/hierarchy.ts
index 81a7be2dd..1b50e835d 100644
--- a/src/mol-model/structure/model/properties/coarse/hierarchy.ts
+++ b/src/mol-model/structure/model/properties/coarse/hierarchy.ts
@@ -2,6 +2,7 @@
  * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author David Sehnal <david.sehnal@gmail.com>
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
 import { mmCIF_Database as mmCIF } from 'mol-io/reader/cif/schema/mmcif'
@@ -17,6 +18,8 @@ export interface CoarsedElementKeys {
     // assign a key to each element, index to the CoarseHierarchy.models table
     modelKey: ArrayLike<number>,
 
+    /** find index of the residue/feature element where seq_id is included */
+    findSequenceKey(entityId: string, asym_id: string, seq_id: number): number
     findChainKey(entityId: string, asym_id: string): number
 }
 
diff --git a/src/mol-model/structure/model/properties/utils/atomic-keys.ts b/src/mol-model/structure/model/properties/utils/atomic-keys.ts
index 628013288..dc4a3ac3f 100644
--- a/src/mol-model/structure/model/properties/utils/atomic-keys.ts
+++ b/src/mol-model/structure/model/properties/utils/atomic-keys.ts
@@ -92,11 +92,5 @@ export function getAtomicKeys(data: AtomicData, entities: Entities, segments: At
 
     const { findChainKey, findResidueKey } = createLookUp(entities, chainMaps, residueMaps);
 
-    return {
-        residueKey: residueKey,
-        chainKey: chainKey,
-        entityKey: entityKey,
-        findChainKey,
-        findResidueKey
-    };
+    return { residueKey, chainKey, entityKey, findChainKey, findResidueKey };
 }
\ No newline at end of file
diff --git a/src/mol-model/structure/model/properties/utils/coarse-keys.ts b/src/mol-model/structure/model/properties/utils/coarse-keys.ts
index a67d34580..67539ec4d 100644
--- a/src/mol-model/structure/model/properties/utils/coarse-keys.ts
+++ b/src/mol-model/structure/model/properties/utils/coarse-keys.ts
@@ -1,7 +1,8 @@
 /**
- * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author David Sehnal <david.sehnal@gmail.com>
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
 import { Entities } from '../common';
@@ -21,16 +22,27 @@ function getElementSubstructureKeyMap(map: Map<number, Map<string, number>>, key
     return ret;
 }
 
-function createLookUp(entities: Entities, chain: Map<number, Map<string, number>>) {
+function createLookUp(entities: Entities, chain: Map<number, Map<string, number>>, seq: Map<number, Map<number, number>>) {
     const getEntKey = entities.getEntityIndex;
     const findChainKey: CoarsedElementKeys['findChainKey'] = (e, c) => {
-        let eKey = getEntKey(e);
+        const eKey = getEntKey(e);
         if (eKey < 0) return -1;
         const cm = chain.get(eKey)!;
         if (!cm.has(c)) return -1;
         return cm.get(c)!;
     }
-    return { findChainKey };
+    const findSequenceKey: CoarsedElementKeys['findSequenceKey'] = (e, c, s) => {
+        const eKey = getEntKey(e);
+        if (eKey < 0) return -1;
+        const cm = chain.get(eKey);
+        if (cm === undefined) return -1
+        const cKey = cm.get(c)
+        if (cKey === undefined) return -1
+        const sm = seq.get(cKey)!
+        if (!sm.has(s)) return -1;
+        return sm.get(s)!
+    }
+    return { findChainKey, findSequenceKey };
 }
 
 function missingEntity(k: string) {
@@ -42,9 +54,11 @@ function missingModel(k: string) {
 }
 
 export function getCoarseKeys(data: CoarseElementData, modelIndex: (id: number) => number, entities: Entities): CoarsedElementKeys {
-    const { model_id, entity_id, asym_id, count, chainSegments } = data;
+    const { model_id, entity_id, asym_id, seq_id_begin, seq_id_end, count, chainSegments } = data;
 
+    const seqMaps = new Map<number, Map<number, number>>();
     const chainMaps = new Map<number, Map<string, number>>(), chainCounter = { index: 0 };
+
     const chainKey = new Int32Array(count);
     const entityKey = new Int32Array(count);
     const modelKey = new Int32Array(count);
@@ -61,14 +75,22 @@ export function getCoarseKeys(data: CoarseElementData, modelIndex: (id: number)
         const map = getElementSubstructureKeyMap(chainMaps, entityKey[start]);
         const key = getElementKey(map, asym_id.value(start), chainCounter);
         for (let i = start; i < end; i++) chainKey[i] = key;
+
+        // create seq_id map for the ranges defined by seq_id_begin and seq_id_end
+        const seqMap: Map<number, number> = new Map()
+        seqMaps.set(key, seqMap)
+        for (let i = start; i < end; i++) {
+            const seqStart = seq_id_begin.value(i)
+            const seqEnd = seq_id_end.value(i)
+            for (let j = seqStart; j <= seqEnd; j++) {
+                seqMap.set(j, i)
+            }
+        }
     }
 
-    const { findChainKey } = createLookUp(entities, chainMaps);
 
-    return {
-        chainKey: chainKey,
-        entityKey: entityKey,
-        modelKey: modelKey,
-        findChainKey
-    };
+
+    const { findChainKey, findSequenceKey } = createLookUp(entities, chainMaps, seqMaps);
+
+    return { chainKey, entityKey, modelKey, findSequenceKey, findChainKey };
 }
\ No newline at end of file
-- 
GitLab