JCAMP-DX Converter for NMR Data

This library provides tools for working with JCAMP-DX formatted spectroscopic data, with a focus on NMR (Nuclear Magnetic Resonance) data. It can:

Download:  

jcampdx.tar.gz

Installation:

Prerequisites

The following Python packages are required:

Installation Steps

  1. Install the required dependencies:
    cd jcampdx
    pip install numpy scipy matplotlib
    pip install nmrglue
    pip install rdkit
  2. For RDKit installation issues, you may need to use conda:
    conda install -c conda-forge rdkit

Verifying Installation

To verify that the installation was successful, you can run a simple test:

python -c "import jcamp; print('JCAMP-DX library installed successfully')"

Development Installation

For development purposes, you can install the package in development mode:

pip install -e .

Usage:

Command Line Interface

The main entry point for creating JCAMP-DX files is createjcamp.py. It can be used to convert various NMR data formats to JCAMP-DX.

Basic usage:

python createjcamp.py [options...] > output.jdx

To see all available options:

python createjcamp.py --help
Command Line Options
General Options
Block Types
File Options
Metadata Options
Converting from NMRML format

NMRML is an XML-based format for NMR data. The library can directly convert NMRML files to JCAMP-DX:

python createjcamp.py --fromnmrml input_file.nmrML > output.jdx

This is a simplified command that handles all the conversion details automatically.

Creating a JCAMP-DX file with multiple blocks

A single JCAMP-DX file can contain multiple blocks of different types. This example creates a file with structure, spectrum, peak list, and assignments:

python createjcamp.py --title "TEST FILE" \
--block STRUCTURE   --file structure.mol                --title "STRUCTURE" \
--block 1DSPECTRUM  --file example_1h_spectrum.txt csv  --title "SPECTRUM"      --frq 500 --nuc 1H --refcpd DSS --refppm 0 \
--block 1DPEAKS     --file example_1h_peaklist.txt csv  --title "PEAKS"         --frq 500 --nuc 1H --link 1 \
--block ASSIGNMENTS --file example_1h_peaklist.txt csv  --title "ASSIGNMENTS"   --frq 500 --nuc 1H --link 2 \
> output.jdx

The --link option creates cross-references between blocks, which is useful for connecting related data (e.g., a spectrum and its peak assignments).

Converting Vendor-Specific Data
Bruker Data
python createjcamp.py --title "FROM BRUKER" --block 1DSPECTRUM --file ./nmr.fid/pdata/1/ bruk > output.jdx

For Bruker 2D data:

python createjcamp.py --title "BRUKER 2D" --block 2DSPECTRUM --file ./nmr.fid/pdata/1/ bruk > output_2d.jdx
Varian Data
python createjcamp.py --title "FROM VARIAN" --block 1DSPECTRUM --file ./varian_data/ var > output.jdx
python createjcamp.py --title "VARIAN 2D" --block 2DSPECTRUM --file ./varian_data/ var > output_2d.jdx

Python API

import jcamp from jcamp 
import FileParser

# Parse a JCAMP-DX file
data = FileParser.parse_jcamp("input.jdx")

# Create a JCAMP-DX object from parsed data
jdx = FileParser.create_jcamp(data)

# Scale the data for optimal representation
jdx.data.scale_x()
jdx.data.scale_y()

# Convert to nmrglue format
udic, d = jdx.to_nmrglue_1d()

# Write to a new JCAMP-DX file
with open("output.jdx", "w") as f:
    f.write(jdx.write())

Supported Data Types and Formats:

Input Formats

JCAMP-DX Block Types

Algorithm Details:

JCAMP-DX Format and Structure

JCAMP-DX (Joint Committee on Atomic and Molecular Physical Data - Data Exchange) is a standardized format for exchanging spectroscopic data. The format consists of:

This library implements JCAMP-DX versions 4.24, 5.01, and 6.00, with a focus on NMR data as specified in the JCAMP-DX standard.

JCAMP-DX Encoding

These encoding formats are used to compress the data and reduce file size while maintaining data integrity. The encoding/decoding process is handled by the encode.pyand decode.py modules.

Data Processing Workflow

  1. Parsing: Read input data from various formats (JCAMP-DX, Bruker, Varian, etc.)
  2. Data Structure Creation: Create appropriate data structures for the type of data
  3. Processing: Apply necessary transformations (scaling, normalization, etc.)
  4. Encoding: Convert data to JCAMP-DX format with appropriate encoding
  5. Output: Write the formatted data to a file

Data Processing Features

Scaling data
jdx.data.scale_x()  # Scale x-axis data
jdx.data.scale_y()  # Scale y-axis data to fit in 16-bit integer range
Format conversion
# Convert to nmrglue format
udic, data = jdx.to_nmrglue_1d()

# Create JCAMP-DX from nmrglue data
jdx = NMRSpectrum.from_nmrglue_1d(udic, data)
Complex data handling
# For FID data with complex values
fid = NMRFid()
fid.data = NTuples1DFIDData(complex_data)
Chemical structure processing
# Add a molecular structure to a JCAMP-DX file
block = jcamp_dx.blocks[0]
block.add_mol("structure.mol")
Peak assignments
# Add peak assignments
block.add_assignments("assignments.csv", filetype="csv")

Digital Filter Removal

For Bruker data, the library can remove the digital filter effects that are present in the raw FID data. This is important for accurate processing of Bruker NMR data:

# Remove digital filter effects
grpdly = remove_digital_filter(dic, data)

The algorithm uses the group delay parameter from the Bruker acquisition parameters to correct the phase distortion introduced by the digital filter.

Multidimensional Data Handling

# Create a 2D spectrum
spec2d = NMRSpectrum2D()
spec2d.data = NTuplesNDSpectrumData(data2d, ranges)

Examples:

Parsing and Visualizing a 1D NMR Spectrum

import jcamp from jcamp 
import FileParser
import matplotlib.pyplot as plt
import numpy as np

# Parse a JCAMP-DX file
data = FileParser.parse_jcamp("spectrum.jdx")
jdx = FileParser.create_jcamp(data)

# Convert to nmrglue format
udic, d = jdx.to_nmrglue_1d()

# Create x-axis in ppm
xaxis = np.fft.fftshift(np.fft.fftfreq(udic[0]['size'], 1/udic[0]['sw']))[::-1]
if udic[0]['car'] != 999.99:
    xaxis += udic[0]['car']
    xaxis /= udic[0]['obs']

# Plot the spectrum
plt.figure(figsize=(10, 6))
plt.plot(xaxis, d)
plt.xlabel('Chemical Shift (ppm)')
plt.ylabel('Intensity')
plt.title('NMR Spectrum')
plt.gca().invert_xaxis()
plt.show()

License:

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments:

This library uses the following open-source projects: