MolSpec

MolSpec is a standalone JavaScript module for embedding metabolite spectrum data into any page. It provides two entry points: MolSpec.init fetches and displays all available spectra for a given metabolite as a summary table, and MolSpec.initViewer renders a full single-spectrum show page — including the spectrum graph, experimental conditions, references, and (for NMR) an assignment table and 3D structure viewer. Both functions communicate with the /spectra_proxy backend routes and delegate rendering to the appropriate JSV viewer. MolSpec is not part of the jsv2 npm package; it is a standalone molspec.js script, and the host application must provide the backend routes it calls.

Supported Spectrum Types

MolSpec handles the following spectrum types, identified by their string key:

KeyLabel
nmr_one_d1D NMR
nmr_two_d2D NMR
c_msGC-MS
ei_msMS (EI)
ms_msLC-MS/MS
ms_irMS-IR

MolSpec.init

Fetches all available spectra for a metabolite and renders a summary table inside a container element. Use this on metabolite show pages where you want to list every spectrum type in one place.

MolSpec.init(metaboliteId, databaseId, containerId, options);

Parameters

metaboliteId

Internal numeric ID of the metabolite record. Currently unused by MolSpec (kept for API compatibility).

number | required

databaseId

External database identifier for the metabolite (e.g. HMDB accession). Sent to the proxy as ?database_id= when fetching each spectrum type.

string | required

containerId

ID (without #) of the DOM element that will receive the rendered table.

string | required

options

Optional configuration object.

object | default: {}

Example

MolSpec.init(1234, "HMDB0000001", "spectra-list");
// With options:
MolSpec.init(1234, "HMDB0000001", "spectra-list", {
  prefix: "specdb",
  spectraTypes: [{ key: "nmr_one_d", label: "1D NMR" }],
  onResults: function(results) {
    console.log(results);
  },
});

MolSpec.initViewer

Renders a complete spectrum show page inside a container element. Reads the spectrum type and ID from data-type and data-id attributes on the container, fetches the spectrum record from /spectra_proxy/<type>/<id>/raw and the compound name from /metabolites/<database_id>/info, then builds and inserts the panels: Spectrum Details, Spectrum View (delegated to the appropriate JSV viewer), Experimental Conditions, Notes (when the spectrum has notes), and References. For 1D NMR with an nmrML file, an assignment table and 3D structure viewer are also appended.

// Container element must have data-type and data-id attributes:
// <div id="spectrum-viewer" data-type="nmr_one_d" data-id="42"></div>
MolSpec.initViewer("spectrum-viewer");

Parameters

containerId

ID (without #) of the container element. Must have data-type and data-id attributes set.

string | required

options

Optional configuration object.

object | default: {}

data-type values and viewer delegation

initViewer delegates spectrum rendering based on data-type to these internal functions (they are not exposed on MolSpec or window):

data-typeViewer calledNotes
nmr_one_dinitNmrOneD()Loads 3Dmol.js first if an nmrML document is present; renders assignment table and 3D structure
nmr_two_dinitNmrTwoD()Falls back to the spectrum's image document if no JSON spectrum data is available
ms_irinitIrViewer()Fetches XML format from the proxy
c_ms, ei_ms, ms_msinitMsViewer(chartType)Fetches the peak list from /spectra_proxy/<type>/<id>/peaks; renders a bar chart (1D-EIMS for ei_ms, 1D-LCMS otherwise)

Example

MolSpec.initViewer("spectrum-viewer");
// With custom render callback:
MolSpec.initViewer("spectrum-viewer", {
  onRender: function(spectrum, container, type, id) {
    container.innerHTML = "<pre>" + JSON.stringify(spectrum, null, 2) + "</pre>";
  },
});

MolSpec.renderTable

Low-level helper that renders a results array (as returned by onResults) into a summary table inside a container. Called internally by MolSpec.init when no onResults callback is provided. Exposed publicly for cases where you fetch and post-process results yourself before rendering.

MolSpec.renderTable(containerId, results, prefix);

containerId
string | required — ID of the target DOM element

results

Array of { type: { key, label }, spectra: [...] } objects, as returned by the onResults callback.

array | required

prefix

URL prefix for "View Spectrum" links — same as options.prefix in MolSpec.init.

string | default: null

MolSpec.SPECTRUM_TYPES

The default array of spectrum type objects used when no spectraTypes override is given. Each entry has a key (proxy route segment) and a label (display name).

MolSpec.SPECTRUM_TYPES
// [
//   { key: "c_ms",      label: "GC-MS" },
//   { key: "ei_ms",     label: "MS" },
//   { key: "ms_ms",     label: "LC-MS/MS" },
//   { key: "nmr_one_d", label: "1D NMR" },
//   { key: "nmr_two_d", label: "2D NMR" },
//   { key: "ms_ir",     label: "MS-IR" },
// ]