Skip to content

Commit

Permalink
Map data types and spectra layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
f-idiris committed Oct 12, 2023
1 parent a12ced7 commit 594c57a
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 117 deletions.
11 changes: 11 additions & 0 deletions chem_spectra/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from flask import Flask
from flask_jwt_extended import JWTManager

import logging

Expand All @@ -23,6 +24,8 @@ def create_app(test_config=None):
except OSError:
pass

jwt = JWTManager(app)

#create logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -52,4 +55,12 @@ def ping():
from chem_spectra.controller.transform_api import trans_api
app.register_blueprint(trans_api)

# spectra type api
from chem_spectra.controller.spectra_type_api import spectra_type_api
app.register_blueprint(spectra_type_api)

# spectra layout api
from chem_spectra.controller.spectra_layout_api import spectra_layout_api
app.register_blueprint(spectra_layout_api)

return app
25 changes: 25 additions & 0 deletions chem_spectra/controller/spectra_layout_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json
from flask import jsonify, Blueprint
import os
import shutil
from flask_jwt_extended import jwt_required

spectra_layout_api = Blueprint('spectra_layout_api', __name__)
script_dir = os.path.dirname(__file__)
data_type_json_path = os.path.join(script_dir, '../lib/converter/jcamp/data_type.json')

def load_data_types():
try:
with open(data_type_json_path, 'r') as mapping_file:
return json.load(mapping_file)
except FileNotFoundError:
example_json_path = os.path.join(script_dir, '../lib/converter/jcamp/data_type.json.example')
shutil.copy(example_json_path, data_type_json_path)
with open(data_type_json_path, 'r') as mapping_file:
return json.load(mapping_file)

@spectra_layout_api.route('/api/v1/chemspectra/spectra_layouts', methods=['GET'])
@jwt_required()
def get_spectra_layouts():
existing_data_types = load_data_types()
return jsonify(existing_data_types["datatypes"]), 200
56 changes: 56 additions & 0 deletions chem_spectra/controller/spectra_type_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import json
import os
from flask import request, jsonify, Blueprint
import shutil
from flask_jwt_extended import jwt_required

spectra_type_api = Blueprint('spectra_type_api', __name__)

script_dir = os.path.dirname(__file__)
data_type_json_path = os.path.join(script_dir, '../lib/converter/jcamp/data_type.json')

def load_data_types():
try:
with open(data_type_json_path, 'r') as mapping_file:
return json.load(mapping_file)
except FileNotFoundError:
example_json_path = os.path.join(script_dir, '../lib/converter/jcamp/data_type.json.example')
shutil.copy(example_json_path, data_type_json_path)
with open(data_type_json_path, 'r') as mapping_file:
return json.load(mapping_file)

def save_data_types(data_types):
with open(data_type_json_path, 'w') as mapping_file:
json.dump(data_types, mapping_file, indent=4)

@spectra_type_api.route('/api/v1/chemspectra/data_type', methods=['POST'])
@jwt_required()
def create_data_type():
request_data = request.get_json()
new_data_type_mapping = request_data.get("new_data_type")
existing_data_types = load_data_types()

for layout, data_type in new_data_type_mapping.items():
if layout in existing_data_types["datatypes"] and data_type not in existing_data_types['datatypes'][layout]:
existing_data_types["datatypes"][layout].append(data_type)
elif layout in existing_data_types["datatypes"] and data_type in existing_data_types['datatypes'][layout]:
return jsonify({"message": f"Data type '{data_type}' already exists"}), 400
else:
return jsonify({"message": f"Layout '{layout}' does not exist"}), 400

save_data_types(existing_data_types)

return jsonify({"message": "Data type created successfully"}), 200

@spectra_type_api.route('/api/v1/chemspectra/data_type/<data_type>', methods=['DELETE'])
@jwt_required()
def delete_data_type(data_type):
existing_data_types = load_data_types()

