nmr-processing 7.4.3 → 8.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/assignment/getAssignments.js +1 -1
- package/lib/assignment/getAssignments.js.map +1 -1
- package/lib/assignment/utils/getAssignment/checkIDs.d.ts +1 -1
- package/lib/assignment/utils/getAssignment/checkIDs.js.map +1 -1
- package/lib/databases/getDatabase.d.ts +6 -1
- package/lib/databases/getDatabase.js +13 -3
- package/lib/databases/getDatabase.js.map +1 -1
- package/lib/index.d.ts +4 -2
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/peaks/NMRPeak1D.d.ts +0 -1
- package/lib/peaks/peaksToRanges.d.ts +16 -0
- package/lib/peaks/peaksToRanges.js +2 -1
- package/lib/peaks/peaksToRanges.js.map +1 -1
- package/lib/peaks/solventSuppression.d.ts +3 -0
- package/lib/peaks/solventSuppression.js +155 -0
- package/lib/peaks/solventSuppression.js.map +1 -0
- package/lib/peaks/util/jAnalyzer.d.ts +1 -1
- package/lib/peaks/util/jAnalyzer.js +3 -2
- package/lib/peaks/util/jAnalyzer.js.map +1 -1
- package/lib/prediction/predictAllSpectra.d.ts +22 -4
- package/lib/prediction/predictAllSpectra.js +102 -16
- package/lib/prediction/predictAllSpectra.js.map +1 -1
- package/lib/ranges/markSolventSignal.d.ts +3 -0
- package/lib/ranges/markSolventSignal.js +107 -0
- package/lib/ranges/markSolventSignal.js.map +1 -0
- package/lib/ranges/rangesToXY.js +27 -3
- package/lib/ranges/rangesToXY.js.map +1 -1
- package/lib/signals/addDummySignals.d.ts +2 -0
- package/lib/signals/addDummySignals.js +56 -0
- package/lib/signals/addDummySignals.js.map +1 -0
- package/lib/signals/hackSignalsToXY.js +2 -48
- package/lib/signals/hackSignalsToXY.js.map +1 -1
- package/lib/signals/simulation/simulate1D.d.ts +1 -39
- package/lib/signals/simulation/simulate1D.js +13 -240
- package/lib/signals/simulation/simulate1D.js.map +1 -1
- package/lib/signals/simulation/simulateXYPeaks.d.ts +47 -0
- package/lib/signals/simulation/simulateXYPeaks.js +246 -0
- package/lib/signals/simulation/simulateXYPeaks.js.map +1 -0
- package/lib/utilities/getFrequency.d.ts +1 -1
- package/lib/utilities/getFrequency.js +4 -4
- package/lib/utilities/getFrequency.js.map +1 -1
- package/lib/xyz/NMRSignal2D.d.ts +3 -3
- package/lib/xyz/util/formatZone.d.ts +3 -0
- package/lib/xyz/util/formatZone.js +38 -0
- package/lib/xyz/util/formatZone.js.map +1 -0
- package/lib/xyz/xyzAutoZonesPicking.d.ts +1 -2
- package/lib/xyz/xyzAutoZonesPicking.js +2 -34
- package/lib/xyz/xyzAutoZonesPicking.js.map +1 -1
- package/lib/xyz/xyzJResAnalyzer.d.ts +6 -1
- package/lib/xyz/xyzJResAnalyzer.js +9 -8
- package/lib/xyz/xyzJResAnalyzer.js.map +1 -1
- package/lib-esm/assignment/getAssignments.js +1 -1
- package/lib-esm/assignment/getAssignments.js.map +1 -1
- package/lib-esm/assignment/utils/getAssignment/checkIDs.js.map +1 -1
- package/lib-esm/databases/getDatabase.js +13 -3
- package/lib-esm/databases/getDatabase.js.map +1 -1
- package/lib-esm/index.js +3 -1
- package/lib-esm/index.js.map +1 -1
- package/lib-esm/peaks/peaksToRanges.js +1 -1
- package/lib-esm/peaks/peaksToRanges.js.map +1 -1
- package/lib-esm/peaks/solventSuppression.js +151 -0
- package/lib-esm/peaks/solventSuppression.js.map +1 -0
- package/lib-esm/peaks/util/jAnalyzer.js +3 -2
- package/lib-esm/peaks/util/jAnalyzer.js.map +1 -1
- package/lib-esm/prediction/predictAllSpectra.js +102 -16
- package/lib-esm/prediction/predictAllSpectra.js.map +1 -1
- package/lib-esm/ranges/markSolventSignal.js +100 -0
- package/lib-esm/ranges/markSolventSignal.js.map +1 -0
- package/lib-esm/ranges/rangesToXY.js +27 -3
- package/lib-esm/ranges/rangesToXY.js.map +1 -1
- package/lib-esm/signals/addDummySignals.js +52 -0
- package/lib-esm/signals/addDummySignals.js.map +1 -0
- package/lib-esm/signals/hackSignalsToXY.js +2 -48
- package/lib-esm/signals/hackSignalsToXY.js.map +1 -1
- package/lib-esm/signals/simulation/simulate1D.js +14 -238
- package/lib-esm/signals/simulation/simulate1D.js.map +1 -1
- package/lib-esm/signals/simulation/simulateXYPeaks.js +239 -0
- package/lib-esm/signals/simulation/simulateXYPeaks.js.map +1 -0
- package/lib-esm/utilities/getFrequency.js +1 -1
- package/lib-esm/utilities/getFrequency.js.map +1 -1
- package/lib-esm/xyz/util/formatZone.js +34 -0
- package/lib-esm/xyz/util/formatZone.js.map +1 -0
- package/lib-esm/xyz/xyzAutoZonesPicking.js +1 -33
- package/lib-esm/xyz/xyzAutoZonesPicking.js.map +1 -1
- package/lib-esm/xyz/xyzJResAnalyzer.js +9 -8
- package/lib-esm/xyz/xyzJResAnalyzer.js.map +1 -1
- package/package.json +14 -12
- package/src/assignment/getAssignments.ts +1 -1
- package/src/assignment/utils/getAssignment/checkIDs.ts +1 -1
- package/src/databases/getDatabase.ts +19 -2
- package/src/index.ts +5 -2
- package/src/peaks/NMRPeak1D.ts +0 -1
- package/src/peaks/peaksToRanges.ts +1 -1
- package/src/peaks/solventSuppression.ts +209 -0
- package/src/peaks/util/jAnalyzer.ts +5 -3
- package/src/prediction/predictAllSpectra.ts +151 -14
- package/src/ranges/markSolventSignal.ts +121 -0
- package/src/ranges/rangesToXY.ts +33 -4
- package/src/signals/addDummySignals.ts +77 -0
- package/src/signals/hackSignalsToXY.ts +2 -72
- package/src/signals/simulation/simulate1D.ts +14 -319
- package/src/signals/simulation/simulateXYPeaks.ts +332 -0
- package/src/utilities/getFrequency.ts +1 -1
- package/src/xyz/NMRSignal2D.ts +3 -3
- package/src/xyz/util/formatZone.ts +36 -0
- package/src/xyz/xyzAutoZonesPicking.ts +1 -35
- package/src/xyz/xyzJResAnalyzer.ts +14 -7
- package/lib/constants/gyromagneticRatio.d.ts +0 -6
- package/lib/constants/gyromagneticRatio.js +0 -26
- package/lib/constants/gyromagneticRatio.js.map +0 -1
- package/lib-esm/constants/gyromagneticRatio.js +0 -23
- package/lib-esm/constants/gyromagneticRatio.js.map +0 -1
- package/src/constants/gyromagneticRatio.ts +0 -49
|
@@ -1,54 +1,10 @@
|
|
|
1
|
-
import binarySearch from 'binary-search';
|
|
2
1
|
import { DataXY } from 'cheminfo-types';
|
|
3
|
-
import {
|
|
4
|
-
import type { Matrix as MatrixClassType } from 'ml-matrix';
|
|
5
|
-
import type { Shape1D } from 'ml-peak-shape-generator';
|
|
6
|
-
import { SparseMatrix } from 'ml-sparse-matrix';
|
|
7
|
-
import { SpectrumGenerator } from 'spectrum-generator';
|
|
2
|
+
import { generateSpectrum } from 'spectrum-generator';
|
|
8
3
|
|
|
9
4
|
import type { SpinSystem } from '../spinSystem';
|
|
10
5
|
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
const smallValue = 1e-2;
|
|
14
|
-
|
|
15
|
-
interface Simulate1DOptions {
|
|
16
|
-
/**
|
|
17
|
-
* The linewidth of the output spectrum, expresed in Hz.
|
|
18
|
-
* @default 1
|
|
19
|
-
*/
|
|
20
|
-
lineWidth?: number;
|
|
21
|
-
/**
|
|
22
|
-
* Maximum number of atoms on each cluster that can be considered to be simulated together. It affects the the quality and speed of the simulation.
|
|
23
|
-
* @default 8
|
|
24
|
-
*/
|
|
25
|
-
maxClusterSize?: number;
|
|
26
|
-
/**
|
|
27
|
-
* The frequency in Mhz of the fake spectrometer that records the spectrum.
|
|
28
|
-
* @default 400
|
|
29
|
-
*/
|
|
30
|
-
frequency?: number;
|
|
31
|
-
/**
|
|
32
|
-
* The low limit of the ordinate variable.
|
|
33
|
-
* @default 0
|
|
34
|
-
*/
|
|
35
|
-
from?: number;
|
|
36
|
-
/**
|
|
37
|
-
* The upper limit of the ordinate variable.
|
|
38
|
-
* @default 10
|
|
39
|
-
*/
|
|
40
|
-
to?: number;
|
|
41
|
-
/**
|
|
42
|
-
* Number of points of the output spectrum.
|
|
43
|
-
* @default 16K
|
|
44
|
-
*/
|
|
45
|
-
nbPoints?: number;
|
|
46
|
-
/**
|
|
47
|
-
* Shape options
|
|
48
|
-
* @default {kind:'gaussian'}
|
|
49
|
-
*/
|
|
50
|
-
shape?: Shape1D;
|
|
51
|
-
}
|
|
6
|
+
import type { Simulate1DOptions } from './simulateXYPeaks';
|
|
7
|
+
import { simulateXYPeaks } from './simulateXYPeaks';
|
|
52
8
|
|
|
53
9
|
/**
|
|
54
10
|
* This function simulates a one dimensional nmr spectrum. This function returns an array containing the relative intensities of the spectrum in the specified simulation window (from-to).
|
|
@@ -63,7 +19,6 @@ export default function simulate1D(
|
|
|
63
19
|
): DataXY {
|
|
64
20
|
let {
|
|
65
21
|
lineWidth = 1,
|
|
66
|
-
maxClusterSize = 8,
|
|
67
22
|
frequency: frequencyMHz = 400,
|
|
68
23
|
from = 0,
|
|
69
24
|
to = 10,
|
|
@@ -75,277 +30,17 @@ export default function simulate1D(
|
|
|
75
30
|
|
|
76
31
|
let peakWidth = lineWidth / frequencyMHz;
|
|
77
32
|
|
|
78
|
-
|
|
79
|
-
from,
|
|
80
|
-
to,
|
|
81
|
-
nbPoints,
|
|
82
|
-
shape,
|
|
83
|
-
peakWidthFct: () => peakWidth,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const chemicalShifts = spinSystem.chemicalShifts.slice();
|
|
87
|
-
for (let i = 0; i < chemicalShifts.length; i++) {
|
|
88
|
-
chemicalShifts[i] = chemicalShifts[i] * frequencyMHz;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const multiplicity = spinSystem.multiplicity;
|
|
92
|
-
for (const cluster of spinSystem.clusters) {
|
|
93
|
-
let clusterFake = cluster.map((cluster) =>
|
|
94
|
-
cluster < 0 ? -cluster - 1 : cluster,
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
let weight = 1;
|
|
98
|
-
let sumI = 0;
|
|
99
|
-
let frequencies: number[] = [];
|
|
100
|
-
let intensities: number[] = [];
|
|
101
|
-
if (cluster.length > maxClusterSize) {
|
|
102
|
-
// This is a single spin, but the cluster exceeds the maxClusterSize criteria
|
|
103
|
-
// we use the simple multiplicity algorithm
|
|
104
|
-
// Add the central peak. It will be split with every single J coupling.
|
|
105
|
-
let index = 0;
|
|
106
|
-
while (cluster[index++] < 0);
|
|
107
|
-
index = cluster[index - 1];
|
|
108
|
-
frequencies.push(-chemicalShifts[index]);
|
|
109
|
-
for (let i = 0; i < cluster.length; i++) {
|
|
110
|
-
if (cluster[i] < 0) {
|
|
111
|
-
let jc = spinSystem.couplingConstants.get(index, clusterFake[i]) / 2;
|
|
112
|
-
let currentSize = frequencies.length;
|
|
113
|
-
for (let j = 0; j < currentSize; j++) {
|
|
114
|
-
frequencies.push(frequencies[j] + jc);
|
|
115
|
-
frequencies[j] -= jc;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
frequencies.sort((a, b) => a - b);
|
|
121
|
-
sumI = frequencies.length;
|
|
122
|
-
weight = 1;
|
|
123
|
-
|
|
124
|
-
for (let i = 0; i < sumI; i++) {
|
|
125
|
-
intensities.push(1);
|
|
126
|
-
}
|
|
127
|
-
} else {
|
|
128
|
-
const hamiltonian = getHamiltonian(
|
|
129
|
-
chemicalShifts,
|
|
130
|
-
spinSystem.couplingConstants,
|
|
131
|
-
multiplicity,
|
|
132
|
-
spinSystem.connectivity,
|
|
133
|
-
clusterFake,
|
|
134
|
-
);
|
|
135
|
-
const hamSize = hamiltonian.rows;
|
|
136
|
-
// TODO: add support for sparse matrix in matrix types.
|
|
137
|
-
// @ts-expect-error sparse matrix not supported
|
|
138
|
-
const evd = new EVD(hamiltonian);
|
|
139
|
-
const V = evd.eigenvectorMatrix;
|
|
140
|
-
const diagB = evd.realEigenvalues;
|
|
141
|
-
const assignmentMatrix = new SparseMatrix(hamSize, hamSize);
|
|
142
|
-
const multLen = cluster.length;
|
|
143
|
-
weight = 0;
|
|
144
|
-
for (let n = 0; n < multLen; n++) {
|
|
145
|
-
const L = getPauliMatrix(multiplicity[clusterFake[n]]);
|
|
146
|
-
|
|
147
|
-
let temp = 1;
|
|
148
|
-
for (let j = 0; j < n; j++) {
|
|
149
|
-
temp *= multiplicity[clusterFake[j]];
|
|
150
|
-
}
|
|
151
|
-
const A = SparseMatrix.eye(temp);
|
|
152
|
-
|
|
153
|
-
temp = 1;
|
|
154
|
-
for (let j = n + 1; j < multLen; j++) {
|
|
155
|
-
temp *= multiplicity[clusterFake[j]];
|
|
156
|
-
}
|
|
157
|
-
const B = SparseMatrix.eye(temp);
|
|
158
|
-
const tempMat = A.kroneckerProduct(L.m).kroneckerProduct(B);
|
|
159
|
-
if (cluster[n] >= 0) {
|
|
160
|
-
assignmentMatrix.add(tempMat.mul(cluster[n] + 1));
|
|
161
|
-
weight++;
|
|
162
|
-
} else {
|
|
163
|
-
assignmentMatrix.add(tempMat.mul(cluster[n]));
|
|
164
|
-
}
|
|
165
|
-
}
|
|
33
|
+
const xyPeaks = simulateXYPeaks(spinSystem, options);
|
|
166
34
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
return v;
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
let rhoip2 = rhoip.clone();
|
|
181
|
-
assignmentMatrix.forEachNonZero((i: number, j: number, v: number) => {
|
|
182
|
-
if (v < 0) {
|
|
183
|
-
for (let k = 0; k < V.columns; k++) {
|
|
184
|
-
let element = V.get(j, k);
|
|
185
|
-
if (element !== 0) {
|
|
186
|
-
rhoip2.set(i, k, rhoip2.get(i, k) + element);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return v;
|
|
191
|
-
});
|
|
192
|
-
const tV = V.transpose();
|
|
193
|
-
|
|
194
|
-
rhoip = tV.mmul(rhoip);
|
|
195
|
-
const sparseRhoip = new SparseMatrix(rhoip.to2DArray(), {
|
|
196
|
-
threshold: smallValue,
|
|
197
|
-
});
|
|
198
|
-
triuTimesAbs(sparseRhoip, smallValue);
|
|
199
|
-
|
|
200
|
-
rhoip2 = tV.mmul(rhoip2);
|
|
201
|
-
const sparseRhoip2 = new SparseMatrix(rhoip2.to2DArray(), {
|
|
202
|
-
threshold: smallValue,
|
|
203
|
-
});
|
|
204
|
-
sparseRhoip2.forEachNonZero((i, j, v) => {
|
|
205
|
-
return v;
|
|
206
|
-
});
|
|
207
|
-
triuTimesAbs(sparseRhoip2, smallValue);
|
|
208
|
-
sparseRhoip2.forEachNonZero((i, j, v) => {
|
|
209
|
-
let val = rhoip.get(i, j);
|
|
210
|
-
val = Math.min(Math.abs(val), Math.abs(v));
|
|
211
|
-
val *= val;
|
|
212
|
-
|
|
213
|
-
sumI += val;
|
|
214
|
-
let valFreq = diagB[i] - diagB[j];
|
|
215
|
-
let insertIn = binarySearch(
|
|
216
|
-
frequencies,
|
|
217
|
-
valFreq,
|
|
218
|
-
(a: number, b: number) => a - b,
|
|
219
|
-
);
|
|
220
|
-
if (insertIn < 0) {
|
|
221
|
-
frequencies.splice(-1 - insertIn, 0, valFreq);
|
|
222
|
-
intensities.splice(-1 - insertIn, 0, val);
|
|
223
|
-
} else {
|
|
224
|
-
intensities[insertIn] += val;
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const numFreq = frequencies.length;
|
|
230
|
-
|
|
231
|
-
if (numFreq > 0) {
|
|
232
|
-
weight /= sumI;
|
|
233
|
-
const diff = lineWidth / 64;
|
|
234
|
-
let valFreq = frequencies[0];
|
|
235
|
-
let inte = intensities[0];
|
|
236
|
-
let count = 1;
|
|
237
|
-
for (let i = 1; i < numFreq; i++) {
|
|
238
|
-
if (Math.abs(frequencies[i] - valFreq / count) < diff) {
|
|
239
|
-
inte += intensities[i];
|
|
240
|
-
valFreq += frequencies[i];
|
|
241
|
-
count++;
|
|
242
|
-
} else {
|
|
243
|
-
spectrumGenerator.addPeak({
|
|
244
|
-
x: -valFreq / count / frequencyMHz,
|
|
245
|
-
y: inte * weight,
|
|
246
|
-
});
|
|
247
|
-
valFreq = frequencies[i];
|
|
248
|
-
inte = intensities[i];
|
|
249
|
-
count = 1;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
spectrumGenerator.addPeak({
|
|
254
|
-
x: -valFreq / count / frequencyMHz,
|
|
255
|
-
y: inte * weight,
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
return spectrumGenerator.getSpectrum();
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function triuTimesAbs(A: SparseMatrix, val: number) {
|
|
263
|
-
A.forEachNonZero((i, j, v) => {
|
|
264
|
-
if (i > j) return 0;
|
|
265
|
-
if (Math.abs(v) <= val) return 0;
|
|
266
|
-
return v;
|
|
35
|
+
return generateSpectrum(xyPeaks, {
|
|
36
|
+
generator: {
|
|
37
|
+
from,
|
|
38
|
+
to,
|
|
39
|
+
nbPoints,
|
|
40
|
+
},
|
|
41
|
+
peakOptions: {
|
|
42
|
+
shape,
|
|
43
|
+
width: peakWidth,
|
|
44
|
+
},
|
|
267
45
|
});
|
|
268
46
|
}
|
|
269
|
-
/**
|
|
270
|
-
* Create a hamiltonian matrix for the given spinsystem
|
|
271
|
-
* @param {Array} chemicalShifts - An array containing the chemical shift in Hz
|
|
272
|
-
* @param {Array} couplingConstants - An array containing the coupling constants in Hz
|
|
273
|
-
* @param {Array} multiplicity - An array specifiying the multiplicities of each scalar coupling
|
|
274
|
-
* @param {Array} conMatrix - A one step connectivity matrix for the given spin system
|
|
275
|
-
* @param {Array} cluster - An binary array specifiying the spins to be considered for this hamiltonial
|
|
276
|
-
* @return {object}
|
|
277
|
-
*/
|
|
278
|
-
function getHamiltonian(
|
|
279
|
-
chemicalShifts: number[],
|
|
280
|
-
couplingConstants: MatrixClassType,
|
|
281
|
-
multiplicity: number[],
|
|
282
|
-
conMatrix: MatrixClassType,
|
|
283
|
-
cluster: number[],
|
|
284
|
-
) {
|
|
285
|
-
let hamSize = 1;
|
|
286
|
-
for (const element of cluster) {
|
|
287
|
-
hamSize *= multiplicity[element];
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
const clusterHam = new SparseMatrix(hamSize, hamSize);
|
|
291
|
-
|
|
292
|
-
for (let pos = 0; pos < cluster.length; pos++) {
|
|
293
|
-
let n = cluster[pos];
|
|
294
|
-
|
|
295
|
-
const L = getPauliMatrix(multiplicity[n]);
|
|
296
|
-
|
|
297
|
-
let A1, B1;
|
|
298
|
-
let temp = 1;
|
|
299
|
-
for (let i = 0; i < pos; i++) {
|
|
300
|
-
temp *= multiplicity[cluster[i]];
|
|
301
|
-
}
|
|
302
|
-
A1 = SparseMatrix.eye(temp);
|
|
303
|
-
|
|
304
|
-
temp = 1;
|
|
305
|
-
for (let i = pos + 1; i < cluster.length; i++) {
|
|
306
|
-
temp *= multiplicity[cluster[i]];
|
|
307
|
-
}
|
|
308
|
-
B1 = SparseMatrix.eye(temp);
|
|
309
|
-
|
|
310
|
-
const alpha = chemicalShifts[n];
|
|
311
|
-
const kronProd = A1.kroneckerProduct(L.z).kroneckerProduct(B1);
|
|
312
|
-
clusterHam.add(kronProd.mul(alpha));
|
|
313
|
-
for (let pos2 = 0; pos2 < cluster.length; pos2++) {
|
|
314
|
-
const k = cluster[pos2];
|
|
315
|
-
if (conMatrix.get(n, k) === 1) {
|
|
316
|
-
const S = getPauliMatrix(multiplicity[k]);
|
|
317
|
-
|
|
318
|
-
let A2, B2;
|
|
319
|
-
let temp = 1;
|
|
320
|
-
for (let i = 0; i < pos2; i++) {
|
|
321
|
-
temp *= multiplicity[cluster[i]];
|
|
322
|
-
}
|
|
323
|
-
A2 = SparseMatrix.eye(temp);
|
|
324
|
-
|
|
325
|
-
temp = 1;
|
|
326
|
-
for (let i = pos2 + 1; i < cluster.length; i++) {
|
|
327
|
-
temp *= multiplicity[cluster[i]];
|
|
328
|
-
}
|
|
329
|
-
B2 = SparseMatrix.eye(temp);
|
|
330
|
-
|
|
331
|
-
const kron1 = A1.kroneckerProduct(L.x)
|
|
332
|
-
.kroneckerProduct(B1)
|
|
333
|
-
.mmul(A2.kroneckerProduct(S.x).kroneckerProduct(B2));
|
|
334
|
-
kron1.add(
|
|
335
|
-
A1.kroneckerProduct(L.y)
|
|
336
|
-
.kroneckerProduct(B1)
|
|
337
|
-
.mul(-1)
|
|
338
|
-
.mmul(A2.kroneckerProduct(S.y).kroneckerProduct(B2)),
|
|
339
|
-
);
|
|
340
|
-
kron1.add(
|
|
341
|
-
A1.kroneckerProduct(L.z)
|
|
342
|
-
.kroneckerProduct(B1)
|
|
343
|
-
.mmul(A2.kroneckerProduct(S.z).kroneckerProduct(B2)),
|
|
344
|
-
);
|
|
345
|
-
|
|
346
|
-
clusterHam.add(kron1.mul(couplingConstants.get(n, k) / 2));
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
return clusterHam;
|
|
351
|
-
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import binarySearch from 'binary-search';
|
|
2
|
+
import { Matrix, EVD } from 'ml-matrix';
|
|
3
|
+
import type { Matrix as MatrixClassType } from 'ml-matrix';
|
|
4
|
+
import type { Shape1D } from 'ml-peak-shape-generator';
|
|
5
|
+
import { SparseMatrix } from 'ml-sparse-matrix';
|
|
6
|
+
|
|
7
|
+
import type { SpinSystem } from '../spinSystem';
|
|
8
|
+
|
|
9
|
+
import getPauliMatrix from './getPauliMatrix';
|
|
10
|
+
|
|
11
|
+
const smallValue = 1e-2;
|
|
12
|
+
|
|
13
|
+
export interface Simulate1DOptions {
|
|
14
|
+
/**
|
|
15
|
+
* The linewidth of the output spectrum, expresed in Hz.
|
|
16
|
+
* @default 1
|
|
17
|
+
*/
|
|
18
|
+
lineWidth?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Maximum number of atoms on each cluster that can be considered to be simulated together. It affects the the quality and speed of the simulation.
|
|
21
|
+
* @default 8
|
|
22
|
+
*/
|
|
23
|
+
maxClusterSize?: number;
|
|
24
|
+
/**
|
|
25
|
+
* The frequency in Mhz of the fake spectrometer that records the spectrum.
|
|
26
|
+
* @default 400
|
|
27
|
+
*/
|
|
28
|
+
frequency?: number;
|
|
29
|
+
/**
|
|
30
|
+
* The low limit of the ordinate variable.
|
|
31
|
+
* @default 0
|
|
32
|
+
*/
|
|
33
|
+
from?: number;
|
|
34
|
+
/**
|
|
35
|
+
* The upper limit of the ordinate variable.
|
|
36
|
+
* @default 10
|
|
37
|
+
*/
|
|
38
|
+
to?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Number of points of the output spectrum.
|
|
41
|
+
* @default 16K
|
|
42
|
+
*/
|
|
43
|
+
nbPoints?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Shape options
|
|
46
|
+
* @default {kind:'gaussian'}
|
|
47
|
+
*/
|
|
48
|
+
shape?: Shape1D;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function simulateXYPeaks(
|
|
52
|
+
/**
|
|
53
|
+
* The SpinSystem object to be simulated
|
|
54
|
+
*/
|
|
55
|
+
spinSystem: SpinSystem,
|
|
56
|
+
options: Simulate1DOptions = {},
|
|
57
|
+
) {
|
|
58
|
+
let {
|
|
59
|
+
lineWidth = 1,
|
|
60
|
+
maxClusterSize = 8,
|
|
61
|
+
frequency: frequencyMHz = 400,
|
|
62
|
+
} = options;
|
|
63
|
+
|
|
64
|
+
const chemicalShifts = spinSystem.chemicalShifts.slice();
|
|
65
|
+
for (let i = 0; i < chemicalShifts.length; i++) {
|
|
66
|
+
chemicalShifts[i] = chemicalShifts[i] * frequencyMHz;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const multiplicity = spinSystem.multiplicity;
|
|
70
|
+
|
|
71
|
+
const xyPeaks = [];
|
|
72
|
+
for (const cluster of spinSystem.clusters) {
|
|
73
|
+
let clusterFake = cluster.map((cluster) =>
|
|
74
|
+
cluster < 0 ? -cluster - 1 : cluster,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
let weight = 1;
|
|
78
|
+
let sumI = 0;
|
|
79
|
+
let frequencies: number[] = [];
|
|
80
|
+
let intensities: number[] = [];
|
|
81
|
+
if (cluster.length > maxClusterSize) {
|
|
82
|
+
// This is a single spin, but the cluster exceeds the maxClusterSize criteria
|
|
83
|
+
// we use the simple multiplicity algorithm
|
|
84
|
+
// Add the central peak. It will be split with every single J coupling.
|
|
85
|
+
let index = 0;
|
|
86
|
+
while (cluster[index++] < 0);
|
|
87
|
+
index = cluster[index - 1];
|
|
88
|
+
frequencies.push(-chemicalShifts[index]);
|
|
89
|
+
for (let i = 0; i < cluster.length; i++) {
|
|
90
|
+
if (cluster[i] < 0) {
|
|
91
|
+
let jc = spinSystem.couplingConstants.get(index, clusterFake[i]) / 2;
|
|
92
|
+
let currentSize = frequencies.length;
|
|
93
|
+
for (let j = 0; j < currentSize; j++) {
|
|
94
|
+
frequencies.push(frequencies[j] + jc);
|
|
95
|
+
frequencies[j] -= jc;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
frequencies.sort((a, b) => a - b);
|
|
101
|
+
sumI = frequencies.length;
|
|
102
|
+
weight = 1;
|
|
103
|
+
|
|
104
|
+
for (let i = 0; i < sumI; i++) {
|
|
105
|
+
intensities.push(1);
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
const hamiltonian = getHamiltonian(
|
|
109
|
+
chemicalShifts,
|
|
110
|
+
spinSystem.couplingConstants,
|
|
111
|
+
multiplicity,
|
|
112
|
+
spinSystem.connectivity,
|
|
113
|
+
clusterFake,
|
|
114
|
+
);
|
|
115
|
+
const hamSize = hamiltonian.rows;
|
|
116
|
+
// TODO: add support for sparse matrix in matrix types.
|
|
117
|
+
// @ts-expect-error sparse matrix not supported
|
|
118
|
+
const evd = new EVD(hamiltonian);
|
|
119
|
+
const V = evd.eigenvectorMatrix;
|
|
120
|
+
const diagB = evd.realEigenvalues;
|
|
121
|
+
const assignmentMatrix = new SparseMatrix(hamSize, hamSize);
|
|
122
|
+
const multLen = cluster.length;
|
|
123
|
+
weight = 0;
|
|
124
|
+
for (let n = 0; n < multLen; n++) {
|
|
125
|
+
const L = getPauliMatrix(multiplicity[clusterFake[n]]);
|
|
126
|
+
|
|
127
|
+
let temp = 1;
|
|
128
|
+
for (let j = 0; j < n; j++) {
|
|
129
|
+
temp *= multiplicity[clusterFake[j]];
|
|
130
|
+
}
|
|
131
|
+
const A = SparseMatrix.eye(temp);
|
|
132
|
+
|
|
133
|
+
temp = 1;
|
|
134
|
+
for (let j = n + 1; j < multLen; j++) {
|
|
135
|
+
temp *= multiplicity[clusterFake[j]];
|
|
136
|
+
}
|
|
137
|
+
const B = SparseMatrix.eye(temp);
|
|
138
|
+
const tempMat = A.kroneckerProduct(L.m).kroneckerProduct(B);
|
|
139
|
+
if (cluster[n] >= 0) {
|
|
140
|
+
assignmentMatrix.add(tempMat.mul(cluster[n] + 1));
|
|
141
|
+
weight++;
|
|
142
|
+
} else {
|
|
143
|
+
assignmentMatrix.add(tempMat.mul(cluster[n]));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let rhoip = Matrix.zeros(hamSize, hamSize);
|
|
148
|
+
assignmentMatrix.forEachNonZero((i, j, v) => {
|
|
149
|
+
if (v > 0) {
|
|
150
|
+
for (let k = 0; k < V.columns; k++) {
|
|
151
|
+
let element = V.get(j, k);
|
|
152
|
+
if (element !== 0) {
|
|
153
|
+
rhoip.set(i, k, rhoip.get(i, k) + element);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return v;
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
let rhoip2 = rhoip.clone();
|
|
161
|
+
assignmentMatrix.forEachNonZero((i: number, j: number, v: number) => {
|
|
162
|
+
if (v < 0) {
|
|
163
|
+
for (let k = 0; k < V.columns; k++) {
|
|
164
|
+
let element = V.get(j, k);
|
|
165
|
+
if (element !== 0) {
|
|
166
|
+
rhoip2.set(i, k, rhoip2.get(i, k) + element);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return v;
|
|
171
|
+
});
|
|
172
|
+
const tV = V.transpose();
|
|
173
|
+
|
|
174
|
+
rhoip = tV.mmul(rhoip);
|
|
175
|
+
const sparseRhoip = new SparseMatrix(rhoip.to2DArray(), {
|
|
176
|
+
threshold: smallValue,
|
|
177
|
+
});
|
|
178
|
+
triuTimesAbs(sparseRhoip, smallValue);
|
|
179
|
+
|
|
180
|
+
rhoip2 = tV.mmul(rhoip2);
|
|
181
|
+
const sparseRhoip2 = new SparseMatrix(rhoip2.to2DArray(), {
|
|
182
|
+
threshold: smallValue,
|
|
183
|
+
});
|
|
184
|
+
sparseRhoip2.forEachNonZero((i, j, v) => {
|
|
185
|
+
return v;
|
|
186
|
+
});
|
|
187
|
+
triuTimesAbs(sparseRhoip2, smallValue);
|
|
188
|
+
sparseRhoip2.forEachNonZero((i, j, v) => {
|
|
189
|
+
let val = rhoip.get(i, j);
|
|
190
|
+
val = Math.min(Math.abs(val), Math.abs(v));
|
|
191
|
+
val *= val;
|
|
192
|
+
|
|
193
|
+
sumI += val;
|
|
194
|
+
let valFreq = diagB[i] - diagB[j];
|
|
195
|
+
let insertIn = binarySearch(
|
|
196
|
+
frequencies,
|
|
197
|
+
valFreq,
|
|
198
|
+
(a: number, b: number) => a - b,
|
|
199
|
+
);
|
|
200
|
+
if (insertIn < 0) {
|
|
201
|
+
frequencies.splice(-1 - insertIn, 0, valFreq);
|
|
202
|
+
intensities.splice(-1 - insertIn, 0, val);
|
|
203
|
+
} else {
|
|
204
|
+
intensities[insertIn] += val;
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const numFreq = frequencies.length;
|
|
210
|
+
|
|
211
|
+
if (numFreq > 0) {
|
|
212
|
+
weight /= sumI;
|
|
213
|
+
const diff = lineWidth / 64;
|
|
214
|
+
let valFreq = frequencies[0];
|
|
215
|
+
let inte = intensities[0];
|
|
216
|
+
let count = 1;
|
|
217
|
+
for (let i = 1; i < numFreq; i++) {
|
|
218
|
+
if (Math.abs(frequencies[i] - valFreq / count) < diff) {
|
|
219
|
+
inte += intensities[i];
|
|
220
|
+
valFreq += frequencies[i];
|
|
221
|
+
count++;
|
|
222
|
+
} else {
|
|
223
|
+
xyPeaks.push({
|
|
224
|
+
x: -valFreq / count / frequencyMHz,
|
|
225
|
+
y: inte * weight,
|
|
226
|
+
});
|
|
227
|
+
valFreq = frequencies[i];
|
|
228
|
+
inte = intensities[i];
|
|
229
|
+
count = 1;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
xyPeaks.push({
|
|
234
|
+
x: -valFreq / count / frequencyMHz,
|
|
235
|
+
y: inte * weight,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return xyPeaks;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function triuTimesAbs(A: SparseMatrix, val: number) {
|
|
244
|
+
A.forEachNonZero((i, j, v) => {
|
|
245
|
+
if (i > j) return 0;
|
|
246
|
+
if (Math.abs(v) <= val) return 0;
|
|
247
|
+
return v;
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Create a hamiltonian matrix for the given spinsystem
|
|
252
|
+
* @param {Array} chemicalShifts - An array containing the chemical shift in Hz
|
|
253
|
+
* @param {Array} couplingConstants - An array containing the coupling constants in Hz
|
|
254
|
+
* @param {Array} multiplicity - An array specifiying the multiplicities of each scalar coupling
|
|
255
|
+
* @param {Array} conMatrix - A one step connectivity matrix for the given spin system
|
|
256
|
+
* @param {Array} cluster - An binary array specifiying the spins to be considered for this hamiltonial
|
|
257
|
+
* @return {object}
|
|
258
|
+
*/
|
|
259
|
+
function getHamiltonian(
|
|
260
|
+
chemicalShifts: number[],
|
|
261
|
+
couplingConstants: MatrixClassType,
|
|
262
|
+
multiplicity: number[],
|
|
263
|
+
conMatrix: MatrixClassType,
|
|
264
|
+
cluster: number[],
|
|
265
|
+
) {
|
|
266
|
+
let hamSize = 1;
|
|
267
|
+
for (const element of cluster) {
|
|
268
|
+
hamSize *= multiplicity[element];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const clusterHam = new SparseMatrix(hamSize, hamSize);
|
|
272
|
+
|
|
273
|
+
for (let pos = 0; pos < cluster.length; pos++) {
|
|
274
|
+
let n = cluster[pos];
|
|
275
|
+
|
|
276
|
+
const L = getPauliMatrix(multiplicity[n]);
|
|
277
|
+
|
|
278
|
+
let A1, B1;
|
|
279
|
+
let temp = 1;
|
|
280
|
+
for (let i = 0; i < pos; i++) {
|
|
281
|
+
temp *= multiplicity[cluster[i]];
|
|
282
|
+
}
|
|
283
|
+
A1 = SparseMatrix.eye(temp);
|
|
284
|
+
|
|
285
|
+
temp = 1;
|
|
286
|
+
for (let i = pos + 1; i < cluster.length; i++) {
|
|
287
|
+
temp *= multiplicity[cluster[i]];
|
|
288
|
+
}
|
|
289
|
+
B1 = SparseMatrix.eye(temp);
|
|
290
|
+
|
|
291
|
+
const alpha = chemicalShifts[n];
|
|
292
|
+
const kronProd = A1.kroneckerProduct(L.z).kroneckerProduct(B1);
|
|
293
|
+
clusterHam.add(kronProd.mul(alpha));
|
|
294
|
+
for (let pos2 = 0; pos2 < cluster.length; pos2++) {
|
|
295
|
+
const k = cluster[pos2];
|
|
296
|
+
if (conMatrix.get(n, k) === 1) {
|
|
297
|
+
const S = getPauliMatrix(multiplicity[k]);
|
|
298
|
+
|
|
299
|
+
let A2, B2;
|
|
300
|
+
let temp = 1;
|
|
301
|
+
for (let i = 0; i < pos2; i++) {
|
|
302
|
+
temp *= multiplicity[cluster[i]];
|
|
303
|
+
}
|
|
304
|
+
A2 = SparseMatrix.eye(temp);
|
|
305
|
+
|
|
306
|
+
temp = 1;
|
|
307
|
+
for (let i = pos2 + 1; i < cluster.length; i++) {
|
|
308
|
+
temp *= multiplicity[cluster[i]];
|
|
309
|
+
}
|
|
310
|
+
B2 = SparseMatrix.eye(temp);
|
|
311
|
+
|
|
312
|
+
const kron1 = A1.kroneckerProduct(L.x)
|
|
313
|
+
.kroneckerProduct(B1)
|
|
314
|
+
.mmul(A2.kroneckerProduct(S.x).kroneckerProduct(B2));
|
|
315
|
+
kron1.add(
|
|
316
|
+
A1.kroneckerProduct(L.y)
|
|
317
|
+
.kroneckerProduct(B1)
|
|
318
|
+
.mul(-1)
|
|
319
|
+
.mmul(A2.kroneckerProduct(S.y).kroneckerProduct(B2)),
|
|
320
|
+
);
|
|
321
|
+
kron1.add(
|
|
322
|
+
A1.kroneckerProduct(L.z)
|
|
323
|
+
.kroneckerProduct(B1)
|
|
324
|
+
.mmul(A2.kroneckerProduct(S.z).kroneckerProduct(B2)),
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
clusterHam.add(kron1.mul(couplingConstants.get(n, k) / 2));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return clusterHam;
|
|
332
|
+
}
|
package/src/xyz/NMRSignal2D.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { FromTo } from 'cheminfo-types';
|
|
2
2
|
import type { Peak2D } from 'ml-matrix-peaks-finder';
|
|
3
3
|
|
|
4
|
-
export interface
|
|
4
|
+
export interface Signal2DProjection {
|
|
5
5
|
nucleus?: string;
|
|
6
6
|
delta: number;
|
|
7
7
|
resolution?: number;
|
|
@@ -9,8 +9,8 @@ export interface Signal2DAxisData {
|
|
|
9
9
|
diaIDs?: string[];
|
|
10
10
|
}
|
|
11
11
|
export interface NMRSignal2D {
|
|
12
|
-
x:
|
|
13
|
-
y:
|
|
12
|
+
x: Signal2DProjection;
|
|
13
|
+
y: Signal2DProjection;
|
|
14
14
|
j?: {
|
|
15
15
|
pathLength?: number | FromTo;
|
|
16
16
|
};
|