corems.encapsulation.input.parameter_from_json

  1from pathlib import Path
  2import json
  3import toml
  4
  5from corems.encapsulation.factory.parameters import MSParameters, LCMSParameters
  6from corems.encapsulation.factory.processingSetting import (
  7    MolecularFormulaSearchSettings,
  8    TransientSetting,
  9)
 10from corems.encapsulation.factory.processingSetting import (
 11    MassSpectrumSetting,
 12    DataInputSetting,
 13)
 14from corems.encapsulation.factory.processingSetting import MassSpecPeakSetting
 15from corems.encapsulation.factory.processingSetting import GasChromatographSetting
 16from corems.encapsulation.factory.processingSetting import CompoundSearchSettings
 17
 18
 19def load_and_set_toml_parameters_ms(mass_spec_obj, parameters_path=False):
 20    """Load parameters from a toml file and set the parameters in the mass_spec_obj
 21
 22    Parameters
 23    ----------
 24    mass_spec_obj : MassSpectrum
 25        corems MassSpectrum object
 26
 27    parameters_path : str, optional
 28        path to the parameters file, by default False
 29
 30    Raises
 31    ------
 32    FileNotFoundError
 33        if the file is not found
 34    """
 35
 36    if parameters_path:
 37        file_path = Path(parameters_path)
 38
 39    else:
 40        filename = "SettingsCoreMS.toml"
 41        file_path = Path.cwd() / filename
 42
 43    if file_path.exists():
 44        with open(
 45            file_path,
 46            "r",
 47            encoding="utf8",
 48        ) as stream:
 49            data_loaded = toml.load(stream)
 50            _set_dict_data_ms(data_loaded, mass_spec_obj)
 51    else:
 52        raise FileNotFoundError("Could not locate %s", file_path)
 53
 54
 55def load_and_set_parameters_ms(mass_spec_obj, parameters_path=False):
 56    """Load parameters from a json file and set the parameters in the mass_spec_obj
 57
 58    Parameters
 59    ----------
 60    mass_spec_obj : MassSpectrum
 61        corems MassSpectrum object
 62    parameters_path : str, optional
 63        path to the parameters file, by default False
 64
 65    Raises
 66    ------
 67    FileNotFoundError
 68        if the file is not found
 69    """
 70
 71    if parameters_path:
 72        file_path = Path(parameters_path)
 73
 74    else:
 75        filename = "SettingsCoreMS.json"
 76        file_path = Path.cwd() / filename
 77
 78    if file_path.exists():
 79        with open(
 80            file_path,
 81            "r",
 82            encoding="utf8",
 83        ) as stream:
 84            data_loaded = json.load(stream)
 85            _set_dict_data_ms(data_loaded, mass_spec_obj)
 86    else:
 87        raise FileNotFoundError("Could not locate %s", file_path)
 88
 89
 90def load_and_set_toml_parameters_gcms(gcms_obj, parameters_path=False):
 91    """Load parameters from a toml file and set the parameters in the GCMS object
 92
 93    Parameters
 94    ----------
 95    gcms_obj : GCMSBase
 96        corems GCMSBase object
 97    parameters_path : str, optional
 98        path to the parameters file, by default False
 99
100    Raises
101    ------
102    FileNotFoundError
103        if the file is not found
104    """
105
106    if parameters_path:
107        file_path = Path(parameters_path)
108
109    else:
110        filename = "SettingsCoreMS.toml"
111        file_path = Path.cwd() / filename
112
113    if file_path.exists():
114        with open(
115            file_path,
116            "r",
117            encoding="utf8",
118        ) as stream:
119            data_loaded = toml.load(stream)
120            _set_dict_data_gcms(data_loaded, gcms_obj)
121    else:
122        raise FileNotFoundError("Could not locate %s", file_path)
123
124
125def load_and_set_parameters_gcms(gcms_obj, parameters_path=False):
126    """Load parameters from a json file and set the parameters in the GCMS object
127
128    Parameters
129    ----------
130    gcms_obj : GCMSBase
131        corems GCMSBase object
132    parameters_path : str, optional
133        path to the parameters file, by default False
134
135    Raises
136    ------
137    FileNotFoundError
138        if the file is not found
139    """
140
141    if parameters_path:
142        file_path = Path(parameters_path)
143
144    else:
145        filename = "SettingsCoreMS.json"
146        file_path = Path.cwd() / filename
147
148    if file_path.exists():
149        with open(
150            file_path,
151            "r",
152            encoding="utf8",
153        ) as stream:
154            data_loaded = json.load(stream)
155            _set_dict_data_gcms(data_loaded, gcms_obj)
156    else:
157        raise FileNotFoundError("Could not locate %s", file_path)
158
159
160def load_and_set_json_parameters_lcms(lcms_obj, parameters_path=False):
161    """Load parameters from a json file and set the parameters in the LCMS object
162
163    Parameters
164    ----------
165    lcms_obj : LCMSBase
166        corems LCMSBase object
167    parameters_path : str
168        path to the parameters file saved as a .json, by default False
169
170    Raises
171    ------
172    FileNotFoundError
173        if the file is not found
174    """
175
176    if parameters_path:
177        file_path = Path(parameters_path)
178
179    if file_path.exists():
180        with open(
181            file_path,
182            "r",
183            encoding="utf8",
184        ) as stream:
185            data_loaded = json.load(stream)
186            _set_dict_data_lcms(data_loaded, lcms_obj)
187    else:
188        raise FileNotFoundError("Could not locate %s", file_path)
189
190
191def load_and_set_toml_parameters_lcms(lcms_obj, parameters_path=False):
192    """Load parameters from a toml file and set the parameters in the LCMS object
193
194    Parameters
195    ----------
196    lcms_obj : LCMSBase
197        corems LCMSBase object
198    parameters_path : str
199        path to the parameters file saved as a .toml, by default False
200
201    Raises
202    ------
203    FileNotFoundError
204        if the file is not found
205    """
206
207    if parameters_path:
208        file_path = Path(parameters_path)
209
210    if file_path.exists():
211        with open(
212            file_path,
213            "r",
214            encoding="utf8",
215        ) as stream:
216            data_loaded = toml.load(stream)
217            _set_dict_data_lcms(data_loaded, lcms_obj)
218    else:
219        raise FileNotFoundError("Could not locate %s", file_path)
220
221
222def _set_dict_data_gcms(data_loaded, gcms_obj):
223    """Set the parameters in the GCMS object from a dict
224
225    This function is called by load_and_set_parameters_gcms and load_and_set_toml_parameters_gcms and should not be called directly.
226
227    Parameters
228    ----------
229    data_loaded : dict
230        dict with the parameters
231    gcms_obj : GCMSBase
232        corems GCMSBase object
233    """
234
235    classes = [
236        GasChromatographSetting(),
237        CompoundSearchSettings(),
238    ]
239
240    labels = ["GasChromatograph", "MolecularSearch"]
241
242    label_class = zip(labels, classes)
243
244    if data_loaded:
245        for label, classe in label_class:
246            class_data = data_loaded.get(label)
247            # not always we will not all the settings
248            # this allow a class data to be none and continue
249            # to import the other classes
250            if class_data:
251                for item, value in class_data.items():
252                    setattr(classe, item, value)
253
254    gcms_obj.chromatogram_settings = classes[0]
255    gcms_obj.molecular_search_settings = classes[1]
256
257
258def _set_dict_data_lcms(data_loaded, lcms_obj):
259    """Set the parameters on a LCMS object from a dict
260
261    This function is called by load_and_set_parameters_lcms and load_and_set_toml_parameters_lcms and should not be called directly.
262
263    Parameters
264    ----------
265    data_loaded : dict
266        dict with the parameters
267    lcms_obj : LCMSBase
268        corems LCMSBase object
269    """
270
271    # Load the lcms parameters
272    default_params = LCMSParameters(use_defaults=True)
273    lcms_params = data_loaded.get("LiquidChromatograph")
274    for item, value in lcms_params.items():
275        # If the original value is a tuple but the new one is a list we need to convert the list to a tuple
276        if isinstance(value, list) and isinstance(
277            getattr(default_params.lc_ms, item), tuple
278        ):
279            setattr(lcms_obj.parameters.lc_ms, item, tuple(value))
280        else:
281            setattr(lcms_obj.parameters.lc_ms, item, value)
282
283    def set_ms_params_by_key(ms_key):
284        classes = [
285            MassSpectrumSetting,
286            MassSpecPeakSetting,
287            MolecularFormulaSearchSettings,
288            DataInputSetting,
289            TransientSetting,
290        ]
291
292        labels = [
293            "mass_spectrum",
294            "ms_peak",
295            "molecular_search",
296            "data_input",
297            "transient",
298        ]
299
300        label_class = zip(labels, classes)
301
302        for label, classe in label_class:
303            class_data = data_loaded.get("mass_spectrum").get(ms_key).get(label)
304            param_instance = classe()
305            if class_data is not None:
306                # Set the attributes of the nested class
307                for item, value in class_data.items():
308                    if item == "usedAtoms":
309                        # Convert the lists to tuples
310                        for atom, atom_value in value.items():
311                            value[atom] = tuple(atom_value)
312                    if isinstance(value, list) and isinstance(
313                        getattr(param_instance, item), tuple
314                    ):
315                        setattr(param_instance, item, tuple(value))
316                    else:
317                        setattr(param_instance, item, value)
318            setattr(lcms_obj.parameters.mass_spectrum[ms_key], label, param_instance)
319
320    # Load the mass spectrum parameters
321    ms_keys = data_loaded["mass_spectrum"].keys()
322    for ms_key in ms_keys:
323        lcms_obj.parameters.mass_spectrum[ms_key] = MSParameters()
324        set_ms_params_by_key(ms_key)
325
326
327def _set_dict_data_ms(data_loaded, mass_spec_obj):
328    """Set the parameters in the MassSpectrum object from a dict
329
330    This function is called by load_and_set_parameters_ms and load_and_set_toml_parameters_ms and should not be called directly.
331
332    Parameters
333    ----------
334    data_loaded : dict
335        dict with the parameters
336    mass_spec_obj : MassSpectrum
337        corems MassSpectrum object
338    """
339
340    from copy import deepcopy
341
342    classes = [
343        MolecularFormulaSearchSettings(),
344        TransientSetting(),
345        MassSpectrumSetting(),
346        MassSpecPeakSetting(),
347    ]
348
349    labels = ["MolecularFormulaSearch", "Transient", "MassSpectrum", "MassSpecPeak"]
350
351    label_class = zip(labels, classes)
352
353    if data_loaded:
354        for label, classe in label_class:
355            class_data = data_loaded.get(label)
356            # not always we will have all the settings classes
357            # this allow a class data to be none and continue
358            # to import the other classes
359            if class_data:
360                for item, value in class_data.items():
361                    setattr(classe, item, value)
362
363    mass_spec_obj.molecular_search_settings = classes[0]
364    mass_spec_obj.transient_settings = classes[1]
365    mass_spec_obj.settings = classes[2]
366    mass_spec_obj.mspeaks_settings = classes[3]
367
368
369def load_and_set_toml_parameters_class(
370    parameter_label, instance_parameters_class, parameters_path=False
371):
372    """Load parameters from a toml file and set the parameters in the instance_parameters_class
373
374    Parameters
375    ----------
376    parameter_label : str
377        label of the parameters in the toml file
378    instance_parameters_class : object
379        instance of the parameters class
380    parameters_path : str, optional
381        path to the parameters file, by default False
382
383    Raises
384    ------
385    FileNotFoundError
386        if the file is not found
387
388    Returns
389    -------
390    object
391        instance of the parameters class
392    """
393
394    if parameters_path:
395        file_path = Path(parameters_path)
396
397    else:
398        file_path = Path.cwd() / "SettingsCoreMS.toml"
399
400    if file_path.exists():
401        with open(
402            file_path,
403            "r",
404            encoding="utf8",
405        ) as stream:
406            data_loaded = toml.load(stream)
407            parameter_class = _set_dict_data(
408                data_loaded, parameter_label, instance_parameters_class
409            )
410
411            return parameter_class
412    else:
413        raise FileNotFoundError("Could not locate %s", file_path)
414
415
416def load_and_set_parameters_class(
417    parameter_label, instance_parameters_class, parameters_path=False
418):
419    """Load parameters from a json file and set the parameters in the instance_parameters_class
420
421    Parameters
422    ----------
423    parameter_label : str
424        label of the parameters in the json file
425    instance_parameters_class : object
426        instance of the parameters class
427    parameters_path : str, optional
428        path to the parameters file, by default False
429
430    Raises
431    ------
432    FileNotFoundError
433        if the file is not found
434
435    Returns
436    -------
437    object
438        instance of the parameters class
439    """
440
441    if parameters_path:
442        file_path = Path(parameters_path)
443
444    else:
445        file_path = Path.cwd() / "SettingsCoreMS.json"
446
447    if file_path.exists():
448        with open(
449            file_path,
450            "r",
451            encoding="utf8",
452        ) as stream:
453            data_loaded = json.load(stream)
454            parameter_class = _set_dict_data(
455                data_loaded, parameter_label, instance_parameters_class
456            )
457
458            return parameter_class
459    else:
460        raise FileNotFoundError("Could not locate %s", file_path)
461
462
463def _set_dict_data(data_loaded, parameter_label, instance_ParameterClass):
464    """Set the parameters in an instance of a parameter class from a dict
465
466    This function is called by load_and_set_parameters_class and load_and_set_toml_parameters_class and should not be called directly.
467
468    Parameters
469    ----------
470    data_loaded : dict
471        dict with the parameters
472    parameter_label : str
473        label of the parameters in the json file
474    instance_ParameterClass : object
475        instance of the parameters class
476
477    Returns
478    -------
479    object
480        instance of the parameters class
481    """
482
483    classes = [instance_ParameterClass]
484
485    labels = [parameter_label]
486
487    label_class = zip(labels, classes)
488
489    if data_loaded:
490        for label, classe in label_class:
491            class_data = data_loaded.get(label)
492            # not always we will have all the settings classes
493            # this allow a class data to be none and continue
494            # to import the other classes
495            if class_data:
496                for item, value in class_data.items():
497                    setattr(classe, item, value)
498
499    return classes[0]
def load_and_set_toml_parameters_ms(mass_spec_obj, parameters_path=False):
20def load_and_set_toml_parameters_ms(mass_spec_obj, parameters_path=False):
21    """Load parameters from a toml file and set the parameters in the mass_spec_obj
22
23    Parameters
24    ----------
25    mass_spec_obj : MassSpectrum
26        corems MassSpectrum object
27
28    parameters_path : str, optional
29        path to the parameters file, by default False
30
31    Raises
32    ------
33    FileNotFoundError
34        if the file is not found
35    """
36
37    if parameters_path:
38        file_path = Path(parameters_path)
39
40    else:
41        filename = "SettingsCoreMS.toml"
42        file_path = Path.cwd() / filename
43
44    if file_path.exists():
45        with open(
46            file_path,
47            "r",
48            encoding="utf8",
49        ) as stream:
50            data_loaded = toml.load(stream)
51            _set_dict_data_ms(data_loaded, mass_spec_obj)
52    else:
53        raise FileNotFoundError("Could not locate %s", file_path)

