UNPKG

@iktos-oss/molecule-representation

Version:

exports interactif molecule represnetations as react components

203 lines (199 loc) 10.6 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.get_svg_from_smarts = exports.get_svg = void 0; /* 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. */ const rdkit_provider_1 = require("@iktos-oss/rdkit-provider"); const constants_1 = require("../constants"); const svg_computation_1 = require("./svg-computation"); const DEFAULT_DRAWING_DETAILS = { bondLineWidth: 1, backgroundColour: [1, 1, 1, 0], highlightColour: constants_1.HIGHLIGHT_RDKIT_COLORS[0], highlightRadius: 0.3, fixedBondLength: 50, }; const get_svg = (props, worker) => __awaiter(void 0, void 0, void 0, function* () { var _a; const { smiles: initialSmiles, width, height, details = {}, atomsToHighlight = [], bondsToHighlight = [], isClickable, clickableAtoms, alignmentDetails, heatmapAtomsWeights = {}, highlightColor, canonicalize, generateClickableHotspots = false, } = props; if (!initialSmiles) return null; let smiles = initialSmiles; if (canonicalize) { const { canonicalForm } = yield (0, rdkit_provider_1.getCanonicalFormForStructure)(worker, { structure: initialSmiles }); if (!canonicalForm) return null; smiles = canonicalForm; } let clickableSvg; if (isClickable && !generateClickableHotspots) { clickableSvg = yield (0, exports.get_svg)(Object.assign(Object.assign({}, props), { smiles, generateClickableHotspots: true }), worker); } const moleculeDetails = yield (0, rdkit_provider_1.getMoleculeDetails)(worker, { smiles, returnFullDetails: true }); if (!moleculeDetails) return null; const baseAtomColors = getHighlightColors(atomsToHighlight, highlightColor); const baseBondColors = getHighlightColors(bondsToHighlight, highlightColor); const { highlightAtomRadii, heatmapHighlightColors } = computeHeatmapOptions(heatmapAtomsWeights, highlightColor); const { atomHighlights: alignmentAtomColors, bondHighlights: alignmentBondColors } = alignmentDetails ? yield computeAlignmentHighlights(worker, smiles, alignmentDetails) : { atomHighlights: {}, bondHighlights: {} }; const clickableAtomBgColors = computeClickableAtomHighlights(moleculeDetails, clickableAtoms, generateClickableHotspots); const highlightAtomColors = Object.assign(Object.assign(Object.assign(Object.assign({}, clickableAtomBgColors), heatmapHighlightColors), alignmentAtomColors), baseAtomColors); const highlightBondColors = Object.assign(Object.assign({}, alignmentBondColors), baseBondColors); const atomsToDraw = generateClickableHotspots ? Object.keys(highlightAtomColors).map(Number) : [ ...atomsToHighlight.flat(), ...((_a = clickableAtoms === null || clickableAtoms === void 0 ? void 0 : clickableAtoms.clickableAtomsIds) !== null && _a !== void 0 ? _a : []), ...Object.keys(heatmapAtomsWeights) .map((n) => { var _a; return (_a = parseInt(n)) !== null && _a !== void 0 ? _a : null; }) .filter((n) => n !== null), ]; const bondsToDraw = Object.keys(highlightBondColors).map(Number); const rdkitDrawingOptions = Object.assign(Object.assign(Object.assign({}, DEFAULT_DRAWING_DETAILS), { width, height, atoms: atomsToDraw, bonds: bondsToDraw, highlightAtomColors, highlightBondColors, highlightAtomRadii }), details); try { let { svg } = yield (0, rdkit_provider_1.getSvg)(worker, { smiles, drawingDetails: rdkitDrawingOptions, alignmentDetails, }); if (svg && clickableSvg) { svg = (0, svg_computation_1.appendEllipsesToSvg)(svg, clickableSvg); } if (!svg && alignmentDetails) { console.error('@iktos-oss/molecule-representation: Failed to draw with alignment, falling back to no alignment.'); const { svg: svgRetry } = yield (0, rdkit_provider_1.getSvg)(worker, { smiles, drawingDetails: rdkitDrawingOptions }); return svgRetry; } return svg; } catch (error) { console.error(error); return null; } }); exports.get_svg = get_svg; const get_svg_from_smarts = (props, drawAsSmiles = false, worker) => __awaiter(void 0, void 0, void 0, function* () { if (!worker || !props.smarts) return null; const { canonicalForm: canonicalSmarts } = yield (0, rdkit_provider_1.getCanonicalFormForStructure)(worker, { structure: props.smarts, useQMol: true, }); if (!canonicalSmarts) return null; if (drawAsSmiles) { const { svg } = yield (0, rdkit_provider_1.getSvg)(worker, Object.assign(Object.assign({}, props), { smiles: props.smarts })); return svg; } const { svg } = yield (0, rdkit_provider_1.getSvgFromSmarts)(worker, Object.assign(Object.assign({}, props), { smarts: canonicalSmarts })); return svg; }); exports.get_svg_from_smarts = get_svg_from_smarts; function getHighlightColors(items = [], highlightColor) { // give each array of atoms a color, enabling multi-color highlights const highlightColors = {}; const limit = constants_1.HIGHLIGHT_RDKIT_COLORS.length; items.forEach((item, i) => { const color = highlightColor !== null && highlightColor !== void 0 ? highlightColor : constants_1.HIGHLIGHT_RDKIT_COLORS[i % limit]; if (!item) return; for (const index of item) { highlightColors[index] = color; } }); return highlightColors; } function computeHeatmapOptions(heatmapAtomsWeights = {}, highlightColor) { const highlightAtomRadii = {}; const heatmapHighlightColors = {}; const weights = Object.values(heatmapAtomsWeights); if (weights.length === 0) return { highlightAtomRadii, heatmapHighlightColors }; const maxWeight = Math.max(...weights); const minWeight = Math.min(...weights); const weightRange = maxWeight - minWeight || 1; const minRadius = 0.15; const maxRadius = 0.5; for (const [atomIdx, weight] of Object.entries(heatmapAtomsWeights)) { const normalizedWeight = (weight - minWeight) / weightRange; highlightAtomRadii[+atomIdx] = minRadius + normalizedWeight * (maxRadius - minRadius); } const atomIndices = [Object.keys(heatmapAtomsWeights).map(Number)]; Object.assign(heatmapHighlightColors, getHighlightColors(atomIndices, highlightColor)); return { highlightAtomRadii, heatmapHighlightColors }; } function computeAlignmentHighlights(worker, smiles, alignmentDetails) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const atomHighlights = {}; const bondHighlights = {}; if (!alignmentDetails.highlightColor || !alignmentDetails.molBlock) { return { atomHighlights, bondHighlights }; } const { structure: smarts } = yield (0, rdkit_provider_1.convertMolNotation)(worker, { moleculeString: alignmentDetails.molBlock, targetNotation: 'smarts', sourceNotation: 'molblock', }); if (!smarts) return { atomHighlights, bondHighlights }; const match = yield (0, rdkit_provider_1.getMatchingSubstructure)(worker, { structure: smiles, substructure: smarts }); if (!match) return { atomHighlights, bondHighlights }; const color = alignmentDetails.highlightColor; (_a = match.matchingAtoms) === null || _a === void 0 ? void 0 : _a.forEach((idx) => (atomHighlights[idx] = color)); (_b = match.matchingBonds) === null || _b === void 0 ? void 0 : _b.forEach((idx) => (bondHighlights[idx] = color)); return { atomHighlights, bondHighlights }; }); } function computeClickableAtomHighlights(moleculeDetails, clickableAtoms, isForClickableOverlay) { var _a, _b; if (!moleculeDetails) return {}; const clickableAtomColors = {}; const backgroundColor = (_a = clickableAtoms === null || clickableAtoms === void 0 ? void 0 : clickableAtoms.clickableAtomsBackgroundColor) !== null && _a !== void 0 ? _a : constants_1.TRANSPARANT_RDKIT_COLOR; const clickableAtomIds = (_b = clickableAtoms === null || clickableAtoms === void 0 ? void 0 : clickableAtoms.clickableAtomsIds) !== null && _b !== void 0 ? _b : [...Array(moleculeDetails.NumHeavyAtoms).keys()]; for (const atomId of clickableAtomIds) { clickableAtomColors[atomId] = isForClickableOverlay ? constants_1.TRANSPARANT_RDKIT_COLOR : backgroundColor; } if (isForClickableOverlay) { for (let i = 0; i < moleculeDetails.NumHeavyAtoms; i++) { if (!clickableAtomIds.includes(i)) { clickableAtomColors[i] = constants_1.TRANSPARANT_RDKIT_COLOR; } } } return clickableAtomColors; } //# sourceMappingURL=draw.js.map