Browse Source

creation of charge and stereo config data

JonStargaryen 4 years ago
parent
commit
7216a25877
2 changed files with 75 additions and 10 deletions
  1. 59 10
      src/cli/chem-comp-bond/create-table.ts
  2. 16 0
      src/mol-io/reader/cif/schema/ccd-extras.ts

+ 59 - 10
src/cli/chem-comp-bond/create-table.ts

@@ -23,6 +23,7 @@ import { CCD_Schema } from '../../mol-io/reader/cif/schema/ccd';
 import { SetUtils } from '../../mol-util/set';
 import { DefaultMap } from '../../mol-util/map';
 import { mmCIF_chemCompBond_schema } from '../../mol-io/reader/cif/schema/mmcif-extras';
+import { ccd_chemCompAtom_schema } from '../../mol-io/reader/cif/schema/ccd-extraas';
 
 export async function ensureAvailable(path: string, url: string) {
     if (FORCE_DOWNLOAD || !fs.existsSync(path)) {
@@ -135,7 +136,7 @@ function checkAddingBondsFromPVCD(pvcd: DatabaseCollection<CCD_Schema>) {
     }
 }
 
-async function createBonds() {
+async function createBonds(atomsRequested: boolean) {
     await ensureDataAvailable();
     const ccd = await readCCD();
     const pvcd = await readPVCD();
@@ -200,25 +201,68 @@ async function createBonds() {
     });
 
     const bondDatabase =  Database.ofTables(
-        TABLE_NAME,
+        CCB_TABLE_NAME,
         { chem_comp_bond: mmCIF_chemCompBond_schema },
         { chem_comp_bond: bondTable }
     );
 
-    return bondDatabase;
+    return { bonds: bondDatabase, atoms: atomsRequested ? createAtoms(ccd) : void 0 };
 }
 
-async function run(out: string, binary = false) {
-    const bonds = await createBonds();
+function createAtoms(ccd: DatabaseCollection<CCD_Schema>) {
+    const comp_id: string[] = [];
+    const atom_id: string[] = [];
+    const charge: number[] = [];
+    const pdbx_stereo_config: typeof CCD_Schema.chem_comp_atom['pdbx_stereo_config']['T'][] = [];
+
+    function addAtoms(compId: string, cca: CCA) {
+        for (let i = 0, il = cca._rowCount; i < il; ++i) {
+            atom_id.push(cca.atom_id.value(i));
+            comp_id.push(compId);
+            charge.push(cca.charge.value(i));
+            pdbx_stereo_config.push(cca.pdbx_stereo_config.value(i));
+        }
+    }
+
+    // add atoms from CCD
+    for (const k in ccd) {
+        const { chem_comp, chem_comp_atom } = ccd[k];
+        if (chem_comp_atom._rowCount) {
+            addAtoms(chem_comp.id.value(0), chem_comp_atom);
+        }
+    }
+
+    const atomTable = Table.ofArrays(ccd_chemCompAtom_schema, {
+        comp_id, atom_id, charge, pdbx_stereo_config
+    });
 
-    const cif = getEncodedCif(TABLE_NAME, bonds, binary);
+    return Database.ofTables(
+        CCA_TABLE_NAME,
+        { chem_comp_atom: ccd_chemCompAtom_schema },
+        { chem_comp_atom: atomTable }
+    );
+}
+
+async function run(out: string, binary = false, ccaOut?: string) {
+    const { bonds, atoms } = await createBonds(!!ccaOut);
+
+    const ccbCif = getEncodedCif(CCB_TABLE_NAME, bonds, binary);
     if (!fs.existsSync(path.dirname(out))) {
         fs.mkdirSync(path.dirname(out));
     }
-    writeFile(out, cif);
+    writeFile(out, ccbCif);
+
+    if (!!ccaOut) {
+        const ccaCif = getEncodedCif(CCA_TABLE_NAME, atoms, binary);
+        if (!fs.existsSync(path.dirname(ccaOut))) {
+            fs.mkdirSync(path.dirname(ccaOut));
+        }
+        writeFile(ccaOut, ccaCif);
+    }
 }
 
-const TABLE_NAME = 'CHEM_COMP_BONDS';
+const CCB_TABLE_NAME = 'CHEM_COMP_BONDS';
+const CCA_TABLE_NAME = 'CHEM_COMP_ATOMS';
 
 const DATA_DIR = path.join(__dirname, '..', '..', '..', '..', 'build/data');
 const CCD_PATH = path.join(DATA_DIR, 'components.cif');
@@ -241,13 +285,18 @@ parser.addArgument([ '--binary', '-b' ], {
     action: 'storeTrue',
     help: 'Output as BinaryCIF.'
 });
+parser.addArgument(['--ccaOut', '-a'], {
+    help: 'Optional generated file output path for chem_comp_atom data.',
+    required: false
+});
 interface Args {
     out: string
     forceDownload?: boolean
-    binary?: boolean
+    binary?: boolean,
+    ccaOut?: string
 }
 const args: Args = parser.parseArgs();
 
 const FORCE_DOWNLOAD = args.forceDownload;
 
-run(args.out, args.binary);
+run(args.out, args.binary, args.ccaOut);

+ 16 - 0
src/mol-io/reader/cif/schema/ccd-extras.ts

@@ -0,0 +1,16 @@
+/**
+ * Copyright (c) 2020 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 { CCD_Schema } from './ccd';
+
+// a reduced chem_comp_atom schema that provides charge and stereo_config information
+export const ccd_chemCompAtom_schema = {
+    comp_id: CCD_Schema.chem_comp_atom.comp_id,
+    atom_id: CCD_Schema.chem_comp_atom.atom_id,
+    charge: CCD_Schema.chem_comp_atom.charge,
+    pdbx_stereo_config: CCD_Schema.chem_comp_atom.pdbx_stereo_config
+};