Skip to content

Commit

Permalink
fix: store molfile data into nmrium
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed May 3, 2024
1 parent 469314e commit 5340a21
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 10 deletions.
13 changes: 11 additions & 2 deletions chem_spectra/controller/transform_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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)

Expand Down Expand Up @@ -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])
Expand Down
25 changes: 20 additions & 5 deletions chem_spectra/lib/composer/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down Expand Up @@ -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
}
]
3 changes: 2 additions & 1 deletion tests/fixtures/source/nmrium/nmrium_data_schema_v3.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"version": 3,
"actionType": "",
"spectra": []
"spectra": [],
"molecules": []
}
3 changes: 2 additions & 1 deletion tests/fixtures/source/nmrium/nmrium_data_schema_v4.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"version": 4,
"data": {
"spectra": []
"spectra": [],
"molecules": []
},
"view": {}
}
3 changes: 2 additions & 1 deletion tests/fixtures/source/nmrium/nmrium_data_schema_v5.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"version": 5,
"data": {
"spectra": []
"spectra": [],
"molecules": []
},
"view": {}
}
50 changes: 50 additions & 0 deletions tests/lib/composer/test_ni_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -29,6 +31,15 @@ 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
# molfile.close()
# molfile_data = FileContainer().from_str(molfile_str)
# return molfile_data

def test_init_ni_composer_failed():
with pytest.raises(Exception) as error:
_ = NIComposer(None)
Expand Down Expand Up @@ -203,3 +214,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

0 comments on commit 5340a21

Please sign in to comment.