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: add missing peaks when use multiplet-analysis #2814

Merged
merged 3 commits into from
Jan 5, 2024
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
100 changes: 3 additions & 97 deletions src/data/data1d/Spectrum1D/ranges/detectSignals.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { DataXY } from 'cheminfo-types';
import { xGetFromToIndex } from 'ml-spectra-processing';
import { analyseMultiplet } from 'multiplet-analysis';
import {
NMRPeak1DWithShapeID,
signalJoinCouplings,
xyAutoPeaksPicking,
xyAutoRangesPicking,
} from 'nmr-processing';
import { xyAutoRangesPicking } from 'nmr-processing';

import { detectSignalsByMultipletAnalysis } from './detectSignalsByMultipletAnalysis';

const MAX_LENGTH = 4092;
export default function detectSignals(
Expand Down Expand Up @@ -49,93 +45,3 @@ export default function detectSignals(
});
}
}

function detectSignalsByMultipletAnalysis(data: DataXY, options: any) {
const { fromIndex, toIndex, frequency } = options;
const dataRoi = {
x: data.x.slice(fromIndex, toIndex),
y: data.y.slice(fromIndex, toIndex),
};

const result = analyseMultiplet(dataRoi, {
frequency,
minimalResolution: 0.1,
maxTestedJ: 17,
checkSymmetryFirst: true,
takeBestPartMultiplet: true,
correctVerticalOffset: true,
symmetrizeEachStep: false,
decreasingJvalues: true,
makeShortCutForSpeed: true,
});

if (result && result.chemShift === undefined) return [];

const { delta, js } = joinCouplings(result);

let cs = 0;
let area = 0;
for (let i = 0; i < dataRoi.x.length; i++) {
cs += dataRoi.x[i] * dataRoi.y[i];
area += dataRoi.y[i];
}
cs /= area;

const multiplicity = getMultiplicity(js, { cs, data, delta, frequency });

return [
{
multiplicity,
kind: 'signal',
delta: cs,
js,
diaIDs: [],
},
];
}

function getMultiplicity(
js,
options: { cs: number; delta: number; frequency: number; data: DataXY },
) {
const { cs, delta, frequency, data } = options;

if (js?.length > 0) {
return js.map((j) => j.multiplicity).join('');
}
//check if the massive center is closer to the shift from multiplet-analysis,
//if true, is it possibly a singlet.
if (Math.abs(cs - delta) / cs < 1e-3) {
let peaks: NMRPeak1DWithShapeID[] = [];

if (js?.length === 0) {
peaks = xyAutoPeaksPicking(data, { frequency });
}

if (peaks.length === 1) return 's';
}

return 'm';
}

function joinCouplings(result: any) {
const { chemShift: delta, js } = result;
let jCouplings = js;
if (js.length > 1) {
try {
jCouplings = signalJoinCouplings(
{
delta,
js,
},
{ tolerance: 0.6, ignoreDiaIDs: true },
).js;
} catch (error) {
reportError(error);
}
}
return {
delta,
js: jCouplings,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { DataXY } from 'cheminfo-types';
import { analyseMultiplet } from 'multiplet-analysis';
import {
NMRPeak1DWithShapeID,
signalJoinCouplings,
xyAutoPeaksPicking,
} from 'nmr-processing';

export function detectSignalsByMultipletAnalysis(data: DataXY, options: any) {
const { fromIndex, toIndex, frequency } = options;
const dataRoi = {
x: data.x.slice(fromIndex, toIndex),
y: data.y.slice(fromIndex, toIndex),
};

const result = analyseMultiplet(dataRoi, {
frequency,
minimalResolution: 0.1,
maxTestedJ: 17,
checkSymmetryFirst: true,
takeBestPartMultiplet: true,
correctVerticalOffset: true,
symmetrizeEachStep: false,
decreasingJvalues: true,
makeShortCutForSpeed: true,
});

if (result && result.chemShift === undefined) return [];

const { delta, js } = joinCouplings(result);

let cs = 0;
let area = 0;
for (let i = 0; i < dataRoi.x.length; i++) {
cs += dataRoi.x[i] * dataRoi.y[i];
area += dataRoi.y[i];
}
cs /= area;

const peaks = xyAutoPeaksPicking(dataRoi, { frequency });

const multiplicity = getMultiplicity(js, { cs, delta, peaks });

return [
{
multiplicity,
kind: 'signal',
delta: cs,
js,
peaks,
diaIDs: [],
},
];
}

function getMultiplicity(
js,
options: { cs: number; delta: number; peaks: NMRPeak1DWithShapeID[] },
) {
const { cs, delta, peaks } = options;

if (js?.length > 0) {
return js.map((j) => j.multiplicity).join('');
}
//check if the massive center is closer to the shift from multiplet-analysis,
//if true, is it possibly a singlet.

Check warning on line 66 in src/data/data1d/Spectrum1D/ranges/detectSignalsByMultipletAnalysis.ts

View check run for this annotation

Codecov / codecov/patch

src/data/data1d/Spectrum1D/ranges/detectSignalsByMultipletAnalysis.ts#L65-L66

Added lines #L65 - L66 were not covered by tests
return peaks.length === 1 && Math.abs(cs - delta) / cs < 1e-3 ? 's' : 'm';
}

function joinCouplings(result: any) {
const { chemShift: delta, js } = result;
let jCouplings = js;
if (js.length > 1) {
try {
jCouplings = signalJoinCouplings(
{
delta,
js,
},
{ tolerance: 0.6, ignoreDiaIDs: true },
).js;
} catch (error) {
reportError(error);
}

Check warning on line 84 in src/data/data1d/Spectrum1D/ranges/detectSignalsByMultipletAnalysis.ts

View check run for this annotation

Codecov / codecov/patch

src/data/data1d/Spectrum1D/ranges/detectSignalsByMultipletAnalysis.ts#L83-L84

Added lines #L83 - L84 were not covered by tests
}
return {
delta,
js: jCouplings,
};
}
Loading