Ver Fonte

cleanup

JonStargaryen há 4 anos atrás
pai
commit
240de5b24d

+ 54 - 13
src/servers/model/ligand-writer/ligand-encoder.ts

@@ -9,24 +9,64 @@ import Writer from '../../../mol-io/writer/writer';
 import { Encoder, Category, Field } from '../../../mol-io/writer/cif/encoder';
 import { ComponentBond } from '../../../mol-model-formats/structure/property/bonds/comp';
 
-export interface Atom {
-    id: string,
-    x: number,
-    y: number,
-    z: number,
+interface Atom {
+    label_atom_id: string,
+    Cartn_x: number,
+    Cartn_y: number,
+    Cartn_z: number,
     type_symbol: string
 }
 
+function Atom(partial: any): Atom {
+    return { 
+        label_atom_id: partial.label_atom_id,
+        Cartn_x: partial.Cartn_x,
+        Cartn_y: partial.Cartn_y,
+        Cartn_z: partial.Cartn_z,
+        type_symbol: partial.type_symbol
+    }
+}
+
 export abstract class LigandEncoder implements Encoder<string> {
     protected builder: StringBuilder;
+    protected meta: StringBuilder;
     protected componentData: ComponentBond;
+    protected error = false;
+    protected encoded = false;
     readonly isBinary = false;
     binaryEncodingProvider = void 0;
 
-    abstract writeCategory<Ctx>(category: Category<Ctx>, context?: Ctx): void;
-
     abstract encode(): void;
 
+    abstract _writeCategory<Ctx>(category: Category<Ctx>, context?: Ctx): void;
+
+    protected abstract writeFullCategory<Ctx>(sb: StringBuilder, category: Category<Ctx>, context?: Ctx): void;
+
+    writeCategory<Ctx>(category: Category<Ctx>, context?: Ctx) {
+        if (this.encoded) {
+            throw new Error('The writer contents have already been encoded, no more writing.');
+        }
+
+        if (this.metaInformation && (category.name === 'model_server_result' || category.name === 'model_server_params' || category.name === 'model_server_stats')) {
+            this.writeFullCategory(this.meta, category, context);
+            return;
+        }
+
+        // if error: force writing of meta information
+        if (category.name === 'model_server_error') {
+            this.writeFullCategory(this.meta, category, context);
+            this.error = true;
+            return;
+        }
+
+        // only care about atom_site category when writing SDF
+        if (category.name !== 'atom_site') {
+            return;
+        }
+
+        this._writeCategory(category, context);
+    }
+
     setComponentBondData(componentData: ComponentBond) {
         this.componentData = componentData;
     }
@@ -53,10 +93,10 @@ export abstract class LigandEncoder implements Encoder<string> {
         // all of this is used to ensure that only 1 residue is written
         const auxiliaryFields = this.getSortedFields(instance, ['label_seq_id', 'label_asym_id', 'pdbx_PDB_ins_code', 'pdbx_PDB_model_num']);
 
-        return this.getAtomsInternal(source, sortedFields, label_atom_id, auxiliaryFields);
+        return this._getAtoms(source, sortedFields, label_atom_id, auxiliaryFields);
     }
 
-    private getAtomsInternal(source: any, fields: Field<any, any>[], label_atom_id: Field<any, any>, auxiliaryFields: Field<any, any>[]): Atom[] {
+    private _getAtoms(source: any, fields: Field<any, any>[], label_atom_id: Field<any, any>, auxiliaryFields: Field<any, any>[]): Atom[] {
         const atoms: Atom[] = [];
         let index = 0;
         let id: (string | number)[] | undefined = void 0;
@@ -88,14 +128,14 @@ export abstract class LigandEncoder implements Encoder<string> {
                     index++;
                     continue;
                 }
-                const d: (string | number)[] = [lai];
+                const a: { [k: string]: (string | number) } = { 'label_atom_id': lai };
 
                 for (let _f = 0, _fl = fields.length; _f < _fl; _f++) {
                     const f: Field<any, any> = fields[_f]!;
-                    d.push(f.value(key, data, index));
+                    a[f.name] = f.value(key, data, index);
                 }
 
-                atoms.push({ id: d[0] as string, x: d[1] as number, y: d[2] as number, z: d[3] as number, type_symbol: d[4] as string});
+                atoms.push(Atom(a));
                 index++;
             }
         }
@@ -137,7 +177,8 @@ export abstract class LigandEncoder implements Encoder<string> {
         return true;
     }
 
-    constructor(readonly encoder: string, readonly hydrogens: boolean) {
+    constructor(readonly encoder: string, readonly metaInformation: boolean, readonly hydrogens: boolean) {
         this.builder = StringBuilder.create();
+        this.meta = StringBuilder.create();
     }
 }

+ 10 - 36
src/servers/model/ligand-writer/mol/encoder.ts

@@ -13,32 +13,7 @@ import { LigandEncoder } from '../ligand-encoder';
 // SDF wraps MOL and allows for multiple molecules per file as well as additional properties
 // TODO add support for stereo/chiral flags, add charges
 export class MolEncoder extends LigandEncoder {
-    private meta: StringBuilder;
-    private encoded = false;
-    private error = false;
-
-    writeCategory<Ctx>(category: Category<Ctx>, context?: Ctx) {
-        if (this.encoded) {
-            throw new Error('The writer contents have already been encoded, no more writing.');
-        }
-
-        if (this.metaInformation && (category.name === 'model_server_result' || category.name === 'model_server_params' || category.name === 'model_server_stats')) {
-            this.writeFullCategory(this.meta, category, context);
-            return;
-        }
-
-        // if error: force writing of meta information
-        if (category.name === 'model_server_error') {
-            this.writeFullCategory(this.meta, category, context);
-            this.error = true;
-            return;
-        }
-
-        // only care about atom_site category when writing SDF
-        if (category.name !== 'atom_site') {
-            return;
-        }
-
+    _writeCategory<Ctx>(category: Category<Ctx>, context?: Ctx) {
         // use separate builder because we still need to write Counts and Bonds line
         const ctab = StringBuilder.create();
         const bonds = StringBuilder.create();
@@ -57,17 +32,17 @@ export class MolEncoder extends LigandEncoder {
         const atoms = this.getAtoms(instance, source);
         for (let i1 = 0, il = atoms.length; i1 < il; i1++) {
             const atom = atoms[i1];
-            StringBuilder.writePadLeft(ctab, atom.x.toFixed(4), 10);
-            StringBuilder.writePadLeft(ctab, atom.y.toFixed(4), 10);
-            StringBuilder.writePadLeft(ctab, atom.z.toFixed(4), 10);
+            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);
             StringBuilder.whitespace1(ctab);
             StringBuilder.writePadRight(ctab, atom.type_symbol, 2);
             StringBuilder.writeSafe(ctab, '  0  0  0  0  0  0  0  0  0  0  0  0\n');
 
-            bondMap.map.get(atom.id)!.forEach((v, k) => {
-                const i2 = atoms.findIndex(e => e.id === k);
+            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.id === k) > -1 && !this.skipHydrogen(label2)) {
+                if (i1 < i2 && atoms.findIndex(e => e.label_atom_id === k) > -1 && !this.skipHydrogen(label2)) {
                     const { order } = v;
                     StringBuilder.writeIntegerPadLeft(bonds, i1 + 1, 3);
                     StringBuilder.writeIntegerPadLeft(bonds, i2 + 1, 3);
@@ -89,7 +64,7 @@ export class MolEncoder extends LigandEncoder {
         StringBuilder.writeSafe(this.builder, 'M  END\n');
     }
 
-    private writeFullCategory<Ctx>(sb: StringBuilder, category: Category<Ctx>, context?: Ctx) {
+    protected writeFullCategory<Ctx>(sb: StringBuilder, category: Category<Ctx>, context?: Ctx) {
         const { instance, source } = getCategoryInstanceData(category, context);
         const fields = instance.fields;
         const src = source[0];
@@ -121,12 +96,11 @@ export class MolEncoder extends LigandEncoder {
         this.encoded = true;
     }
 
-    constructor(readonly encoder: string, readonly metaInformation: boolean, readonly hydrogens: boolean, readonly terminator: string = '') {
-        super(encoder, hydrogens);
+    constructor(encoder: string, metaInformation: boolean, hydrogens: boolean, readonly terminator: string = '') {
+        super(encoder, metaInformation, hydrogens);
 
         if (metaInformation && !terminator) {
             throw new Error('meta-information cannot be written for MOL files');
         }
-        this.meta = StringBuilder.create();
     }
 }

+ 8 - 33
src/servers/model/ligand-writer/mol2/encoder.ts

@@ -14,33 +14,9 @@ import { BondType } from "../../../../mol-model/structure/model/types";
 // TODO amide (and real sp/sp2/sp3) support for bonds and SYBYL atom types: see https://www.sdsc.edu/CCMS/Packages/cambridge/pluto/atom_types.html
 // TODO support charges
 export class Mol2Encoder extends LigandEncoder {
-    private meta: StringBuilder;
     private out: StringBuilder;
-    private encoded = false;
-    private error = false;
-
-    writeCategory<Ctx>(category: Category<Ctx>, context?: Ctx): void {
-        if (this.encoded) {
-            throw new Error('The writer contents have already been encoded, no more writing.');
-        }
-
-        if (this.metaInformation && (category.name === 'model_server_result' || category.name === 'model_server_params' || category.name === 'model_server_stats')) {
-            this.writeFullCategory(this.meta, category, context);
-            return;
-        }
-
-        // if error: force writing of meta information
-        if (category.name === 'model_server_error') {
-            this.writeFullCategory(this.meta, category, context);
-            this.error = true;
-            return;
-        }
-
-        // only care about atom_site category when writing SDF
-        if (category.name !== 'atom_site') {
-            return;
-        }
 
+    _writeCategory<Ctx>(category: Category<Ctx>, context?: Ctx): void {
         const a = StringBuilder.create();
         const b = StringBuilder.create();
         const { instance, source } = getCategoryInstanceData(category, context);
@@ -59,10 +35,10 @@ export class Mol2Encoder extends LigandEncoder {
             const atom = atoms[i1];
 
             let aromatic = false;
-            bondMap.map.get(atom.id)!.forEach((v, k) => {
-                const i2 = atoms.findIndex(e => e.id === k);
+            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.id === k) > -1 && !this.skipHydrogen(label2)) {
+                if (i1 < i2 && atoms.findIndex(e => e.label_atom_id === k) > -1 && !this.skipHydrogen(label2)) {
                     const { order, flags } = v;
                     const ar = flags === BondType.Flag.Aromatic;
                     if (ar) aromatic = true;
@@ -72,7 +48,7 @@ export class Mol2Encoder extends LigandEncoder {
             });
 
             const sub = aromatic ? '.ar' : '';
-            StringBuilder.writeSafe(a, `${i1 + 1} ${atom.type_symbol} ${atom.x.toFixed(3)} ${atom.y.toFixed(3)} ${atom.z.toFixed(3)} ${atom.type_symbol}${sub} 1 ${name} 0.000\n`);
+            StringBuilder.writeSafe(a, `${i1 + 1} ${atom.type_symbol} ${atom.Cartn_x.toFixed(3)} ${atom.Cartn_y.toFixed(3)} ${atom.Cartn_z.toFixed(3)} ${atom.type_symbol}${sub} 1 ${name} 0.000\n`);
         }
 
         StringBuilder.writeSafe(this.out, `@<TRIPOS>MOLECULE\n${name}\n${atoms.length} ${bondCount} 0 0 0\nSMALL\nNO_CHARGES\n\n`);
@@ -81,7 +57,7 @@ export class Mol2Encoder extends LigandEncoder {
         StringBuilder.writeSafe(this.out, `@<TRIPOS>SUBSTRUCTURE\n${name} ${name} 1\n`);
     }
 
-    private writeFullCategory<Ctx>(sb: StringBuilder, category: Category<Ctx>, context?: Ctx) {
+    protected writeFullCategory<Ctx>(sb: StringBuilder, category: Category<Ctx>, context?: Ctx) {
         const { instance, source } = getCategoryInstanceData(category, context);
         const fields = instance.fields;
         const src = source[0];
@@ -110,9 +86,8 @@ export class Mol2Encoder extends LigandEncoder {
         this.encoded = true;
     }
 
-    constructor(readonly encoder: string, readonly metaInformation: boolean, readonly hydrogens: boolean) {
-        super(encoder, hydrogens);
-        this.meta = StringBuilder.create();
+    constructor(encoder: string, metaInformation: boolean, hydrogens: boolean) {
+        super(encoder, metaInformation, hydrogens);
         this.out = StringBuilder.create();
     }
 }