hqs_nmr.spectrumio

Deserialization and serialization from various NMR spectrum formats.

Functions

bruker_dir_to_jdx(directory, molecule_name)

Read spectral data from a Bruker directory and write it to a JCAMP-DX file.

read_bruker_dir(directory[, source, license_id])

Read spectral data from Bruker directory.

read_jdx(file[, source, license_id])

Read spectral data from a JCAMP-DX (JDX) file.

read_jdx_string(file_content[, source, ...])

Parses JDX spectral data from string.

read_magritek_dir(directory[, dx_file, ...])

Read spectrum data from Magritek Spinsolve folder.

write_jdx(x_data, y_data, frequency_MHz, ...)

Write a JCAMP-DX file.

hqs_nmr.spectrumio._extract_solvent_jcampdx(data: dict[str, Any]) list[str][source]

Extracts solvent string from a JCAMP-DX data dictionary if available.

With JCAMP-DX version 5.01 a new optional “.SOLVENT NAME” key has been added to store a description of the solvent. According to the specification this may include pH, ionic strength, if relevant. This function does not distinguish the solvent name from such additional data.

A few notes regarding the parsing via the nmrglue package: Since a JCAMP-DX file may contain more than one block (ends with “##END”), nmrglue collects all values of a key in a list. Furthermore, nmrglue normalizes the key in the data dictionary, i.e. while the JCAMP-DX file may contain “##.SOLVENTNAME” or “##.SOLVENT NAME”, the resulting data dictionary only contains a “.SOLVENTNAME” key in both cases.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

Solvent string(s) or an empty list if no solvent information was found.

hqs_nmr.spectrumio._extract_solvent_bruker(data: dict[str, Any]) str[source]

Extracts solvent string from a Bruker data dictionary if available.

The Bruker directory format stores the solvent name in the acquisition file (acqus or acqu) under the non-standard key “##$SOLVENT”. The solvent name is usually enclosed in angle brackets (e.g. <DMSO>), which are stripped of with this function.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

Solvent string or an empty string if no solvent information was found.

hqs_nmr.spectrumio._extract_solvent_magritek(data: dict[str, Any]) str[source]

Extracts solvent name from data dictionary from Magritek.

When reading a full directory, solvent should come from the acquisition parameters. If the FID data in a JCAMP-DX file has been loaded, solvent would try to be read from there if the first option fails or if it is the only available file in the directory.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

Solvent string(s) or an empty list if no solvent information was found.

hqs_nmr.spectrumio._extract_temperature_jcampdx(data: dict[str, Any]) list[float][source]

Extracts temperature (in Kelvin) from a JCAMP-DX data dictionary if available.

JCAMP-DX files store the temperature in degrees Celsius in the “##TEMPERATURE” field. Note that this function returns the temperature in Kelvin.

A few notes regarding the parsing via the nmrglue package: Since a JCAMP-DX file may contain more than one block (ends with “##END”), nmrglue collects all values of a key in a list.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

Temperature(s) in Kelvin or an empty list if no temperature was found.

hqs_nmr.spectrumio._extract_temperature_bruker(data: dict[str, Any]) float | None[source]

Extracts temperature (in Kelvin) from a Bruker data dictionary if available.

The Bruker directory format stores the temperature (in Kelvin) in the acquisition file (acqus or acqu) under the non-standard key “##$TE”. The data dictionary produced by nmrglue.bruker.read_pdata holds the temperature already as a float.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

Temperature in Kelvin or None if no solvent information was found.

hqs_nmr.spectrumio._extract_temperature_magritek(data: dict[str, Any]) float | None[source]

Extracts temperature from data dictionary from Magritek.

When reading a full directory, temperature should come from the acquisition parameters. Alternatively, if the FID data in a JCAMP-DX file has been loaded, temperature would try to be read from there. If the temperature come from the acquisition parameters, an average of the temperature of the benchtop box at the beginning (“StartProtocolTemperatureBox”) and end of the experiment (“CurrentTemperatureBox”) is taken.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

Temperature in Kelvin, if available, otherwise None.

hqs_nmr.spectrumio._extract_isotopes_jcampdx(data: dict[str, Any]) list[Isotope][source]

Extracts isotope from a JCAMP-DX data dictionary.

JCAMP-DX files store the isotope in the “##.OBSERVE NUCLEUS” field. The corresponding value usually contains a leading caret (e.g. ^13C), which is removed in this function.

A few notes regarding the parsing via the nmrglue package: Since a JCAMP-DX file may contain more than one block (ends with “##END”), nmrglue collects all values of a key in a list. Furthermore, nmrglue normalizes the key in the data dictionary, i.e. while the JCAMP-DX file may contain “##.OBSERVENUCLEUS” or “##.OBSERVE NUCLEUS”, the resulting data dictionary only contains a “.OBSERVENUCLEUS” key in both cases. NOTE: If both “.OBSERVENUCLEUS” and “.OBSERVE NUCLEUS” exist in the JCAMP-DX file, the nmrglue package unfortunately merges the two keys resulting in additional items in the isotope list. This function ensures that the isotope list has as many elements as the raw data for “.OBSERVEFREQUENCY”.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

List of isotopes.

hqs_nmr.spectrumio._extract_isotope_bruker(data: dict[str, Any]) Isotope[source]

Extracts isotope from a Bruker data dictionary.

The Bruker directory format stores the isotope in the processing file (procs or proc) “##$AXNUC”. The isotope string is usually enclosed in angle brackets (e.g. <1H>). Contrary to the solvent name, the isotope string stored in the data dictionary does not include the angle brackets. If the “##$AXNUC” key cannot be found in the procs file, it is attempted to extract the isotope from the acqus file instead (“##$NUC1” key).

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

Isotope of observed nucleus.

hqs_nmr.spectrumio._extract_isotope_magritek(data: dict[str, Any]) Isotope[source]

Extracts isotope from data dictionary from Magritek.

When reading a full directory, isotope should come from the acquisition parameters. If a JCAMP-DX file was loaded (with FID data), isotope would also try to be read from there.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Returns:

Isotope of observed nucleus.

hqs_nmr.spectrumio._extract_spectrometer_freq_magritek(data: dict[str, Any]) float[source]

Extracts spectrometer frequency from data dictionary from Magritek.

When reading a full directory, spectrometer frequency should come from the acquisition parameters. If a JCAMP-DX file was loaded (with FID data), it would also try to be read from there.

Parameters:

data – Data dictionary resulting from parsing with nmrglue.

Raises:

ValueError – Spectrometer frequency cannot be extracted.

Returns:

Spectrometer Frequency in MHz.

hqs_nmr.spectrumio._extract_xdata_jcampdx(jdx_content: str, npoints: int) list[ndarray][source]

Extracts x-data (chemical shift values) directly from a JCAMP-DX file.

Custom extraction of x-data for the (XY..XY) format, which allows no even spacing along the x-axis. Note that nmrglue does neither support the XYPOINTS data class, nor returning the x array when the (XY..XY) specifier is used as XYDATA.

NOTE: it is assumed that the JCAMP-DX file contains only one block of XYDATA data.

Parameters:
  • jdx_content – Content of the JCAMP-DX file.

  • npoints – Number of points in the spectrum.

Raises:

ValueError – Inconsistent JDX parameters.

Returns:

x-data with the real values of the (XY..XY) block.

hqs_nmr.spectrumio._fourier_transformation(data: dict[str, Any], ydata: ndarray) tuple[ndarray, ndarray][source]

Perform Fourier transformation of the NMR FID using nmrglue.

Parameters:
  • data – Data dictionary resulting from parsing with nmrglue.

  • ydata – NMR FID data.

hqs_nmr.spectrumio.read_jdx(file: Path | str, source: str = 'Unknown', license_id: str = 'Unknown') NMRExperimentalSpectrum1D[source]

Read spectral data from a JCAMP-DX (JDX) file.

NOTE: it is assumed that the JCAMP-DX file contains only one block, i.e. one spectrum and one isotope.

Parameters:
  • file – Path to the JDX file.

  • source – Owner or producer of the spectrum.

  • license_id – License associated with the spectrum (as SPDX identifier if possible).

Raises:
  • RuntimeError – Parsing failed.

  • NotImplementedError – Unsupported spectral data.

  • ValueError – Inconsistent JDX parameters.

Returns:

A NMRExperimentalSpectrum1D object with chemical shifts (in ppm) and intensities.

hqs_nmr.spectrumio.read_jdx_string(file_content: str, source: str = 'Unknown', license_id: str = 'Unknown') tuple[ndarray, ndarray][source]

Parses JDX spectral data from string.

Note this function creates a temporary file in order to match the function interface of nmrglue.jcampdx.read.

Parameters:
  • file_content – Content of a JCAMP-DX file.

  • source – Owner or producer of the spectrum.

  • license_id – License associated with the spectrum (as SPDX identifier if possible).

Returns:

A tuple of chemical shifts (in ppm) and intensities.

hqs_nmr.spectrumio.read_bruker_dir(directory: Path | str, source: str = 'Unknown', license_id: str = 'Unknown') NMRExperimentalSpectrum1D[source]

Read spectral data from Bruker directory.

Parameters:
  • directory – Path to the “pdata” subdirectory of the Bruker directory, for example bmse0001230/pdata/1.

  • source – Owner or producer of the spectrum.

  • license_id – License associated with the spectrum (as SPDX identifier if possible).

Returns:

A NMRExperimentalSpectrum1D object containing chemical shifts (in ppm) and intensities.

hqs_nmr.spectrumio.read_magritek_dir(directory: Path | str, dx_file: Path | str = 'nmr_fid.dx', source: str = 'Unknown', license_id: str = 'Unknown') NMRExperimentalSpectrum1D[source]

Read spectrum data from Magritek Spinsolve folder.

The function nmrglue.spinsolve.read can read the files “nmr_fid.dx”, “data.1d”, “fid.1d”, “spectrum.1d”, and “spectrum_processed.1d” in that order of priority to get the spectral data. In addition, it reads the parameter files such as “acqu.par” (acquisition parameters) and “proc.par” (processing parameters), if present.

The output from nmrglue.spinsolve.read needs to be interpreted. It is a tuple where the first element is a nested dictionary with relevant information about the experiment. The second element is an array with the spectral points (ydata). The content of the nested dictionary depends on the file from where the spectral data is imported. In this function, two spectrum files are considered (in the following order):

1) The “spectrum.1d” binary file containing data in the frequency domain (Fourier-transformed spectral data and a frequency axis, before any processing is applied). 2) The “nmr_fid.dx” file containing NMR FID data in a JCAMP-DX standard file format (Fourier transform is required).

Note that the default order of spectrum files is changed prioritizing “spectrum.1d” over “nmr_fid.dx” to potentially avoid an unnecessary Fourier transformation.

Depending on the spectrum file that is read, the parameters dictionary will have different content. If the “spectrum.1d” file is found, the “spectrum” dictionary will have the spectral data and if the JCAMP-DX file is read the “dx” dictionary will contain important information. When dealing with full folders, the acquisition parameters will be read from the “acqu.par” file, from which information related to the experiment will be retrieved. That is important in combination with the “spectrum.1d” binary file, since the “spectrum” dictionary only contains data points information (e.g., x-axis in ppm). When reading the JCAMP-DX file, the “dx” dictionary will contain the input to perform a Fourier transform, as well as other experimental details.

This function can also extract the spectrum from a single JCAMP-DX file as it contains the spectral data (FID) and the most relevant parameters. In this case, one has to specify the directory containing the file via directory. If different from the default (“nmr_fid.dx”), custom file names can be set via the dx_file argument.

Parameters:
  • directory – Folder name containing the Magritek spectrum data.

  • dx_file – Name of the JCAMP-DX file containing the NMR FID data (useful if only this file is available).

  • source – Owner or producer of the spectrum.

  • license_id – License associated with the spectrum (as SPDX identifier if possible).

Raises:

NotImplementedError – If no compatible spectrum file is found in the specified directory.

Returns:

A NMRExperimentalSpectrum1D object containing chemical shifts (in ppm) and intensities.

hqs_nmr.spectrumio.write_jdx(x_data: ndarray, y_data: ndarray, frequency_MHz: float, molecule_name: str, solvent: str = 'CDCl3', isotope: str = '1H', manual_shift: float | None = None) None[source]

Write a JCAMP-DX file.

This function writes a JCAMP-DX file from given x and y data. It is able of moving the x-axis by a specified amount. This is useful when the experimental spectrum is wrong referenced.

Parameters:
  • x_data – X data, shifts in ppm.

  • y_data – Y data, intensity.

  • frequency_MHz – Observation frequency in MHz (depends on the isotope).

  • molecule_name – Molecule name.

  • solvent – Solvent name. Defaults to “CDCl3”.

  • isotope – String representation of the isotope as [atomic mass number][symbol]. Defaults to “1H”.

  • manual_shift – Manual shift in ppm. Defaults to None.

hqs_nmr.spectrumio.bruker_dir_to_jdx(directory: Path | str, molecule_name: str) None[source]

Read spectral data from a Bruker directory and write it to a JCAMP-DX file.

Parameters:
  • directory – Path to the Bruker directory.

  • molecule_name – Molecule name.