Source code for ccpn.AnalysisMetabolomics.lib.persistence

#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2021"
__credits__ = ("Ed Brooksbank, Joanna Fox, Victoria A Higman, Luca Mureddu, Eliza Płoskoń",
               "Timothy J Ragan, Brian O Smith, Gary S Thompson & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license")
__reference__ = ("Skinner, S.P., Fogh, R.H., Boucher, W., Ragan, T.J., Mureddu, L.G., & Vuister, G.W.",
                 "CcpNmr AnalysisAssign: a flexible platform for integrated NMR analysis",
                 "J.Biomol.Nmr (2016), 66, 111-124, http://doi.org/10.1007/s10858-016-0060-y")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: Ed Brooksbank $"
__dateModified__ = "$dateModified: 2021-06-30 09:45:21 +0100 (Wed, June 30, 2021) $"
__version__ = "$Revision: 3.0.4 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: TJ Ragan $"
__date__ = "$Date: 2017-04-07 10:28:45 +0000 (Fri, April 07, 2017) $"
#=========================================================================================
# Start of code
#=========================================================================================

import os
from collections import OrderedDict
import numpy as np
import pandas as pd
from ccpn.util.Path import aPath


FIELD = 400
SW_PPM = 14
CENTER = 4.7
POINTS = 2 ** 14

spectrum_x_ppm = np.linspace(CENTER + SW_PPM / 2, CENTER - SW_PPM / 2, POINTS)
spectrum_x_hz = spectrum_x_ppm * FIELD

procs = {'BYTORDP': 0,  # Byte order, little (0) or big (1) endian
         'NC_proc': 0,  # Data scaling factor, -3 means data were multiplied by 2**3, 4 means divided by 2**4
         'SI': POINTS,  # Size of processed data
         'XDIM': 0,  # Block size for 2D & 3D data
         'FTSIZE': POINTS,  # Size of FT output.  Same as SI except for strip plotting.
         'SW_p': SW_PPM * FIELD,  # Spectral width of processed data in Hz
         'SF': FIELD,  # Spectral reference position (center of spectrum)
         'OFFSET': SW_PPM / 2 + CENTER,  # ppm value of left-most point in spectrum
         'AXNUC': '<1H>',
         'LB': 0.3,  # Lorentzian broadening size (Hz)
         'GB': 0,  # Gaussian broadening factor
         'SSB': 0,  # Sine bell shift pi/ssb.  =1 for sine and =2 for cosine.  values <2 default to sine
         'WDW': 1,  # Window multiplication mode
         'TM1': 0,  # End of the rising edge of trapezoidal, takes a value from 0-1, must be less than TM2
         'TM2': 1,  # Beginings of the falling edge of trapezoidal, takes a value from 0-1, must be greater than TM1
         'BC_mod': 0  # Baseline correction mode (em, gm, sine, qsine, trap, user(?), sinc, qsinc, traf, trafs(JMR 71 1987, 237))
         }


[docs]class Borg: _shared_state = {} def __init__(self): self.__dict__ = self._shared_state
[docs]class MetabolomicsPersistenceDict(OrderedDict): class __Inner(OrderedDict): pass instance = None def __new__(cls, *args, **kwargs): if cls.instance is None: cls.instance = cls.__Inner(*args, **kwargs) return cls.instance
[docs]def bruker1dDict(refDF=None, SF=1, FTSIZE=None, SW_p=None, OFFSET=None): procs = {'BYTORDP': 0, # Byte order, little (0) or big (1) endian 'XDIM': 0, # Block size for 2D & 3D data 'SF': SF, # Spectral reference frequency (center of spectrum) 'FTSIZE': FTSIZE, # Size of FT output. Same as SI except for strip plotting. 'AXNUC': '<1H>', 'WDW': 1, # Window multiplication mode 'BC_mod': 0, # Baseline correction mode (em, gm, sine, qsine, trap, user(?), sinc, qsinc, traf, trafs(JMR 71 1987, 237)) 'LB': 0.0, # Lorentzian broadening size (Hz) 'GB': 0.0, # Gaussian broadening factor 'SSB': 0.0, # Sine bell shift pi/ssb. =1 for sine and =2 for cosine. values <2 default to sine 'TM1': 0.0, # End of the rising edge of trapezoidal, takes a value from 0-1, must be less than TM2 'TM2': 1.0, # Beginings of the falling edge of trapezoidal, takes a value from 0-1, must be greater than TM1 } procs['SW_p'] = SW_p # Spectral width of processed data in Hz procs['OFFSET'] = OFFSET # ppm value of left-most point in spectrum procs['SI'] = FTSIZE if refDF is not None: ppmMin, ppmMax = refDF.columns.min(), refDF.columns.max() swPpm = float(ppmMax) - float(ppmMin) procs['SW_p'] = swPpm * procs['SF'] procs['OFFSET'] = ppmMax procs['FTSIZE'] = len(refDF.columns) procs['SI'] = len(refDF.columns) return procs
[docs]def writeBruker(directory, dic, data): procDir = 'pdata/1' realFileName = '1r' _directory = aPath(directory) try: _directory.fetchDir(procDir) except FileExistsError: pass specMax2 = np.log2(data.max()) factor = int(29 - specMax2) data = data * 2 ** factor dic['NC_proc'] = -factor with open(_directory / procDir / 'procs', 'w') as f: for k in sorted(dic.keys()): f.write('##${}= {}\n'.format(k, dic[k])) with open(_directory / procDir / realFileName, 'wb') as f: f.write(data.astype('<i4').tobytes())
[docs]def spectraDicToBrukerExperiment(spectraDF, directoryName, **kwargs): if isinstance(spectraDF, dict): l = [pd.Series(spectraDF[name][1], index=spectraDF[name][0], name=name) for name in sorted(spectraDF.keys())] spectraDF = pd.concat(l, axis=1).T procs = bruker1dDict(spectraDF, SF=500) _directory = aPath(directoryName) for pcName in spectraDF.index: writeBruker(_directory / pcName, procs, spectraDF.ix[pcName].values)