brukerconverter
Advanced tools
Comparing version 5.0.3 to 6.0.0
import { DoubleArray } from 'cheminfo-types'; | ||
import type { FileCollectionItem, FileCollection } from 'filelist-utils'; | ||
import { DoubleMatrix } from 'ml-spectra-processing'; | ||
@@ -72,3 +71,3 @@ type FirstLevelKeys = | ||
expno: number; | ||
fileCollection: FileCollection; | ||
fileCollectionItems: FileCollectionItem[]; | ||
processedData?: ProcessedData; | ||
@@ -136,4 +135,4 @@ is2D?: boolean; | ||
export interface MinMax { | ||
z: DoubleMatrix; | ||
export interface MinMaxContent { | ||
z: DoubleArray[]; | ||
minZ: number; | ||
@@ -146,2 +145,4 @@ maxZ: number; | ||
} | ||
export type MinMax = Record<string, MinMaxContent>; | ||
export interface SpectraData2D { | ||
@@ -148,0 +149,0 @@ spectra: Spectrum2D[]; |
145
lib/index.js
@@ -227,2 +227,4 @@ 'use strict'; | ||
const QSEQ = 2; | ||
async function setFIDSpectrumData(file, spectra) { | ||
@@ -238,11 +240,10 @@ let td = parseInt(spectra.meta.TD[0], 10); | ||
let SW_H = Number(spectra.meta.SW_h[0]); | ||
const SW_H = Number(spectra.meta.SW_h[0]); | ||
let SF = Number(spectra.meta.SFO1[0]); | ||
const SF = Number(spectra.meta.SFO1[0]); | ||
spectra.meta.DATATYPE = 'NMR FID'; | ||
let DW = 1 / (2 * SW_H); | ||
let AQ = td * DW; | ||
const DW = 1 / (2 * SW_H); | ||
const AQ = td * DW; | ||
let endian = parseInt(spectra.meta.BYTORDA, 10); | ||
@@ -263,3 +264,6 @@ endian = endian ? 0 : 1; | ||
const stopReading = td / 2; | ||
const aqMode = spectra.meta.AQ_mod; | ||
const deltaX = aqMode === QSEQ ? DW : 2 * DW; | ||
const nbPoints = aqMode === QSEQ ? td : (td / 2) >> 0; | ||
for (let j = 0; j < nbSubSpectra; j++) { | ||
@@ -269,3 +273,3 @@ let toSave = { | ||
dataTable: '(X++(R..R))', | ||
nbPoints: td, | ||
nbPoints, | ||
firstX: 0, | ||
@@ -277,16 +281,24 @@ lastX: AQ, | ||
data: { | ||
x: mlSpectraProcessing.createStepArray({ length: td, step: DW }), | ||
re: new Float64Array(td), | ||
im: new Float64Array(td), | ||
x: mlSpectraProcessing.createStepArray({ length: nbPoints, step: deltaX }), | ||
re: new Float64Array(nbPoints), | ||
im: new Float64Array(nbPoints), | ||
}, | ||
isXYdata: true, | ||
isFID: true, | ||
observeFrequency: SF, | ||
title: spectra.meta.TITLE, | ||
deltaX: DW, | ||
deltaX, | ||
}; | ||
spectra.spectra[j] = toSave; | ||
for (let i = 0; i < stopReading; i++) { | ||
spectra.spectra[j].data.re[i] = ioBuffer.readInt32(); | ||
spectra.spectra[j].data.im[i] = ioBuffer.readInt32(); | ||
// const dtypa = spectra.meta.DTYPA;// we should use it for float or double FID data; | ||
if (aqMode === QSEQ) { | ||
for (let i = 0; i < nbPoints; i++) { | ||
spectra.spectra[j].data.re[i] = ioBuffer.readInt32(); | ||
} | ||
} else { | ||
for (let i = 0; i < nbPoints; i++) { | ||
spectra.spectra[j].data.re[i] = ioBuffer.readInt32(); | ||
spectra.spectra[j].data.im[i] = ioBuffer.readInt32(); | ||
} | ||
} | ||
@@ -296,7 +308,9 @@ } | ||
async function setXYSpectrumData(file, spectra) { | ||
let ioBufferReal = file.re ? new iobuffer.IOBuffer(await file.re.arrayBuffer()) : null; | ||
let ioBufferImaginary = file.im | ||
? new iobuffer.IOBuffer(await file.im.arrayBuffer()) | ||
: null; | ||
async function setProcessedSpectrumData(files, spectra) { | ||
const buffers = {}; | ||
for (const key in files) { | ||
if (!files[key]) continue; | ||
buffers[key] = new iobuffer.IOBuffer(await files[key].arrayBuffer()); | ||
} | ||
let td = getDirectParameter(spectra.meta.SI); | ||
@@ -317,8 +331,7 @@ let swP = getDirectParameter(spectra.meta.SW_p); | ||
let nbSubSpectra = spectra.meta.nbSubSpectra ? spectra.meta.nbSubSpectra : 1; | ||
if (endian) { | ||
if (file.re) ioBufferReal.setLittleEndian(); | ||
if (file.im) ioBufferImaginary.setLittleEndian(); | ||
for (const key in buffers) buffers[key].setLittleEndian(); | ||
} else { | ||
if (file.re) ioBufferReal.setBigEndian(); | ||
if (file.im) ioBufferImaginary.setBigEndian(); | ||
for (const key in buffers) buffers[key].setBigEndian(); | ||
} | ||
@@ -335,2 +348,3 @@ | ||
isXYdata: true, | ||
isFT: true, | ||
nucleus: spectra.meta.NUC1, | ||
@@ -342,21 +356,15 @@ observeFrequency: sf, | ||
let deltaX = toSave.deltaX; | ||
let x = new Float64Array(td); | ||
let re = new Float64Array(td); | ||
let im = file.im ? new Float64Array(td) : null; | ||
if (im) { | ||
const deltaX = toSave.deltaX; | ||
const x = mlSpectraProcessing.xSequentialFill({ from: offset, step: deltaX, size: td }); | ||
const datum = {}; | ||
for (const key in buffers) { | ||
const buffer = buffers[key]; | ||
const data = new Float64Array(td); | ||
for (let k = 0; k < td; ++k) { | ||
x[k] = offset + k * deltaX; | ||
re[k] = ioBufferReal.readInt32(); | ||
im[k] = ioBufferImaginary.readInt32(); | ||
data[k] = buffer.readInt32(); | ||
} | ||
} else { | ||
for (let k = 0; k < td; ++k) { | ||
x[k] = offset + k * deltaX; | ||
re[k] = ioBufferReal.readInt32(); | ||
} | ||
datum[key] = data; | ||
} | ||
toSave.data = im ? { x, re, im } : { x, re }; | ||
toSave.data = { ...datum, x }; | ||
@@ -377,3 +385,3 @@ spectra.spectra.push(toSave); | ||
if (files['1r'] || files['1i']) { | ||
await setXYSpectrumData( | ||
await setProcessedSpectrumData( | ||
{ | ||
@@ -419,3 +427,5 @@ re: files['1r'], | ||
result.meta.nbSubSpectra = files['2rr'] | ||
result.meta.nbSubSpectra = ['2rr', '2ri', '2ir', '2ii'].some( | ||
(key) => key in files, | ||
) | ||
? parseInt(result.meta.SI[1], 10) | ||
@@ -442,3 +452,11 @@ : parseInt(result.meta.TD[1], 10); | ||
result.meta.lastY = lastY; | ||
await setXYSpectrumData({ re: files['2rr'] }, result); | ||
await setProcessedSpectrumData( | ||
{ | ||
rr: files['2rr'], | ||
ri: files['2ri'], | ||
ir: files['2ir'], | ||
ii: files['2ii'], | ||
}, | ||
result, | ||
); | ||
} else if (files.ser) { | ||
@@ -484,13 +502,15 @@ firstY = 0; | ||
* @param spectra | ||
* @returns {{z: Array, minX: *, maxX: *, minY: *, maxY: *, minZ: *, maxZ: *, noise: number}} | ||
* @returns {{z: Array<Array<number>>, minX: *, maxX: *, minY: *, maxY: *, minZ: *, maxZ: *}} | ||
*/ | ||
function convertTo3DZ(spectra) { | ||
function convertTo3DZ(datum) { | ||
const spectra = datum.spectra; | ||
// if (fileKeys.length === 0) { | ||
// throw new Error('There is not data to extract'); | ||
// } | ||
let ySize = spectra.length; | ||
let xSize = spectra[0].data.re.length; | ||
let z = new Array(ySize); | ||
let xSize = spectra[0].data.x.length; | ||
for (let i = 0; i < ySize; i++) { | ||
z[i] = new Float64Array(spectra[i].data.re); | ||
} | ||
const firstX = spectra[0].data.x[0]; | ||
@@ -501,2 +521,27 @@ const lastX = spectra[0].data.x[xSize - 1]; | ||
const minMax = {}; | ||
const keyFiles = Object.keys(spectra[0].data).filter((key) => key !== 'x'); | ||
for (const keyFile of keyFiles) { | ||
minMax[keyFile] = extractZMatrix(datum, { | ||
keyFile, | ||
ySize, | ||
firstX, | ||
lastX, | ||
firstY, | ||
lastY, | ||
}); | ||
} | ||
return minMax; | ||
} | ||
function extractZMatrix(datum, options) { | ||
const spectra = datum.spectra; | ||
const { keyFile, ySize, firstX, lastX, firstY, lastY } = options; | ||
let z = new Array(ySize); | ||
for (let i = 0; i < ySize; i++) { | ||
z[i] = new Float64Array(spectra[i].data[keyFile]); | ||
} | ||
// Because the min / max value are the only information about the matrix if we invert | ||
@@ -545,3 +590,3 @@ // min and max we need to invert the array | ||
let result; | ||
if (brukerFiles.ser || brukerFiles['2rr']) { | ||
if (['ser', '2rr', '2ri', '2ir', '2ii'].some((key) => key in brukerFiles)) { | ||
result = await convert2D(brukerFiles, options); | ||
@@ -586,3 +631,3 @@ } else if (brukerFiles['1r'] || brukerFiles['1i'] || brukerFiles.fid) { | ||
if (result.twoD) { | ||
result.minMax = convertTo3DZ(result.spectra); | ||
result.minMax = convertTo3DZ(result); | ||
@@ -589,0 +634,0 @@ if (!options.keepSpectra) { |
{ | ||
"name": "brukerconverter", | ||
"version": "5.0.3", | ||
"version": "6.0.0", | ||
"description": "Parse and convert Bruker raw data", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -39,3 +39,3 @@ import { getCoffee } from 'bruker-data-test'; | ||
dataTable: '(X++(R..R))', | ||
nbPoints: 65536, | ||
nbPoints: 32768, | ||
firstX: 0, | ||
@@ -42,0 +42,0 @@ lastX: 3.984588800000003, |
@@ -34,4 +34,4 @@ import { getData } from 'bruker-data-test'; | ||
expect(metadataFt.SI[1]).toBe(1024); | ||
expect(minMaxFt.z).toHaveLength(1024); | ||
expect(minMaxFt.z[0]).toHaveLength(1024); | ||
expect(minMaxFt.rr.z).toHaveLength(1024); | ||
expect(minMaxFt.rr.z[0]).toHaveLength(1024); | ||
@@ -43,5 +43,5 @@ let minMaxSer = result[0].minMax; | ||
expect(metadataSer.TD[1]).toBe(128); | ||
expect(minMaxSer.z).toHaveLength(64); | ||
expect(minMaxSer.z[0]).toHaveLength(4096); | ||
expect(minMaxSer.re.z).toHaveLength(64); | ||
expect(minMaxSer.re.z[0]).toHaveLength(2048); | ||
}); | ||
}); |
import { joinInfoMeta } from './joinMetaInfo.js'; | ||
import { parseData } from './parseData'; | ||
import { setFIDSpectrumData } from './setFIDSpectrumData'; | ||
import { setXYSpectrumData } from './setXYSpectrumData'; | ||
import { setProcessedSpectrumData } from './setProcessedSpectrumData'; | ||
@@ -12,3 +12,3 @@ export async function convert1D(files, options) { | ||
if (files['1r'] || files['1i']) { | ||
await setXYSpectrumData( | ||
await setProcessedSpectrumData( | ||
{ | ||
@@ -15,0 +15,0 @@ re: files['1r'], |
@@ -5,3 +5,3 @@ import { joinInfoMeta } from './joinMetaInfo.js'; | ||
import { setFIDSpectrumData } from './setFIDSpectrumData'; | ||
import { setXYSpectrumData } from './setXYSpectrumData'; | ||
import { setProcessedSpectrumData } from './setProcessedSpectrumData'; | ||
@@ -21,3 +21,5 @@ export async function convert2D(files, options) { | ||
result.meta.nbSubSpectra = files['2rr'] | ||
result.meta.nbSubSpectra = ['2rr', '2ri', '2ir', '2ii'].some( | ||
(key) => key in files, | ||
) | ||
? parseInt(result.meta.SI[1], 10) | ||
@@ -44,3 +46,11 @@ : parseInt(result.meta.TD[1], 10); | ||
result.meta.lastY = lastY; | ||
await setXYSpectrumData({ re: files['2rr'] }, result, true); | ||
await setProcessedSpectrumData( | ||
{ | ||
rr: files['2rr'], | ||
ri: files['2ri'], | ||
ir: files['2ir'], | ||
ii: files['2ii'], | ||
}, | ||
result, | ||
); | ||
} else if (files.ser) { | ||
@@ -59,3 +69,3 @@ firstY = 0; | ||
yTransmitterFrequencyOffset / yTransmitterFrequency + yWindowSize / 2; | ||
await setFIDSpectrumData(files.ser, result); | ||
await setFIDSpectrumData(files.ser, result, true); | ||
} | ||
@@ -62,0 +72,0 @@ |
@@ -17,3 +17,3 @@ import { convert1D } from './convert1D'; | ||
let result; | ||
if (brukerFiles.ser || brukerFiles['2rr']) { | ||
if (['ser', '2rr', '2ri', '2ir', '2ii'].some((key) => key in brukerFiles)) { | ||
result = await convert2D(brukerFiles, options); | ||
@@ -58,3 +58,3 @@ } else if (brukerFiles['1r'] || brukerFiles['1i'] || brukerFiles.fid) { | ||
if (result.twoD) { | ||
result.minMax = convertTo3DZ(result.spectra); | ||
result.minMax = convertTo3DZ(result); | ||
@@ -61,0 +61,0 @@ if (!options.keepSpectra) { |
@@ -5,13 +5,15 @@ import { matrixMinMaxZ } from 'ml-spectra-processing'; | ||
* @param spectra | ||
* @returns {{z: Array, minX: *, maxX: *, minY: *, maxY: *, minZ: *, maxZ: *, noise: number}} | ||
* @returns {{z: Array<Array<number>>, minX: *, maxX: *, minY: *, maxY: *, minZ: *, maxZ: *}} | ||
*/ | ||
export default function convertTo3DZ(spectra) { | ||
export default function convertTo3DZ(datum) { | ||
const spectra = datum.spectra; | ||
// if (fileKeys.length === 0) { | ||
// throw new Error('There is not data to extract'); | ||
// } | ||
let ySize = spectra.length; | ||
let xSize = spectra[0].data.re.length; | ||
let z = new Array(ySize); | ||
let xSize = spectra[0].data.x.length; | ||
for (let i = 0; i < ySize; i++) { | ||
z[i] = new Float64Array(spectra[i].data.re); | ||
} | ||
const firstX = spectra[0].data.x[0]; | ||
@@ -22,2 +24,27 @@ const lastX = spectra[0].data.x[xSize - 1]; | ||
const minMax = {}; | ||
const keyFiles = Object.keys(spectra[0].data).filter((key) => key !== 'x'); | ||
for (const keyFile of keyFiles) { | ||
minMax[keyFile] = extractZMatrix(datum, { | ||
keyFile, | ||
ySize, | ||
firstX, | ||
lastX, | ||
firstY, | ||
lastY, | ||
}); | ||
} | ||
return minMax; | ||
} | ||
function extractZMatrix(datum, options) { | ||
const spectra = datum.spectra; | ||
const { keyFile, ySize, firstX, lastX, firstY, lastY } = options; | ||
let z = new Array(ySize); | ||
for (let i = 0; i < ySize; i++) { | ||
z[i] = new Float64Array(spectra[i].data[keyFile]); | ||
} | ||
// Because the min / max value are the only information about the matrix if we invert | ||
@@ -24,0 +51,0 @@ // min and max we need to invert the array |
import { IOBuffer } from 'iobuffer'; | ||
import { createStepArray } from 'ml-spectra-processing'; | ||
import * as aqModeDirect from '../constants/acquisitionModeDirect'; | ||
export async function setFIDSpectrumData(file, spectra) { | ||
@@ -14,11 +16,10 @@ let td = parseInt(spectra.meta.TD[0], 10); | ||
let SW_H = Number(spectra.meta.SW_h[0]); | ||
const SW_H = Number(spectra.meta.SW_h[0]); | ||
let SF = Number(spectra.meta.SFO1[0]); | ||
const SF = Number(spectra.meta.SFO1[0]); | ||
spectra.meta.DATATYPE = 'NMR FID'; | ||
let DW = 1 / (2 * SW_H); | ||
let AQ = td * DW; | ||
const DW = 1 / (2 * SW_H); | ||
const AQ = td * DW; | ||
let endian = parseInt(spectra.meta.BYTORDA, 10); | ||
@@ -39,3 +40,6 @@ endian = endian ? 0 : 1; | ||
const stopReading = td / 2; | ||
const aqMode = spectra.meta.AQ_mod; | ||
const deltaX = aqMode === aqModeDirect.QSEQ ? DW : 2 * DW; | ||
const nbPoints = aqMode === aqModeDirect.QSEQ ? td : (td / 2) >> 0; | ||
for (let j = 0; j < nbSubSpectra; j++) { | ||
@@ -45,3 +49,3 @@ let toSave = { | ||
dataTable: '(X++(R..R))', | ||
nbPoints: td, | ||
nbPoints, | ||
firstX: 0, | ||
@@ -53,18 +57,26 @@ lastX: AQ, | ||
data: { | ||
x: createStepArray({ length: td, step: DW }), | ||
re: new Float64Array(td), | ||
im: new Float64Array(td), | ||
x: createStepArray({ length: nbPoints, step: deltaX }), | ||
re: new Float64Array(nbPoints), | ||
im: new Float64Array(nbPoints), | ||
}, | ||
isXYdata: true, | ||
isFID: true, | ||
observeFrequency: SF, | ||
title: spectra.meta.TITLE, | ||
deltaX: DW, | ||
deltaX, | ||
}; | ||
spectra.spectra[j] = toSave; | ||
for (let i = 0; i < stopReading; i++) { | ||
spectra.spectra[j].data.re[i] = ioBuffer.readInt32(); | ||
spectra.spectra[j].data.im[i] = ioBuffer.readInt32(); | ||
// const dtypa = spectra.meta.DTYPA;// we should use it for float or double FID data; | ||
if (aqMode === aqModeDirect.QSEQ) { | ||
for (let i = 0; i < nbPoints; i++) { | ||
spectra.spectra[j].data.re[i] = ioBuffer.readInt32(); | ||
} | ||
} else { | ||
for (let i = 0; i < nbPoints; i++) { | ||
spectra.spectra[j].data.re[i] = ioBuffer.readInt32(); | ||
spectra.spectra[j].data.im[i] = ioBuffer.readInt32(); | ||
} | ||
} | ||
} | ||
} |
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
53288
23
1515