forked from ScottSoren/EC_MS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMS.py
More file actions
55 lines (42 loc) · 1.34 KB
/
Copy pathMS.py
File metadata and controls
55 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 14 10:28:25 2020
@author: scott
"""
import os, re
data_directory = os.path.dirname(os.path.realpath(__file__)) + os.sep + 'data'
def get_NIST_spectrum(mol):
'''
a parser for NIST-exported .jdx files
'''
data_folder = data_directory + os.sep + 'NIST_spectra_data'
if type(mol) is not str:
try:
mol = mol.real_name
except AttributeError:
mol = mol.name
file_list = os.listdir(data_folder)
try:
file = next(f for f in file_list if re.search('^' + mol, f))
except StopIteration:
print('WARNING!!! No Spectrum available for ' + mol)
raise FileNotFoundError
#^ file-extension-ambiguous because I might forget and save them as .txt at some point
with open(data_folder + os.sep + file) as f:
lines = f.readlines()
in_data = False
spectrum = {}
for line in lines:
if 'END' in line:
break
if in_data:
#print(line) # debugging
mass_values = line.strip().split(' ')
for mass_value in mass_values:
mass, value = mass_value.split(',')
mass = 'M' + mass
value = eval(value)
spectrum[mass] = value
elif 'PEAK TABLE' in line:
in_data = True
return spectrum