Skip to content

Commit

Permalink
feat WIP: process hplc ms
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Sep 4, 2024
1 parent 9b66d5e commit f706c37
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 46 deletions.
40 changes: 6 additions & 34 deletions chem_spectra/controller/transform_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,40 +65,12 @@ def zip_jcamp_n_img():
rsp.headers['X-Extra-Info-JSON'] = json.dumps({'spc_type': 'bagit', 'invalid_molfile': invalid_molfile})
elif isinstance(cmpsr, LCMSComposer):
# check if composered model is hplc ms
# list_jcamps, list_images, list_csv, combined_image = cmpsr.data, cmpsr.images, cmpsr.list_csv, cmpsr.combined_image
# dst_list = []
# for idx in range(len(list_jcamps)):
# tf_jcamp = list_jcamps[idx]
# tf_img = list_images[idx]
# tf_csv = list_csv[idx]
# tf_arr = [tf_jcamp, tf_img, tf_csv]
# dst_list.append(tf_arr)

# if combined_image is not None:
# dst_list.append(combined_image)

# memory = to_zip_bag_it_response(dst_list)
# rsp = make_response(
# send_file(
# memory,
# download_name='spectrum.zip',
# as_attachment=True
# )
# )
# rsp.headers['X-Extra-Info-JSON'] = json.dumps({'spc_type': 'bagit', 'invalid_molfile': invalid_molfile})
list_jcamps = cmpsr.data
dst_list = [[list_jcamps[0]], [list_jcamps[1]], [list_jcamps[2]]]
print(dst_list)
# dst_list = []
# for idx in range(len(list_jcamps)):
# tf_jcamp = list_jcamps[idx]
# tf_img = list_images[idx]
# tf_csv = list_csv[idx]
# tf_arr = [tf_jcamp, tf_img, tf_csv]
# dst_list.append(tf_arr)

# if combined_image is not None:
# dst_list.append(combined_image)
dst_list = []
for idx in range(len(list_jcamps)):
tf_jcamp = list_jcamps[idx]
tf_arr = [tf_jcamp]
dst_list.append(tf_arr)

memory = to_zip_bag_it_response(dst_list)
rsp = make_response(
Expand All @@ -108,7 +80,7 @@ def zip_jcamp_n_img():
as_attachment=True
)
)
rsp.headers['X-Extra-Info-JSON'] = json.dumps({'spc_type': 'bagit', 'invalid_molfile': False})
rsp.headers['X-Extra-Info-JSON'] = json.dumps({'spc_type': 'hplc', 'invalid_molfile': invalid_molfile})
elif isinstance(cmpsr, collections.abc.Sequence):
dst_list = []
spc_type = ''
Expand Down
85 changes: 73 additions & 12 deletions chem_spectra/lib/composer/lcms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ def __compose(self):

uvvis = self.__gen_uvvis(data=uvvis_data)
uvvis_jcamp = self.tf_jcamp(uvvis)
return [tic_positive_jcamp, tic_negative_jcamp, uvvis_jcamp, spectra_postive_data, spectra_negative_data]

mz_positive = self.__gen_mz_spectra(data=spectra_postive_data)
mz_positive_jcamp = self.tf_jcamp(mz_positive)

mz_negative = self.__gen_mz_spectra(data=spectra_negative_data, is_negative=True)
mz_negative_jcamp = self.tf_jcamp(mz_negative)
return [tic_positive_jcamp, tic_negative_jcamp, uvvis_jcamp, mz_positive_jcamp, mz_negative_jcamp]

def __gen_tic(self, tic_data, is_negative = False):
time, intensity = tic_data['time'], tic_data['Intensity']
Expand Down Expand Up @@ -61,7 +67,7 @@ def __gen_tic(self, tic_data, is_negative = False):

return content

def __gen_uvvis(self, data, is_negative = False):
def __gen_uvvis(self, data):
content = [
'\n',
TEXT_SPECTRUM_ORIG,
Expand All @@ -73,7 +79,13 @@ def __gen_uvvis(self, data, is_negative = False):
'##OWNER=\n',
'##SPECTROMETER/DATA SYSTEM=\n',
'##$CSCATEGORY=UVVIS SPECTRUM\n',
'##UNITS= M/Z, RELATIVE ABUNDANCE\n',
'##VAR_NAME= RETENTION TIME, DETECTOR SIGNAL, WAVELENGTH\n',
'##SYMBOL= X, Y, T\n',
'##VAR_TYPE= INDEPENDENT, DEPENDENT, INDEPENDENT\n',
'##VAR_FORM= AFFN, AFFN, AFFN\n',
'##VAR_DIM= , , 3\n',
'##UNITS= RETENTION TIME, DETECTOR SIGNAL, WAVELENGTH\n',
'##FIRST= , , 1\n',
]