Load parameters from a toml file and set the parameters in the mass_spec_obj

Parameters
  • mass_spec_obj (MassSpectrum): corems MassSpectrum object
  • parameters_path (str, optional): path to the parameters file, by default False
Raises
  • FileNotFoundError: if the file is not found
def load_and_set_parameters_ms(mass_spec_obj, parameters_path=False):
56def load_and_set_parameters_ms(mass_spec_obj, parameters_path=False):
57    """Load parameters from a json file and set the parameters in the mass_spec_obj
58
59    Parameters
60    ----------
61    mass_spec_obj : MassSpectrum
62        corems MassSpectrum object
63    parameters_path : str, optional
64        path to the parameters file, by default False
65
66    Raises
67    ------
68    FileNotFoundError
69        if the file is not found
70    """
71
72    if parameters_path:
73        file_path = Path(parameters_path)
74
75    else:
76        filename = "SettingsCoreMS.json"
77        file_path = Path.cwd() / filename
78
79    if file_path.exists():
80        with open(
81            file_path,
82            "r",
83            encoding="utf8",
84        ) as stream:
85            data_loaded = json.load(stream)
86            _set_dict_data_ms(data_loaded, mass_spec_obj)
87    else:
88        raise FileNotFoundError("Could not locate %s", file_path)