for layout, data_types in existing_data_types['datatypes'].items():
if data_type in data_types:
data_types.remove(data_type)
save_data_types(existing_data_types)
return jsonify({"message": f"Data type '{data_type}' deleted successfully"}), 200

return jsonify({"message": f"Data type '{data_type}' not found"}), 404
114 changes: 34 additions & 80 deletions chem_spectra/lib/converter/jcamp/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import nmrglue as ng
import json

from chem_spectra.lib.converter.share import parse_params, parse_solvent
import os

data_type_json = os.path.join(os.path.dirname(__file__), 'data_type.json')


class JcampBaseConverter:
Expand Down Expand Up @@ -43,82 +47,34 @@ def __read(self, path):

def __set_datatype(self):
dts = self.datatypes
if 'NMR SPECTRUM' in dts:
return 'NMR SPECTRUM'
elif 'NMRSPECTRUM' in dts: # MNova
return 'NMR SPECTRUM'
elif 'INFRARED SPECTRUM' in dts:
return 'INFRARED SPECTRUM'
elif 'RAMAN SPECTRUM' in dts:
return 'RAMAN SPECTRUM'
elif 'MASS SPECTRUM' in dts:
return 'MASS SPECTRUM'
elif 'HPLC UV/VIS SPECTRUM' in dts:
return 'HPLC UV/VIS SPECTRUM'
elif 'HPLC UV-VIS' in dts:
return 'HPLC UV/VIS SPECTRUM'
elif 'UV/VIS SPECTRUM' in dts:
return 'UV/VIS SPECTRUM'
elif 'UV-VIS' in dts:
return 'UV/VIS SPECTRUM'
elif 'ULTRAVIOLET SPECTRUM' in dts:
return 'UV/VIS SPECTRUM'
elif 'THERMOGRAVIMETRIC ANALYSIS' in dts:
return 'THERMOGRAVIMETRIC ANALYSIS'
elif 'X-RAY DIFFRACTION' in dts:
return 'X-RAY DIFFRACTION'
elif 'CYCLIC VOLTAMMETRY' in dts:
return 'CYCLIC VOLTAMMETRY'
elif 'SIZE EXCLUSION CHROMATOGRAPHY' in dts:
return 'SIZE EXCLUSION CHROMATOGRAPHY'
elif 'CIRCULAR DICHROISM SPECTROSCOPY' in dts:
return 'CIRCULAR DICHROISM SPECTROSCOPY'
elif 'SORPTION-DESORPTION MEASUREMENT' in dts:
return 'SORPTION-DESORPTION MEASUREMENT'
elif 'Emissions' in dts or 'EMISSIONS' in dts or 'FLUORESCENCE SPECTRUM' in dts or 'FL SPECTRUM' in dts:
return 'Emissions'
elif 'DLS ACF' in dts:
return 'DLS ACF'
elif 'DLS INTENSITY' in dts or 'DLS intensity' in dts:
return 'DLS intensity'
dt_dict = {
'NMR': 'NMR SPECTRUM',
'INFRARED': 'INFRARED SPECTRUM',
'RAMAN': 'RAMAN SPECTRUM',
'MS': 'MASS SPECTRUM',
'HPLC UVVIS': 'HPLC UV/VIS SPECTRUM',
'UVVIS': 'UV/VIS SPECTRUM',
}

with open(data_type_json, 'r') as mapping_file:
data_type_mappings = json.load(mapping_file)["datatypes"]
for key, values in data_type_mappings.items():
for dt in dts:
if dt in values and key in dt_dict:
return dt_dict[key]
elif dt in values and not key in dt_dict:
return key
return ''

