From 27c1aa287892b41b45f70eb3404f508395fd391c Mon Sep 17 00:00:00 2001 From: Lan Le Date: Fri, 3 May 2024 15:04:39 +0200 Subject: [PATCH] fix: store molfile data into nmrium --- chem_spectra/controller/transform_api.py | 13 ++++- chem_spectra/lib/composer/ni.py | 25 ++++++++-- setup.py | 2 +- .../source/nmrium/nmrium_data_schema_v3.json | 3 +- .../source/nmrium/nmrium_data_schema_v4.json | 3 +- .../source/nmrium/nmrium_data_schema_v5.json | 3 +- tests/lib/composer/test_ni_composer.py | 47 +++++++++++++++++++ 7 files changed, 85 insertions(+), 11 deletions(-) diff --git a/chem_spectra/controller/transform_api.py b/chem_spectra/controller/transform_api.py index 2ae439c2..3df88b9a 100644 --- a/chem_spectra/controller/transform_api.py +++ b/chem_spectra/controller/transform_api.py @@ -14,6 +14,7 @@ from chem_spectra.model.transformer import TransformerModel as TraModel from chem_spectra.lib.converter.bagit.base import BagItBaseConverter +from chem_spectra.model.molecule import MoleculeModel trans_api = Blueprint('transform_api', __name__) @@ -33,7 +34,8 @@ def zip_jcamp_n_img(): molfile = FileContainer(request.files.get('molfile')) params = extract_params(request) if file: # and allowed_file(file): - cmpsr, invalid_molfile = TraModel(file, molfile=molfile, params=params).to_composer() + transform_model = TraModel(file, molfile=molfile, params=params) + cmpsr, invalid_molfile = transform_model.to_composer() if (not cmpsr): abort(403) @@ -79,7 +81,14 @@ def zip_jcamp_n_img(): ) rsp.headers['X-Extra-Info-JSON'] = json.dumps({'spc_type': spc_type, 'invalid_molfile': invalid_molfile}) else: - tf_jcamp, tf_img, tf_csv, tf_nmrium = cmpsr.tf_jcamp(), cmpsr.tf_img(), cmpsr.tf_csv(), cmpsr.generate_nmrium() + tf_jcamp, tf_img, tf_csv = cmpsr.tf_jcamp(), cmpsr.tf_img(), cmpsr.tf_csv() + tf_nmrium = None + try: + molecule_model = MoleculeModel(molfile, cmpsr.core.ncl, decorate=False) + tf_nmrium = cmpsr.generate_nmrium(molfile_data=molecule_model.moltxt) + except Exception: + pass + spc_type = cmpsr.core.ncl if cmpsr.core.typ == 'NMR' else cmpsr.core.typ if (tf_csv is not None and tf_csv != False): memory = to_zip_response([tf_jcamp, tf_img, tf_csv]) diff --git a/chem_spectra/lib/composer/ni.py b/chem_spectra/lib/composer/ni.py index 6d30bd0c..3ed13c7c 100644 --- a/chem_spectra/lib/composer/ni.py +++ b/chem_spectra/lib/composer/ni.py @@ -587,15 +587,15 @@ def tf_csv(self): }) return tf_csv - def generate_nmrium(self, version=4): + def generate_nmrium(self, version=4, molfile_data=None): typ = self.core.typ if 'NMR' != typ: return None if version == 3: - dic_data = self.__generate_nmrium_version_3() + dic_data = self.__generate_nmrium_version_3(molfile_data=molfile_data) else: - dic_data = self.__generate_nmrium_data(version) + dic_data = self.__generate_nmrium_data(version=version, molfile_data=molfile_data) json_data = json.dumps(dic_data) @@ -604,16 +604,18 @@ def generate_nmrium(self, version=4): tf_nmrium.seek(0) return tf_nmrium - def __generate_nmrium_version_3(self): + def __generate_nmrium_version_3(self, molfile_data=None): dic_data = {'actionType': 'INITIATE', 'version': 3} spectra = self.__generate_nmrim_spectra() dic_data['spectra'] = spectra + dic_data['molecules'] = self.__generate_molecules(molfile_data) return dic_data - def __generate_nmrium_data(self, version): + def __generate_nmrium_data(self, version, molfile_data=None): dic_data = {'version': version, 'data': {}, 'view': {}} spectra = self.__generate_nmrim_spectra() dic_data['data']['spectra'] = spectra + dic_data['data']['molecules'] = self.__generate_molecules(molfile_data) return dic_data def __generate_nmrim_spectra(self): @@ -793,3 +795,16 @@ def __generate_nmrim_ranges(self): dic_ranges['values'].append(rang_item) return dic_ranges + + def __generate_molecules(self, molfile_data): + if molfile_data is None: + return [] + + molecule_id = str(uuid.uuid4()) + return [ + { + 'id': molecule_id, + 'label': 'P1', + 'molfile': molfile_data + } + ] diff --git a/setup.py b/setup.py index d4d01c1e..7bc7e34e 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='chem-spectra-app', - version='1.1.1', + version='1.1.2', packages=find_packages(), include_package_data=True, zip_safe=False, diff --git a/tests/fixtures/source/nmrium/nmrium_data_schema_v3.json b/tests/fixtures/source/nmrium/nmrium_data_schema_v3.json index 8ca73fdf..49b01cb5 100644 --- a/tests/fixtures/source/nmrium/nmrium_data_schema_v3.json +++ b/tests/fixtures/source/nmrium/nmrium_data_schema_v3.json @@ -1,5 +1,6 @@ { "version": 3, "actionType": "", - "spectra": [] + "spectra": [], + "molecules": [] } \ No newline at end of file diff --git a/tests/fixtures/source/nmrium/nmrium_data_schema_v4.json b/tests/fixtures/source/nmrium/nmrium_data_schema_v4.json index 553df45b..2abdca57 100644 --- a/tests/fixtures/source/nmrium/nmrium_data_schema_v4.json +++ b/tests/fixtures/source/nmrium/nmrium_data_schema_v4.json @@ -1,7 +1,8 @@ { "version": 4, "data": { - "spectra": [] + "spectra": [], + "molecules": [] }, "view": {} } \ No newline at end of file diff --git a/tests/fixtures/source/nmrium/nmrium_data_schema_v5.json b/tests/fixtures/source/nmrium/nmrium_data_schema_v5.json index 9111c08c..d648aed0 100644 --- a/tests/fixtures/source/nmrium/nmrium_data_schema_v5.json +++ b/tests/fixtures/source/nmrium/nmrium_data_schema_v5.json @@ -1,7 +1,8 @@ { "version": 5, "data": { - "spectra": [] + "spectra": [], + "molecules": [] }, "view": {} } \ No newline at end of file diff --git a/tests/lib/composer/test_ni_composer.py b/tests/lib/composer/test_ni_composer.py index 44b9a4a2..8f6eeea0 100644 --- a/tests/lib/composer/test_ni_composer.py +++ b/tests/lib/composer/test_ni_composer.py @@ -4,10 +4,12 @@ from chem_spectra.lib.converter.jcamp.base import JcampBaseConverter from chem_spectra.lib.converter.jcamp.ni import JcampNIConverter from chem_spectra.lib.composer.ni import NIComposer +from chem_spectra.controller.helper.file_container import FileContainer source_nmr = './tests/fixtures/source/1H.dx' source_nmr_edit = './tests/fixtures/source/1H.edit.jdx' source_ir = './tests/fixtures/source/IR.dx' +source_dir_molfile = './tests/fixtures/source/molfile/c60h57fn4.mol' @pytest.fixture def data_schema(): @@ -29,6 +31,12 @@ def jcamp_file_1h_edit(): def jcamp_file_ir(): return source_ir +@pytest.fixture +def molfile_data(): + molfile = open(source_dir_molfile, "r") + molfile_str = molfile.read() + return molfile_str + def test_init_ni_composer_failed(): with pytest.raises(Exception) as error: _ = NIComposer(None) @@ -203,3 +211,42 @@ def test_ni_composer_generate_nmrium(jcamp_file_1h): y_value = xy_points['re'] assert len(x_values) == 65536 assert len(y_value) == 65536 + +def test_ni_composer_generate_nmrium_without_molfile(jcamp_file_1h): + base_converter = JcampBaseConverter(jcamp_file_1h) + ni_converter = JcampNIConverter(base=base_converter) + ni_composer = NIComposer(core=ni_converter) + nmrium_file = ni_composer.generate_nmrium() + + assert nmrium_file is not None + with open(nmrium_file.name) as file: + file_content = file.read() + assert file_content != "" + + dic_content = json.loads(file_content) + dic_data = dic_content['data'] + molecules = dic_data['molecules'] + assert molecules is not None + assert len(molecules) == 0 + +def test_ni_composer_generate_nmrium_with_molfile(jcamp_file_1h, molfile_data): + base_converter = JcampBaseConverter(jcamp_file_1h) + ni_converter = JcampNIConverter(base=base_converter) + ni_composer = NIComposer(core=ni_converter) + nmrium_file = ni_composer.generate_nmrium(molfile_data=molfile_data) + + assert nmrium_file is not None + with open(nmrium_file.name) as file: + file_content = file.read() + assert file_content != "" + + dic_content = json.loads(file_content) + dic_data = dic_content['data'] + molecules = dic_data['molecules'] + assert molecules is not None + assert len(molecules) == 1 + + molecule_data = molecules[0] + assert 'id' in molecule_data.keys() + assert molecule_data['label'] == 'P1' + assert molecule_data['molfile'] == molfile_data