@iktos-oss/rdkit-provider
Version:
exports an initialized RDKit instance, with helper functions
277 lines (273 loc) • 10.9 kB
JavaScript
;
/*
MIT License
Copyright (c) 2023 Iktos
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStereoTags = exports.addHs = exports.removeHs = exports.getNewCoords = exports.convertMolNotation = exports.isValidMolBlock = exports.getMatchingSubstructure = exports.hasMatchingSubstructure = exports.isValidSmarts = exports.isValidSmiles = exports.getMorganFp = exports.isChiral = exports.getCanonicalFormForStructure = exports.getMoleculeDetails = exports.getSvgFromSmarts = exports.getSvg = void 0;
const molecule_1 = require("./molecule");
const getSvg = ({ smiles, drawingDetails, alignmentDetails, }) => {
const molecules = alignmentDetails
? (0, molecule_1.get_molecules)([smiles, alignmentDetails.molBlock], globalThis.workerRDKit)
: (0, molecule_1.get_molecules)([smiles], globalThis.workerRDKit);
const [mol] = molecules;
if (!mol)
return null;
if (alignmentDetails) {
const [_, molToAlignWith] = molecules;
if (!molToAlignWith)
return null;
mol.generate_aligned_coords(molToAlignWith, JSON.stringify({ useCoordGen: globalThis.rdkitWorkerGlobals.preferCoordgen }));
}
const drawingDetailsStringifyed = drawingDetails ? JSON.stringify(drawingDetails) : '';
const svg = mol.get_svg_with_highlights(drawingDetailsStringifyed);
if (alignmentDetails) {
// reset coords if alignment was used (mol could be in cache)
mol.set_new_coords(globalThis.rdkitWorkerGlobals.preferCoordgen);
}
(0, molecule_1.release_molecules)(molecules);
return svg;
};
exports.getSvg = getSvg;
const getSvgFromSmarts = ({ smarts, width, height }) => {
const [smartsMol] = (0, molecule_1.get_query_molecules)([smarts], globalThis.workerRDKit);
if (!smartsMol)
return null;
const svg = smartsMol.get_svg(width, height);
(0, molecule_1.release_molecules)([smartsMol]);
return svg;
};
exports.getSvgFromSmarts = getSvgFromSmarts;
function getMoleculeDetails({ smiles, returnFullDetails }) {
const [mol] = (0, molecule_1.get_molecules)([smiles], globalThis.workerRDKit);
if (!mol)
return null;
const details = JSON.parse(mol.get_descriptors());
(0, molecule_1.release_molecules)([mol]);
if (returnFullDetails) {
return details;
}
// Return basic details (numAtoms, numRings) will be deprecated in ^v3.0.0
return {
numAtoms: details.NumHeavyAtoms,
numRings: details.NumRings,
};
}
exports.getMoleculeDetails = getMoleculeDetails;
const getCanonicalFormForStructure = ({ structure, useQMol = false, }) => {
return (0, exports.convertMolNotation)({
moleculeString: structure,
targetNotation: useQMol ? 'smarts' : 'smiles',
useQMol,
sourceNotation: undefined,
});
};
exports.getCanonicalFormForStructure = getCanonicalFormForStructure;
const isChiral = (smiles) => {
const [mol] = (0, molecule_1.get_molecules)([smiles], globalThis.workerRDKit);
if (!mol) {
throw new Error('@iktos-oss/rdkit-provider: Failed to instanciate molecule');
}
try {
// @ts-ignore
const achiralSmiles = mol.get_smiles(JSON.stringify({ doIsomericSmiles: false }));
// @ts-ignore
const chiralSmiles = mol.get_smiles(JSON.stringify({ doIsomericSmiles: true }));
return achiralSmiles !== chiralSmiles;
}
finally {
(0, molecule_1.release_molecules)([mol]);
}
};
exports.isChiral = isChiral;
const getMorganFp = ({ smiles, options, }) => {
const [mol] = (0, molecule_1.get_molecules)([smiles], globalThis.workerRDKit);
if (!mol) {
throw new Error('@iktos-oss/rdkit-provider: Failed to instanciate molecule');
}
try {
if (options) {
return mol.get_morgan_fp(JSON.stringify(options));
}
return mol.get_morgan_fp();
}
finally {
(0, molecule_1.release_molecules)([mol]);
}
};
exports.getMorganFp = getMorganFp;
const isValidSmiles = (smiles) => {
if (!smiles)
return false;
const [mol] = (0, molecule_1.get_molecules)([smiles], globalThis.workerRDKit);
if (!mol)
return false;
const isValid = mol.is_valid();
(0, molecule_1.release_molecules)([mol]);
return isValid;
};
exports.isValidSmiles = isValidSmiles;
const isValidSmarts = (smarts) => {
if (!smarts)
return false;
const [mol] = (0, molecule_1.get_query_molecules)([smarts], globalThis.workerRDKit);
if (!mol)
return false;
const isValid = mol.is_valid();
(0, molecule_1.release_molecules)([mol]);
return isValid;
};
exports.isValidSmarts = isValidSmarts;
const hasMatchingSubstructure = ({ smiles, substructure }) => {
const [smilesMol] = (0, molecule_1.get_molecules)([smiles], globalThis.workerRDKit);
const [smartsMol] = (0, molecule_1.get_query_molecules)([substructure], globalThis.workerRDKit);
if (!smilesMol || !smartsMol)
return false;
const substructureMatchDetails = JSON.parse(smilesMol.get_substruct_match(smartsMol));
const matchDetailsNotEmpty = !!substructureMatchDetails && !!Object.keys(substructureMatchDetails).length;
(0, molecule_1.release_molecules)([smilesMol, smartsMol]);
return matchDetailsNotEmpty;
};
exports.hasMatchingSubstructure = hasMatchingSubstructure;
const getMatchingSubstructure = ({ structure, substructure }) => {
const [mol] = (0, molecule_1.get_molecules)([structure], globalThis.workerRDKit);
const [molToMach] = (0, molecule_1.get_query_molecules)([substructure], globalThis.workerRDKit);
if (!mol || !molToMach)
return null;
const { atoms, bonds } = JSON.parse(mol.get_substruct_match(molToMach));
(0, molecule_1.release_molecules)([mol, molToMach]);
return { matchingAtoms: atoms, matchingBonds: bonds };
};
exports.getMatchingSubstructure = getMatchingSubstructure;
const isValidMolBlock = (mdl) => {
if (!mdl.includes('M END'))
return false;
const [mol] = (0, molecule_1.get_molecules)([mdl], globalThis.workerRDKit);
if (!mol)
return false;
try {
return mol.is_valid();
}
finally {
(0, molecule_1.release_molecules)([mol]);
}
};
exports.isValidMolBlock = isValidMolBlock;
/**
* Convert molecule structure to given notation (smiles, smarts, molblock, ...)
* Warning: some notations like inchi and aromatic_form don't work with qmol
*/
const convertMolNotation = ({ moleculeString, targetNotation, sourceNotation, useQMol, }) => {
const shouldUseSmarts = !!useQMol || (useQMol === undefined && sourceNotation === 'smarts');
if (sourceNotation != null) {
if (sourceNotation === targetNotation)
throw new Error('@iktos-oss/rdkit-provider: source and target notations must differ');
if (!_validateSource(moleculeString, sourceNotation))
throw new Error('@iktos-oss/rdkit-provider: molecule string not valid');
}
const [mol] = shouldUseSmarts
? (0, molecule_1.get_query_molecules)([moleculeString], globalThis.workerRDKit)
: (0, molecule_1.get_molecules)([moleculeString], globalThis.workerRDKit);
if (!mol)
return null;
try {
return mol[`get_${targetNotation}`]();
}
catch (e) {
console.error(e);
throw new Error('@iktos-oss/rdkit-provider: target notation not implemented');
}
finally {
(0, molecule_1.release_molecules)([mol]);
}
};
exports.convertMolNotation = convertMolNotation;
const getNewCoords = (structure, useCoordGen) => {
const [mol] = (0, molecule_1.get_molecules)([structure], globalThis.workerRDKit);
if (!mol)
return null;
try {
const mdl = useCoordGen !== undefined ? mol.get_new_coords(useCoordGen) : mol.get_new_coords();
return mdl;
}
finally {
(0, molecule_1.release_molecules)([mol]);
}
};
exports.getNewCoords = getNewCoords;
const removeHs = (structure) => {
const [mol] = (0, molecule_1.get_molecules)([structure], globalThis.workerRDKit);
if (!mol)
return null;
try {
const mdl = mol.remove_hs();
return (0, exports.getNewCoords)(mdl, false);
}
finally {
(0, molecule_1.release_molecules)([mol]);
}
};
exports.removeHs = removeHs;
const addHs = (structure) => {
const [mol] = (0, molecule_1.get_molecules)([structure], globalThis.workerRDKit);
if (!mol)
return null;
try {
let mdl = mol.add_hs();
mdl = (0, exports.getNewCoords)(mdl, false);
return mdl;
}
finally {
(0, molecule_1.release_molecules)([mol]);
}
};
exports.addHs = addHs;
const getStereoTags = (structure) => {
const [mol] = (0, molecule_1.get_molecules)([structure], globalThis.workerRDKit);
if (!mol)
throw new Error('@iktos-oss/rdkit-provider: mol is null');
try {
const stereoTags = mol.get_stereo_tags();
const { CIP_atoms, CIP_bonds } = JSON.parse(stereoTags);
return {
CIP_atoms,
CIP_bonds,
};
}
catch (e) {
console.error(e);
throw new Error('@iktos-oss/rdkit-provider: could not get stereo tags');
}
finally {
(0, molecule_1.release_molecules)([mol]);
}
};
exports.getStereoTags = getStereoTags;
const _validateSource = (structure, sourceNotation) => {
switch (sourceNotation) {
case 'molblock':
return (0, exports.isValidMolBlock)(structure);
case 'smiles':
return (0, exports.isValidSmiles)(structure);
case 'smarts':
return (0, exports.isValidSmarts)(structure);
default:
throw new Error(`@iktos-oss/rdkit-provider: validate ${sourceNotation} not implemented`);
}
};
//# sourceMappingURL=chem.js.map