UNPKG

molstar

Version:

A comprehensive macromolecular library.

288 lines (287 loc) 12.6 kB
#!/usr/bin/env node /** * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { __awaiter, __generator } from "tslib"; import * as argparse from 'argparse'; import * as util from 'util'; import * as path from 'path'; import * as fs from 'fs'; require('util.promisify').shim(); var writeFile = util.promisify(fs.writeFile); import { Database, Table } from '../../mol-data/db'; 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-extras'; import { DefaultDataOptions, ensureDataAvailable, getEncodedCif, readCCD, readPVCD } from './util'; function ccbKey(compId, atomId1, atomId2) { return atomId1 < atomId2 ? "".concat(compId, ":").concat(atomId1, "-").concat(atomId2) : "".concat(compId, ":").concat(atomId2, "-").concat(atomId1); } function ccaKey(compId, atomId) { return "".concat(compId, ":").concat(atomId); } function addChemCompBondToSet(set, ccb) { for (var i = 0, il = ccb._rowCount; i < il; ++i) { set.add(ccbKey(ccb.comp_id.value(i), ccb.atom_id_1.value(i), ccb.atom_id_2.value(i))); } return set; } function addChemCompAtomToSet(set, cca) { for (var i = 0, il = cca._rowCount; i < il; ++i) { set.add(ccaKey(cca.comp_id.value(i), cca.atom_id.value(i))); } return set; } function checkAddingBondsFromPVCD(pvcd) { var ccbSetByParent = DefaultMap(function () { return new Set(); }); for (var k in pvcd) { var _a = pvcd[k], chem_comp = _a.chem_comp, chem_comp_bond = _a.chem_comp_bond; if (chem_comp_bond._rowCount) { var parentIds = chem_comp.mon_nstd_parent_comp_id.value(0); if (parentIds.length === 0) { var set = ccbSetByParent.getDefault(chem_comp.id.value(0)); addChemCompBondToSet(set, chem_comp_bond); } else { for (var i = 0, il = parentIds.length; i < il; ++i) { var parentId = parentIds[i]; var set = ccbSetByParent.getDefault(parentId); addChemCompBondToSet(set, chem_comp_bond); } } } } var _loop_1 = function (k) { var _b = pvcd[k], chem_comp = _b.chem_comp, chem_comp_atom = _b.chem_comp_atom, chem_comp_bond = _b.chem_comp_bond; if (chem_comp_bond._rowCount) { var parentIds = chem_comp.mon_nstd_parent_comp_id.value(0); if (parentIds.length > 0) { var _loop_2 = function (i, il) { var entryBonds = addChemCompBondToSet(new Set(), chem_comp_bond); var entryAtoms = addChemCompAtomToSet(new Set(), chem_comp_atom); var extraBonds = SetUtils.difference(ccbSetByParent.get(parentIds[i]), entryBonds); extraBonds.forEach(function (bk) { var _a = bk.split('|'), a1 = _a[0], a2 = _a[1]; if (entryAtoms.has(a1) && entryAtoms.has(a2)) { console.error("Adding all PVCD bonds would wrongly add bond ".concat(bk, " for ").concat(k)); } }); }; for (var i = 0, il = parentIds.length; i < il; ++i) { _loop_2(i, il); } } } }; for (var k in pvcd) { _loop_1(k); } } function checkAddingAtomsFromPVCD(pvcd) { var ccaSetByParent = DefaultMap(function () { return new Set(); }); for (var k in pvcd) { var _a = pvcd[k], chem_comp = _a.chem_comp, chem_comp_atom = _a.chem_comp_atom; if (chem_comp_atom._rowCount) { var parentIds = chem_comp.mon_nstd_parent_comp_id.value(0); if (parentIds.length === 0) { var set = ccaSetByParent.getDefault(chem_comp.id.value(0)); addChemCompAtomToSet(set, chem_comp_atom); } else { for (var i = 0, il = parentIds.length; i < il; ++i) { var parentId = parentIds[i]; var set = ccaSetByParent.getDefault(parentId); addChemCompAtomToSet(set, chem_comp_atom); } } } } } function createBonds(ccd, pvcd, atomsRequested) { return __awaiter(this, void 0, void 0, function () { function addBonds(compId, ccb, protonationVariant) { for (var i = 0, il = ccb._rowCount; i < il; ++i) { var atomId1 = ccb.atom_id_1.value(i); var atomId2 = ccb.atom_id_2.value(i); var k = ccbKey(compId, atomId1, atomId2); if (!ccbSet.has(k)) { atom_id_1.push(atomId1); atom_id_2.push(atomId2); comp_id.push(compId); value_order.push(ccb.value_order.value(i)); pdbx_aromatic_flag.push(ccb.pdbx_aromatic_flag.value(i)); pdbx_stereo_config.push(ccb.pdbx_stereo_config.value(i)); molstar_protonation_variant.push(protonationVariant ? 'Y' : 'N'); ccbSet.add(k); } } } var ccbSet, comp_id, atom_id_1, atom_id_2, value_order, pdbx_aromatic_flag, pdbx_stereo_config, molstar_protonation_variant, k, _a, chem_comp, chem_comp_bond, parentIds, i, il, k, _b, chem_comp, chem_comp_bond, bondTable, bondDatabase; return __generator(this, function (_c) { ccbSet = new Set(); comp_id = []; atom_id_1 = []; atom_id_2 = []; value_order = []; pdbx_aromatic_flag = []; pdbx_stereo_config = []; molstar_protonation_variant = []; // check adding bonds from PVCD checkAddingBondsFromPVCD(pvcd); // add bonds from PVCD for (k in pvcd) { _a = pvcd[k], chem_comp = _a.chem_comp, chem_comp_bond = _a.chem_comp_bond; if (chem_comp_bond._rowCount) { parentIds = chem_comp.mon_nstd_parent_comp_id.value(0); if (parentIds.length === 0) { addBonds(chem_comp.id.value(0), chem_comp_bond, false); } else { for (i = 0, il = parentIds.length; i < il; ++i) { addBonds(parentIds[i], chem_comp_bond, true); } } } } // add bonds from CCD for (k in ccd) { _b = ccd[k], chem_comp = _b.chem_comp, chem_comp_bond = _b.chem_comp_bond; if (chem_comp_bond._rowCount) { addBonds(chem_comp.id.value(0), chem_comp_bond, false); } } bondTable = Table.ofArrays(mmCIF_chemCompBond_schema, { comp_id: comp_id, atom_id_1: atom_id_1, atom_id_2: atom_id_2, value_order: value_order, pdbx_aromatic_flag: pdbx_aromatic_flag, pdbx_stereo_config: pdbx_stereo_config, molstar_protonation_variant: molstar_protonation_variant }); bondDatabase = Database.ofTables(CCB_TABLE_NAME, { chem_comp_bond: mmCIF_chemCompBond_schema }, { chem_comp_bond: bondTable }); return [2 /*return*/, { bonds: bondDatabase, atoms: atomsRequested ? createAtoms(ccd, pvcd) : void 0 }]; }); }); } function createAtoms(ccd, pvcd) { var ccaSet = new Set(); var comp_id = []; var atom_id = []; var charge = []; var pdbx_stereo_config = []; function addAtoms(compId, cca) { for (var i = 0, il = cca._rowCount; i < il; ++i) { var atomId = cca.atom_id.value(i); var k = ccaKey(compId, atomId); if (!ccaSet.has(k)) { atom_id.push(atomId); comp_id.push(compId); charge.push(cca.charge.value(i)); pdbx_stereo_config.push(cca.pdbx_stereo_config.value(i)); ccaSet.add(k); } } } // check adding atoms from PVCD checkAddingAtomsFromPVCD(pvcd); // add atoms from PVCD for (var k in pvcd) { var _a = pvcd[k], chem_comp = _a.chem_comp, chem_comp_atom = _a.chem_comp_atom; if (chem_comp_atom._rowCount) { var parentIds = chem_comp.mon_nstd_parent_comp_id.value(0); if (parentIds.length === 0) { addAtoms(chem_comp.id.value(0), chem_comp_atom); } else { for (var i = 0, il = parentIds.length; i < il; ++i) { addAtoms(parentIds[i], chem_comp_atom); } } } } // add atoms from CCD for (var k in ccd) { var _b = ccd[k], chem_comp = _b.chem_comp, chem_comp_atom = _b.chem_comp_atom; if (chem_comp_atom._rowCount) { addAtoms(chem_comp.id.value(0), chem_comp_atom); } } var atomTable = Table.ofArrays(ccd_chemCompAtom_schema, { comp_id: comp_id, atom_id: atom_id, charge: charge, pdbx_stereo_config: pdbx_stereo_config }); return Database.ofTables(CCA_TABLE_NAME, { chem_comp_atom: ccd_chemCompAtom_schema }, { chem_comp_atom: atomTable }); } function run(out, binary, options, ccaOut) { if (binary === void 0) { binary = false; } if (options === void 0) { options = DefaultDataOptions; } return __awaiter(this, void 0, void 0, function () { var ccd, pvcd, _a, bonds, atoms, ccbCif, ccaCif; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, ensureDataAvailable(options)]; case 1: _b.sent(); return [4 /*yield*/, readCCD()]; case 2: ccd = _b.sent(); return [4 /*yield*/, readPVCD()]; case 3: pvcd = _b.sent(); return [4 /*yield*/, createBonds(ccd, pvcd, !!ccaOut)]; case 4: _a = _b.sent(), bonds = _a.bonds, atoms = _a.atoms; ccbCif = getEncodedCif(CCB_TABLE_NAME, bonds, binary); if (!fs.existsSync(path.dirname(out))) { fs.mkdirSync(path.dirname(out)); } writeFile(out, ccbCif); if (!!ccaOut) { ccaCif = getEncodedCif(CCA_TABLE_NAME, atoms, binary); if (!fs.existsSync(path.dirname(ccaOut))) { fs.mkdirSync(path.dirname(ccaOut)); } writeFile(ccaOut, ccaCif); } return [2 /*return*/]; } }); }); } var CCB_TABLE_NAME = 'CHEM_COMP_BONDS'; var CCA_TABLE_NAME = 'CHEM_COMP_ATOMS'; var parser = new argparse.ArgumentParser({ add_help: true, description: 'Create a cif file with one big table of all chem_comp_bond entries from the CCD and PVCD.' }); parser.add_argument('out', { help: 'Generated file output path.' }); parser.add_argument('--forceDownload', '-f', { action: 'store_true', help: 'Force download of CCD and PVCD.' }); parser.add_argument('--binary', '-b', { action: 'store_true', help: 'Output as BinaryCIF.' }); parser.add_argument('--ccaOut', '-a', { help: 'Optional generated file output path for chem_comp_atom data.', required: false }); parser.add_argument('--ccdUrl', '-c', { help: 'Fetch the CCD from a custom URL. This forces download of the CCD.', required: false }); parser.add_argument('--pvcdUrl', '-p', { help: 'Fetch the PVCD from a custom URL. This forces download of the PVCD.', required: false }); var args = parser.parse_args(); run(args.out, args.binary, { forceDownload: args.forceDownload, ccdUrl: args.ccdUrl, pvcdUrl: args.pvcdUrl }, args.ccaOut);