@iktos-oss/molecule-representation
Version:
exports interactif molecule represnetations as react components
223 lines (219 loc) • 11.3 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.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MoleculeRepresentation = void 0;
const react_1 = __importStar(require("react"));
const rdkit_provider_1 = require("@iktos-oss/rdkit-provider");
const Zoom_1 = require("../Zoom");
const Spinner_1 = require("../Spinner");
const compare_1 = require("../../utils/compare");
const utils_1 = require("../../utils");
exports.MoleculeRepresentation = (0, react_1.memo)((_a) => {
var { addAtomIndices = false, atomsToHighlight, highlightColor, bondsToHighlight, attachedSvgIcons, clickableAtoms, details, height, id, onAtomClick, onBondClick, onMouseHover, onMouseLeave, smarts, smiles, alignmentDetails, style, showLoadingSpinner = false, showSmartsAsSmiles = false, canonicalize = false, width, zoomable = false, displayZoomToolbar = Zoom_1.DisplayZoomToolbar.ON_HOVER, heatmapAtomsWeights, atomsStyles, bondsStyles } = _a, restOfProps = __rest(_a, ["addAtomIndices", "atomsToHighlight", "highlightColor", "bondsToHighlight", "attachedSvgIcons", "clickableAtoms", "details", "height", "id", "onAtomClick", "onBondClick", "onMouseHover", "onMouseLeave", "smarts", "smiles", "alignmentDetails", "style", "showLoadingSpinner", "showSmartsAsSmiles", "canonicalize", "width", "zoomable", "displayZoomToolbar", "heatmapAtomsWeights", "atomsStyles", "bondsStyles"]);
const { worker } = (0, rdkit_provider_1.useRDKit)();
const [svgContent, setSvgContent] = (0, react_1.useState)('');
const isClickable = (0, react_1.useMemo)(() => !!onAtomClick || !!onBondClick, [onAtomClick, onBondClick]);
const isHoverable = (0, react_1.useMemo)(() => !!onMouseHover || !!onMouseLeave, [onMouseHover, onMouseLeave]);
const isClickableOrHoverable = (0, react_1.useMemo)(() => isClickable || isHoverable, [isClickable, isHoverable]);
(0, react_1.useEffect)(() => {
// the compute svg effect
if (!worker)
return;
const computeSvg = () => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const structureToDraw = (smarts || smiles);
if (!structureToDraw)
return;
const drawingDetails = {
smiles: structureToDraw,
width,
height,
details: Object.assign(Object.assign({}, details), { addAtomIndices }),
alignmentDetails,
highlightColor,
atomsToHighlight,
bondsToHighlight,
heatmapAtomsWeights,
isClickable,
clickableAtoms,
canonicalize,
};
const isSmartsAValidSmiles = showSmartsAsSmiles && !!smarts && (yield (0, rdkit_provider_1.isValidSmiles)(worker, { smiles: smarts })).isValid;
let svg = smarts && !isSmartsAValidSmiles
? yield (0, utils_1.get_svg_from_smarts)({ smarts, width, height }, worker)
: yield (0, utils_1.get_svg)(drawingDetails, worker);
if (!svg)
return;
if (smarts) {
// effects are not yet supported for smarts, only compute/add effects for smiles
setSvgContent(svg);
return;
}
// add effects to svg
const moleculeDetails = yield (0, rdkit_provider_1.getMoleculeDetails)(worker, {
smiles: structureToDraw,
returnFullDetails: true,
});
if (!moleculeDetails)
return;
svg = (0, utils_1.applyUserStyles)({
svg,
numAtoms: moleculeDetails.NumHeavyAtoms,
atomsStyles,
bondsStyles,
});
if (isClickableOrHoverable) {
const atomsHitbox = (0, utils_1.buildAtomsHitboxes)({
numAtoms: moleculeDetails.NumHeavyAtoms,
svg,
clickableAtoms: (clickableAtoms === null || clickableAtoms === void 0 ? void 0 : clickableAtoms.clickableAtomsIds) ? new Set(clickableAtoms.clickableAtomsIds) : null,
isClickable: !!onAtomClick,
});
const bondsHitbox = (0, utils_1.buildBondsHitboxes)({
numAtoms: moleculeDetails.NumHeavyAtoms,
svg,
isClickable: !!onBondClick,
});
svg =
atomsHitbox.length || bondsHitbox.length
? ((_a = (0, utils_1.appendHitboxesToSvg)(svg, atomsHitbox, bondsHitbox)) !== null && _a !== void 0 ? _a : svg)
: svg;
}
if (attachedSvgIcons) {
const iconsCoords = (0, utils_1.computeIconsCoords)({
svg,
attachedIcons: attachedSvgIcons,
});
svg = (_b = (0, utils_1.appendSvgIconsToSvg)(svg, iconsCoords)) !== null && _b !== void 0 ? _b : svg;
}
setSvgContent(svg);
});
computeSvg();
}, [
showSmartsAsSmiles,
smiles,
smarts,
onAtomClick,
onBondClick,
atomsToHighlight,
addAtomIndices,
details,
isClickable,
bondsToHighlight,
width,
height,
worker,
clickableAtoms,
alignmentDetails,
isClickableOrHoverable,
heatmapAtomsWeights,
highlightColor,
attachedSvgIcons,
atomsStyles,
bondsStyles,
canonicalize,
]);
const handleOnClick = (0, react_1.useCallback)((event) => {
const clickedId = event.target.id;
if (isClickable) {
event.preventDefault();
event.stopPropagation();
}
if (onBondClick && clickedId && (0, utils_1.isIdClickedABond)(clickedId)) {
onBondClick((0, utils_1.getClickedBondIdentifiersFromId)(clickedId), event);
}
if (onAtomClick && clickedId && (0, utils_1.isIdClickedAnAtom)(clickedId)) {
onAtomClick((0, utils_1.getAtomIdxFromClickableId)(clickedId), event);
}
}, [onAtomClick, onBondClick, isClickable]);
const handleOnMouseHover = (0, react_1.useCallback)((event) => {
if (!onMouseHover) {
return;
}
const hoveredElementId = event.target.id;
onMouseHover({
atomId: (0, utils_1.isIdClickedAnAtom)(hoveredElementId) ? (0, utils_1.getAtomIdxFromClickableId)(hoveredElementId) : undefined,
bondIdentifiers: (0, utils_1.isIdClickedABond)(hoveredElementId)
? (0, utils_1.getClickedBondIdentifiersFromId)(hoveredElementId)
: undefined,
}, event);
}, [onMouseHover]);
const handleOnMouseLeave = (0, react_1.useCallback)((event) => {
if (!onMouseLeave) {
return;
}
onMouseLeave(event);
}, [onMouseLeave]);
if (!svgContent) {
if (showLoadingSpinner)
return react_1.default.createElement(Spinner_1.Spinner, { width: width, height: height });
return null;
}
const svgElement = (0, utils_1.createSvgElement)(svgContent, Object.assign(Object.assign({ 'data-testid': 'clickable-molecule' }, restOfProps), { className: `molecule ${isClickableOrHoverable ? utils_1.CLICKABLE_MOLECULE_CLASSNAME : ''}`, height,
id, onClick: handleOnClick, onMouseOver: handleOnMouseHover, onMouseLeave: handleOnMouseLeave, style, title: smiles, width }));
return zoomable ? (react_1.default.createElement(Zoom_1.ZoomWrapper, { displayZoomToolbar: displayZoomToolbar, width: width, height: height }, svgElement)) : (svgElement);
}, (prevProps, currentPros) => (0, compare_1.isEqual)(prevProps, currentPros));
exports.MoleculeRepresentation.displayName = 'MoleculeRepresentation';
exports.default = exports.MoleculeRepresentation;
//# sourceMappingURL=MoleculeRepresentation.js.map