Load parameters from a json file and set the parameters in the mass_spec_obj

Parameters
  • mass_spec_obj (MassSpectrum): corems MassSpectrum object
  • parameters_path (str, optional): path to the parameters file, by default False
Raises
  • FileNotFoundError: if the file is not found
def load_and_set_toml_parameters_gcms(gcms_obj, parameters_path=False):
 91def load_and_set_toml_parameters_gcms(gcms_obj, parameters_path=False):
 92    """Load parameters from a toml file and set the parameters in the GCMS object
 93
 94    Parameters
 95    ----------
 96    gcms_obj : GCMSBase
 97        corems GCMSBase object
 98    parameters_path : str, optional
 99        path to the parameters file, by default False
100
101    Raises
102    ------
103    FileNotFoundError
104        if the file is not found
105    """
106
107    if parameters_path:
108        file_path = Path(parameters_path)
109
110    else:
111        filename = "SettingsCoreMS.toml"
112        file_path = Path.cwd() / filename
113
114    if file_path.exists():
115        with open(
116            file_path,
117            "r",
118            encoding="utf8",
119        ) as stream:
120            data_loaded = toml.load(stream)
121            _set_dict_data_gcms(data_loaded, gcms_obj)
122    else:
123        raise FileNotFoundError("Could not locate %s", file_path)

