Data Parser

The Data Parser is a utility class in JSpectraViewer that parses uploaded spectrum files, including nmrML (1D and 2D NMR), JCAMP-DX, mzML, XY/CSV/TSV, and XML spectra (IR, EI-MS). It automatically parses the required X, Y (and Z for 2D NMR) data, plus peak-atom assignments and 3D structure data for 1D NMR nmrML files. 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.

Usage:

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; the DataParser listens for its change event and parses the selected file.

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
});

dataType:

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"]

expectedDataTag:

Optional. Specifies the XML tag that contains the spectrum data. Used for nmrML and other XML files; JCAMP-DX, mzML, XY/CSV/TSV and MOL files are detected automatically and ignore this option.

string | default: "spectrumDataArray"

Supported file formats

The file format is detected from the file name and content before the dataType is applied:

Methods

set_data_type(dataType)

Changes the dataType used for the next parsed file (e.g. when the user switches spectrum type).

set_expected_data_tag(expectedDataTag)

Changes the expectedDataTag used for the next parsed file.

addEventListener(eventName, callback) / on(eventName, callback)

Registers a listener for the DataParser's events (see below). Listen on the DataParser instance, not on the file input element.

Events

fileParsed

Fired when a file is parsed successfully. event.detail contains the parsed data (see below).

fileParseError

Fired when a file cannot be parsed. event.detail is { error, file }: the error message and the file name.

event.detail properties

When the fileParsed event fires, event.detail contains the parsed spectrum data. Pass these values directly into SpectraViewer1D or SpectraViewer2D.

dataParser.addEventListener("fileParsed", (event) => {
  const { x, y, assignmentData } = event.detail;
  let viewer = new SpectraViewer1D("#graph", {
    chartTitle: { text: "1D NMR Spectrum" },
    chartType: "1D-NMR",
    dataset: [{ name: "Spectrum", dataArray: { x, y }, normalize: true, style: { strokeColor: "black" } }],
    axis: { x: { reverse: true, label: { text: "ppm" } }, y: { label: { text: "Intensity" } } },
    grid: { horizontal: {}, vertical: {} },
    controls: { options: { zoom: {} } },
    assignmentTable: {
      containerID: "#assignments",
      data: [assignmentData],
    },
  });
});
dataParser.addEventListener("fileParseError", (event) => {
  console.error(event.detail.file, event.detail.error);
});

event.detail.x

Array of X-axis values (chemical shift in ppm for NMR, wavenumber for IR, wavelength for UV, m/z for MS).

number[]

event.detail.y

Array of Y-axis intensity values, same length as x.

number[]

event.detail.z

Array of Z values. Only present for 2D NMR data (dataType: "2D-NMR").

number[] | undefined for 1D types

event.detail.dataType

The dataType the file was parsed as.

string

event.detail.assignmentData

Peak-to-atom assignment object. Only present for 1D NMR nmrML files that contain assignment data (an atomAssignmentList). Pass as an element of the assignmentTable.data array in SpectraViewer1D. Also carries a .name string (the compound or spectrum name) and a .structure_data object with the molecular structure used by the 3D structure viewer.

object | undefined if no assignment data in file