rainbow-api 1.0.5__tar.gz → 1.0.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/PKG-INFO +1 -1
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/pyproject.toml +1 -1
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow/__init__.py +17 -9
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow/agilent/__init__.py +14 -6
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow/agilent/chemstation.py +97 -48
- rainbow_api-1.0.7/rainbow/waters/__init__.py +53 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow/waters/masslynx.py +69 -55
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow_api.egg-info/PKG-INFO +1 -1
- rainbow-api-1.0.5/rainbow/waters/__init__.py +0 -40
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/COPYING +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/README.md +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow/agilent/masshunter.py +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow/datadirectory.py +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow/datafile.py +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow_api.egg-info/SOURCES.txt +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow_api.egg-info/dependency_links.txt +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow_api.egg-info/requires.txt +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/rainbow_api.egg-info/top_level.txt +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/setup.cfg +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/tests/test_agilent.py +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/tests/test_datadirectory.py +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/tests/test_datafile.py +0 -0
- {rainbow-api-1.0.5 → rainbow_api-1.0.7}/tests/test_waters.py +0 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import os
|
|
1
|
+
import os
|
|
2
2
|
from rainbow.datafile import DataFile
|
|
3
3
|
from rainbow.datadirectory import DataDirectory
|
|
4
4
|
from rainbow import agilent, waters
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
def read(path, prec=0, hrms=False):
|
|
7
|
+
def read(path, prec=0, hrms=False, requested_files=None):
|
|
8
8
|
"""
|
|
9
9
|
Reads a chromatogram data directory. Main method of the package.
|
|
10
10
|
|
|
@@ -22,7 +22,8 @@ def read(path, prec=0, hrms=False):
|
|
|
22
22
|
Args:
|
|
23
23
|
path (str): Path of the directory.
|
|
24
24
|
prec (int, optional): Number of decimals to round ylabels.
|
|
25
|
-
hrms (bool, optional): Flag for Agilent HRMS parsing.
|
|
25
|
+
hrms (bool, optional): Flag for Agilent HRMS parsing.
|
|
26
|
+
requested_files (list, optional): List of filenames to parse.
|
|
26
27
|
|
|
27
28
|
Returns:
|
|
28
29
|
DataDirectory representing the directory.
|
|
@@ -32,22 +33,29 @@ def read(path, prec=0, hrms=False):
|
|
|
32
33
|
raise Exception(f"{path} is not a directory.")
|
|
33
34
|
|
|
34
35
|
if not isinstance(prec, int) or prec < 0:
|
|
35
|
-
raise Exception(f"Invalid precision: {prec}.")
|
|
36
|
+
raise Exception(f"Invalid precision: {prec}.")
|
|
36
37
|
|
|
37
38
|
if not isinstance(hrms, bool):
|
|
38
39
|
raise Exception(f"The hrms flag must be a boolean.")
|
|
39
40
|
|
|
41
|
+
if requested_files is not None and not isinstance(requested_files, list):
|
|
42
|
+
raise Exception(f"The requested_files argument must be a list.")
|
|
43
|
+
|
|
44
|
+
if requested_files:
|
|
45
|
+
requested_files = list(map(str.lower, requested_files))
|
|
46
|
+
|
|
40
47
|
datadir = None
|
|
41
48
|
ext = os.path.splitext(path)[1]
|
|
42
49
|
if ext.upper() == '.D':
|
|
43
|
-
datadir = agilent.read(path, prec, hrms)
|
|
50
|
+
datadir = agilent.read(path, prec, hrms, requested_files)
|
|
44
51
|
elif ext.lower() == '.raw':
|
|
45
|
-
datadir = waters.read(path, prec)
|
|
46
|
-
|
|
52
|
+
datadir = waters.read(path, prec, requested_files)
|
|
53
|
+
|
|
47
54
|
if datadir is None:
|
|
48
55
|
raise Exception(f"Rainbow cannot read {path}.")
|
|
49
56
|
return datadir
|
|
50
57
|
|
|
58
|
+
|
|
51
59
|
def read_metadata(path):
|
|
52
60
|
"""
|
|
53
61
|
Reads the metadata for a chromatogram data directory. Main method of the package.
|
|
@@ -56,7 +64,7 @@ def read_metadata(path):
|
|
|
56
64
|
path (str): Path of the directory.
|
|
57
65
|
|
|
58
66
|
Returns:
|
|
59
|
-
Dictionary
|
|
67
|
+
Dictionary containing a list of datafiles and the metadata.
|
|
60
68
|
|
|
61
69
|
"""
|
|
62
70
|
datadir = None
|
|
@@ -68,4 +76,4 @@ def read_metadata(path):
|
|
|
68
76
|
|
|
69
77
|
if metadata is None:
|
|
70
78
|
raise Exception(f"Rainbow cannot read {path}.")
|
|
71
|
-
return metadata
|
|
79
|
+
return metadata
|
|
@@ -1,22 +1,25 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
1
3
|
from rainbow.agilent import chemstation
|
|
2
4
|
from rainbow.datadirectory import DataDirectory
|
|
3
5
|
|
|
4
6
|
|
|
5
|
-
def read(path, prec=0, hrms=False):
|
|
7
|
+
def read(path, prec=0, hrms=False, requested_files=None):
|
|
6
8
|
"""
|
|
7
9
|
Reads an Agilent .D directory.
|
|
8
10
|
|
|
9
11
|
Args:
|
|
10
12
|
path (str): Path of the directory.
|
|
11
13
|
prec (int, optional): Number of decimals to round masses.
|
|
12
|
-
hrms (bool, optional): Flag for HRMS parsing.
|
|
14
|
+
hrms (bool, optional): Flag for HRMS parsing.
|
|
15
|
+
requested_files (list, optional): List of filenames to parse.
|
|
13
16
|
|
|
14
17
|
Returns:
|
|
15
18
|
DataDirectory representing the Agilent .D directory.
|
|
16
19
|
|
|
17
20
|
"""
|
|
18
21
|
datafiles = []
|
|
19
|
-
datafiles.extend(chemstation.parse_allfiles(path, prec))
|
|
22
|
+
datafiles.extend(chemstation.parse_allfiles(path, prec, requested_files))
|
|
20
23
|
if hrms:
|
|
21
24
|
try:
|
|
22
25
|
from rainbow.agilent import masshunter
|
|
@@ -28,6 +31,7 @@ def read(path, prec=0, hrms=False):
|
|
|
28
31
|
|
|
29
32
|
return DataDirectory(path, datafiles, metadata)
|
|
30
33
|
|
|
34
|
+
|
|
31
35
|
def read_metadata(path):
|
|
32
36
|
"""
|
|
33
37
|
Reads metadata from an Agilent .D directory.
|
|
@@ -36,12 +40,16 @@ def read_metadata(path):
|
|
|
36
40
|
path (str): Path of the directory.
|
|
37
41
|
|
|
38
42
|
Returns:
|
|
39
|
-
Dictionary
|
|
43
|
+
Dictionary containing a list of datafiles and the metadata.
|
|
40
44
|
|
|
41
45
|
"""
|
|
42
46
|
datafiles = []
|
|
43
47
|
metadata = chemstation.parse_metadata(path, datafiles)
|
|
44
48
|
if len(metadata) == 1:
|
|
45
49
|
datadir = read(path)
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
if datadir:
|
|
51
|
+
return {'datafiles': datadir.datafiles + datadir.analog, 'metadata': datadir.metadata}
|
|
52
|
+
return None
|
|
53
|
+
# Masshunter datafiles are not located.
|
|
54
|
+
datafiles = [fn for fn in os.listdir(path) if fn[-3:].lower() in ('.uv', '.ch', '.ms')]
|
|
55
|
+
return {'datafiles': datafiles, 'metadata': metadata}
|
|
@@ -15,7 +15,7 @@ MAIN PARSING METHODS
|
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
def parse_allfiles(path, prec=0):
|
|
18
|
+
def parse_allfiles(path, prec=0, requested_files=None):
|
|
19
19
|
"""
|
|
20
20
|
Finds and parses Agilent Chemstation data files \
|
|
21
21
|
with a .ch, .uv, or .ms extension from a .D directory.
|
|
@@ -23,6 +23,7 @@ def parse_allfiles(path, prec=0):
|
|
|
23
23
|
Args:
|
|
24
24
|
path (str): Path to the .D directory.
|
|
25
25
|
prec (int, optional): Number of decimals to round mz values.
|
|
26
|
+
requested_files (list, optional): List of filenames to parse.
|
|
26
27
|
|
|
27
28
|
Returns:
|
|
28
29
|
List with a DataFile for each parsed data file.
|
|
@@ -30,6 +31,8 @@ def parse_allfiles(path, prec=0):
|
|
|
30
31
|
"""
|
|
31
32
|
datafiles = []
|
|
32
33
|
for name in os.listdir(path):
|
|
34
|
+
if requested_files and name.lower() not in requested_files:
|
|
35
|
+
continue
|
|
33
36
|
datafile = parse_file(os.path.join(path, name), prec)
|
|
34
37
|
if datafile:
|
|
35
38
|
datafiles.append(datafile)
|
|
@@ -85,14 +88,14 @@ def parse_ch(path):
|
|
|
85
88
|
"""
|
|
86
89
|
with open(path, 'rb') as f:
|
|
87
90
|
head = read_string(f, offset=0, gap=1)
|
|
88
|
-
if head
|
|
89
|
-
return parse_ch_fid(path)
|
|
90
|
-
elif head
|
|
91
|
+
if head in ['179', '181']:
|
|
92
|
+
return parse_ch_fid(path, head)
|
|
93
|
+
elif head in ['130', '30']:
|
|
91
94
|
return parse_ch_other(path, head)
|
|
92
95
|
return None
|
|
93
96
|
|
|
94
97
|
|
|
95
|
-
def parse_ch_fid(path):
|
|
98
|
+
def parse_ch_fid(path, head):
|
|
96
99
|
"""
|
|
97
100
|
Parses an Agilent .ch file with FID channel data.
|
|
98
101
|
|
|
@@ -107,21 +110,43 @@ def parse_ch_fid(path):
|
|
|
107
110
|
DataFile with FID data, if the file can be parsed. Otherwise, None.
|
|
108
111
|
|
|
109
112
|
"""
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
if head == '181':
|
|
114
|
+
data_offsets = {
|
|
115
|
+
'num_times': 0x116,
|
|
116
|
+
'scaling_factor': 0x127C,
|
|
117
|
+
'data_start': 0x1800
|
|
118
|
+
}
|
|
119
|
+
metadata_offsets = {
|
|
120
|
+
'notebook': 0x35A,
|
|
121
|
+
'date': 0x957,
|
|
122
|
+
'method': 0xA0E,
|
|
123
|
+
'instrument': 0xC11,
|
|
124
|
+
'unit': 0x104C,
|
|
125
|
+
}
|
|
126
|
+
gap = 2
|
|
127
|
+
elif head == '179':
|
|
128
|
+
data_offsets = {
|
|
129
|
+
'num_times': 0x116,
|
|
130
|
+
'scaling_factor': 0x127C,
|
|
131
|
+
'data_start': 0x1800
|
|
132
|
+
}
|
|
133
|
+
metadata_offsets = {
|
|
134
|
+
'notebook': 0x35A,
|
|
135
|
+
'date': 0x957,
|
|
136
|
+
'method': 0xA0E,
|
|
137
|
+
'instrument': 0xC11,
|
|
138
|
+
'unit': 0x104C,
|
|
139
|
+
'signal': 0x1075
|
|
140
|
+
}
|
|
115
141
|
|
|
116
142
|
f = open(path, 'rb')
|
|
117
143
|
raw_bytes = f.read()
|
|
144
|
+
file_size = f.tell()
|
|
118
145
|
|
|
119
146
|
# Extract the number of retention times.
|
|
120
|
-
|
|
121
|
-
num_times = struct.unpack(">I", f.read(4))[0]
|
|
122
|
-
if num_times == 0:
|
|
123
|
-
return None
|
|
147
|
+
num_times = (file_size - data_offsets['data_start']) // 8
|
|
124
148
|
|
|
149
|
+
f.seek(data_offsets['num_times'] + 4)
|
|
125
150
|
# Compute retention times using the first and last times.
|
|
126
151
|
start_time = struct.unpack(">f", f.read(4))[0]
|
|
127
152
|
end_time = struct.unpack(">f", f.read(4))[0]
|
|
@@ -129,8 +154,10 @@ def parse_ch_fid(path):
|
|
|
129
154
|
times = np.arange(start_time, end_time + 1e-3, delta_time)
|
|
130
155
|
|
|
131
156
|
# Extract the raw data values.
|
|
132
|
-
|
|
133
|
-
|
|
157
|
+
if head == '181':
|
|
158
|
+
data = np.array(decode_double_delta(f, data_offsets['data_start']), dtype=np.float64)
|
|
159
|
+
else:
|
|
160
|
+
data = np.ndarray(num_times, '<d', raw_bytes, data_offsets['data_start'], 8)
|
|
134
161
|
data = data.copy().reshape(-1, 1)
|
|
135
162
|
|
|
136
163
|
# Convert times into minutes.
|
|
@@ -145,14 +172,6 @@ def parse_ch_fid(path):
|
|
|
145
172
|
ylabels = np.array([''])
|
|
146
173
|
|
|
147
174
|
# Extract metadata from file header.
|
|
148
|
-
metadata_offsets = {
|
|
149
|
-
'notebook': 0x35A,
|
|
150
|
-
'date': 0x957,
|
|
151
|
-
'method': 0xA0E,
|
|
152
|
-
'instrument': 0xC11,
|
|
153
|
-
'unit': 0x104C,
|
|
154
|
-
'signal': 0x1075
|
|
155
|
-
}
|
|
156
175
|
metadata = read_header(f, metadata_offsets)
|
|
157
176
|
f.close()
|
|
158
177
|
|
|
@@ -215,31 +234,10 @@ def parse_ch_other(path, head):
|
|
|
215
234
|
int_unpack = struct.Struct('>i').unpack
|
|
216
235
|
|
|
217
236
|
# Extract the raw data values.
|
|
218
|
-
# Count the total number of retention times.
|
|
219
|
-
f.seek(data_offsets['data_start'])
|
|
220
|
-
absorbances = []
|
|
221
|
-
absorb_accum = 0
|
|
222
|
-
while True:
|
|
223
|
-
# If the segment header is invalid, stop reading.
|
|
224
|
-
head = byte_unpack(f.read(1))[0]
|
|
225
|
-
if head != 0x10:
|
|
226
|
-
break
|
|
227
|
-
num_times_seg = byte_unpack(f.read(1))[0]
|
|
228
|
-
|
|
229
|
-
# If the next short is equal to -0x8000
|
|
230
|
-
# then the next absorbance value is the next integer.
|
|
231
|
-
# Otherwise, the short is a delta from the last absorbance value.
|
|
232
|
-
for _ in range(num_times_seg):
|
|
233
|
-
check_int = short_unpack(f.read(2))[0]
|
|
234
|
-
if check_int == -0x8000:
|
|
235
|
-
absorb_accum = int_unpack(f.read(4))[0]
|
|
236
|
-
else:
|
|
237
|
-
absorb_accum += check_int
|
|
238
|
-
absorbances.append(absorb_accum)
|
|
239
|
-
|
|
237
|
+
# Count the total number of retention times.
|
|
240
238
|
# Process the extracted values.
|
|
241
|
-
# If no values are extracted, this file is invalid.
|
|
242
|
-
data = np.array(
|
|
239
|
+
# If no values are extracted, this file is invalid.
|
|
240
|
+
data = np.array(decode_delta(f, data_offsets['data_start']))
|
|
243
241
|
num_times = data.size
|
|
244
242
|
if num_times == 0:
|
|
245
243
|
return None
|
|
@@ -276,6 +274,57 @@ def parse_ch_other(path, head):
|
|
|
276
274
|
|
|
277
275
|
return DataFile(path, detector, times, ylabels, data, metadata)
|
|
278
276
|
|
|
277
|
+
def decode_delta(f, offset):
|
|
278
|
+
byte_unpack = struct.Struct('>B').unpack
|
|
279
|
+
short_unpack = struct.Struct('>h').unpack
|
|
280
|
+
int_unpack = struct.Struct('>i').unpack
|
|
281
|
+
# Extract the raw data values.
|
|
282
|
+
# Count the total number of retention times.
|
|
283
|
+
f.seek(offset)
|
|
284
|
+
absorbances = []
|
|
285
|
+
absorb_accum = 0
|
|
286
|
+
while True:
|
|
287
|
+
# If the segment header is invalid, stop reading.
|
|
288
|
+
head = byte_unpack(f.read(1))[0]
|
|
289
|
+
if head != 0x10:
|
|
290
|
+
break
|
|
291
|
+
num_times_seg = byte_unpack(f.read(1))[0]
|
|
292
|
+
|
|
293
|
+
# If the next short is equal to -0x8000
|
|
294
|
+
# then the next absorbance value is the next integer.
|
|
295
|
+
# Otherwise, the short is a delta from the last absorbance value.
|
|
296
|
+
for _ in range(num_times_seg):
|
|
297
|
+
check_int = short_unpack(f.read(2))[0]
|
|
298
|
+
if check_int == -0x8000:
|
|
299
|
+
absorb_accum = int_unpack(f.read(4))[0]
|
|
300
|
+
else:
|
|
301
|
+
absorb_accum += check_int
|
|
302
|
+
absorbances.append(absorb_accum)
|
|
303
|
+
|
|
304
|
+
return absorbances
|
|
305
|
+
|
|
306
|
+
def decode_double_delta(f, offset):
|
|
307
|
+
byte_unpack = struct.Struct('>B').unpack
|
|
308
|
+
short_unpack = struct.Struct('>h').unpack
|
|
309
|
+
int_unpack = struct.Struct('>i').unpack
|
|
310
|
+
f.seek(0, 2)
|
|
311
|
+
file_size = f.tell()
|
|
312
|
+
f.seek(offset)
|
|
313
|
+
signals = []
|
|
314
|
+
count = 1
|
|
315
|
+
buffer = [0, 0, 0]
|
|
316
|
+
|
|
317
|
+
while f.tell() < file_size:
|
|
318
|
+
buffer[2] = short_unpack(f.read(2))[0]
|
|
319
|
+
if buffer[2] == 0x7fff:
|
|
320
|
+
buffer[0] = short_unpack(f.read(2))[0] << 32 | int_unpack(f.read(4))[0]
|
|
321
|
+
buffer[1] = 0
|
|
322
|
+
else:
|
|
323
|
+
buffer[1] += buffer[2]
|
|
324
|
+
buffer[0] += buffer[1]
|
|
325
|
+
signals.append(buffer[0])
|
|
326
|
+
|
|
327
|
+
return signals
|
|
279
328
|
|
|
280
329
|
"""
|
|
281
330
|
.uv PARSING METHODS
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
from rainbow.waters import masslynx
|
|
5
|
+
from rainbow.datadirectory import DataDirectory
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def read(path, prec=0, requested_files=None):
|
|
9
|
+
"""
|
|
10
|
+
Reads a Waters .raw directory.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
path (str): Path of the directory.
|
|
14
|
+
prec (int, optional): Number of decimals to round ylabels.
|
|
15
|
+
requested_files (list, optional): List of filenames to parse.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
DataDirectory representing the Waters .raw directory.
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
datafiles = []
|
|
22
|
+
datafiles.extend(masslynx.parse_spectrum(path, prec, requested_files))
|
|
23
|
+
datafiles.extend(masslynx.parse_analog(path, requested_files))
|
|
24
|
+
|
|
25
|
+
metadata = masslynx.parse_metadata(path)
|
|
26
|
+
|
|
27
|
+
return DataDirectory(path, datafiles, metadata)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def read_metadata(path):
|
|
31
|
+
"""
|
|
32
|
+
Reads metdata from a Waters .raw directory.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
path (str): Path of the directory.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
Dictionary containing a list of datafiles and the metadata.
|
|
39
|
+
"""
|
|
40
|
+
datafiles = []
|
|
41
|
+
metadata = masslynx.parse_metadata(path)
|
|
42
|
+
if len(metadata) == 1:
|
|
43
|
+
datadir = read(path)
|
|
44
|
+
if datadir:
|
|
45
|
+
return {'datafiles': datadir.datafiles + datadir.analog, 'metadata': metadata}
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
datafiles = [fn for fn in os.listdir(path) if re.match(r'^_FUNC\d{3}.DAT$', fn)]
|
|
49
|
+
if '_CHROMS.INF' in os.listdir(path):
|
|
50
|
+
analog_info = masslynx.parse_chroinf(os.path.join(path, '_CHROMS.INF'))
|
|
51
|
+
for i in range(len(analog_info)):
|
|
52
|
+
datafiles.append(f"_CHRO{i + 1:0>3}.DAT")
|
|
53
|
+
return {'datafiles': datafiles, 'metadata': metadata}
|
|
@@ -7,12 +7,13 @@ import re
|
|
|
7
7
|
import numpy as np
|
|
8
8
|
from rainbow.datafile import DataFile
|
|
9
9
|
|
|
10
|
-
|
|
11
10
|
"""
|
|
12
11
|
SPECTRUM PARSING METHODS
|
|
13
12
|
|
|
14
13
|
"""
|
|
15
|
-
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def parse_spectrum(path, prec=0, requested_files=None):
|
|
16
17
|
"""
|
|
17
18
|
Finds and parses Waters UV and MS spectra from a .raw directory.
|
|
18
19
|
|
|
@@ -22,7 +23,8 @@ def parse_spectrum(path, prec=0):
|
|
|
22
23
|
|
|
23
24
|
Args:
|
|
24
25
|
path (str): Path to the .raw directory.
|
|
25
|
-
prec (int, optional): Number of decimals to round ylabels.
|
|
26
|
+
prec (int, optional): Number of decimals to round ylabels.
|
|
27
|
+
requested_files (list, optional): List of filenames to parse.
|
|
26
28
|
|
|
27
29
|
Returns:
|
|
28
30
|
List with a DataFile for each parsed spectrum.
|
|
@@ -63,25 +65,23 @@ def parse_spectrum(path, prec=0):
|
|
|
63
65
|
|
|
64
66
|
# The raw spectrum data is stored in _FUNC .DAT files.
|
|
65
67
|
# Each MS spectrum has an assigned polarity,
|
|
66
|
-
# but may not have calibration values.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
funcdat_index
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
# but may not have calibration values.
|
|
69
|
+
funcdat_files = sorted(fn for fn in os.listdir(path) if re.match(r'^_FUNC\d{3}.DAT$', fn))
|
|
70
|
+
assert (os.path.getsize(os.path.join(path, "_FUNCTNS.INF")) == 32 * 13 * len(funcdat_files))
|
|
71
|
+
for funcdat_index, funcdat_file in enumerate(funcdat_files):
|
|
72
|
+
if requested_files and funcdat_file.lower() not in requested_files:
|
|
73
|
+
continue
|
|
72
74
|
polarity = None
|
|
73
75
|
calib = None
|
|
74
76
|
if funcdat_index < len(polarities):
|
|
75
77
|
polarity = polarities[funcdat_index]
|
|
76
78
|
if funcdat_index < len(calib_nums):
|
|
77
79
|
calib = calib_nums[funcdat_index]
|
|
78
|
-
datafile = parse_function(
|
|
79
|
-
funcdat_paths[funcdat_index], prec, polarity, calib)
|
|
80
|
+
datafile = parse_function(os.path.join(path, funcdat_file), prec, polarity, calib)
|
|
80
81
|
datafiles.append(datafile)
|
|
81
|
-
funcdat_index += 1
|
|
82
|
-
|
|
83
82
|
return datafiles
|
|
84
83
|
|
|
84
|
+
|
|
85
85
|
def parse_function(path, prec=0, polarity=None, calib=None):
|
|
86
86
|
"""
|
|
87
87
|
Parses data for a Waters function.
|
|
@@ -111,21 +111,22 @@ def parse_function(path, prec=0, polarity=None, calib=None):
|
|
|
111
111
|
raise Exception("The {bytes_per_pair}-bytes format is not supported.")
|
|
112
112
|
|
|
113
113
|
# Extract the ylabels and data values from the _FUNC .DAT file.
|
|
114
|
-
parse_funcdat = parse_funcdat2
|
|
114
|
+
parse_funcdat = parse_funcdat2
|
|
115
115
|
if bytes_per_pair == 6:
|
|
116
116
|
parse_funcdat = parse_funcdat6
|
|
117
117
|
elif bytes_per_pair == 8:
|
|
118
118
|
parse_funcdat = parse_funcdat8
|
|
119
119
|
elif bytes_per_pair == 4:
|
|
120
120
|
parse_funcdat = parse_funcdat4
|
|
121
|
-
ylabels, data = parse_funcdat(path, pair_counts, prec, calib)
|
|
122
|
-
|
|
121
|
+
ylabels, data = parse_funcdat(path, pair_counts, prec, calib)
|
|
122
|
+
|
|
123
123
|
# Spectra without an assigned polarity always contain UV data.
|
|
124
124
|
detector = 'MS' if polarity else 'UV'
|
|
125
125
|
metadata = {'polarity': polarity} if polarity else {}
|
|
126
126
|
|
|
127
127
|
return DataFile(path, detector, times, ylabels, data, metadata)
|
|
128
128
|
|
|
129
|
+
|
|
129
130
|
def parse_funcidx(path):
|
|
130
131
|
"""
|
|
131
132
|
Parses a Waters _FUNC .IDX file.
|
|
@@ -141,7 +142,7 @@ def parse_funcidx(path):
|
|
|
141
142
|
the file format of the corresponding _FUNC .DAT file.
|
|
142
143
|
|
|
143
144
|
"""
|
|
144
|
-
num_times = os.path.getsize(path) // 22
|
|
145
|
+
num_times = os.path.getsize(path) // 22
|
|
145
146
|
|
|
146
147
|
# Extract retention times and indexing info from the _FUNC .IDX file.
|
|
147
148
|
with open(path, 'rb') as f:
|
|
@@ -159,6 +160,7 @@ def parse_funcidx(path):
|
|
|
159
160
|
|
|
160
161
|
return times, pair_counts, bytes_per_pair
|
|
161
162
|
|
|
163
|
+
|
|
162
164
|
def parse_funcdat2(path, pair_counts, prec=0, calib=None):
|
|
163
165
|
"""
|
|
164
166
|
Parses a Waters _FUNC .DAT file with the 2-bytes format.
|
|
@@ -240,18 +242,18 @@ def parse_funcdat4(path, pair_counts, prec=0, calib=None):
|
|
|
240
242
|
# Calculate the `values` from each 4-byte segment.
|
|
241
243
|
num_datapairs = np.sum(pair_counts)
|
|
242
244
|
raw_values = np.ndarray(num_datapairs, '<I', raw_bytes)
|
|
243
|
-
|
|
244
|
-
val_powers = raw_values >> 22
|
|
245
|
+
|
|
246
|
+
val_powers = raw_values >> 22 # get the first 10 bits (drop 22 on 32)
|
|
245
247
|
val_separator = (raw_values >> 21) & 0x1 # get bits at position 11
|
|
246
|
-
val_bases = raw_values & 0x1FFFFF
|
|
247
|
-
val_bases_float = val_bases / 0x400
|
|
248
|
-
|
|
249
|
-
|
|
248
|
+
val_bases = raw_values & 0x1FFFFF # keep the last 21 bits (11-32)
|
|
249
|
+
val_bases_float = val_bases / 0x400 # out of the 21 bits, the 11 first are the integer part and the 10 last are the "decimal" part
|
|
250
|
+
# bit at position 12 being always '1' it means that the float values
|
|
251
|
+
# is always between 1024 &nd 2048 (factor of 2)
|
|
250
252
|
min_val_bases = np.min(val_bases[val_bases > 0])
|
|
251
253
|
max_val_bases = np.max(val_bases)
|
|
252
|
-
val_base_ampl = max_val_bases
|
|
254
|
+
val_base_ampl = max_val_bases - 0xFFFFF
|
|
253
255
|
|
|
254
|
-
values
|
|
256
|
+
values = val_bases_float * (2. ** np.subtract(val_powers, 10, dtype=np.int32))
|
|
255
257
|
|
|
256
258
|
# Note: We have not come across a sample with more than 1 mz value.
|
|
257
259
|
# This may need to be reshaped differently in the future.
|
|
@@ -283,6 +285,7 @@ def parse_funcinf(path):
|
|
|
283
285
|
mzs = np.ndarray((num_funcs, 32), "<f", raw_bytes, 160, (416, 4))
|
|
284
286
|
return mzs
|
|
285
287
|
|
|
288
|
+
|
|
286
289
|
def parse_funcdat6(path, pair_counts, prec=0, calib=None):
|
|
287
290
|
"""
|
|
288
291
|
Parses a Waters _FUNC .DAT file with the 6-bytes format.
|
|
@@ -324,7 +327,7 @@ def parse_funcdat6(path, pair_counts, prec=0, calib=None):
|
|
|
324
327
|
# If it is MS data, calibrate the masses.
|
|
325
328
|
if calib:
|
|
326
329
|
keys = calibrate(keys, calib)
|
|
327
|
-
|
|
330
|
+
|
|
328
331
|
# Then round the keys to the nearest whole number.
|
|
329
332
|
keys = np.round(keys, prec)
|
|
330
333
|
|
|
@@ -345,14 +348,15 @@ def parse_funcdat6(path, pair_counts, prec=0, calib=None):
|
|
|
345
348
|
for i in range(num_times):
|
|
346
349
|
stop_index = cur_index + pair_counts[i]
|
|
347
350
|
np.add.at(
|
|
348
|
-
data[i],
|
|
349
|
-
key_indices[cur_index:stop_index],
|
|
351
|
+
data[i],
|
|
352
|
+
key_indices[cur_index:stop_index],
|
|
350
353
|
values[cur_index:stop_index])
|
|
351
354
|
cur_index = stop_index
|
|
352
355
|
del key_indices, keys, values, pair_counts
|
|
353
356
|
|
|
354
357
|
return ylabels, data
|
|
355
358
|
|
|
359
|
+
|
|
356
360
|
def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
357
361
|
"""
|
|
358
362
|
Parses a Waters _FUNC .DAT file with the 8-bytes format.
|
|
@@ -384,18 +388,18 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
384
388
|
# The data is stored as key-value pairs.
|
|
385
389
|
# For example, in MS data these are mz-intensity pairs.
|
|
386
390
|
# Split each segment into `key_bits` and `val_bits`.
|
|
387
|
-
key_bits = raw_values >> 28
|
|
391
|
+
key_bits = raw_values >> 28
|
|
388
392
|
val_bits = raw_values & 0xFFFFFFF
|
|
389
393
|
del raw_values, raw_bytes
|
|
390
394
|
|
|
391
395
|
# Split `key_bits` into integer and fractional components.
|
|
392
|
-
num_keyint_bits = key_bits >> 31
|
|
396
|
+
num_keyint_bits = key_bits >> 31
|
|
393
397
|
keyint_masks = pow(2, num_keyint_bits) - 1
|
|
394
|
-
num_keyfrac_bits = 31 - num_keyint_bits
|
|
398
|
+
num_keyfrac_bits = 31 - num_keyint_bits
|
|
395
399
|
keyfrac_masks = pow(2, num_keyfrac_bits) - 1
|
|
396
|
-
keyints = (key_bits >> num_keyfrac_bits) & keyint_masks
|
|
400
|
+
keyints = (key_bits >> num_keyfrac_bits) & keyint_masks
|
|
397
401
|
keyfracs = calc_frac(key_bits & keyfrac_masks, num_keyfrac_bits)
|
|
398
|
-
del num_keyint_bits, num_keyfrac_bits, key_bits
|
|
402
|
+
del num_keyint_bits, num_keyfrac_bits, key_bits
|
|
399
403
|
del keyint_masks, keyfrac_masks
|
|
400
404
|
|
|
401
405
|
# Get the `keys` by adding the components.
|
|
@@ -403,7 +407,7 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
403
407
|
keys = keyints + keyfracs
|
|
404
408
|
if calib:
|
|
405
409
|
keys = calibrate(keys, calib)
|
|
406
|
-
del keyints, keyfracs
|
|
410
|
+
del keyints, keyfracs
|
|
407
411
|
|
|
408
412
|
# Round the keys to the nearest whole number.
|
|
409
413
|
keys = np.round(keys, prec)
|
|
@@ -412,14 +416,14 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
412
416
|
# This is based on the number of bits allocated for each integer.
|
|
413
417
|
num_valint_bits = val_bits >> 22
|
|
414
418
|
num_shifted = np.zeros(num_datapairs, np.uint8)
|
|
415
|
-
shift_cond = num_valint_bits > 21
|
|
416
|
-
num_shifted[shift_cond] = num_valint_bits[shift_cond] - 21
|
|
417
|
-
num_valint_bits[shift_cond] = 21
|
|
419
|
+
shift_cond = num_valint_bits > 21
|
|
420
|
+
num_shifted[shift_cond] = num_valint_bits[shift_cond] - 21
|
|
421
|
+
num_valint_bits[shift_cond] = 21
|
|
418
422
|
del shift_cond
|
|
419
423
|
|
|
420
424
|
# Split `val_bits` into integer and fractional components.
|
|
421
425
|
valint_masks = pow(2, num_valint_bits) - 1
|
|
422
|
-
num_valfrac_bits = 21 - num_valint_bits
|
|
426
|
+
num_valfrac_bits = 21 - num_valint_bits
|
|
423
427
|
valfrac_masks = pow(2, num_valfrac_bits) - 1
|
|
424
428
|
valints = ((val_bits >> num_valfrac_bits) & valint_masks) << num_shifted
|
|
425
429
|
valfracs = calc_frac(val_bits & valfrac_masks, num_valfrac_bits)
|
|
@@ -429,7 +433,7 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
429
433
|
# Get the `values` by adding the components.
|
|
430
434
|
values = valints + valfracs
|
|
431
435
|
del valints, valfracs
|
|
432
|
-
|
|
436
|
+
|
|
433
437
|
# Make the array of `ylabels` with keys.
|
|
434
438
|
ylabels = np.unique(keys)
|
|
435
439
|
ylabels.sort()
|
|
@@ -441,14 +445,15 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
441
445
|
for i in range(num_times):
|
|
442
446
|
stop_index = cur_index + pair_counts[i]
|
|
443
447
|
np.add.at(
|
|
444
|
-
data[i],
|
|
445
|
-
key_indices[cur_index:stop_index],
|
|
448
|
+
data[i],
|
|
449
|
+
key_indices[cur_index:stop_index],
|
|
446
450
|
values[cur_index:stop_index])
|
|
447
451
|
cur_index = stop_index
|
|
448
452
|
del key_indices, keys, values, pair_counts
|
|
449
453
|
|
|
450
454
|
return ylabels, data
|
|
451
455
|
|
|
456
|
+
|
|
452
457
|
def calibrate(mzs, calib_nums):
|
|
453
458
|
"""
|
|
454
459
|
Calibrates :obj:`mzs` using :obj:`calib_nums`.
|
|
@@ -469,9 +474,10 @@ def calibrate(mzs, calib_nums):
|
|
|
469
474
|
for coeff in calib_nums:
|
|
470
475
|
calib_mzs += coeff * var
|
|
471
476
|
var *= mzs
|
|
472
|
-
del var
|
|
477
|
+
del var
|
|
473
478
|
return calib_mzs
|
|
474
479
|
|
|
480
|
+
|
|
475
481
|
def calc_frac(keyfrac_bits, num_bits):
|
|
476
482
|
"""
|
|
477
483
|
Decodes fractional values from :obj:`keyfrac_bits`.
|
|
@@ -491,7 +497,7 @@ def calc_frac(keyfrac_bits, num_bits):
|
|
|
491
497
|
1D numpy array with fractional values.
|
|
492
498
|
|
|
493
499
|
"""
|
|
494
|
-
exponent = np.uint64(0x3FF << 52)
|
|
500
|
+
exponent = np.uint64(0x3FF << 52)
|
|
495
501
|
num_shifted = 52 - num_bits
|
|
496
502
|
base = keyfrac_bits << num_shifted
|
|
497
503
|
fracs = (exponent | base).view(np.float64)
|
|
@@ -504,12 +510,15 @@ def calc_frac(keyfrac_bits, num_bits):
|
|
|
504
510
|
ANALOG PARSING METHODS
|
|
505
511
|
|
|
506
512
|
"""
|
|
507
|
-
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def parse_analog(path, requested_files=None):
|
|
508
516
|
"""
|
|
509
517
|
Finds and parses analog data from a Waters .raw directory.
|
|
510
518
|
|
|
511
519
|
Args:
|
|
512
|
-
path (str): Path to the .raw directory.
|
|
520
|
+
path (str): Path to the .raw directory.
|
|
521
|
+
requested_files (list, optional): List of filenames to parse.
|
|
513
522
|
|
|
514
523
|
Returns:
|
|
515
524
|
List of DataFiles that contain analog data.
|
|
@@ -518,16 +527,19 @@ def parse_analog(path):
|
|
|
518
527
|
datafiles = []
|
|
519
528
|
|
|
520
529
|
if '_CHROMS.INF' not in os.listdir(path):
|
|
521
|
-
return datafiles
|
|
530
|
+
return datafiles
|
|
522
531
|
|
|
523
532
|
analog_info = parse_chroinf(os.path.join(path, '_CHROMS.INF'))
|
|
524
533
|
for i in range(len(analog_info)):
|
|
525
|
-
fn =
|
|
526
|
-
|
|
534
|
+
fn = f"_CHRO{i + 1:0>3}.DAT"
|
|
535
|
+
if requested_files and fn.lower() not in requested_files:
|
|
536
|
+
continue
|
|
537
|
+
datafile = parse_chrodat(os.path.join(path, fn), *analog_info[i])
|
|
527
538
|
if datafile:
|
|
528
539
|
datafiles.append(datafile)
|
|
529
540
|
return datafiles
|
|
530
541
|
|
|
542
|
+
|
|
531
543
|
def parse_chroinf(path):
|
|
532
544
|
"""
|
|
533
545
|
Parses a Waters _CHROMS.INF file.
|
|
@@ -543,19 +555,20 @@ def parse_chroinf(path):
|
|
|
543
555
|
|
|
544
556
|
"""
|
|
545
557
|
f = open(path, 'r')
|
|
546
|
-
f.seek(0x84)
|
|
558
|
+
f.seek(0x84) # start offset
|
|
547
559
|
analog_info = []
|
|
548
560
|
while f.tell() < os.path.getsize(path):
|
|
549
561
|
line = re.sub(r'[\0-\x04]|\$CC\$|\([0-9]*\)', '', f.read(0x55)).strip()
|
|
550
562
|
split = line.split(',')
|
|
551
563
|
info = []
|
|
552
|
-
info.append(split[0])
|
|
564
|
+
info.append(split[0]) # name
|
|
553
565
|
if len(split) == 6:
|
|
554
|
-
info.append(split[5])
|
|
566
|
+
info.append(split[5]) # unit
|
|
555
567
|
analog_info.append(info)
|
|
556
568
|
f.close()
|
|
557
569
|
return analog_info
|
|
558
570
|
|
|
571
|
+
|
|
559
572
|
def parse_chrodat(path, name, units=None):
|
|
560
573
|
"""
|
|
561
574
|
Parses a Waters _CHRO .DAT file.
|
|
@@ -587,7 +600,7 @@ def parse_chrodat(path, name, units=None):
|
|
|
587
600
|
with open(path, 'rb') as f:
|
|
588
601
|
raw_bytes = f.read()
|
|
589
602
|
times_immut = np.ndarray(num_times, '<f', raw_bytes, data_start, 8)
|
|
590
|
-
vals_immut = np.ndarray(num_times, '<f', raw_bytes, data_start+4, 8)
|
|
603
|
+
vals_immut = np.ndarray(num_times, '<f', raw_bytes, data_start + 4, 8)
|
|
591
604
|
|
|
592
605
|
# The arrays are copied so that they are mutable.
|
|
593
606
|
# This is just for user convenience.
|
|
@@ -606,8 +619,8 @@ def parse_chrodat(path, name, units=None):
|
|
|
606
619
|
|
|
607
620
|
ylabels = np.array([''])
|
|
608
621
|
metadata = {'signal': name}
|
|
609
|
-
if units:
|
|
610
|
-
metadata['unit'] = units
|
|
622
|
+
if units:
|
|
623
|
+
metadata['unit'] = units
|
|
611
624
|
|
|
612
625
|
return DataFile(path, detector, times, ylabels, vals, metadata)
|
|
613
626
|
|
|
@@ -617,6 +630,7 @@ METADATA PARSING METHOD
|
|
|
617
630
|
|
|
618
631
|
"""
|
|
619
632
|
|
|
633
|
+
|
|
620
634
|
def parse_metadata(path):
|
|
621
635
|
"""
|
|
622
636
|
Parses metadata from a Waters .raw directory.
|
|
@@ -649,4 +663,4 @@ def parse_metadata(path):
|
|
|
649
663
|
value = line.split(': ')[1]
|
|
650
664
|
if not value.isspace():
|
|
651
665
|
metadata['vialpos'] = value
|
|
652
|
-
return metadata
|
|
666
|
+
return metadata
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
from rainbow.waters import masslynx
|
|
2
|
-
from rainbow.datadirectory import DataDirectory
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def read(path, prec=0):
|
|
6
|
-
"""
|
|
7
|
-
Reads a Waters .raw directory.
|
|
8
|
-
|
|
9
|
-
Args:
|
|
10
|
-
path (str): Path of the directory.
|
|
11
|
-
prec (int, optional): Number of decimals to round ylabels.
|
|
12
|
-
|
|
13
|
-
Returns:
|
|
14
|
-
DataDirectory representing the Waters .raw directory.
|
|
15
|
-
|
|
16
|
-
"""
|
|
17
|
-
datafiles = []
|
|
18
|
-
datafiles.extend(masslynx.parse_spectrum(path, prec))
|
|
19
|
-
datafiles.extend(masslynx.parse_analog(path))
|
|
20
|
-
|
|
21
|
-
metadata = masslynx.parse_metadata(path)
|
|
22
|
-
|
|
23
|
-
return DataDirectory(path, datafiles, metadata)
|
|
24
|
-
|
|
25
|
-
def read_metadata(path):
|
|
26
|
-
"""
|
|
27
|
-
Reads metdata from a Waters .raw directory.
|
|
28
|
-
|
|
29
|
-
Args:
|
|
30
|
-
path (str): Path of the directory.
|
|
31
|
-
|
|
32
|
-
Returns:
|
|
33
|
-
Dictionary representing metadata in the Waters .raw directory.
|
|
34
|
-
"""
|
|
35
|
-
datafiles = []
|
|
36
|
-
metadata = masslynx.parse_metadata(path)
|
|
37
|
-
if len(metadata) == 1:
|
|
38
|
-
datadir = read(path)
|
|
39
|
-
return datadir.metadata if datadir else None
|
|
40
|
-
return metadata
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|