Load parameters from a toml file and set the parameters in the GCMS object

Parameters
  • gcms_obj (GCMSBase): corems GCMSBase object
  • parameters_path (str, optional): path to the parameters file, by default False
Raises
  • FileNotFoundError: if the file is not found
def load_and_set_parameters_gcms(gcms_obj, parameters_path=False):
126def load_and_set_parameters_gcms(gcms_obj, parameters_path=False):
127    """Load parameters from a json file and set the parameters in the GCMS object
128
129    Parameters
130    ----------
131    gcms_obj : GCMSBase
132        corems GCMSBase object
133    parameters_path : str, optional
134        path to the parameters file, by default False
135
136    Raises
137    ------
138    FileNotFoundError
139        if the file is not found
140    """
141
142    if parameters_path:
143        file_path = Path(parameters_path)
144
145    else:
146        filename = "SettingsCoreMS.json"
147        file_path = Path.cwd() / filename
148
149    if file_path.exists():
150        with open(
151            file_path,
152            "r",
153            encoding="utf8",
154        ) as stream:
155            data_loaded = json.load(stream)
156            _set_dict_data_gcms(data_loaded, gcms_obj)
157    else:
158        raise FileNotFoundError("Could not locate %s", file_path)

Load parameters from a json file and set the parameters in the GCMS object