msspcs = []
Expand All @@ -83,7 +95,56 @@ def __gen_uvvis(self, data, is_negative = False):
msspc = [
'##PAGE={}\n'.format(time),
'##NPOINTS={}\n'.format(len(xs)),
'##DATA TABLE= (XY..XY)\n',
'##DATA TABLE= (XY..XY), PEAKS\n',
]
for idx, _ in enumerate(xs):
my_content = '{}, {};\n'.format(xs[idx], ys[idx])
msspc += my_content
file_content = ''.join(msspc)
ms_tempfile.write(file_content.encode('utf-8'))

ms_tempfile.seek(0)
lines = ms_tempfile.readlines()
decoded_lines = [line.decode('utf-8').strip() for line in lines]
msspcs = '\n'.join(decoded_lines)
ms_tempfile.close()

content.extend(msspcs)
content.extend(self.__gen_ending())

return content

def __gen_mz_spectra(self, data, is_negative=False):
category = '##$CSCATEGORY=MZ NEGATIVE SPECTRUM\n' if is_negative else '##$CSCATEGORY=MZ POSITIVE SPECTRUM\n'
content = [
'\n',
TEXT_SPECTRUM_ORIG,
'##TITLE={}\n'.format(self.title),
'##JCAMP-DX=5.00\n',
'##DATA TYPE={}\n'.format('LC/MS'),
'##DATA CLASS= NTUPLES\n',
'##ORIGIN=\n',
'##OWNER=\n',
'##SPECTROMETER/DATA SYSTEM=\n',
'##$CSCATEGORY=UVVIS SPECTRUM\n',
'##VAR_NAME= RETENTION TIME, DETECTOR SIGNAL, WAVELENGTH\n',
'##SYMBOL= X, Y, T\n',
'##VAR_TYPE= INDEPENDENT, DEPENDENT, INDEPENDENT\n',
'##VAR_FORM= AFFN, AFFN, AFFN\n',
'##VAR_DIM= , , 3\n',
'##UNITS= RETENTION TIME, DETECTOR SIGNAL, WAVELENGTH\n',
'##FIRST= , , 1\n',
category,
]

msspcs = []
ms_tempfile = tempfile.TemporaryFile()
for time, value in data.items():
xs, ys = value['mz'], value['intensities']
msspc = [
'##PAGE={}\n'.format(time),
'##NPOINTS={}\n'.format(len(xs)),
'##DATA TABLE= (XY..XY), PEAKS\n',
]
for idx, _ in enumerate(xs):
my_content = '{}, {};\n'.format(xs[idx], ys[idx])
Expand Down Expand Up @@ -151,14 +212,14 @@ def tf_jcamp(self, data):

# def __gen_config(self):
# return [
# '##VAR_NAME= MASS, INTENSITY, RETENTION TIME\n',
# '##SYMBOL= X, Y, T\n',
# '##VAR_TYPE= INDEPENDENT, DEPENDENT, INDEPENDENT\n',
# '##VAR_FORM= AFFN, AFFN, AFFN\n',
# '##VAR_DIM= , , 3\n',
# '##UNITS= M/Z, RELATIVE ABUNDANCE, SECONDS\n',
# '##FIRST= , , 1\n',
# # '##LAST= , , {}\n'.format(len(self.core.datatables)),
# '##VAR_NAME= MASS, INTENSITY, RETENTION TIME\n',
# '##SYMBOL= X, Y, T\n',
# '##VAR_TYPE= INDEPENDENT, DEPENDENT, INDEPENDENT\n',
# '##VAR_FORM= AFFN, AFFN, AFFN\n',
# '##VAR_DIM= , , 3\n',
# '##UNITS= M/Z, RELATIVE ABUNDANCE, SECONDS\n',
# '##FIRST= , , 1\n',
# # '##LAST= , , {}\n'.format(len(self.core.datatables)),
# ]

# def __gen_ms_spectra(self):
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/composer/test_lcms_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,33 @@ def test_lcms_composer_tic_uvvis(zip_file):
file_content = file.read()
assert file_content != ""
assert '##$CSCATEGORY=UVVIS SPECTRUM' in file_content

def test_lcms_composer_mz_spectra_positive(zip_file):
with tempfile.TemporaryDirectory() as td:
with zipfile.ZipFile(zip_file, 'r') as z:
z.extractall(td)

lcms_converter = LCMSBaseConverter(td)
lcms_composer = LCMSComposer(core=lcms_converter)

spectra_jcamp = lcms_composer.data[3]
assert spectra_jcamp is not None
with open(spectra_jcamp.name) as file:
file_content = file.read()
assert file_content != ""
assert '##$CSCATEGORY=MZ POSITIVE SPECTRUM' in file_content

def test_lcms_composer_mz_spectra_positive(zip_file):
with tempfile.TemporaryDirectory() as td:
with zipfile.ZipFile(zip_file, 'r') as z:
z.extractall(td)

lcms_converter = LCMSBaseConverter(td)
lcms_composer = LCMSComposer(core=lcms_converter)

spectra_jcamp = lcms_composer.data[4]
assert spectra_jcamp is not None
with open(spectra_jcamp.name) as file:
file_content = file.read()
assert file_content != ""
assert '##$CSCATEGORY=MZ NEGATIVE SPECTRUM' in file_content

0 comments on commit f706c37

Please sign in to comment.