def __typ(self):
dt = self.datatype
if 'NMR SPECTRUM' == dt:
return 'NMR'
elif 'NMRSPECTRUM' == dt: # MNova
return 'NMR'
elif 'INFRARED SPECTRUM' == dt:
return 'INFRARED' # TBD
elif 'RAMAN SPECTRUM' == dt:
return 'RAMAN' # TBD
elif 'MASS SPECTRUM' == dt:
return 'MS'
elif 'HPLC UV/VIS SPECTRUM' == dt:
return 'HPLC UVVIS'
elif 'HPLC UV-VIS' == dt:
return 'HPLC UVVIS'
elif 'UV/VIS SPECTRUM' == dt or 'UV-VIS' == dt or 'ULTRAVIOLET SPECTRUM' == dt:
return 'UVVIS'
elif 'THERMOGRAVIMETRIC ANALYSIS' == dt:
return 'THERMOGRAVIMETRIC ANALYSIS'
elif 'X-RAY DIFFRACTION' == dt:
return 'X-RAY DIFFRACTION'
elif 'CYCLIC VOLTAMMETRY' in dt:
return 'CYCLIC VOLTAMMETRY'
elif 'SIZE EXCLUSION CHROMATOGRAPHY' in dt:
return 'SIZE EXCLUSION CHROMATOGRAPHY'
elif 'CIRCULAR DICHROISM SPECTROSCOPY' in dt:
return 'CIRCULAR DICHROISM SPECTROSCOPY'
elif 'SORPTION-DESORPTION MEASUREMENT' in dt:
return 'SORPTION-DESORPTION MEASUREMENT'
elif 'Emissions' in dt or 'EMISSIONS' in dt or 'FLUORESCENCE SPECTRUM' in dt or 'FL SPECTRUM' in dt:
return 'Emissions'
elif 'DLS ACF' in dt:
return 'DLS ACF'
elif 'DLS INTENSITY' in dt or 'DLS intensity' in dt:
return 'DLS intensity'

with open(data_type_json, 'r') as mapping_file:
data_type_mappings = json.load(mapping_file)["datatypes"]

for key, values in data_type_mappings.items():
if dt in values:
return key
return ''

def __set_dataclass(self):
Expand All @@ -140,12 +96,10 @@ def __is_em_wave(self):
return self.typ in ['INFRARED', 'RAMAN', 'UVVIS']

def __non_nmr(self):
return self.typ in [
'INFRARED', 'RAMAN', 'UVVIS', 'HPLC UVVIS',
'THERMOGRAVIMETRIC ANALYSIS', 'MS', 'X-RAY DIFFRACTION',
'CYCLIC VOLTAMMETRY', 'SIZE EXCLUSION CHROMATOGRAPHY',
'CIRCULAR DICHROISM SPECTROSCOPY', 'SORPTION-DESORPTION MEASUREMENT', 'Emissions',
'DLS ACF', 'DLS intensity']
with open(data_type_json, 'r') as mapping_file:
data_type_mappings = json.load(mapping_file).get("datatypes")
dts = [dt for dt in data_type_mappings.keys() if dt != 'NMR']
return self.typ in dts

def __is_ir(self):
return self.typ in ['INFRARED']
Expand Down Expand Up @@ -175,13 +129,13 @@ def __is_aif(self):
return self.typ in ['SORPTION-DESORPTION MEASUREMENT']

def __is_emissions(self):
return self.typ in ['Emissions', 'EMISSIONS', 'FLUORESCENCE SPECTRUM', 'FL SPECTRUM']
return self.typ in ['Emissions']

def __is_dls_acf(self):
return self.typ in ['DLS ACF']

def __is_dls_intensity(self):
return self.typ in ['DLS INTENSITY', 'DLS intensity']
return self.typ in ['DLS intensity']

