Skip to content

Commit

Permalink
feat: add toJSON and fromJSON methods
Browse files Browse the repository at this point in the history
  • Loading branch information
maasencioh committed Nov 4, 2016
1 parent 1c6089a commit 8848b4c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 5 deletions.
32 changes: 32 additions & 0 deletions src/Chromatogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,38 @@ class Chromatogram {
rescaleTime(conversionFunction) {
this.times = rescaleTime(this.times, conversionFunction);
}

/**
* Parse the content to an JSON Array
* @return {Array<object>} - Returns a list with the following fields:
* * `time`: Number for the retention time
* * `tic`: Number for the total ion chromatogram
* * `mass`: List of mass values and their respective intensities
*/
toJSON() {
var ans = new Array(this.times.length);
const tic = this.findSerieByName('tic').data;
const mass = this.findSerieByName('ms').data.map((ms) => {
var ansMS = new Array(ms[0].length);
for (var i = 0; i < ansMS.length; i++) {
ansMS[i] = {
mass: ms[0][i],
intensity: ms[1][i]
};
}
return ansMS;
});

for (var i = 0; i < ans.length; i++) {
ans[i] = {
time: this.times[i],
tic: tic[i],
mass: mass[i]
};
}

return ans;
}
}

module.exports = Chromatogram;
40 changes: 40 additions & 0 deletions src/fromJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const Chromatogram = require('./Chromatogram');

/**
* Parse from a JSON element to a new Chromatogram
* @param {Array<object>} json - Result from the toJSON
* @return {Chromatogram} - New parsed Chromatogram
*/
function fromJSON(json) {
let time = new Array(json.length);
let tic = new Array(json.length);
let ms = new Array(json.length);

for (var i = 0; i < json.length; i++) {
time[i] = json[i].time;
tic[i] = json[i].tic;
ms[i] = [new Array(json[i].mass.length), new Array(json[i].mass.length)];
for (var j = 0; j < json[i].mass.length; j++) {
ms[i][0][j] = json[i].mass[j].mass;
ms[i][1][j] = json[i].mass[j].intensity;
}
}

let chrom = new Chromatogram(time);
chrom.addSerie({
dimension: 1,
name: 'tic',
data: tic
});
chrom.addSerie({
dimension: 2,
name: 'ms',
data: ms
});

return chrom;
}

module.exports = fromJSON;
2 changes: 1 addition & 1 deletion src/fromJcamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ function fromJcamp(jcamp) {
return new Chromatogram(data);
}

module.exports = fromJcamp;
module.exports = fromJcamp;
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ exports.getKovatsTable = require('./getKovatsTable');
exports.kovatsConversionFunction = require('./kovatsConversionFunction');
exports.rescaleTime = require('./rescaleTime');
exports.fromJcamp = require('./fromJcamp');
exports.fromJSON = require('./fromJSON');
4 changes: 2 additions & 2 deletions src/spectraComparison.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const peakPicking = require('./getPeaks');
const getPeaks = require('./getPeaks');
const massInPeaks = require('./massInPeaks');
const vectorify = require('./vectorify');
const cosine = require('./cosine');
Expand All @@ -14,7 +14,7 @@ const cosine = require('./cosine');
*/
function preprocessing(chromatography, options) {
// peak picking
let peaks = peakPicking(chromatography, options);
let peaks = getPeaks(chromatography, options);
peaks = peaks.sort((a, b) => a.index - b.index);

// integrate mass in the peaks
Expand Down
40 changes: 40 additions & 0 deletions test/JSONparser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import test from 'ava';
import {Chromatogram, fromJSON} from '..';

// https://en.wikipedia.org/wiki/Cauchy_distribution
function lorentzian(x, x0 = 0, gamma = 1) {
return (gamma * gamma) / (Math.PI * gamma * (gamma * gamma + (x - x0) * (x - x0)));
}

test('toJSON - fromJSON', t => {
const size = 30;
const fourth = size / 4;
let times = new Array(size);
let tic = new Array(size);
let ms = new Array(size);
for (let i = 0; i < size; ++i) {
times[i] = i;
tic[i] = lorentzian(i, fourth) + 2 * lorentzian(i, 2 * fourth) + lorentzian(i, 3 * fourth);
ms[i] = [[1, 2, 3], [1, 1, 1]];
}
let chrom = new Chromatogram(times);
chrom.addSerie({
dimension: 1,
name: 'tic',
data: tic
});
chrom.addSerie({
dimension: 2,
name: 'ms',
data: ms
});

let json = chrom.toJSON();
t.is(json.length, chrom.getTimes().length);
t.is(json.length, 30);

let newChrom = fromJSON(json);
t.deepEqual(newChrom.getTimes(), chrom.getTimes());
t.deepEqual(newChrom.findSerieByName('tic'), chrom.findSerieByName('tic'));
t.deepEqual(newChrom.findSerieByName('ms'), chrom.findSerieByName('ms'));
});
3 changes: 1 addition & 2 deletions test/loadJcamp.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import test from 'ava';
import {convert} from 'jcampconverter';
import fs from 'fs';
import Promise from 'bluebird';
import {join} from 'path';
import {Chromatogram, fromJcamp} from '..';
import {fromJcamp} from '..';

const readFileAsync = Promise.promisify(fs.readFile);

Expand Down

0 comments on commit 8848b4c

Please sign in to comment.