The Data Parser is a utility class in JSpectraViewer that allows you to parse various data formats, including JSON, nmrML, and CSV. It automatically parses the required X, Y, peak-atom assignments (NMR), and 3D structure data (NMR). It is designed to work with the SpectraViewer1D and SpectraViewer2D classes for visualizing parsed data. It emits a 'fileParsed' event when the file is successfully parsed, which can be used to trigger further actions in your application. The parsed data can be accessed through the `event.detail` property of the event.
To use the DataParser, you need to initialize it with a file selector and options.The file selector should be an <input type="file"> element.
import { DataParser } from 'jsv2';
let dataParser = new DataParser("#file-selector", { // Initialize the DataParser with the input element selector
dataType: "1D-NMR", // Specify the type of data you expect
expectedDataTag: "spectrumDataArray", // Optional, specify if you expect a specific tag in the data
parserFunction: (data) => {
// Custom parsing function if needed
return data;
}
});Specifies the type of visualization you expect to create.
string | default: "1D-NMR" | ["1D-NMR", "2D-NMR", "1D-EIMS", "1D-LCMS", "1D-IR", "1D-UV"]Optional. Specifies the tag in the data that contains the spectrum data array. Useful for nmrML (or any XML) data.
string | default: "spectrumDataArray" | ["spectrumDataArray", "spectrum.binary", "ms-ir-spectrum", "binary"]Optional. A custom parsing function that takes the raw data as input and returns the parsed data. Returned data structure must be an
object with the following properties:When the fileParsed event fires, event.detail contains the parsed spectrum data. Pass these values directly into SpectraViewer1D or SpectraViewer2D.
document.querySelector("#file-selector").addEventListener("fileParsed", (event) => {
const { x, y, z, assignmentData } = event.detail;
let viewer = new SpectraViewer1D("#graph", {
dataset: [{ dataArray: { x, y }, normalize: true }],
assignmentTable: {
enabled: true,
containerID: "#assignments",
data: [assignmentData],
},
});
viewer.draw();
});Array of X-axis values (chemical shift in ppm for NMR, wavenumber for IR, wavelength for UV, m/z for MS).
number[]Array of Y-axis intensity values, same length as x.
number[]Array of Z values. Only present for 2D NMR data (dataType: "2D-NMR").
number[] | undefined for 1D typesPeak-to-atom assignment object. Present when the parsed file contains NMR assignment data (e.g. nmrML with an atomAssignmentList). Pass as an element of the assignmentTable.data array in SpectraViewer1D. Also carries a .name string (the compound or spectrum name) and an optional .structureData object with 3D coordinates for the molecular structure viewer.
object | undefined if no assignment data in file