def __ncl(self):
try:
Expand Down
19 changes: 19 additions & 0 deletions chem_spectra/lib/converter/jcamp/data_type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"datatypes": {
"NMR": ["NMR SPECTRUM", "NMRSPECTRUM"],
"INFRARED": ["INFRARED SPECTRUM"],
"RAMAN": ["RAMAN SPECTRUM"],
"MS": ["MASS SPECTRUM"],
"HPLC UVVIS": ["HPLC UV/VIS SPECTRUM", "HPLC UV-VIS"],
"UVVIS": ["UV/VIS SPECTRUM", "UV-VIS", "ULTRAVIOLET SPECTRUM"],
"THERMOGRAVIMETRIC ANALYSIS": ["THERMOGRAVIMETRIC ANALYSIS"],
"X-RAY DIFFRACTION": ["X-RAY DIFFRACTION"],
"CYCLIC VOLTAMMETRY": ["CYCLIC VOLTAMMETRY"],
"SIZE EXCLUSION CHROMATOGRAPHY": ["SIZE EXCLUSION CHROMATOGRAPHY"],
"CIRCULAR DICHROISM SPECTROSCOPY": ["CIRCULAR DICHROISM SPECTROSCOPY"],
"SORPTION-DESORPTION MEASUREMENT": ["SORPTION-DESORPTION MEASUREMENT"],
"Emissions": ["Emissions", "EMISSIONS", "FLUORESCENCE SPECTRUM", "FL SPECTRUM"],
"DLS ACF": ["DLS ACF"],
"DLS intensity": ["DLS INTENSITY", "DLS intensity"]
}
}
19 changes: 19 additions & 0 deletions chem_spectra/lib/converter/jcamp/data_type.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"datatypes": {
"NMR": ["NMR SPECTRUM", "NMRSPECTRUM"],
"INFRARED": ["INFRARED SPECTRUM"],
"RAMAN": ["RAMAN SPECTRUM"],
"MS": ["MASS SPECTRUM"],
"HPLC UVVIS": ["HPLC UV/VIS SPECTRUM", "HPLC UV-VIS"],
"UVVIS": ["UV/VIS SPECTRUM", "UV-VIS", "ULTRAVIOLET SPECTRUM"],
"THERMOGRAVIMETRIC ANALYSIS": ["THERMOGRAVIMETRIC ANALYSIS"],
"X-RAY DIFFRACTION": ["X-RAY DIFFRACTION"],
"CYCLIC VOLTAMMETRY": ["CYCLIC VOLTAMMETRY"],
"SIZE EXCLUSION CHROMATOGRAPHY": ["SIZE EXCLUSION CHROMATOGRAPHY"],
"CIRCULAR DICHROISM SPECTROSCOPY": ["CIRCULAR DICHROISM SPECTROSCOPY"],
"SORPTION-DESORPTION MEASUREMENT": ["SORPTION-DESORPTION MEASUREMENT"],
"Emissions": ["Emissions", "EMISSIONS", "FLUORESCENCE SPECTRUM", "FL SPECTRUM"],
"DLS ACF": ["DLS ACF"],
"DLS intensity": ["DLS INTENSITY", "DLS intensity"]
}
}
65 changes: 29 additions & 36 deletions chem_spectra/lib/converter/jcamp/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from chem_spectra.lib.converter.datatable import DatatableModel
from chem_spectra.lib.shared.calc import to_float
from chem_spectra.lib.converter.jcamp.data_parse import make_ni_data_ys, make_ni_data_xs

import json
import os

THRESHOLD_IR = 0.93
THRESHOLD_RAMAN = 0.07
Expand All @@ -14,7 +15,7 @@
THRESHOLD_TGA = 1.05
THRESHOLD_XRD = 1.00
THRESHOLD_EMISSION = 0.5

data_type_json = os.path.join(os.path.dirname(__file__), 'data_type.json')

class JcampNIConverter: # nmr & IR
def __init__(self, base):
Expand Down Expand Up @@ -74,42 +75,34 @@ def __init__(self, base):

