UNPKG

@iktos-oss/molecule-representation

Version:

exports interactif molecule represnetations as react components

209 lines (205 loc) 9.64 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /* 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 { getSvg, getMoleculeDetails, getCanonicalFormForStructure, getMatchingSubstructure, getSvgFromSmarts, convertMolNotation, } from '@iktos-oss/rdkit-provider'; import { HIGHLIGHT_RDKIT_COLORS, TRANSPARANT_RDKIT_COLOR } from '../constants'; export const get_svg = (params, worker) => __awaiter(void 0, void 0, void 0, function* () { var _a; if (!params.smiles) return null; let smiles = params.smiles; if (params.canonicalize) { const { canonicalForm: canonicalSmiles } = yield getCanonicalFormForStructure(worker, { structure: params.smiles, }); if (!canonicalSmiles) return null; smiles = canonicalSmiles; } const { width, height, details = {}, atomsToHighlight, bondsToHighlight, clickableAtoms, alignmentDetails, heatmapAtomsWeights, highlightColor, } = params; const highlightBondColors = getHighlightColors(bondsToHighlight, highlightColor); const highlightAtomColors = getHighlightColors(atomsToHighlight, highlightColor); const moleculeDetails = yield getMoleculeDetails(worker, { smiles, returnFullDetails: true }); if (moleculeDetails) { setHighlightColorForClickableMolecule({ nbAtoms: moleculeDetails.NumHeavyAtoms, clickableAtoms, atomsToHighlight, highlightAtomColors, }); } if (!moleculeDetails) return null; const atomsToDrawWithHighlight = [...Array(moleculeDetails.NumHeavyAtoms).keys()]; const bondsToDrawWithHighlight = (_a = bondsToHighlight === null || bondsToHighlight === void 0 ? void 0 : bondsToHighlight.flat()) !== null && _a !== void 0 ? _a : []; const highlightAtomRadii = {}; if (heatmapAtomsWeights && Object.keys(heatmapAtomsWeights).length) { const maxWeight = Math.max(...Object.values(heatmapAtomsWeights)); const minWeight = Math.min(...Object.values(heatmapAtomsWeights)); const minRadius = 0.15; const maxRadius = 0.5; const weightRange = maxWeight - minWeight || 1; for (const [atomIdx, weight] of Object.entries(heatmapAtomsWeights)) { const normalizedWeight = (weight - minWeight) / weightRange; const radius = minRadius + normalizedWeight * (maxRadius - minRadius); highlightAtomRadii[+atomIdx] = radius; } const heatmapHighlightColors = getHighlightColors([Object.keys(heatmapAtomsWeights).map((v) => +v)], highlightColor); Object.assign(highlightAtomColors, heatmapHighlightColors); } try { if (alignmentDetails) { yield addAlignmentFromMolBlock({ smiles, alignmentDetails, highlightAtomColors, atomsToDrawWithHighlight, highlightBondColors, bondsToDrawWithHighlight, worker, }); } const rdkitDrawingOptions = Object.assign(Object.assign(Object.assign({}, DEFAULT_DRAWING_DETAILS), { width, height, atoms: atomsToDrawWithHighlight, bonds: bondsToDrawWithHighlight, highlightAtomColors, highlightBondColors, highlightAtomRadii }), details); const { svg } = yield getSvg(worker, { smiles, drawingDetails: rdkitDrawingOptions, alignmentDetails, }); if (!svg && !!alignmentDetails) { console.error('@iktos-oss/molecule-representation: failed to draw molecule, falling back to no alignment drawing'); const { svg: svgRetryWithNoAlignment } = yield getSvg(worker, { smiles, drawingDetails: rdkitDrawingOptions, }); return svgRetryWithNoAlignment; } return svg; } catch (error) { console.error(error); return null; } }); export const get_svg_from_smarts = (params, worker) => __awaiter(void 0, void 0, void 0, function* () { if (!worker) return null; if (!params.smarts) return null; const { canonicalForm: canonicalSmarts } = yield getCanonicalFormForStructure(worker, { structure: params.smarts, useQMol: true, }); if (!canonicalSmarts) return null; const { svg } = yield getSvgFromSmarts(worker, Object.assign(Object.assign({}, params), { smarts: canonicalSmarts })); return svg; }); const getHighlightColors = (items, highlightColor) => { // give each array of atoms a color, enabling multi-color highlights const highlightColors = {}; const limit = HIGHLIGHT_RDKIT_COLORS.length; let cpt = 0; for (const item of items !== null && items !== void 0 ? items : []) { const color = highlightColor !== null && highlightColor !== void 0 ? highlightColor : HIGHLIGHT_RDKIT_COLORS[cpt++ % limit]; if (!item) continue; for (const atomIdx of item) { highlightColors[atomIdx] = color; } } return highlightColors; }; const addAlignmentFromMolBlock = ({ smiles, alignmentDetails, highlightAtomColors, atomsToDrawWithHighlight, highlightBondColors, bondsToDrawWithHighlight, worker, }) => __awaiter(void 0, void 0, void 0, function* () { if (!alignmentDetails.highlightColor) { return; } const { structure: alignmentDetailsSmilesToMatch } = yield convertMolNotation(worker, { moleculeString: alignmentDetails.molBlock, targetNotation: 'smarts', sourceNotation: 'molblock', }); if (!alignmentDetailsSmilesToMatch) return; const matchDetails = yield getMatchingSubstructure(worker, { structure: smiles, substructure: alignmentDetailsSmilesToMatch, }); if (!matchDetails) return; const { matchingAtoms, matchingBonds } = matchDetails; if (matchingAtoms) { addAtomsOrBondsToHighlight({ indicies: matchingAtoms, highlightColors: highlightAtomColors, indiciesToHighlight: atomsToDrawWithHighlight, color: alignmentDetails.highlightColor, }); } if (matchingBonds) { addAtomsOrBondsToHighlight({ indicies: matchingBonds, highlightColors: highlightBondColors, indiciesToHighlight: bondsToDrawWithHighlight, color: alignmentDetails.highlightColor, }); } }); const addAtomsOrBondsToHighlight = ({ indicies, indiciesToHighlight, highlightColors, color = HIGHLIGHT_RDKIT_COLORS[0], }) => { for (const idx of indicies) { highlightColors[idx] = color; if (!indiciesToHighlight.includes(idx)) { indiciesToHighlight.push(idx); } } }; const setHighlightColorForClickableMolecule = ({ nbAtoms, clickableAtoms, atomsToHighlight, highlightAtomColors, }) => { var _a, _b; const clickableAtomsBackgroundColor = (_a = clickableAtoms === null || clickableAtoms === void 0 ? void 0 : clickableAtoms.clickableAtomsBackgroundColor) !== null && _a !== void 0 ? _a : TRANSPARANT_RDKIT_COLOR; const clickableAtomIds = (_b = clickableAtoms === null || clickableAtoms === void 0 ? void 0 : clickableAtoms.clickableAtomsIds) !== null && _b !== void 0 ? _b : [...Array(nbAtoms).keys()]; for (let i = 0; i < nbAtoms; i++) { if (atomsToHighlight === null || atomsToHighlight === void 0 ? void 0 : atomsToHighlight.flat().includes(i)) continue; if (clickableAtomIds.includes(i)) { highlightAtomColors[i] = clickableAtomsBackgroundColor; } else { highlightAtomColors[i] = TRANSPARANT_RDKIT_COLOR; } } }; const DEFAULT_DRAWING_DETAILS = { bondLineWidth: 1, backgroundColour: [1, 1, 1, 0], highlightColour: HIGHLIGHT_RDKIT_COLORS[0], highlightRadius: 0.3, fixedBondLength: 50, }; //# sourceMappingURL=draw.js.map