Parameters
  • gcms_obj (GCMSBase): corems GCMSBase object
  • parameters_path (str, optional): path to the parameters file, by default False
Raises
  • FileNotFoundError: if the file is not found
def load_and_set_json_parameters_lcms(lcms_obj, parameters_path=False):
161def load_and_set_json_parameters_lcms(lcms_obj, parameters_path=False):
162    """Load parameters from a json file and set the parameters in the LCMS object
163
164    Parameters
165    ----------
166    lcms_obj : LCMSBase
167        corems LCMSBase object
168    parameters_path : str
169        path to the parameters file saved as a .json, by default False
170
171    Raises
172    ------
173    FileNotFoundError
174        if the file is not found
175    """
176
177    if parameters_path:
178        file_path = Path(parameters_path)
179
180    if file_path.exists():
181        with open(
182            file_path,
183            "r",
184            encoding="utf8",
185        ) as stream:
186            data_loaded = json.load(stream)
187            _set_dict_data_lcms(data_loaded, lcms_obj)
188    else:
189        raise FileNotFoundError("Could not locate %s", file_path)

Load parameters from a json file and set the parameters in the LCMS object

Parameters
  • lcms_obj (LCMSBase): corems LCMSBase object
  • parameters_path (str): path to the parameters file saved as a .json, by default False
Raises
  • FileNotFoundError: if the file is not found
