UNPKG

@iktos-oss/rdkit-provider

Version:

exports an initialized RDKit instance, with helper functions

258 lines (254 loc) 9.26 kB
/* 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. */ import { get_molecules, get_query_molecules, release_molecules } from './molecule'; export const getSvg = ({ smiles, drawingDetails, alignmentDetails, }) => { const molecules = alignmentDetails ? get_molecules([smiles, alignmentDetails.molBlock], globalThis.workerRDKit) : 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); } release_molecules(molecules); return svg; }; export const getSvgFromSmarts = ({ smarts, width, height }) => { const [smartsMol] = get_query_molecules([smarts], globalThis.workerRDKit); if (!smartsMol) return null; const svg = smartsMol.get_svg(width, height); release_molecules([smartsMol]); return svg; }; export function getMoleculeDetails({ smiles, returnFullDetails }) { const [mol] = get_molecules([smiles], globalThis.workerRDKit); if (!mol) return null; const details = JSON.parse(mol.get_descriptors()); 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, }; } export const getCanonicalFormForStructure = ({ structure, useQMol = false, }) => { return convertMolNotation({ moleculeString: structure, targetNotation: useQMol ? 'smarts' : 'smiles', useQMol, sourceNotation: undefined, }); }; export const isChiral = (smiles) => { const [mol] = 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 { release_molecules([mol]); } }; export const getMorganFp = ({ smiles, options, }) => { const [mol] = 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 { release_molecules([mol]); } }; export const isValidSmiles = (smiles) => { if (!smiles) return false; const [mol] = get_molecules([smiles], globalThis.workerRDKit); if (!mol) return false; const isValid = mol.is_valid(); release_molecules([mol]); return isValid; }; export const isValidSmarts = (smarts) => { if (!smarts) return false; const [mol] = get_query_molecules([smarts], globalThis.workerRDKit); if (!mol) return false; const isValid = mol.is_valid(); release_molecules([mol]); return isValid; }; export const hasMatchingSubstructure = ({ smiles, substructure }) => { const [smilesMol] = get_molecules([smiles], globalThis.workerRDKit); const [smartsMol] = 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; release_molecules([smilesMol, smartsMol]); return matchDetailsNotEmpty; }; export const getMatchingSubstructure = ({ structure, substructure }) => { const [mol] = get_molecules([structure], globalThis.workerRDKit); const [molToMach] = get_query_molecules([substructure], globalThis.workerRDKit); if (!mol || !molToMach) return null; const { atoms, bonds } = JSON.parse(mol.get_substruct_match(molToMach)); release_molecules([mol, molToMach]); return { matchingAtoms: atoms, matchingBonds: bonds }; }; export const isValidMolBlock = (mdl) => { if (!mdl.includes('M END')) return false; const [mol] = get_molecules([mdl], globalThis.workerRDKit); if (!mol) return false; try { return mol.is_valid(); } finally { release_molecules([mol]); } }; /** * Convert molecule structure to given notation (smiles, smarts, molblock, ...) * Warning: some notations like inchi and aromatic_form don't work with qmol */ export 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 ? get_query_molecules([moleculeString], globalThis.workerRDKit) : 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 { release_molecules([mol]); } }; export const getNewCoords = (structure, useCoordGen) => { const [mol] = 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 { release_molecules([mol]); } }; export const removeHs = (structure) => { const [mol] = get_molecules([structure], globalThis.workerRDKit); if (!mol) return null; try { const mdl = mol.remove_hs(); return getNewCoords(mdl, false); } finally { release_molecules([mol]); } }; export const addHs = (structure) => { const [mol] = get_molecules([structure], globalThis.workerRDKit); if (!mol) return null; try { let mdl = mol.add_hs(); mdl = getNewCoords(mdl, false); return mdl; } finally { release_molecules([mol]); } }; export const getStereoTags = (structure) => { const [mol] = 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 { release_molecules([mol]); } }; const _validateSource = (structure, sourceNotation) => { switch (sourceNotation) { case 'molblock': return isValidMolBlock(structure); case 'smiles': return isValidSmiles(structure); case 'smarts': return isValidSmarts(structure); default: throw new Error(`@iktos-oss/rdkit-provider: validate ${sourceNotation} not implemented`); } }; //# sourceMappingURL=chem.js.map