def __thres(self):
dt = self.datatype
if 'NMR SPECTRUM' == dt:
return THRESHOLD_NMR
elif 'NMRSPECTRUM' == dt: # MNova
return THRESHOLD_NMR
elif 'INFRARED SPECTRUM' == dt:
return THRESHOLD_IR
elif 'RAMAN SPECTRUM' == dt:
return THRESHOLD_RAMAN
elif 'MASS SPECTRUM' == dt:
return THRESHOLD_MS
elif 'HPLC UV/VIS SPECTRUM' == dt:
return THRESHOLD_UVVIS
elif 'HPLC UV-VIS' == dt:
return THRESHOLD_UVVIS
elif dt in ['UV/VIS SPECTRUM', 'UV-VIS', 'ULTRAVIOLET SPECTRUM']:
return THRESHOLD_UVVIS
elif dt in ['THERMOGRAVIMETRIC ANALYSIS', 'DLS ACF']:
return THRESHOLD_TGA
elif dt in ['X-RAY DIFFRACTION', 'CIRCULAR DICHROISM SPECTROSCOPY', 'CYCLIC VOLTAMMETRY', 'SORPTION-DESORPTION MEASUREMENT',
'DLS INTENSITY', 'DLS intensity']:
return THRESHOLD_XRD
elif dt in ['Emissions', 'EMISSIONS', 'FLUORESCENCE SPECTRUM', 'FL SPECTRUM']:
return THRESHOLD_EMISSION
return 0.5
threshold_values = {
"NMR": THRESHOLD_NMR,
"INFRARED": THRESHOLD_IR,
"RAMAN": THRESHOLD_RAMAN,
"MS": THRESHOLD_MS,
"HPLC UVVIS": THRESHOLD_UVVIS,
"UVVIS": THRESHOLD_UVVIS,
"THERMOGRAVIMETRIC ANALYSIS": THRESHOLD_TGA,
"DLS ACF": THRESHOLD_TGA,
"X-RAY DIFFRACTION": THRESHOLD_XRD,
"CIRCULAR DICHROISM SPECTROSCOPY": THRESHOLD_XRD,
"CYCLIC VOLTAMMETRY": THRESHOLD_XRD,
"SORPTION-DESORPTION MEASUREMENT": THRESHOLD_XRD,
"DLS intensity": THRESHOLD_XRD,
"Emissions": THRESHOLD_EMISSION
}
with open(data_type_json, 'r') as mapping_file:
data_type_mappings = json.load(mapping_file)["datatypes"]
key = next((k for k, v in data_type_mappings.items() if dt in v), None)

return threshold_values.get(key, 0.5)


def __index_target(self):
target_topics = [
'NMR SPECTRUM', 'NMRSPECTRUM',
'INFRARED SPECTRUM', 'RAMAN SPECTRUM',
'MASS SPECTRUM', 'UV/VIS SPECTRUM', 'UV-VIS', 'ULTRAVIOLET SPECTRUM',
'HPLC UV-VIS', 'HPLC UV/VIS SPECTRUM',
'THERMOGRAVIMETRIC ANALYSIS', 'X-RAY DIFFRACTION',
'CYCLIC VOLTAMMETRY', 'SIZE EXCLUSION CHROMATOGRAPHY',
'CIRCULAR DICHROISM SPECTROSCOPY', 'SORPTION-DESORPTION MEASUREMENT',
'Emissions', 'EMISSIONS', 'FLUORESCENCE SPECTRUM', 'FL SPECTRUM', 'DLS ACF', 'DLS INTENSITY', 'DLS intensity'
]
with open(data_type_json, 'r') as mapping_file:
target = json.load(mapping_file).get("datatypes").values()
target_topics = [value for values in target for value in values]

for tp in target_topics:
if tp in self.datatypes:
idx = self.datatypes.index(tp)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ urllib3==1.26.5
Werkzeug==2.2.3
zipp==0.5.2
pyopenms==2.6.0
PyJWT==2.8.0
flask-jwt-extended==4.5.2
Loading

0 comments on commit 594c57a

Please sign in to comment.