Skip to content
Snippets Groups Projects
Commit fd9dac86 authored by Alexander Rose's avatar Alexander Rose
Browse files

combined duplicate chem_comp_bond props

parent fe1f3bd4
No related branches found
No related tags found
No related merge requests found
...@@ -8,8 +8,9 @@ ...@@ -8,8 +8,9 @@
import { Model } from '../../../../mol-model/structure/model/model' import { Model } from '../../../../mol-model/structure/model/model'
import { LinkType } from '../../../../mol-model/structure/model/types' import { LinkType } from '../../../../mol-model/structure/model/types'
import { CustomPropertyDescriptor } from '../../../../mol-model/structure'; 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 { CifWriter } from '../../../../mol-io/writer/cif'
import { Table } from '../../../../mol-data/db';
export interface ComponentBond { export interface ComponentBond {
entries: Map<string, ComponentBond.Entry> entries: Map<string, ComponentBond.Entry>
...@@ -24,7 +25,7 @@ export namespace ComponentBond { ...@@ -24,7 +25,7 @@ export namespace ComponentBond {
categories: [{ categories: [{
name: 'chem_comp_bond', name: 'chem_comp_bond',
instance(ctx) { 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; if (!chem_comp_bond) return CifWriter.Category.Empty;
const comp_names = ctx.structures[0].uniqueResidueNames; const comp_names = ctx.structures[0].uniqueResidueNames;
...@@ -51,14 +52,23 @@ export namespace ComponentBond { ...@@ -51,14 +52,23 @@ export namespace ComponentBond {
return true; 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 (!force && model.customProperties.has(Descriptor)) return true;
if (model._staticPropertyData.__ComponentBondData__) delete model._staticPropertyData.__ComponentBondData__; 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.customProperties.add(Descriptor);
model._staticPropertyData[PropName] = bonds; model._staticPropertyData.__ComponentBondData__ = chem_comp_bond;
return true; 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 { export class ComponentBondImpl implements ComponentBond {
entries: Map<string, ComponentBond.Entry> = new Map(); entries: Map<string, ComponentBond.Entry> = new Map();
......
/**
* 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))
})
}
\ No newline at end of file
...@@ -7,34 +7,32 @@ ...@@ -7,34 +7,32 @@
import * as fs from 'fs' import * as fs from 'fs'
import * as util from 'util' import * as util from 'util'
import { AttachModelProperty } from '../../property-provider'; 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 { CIF } from '../../../../mol-io/reader/cif';
import { getParam } from '../../../common/util'; 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() require('util.promisify').shim()
const readFile = util.promisify(fs.readFile) const readFile = util.promisify(fs.readFile)
export const wwPDB_chemCompBond: AttachModelProperty = ({ model, params }) => { export const wwPDB_chemCompBond: AttachModelProperty = async ({ model, params }) => {
const wwPDB_apiSourceTable = getChemCompBondTableProvider(getTablePath(params)) const table = await getChemCompBondTable(getTablePath(params))
return ChemCompBond.attachFromCifOrTable(model, { wwPDB_apiSourceTable }); return ComponentBond.attachFromExternalData(model, table, true)
} }
async function read(path: string) { async function read(path: string) {
return path.endsWith('.bcif') ? new Uint8Array(await readFile(path)) : readFile(path, 'utf8'); 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: mmCIF_Database['chem_comp_bond']
let chemCompBondTable: Table<ChemCompBond.Schema['chem_comp_bond']> async function getChemCompBondTable(path: string): Promise<mmCIF_Database['chem_comp_bond']> {
return async function() { if (!chemCompBondTable) {
if (chemCompBondTable === undefined) { const parsed = await CIF.parse(await read(path)).run()
const parsed = await CIF.parse(await read(path)).run() if (parsed.isError) throw new Error(parsed.toString())
if (parsed.isError) throw new Error(parsed.toString()) const table = CIF.toDatabase(mmCIF_Schema, parsed.result.blocks[0])
const table = CIF.toDatabase(ChemCompBond.Schema, parsed.result.blocks[0]) chemCompBondTable = table.chem_comp_bond
chemCompBondTable = table.chem_comp_bond
}
return chemCompBondTable
} }
return chemCompBondTable
} }
function getTablePath(params: any) { function getTablePath(params: any) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment