nmr-simulation
Advanced tools
Comparing version 1.0.14 to 1.0.15
@@ -27,2 +27,3 @@ 'use strict'; | ||
var lineWidthY = options.lineWidthY || defOptions[toLabel].lineWidth; | ||
let symmetrize = options.symmetrize || false; | ||
@@ -74,2 +75,5 @@ var sigmaX = lineWidthX / frequencyX; | ||
addPeak(spectraMatrix, peak); | ||
if (symmetrize) { | ||
addPeak(spectraMatrix, { x: peak.y, y: peak.x, z: peak.z, widthX: peak.widthY, widthY: peak.widthX }); | ||
} | ||
i++; | ||
@@ -81,3 +85,3 @@ } | ||
function unitsToArrayPoints(x, from, to, nbPoints) { | ||
return ((x - from) * nbPoints - 1) / (to - from); | ||
return (x - from) * (nbPoints - 1) / (to - from); | ||
} | ||
@@ -84,0 +88,0 @@ |
{ | ||
"name": "nmr-simulation", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "Simulate NMR spectra from spin systems", | ||
@@ -36,8 +36,8 @@ "main": "lib/index.js", | ||
"new-array": "^1.0.0", | ||
"num-sort": "^1.0.0", | ||
"should": "^13.2.3" | ||
"num-sort": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"nmr-predictor": "^1.1.7" | ||
"nmr-predictor": "^1.1.9", | ||
"should": "^13.2.3" | ||
} | ||
} |
189
README.md
@@ -11,4 +11,25 @@ # nmr-simulation | ||
This library is a JavaScript implementation of such algorithm. The spin system is specified as a set of magnetically equivalent spins. The description of the input format is ilustrated by means of the bellow example. There we have a spin system composed of 4 spins(11, 12, 5 and 6). Spins 11 and 12 are chemically equivalent, so they must be specified on the same group. nbAtoms stands for the number of atoms in the group. It should math the atomIDs.length. AtomLabel stands for the kind of atom of the group. It could be H, C, N, P refering to nuclei 1H, 13C, 15N and 31P respectively. Coupling between atoms should be specified in the j array. | ||
This library is a JavaScript implementation of such algorithm. The spin system is specified as a set of magnetically equivalent spins. The description of the input format is ilustrated by means of the bellow example. There we have a spin system composed of 4 spins(11, 12, 5 and 6). Spins 11 and 12 are chemically equivalent, so they must be specified on the same group. nbAtoms stands for the number of atoms in the group. It should math the atomIDs.length. AtomLabel stands for the kind of atom of the group. It could be H, C, N, P refering to nuclei 1H, 13C, 15N and 31P respectively. | ||
Coupling between atoms could be specified in the j array. Each element represent a coupling from the current group of atoms to the atoms specified by the 'assignment' property. The 'coupling' property stands for the scalar coupling constant in Hertz. The 'multiplicity' property stands for the number of equivalent atoms refered by the 'assignment' property. Actually only 'd' is supported, meaning that only a coupling to a single atom could be specified by each entry in the j array. The 'distance' parameter stands for the number of bonds that separate the atoms involved in the entanglement. | ||
For a better explanation, we show in the example, a very simple spin system of 3 hydrogen atoms AXX: '11', '5' and '6'. There, the atoms '5' and '6' are chemically equivalent, thus, we have a only 2 entries in the array: The first one for the atom with atomIDs: ['11'] and the second one for the atoms with atomIDs: ['5', '6']. The atom '11' resonates at 1.2ppm and the atoms '5' and '6' that resonates at 1.3ppm. The atom '11' has a coupling constant with the other atoms of 7.4 Hz, and the distance between then is of 3 bonds. | ||
The simulator will produce a spectrum at 400.082470657773 Mhz, from 1 to 1.5 ppm, and 3Hz of linewidth. The spectrum will contain 512 points. | ||
## Parameters | ||
*spinSystem*: A JSON object describing a spinSystem ready for simulation. As this object is to complex to be defined manually, we created a helper that formats the systemSystem from a JSON definition. | ||
## Options | ||
| Option | Description | | ||
| --- | --- | | ||
| **frequency** | The frequency in Mhz of the fake spectrometer that records the spectrum. 400 by default| | ||
|**from** | The low limit of the ordinate variable. 0 by default| | ||
|**to** | The high limit of the ordinate variable. 10 by default| | ||
|**lineWidth** | The linewidth of the output spectrum, expresed in Hz. 1Hz by default| | ||
|**nbPoints** | Number of points of the output spectrum. 1024 by default| | ||
|**maxClusterSize** | 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. 10 by default| | ||
|**output** | ['y' or 'xy'] it specify the output format. if 'y' is specified, the output of the simulation will be a single vector containing the y data of the spectrum. if 'xy' is specified, the output of the simulation will be an object containing {x,[], y:[]}, the x, y of the spectrum. 'y' by default| | ||
## Installation | ||
@@ -20,3 +41,3 @@ | ||
## Example | ||
## Example 1 | ||
@@ -29,10 +50,12 @@ ```js | ||
{ | ||
atomIDs: ['11', '12'], | ||
nbAtoms: 2, | ||
delta: 1, | ||
atomLabel: 'H' | ||
atomIDs: ['11'], | ||
nbAtoms: 1, | ||
delta: 1.2, | ||
atomLabel: 'H', | ||
j:[{'assignment':['5'],'coupling':7.4,'multiplicity':'d','distance':3}, | ||
{'assignment':['6'],'coupling':7.4,'multiplicity':'d','distance':3}] | ||
}, { | ||
atomIDs: ['5', '6'], | ||
nbAtoms: 2, | ||
delta: 1, | ||
delta: 1.3, | ||
atomLabel: 'H' | ||
@@ -44,6 +67,6 @@ } | ||
frequency: 400.082470657773, | ||
from: 0, | ||
to: 3, | ||
from: 1, | ||
to: 1.5, | ||
lineWidth: 3, | ||
nbPoints: 16384, | ||
nbPoints: 512, | ||
maxClusterSize: 8, | ||
@@ -53,10 +76,148 @@ output: 'xy' | ||
const spinSystem = nmr.SpinSystem.fromPrediction(prediction1h); | ||
const spinSystem = simulation.SpinSystem.fromPrediction(prediction); | ||
spinSystem.ensureClusterSize(options1h); | ||
var spectrum = nmr.simulate1D(spinSystem, options1h); | ||
console.log(spectrum.x);// x in PPM | ||
console.log(spectrum.y);// y in arbitrary units | ||
var spectrum = simulation.simulate1D(spinSystem, options1h); | ||
console.log(JSON.stringify(spectrum.x));// x in PPM | ||
console.log(JSON.stringify(spectrum.y));// y in arbitrary units | ||
``` | ||
A plot of the output spectrum | ||
<img width="300" alt="output" src="https://github.com/cheminfo-js/spectra/blob/master/packages/nmr-simulation/examples/spectrum.png"> | ||
## Example 2: Simulating a 2D spectrum. | ||
This example ilustrates the simulation of a sytste (B, AA, C ). Atoms of the group AA, resonating at 2ppm, are coupled with the atom B that resonates at 1ppm. The scalar coupling between them | ||
is 7 Hz. Atoms of the group AA are coupled with atom C, that resonates at 3ppm. The scalar coupling between then is 16Hz. There is not direct coupling between atom B and C. | ||
This should produce a pattern like the below in the code: | ||
```js | ||
/* | ||
x: 0.5 1.0 1.5 2.0 2.5 3.0 3.5 | ||
____________________________________ | ||
0.5 | 0 0 0 0 0 0 0 | ||
1.0 | 0 1 0 1 0 0 0 | ||
1.5 | 0 0 0 0 0 0 0 | ||
2.0 | 0 1 0 1 0 1 0 | ||
2.5 | 0 0 0 0 0 0 0 | ||
3.0 | 0 0 0 1 0 1 0 | ||
3.5 | 0 0 0 0 0 0 0 | ||
*/ | ||
const simulation = require('nmr-simulation'); | ||
const prediction = [{ | ||
fromDiaID: 'A', toDiaID: 'A', fromAtoms: [8, 14], toAtoms: [8, 14], | ||
fromLabel: 'H', toLabel: 'H', pathLength: 0, | ||
fromChemicalShift: 2, toChemicalShift: 2, | ||
fromAtomLabel: 'H', toAtomLabel: 'H' | ||
}, | ||
{ | ||
fromDiaID: 'B', toDiaID: 'B', fromAtoms: [9], toAtoms: [9], | ||
fromLabel: 'H', toLabel: 'H', pathLength: 0, | ||
fromChemicalShift: 1, toChemicalShift: 1, | ||
fromAtomLabel: 'H', toAtomLabel: 'H' | ||
}, | ||
{ | ||
fromDiaID: 'C', toDiaID: 'C', fromAtoms: [10], toAtoms: [10], | ||
fromLabel: 'H', toLabel: 'H', pathLength: 0, | ||
fromChemicalShift: 3, toChemicalShift: 3, | ||
fromAtomLabel: 'H', toAtomLabel: 'H' | ||
}, | ||
{ | ||
fromDiaID: 'A', toDiaID: 'B', fromAtoms: [8, 14], toAtoms: [9], | ||
fromLabel: 'H', toLabel: 'H', pathLength: 3, | ||
fromChemicalShift: 2, toChemicalShift: 1, | ||
fromAtomLabel: 'H', toAtomLabel: 'H', j: 7 | ||
}, | ||
{ | ||
fromDiaID: 'A', toDiaID: 'C', fromAtoms: [8, 14], toAtoms: [10], | ||
fromLabel: 'H', toLabel: 'H', pathLength: 3, | ||
fromChemicalShift: 2, toChemicalShift: 3, | ||
fromAtomLabel: 'H', toAtomLabel: 'H', j: 16 | ||
}]; | ||
var optionsCOSY = { | ||
frequencyX: 1, frequencyY: 1, | ||
lineWidthX: 0.07, lineWidthY: 0.07, //Hz | ||
firstX: 0.5, lastX: 3.5, | ||
firstY: 0.5, lastY: 3.5, | ||
nbPointsX: 7, nbPointsY: 7, | ||
symmetrize: true | ||
}; | ||
var spectrum = simulation.simulate2D(prediction, optionsCOSY); | ||
//Lets make a logical matrix. Smaller values than 1e-1 are 0, greather that that are 1 | ||
spectrum = spectrum.map(row => { | ||
return row.map(value => value < 1e-1 ? 0 : 1); | ||
}) | ||
console.log(spectrum); | ||
``` | ||
## Example 3: Simulating 1H and COSY spectra for Ethylbenzene. | ||
Chemical shift and coupling constants are predicted using Spinus, through our prediction library nmr-predictor | ||
```js | ||
'use strict'; | ||
const sm = require('nmr-simulation'); | ||
const predictor = require("nmr-predictor"); | ||
var molfile = `Benzene, ethyl-, ID: C100414 | ||
NIST 16081116462D 1 1.00000 0.00000 | ||
Copyright by the U.S. Sec. Commerce on behalf of U.S.A. All rights reserved. | ||
8 8 0 0 0 1 V2000 | ||
0.5015 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 | ||
0.0000 0.8526 0.0000 C 0 0 0 0 0 0 0 0 0 | ||
1.5046 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 | ||
2.0062 0.8526 0.0000 C 0 0 0 0 0 0 0 0 0 | ||
3.0092 0.8526 0.0000 C 0 0 0 0 0 0 0 0 0 | ||
1.5046 1.7554 0.0000 C 0 0 0 0 0 0 0 0 0 | ||
0.5015 1.7052 0.0000 C 0 0 0 0 0 0 0 0 0 | ||
3.5108 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 | ||
1 2 2 0 0 0 | ||
3 1 1 0 0 0 | ||
2 7 1 0 0 0 | ||
4 3 2 0 0 0 | ||
4 5 1 0 0 0 | ||
6 4 1 0 0 0 | ||
5 8 1 0 0 0 | ||
7 6 2 0 0 0 | ||
M END | ||
`; | ||
//Predict and simulate 1H and COSY for Ethylbenzene | ||
predictor.spinus(molfile, { group: false }).then(prediction => { | ||
const spinSystem = sm.SpinSystem.fromPrediction(prediction); | ||
var options = { | ||
frequency: 400.082470657773, | ||
from: 0, | ||
to: 11, | ||
lineWidth: 1, | ||
nbPoints: 16384, | ||
maxClusterSize: 8 | ||
} | ||
spinSystem.ensureClusterSize(options); | ||
//Simulate 1H spectrum | ||
var simulation1D = sm.simulate1D(spinSystem, options); | ||
//Predict 2D components. It will allow to observe interaction between atoms from 0 to 3 bond of distance | ||
const prediction2D = predictor.twoD(prediction, prediction, molfile, { minLength: 0, maxLength: 3 }); | ||
var optionsCOSY = { | ||
frequencyX: 400.08, frequencyY: 400.08, | ||
lineWidthX: 2, lineWidthY: 2, //Hz | ||
firstX: 0, lastX: 11, | ||
firstY: 0, lastY: 11, | ||
nbPointsX: 512, nbPointsY: 512, | ||
symmetrize: true | ||
}; | ||
//Simulate COSY spectrum | ||
var spectrum2D = sm.simulate2D(prediction2D, optionsCOSY); | ||
}, reject => { console.log(reject) }); | ||
``` | ||
## License | ||
@@ -63,0 +224,0 @@ |
@@ -16,2 +16,3 @@ import Matrix from 'ml-matrix'; | ||
var lineWidthY = options.lineWidthY || defOptions[toLabel].lineWidth; | ||
let symmetrize = options.symmetrize || false; | ||
@@ -63,2 +64,5 @@ var sigmaX = lineWidthX / frequencyX; | ||
addPeak(spectraMatrix, peak); | ||
if (symmetrize) { | ||
addPeak(spectraMatrix, { x: peak.y, y: peak.x, z: peak.z, widthX: peak.widthY, widthY: peak.widthX }); | ||
} | ||
i++; | ||
@@ -70,3 +74,3 @@ } | ||
function unitsToArrayPoints(x, from, to, nbPoints) { | ||
return ((x - from) * nbPoints - 1) / (to - from); | ||
return ((x - from) * (nbPoints - 1)) / (to - from); | ||
} | ||
@@ -73,0 +77,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59245
7
1316
231
2
- Removedshould@^13.2.3
- Removedshould@13.2.3(transitive)
- Removedshould-equal@2.0.0(transitive)
- Removedshould-format@3.0.3(transitive)
- Removedshould-type@1.4.0(transitive)
- Removedshould-type-adaptors@1.1.0(transitive)
- Removedshould-util@1.0.1(transitive)