From 22190c178c7cbddcca30c7608d97383b9dfca51c Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Wed, 18 Apr 2018 22:24:29 +0200
Subject: [PATCH] Renamed ElementGroup.id to key

---
 src/mol-geo/representation/structure/index.ts |  4 +--
 .../structure/structure/element/group.ts      |  7 ++--
 src/perf-tests/structure.ts                   | 35 ++++++++++++++++---
 3 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/src/mol-geo/representation/structure/index.ts b/src/mol-geo/representation/structure/index.ts
index 9b1750019..3ee1a5502 100644
--- a/src/mol-geo/representation/structure/index.ts
+++ b/src/mol-geo/representation/structure/index.ts
@@ -34,11 +34,11 @@ export class StructureRepresentation {
             const { elements, units } = structure;
             const uniqueGroups = EquivalenceClasses<number, ElementGroup>(
                 ElementGroup.hashCode,
-                (a, b) => units[a.id].model.id === units[b.id].model.id && OrderedSet.areEqual(a.elements, b.elements));
+                (a, b) => units[a.key].model.id === units[b.key].model.id && OrderedSet.areEqual(a.elements, b.elements));
 
             for (let i = 0, _i = ElementSet.unitCount(elements); i < _i; i++) {
                 const group = ElementSet.unitGetByIndex(elements, i);
-                uniqueGroups.add(group.id, group);
+                uniqueGroups.add(group.key, group);
 
             }
 
diff --git a/src/mol-model/structure/structure/element/group.ts b/src/mol-model/structure/structure/element/group.ts
index ab1d18146..ff542066d 100644
--- a/src/mol-model/structure/structure/element/group.ts
+++ b/src/mol-model/structure/structure/element/group.ts
@@ -9,7 +9,8 @@ import Unit from '../unit'
 
 interface ElementGroup {
     elements: OrderedSet,
-    id: number
+    // Unique identifier of the group, usable as partial key for various "caches".
+    key: number
 }
 
 namespace ElementGroup {
@@ -20,7 +21,7 @@ namespace ElementGroup {
     }
 
     export function createNew(elements: OrderedSet): ElementGroup {
-        return { id: nextId(), elements };
+        return { key: nextKey(), elements };
     }
 
     export function create(unit: Unit, elements: OrderedSet): ElementGroup {
@@ -61,7 +62,7 @@ namespace ElementGroup {
     }
 
     let _id = 0;
-    function nextId() {
+    function nextKey() {
         const ret = _id;
         _id = (_id + 1) % 0x3fffffff;
         return ret;
diff --git a/src/perf-tests/structure.ts b/src/perf-tests/structure.ts
index cddec0a7a..e27ae3f04 100644
--- a/src/perf-tests/structure.ts
+++ b/src/perf-tests/structure.ts
@@ -11,11 +11,12 @@ import * as fs from 'fs'
 import fetch from 'node-fetch'
 import CIF from 'mol-io/reader/cif'
 
-import { Structure, Model, Queries as Q, Element, ElementGroup, ElementSet, Selection, Symmetry } from 'mol-model/structure'
-import { Segmentation } from 'mol-data/int'
+import { Structure, Model, Queries as Q, Element, ElementGroup, ElementSet, Selection, Symmetry, Unit } from 'mol-model/structure'
+import { Segmentation, OrderedSet } from 'mol-data/int'
 
 import to_mmCIF from 'mol-model/structure/export/mmcif'
 import { Run } from 'mol-task';
+import { EquivalenceClasses } from 'mol-data/util';
 
 require('util.promisify').shim();
 const readFileAsync = util.promisify(fs.readFile);
@@ -100,7 +101,7 @@ async function ensureBcifAvailable(pdbId: string) {
     }
 }
 
-async function getBcif(pdbId: string) {
+export async function getBcif(pdbId: string) {
     await ensureBcifAvailable(pdbId);
     return await readCIF(getBcifPath(pdbId));
 }
@@ -301,11 +302,35 @@ export namespace PropertyAccess {
         console.log('exported');
     }
 
+    export function testGrouping(structure: Structure) {
+        const { elements, units } = Symmetry.buildAssembly(structure, '1');
+        console.log('grouping', units.length);
+        console.log('built asm');
+
+        const uniqueGroups = EquivalenceClasses<number, { unit: Unit, group: ElementGroup }>(
+            ({ unit, group }) => ElementGroup.hashCode(group),
+            (a, b) => a.unit.model.id === b.unit.model.id && (a.group.key === b.group.key && OrderedSet.areEqual(a.group.elements, b.group.elements))
+        );
+
+        for (let i = 0, _i = ElementSet.unitCount(elements); i < _i; i++) {
+            const group = ElementSet.unitGetByIndex(elements, i);
+            const unitId = ElementSet.unitGetId(elements, i);
+            uniqueGroups.add(unitId, { unit: units[unitId], group });
+        }
+
+        console.log('group count', uniqueGroups.groups.length);
+    }
+
     export async function run() {
-        const { structures, models/*, mmcif*/ } = await getBcif('1cbs');
+        //const { structures, models/*, mmcif*/ } = await getBcif('1cbs');
         // const { structures, models } = await getBcif('3j3q');
 
-        //const { structures, models, mmcif } = await readCIF('e:/test/quick/1cbs_updated.cif');
+        const { structures, models /*, mmcif*/ } = await readCIF('e:/test/quick/1hrv_updated.cif');
+        const { structures: s1, /*, mmcif*/ } = await readCIF('e:/test/quick/1tqn_updated.cif');
+
+        testGrouping(structures[0]);
+        console.log('------');
+        testGrouping(s1[0]);
         //const { structures, models/*, mmcif*/ } = await readCIF('e:/test/quick/5j7v_updated.cif');
 
         //console.log(mmcif.pdbx_struct_oper_list.matrix.toArray());
-- 
GitLab