Browse Source

combined duplicate chem_comp_bond props

Alexander Rose 5 years ago
parent
commit
fd9dac86b9

+ 14 - 4
src/mol-model-formats/structure/mmcif/bonds/comp.ts

@@ -8,8 +8,9 @@
 import { Model } from '../../../../mol-model/structure/model/model'
 import { LinkType } from '../../../../mol-model/structure/model/types'
 import { CustomPropertyDescriptor } from '../../../../mol-model/structure';
-import { mmCIF_Database } from '../../../../mol-io/reader/cif/schema/mmcif';
+import { mmCIF_Database, mmCIF_Schema } from '../../../../mol-io/reader/cif/schema/mmcif';
 import { CifWriter } from '../../../../mol-io/writer/cif'
+import { Table } from '../../../../mol-data/db';
 
 export interface ComponentBond {
     entries: Map<string, ComponentBond.Entry>
@@ -24,7 +25,7 @@ export namespace ComponentBond {
             categories: [{
                 name: 'chem_comp_bond',
                 instance(ctx) {
-                    const chem_comp_bond = getChemCompBond(ctx.structures[0].model);
+                    const chem_comp_bond = getChemCompBond(ctx.firstModel);
                     if (!chem_comp_bond) return CifWriter.Category.Empty;
 
                     const comp_names = ctx.structures[0].uniqueResidueNames;
@@ -51,14 +52,23 @@ export namespace ComponentBond {
         return true;
     }
 
-    export function attachFromExternalData(model: Model, bonds: ComponentBond, force = false) {
+    export function attachFromExternalData(model: Model, table: mmCIF_Database['chem_comp_bond'], force = false) {
         if (!force && model.customProperties.has(Descriptor)) return true;
         if (model._staticPropertyData.__ComponentBondData__) delete model._staticPropertyData.__ComponentBondData__;
+        const chem_comp_bond = chemCompBondFromTable(model, table);
+        if (chem_comp_bond._rowCount === 0) return false;
+
         model.customProperties.add(Descriptor);
-        model._staticPropertyData[PropName] = bonds;
+        model._staticPropertyData.__ComponentBondData__ = chem_comp_bond;
         return true;
     }
 
+    function chemCompBondFromTable(model: Model, table: mmCIF_Database['chem_comp_bond']): mmCIF_Database['chem_comp_bond'] {
+        return Table.pick(table, mmCIF_Schema.chem_comp_bond, (i: number) => {
+            return model.properties.chemicalComponentMap.has(table.comp_id.value(i))
+        })
+    }
+
     export class ComponentBondImpl implements ComponentBond {
         entries: Map<string, ComponentBond.Entry> = new Map();
 

+ 0 - 84
src/mol-model-props/wwpdb/chem-comp-bond.ts

@@ -1,84 +0,0 @@
-/**
- * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author Alexander Rose <alexander.rose@weirdbyte.de>
- */
-
-import { Column, Table } from '../../mol-data/db';
-import { toTable } from '../../mol-io/reader/cif/schema';
-import { Model, CustomPropertyDescriptor } from '../../mol-model/structure';
-import { mmCIF_chemCompBond_schema } from '../../mol-io/reader/cif/schema/mmcif-extras';
-import { CifWriter } from '../../mol-io/writer/cif';
-
-export namespace ChemCompBond {
-    export type Property = Table<Schema['chem_comp_bond']>
-
-    export function getFromModel(model: Model): Property {
-        if (model.sourceData.kind !== 'mmCIF') return Table.ofUndefinedColumns(Schema.chem_comp_bond, 0);
-        const { chem_comp_bond } = model.sourceData.data
-        return Table.ofColumns(Schema.chem_comp_bond, {
-            ...chem_comp_bond,
-            molstar_protonation_variant: Column.Undefined(chem_comp_bond._rowCount, Column.Schema.Str())
-        });
-    }
-
-    export function get(model: Model): Property {
-        return model._staticPropertyData.__ChemCompBond__ || getFromModel(model);
-    }
-    function set(model: Model, prop: Property) {
-        (model._staticPropertyData.__ChemCompBond__ as Property) = prop;
-    }
-
-    export const Schema = { chem_comp_bond: mmCIF_chemCompBond_schema };
-    export type Schema = typeof Schema
-
-    export const Descriptor = CustomPropertyDescriptor({
-        isStatic: true,
-        name: 'chem_comp_bond',
-        cifExport: {
-            prefix: '',
-            context(ctx): Property { return get(ctx.firstModel); },
-            categories: [{
-                name: 'chem_comp_bond',
-                instance(ctx: Property) {
-                    return CifWriter.Category.ofTable(ctx);
-                }
-            }]
-        }
-    });
-
-    function fromCifData(model: Model): Table<Schema['chem_comp_bond']> | undefined {
-        if (model.sourceData.kind !== 'mmCIF') return void 0;
-        const cat = model.sourceData.frame.categories.chem_comp_bond;
-        if (!cat) return void 0;
-        return toTable(Schema.chem_comp_bond, cat);
-    }
-
-    export async function attachFromCifOrTable(model: Model, params: {
-        // optional Table source
-        wwPDB_apiSourceTable?: (model: Model) => Promise<Table<Schema['chem_comp_bond']>>
-    }) {
-        if (model.customProperties.has(Descriptor)) return true;
-
-        let chemCompBond: Table<Schema['chem_comp_bond']> | undefined = fromCifData(model);
-        if (chemCompBond === void 0 && params.wwPDB_apiSourceTable) {
-            const data = await params.wwPDB_apiSourceTable(model);
-            if (!data) return false;
-            chemCompBond = chemCompBondFromTable(model, data);
-        } else {
-            return false;
-        }
-
-        if (!chemCompBond) return false;
-
-        model.customProperties.add(Descriptor);
-        set(model, chemCompBond);
-        return true;
-    }
-}
-
-function chemCompBondFromTable(model: Model, table: Table<ChemCompBond.Schema['chem_comp_bond']>): Table<ChemCompBond.Schema['chem_comp_bond']> {
-    return Table.pick(table, ChemCompBond.Schema.chem_comp_bond, (i: number) => {
-        return model.properties.chemicalComponentMap.has(table.comp_id.value(i))
-    })
-}

+ 13 - 15
src/servers/model/properties/providers/wwpdb.ts

@@ -7,34 +7,32 @@
 import * as fs from 'fs'
 import * as util from 'util'
 import { AttachModelProperty } from '../../property-provider';
-import { ChemCompBond } from '../../../../mol-model-props/wwpdb/chem-comp-bond';
-import { Table } from '../../../../mol-data/db';
 import { CIF } from '../../../../mol-io/reader/cif';
 import { getParam } from '../../../common/util';
+import { ComponentBond } from '../../../../mol-model-formats/structure/mmcif/bonds';
+import { mmCIF_Database, mmCIF_Schema } from '../../../../mol-io/reader/cif/schema/mmcif';
 
 require('util.promisify').shim()
 const readFile = util.promisify(fs.readFile)
 
-export const wwPDB_chemCompBond: AttachModelProperty = ({ model, params }) => {
-    const wwPDB_apiSourceTable = getChemCompBondTableProvider(getTablePath(params))
-    return ChemCompBond.attachFromCifOrTable(model, { wwPDB_apiSourceTable });
+export const wwPDB_chemCompBond: AttachModelProperty = async ({ model, params }) => {
+    const table = await getChemCompBondTable(getTablePath(params))
+    return ComponentBond.attachFromExternalData(model, table, true)
 }
 
 async function read(path: string) {
     return path.endsWith('.bcif') ? new Uint8Array(await readFile(path)) : readFile(path, 'utf8');
 }
 
-function getChemCompBondTableProvider(path: string): () => Promise<Table<ChemCompBond.Schema['chem_comp_bond']>> {
-    let chemCompBondTable: Table<ChemCompBond.Schema['chem_comp_bond']>
-    return async function() {
-        if (chemCompBondTable === undefined) {
-            const parsed = await CIF.parse(await read(path)).run()
-            if (parsed.isError) throw new Error(parsed.toString())
-            const table = CIF.toDatabase(ChemCompBond.Schema, parsed.result.blocks[0])
-            chemCompBondTable = table.chem_comp_bond
-        }
-        return chemCompBondTable
+let chemCompBondTable: mmCIF_Database['chem_comp_bond']
+async function getChemCompBondTable(path: string): Promise<mmCIF_Database['chem_comp_bond']> {
+    if (!chemCompBondTable) {
+        const parsed = await CIF.parse(await read(path)).run()
+        if (parsed.isError) throw new Error(parsed.toString())
+        const table = CIF.toDatabase(mmCIF_Schema, parsed.result.blocks[0])
+        chemCompBondTable = table.chem_comp_bond
     }
+    return chemCompBondTable
 }
 
 function getTablePath(params: any) {