Skip to content

Commit

Permalink
feat: remap existing assignments when molecule is edited (#2779)
Browse files Browse the repository at this point in the history
Closes: #1329
  • Loading branch information
targos committed Dec 1, 2023
1 parent 683e511 commit 840c3e5
Show file tree
Hide file tree
Showing 7 changed files with 1,459 additions and 22 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -90,7 +90,7 @@
"nmredata": "^0.9.7",
"numeral": "^2.0.6",
"openchemlib": "^8.7.1",
"openchemlib-utils": "^5.4.0",
"openchemlib-utils": "^5.6.0",
"papaparse": "^5.4.1",
"re-resizable": "6.9.11",
"react-d3-utils": "^1.0.0",
Expand Down
42 changes: 33 additions & 9 deletions src/component/modal/MoleculeStructureEditorModal.tsx
@@ -1,5 +1,7 @@
/** @jsxImportSource @emotion/react */
import { useState, useEffect, useCallback } from 'react';
import { Molecule } from 'openchemlib/full';
import { TopicMolecule } from 'openchemlib-utils';
import { useState, useEffect, useCallback, useMemo } from 'react';
import { StructureEditor, IStructureEditorProps } from 'react-ocl/full';
import { ConfirmModal, Modal, useOnOff } from 'react-science/ui';

Expand All @@ -23,15 +25,28 @@ function MoleculeStructureEditorModal(
isOpen = false,
} = props;

const initialMolfile = selectedMolecule?.molfile;
const initialEnhancedMolfile = useMemo(() => {
if (!initialMolfile) {
return null;
}
// Add mapNo to molfile, so we can remap diaIDs after edition.
const molecule = Molecule.fromMolfile(initialMolfile);
const topicMolecule = new TopicMolecule(molecule);
topicMolecule.ensureMapNo();
const newMolfile = topicMolecule.toMolfile({ version: 3 });
return { molfile: newMolfile, topicMolecule };
}, [initialMolfile]);

const [molfile, setMolfile] = useState<string | null>(null);
const dispatch = useDispatch();
useEffect(() => {
if (selectedMolecule) {
setMolfile(selectedMolecule.molfile);
if (initialEnhancedMolfile) {
setMolfile(initialEnhancedMolfile.molfile);
} else {
setMolfile(null);
}
}, [selectedMolecule]);
}, [initialEnhancedMolfile]);

const cb = useCallback<Exclude<IStructureEditorProps['onChange'], undefined>>(
(newMolfile, molecule) => {
Expand All @@ -50,14 +65,18 @@ function MoleculeStructureEditorModal(

const handleSave = useCallback(() => {
if (molfile) {
if (selectedMolecule) {
if (selectedMolecule && initialEnhancedMolfile) {
const { id, label } = selectedMolecule;
const editedMolecule = Molecule.fromMolfile(molfile);
const mappings =
initialEnhancedMolfile.topicMolecule.getDiaIDsMapping(editedMolecule);
dispatch({
type: 'SET_MOLECULE',
payload: {
molfile,
id,
label,
mappings,
},
});
onClose('replace');
Expand All @@ -72,7 +91,14 @@ function MoleculeStructureEditorModal(
onClose('new');
}
}
}, [molfile, selectedMolecule, dispatch, floatMoleculeOnSave, onClose]);
}, [
molfile,
selectedMolecule,
initialEnhancedMolfile,
dispatch,
floatMoleculeOnSave,
onClose,
]);

return (
<ConfirmModal
Expand All @@ -85,7 +111,7 @@ function MoleculeStructureEditorModal(
>
<Modal.Body>
<StructureEditor
initialMolfile={selectedMolecule?.molfile}
initialMolfile={initialEnhancedMolfile?.molfile}
svgMenu
fragment={false}
onChange={cb}
Expand Down Expand Up @@ -115,5 +141,3 @@ export function useMoleculeEditor(floatMoleculeOnSave = false) {
);
return { modal, openMoleculeEditor };
}

export default MoleculeStructureEditorModal;
21 changes: 15 additions & 6 deletions src/component/reducer/actions/MoleculeActions.ts
Expand Up @@ -2,7 +2,7 @@ import { Logger } from 'cheminfo-types';
import { FifoLogger } from 'fifo-logger';
import { Draft } from 'immer';
import OCL from 'openchemlib/full';
import { nbLabileH, getAtoms } from 'openchemlib-utils';
import { nbLabileH, getAtoms, TopicMolecule } from 'openchemlib-utils';

import {
PredictedSpectraResult,
Expand All @@ -25,6 +25,7 @@ import { ActionType } from '../types/ActionType';
import { unlinkRange } from './RangesActions';
import { setActiveTab } from './ToolsActions';
import { unlinkZone } from './ZonesActions';
import { deepReplaceDiaIDs } from './utilities/deepReplaceDiaIDs';

interface AddMoleculeProps {
molfile: string;
Expand All @@ -35,7 +36,12 @@ type AddMoleculesAction = ActionType<
'ADD_MOLECULES',
{ molecules: StateMolecule[] }
>;
type SetMoleculeAction = ActionType<'SET_MOLECULE', Required<StateMolecule>>;
type SetMoleculeAction = ActionType<
'SET_MOLECULE',
Required<StateMolecule> & {
mappings?: ReturnType<TopicMolecule['getDiaIDsMapping']>;
}
>;
type DeleteMoleculeAction = ActionType<
'DELETE_MOLECULE',
{ id: string; assignmentData: AssignmentContext }
Expand Down Expand Up @@ -108,8 +114,8 @@ function handleAddMolecules(draft: Draft<State>, action: AddMoleculesAction) {
}
}

function setMolecule(draft: Draft<State>, props: Required<StateMolecule>) {
const { id, label, molfile } = props;
function setMolecule(draft: Draft<State>, props: SetMoleculeAction['payload']) {
const { id, label, molfile, mappings } = props;
MoleculeManager.setMolfile(draft.molecules, {
id,
label,
Expand All @@ -121,6 +127,10 @@ function setMolecule(draft: Draft<State>, props: Required<StateMolecule>) {
*/
const index = draft.molecules.findIndex((molecule) => molecule.id === id);

if (mappings) {
deepReplaceDiaIDs(draft, mappings);
}

changeSpectraRelativeSum(
draft,
id,
Expand Down Expand Up @@ -328,8 +338,7 @@ function getFloatingMoleculeInitialPosition(id: string, draft: Draft<State>) {
const rows = Math.floor(height / baseHeight);
const moleculeKeys = Object.keys(molecules);

let index = 0;

let index: number;
if (!molecules[id]) {
index = moleculeKeys.length - 1;
} else {
Expand Down

0 comments on commit 840c3e5

Please sign in to comment.