def load_and_set_toml_parameters_lcms(lcms_obj, parameters_path=False):
192def load_and_set_toml_parameters_lcms(lcms_obj, parameters_path=False):
193    """Load parameters from a toml file and set the parameters in the LCMS object
194
195    Parameters
196    ----------
197    lcms_obj : LCMSBase
198        corems LCMSBase object
199    parameters_path : str
200        path to the parameters file saved as a .toml, by default False
201
202    Raises
203    ------
204    FileNotFoundError
205        if the file is not found
206    """
207
208    if parameters_path:
209        file_path = Path(parameters_path)
210
211    if file_path.exists():
212        with open(
213            file_path,
214            "r",
215            encoding="utf8",
216        ) as stream:
217            data_loaded = toml.load(stream)
218            _set_dict_data_lcms(data_loaded, lcms_obj)
219    else:
220        raise FileNotFoundError("Could not locate %s", file_path)

Load parameters from a toml file and set the parameters in the LCMS object

Parameters
  • lcms_obj (LCMSBase): corems LCMSBase object
  • parameters_path (str): path to the parameters file saved as a .toml, by default False
Raises
  • FileNotFoundError: if the file is not found
def load_and_set_toml_parameters_class(parameter_label, instance_parameters_class, parameters_path=False):
370def load_and_set_toml_parameters_class(
371    parameter_label, instance_parameters_class, parameters_path=False
372):
373    """Load parameters from a toml file and set the parameters in the instance_parameters_class
374
375    Parameters
376    ----------
377    parameter_label : str
378        label of the parameters in the toml file
379    instance_parameters_class : object
380        instance of the parameters class
381    parameters_path : str, optional
382        path to the parameters file, by default False
383
384    Raises
385    ------
386    FileNotFoundError
387        if the file is not found
388
389    Returns
390    -------
391    object
392        instance of the parameters class
393    """
394
395    if parameters_path:
396        file_path = Path(parameters_path)
397
398    else:
399        file_path = Path.cwd() / "SettingsCoreMS.toml"
400
401    if file_path.exists():
402        with open(
403            file_path,
404            "r",
405            encoding="utf8",
406        ) as stream:
407            data_loaded = toml.load(stream)
408            parameter_class = _set_dict_data(
409                data_loaded, parameter_label, instance_parameters_class
410            )
411
412            return parameter_class
413    else:
414        raise FileNotFoundError("Could not locate %s", file_path)

Load parameters from a toml file and set the parameters in the instance_parameters_class

Parameters
  • parameter_label (str): label of the parameters in the toml file
  • instance_parameters_class (object): instance of the parameters class
  • parameters_path (str, optional): path to the parameters file, by default False
Raises
  • FileNotFoundError: if the file is not found
Returns
  • object: instance of the parameters class
def load_and_set_parameters_class(parameter_label, instance_parameters_class, parameters_path=False):
417def load_and_set_parameters_class(
418    parameter_label, instance_parameters_class, parameters_path=False
419):
420    """Load parameters from a json file and set the parameters in the instance_parameters_class
421
422    Parameters
423    ----------
424    parameter_label : str
425        label of the parameters in the json file
426    instance_parameters_class : object
427        instance of the parameters class
428    parameters_path : str, optional
429        path to the parameters file, by default False
430
431    Raises
432    ------
433    FileNotFoundError
434        if the file is not found
435
436    Returns
437    -------
438    object
439        instance of the parameters class
440    """
441
442    if parameters_path:
443        file_path = Path(parameters_path)
444
445    else:
446        file_path = Path.cwd() / "SettingsCoreMS.json"
447
448    if file_path.exists():
449        with open(
450            file_path,
451            "r",
452            encoding="utf8",
453        ) as stream:
454            data_loaded = json.load(stream)
455            parameter_class = _set_dict_data(
456                data_loaded, parameter_label, instance_parameters_class
457            )
458
459            return parameter_class
460    else:
461        raise FileNotFoundError("Could not locate %s", file_path)

Load parameters from a json file and set the parameters in the instance_parameters_class

Parameters
  • parameter_label (str): label of the parameters in the json file
  • instance_parameters_class (object): instance of the parameters class
  • parameters_path (str, optional): path to the parameters file, by default False
Raises
  • FileNotFoundError: if the file is not found
Returns
  • object: instance of the parameters class