Data Parser

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.

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.

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

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

parserFunction:

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:function | default: null | (data) => { return data; }

event.detail 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();
});

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.assignmentData

Peak-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