Skip to content

Commit

Permalink
feat: predict carbon from molfile
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Aug 2, 2021
1 parent 1b97d71 commit 8706b0e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 39 deletions.
22 changes: 11 additions & 11 deletions src/component/modal/PredictSpectraModal.tsx
Expand Up @@ -75,12 +75,12 @@ const FREQUENCIES: Array<{ key: number; value: number; label: string }> = [
];

const INITIAL_VALUE = {
'1h': { from: -1, to: 12 },
'13c': { from: -5, to: 220 },
'1H': { from: -1, to: 12 },
'13C': { from: -5, to: 220 },
frequency: 400,
spectra: {
'1h': true,
'13c': false,
'1H': true,
'13C': false,
cosy: false,
hsqc: false,
hmbc: false,
Expand Down Expand Up @@ -147,13 +147,13 @@ function PredictSpectraModal({
<IsotopesViewer value="1H" className="custom-label" />
<FormikInput
label="From"
name="1h.from"
name="1H.from"
type="number"
style={{ label: { padding: '0 10px 0 0' } }}
/>
<FormikInput
label="To"
name="1h.to"
name="1H.to"
type="number"
style={{ label: { padding: '0 10px' } }}
/>
Expand All @@ -162,13 +162,13 @@ function PredictSpectraModal({
<IsotopesViewer value="13C" className="custom-label" />
<FormikInput
label="From"
name="13c.from"
name="13C.from"
type="number"
style={{ label: { padding: '0 10px 0 0' } }}
/>
<FormikInput
label="To"
name="13c.to"
name="13C.to"
type="number"
style={{ label: { padding: '0 10px' } }}
/>
Expand All @@ -181,12 +181,12 @@ function PredictSpectraModal({
style={{ justifyContent: 'space-between' }}
>
<div className="row">
<FormikCheckBox name="spectra.1h" />
<FormikCheckBox name="spectra.1H" />
<IsotopesViewer value="1H" className="nucleus-label" />
</div>
<div className="row">
<FormikCheckBox disabled name="spectra.13c" />
<IsotopesViewer value="13C" className="nucleus-label disabled" />
<FormikCheckBox name="spectra.13C" />
<IsotopesViewer value="13C" className="nucleus-label" />
</div>
<div className="row">
<FormikCheckBox disabled name="spectra.cosy" />
Expand Down
14 changes: 8 additions & 6 deletions src/component/reducer/Reducer.ts
@@ -1,8 +1,7 @@
import { Draft, produce } from 'immer';
import { buildCorrelationData, Types } from 'nmr-correlation';
import { predictProton } from 'nmr-processing';
import OCL from 'openchemlib/full';

import predictSpectrum from '../../data/PredictionManager';
import * as SpectraManager from '../../data/SpectraManager';
import { Range } from '../../data/data1d/Spectrum1D';
import migrateData from '../../data/migration';
Expand Down Expand Up @@ -254,9 +253,12 @@ export function dispatchMiddleware(dispatch) {
break;
}
case types.PREDICT_SPECTRA: {
const molecule = OCL.Molecule.fromMolfile(action.payload.mol.molfile);
void predictProton(molecule, {}).then((result) => {
action.payload.fromMolfile = result;
const {
mol: { molfile },
options,
} = action.payload;
void predictSpectrum(molfile, options).then((result) => {
action.payload.data = result;
action.payload.usedColors = usedColors;
dispatch(action);
});
Expand Down Expand Up @@ -413,7 +415,7 @@ function innerSpectrumReducer(draft: Draft<State>, action) {
return MoleculeActions.deleteMoleculeHandler(draft, action);

case types.PREDICT_SPECTRA:
return MoleculeActions.predictSpectraFromMolculeHandler(draft, action);
return MoleculeActions.predictSpectraFromMoleculeHandler(draft, action);

case types.SET_CORRELATIONS_MF:
return CorrelationsActions.handleSetMF(draft, action.payload);
Expand Down
47 changes: 25 additions & 22 deletions src/component/reducer/actions/MoleculeActions.ts
Expand Up @@ -13,7 +13,6 @@ import { DISPLAYER_MODE } from '../core/Constants';
import { handleUnlinkRange } from './RangesActions';
import { setActiveTab } from './ToolsActions';
import { handleUnlinkZone } from './ZonesActions';
import { setZoom } from './Zoom';

function addMoleculeHandler(draft: Draft<State>, molfile) {
MoleculeManager.addMolfile(draft.molecules, molfile);
Expand All @@ -38,28 +37,32 @@ function deleteMoleculeHandler(draft: Draft<State>, action) {
draft.molecules.splice(moleculeIndex, 1);
}

function predictSpectraFromMolculeHandler(draft: Draft<State>, action) {
const { fromMolfile, options, usedColors } = action.payload;
const { x, y } = signalsToXY(fromMolfile.signals, {});
let id: any = null;
if (options.spectra['1h']) {
const datum = initiateDatum1D(
{
data: { x, im: null, re: y },
info: { nucleus: '1H' },
},
usedColors,
);
id = datum.id;
datum.ranges.values = mapRanges(fromMolfile.ranges, datum);
updateIntegralRanges(datum);
draft.data.push(datum);
const activeSpectrum = { id, index: draft.data.length - 1 };
draft.tabActiveSpectrum['1H'] = activeSpectrum;
draft.activeSpectrum = activeSpectrum;
function predictSpectraFromMoleculeHandler(draft: Draft<State>, action) {
const { data, options, usedColors } = action.payload;

for (const predictedDatum of data) {
const { nucleus, signals, ranges } = predictedDatum;
if (['1H', '13C'].includes(nucleus)) {
const { x, y } = signalsToXY(signals, { ...options[nucleus] });
const datum = initiateDatum1D(
{
data: { x, im: null, re: y },
info: { nucleus },
},
usedColors,
);
datum.ranges.values = mapRanges(ranges, datum);
updateIntegralRanges(datum);
draft.data.push(datum);

draft.tabActiveSpectrum[nucleus] = {
id: datum.id,
index: draft.data.length - 1,
};
}
}

setActiveTab(draft);
setZoom(draft, 0.9, id);

draft.isLoading = false;
}
Expand All @@ -68,5 +71,5 @@ export {
addMoleculeHandler,
setMoleculeHandler,
deleteMoleculeHandler,
predictSpectraFromMolculeHandler,
predictSpectraFromMoleculeHandler,
};
42 changes: 42 additions & 0 deletions src/data/PredictionManager.ts
@@ -0,0 +1,42 @@
import { predictProton as predictProtonSpectrum } from 'nmr-processing';
import OCL from 'openchemlib/full';

const baseURL = 'https://nmr-prediction.service.zakodium.com';

async function predictCarbon(molfile: string): Promise<any> {
const response = await fetch(`${baseURL}/v1/predict/carbon`, {
headers: {
accept: 'application/json',
'content-type': 'application/json',
},
body: JSON.stringify({ molfile }),
method: 'POST',
});
return (await response.json()).data;
}

async function predictProton(molfile: string): Promise<any> {
const molecule = OCL.Molecule.fromMolfile(molfile);
return predictProtonSpectrum(molecule, {});
}

export default async function predictSpectrum(molfile: string, options) {
const promises: any = [];
if (options.spectra['1H']) {
const promises1H = predictProton(molfile).then((result) => ({
nucleus: '1H',
...result,
}));
promises.push(promises1H);
}

if (options.spectra['13C']) {
const promises13C = predictCarbon(molfile).then((result) => ({
nucleus: '13C',
...result,
}));
promises.push(promises13C);
}

return Promise.all(promises);
}

0 comments on commit 8706b0e

Please sign in to comment.