Browse Source

rewire mol/sdf

JonStargaryen 4 years ago
parent
commit
73ada6b1f1
2 changed files with 20 additions and 17 deletions
  1. 4 3
      src/mol-io/writer/ligand-encoder.ts
  2. 16 14
      src/mol-io/writer/mol/encoder.ts

+ 4 - 3
src/mol-io/writer/ligand-encoder.ts

@@ -11,11 +11,11 @@ import { ComponentAtom } from '../../mol-model-formats/structure/property/bonds/
 import { ComponentBond } from '../../mol-model-formats/structure/property/bonds/comp';
 
 interface Atom {
-    label_atom_id: string,
     Cartn_x: number,
     Cartn_y: number,
     Cartn_z: number,
-    type_symbol: string
+    type_symbol: string,
+    index: number
 }
 
 function Atom(partial: any): Atom {
@@ -114,13 +114,14 @@ export abstract class LigandEncoder implements Encoder<string> {
                     index++;
                     continue;
                 }
-                const a: { [k: string]: (string | number) } = { 'label_atom_id': lai };
+                const a: { [k: string]: (string | number) } = {};
 
                 for (let _f = 0, _fl = fields.length; _f < _fl; _f++) {
                     const f: Field<any, any> = fields[_f]!;
                     a[f.name] = f.value(key, data, index);
                 }
                 a[type_symbol.name] = ts;
+                a['index'] = index;
 
                 atoms.set(lai, Atom(a));
                 index++;

+ 16 - 14
src/mol-io/writer/mol/encoder.ts

@@ -31,24 +31,26 @@ export class MolEncoder extends LigandEncoder {
 
         // traverse once to determine all actually present atoms
         const atoms = this.getAtoms(instance, source);
-        for (let i1 = 0, il = atoms.length; i1 < il; i1++) {
-            const atom = atoms[i1];
-            const { charge, stereo_config } = atomMap.map.get(atom.label_atom_id)!;
-            StringBuilder.writePadLeft(ctab, atom.Cartn_x.toFixed(4), 10);
-            StringBuilder.writePadLeft(ctab, atom.Cartn_y.toFixed(4), 10);
-            StringBuilder.writePadLeft(ctab, atom.Cartn_z.toFixed(4), 10);
+        atoms.forEach((atom1, label_atom_id1) => {
+            const { index: i1 } = atom1;
+            const { charge, stereo_config } = atomMap.map.get(label_atom_id1)!;
+            StringBuilder.writePadLeft(ctab, atom1.Cartn_x.toFixed(4), 10);
+            StringBuilder.writePadLeft(ctab, atom1.Cartn_y.toFixed(4), 10);
+            StringBuilder.writePadLeft(ctab, atom1.Cartn_z.toFixed(4), 10);
             StringBuilder.whitespace1(ctab);
-            StringBuilder.writePadRight(ctab, atom.type_symbol, 2);
+            StringBuilder.writePadRight(ctab, atom1.type_symbol, 2);
             StringBuilder.writeSafe(ctab, '  0');
             StringBuilder.writeIntegerPadLeft(ctab, this.mapCharge(charge), 3);
             StringBuilder.writeSafe(ctab, '  0  0  0  0  0  0  0  0  0  0\n');
             if (stereo_config !== 'N') chiral = true;
 
-            bondMap.map.get(atom.label_atom_id)!.forEach((v, k) => {
-                const i2 = atoms.findIndex(e => e.label_atom_id === k);
-                const label2 = this.getLabel(k);
-                if (i1 < i2 && atoms.findIndex(e => e.label_atom_id === k) > -1 && !this.skipHydrogen(label2)) {
-                    const { order } = v;
+            bondMap.map.get(label_atom_id1)!.forEach((bond, label_atom_id2) => {
+                const atom2 = atoms.get(label_atom_id2);
+                if (!atom2) return;
+
+                const { index: i2, type_symbol: type_symbol2 } = atom2;
+                if (i1 < i2 && !this.skipHydrogen(type_symbol2)) {
+                    const { order } = bond;
                     StringBuilder.writeIntegerPadLeft(bonds, i1 + 1, 3);
                     StringBuilder.writeIntegerPadLeft(bonds, i2 + 1, 3);
                     StringBuilder.writeIntegerPadLeft(bonds, order, 3);
@@ -56,10 +58,10 @@ export class MolEncoder extends LigandEncoder {
                     bondCount++;
                 }
             });
-        }
+        });
 
         // write counts line
-        StringBuilder.writeIntegerPadLeft(this.builder, atoms.length, 3);
+        StringBuilder.writeIntegerPadLeft(this.builder, atoms.size, 3);
         StringBuilder.writeIntegerPadLeft(this.builder, bondCount, 3);
         StringBuilder.writeSafe(this.builder, `  0  0  ${chiral ? 1 : 0}  0  0  0  0  0  0\n`);