Browse Source

add getTripletIndices & triplets to UnitResonance

Alexander Rose 3 years ago
parent
commit
8495d834c8
2 changed files with 27 additions and 8 deletions
  1. 1 0
      CHANGELOG.md
  2. 26 8
      src/mol-model/structure/structure/unit/resonance.ts

+ 1 - 0
CHANGELOG.md

@@ -12,6 +12,7 @@ Note that since we don't clearly distinguish between a public and private interf
 - Fix visual for bonds between two aromatic rings
 - Fix visual for delocalized bonds (parsed from mmcif and mol2)
 - Fix ring computation algorithm
+- Add ``UnitResonance`` property with info about delocalized triplets
 
 ## [v3.2.0] - 2022-02-17
 

+ 26 - 8
src/mol-model/structure/structure/unit/resonance.ts

@@ -4,6 +4,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
+import { SortedArray } from '../../../../mol-data/int/sorted-array';
 import { sortedCantorPairing } from '../../../../mol-data/util';
 import { BondType } from '../../model/types';
 import { StructureElement } from '../element';
@@ -15,13 +16,16 @@ export type UnitResonance = {
      *
      * Does not include triplets that are part of aromatic rings.
      */
-    delocalizedTriplets: {
+    readonly delocalizedTriplets: {
         /** Return 3rd element in triplet or undefined if `a` and `b` are not part of a triplet */
-        getThirdElement: (a: StructureElement.UnitIndex, b: StructureElement.UnitIndex) => StructureElement.UnitIndex | undefined
+        readonly getThirdElement: (a: StructureElement.UnitIndex, b: StructureElement.UnitIndex) => StructureElement.UnitIndex | undefined
+        /** Return index into `triplets` or undefined if `a` is not part of any triplet */
+        readonly getTripletIndices: (a: StructureElement.UnitIndex) => number[] | undefined
+        readonly triplets: SortedArray<StructureElement.UnitIndex>[]
     }
 }
 
-export function getResonance(unit: Unit.Atomic) {
+export function getResonance(unit: Unit.Atomic): UnitResonance {
     return {
         delocalizedTriplets: getDelocalizedTriplets(unit)
     };
@@ -33,7 +37,17 @@ function getDelocalizedTriplets(unit: Unit.Atomic) {
     const { order: _order, flags: _flags } = edgeProps;
     const { elementAromaticRingIndices } = unit.rings;
 
-    const map = new Map<number, StructureElement.UnitIndex>();
+    const triplets: SortedArray<StructureElement.UnitIndex>[] = [];
+    const thirdElementMap = new Map<number, StructureElement.UnitIndex>();
+    const indicesMap = new Map<number, number[]>();
+
+    const add = (a: StructureElement.UnitIndex, b: StructureElement.UnitIndex, c: StructureElement.UnitIndex) => {
+        const index = triplets.length;
+        triplets.push(SortedArray.ofUnsortedArray([a, b, c]));
+        thirdElementMap.set(sortedCantorPairing(a, b), c);
+        if (indicesMap.has(a)) indicesMap.get(a)!.push(index);
+        else indicesMap.set(a, [index]);
+    };
 
     for (let i = 0 as StructureElement.UnitIndex; i < unit.elements.length; i++) {
         if (elementAromaticRingIndices.has(i)) continue;
@@ -50,16 +64,20 @@ function getDelocalizedTriplets(unit: Unit.Atomic) {
         }
 
         if (deloBonds.length >= 2) {
-            map.set(sortedCantorPairing(i, deloBonds[0]), deloBonds[1]);
+            add(i, deloBonds[0], deloBonds[1]);
             for (let j = 1, jl = deloBonds.length; j < jl; j++) {
-                map.set(sortedCantorPairing(i, deloBonds[j]), deloBonds[0]);
+                add(i, deloBonds[j], deloBonds[0]);
             }
         }
     }
 
     return {
         getThirdElement: (a: StructureElement.UnitIndex, b: StructureElement.UnitIndex) => {
-            return map.get(sortedCantorPairing(a, b));
-        }
+            return thirdElementMap.get(sortedCantorPairing(a, b));
+        },
+        getTripletIndices: (a: StructureElement.UnitIndex) => {
+            return indicesMap.get(a);
+        },
+        triplets,
     };
 }