Skip to content

Commit 4d1700b

Browse files
committed
Optimize: Tested for NMR spectra
1 parent e4e1d89 commit 4d1700b

File tree

10 files changed

+4922
-4778
lines changed

10 files changed

+4922
-4778
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
# global-spectral-deconvolution
2+
3+
Global Spectra Deconvolution + Peak optimizer
4+
5+
## Example
6+
``
7+
var CC = require('chemcalc');
8+
var Stat = require('ml-stat');
9+
var peakPicking = require("../src/index");
10+
11+
12+
var spectrum=CC.analyseMF("Cl2.Br2", {isotopomers:'arrayXXYY', fwhm:0.01, gaussianWidth: 11});
13+
var xy=spectrum.arrayXXYY;
14+
var x=xy[0];
15+
var y=xy[1];
16+
//Just a fake noiseLevel
17+
var noiseLevel=Stat.array.median(y.filter(function(a) {return (a>0)}))*3;
18+
19+
var result=peakPicking.gsd(x, y, {noiseLevel: noiseLevel, minMaxRatio:0, broadRatio:0,smoothY:false});
20+
result = peakPicking.optimize(result,x,y,1,"gaussian");`
21+
```

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ml-gsd",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Global Spectra Deconvolution",
55
"keywords": [
66
"optimization",

dist/ml-gsd.js

Lines changed: 4864 additions & 4748 deletions
Large diffs are not rendered by default.

dist/ml-gsd.min.js

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ml-gsd.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
{
22
"name": "ml-gsd",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Global Spectra Deconvolution",
55
"main": "src/index.js",
66
"directories": {
77
"lib": "src",
88
"test": "test"
99
},
1010
"scripts": {
11-
"build": "gulp build",
11+
"build": "cheminfo build",
1212
"test": "mocha --require should --reporter mocha-better-spec-reporter --recursive"
1313
},
1414
"repository": {
1515
"type": "git",
1616
"url": "https://github.com/mljs/global-spectra-deconvolution.git"
1717
},
1818
"keywords": [
19-
"Global Spectra Deconvolution"
19+
"Global Spectra Deconvolution",
20+
"peak",
21+
"picking",
22+
"optimization",
23+
"gsd"
2024
],
2125
"author": "Andres Castillo",
2226
"license": "MIT",
@@ -39,10 +43,11 @@
3943
"should": "^7.0.2",
4044
"xy-parser": "^1.0.0",
4145
"chemcalc": "^3.0.1",
42-
"ml-stat": "^1.0.1"
46+
"ml-stat": "^1.0.1",
47+
"cheminfo-tools": "^1.0.2"
4348
},
4449
"dependencies": {
45-
"ml-optimize-lorentzian": "../optimize-lorentzian",
50+
"ml-optimize-lorentzian": "0.0.2",
4651
"ml-stat": "^1.0.1",
4752
"xy-parser": "^1.1.0"
4853
}

src/gsd.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
var optimizePeaks = require("../src/optimize");
1+
var Opt = require("ml-optimize-lorentzian");
32

43
function gsd(x, y, options){
54
var options=Object.create(options || {});
@@ -174,9 +173,11 @@ function gsd(x, y, options){
174173
}
175174
}
176175
else{
177-
var fitted = optimizePeaks.optimizeSingleLorentzian(candidates);
176+
var fitted = Opt.optimizeSingleLorentzian(candidates,{x:candidates[maxI][0],
177+
width:Math.abs(candidates[0][0]-candidates[candidates.length-1][0])},
178+
[]);
178179
//console.log(fitted);
179-
signals.push(fitted);
180+
signals.push([fitted[0][0],fitted[0][1],fitted[0][2]]);
180181
}
181182
candidates = [];
182183
max = 0;

src/optimize.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,30 @@
44
var Opt = require("ml-optimize-lorentzian");
55

66
function sampleFunction(from, to, x, y, lastIndex){
7-
//console.log(from+" "+to);
8-
//console.log(lastIndex+" "+x[lastIndex[0]]);
97
var nbPoints = x.length;
108
var sampleX = [];
119
var sampleY = [];
1210
var direction = Math.sign(x[1]-x[0]);//Direction of the derivative
13-
var delta = (to-from)/2;
11+
if(direction==-1){
12+
lastIndex[0]= x.length-1;
13+
}
14+
15+
var delta = Math.abs(to-from)/2;
1416
var mid = (from+to)/2;
1517
var stop = false;
1618
var index = lastIndex[0];
17-
while(!stop&&index<nbPoints){
19+
while(!stop&&index<nbPoints&&index>=0){
1820
if(Math.abs(x[index]-mid)<=delta){
1921
sampleX.push(x[index]);
2022
sampleY.push(y[index]);
21-
index++;
22-
23+
index+=direction;
2324
}
2425
//It is outside the range.
2526
else{
2627

27-
if(Math.sign(mid-x[index])==direction){
28+
if(Math.sign(mid-x[index])==1){
2829
//We'll reach the mid going in the current direction
29-
index++;
30+
index+=direction;
3031
}
3132
else{
3233
//There is not more peaks in the current range
@@ -46,13 +47,12 @@ function optimizePeaks(peakList,x,y,n, fnType){
4647
var factor = 1;
4748
if(fnType=="gaussian")
4849
factor = 1.17741;//From https://en.wikipedia.org/wiki/Gaussian_function#Properties
49-
//console.log(x[0]+" "+x[1]);
5050
for(i=0;i<groups.length;i++){
51-
//console.log(peakList[i]);
5251
var peaks = groups[i].group;
5352
if(peaks.length>1){
5453
//Multiple peaks
5554
//console.log("Pending group of overlaped peaks "+peaks.length);
55+
//console.log("here1");
5656
//console.log(groups[i].limits);
5757
var sampling = sampleFunction(groups[i].limits[0]-groups[i].limits[1],groups[i].limits[0]+groups[i].limits[1],x,y,lastIndex);
5858
//console.log(sampling);
@@ -68,8 +68,9 @@ function optimizePeaks(peakList,x,y,n, fnType){
6868
optPeaks = Opt.optimizeLorentzianSum(sampling, peaks, opts);
6969
}
7070
}
71+
//console.log(optPeak);
7172
for(j=0;j<optPeaks.length;j++){
72-
result.push({x:optPeaks[j][0],y:optPeaks[j][1],width:optPeaks[j][2]*factor});
73+
result.push({x:optPeaks[j][0][0],y:optPeaks[j][1][0],width:optPeaks[j][2][0]*factor});
7374
}
7475
}
7576
}
@@ -78,7 +79,8 @@ function optimizePeaks(peakList,x,y,n, fnType){
7879
peaks = peaks[0];
7980
var sampling = sampleFunction(peaks.x-n*peaks.width,
8081
peaks.x+n*peaks.width,x,y,lastIndex);
81-
//console.log(sampling);
82+
//console.log("here2");
83+
//console.log(groups[i].limits);
8284
if(sampling[0].length>5){
8385
var error = peaks.width/1000;
8486
var opts = [ 3, 100, error, error, error, error*10, error*10, 11, 9, 1 ];
@@ -92,7 +94,8 @@ function optimizePeaks(peakList,x,y,n, fnType){
9294
var optPeak = Opt.optimizeSingleLorentzian([sampling[0],sampling[1]], peaks, opts);
9395
}
9496
}
95-
result.push({x:optPeak[0],y:optPeak[1],width:optPeak[2]*factor}); // From https://en.wikipedia.org/wiki/Gaussian_function#Properties}
97+
//console.log(optPeak);
98+
result.push({x:optPeak[0][0],y:optPeak[1][0],width:optPeak[2][0]*factor}); // From https://en.wikipedia.org/wiki/Gaussian_function#Properties}
9699
}
97100
}
98101

test/massPeakPicking.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ console.log(result);
2222
describe.only('Check the peak picking of a simulated mass spectrum', function () {
2323

2424
it('Check result', function () {
25-
25+
2626
});
2727
});
2828

test/ubiquitin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ describe('Global spectra deconvolution2', function () {
3232
console.log("Parsing + gsd + optimization time: "+(d.getTime()-n));
3333

3434

35-
for(var i=0;i<result.length;i++){
35+
/* for(var i=0;i<result.length;i++){
3636
//if(result[i].width<0.05)
3737
console.log(result[i].x+" "+result[i].y+" "+result[i].width);
3838
}
39-
console.log(result.length);
39+
console.log(result.length);*/
4040

4141
// Test case obtained from Pag 443, Chap 8.
4242
it('Should provide the right result ...', function () {

0 commit comments

Comments
 (0)