-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
58,588 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const { readFileSync } = require('fs'); | ||
const join = require('path').join; | ||
|
||
const mzML = require('..'); | ||
|
||
const pathFiles = join(__dirname, '/../../testFiles/mzML/'); | ||
|
||
describe('mzML', () => { | ||
it('read tiny.mzML', () => { | ||
const data = readFileSync(join(pathFiles, 'tiny.mzML')); | ||
let response = mzML(data); | ||
expect(response.times).toStrictEqual([5.8905, 5.9905, 42.05]); | ||
expect(response.series.ms.data).toHaveLength(3); | ||
expect(response.series.ms.data[0][0]).toHaveLength(15); | ||
expect(response.series.ms.data[1][0]).toHaveLength(10); | ||
expect(response.series.ms.data[2][0]).toHaveLength(15); | ||
}); | ||
|
||
it('read test.mzML', () => { | ||
const data = readFileSync(`${pathFiles}test.mzML`); | ||
let response = mzML(data); | ||
expect(response.times).toHaveLength(1500); | ||
expect(response.times.slice(0, 6)).toStrictEqual([ | ||
0, | ||
0.2, | ||
0.4, | ||
0.6, | ||
0.8, | ||
1 | ||
]); | ||
expect(response.series.ms.data).toHaveLength(1500); | ||
expect(response.series.ms.data[0][0]).toHaveLength(336); | ||
expect(response.series.ms.data[1][0]).toHaveLength(465); | ||
expect(response.series.ms.data[2][0]).toHaveLength(465); | ||
}); | ||
|
||
it('read compressed 32bits', () => { | ||
const data = readFileSync(`${pathFiles}small_zlib.pwiz.1.1.mzML`); | ||
let response = mzML(data); | ||
expect(response.times).toHaveLength(48); | ||
expect(response.times.slice(0, 6)).toStrictEqual([ | ||
0.004935, | ||
0.007896666666666666, | ||
0.011218333333333334, | ||
0.022838333333333332, | ||
0.034925, | ||
0.04862 | ||
]); | ||
expect(response.series.ms.data).toHaveLength(48); | ||
expect(response.series.ms.data[0][0]).toHaveLength(19914); | ||
expect(response.series.ms.data[1][0]).toHaveLength(19800); | ||
expect(response.series.ms.data[2][0]).toHaveLength(485); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const processMetadata = require('./processMetaData'); | ||
const processSpectrumList = require('./processSpectrumList'); | ||
|
||
function processMZData(topLevel, result) { | ||
processMetadata(topLevel, result.metadata); | ||
processSpectrumList(topLevel, result.times, result.series.ms.data); | ||
} | ||
|
||
module.exports = processMZData; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const pako = require('pako'); | ||
const toByteArray = require('base64-js').toByteArray; | ||
const parseCvParam = require('./parseCvParam'); | ||
|
||
function decodeData(node) { | ||
let data = node.binary; | ||
let attr = node._attr; | ||
let cvParam = parseCvParam(node.cvParam); | ||
if (!data || !attr) return []; | ||
let buffer = decoder(data, cvParam); | ||
let kind = ''; | ||
if (cvParam.mzArray) { | ||
kind = 'mz'; | ||
} else if (cvParam.intensityArray) { | ||
kind = 'intensity'; | ||
} else { | ||
throw new Error('unknown binary data type'); | ||
} | ||
|
||
if (cvParam['64BitFloat']) { | ||
let result = {}; | ||
result[kind] = new Float64Array(buffer.buffer); | ||
return result; | ||
} else if (cvParam['32BitFloat']) { | ||
let result = {}; | ||
result[kind] = new Float32Array(buffer.buffer); | ||
return result; | ||
} | ||
|
||
throw new Error(`unknown precision in decoder: ${attr.precision}`); | ||
} | ||
|
||
function decoder(base64Encoded, cvParams = {}) { | ||
if (cvParams.zlibCompression) { | ||
return pako.inflate(toByteArray(base64Encoded)); | ||
} else { | ||
return toByteArray(base64Encoded); | ||
} | ||
} | ||
|
||
module.exports = decodeData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
const camelCase = require('camelcase'); | ||
|
||
function parseCvParam(cvParam) { | ||
let result = {}; | ||
if (!cvParam) return result; | ||
let cvParams; | ||
if (Array.isArray(cvParam)) { | ||
cvParams = cvParam; | ||
} else { | ||
cvParams = [cvParam]; | ||
} | ||
for (let param of cvParams) { | ||
let attr = param._attr; | ||
if (attr.name) { | ||
result[camelCase(attr.name.toLowerCase().replace(/[^ a-z0-9]/g, ''))] = { | ||
accession: attr.accession, | ||
cvLabel: attr.cvLabel, | ||
value: attr.value, | ||
name: attr.name | ||
}; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
module.exports = parseCvParam; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const processSpectrumList = require('./processSpectrumList'); | ||
|
||
function processMZML(topLevel, result) { | ||
//processMetadata(topLevel, result.metadata); | ||
processSpectrumList(topLevel, result.times, result.series.ms.data); | ||
} | ||
|
||
module.exports = processMZML; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const parseCvParam = require('./parseCvParam'); | ||
const parseBinaryDataArray = require('./parseBinaryDataArray'); | ||
|
||
function processSpectrumList(parsed, times, msData) { | ||
if ( | ||
!parsed || | ||
!parsed.run || | ||
!parsed.run.spectrumList || | ||
!parsed.run.spectrumList.spectrum | ||
) | ||
return; | ||
let spectrumList = parsed.run.spectrumList.spectrum; | ||
for (let spectrum of spectrumList) { | ||
let scanList = spectrum.scanList; | ||
if (Array.isArray(scanList)) throw new Error('Unsupported scanList'); | ||
|
||
let scan = scanList.scan; | ||
|
||
if (typeof scan !== 'object') continue; | ||
if (Array.isArray(scan)) { | ||
throw new Error('processSpectrumList: scan may not be an array'); | ||
} | ||
let cvParam = parseCvParam(scan.cvParam); | ||
times.push(cvParam.scanStartTime.value); | ||
|
||
let dataArrayList = spectrum.binaryDataArrayList.binaryDataArray; | ||
if (dataArrayList.length !== 2) { | ||
throw new Error('Can not decodeData because length !== 2'); | ||
} | ||
|
||
let first = parseBinaryDataArray(dataArrayList[0]); | ||
let second = parseBinaryDataArray(dataArrayList[1]); | ||
|
||
msData.push([first.mz || second.mz, second.intensity || first.intensity]); | ||
} | ||
} | ||
|
||
module.exports = processSpectrumList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pako from 'pako'; | ||
import { decode } from 'base64-arraybuffer'; | ||
|
||
export function decoder(base64Encoded, options = {}) { | ||
const { compressionAlgorithm } = options; | ||
let decoded; | ||
switch (compressionAlgorithm) { | ||
case 'zlib': | ||
decoded = pako.deflate(decode(base64Encoded)); | ||
break; | ||
case undefined: | ||
case '': | ||
decoded = decode(base64Encoded); | ||
break; | ||
default: | ||
throw new Error( | ||
`utils.decoder: unknown compression: ${compressionAlgorithm}`, | ||
); | ||
} | ||
if (!decoded.byteLength % 8) { | ||
throw new Error('decode to Float64Array not the right length'); | ||
} | ||
return new Float64Array(decoded); | ||
} | ||
|
||
export function formatResult(spectra) { | ||
let result = { | ||
times: [], | ||
series: { | ||
ms: { | ||
data: [], | ||
dimensions: 2, | ||
}, | ||
}, | ||
}; | ||
for (let index in spectra) { | ||
let element = spectra[index]; | ||
if (element.time && element.mass && element.intensity) { | ||
result.times.push(Number(element.time)); | ||
result.series.ms.data.push([element.mass, element.intensity]); | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const searchObjectKey = require('../searchObjectKey'); | ||
|
||
test('searchObjectKey', () => { | ||
const object = { | ||
key1: { | ||
key2: true, | ||
key3: false, | ||
key4: { | ||
key5: true, | ||
key6: true | ||
} | ||
} | ||
}; | ||
|
||
expect(searchObjectKey(object, /key2/)).toEqual({ key2: true }); | ||
expect(searchObjectKey(object, /key4/)).toEqual({ | ||
key4: { | ||
key5: true, | ||
key6: true | ||
} | ||
}); | ||
|
||
expect(searchObjectKey(object, /key5/)).toEqual({ | ||
key5: true | ||
}); | ||
expect(searchObjectKey(object, /key7/)).toBe(undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function searchObjectKey(object, searchKey) { | ||
for (let key in object) { | ||
if (key.match(searchKey)) { | ||
let result = {}; | ||
result[key.toLowerCase()] = object[key]; | ||
return result; | ||
} else if (typeof object[key] === 'object' && !Array.isArray(object[key])) { | ||
let result = searchObjectKey(object[key], searchKey); | ||
if (result) return result; | ||
} else { | ||
} | ||
} | ||
} | ||
|
||
module.exports = searchObjectKey; |
Oops, something went wrong.