Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remap existing assignments when molecule is updated #2779

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading
Loading