rainbow-api 1.0.4__tar.gz → 1.0.6__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.4 → rainbow-api-1.0.6}/PKG-INFO +1 -1
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/pyproject.toml +1 -1
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow/__init__.py +17 -9
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow/agilent/__init__.py +14 -6
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow/agilent/chemstation.py +183 -98
- rainbow-api-1.0.6/rainbow/waters/__init__.py +53 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow/waters/masslynx.py +123 -48
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow_api.egg-info/PKG-INFO +1 -1
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/tests/test_agilent.py +17 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/tests/test_waters.py +8 -0
- rainbow-api-1.0.4/rainbow/waters/__init__.py +0 -40
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/COPYING +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/README.md +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow/agilent/masshunter.py +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow/datadirectory.py +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow/datafile.py +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow_api.egg-info/SOURCES.txt +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow_api.egg-info/dependency_links.txt +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow_api.egg-info/requires.txt +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/rainbow_api.egg-info/top_level.txt +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/setup.cfg +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/tests/test_datadirectory.py +0 -0
- {rainbow-api-1.0.4 → rainbow-api-1.0.6}/tests/test_datafile.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}
|
|
@@ -3,19 +3,19 @@ Methods for parsing Agilent Chemstation files.
|
|
|
3
3
|
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
|
-
import os
|
|
6
|
+
import os
|
|
7
7
|
import struct
|
|
8
8
|
import numpy as np
|
|
9
9
|
from lxml import etree
|
|
10
10
|
from rainbow.datafile import DataFile
|
|
11
11
|
|
|
12
|
-
|
|
13
12
|
"""
|
|
14
13
|
MAIN PARSING METHODS
|
|
15
14
|
|
|
16
15
|
"""
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
|
|
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,11 +31,14 @@ 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)
|
|
36
39
|
return datafiles
|
|
37
40
|
|
|
41
|
+
|
|
38
42
|
def parse_file(path, prec=0):
|
|
39
43
|
"""
|
|
40
44
|
Parses an Agilent Chemstation data file.
|
|
@@ -64,6 +68,7 @@ def parse_file(path, prec=0):
|
|
|
64
68
|
|
|
65
69
|
"""
|
|
66
70
|
|
|
71
|
+
|
|
67
72
|
def parse_ch(path):
|
|
68
73
|
"""
|
|
69
74
|
Parses an Agilent .ch file.
|
|
@@ -81,15 +86,14 @@ def parse_ch(path):
|
|
|
81
86
|
Otherwise, None.
|
|
82
87
|
|
|
83
88
|
"""
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return None
|
|
89
|
+
with open(path, 'rb') as f:
|
|
90
|
+
head = read_string(f, offset=0, gap=1)
|
|
91
|
+
if head == '179':
|
|
92
|
+
return parse_ch_fid(path)
|
|
93
|
+
elif head == '130' or head == '30':
|
|
94
|
+
return parse_ch_other(path, head)
|
|
95
|
+
return None
|
|
96
|
+
|
|
93
97
|
|
|
94
98
|
def parse_ch_fid(path):
|
|
95
99
|
"""
|
|
@@ -120,7 +124,7 @@ def parse_ch_fid(path):
|
|
|
120
124
|
num_times = struct.unpack(">I", f.read(4))[0]
|
|
121
125
|
if num_times == 0:
|
|
122
126
|
return None
|
|
123
|
-
|
|
127
|
+
|
|
124
128
|
# Compute retention times using the first and last times.
|
|
125
129
|
start_time = struct.unpack(">f", f.read(4))[0]
|
|
126
130
|
end_time = struct.unpack(">f", f.read(4))[0]
|
|
@@ -134,30 +138,31 @@ def parse_ch_fid(path):
|
|
|
134
138
|
|
|
135
139
|
# Convert times into minutes.
|
|
136
140
|
times /= 60000
|
|
137
|
-
|
|
141
|
+
|
|
138
142
|
# Scale the absorbances.
|
|
139
143
|
f.seek(data_offsets['scaling_factor'])
|
|
140
144
|
scaling_factor = struct.unpack('>d', f.read(8))[0]
|
|
141
145
|
data *= scaling_factor
|
|
142
|
-
|
|
146
|
+
|
|
143
147
|
# No ylabel for FID data.
|
|
144
148
|
ylabels = np.array([''])
|
|
145
149
|
|
|
146
150
|
# Extract metadata from file header.
|
|
147
151
|
metadata_offsets = {
|
|
148
|
-
'notebook': 0x35A,
|
|
149
|
-
'date': 0x957,
|
|
150
|
-
'method': 0xA0E,
|
|
151
|
-
'instrument': 0xC11,
|
|
152
|
-
'unit':
|
|
153
|
-
'signal': 0x1075
|
|
152
|
+
'notebook': 0x35A,
|
|
153
|
+
'date': 0x957,
|
|
154
|
+
'method': 0xA0E,
|
|
155
|
+
'instrument': 0xC11,
|
|
156
|
+
'unit': 0x104C,
|
|
157
|
+
'signal': 0x1075
|
|
154
158
|
}
|
|
155
159
|
metadata = read_header(f, metadata_offsets)
|
|
156
160
|
f.close()
|
|
157
161
|
|
|
158
162
|
return DataFile(path, 'FID', times, ylabels, data, metadata)
|
|
159
163
|
|
|
160
|
-
|
|
164
|
+
|
|
165
|
+
def parse_ch_other(path, head):
|
|
161
166
|
"""
|
|
162
167
|
Parses an Agilent .ch file with CAD, ELSD, or UV channel data.
|
|
163
168
|
|
|
@@ -174,11 +179,38 @@ def parse_ch_other(path):
|
|
|
174
179
|
DataFile with CAD, ELSD, or UV data, if parsable. Otherwise, None.
|
|
175
180
|
|
|
176
181
|
"""
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
+
if head == '130':
|
|
183
|
+
data_offsets = {
|
|
184
|
+
'time_range': 0x11A,
|
|
185
|
+
'scaling_factor': 0x127C,
|
|
186
|
+
'data_start': 0x1800
|
|
187
|
+
}
|
|
188
|
+
metadata_offsets = {
|
|
189
|
+
'notebook': 0x35A,
|
|
190
|
+
'date': 0x957,
|
|
191
|
+
'method': 0xA0E,
|
|
192
|
+
'instrument': 0xC11,
|
|
193
|
+
'unit': 0x104C,
|
|
194
|
+
'signal': 0x1075
|
|
195
|
+
}
|
|
196
|
+
gap = 2
|
|
197
|
+
elif head == '30':
|
|
198
|
+
data_offsets = {
|
|
199
|
+
'time_range': 0x11A,
|
|
200
|
+
'scaling_factor': 0x284,
|
|
201
|
+
'data_start': 0x400
|
|
202
|
+
}
|
|
203
|
+
metadata_offsets = {
|
|
204
|
+
'notebook': 0x18,
|
|
205
|
+
'date': 0xB2,
|
|
206
|
+
'method': 0xE4,
|
|
207
|
+
'instrument': 0xDA,
|
|
208
|
+
'unit': 0x244,
|
|
209
|
+
'signal': 0x254
|
|
210
|
+
}
|
|
211
|
+
gap = 1
|
|
212
|
+
else:
|
|
213
|
+
return None
|
|
182
214
|
|
|
183
215
|
f = open(path, 'rb')
|
|
184
216
|
byte_unpack = struct.Struct('>B').unpack
|
|
@@ -192,10 +224,10 @@ def parse_ch_other(path):
|
|
|
192
224
|
absorb_accum = 0
|
|
193
225
|
while True:
|
|
194
226
|
# If the segment header is invalid, stop reading.
|
|
195
|
-
head = byte_unpack(f.read(1))[0]
|
|
227
|
+
head = byte_unpack(f.read(1))[0]
|
|
196
228
|
if head != 0x10:
|
|
197
229
|
break
|
|
198
|
-
num_times_seg = byte_unpack(f.read(1))[0]
|
|
230
|
+
num_times_seg = byte_unpack(f.read(1))[0]
|
|
199
231
|
|
|
200
232
|
# If the next short is equal to -0x8000
|
|
201
233
|
# then the next absorbance value is the next integer.
|
|
@@ -204,7 +236,7 @@ def parse_ch_other(path):
|
|
|
204
236
|
check_int = short_unpack(f.read(2))[0]
|
|
205
237
|
if check_int == -0x8000:
|
|
206
238
|
absorb_accum = int_unpack(f.read(4))[0]
|
|
207
|
-
else:
|
|
239
|
+
else:
|
|
208
240
|
absorb_accum += check_int
|
|
209
241
|
absorbances.append(absorb_accum)
|
|
210
242
|
|
|
@@ -217,7 +249,7 @@ def parse_ch_other(path):
|
|
|
217
249
|
|
|
218
250
|
# Calculate retention times using the first and last times.
|
|
219
251
|
f.seek(data_offsets['time_range'])
|
|
220
|
-
start_time, end_time = struct.unpack('>
|
|
252
|
+
start_time, end_time = struct.unpack('>ii', f.read(8))
|
|
221
253
|
delta_time = (end_time - start_time) / (num_times - 1)
|
|
222
254
|
times = np.arange(start_time, end_time + 1e-3, delta_time)
|
|
223
255
|
|
|
@@ -230,15 +262,8 @@ def parse_ch_other(path):
|
|
|
230
262
|
data = data.reshape(-1, 1) * scaling_factor
|
|
231
263
|
|
|
232
264
|
# Read file metadata.
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
'date': 0x957,
|
|
236
|
-
'method': 0xA0E,
|
|
237
|
-
'instrument': 0xC11,
|
|
238
|
-
'unit': 0x104C,
|
|
239
|
-
'signal': 0x1075
|
|
240
|
-
}
|
|
241
|
-
metadata = read_header(f, metadata_offsets)
|
|
265
|
+
|
|
266
|
+
metadata = read_header(f, metadata_offsets, gap=gap)
|
|
242
267
|
f.close()
|
|
243
268
|
|
|
244
269
|
# Determine the detector and ylabels using metadata.
|
|
@@ -260,6 +285,50 @@ def parse_ch_other(path):
|
|
|
260
285
|
|
|
261
286
|
"""
|
|
262
287
|
|
|
288
|
+
|
|
289
|
+
def decode_uv_delta(f, data_offsets, num_times, num_wavelengths):
|
|
290
|
+
uint_unpack = struct.Struct('<I').unpack
|
|
291
|
+
int_unpack = struct.Struct('<i').unpack
|
|
292
|
+
short_unpack = struct.Struct('<h').unpack
|
|
293
|
+
|
|
294
|
+
f.seek(data_offsets["data_start"])
|
|
295
|
+
times = np.empty(num_times, dtype=np.uint32)
|
|
296
|
+
data = np.empty((num_times, num_wavelengths), dtype=np.int64)
|
|
297
|
+
for i in range(num_times):
|
|
298
|
+
f.read(4)
|
|
299
|
+
times[i] = uint_unpack(f.read(4))[0]
|
|
300
|
+
f.read(14)
|
|
301
|
+
# If the next short is equal to -0x8000
|
|
302
|
+
# then the next absorbance value is the next integer.
|
|
303
|
+
# Otherwise, the short is a delta from the last absorbance value.
|
|
304
|
+
absorb_accum = 0
|
|
305
|
+
for j in range(num_wavelengths):
|
|
306
|
+
check_int = short_unpack(f.read(2))[0]
|
|
307
|
+
if check_int == -0x8000:
|
|
308
|
+
absorb_accum = int_unpack(f.read(4))[0]
|
|
309
|
+
else:
|
|
310
|
+
absorb_accum += check_int
|
|
311
|
+
data[i, j] = absorb_accum
|
|
312
|
+
|
|
313
|
+
return times, data
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def decode_uv_array(f, data_offsets, num_times, num_wavelengths):
|
|
317
|
+
uint_unpack = struct.Struct('<I').unpack
|
|
318
|
+
|
|
319
|
+
f.seek(data_offsets["data_start"])
|
|
320
|
+
times = np.empty(num_times, dtype=np.uint32)
|
|
321
|
+
data = np.empty((num_times, num_wavelengths), dtype=np.float64)
|
|
322
|
+
for i in range(num_times):
|
|
323
|
+
f.read(4)
|
|
324
|
+
times[i] = uint_unpack(f.read(4))[0]
|
|
325
|
+
f.read(14)
|
|
326
|
+
for j in range(num_wavelengths):
|
|
327
|
+
data[i, j] = struct.unpack('<d', f.read(8))[0]
|
|
328
|
+
|
|
329
|
+
return times, data
|
|
330
|
+
|
|
331
|
+
|
|
263
332
|
def parse_uv(path):
|
|
264
333
|
"""
|
|
265
334
|
Parses an Agilent .uv file.
|
|
@@ -275,20 +344,52 @@ def parse_uv(path):
|
|
|
275
344
|
DataFile with UV data, if the file can be parsed. Otherwise, None.
|
|
276
345
|
|
|
277
346
|
"""
|
|
278
|
-
data_offsets = {
|
|
279
|
-
'num_times': 0x116,
|
|
280
|
-
'scaling_factor': 0xC0D,
|
|
281
|
-
'data_start': 0x1000
|
|
282
|
-
}
|
|
283
347
|
|
|
284
348
|
f = open(path, 'rb')
|
|
285
|
-
uint_unpack = struct.Struct('<I').unpack
|
|
286
|
-
int_unpack = struct.Struct('<i').unpack
|
|
349
|
+
uint_unpack = struct.Struct('<I').unpack
|
|
350
|
+
int_unpack = struct.Struct('<i').unpack
|
|
287
351
|
short_unpack = struct.Struct('<h').unpack
|
|
288
352
|
|
|
289
|
-
# Validate file header.
|
|
290
|
-
head =
|
|
291
|
-
|
|
353
|
+
# Validate file header.
|
|
354
|
+
head = read_string(f, 0, gap=1)
|
|
355
|
+
|
|
356
|
+
if head == '131':
|
|
357
|
+
data_offsets = {
|
|
358
|
+
'num_times': 0x116,
|
|
359
|
+
'scaling_factor': 0xC0D,
|
|
360
|
+
'data_start': 0x1000
|
|
361
|
+
}
|
|
362
|
+
metadata_offsets = {
|
|
363
|
+
"notebook": 0x35A,
|
|
364
|
+
"date": 0x957,
|
|
365
|
+
"method": 0xA0E,
|
|
366
|
+
"unit": 0xC15,
|
|
367
|
+
"signal": 0xC40,
|
|
368
|
+
"vialpos": 0xFD7
|
|
369
|
+
}
|
|
370
|
+
file_type = read_string(f, 347, gap=2)
|
|
371
|
+
if file_type.startswith('LC'):
|
|
372
|
+
decode = decode_uv_delta
|
|
373
|
+
elif file_type.startswith('OL'):
|
|
374
|
+
decode = decode_uv_array
|
|
375
|
+
else:
|
|
376
|
+
return None
|
|
377
|
+
gap = 2
|
|
378
|
+
elif head == '31':
|
|
379
|
+
data_offsets = {
|
|
380
|
+
'num_times': 0x116,
|
|
381
|
+
'scaling_factor': 0x13E,
|
|
382
|
+
'data_start': 0x200
|
|
383
|
+
}
|
|
384
|
+
metadata_offsets = {
|
|
385
|
+
"notebook": 0x18,
|
|
386
|
+
"date": 0xB2,
|
|
387
|
+
"method": 0xE4,
|
|
388
|
+
"unit": 0x146
|
|
389
|
+
}
|
|
390
|
+
decode = decode_uv_delta
|
|
391
|
+
gap = 1
|
|
392
|
+
else:
|
|
292
393
|
f.close()
|
|
293
394
|
return None
|
|
294
395
|
|
|
@@ -309,24 +410,7 @@ def parse_uv(path):
|
|
|
309
410
|
num_wavelengths = wavelengths.size
|
|
310
411
|
|
|
311
412
|
# Extract the retention times and absorbances from each data segment.
|
|
312
|
-
f
|
|
313
|
-
times = np.empty(num_times, dtype=np.uint32)
|
|
314
|
-
data = np.empty((num_times, num_wavelengths), dtype=np.int64)
|
|
315
|
-
for i in range(num_times):
|
|
316
|
-
f.read(4)
|
|
317
|
-
times[i] = uint_unpack(f.read(4))[0]
|
|
318
|
-
f.read(14)
|
|
319
|
-
# If the next short is equal to -0x8000
|
|
320
|
-
# then the next absorbance value is the next integer.
|
|
321
|
-
# Otherwise, the short is a delta from the last absorbance value.
|
|
322
|
-
absorb_accum = 0
|
|
323
|
-
for j in range(num_wavelengths):
|
|
324
|
-
check_int = short_unpack(f.read(2))[0]
|
|
325
|
-
if check_int == -0x8000:
|
|
326
|
-
absorb_accum = int_unpack(f.read(4))[0]
|
|
327
|
-
else:
|
|
328
|
-
absorb_accum += check_int
|
|
329
|
-
data[i, j] = absorb_accum
|
|
413
|
+
times, data = decode(f, data_offsets, num_times, num_wavelengths)
|
|
330
414
|
|
|
331
415
|
# Covert times to minutes.
|
|
332
416
|
times = times / 60000
|
|
@@ -337,19 +421,13 @@ def parse_uv(path):
|
|
|
337
421
|
data = data * scaling_factor
|
|
338
422
|
|
|
339
423
|
# Read file metadata.
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
"date": 0x957,
|
|
343
|
-
"method": 0xA0E,
|
|
344
|
-
"unit": 0xC15,
|
|
345
|
-
"signal": 0xC40,
|
|
346
|
-
"vialpos": 0xFD7
|
|
347
|
-
}
|
|
348
|
-
metadata = read_header(f, metadata_offsets)
|
|
424
|
+
|
|
425
|
+
metadata = read_header(f, metadata_offsets, gap=gap)
|
|
349
426
|
f.close()
|
|
350
427
|
|
|
351
428
|
return DataFile(path, 'UV', times, wavelengths, data, metadata)
|
|
352
429
|
|
|
430
|
+
|
|
353
431
|
def parse_uv_partial(path):
|
|
354
432
|
"""
|
|
355
433
|
Parses a partial Agilent .uv file.
|
|
@@ -370,10 +448,10 @@ def parse_uv_partial(path):
|
|
|
370
448
|
}
|
|
371
449
|
|
|
372
450
|
f = open(path, 'rb')
|
|
373
|
-
uint_unpack = struct.Struct('<I').unpack
|
|
374
|
-
int_unpack = struct.Struct('<i').unpack
|
|
451
|
+
uint_unpack = struct.Struct('<I').unpack
|
|
452
|
+
int_unpack = struct.Struct('<i').unpack
|
|
375
453
|
short_unpack = struct.Struct('<h').unpack
|
|
376
|
-
|
|
454
|
+
|
|
377
455
|
# Compute the wavelengths by taking the range from
|
|
378
456
|
# the header of the first data segment.
|
|
379
457
|
# If this process fails, then the file is not a partial.
|
|
@@ -403,7 +481,7 @@ def parse_uv_partial(path):
|
|
|
403
481
|
check_int = short_unpack(f.read(2))[0]
|
|
404
482
|
if check_int == -0x8000:
|
|
405
483
|
absorb_accum = int_unpack(f.read(4))[0]
|
|
406
|
-
else:
|
|
484
|
+
else:
|
|
407
485
|
absorb_accum += check_int
|
|
408
486
|
absorbances.append(absorb_accum)
|
|
409
487
|
except Exception:
|
|
@@ -438,7 +516,8 @@ def parse_uv_partial(path):
|
|
|
438
516
|
|
|
439
517
|
"""
|
|
440
518
|
|
|
441
|
-
|
|
519
|
+
|
|
520
|
+
def parse_ms(path, prec=0):
|
|
442
521
|
"""
|
|
443
522
|
Parses an Agilent .ms file.
|
|
444
523
|
|
|
@@ -478,10 +557,10 @@ def parse_ms(path, prec=0):
|
|
|
478
557
|
if type_ms == "MSD Spectral File":
|
|
479
558
|
f.seek(data_offsets['lc_num_times'])
|
|
480
559
|
num_times = int_unpack(f.read(4))[0]
|
|
481
|
-
else:
|
|
560
|
+
else:
|
|
482
561
|
f.seek(data_offsets['gc_num_times'])
|
|
483
562
|
num_times = struct.unpack('<I', f.read(4))[0]
|
|
484
|
-
|
|
563
|
+
|
|
485
564
|
# Go to the data start offset.
|
|
486
565
|
f.seek(data_offsets['data_start'])
|
|
487
566
|
f.seek(short_unpack(f.read(2))[0] * 2 - 2)
|
|
@@ -500,7 +579,7 @@ def parse_ms(path, prec=0):
|
|
|
500
579
|
pair_bytes = f.read(pair_counts[i] * 4)
|
|
501
580
|
pair_bytearr.extend(pair_bytes)
|
|
502
581
|
f.read(10)
|
|
503
|
-
|
|
582
|
+
|
|
504
583
|
# Minor processing on the extracted data.
|
|
505
584
|
raw_bytes = bytes(pair_bytearr)
|
|
506
585
|
times = times / 60000
|
|
@@ -509,7 +588,7 @@ def parse_ms(path, prec=0):
|
|
|
509
588
|
# Calculate the mz values.
|
|
510
589
|
mzs = np.ndarray(total_paircount, '>H', raw_bytes, 0, 4)
|
|
511
590
|
mzs = np.round(mzs / 20, prec)
|
|
512
|
-
|
|
591
|
+
|
|
513
592
|
# Calculate the intensity values.
|
|
514
593
|
int_encs = np.ndarray(total_paircount, '>H', raw_bytes, 2, 4)
|
|
515
594
|
int_heads = int_encs >> 14
|
|
@@ -528,8 +607,8 @@ def parse_ms(path, prec=0):
|
|
|
528
607
|
for i in range(num_times):
|
|
529
608
|
stop_index = cur_index + pair_counts[i]
|
|
530
609
|
np.add.at(
|
|
531
|
-
data[i],
|
|
532
|
-
mz_indices[cur_index:stop_index],
|
|
610
|
+
data[i],
|
|
611
|
+
mz_indices[cur_index:stop_index],
|
|
533
612
|
int_values[cur_index:stop_index])
|
|
534
613
|
cur_index = stop_index
|
|
535
614
|
del mz_indices, mzs, int_values, pair_counts
|
|
@@ -544,6 +623,7 @@ def parse_ms(path, prec=0):
|
|
|
544
623
|
|
|
545
624
|
return DataFile(path, 'MS', times, ylabels, data, metadata)
|
|
546
625
|
|
|
626
|
+
|
|
547
627
|
def parse_ms_partial(path, prec=0):
|
|
548
628
|
"""
|
|
549
629
|
Parses a partial Agilent .ms file.
|
|
@@ -575,7 +655,7 @@ def parse_ms_partial(path, prec=0):
|
|
|
575
655
|
# but it has been constant for every .ms file we have tested.
|
|
576
656
|
# Since the start offset is not stored in partials, this code uses that
|
|
577
657
|
# "constant" common starting offset. It may not work in all cases.
|
|
578
|
-
f.seek(0x2F2)
|
|
658
|
+
f.seek(0x2F2)
|
|
579
659
|
|
|
580
660
|
# Extract retention times and data pair counts for each time.
|
|
581
661
|
# Store the bytes holding mz-intensity pairs.
|
|
@@ -603,11 +683,11 @@ def parse_ms_partial(path, prec=0):
|
|
|
603
683
|
pair_counts = np.array(pair_counts)
|
|
604
684
|
num_times = times.size
|
|
605
685
|
total_paircount = np.sum(pair_counts)
|
|
606
|
-
|
|
686
|
+
|
|
607
687
|
# Calculate the mz values.
|
|
608
688
|
mzs = np.ndarray(total_paircount, '>H', raw_bytes, 0, 4)
|
|
609
689
|
mzs = np.round(mzs / 20, prec)
|
|
610
|
-
|
|
690
|
+
|
|
611
691
|
# Calculate the intensity values.
|
|
612
692
|
int_encs = np.ndarray(total_paircount, '>H', raw_bytes, 2, 4)
|
|
613
693
|
int_heads = int_encs >> 14
|
|
@@ -626,8 +706,8 @@ def parse_ms_partial(path, prec=0):
|
|
|
626
706
|
for i in range(num_times):
|
|
627
707
|
stop_index = cur_index + pair_counts[i]
|
|
628
708
|
np.add.at(
|
|
629
|
-
data[i],
|
|
630
|
-
mz_indices[cur_index:stop_index],
|
|
709
|
+
data[i],
|
|
710
|
+
mz_indices[cur_index:stop_index],
|
|
631
711
|
int_values[cur_index:stop_index])
|
|
632
712
|
cur_index = stop_index
|
|
633
713
|
del mz_indices, mzs, int_values, pair_counts
|
|
@@ -638,7 +718,7 @@ def parse_ms_partial(path, prec=0):
|
|
|
638
718
|
'method': 0xE4
|
|
639
719
|
}
|
|
640
720
|
metadata = read_header(f, metadata_offsets, 1)
|
|
641
|
-
f.close()
|
|
721
|
+
f.close()
|
|
642
722
|
|
|
643
723
|
return DataFile(path, 'MS', times, ylabels, data, metadata)
|
|
644
724
|
|
|
@@ -648,6 +728,7 @@ FILE METADATA PARSING METHODS
|
|
|
648
728
|
|
|
649
729
|
"""
|
|
650
730
|
|
|
731
|
+
|
|
651
732
|
def read_header(f, offsets, gap=2):
|
|
652
733
|
"""
|
|
653
734
|
Extracts metadata from the header of an Agilent data file.
|
|
@@ -668,6 +749,7 @@ def read_header(f, offsets, gap=2):
|
|
|
668
749
|
metadata[key] = string
|
|
669
750
|
return metadata
|
|
670
751
|
|
|
752
|
+
|
|
671
753
|
def read_string(f, offset, gap=2):
|
|
672
754
|
"""
|
|
673
755
|
Extracts a string from the specified offset.
|
|
@@ -696,6 +778,7 @@ DIRECTORY METADATA PARSING METHODS
|
|
|
696
778
|
|
|
697
779
|
"""
|
|
698
780
|
|
|
781
|
+
|
|
699
782
|
def parse_metadata(path, datafiles):
|
|
700
783
|
"""
|
|
701
784
|
Parses Agilent metadata at the directory level.
|
|
@@ -765,7 +848,7 @@ def parse_metadata(path, datafiles):
|
|
|
765
848
|
for line in lines:
|
|
766
849
|
stripped = line.strip()
|
|
767
850
|
if "Alsbottle" not in stripped:
|
|
768
|
-
continue
|
|
851
|
+
continue
|
|
769
852
|
vialnum = stripped.split()[-1]
|
|
770
853
|
if int(vialnum):
|
|
771
854
|
metadata['vialpos'] = vialnum
|
|
@@ -788,14 +871,15 @@ def parse_metadata(path, datafiles):
|
|
|
788
871
|
vialpos = get_nextstr(split, "Vial")
|
|
789
872
|
if not vialpos:
|
|
790
873
|
vialpos = get_nextstr(split, "location")
|
|
791
|
-
if not vialpos:
|
|
874
|
+
if not vialpos:
|
|
792
875
|
vialpos = get_nextstr(split, "sample")
|
|
793
876
|
if vialpos:
|
|
794
877
|
metadata['vialpos'] = vialpos.replace("'", "")
|
|
795
878
|
break
|
|
796
|
-
|
|
879
|
+
|
|
797
880
|
return metadata
|
|
798
881
|
|
|
882
|
+
|
|
799
883
|
def get_xml_vialnum(path):
|
|
800
884
|
"""
|
|
801
885
|
Returns the VialNumber from an XML document, if it exists.
|
|
@@ -811,6 +895,7 @@ def get_xml_vialnum(path):
|
|
|
811
895
|
return vialnum.text
|
|
812
896
|
return None
|
|
813
897
|
|
|
898
|
+
|
|
814
899
|
def get_nextstr(str_list, target_str):
|
|
815
900
|
"""
|
|
816
901
|
Returns the string at the next index in :obj:`str_list`, if it exists.
|
|
@@ -824,4 +909,4 @@ def get_nextstr(str_list, target_str):
|
|
|
824
909
|
next_str = str_list[str_list.index(target_str) + 1]
|
|
825
910
|
return next_str
|
|
826
911
|
except Exception:
|
|
827
|
-
return None
|
|
912
|
+
return None
|
|
@@ -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.
|
|
@@ -107,23 +107,26 @@ def parse_function(path, prec=0, polarity=None, calib=None):
|
|
|
107
107
|
# and _FUNC .DAT data format from the _FUNC .IDX file.
|
|
108
108
|
# A "pair" refers to a data pair of mz-intensity or wavelength-absorbance.
|
|
109
109
|
times, pair_counts, bytes_per_pair = parse_funcidx(path[:-3] + 'IDX')
|
|
110
|
-
if bytes_per_pair not in {2, 6, 8}:
|
|
110
|
+
if bytes_per_pair not in {2, 4, 6, 8}:
|
|
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
|
-
|
|
120
|
-
|
|
119
|
+
elif bytes_per_pair == 4:
|
|
120
|
+
parse_funcdat = parse_funcdat4
|
|
121
|
+
ylabels, data = parse_funcdat(path, pair_counts, prec, calib)
|
|
122
|
+
|
|
121
123
|
# Spectra without an assigned polarity always contain UV data.
|
|
122
124
|
detector = 'MS' if polarity else 'UV'
|
|
123
125
|
metadata = {'polarity': polarity} if polarity else {}
|
|
124
126
|
|
|
125
127
|
return DataFile(path, detector, times, ylabels, data, metadata)
|
|
126
128
|
|
|
129
|
+
|
|
127
130
|
def parse_funcidx(path):
|
|
128
131
|
"""
|
|
129
132
|
Parses a Waters _FUNC .IDX file.
|
|
@@ -139,7 +142,7 @@ def parse_funcidx(path):
|
|
|
139
142
|
the file format of the corresponding _FUNC .DAT file.
|
|
140
143
|
|
|
141
144
|
"""
|
|
142
|
-
num_times = os.path.getsize(path) // 22
|
|
145
|
+
num_times = os.path.getsize(path) // 22
|
|
143
146
|
|
|
144
147
|
# Extract retention times and indexing info from the _FUNC .IDX file.
|
|
145
148
|
with open(path, 'rb') as f:
|
|
@@ -157,6 +160,7 @@ def parse_funcidx(path):
|
|
|
157
160
|
|
|
158
161
|
return times, pair_counts, bytes_per_pair
|
|
159
162
|
|
|
163
|
+
|
|
160
164
|
def parse_funcdat2(path, pair_counts, prec=0, calib=None):
|
|
161
165
|
"""
|
|
162
166
|
Parses a Waters _FUNC .DAT file with the 2-bytes format.
|
|
@@ -201,6 +205,65 @@ def parse_funcdat2(path, pair_counts, prec=0, calib=None):
|
|
|
201
205
|
|
|
202
206
|
return ylabels, data
|
|
203
207
|
|
|
208
|
+
|
|
209
|
+
def parse_funcdat4(path, pair_counts, prec=0, calib=None):
|
|
210
|
+
"""
|
|
211
|
+
Parses a Waters _FUNC .DAT file with the 4-bytes format.
|
|
212
|
+
|
|
213
|
+
This format may contain MS or UV data.
|
|
214
|
+
|
|
215
|
+
Learn more about this file format :ref:`here <funcdat6>`.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
path (str): Path to the Waters _FUNC .DAT file.
|
|
219
|
+
pair_counts (np.ndarray):
|
|
220
|
+
1D array with the number of data pairs at each retention time.
|
|
221
|
+
prec (int, optional): Number of decimals to round ylabels.
|
|
222
|
+
calib (list, optional): Float calibration values of the spectrum.
|
|
223
|
+
|
|
224
|
+
Returns:
|
|
225
|
+
1D numpy array with ylabels. 2D numpy array with data values \
|
|
226
|
+
where the rows correspond to retention times and \
|
|
227
|
+
the columns correspond to ylabels.
|
|
228
|
+
|
|
229
|
+
"""
|
|
230
|
+
# Extract the mz values from the _FUNCTNS.INF file.
|
|
231
|
+
# This code makes the assumption that in this format the
|
|
232
|
+
# number of mz values is constant at each retention time.
|
|
233
|
+
inf_path = os.path.join(os.path.dirname(path), '_FUNCTNS.INF')
|
|
234
|
+
func_index = int(re.findall("\d+", os.path.basename(path))[0]) - 1
|
|
235
|
+
mzs = parse_funcinf(inf_path)[func_index]
|
|
236
|
+
ylabels = mzs[mzs != 0.0]
|
|
237
|
+
|
|
238
|
+
# Read most significant 4 bytes from each segment into `raw_values`.
|
|
239
|
+
with open(path, 'rb') as f:
|
|
240
|
+
raw_bytes = f.read()
|
|
241
|
+
|
|
242
|
+
# Calculate the `values` from each 4-byte segment.
|
|
243
|
+
num_datapairs = np.sum(pair_counts)
|
|
244
|
+
raw_values = np.ndarray(num_datapairs, '<I', raw_bytes)
|
|
245
|
+
|
|
246
|
+
val_powers = raw_values >> 22 # get the first 10 bits (drop 22 on 32)
|
|
247
|
+
val_separator = (raw_values >> 21) & 0x1 # get bits at position 11
|
|
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)
|
|
252
|
+
min_val_bases = np.min(val_bases[val_bases > 0])
|
|
253
|
+
max_val_bases = np.max(val_bases)
|
|
254
|
+
val_base_ampl = max_val_bases - 0xFFFFF
|
|
255
|
+
|
|
256
|
+
values = val_bases_float * (2. ** np.subtract(val_powers, 10, dtype=np.int32))
|
|
257
|
+
|
|
258
|
+
# Note: We have not come across a sample with more than 1 mz value.
|
|
259
|
+
# This may need to be reshaped differently in the future.
|
|
260
|
+
data = values.reshape((pair_counts.size, ylabels.size))
|
|
261
|
+
|
|
262
|
+
del val_bases, val_powers, raw_values, raw_bytes
|
|
263
|
+
|
|
264
|
+
return ylabels, data
|
|
265
|
+
|
|
266
|
+
|
|
204
267
|
def parse_funcinf(path):
|
|
205
268
|
"""
|
|
206
269
|
Parses a Waters _FUNCTNS.INF file.
|
|
@@ -222,6 +285,7 @@ def parse_funcinf(path):
|
|
|
222
285
|
mzs = np.ndarray((num_funcs, 32), "<f", raw_bytes, 160, (416, 4))
|
|
223
286
|
return mzs
|
|
224
287
|
|
|
288
|
+
|
|
225
289
|
def parse_funcdat6(path, pair_counts, prec=0, calib=None):
|
|
226
290
|
"""
|
|
227
291
|
Parses a Waters _FUNC .DAT file with the 6-bytes format.
|
|
@@ -263,7 +327,7 @@ def parse_funcdat6(path, pair_counts, prec=0, calib=None):
|
|
|
263
327
|
# If it is MS data, calibrate the masses.
|
|
264
328
|
if calib:
|
|
265
329
|
keys = calibrate(keys, calib)
|
|
266
|
-
|
|
330
|
+
|
|
267
331
|
# Then round the keys to the nearest whole number.
|
|
268
332
|
keys = np.round(keys, prec)
|
|
269
333
|
|
|
@@ -284,14 +348,15 @@ def parse_funcdat6(path, pair_counts, prec=0, calib=None):
|
|
|
284
348
|
for i in range(num_times):
|
|
285
349
|
stop_index = cur_index + pair_counts[i]
|
|
286
350
|
np.add.at(
|
|
287
|
-
data[i],
|
|
288
|
-
key_indices[cur_index:stop_index],
|
|
351
|
+
data[i],
|
|
352
|
+
key_indices[cur_index:stop_index],
|
|
289
353
|
values[cur_index:stop_index])
|
|
290
354
|
cur_index = stop_index
|
|
291
355
|
del key_indices, keys, values, pair_counts
|
|
292
356
|
|
|
293
357
|
return ylabels, data
|
|
294
358
|
|
|
359
|
+
|
|
295
360
|
def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
296
361
|
"""
|
|
297
362
|
Parses a Waters _FUNC .DAT file with the 8-bytes format.
|
|
@@ -323,18 +388,18 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
323
388
|
# The data is stored as key-value pairs.
|
|
324
389
|
# For example, in MS data these are mz-intensity pairs.
|
|
325
390
|
# Split each segment into `key_bits` and `val_bits`.
|
|
326
|
-
key_bits = raw_values >> 28
|
|
391
|
+
key_bits = raw_values >> 28
|
|
327
392
|
val_bits = raw_values & 0xFFFFFFF
|
|
328
393
|
del raw_values, raw_bytes
|
|
329
394
|
|
|
330
395
|
# Split `key_bits` into integer and fractional components.
|
|
331
|
-
num_keyint_bits = key_bits >> 31
|
|
396
|
+
num_keyint_bits = key_bits >> 31
|
|
332
397
|
keyint_masks = pow(2, num_keyint_bits) - 1
|
|
333
|
-
num_keyfrac_bits = 31 - num_keyint_bits
|
|
398
|
+
num_keyfrac_bits = 31 - num_keyint_bits
|
|
334
399
|
keyfrac_masks = pow(2, num_keyfrac_bits) - 1
|
|
335
|
-
keyints = (key_bits >> num_keyfrac_bits) & keyint_masks
|
|
400
|
+
keyints = (key_bits >> num_keyfrac_bits) & keyint_masks
|
|
336
401
|
keyfracs = calc_frac(key_bits & keyfrac_masks, num_keyfrac_bits)
|
|
337
|
-
del num_keyint_bits, num_keyfrac_bits, key_bits
|
|
402
|
+
del num_keyint_bits, num_keyfrac_bits, key_bits
|
|
338
403
|
del keyint_masks, keyfrac_masks
|
|
339
404
|
|
|
340
405
|
# Get the `keys` by adding the components.
|
|
@@ -342,7 +407,7 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
342
407
|
keys = keyints + keyfracs
|
|
343
408
|
if calib:
|
|
344
409
|
keys = calibrate(keys, calib)
|
|
345
|
-
del keyints, keyfracs
|
|
410
|
+
del keyints, keyfracs
|
|
346
411
|
|
|
347
412
|
# Round the keys to the nearest whole number.
|
|
348
413
|
keys = np.round(keys, prec)
|
|
@@ -351,14 +416,14 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
351
416
|
# This is based on the number of bits allocated for each integer.
|
|
352
417
|
num_valint_bits = val_bits >> 22
|
|
353
418
|
num_shifted = np.zeros(num_datapairs, np.uint8)
|
|
354
|
-
shift_cond = num_valint_bits > 21
|
|
355
|
-
num_shifted[shift_cond] = num_valint_bits[shift_cond] - 21
|
|
356
|
-
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
|
|
357
422
|
del shift_cond
|
|
358
423
|
|
|
359
424
|
# Split `val_bits` into integer and fractional components.
|
|
360
425
|
valint_masks = pow(2, num_valint_bits) - 1
|
|
361
|
-
num_valfrac_bits = 21 - num_valint_bits
|
|
426
|
+
num_valfrac_bits = 21 - num_valint_bits
|
|
362
427
|
valfrac_masks = pow(2, num_valfrac_bits) - 1
|
|
363
428
|
valints = ((val_bits >> num_valfrac_bits) & valint_masks) << num_shifted
|
|
364
429
|
valfracs = calc_frac(val_bits & valfrac_masks, num_valfrac_bits)
|
|
@@ -368,7 +433,7 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
368
433
|
# Get the `values` by adding the components.
|
|
369
434
|
values = valints + valfracs
|
|
370
435
|
del valints, valfracs
|
|
371
|
-
|
|
436
|
+
|
|
372
437
|
# Make the array of `ylabels` with keys.
|
|
373
438
|
ylabels = np.unique(keys)
|
|
374
439
|
ylabels.sort()
|
|
@@ -380,14 +445,15 @@ def parse_funcdat8(path, pair_counts, prec=0, calib=None):
|
|
|
380
445
|
for i in range(num_times):
|
|
381
446
|
stop_index = cur_index + pair_counts[i]
|
|
382
447
|
np.add.at(
|
|
383
|
-
data[i],
|
|
384
|
-
key_indices[cur_index:stop_index],
|
|
448
|
+
data[i],
|
|
449
|
+
key_indices[cur_index:stop_index],
|
|
385
450
|
values[cur_index:stop_index])
|
|
386
451
|
cur_index = stop_index
|
|
387
452
|
del key_indices, keys, values, pair_counts
|
|
388
453
|
|
|
389
454
|
return ylabels, data
|
|
390
455
|
|
|
456
|
+
|
|
391
457
|
def calibrate(mzs, calib_nums):
|
|
392
458
|
"""
|
|
393
459
|
Calibrates :obj:`mzs` using :obj:`calib_nums`.
|
|
@@ -408,9 +474,10 @@ def calibrate(mzs, calib_nums):
|
|
|
408
474
|
for coeff in calib_nums:
|
|
409
475
|
calib_mzs += coeff * var
|
|
410
476
|
var *= mzs
|
|
411
|
-
del var
|
|
477
|
+
del var
|
|
412
478
|
return calib_mzs
|
|
413
479
|
|
|
480
|
+
|
|
414
481
|
def calc_frac(keyfrac_bits, num_bits):
|
|
415
482
|
"""
|
|
416
483
|
Decodes fractional values from :obj:`keyfrac_bits`.
|
|
@@ -430,7 +497,7 @@ def calc_frac(keyfrac_bits, num_bits):
|
|
|
430
497
|
1D numpy array with fractional values.
|
|
431
498
|
|
|
432
499
|
"""
|
|
433
|
-
exponent = np.uint64(0x3FF << 52)
|
|
500
|
+
exponent = np.uint64(0x3FF << 52)
|
|
434
501
|
num_shifted = 52 - num_bits
|
|
435
502
|
base = keyfrac_bits << num_shifted
|
|
436
503
|
fracs = (exponent | base).view(np.float64)
|
|
@@ -443,12 +510,15 @@ def calc_frac(keyfrac_bits, num_bits):
|
|
|
443
510
|
ANALOG PARSING METHODS
|
|
444
511
|
|
|
445
512
|
"""
|
|
446
|
-
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def parse_analog(path, requested_files=None):
|
|
447
516
|
"""
|
|
448
517
|
Finds and parses analog data from a Waters .raw directory.
|
|
449
518
|
|
|
450
519
|
Args:
|
|
451
|
-
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.
|
|
452
522
|
|
|
453
523
|
Returns:
|
|
454
524
|
List of DataFiles that contain analog data.
|
|
@@ -457,16 +527,19 @@ def parse_analog(path):
|
|
|
457
527
|
datafiles = []
|
|
458
528
|
|
|
459
529
|
if '_CHROMS.INF' not in os.listdir(path):
|
|
460
|
-
return datafiles
|
|
530
|
+
return datafiles
|
|
461
531
|
|
|
462
532
|
analog_info = parse_chroinf(os.path.join(path, '_CHROMS.INF'))
|
|
463
533
|
for i in range(len(analog_info)):
|
|
464
|
-
fn =
|
|
465
|
-
|
|
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])
|
|
466
538
|
if datafile:
|
|
467
539
|
datafiles.append(datafile)
|
|
468
540
|
return datafiles
|
|
469
541
|
|
|
542
|
+
|
|
470
543
|
def parse_chroinf(path):
|
|
471
544
|
"""
|
|
472
545
|
Parses a Waters _CHROMS.INF file.
|
|
@@ -482,19 +555,20 @@ def parse_chroinf(path):
|
|
|
482
555
|
|
|
483
556
|
"""
|
|
484
557
|
f = open(path, 'r')
|
|
485
|
-
f.seek(0x84)
|
|
558
|
+
f.seek(0x84) # start offset
|
|
486
559
|
analog_info = []
|
|
487
560
|
while f.tell() < os.path.getsize(path):
|
|
488
561
|
line = re.sub(r'[\0-\x04]|\$CC\$|\([0-9]*\)', '', f.read(0x55)).strip()
|
|
489
562
|
split = line.split(',')
|
|
490
563
|
info = []
|
|
491
|
-
info.append(split[0])
|
|
564
|
+
info.append(split[0]) # name
|
|
492
565
|
if len(split) == 6:
|
|
493
|
-
info.append(split[5])
|
|
566
|
+
info.append(split[5]) # unit
|
|
494
567
|
analog_info.append(info)
|
|
495
568
|
f.close()
|
|
496
569
|
return analog_info
|
|
497
570
|
|
|
571
|
+
|
|
498
572
|
def parse_chrodat(path, name, units=None):
|
|
499
573
|
"""
|
|
500
574
|
Parses a Waters _CHRO .DAT file.
|
|
@@ -526,7 +600,7 @@ def parse_chrodat(path, name, units=None):
|
|
|
526
600
|
with open(path, 'rb') as f:
|
|
527
601
|
raw_bytes = f.read()
|
|
528
602
|
times_immut = np.ndarray(num_times, '<f', raw_bytes, data_start, 8)
|
|
529
|
-
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)
|
|
530
604
|
|
|
531
605
|
# The arrays are copied so that they are mutable.
|
|
532
606
|
# This is just for user convenience.
|
|
@@ -545,8 +619,8 @@ def parse_chrodat(path, name, units=None):
|
|
|
545
619
|
|
|
546
620
|
ylabels = np.array([''])
|
|
547
621
|
metadata = {'signal': name}
|
|
548
|
-
if units:
|
|
549
|
-
metadata['unit'] = units
|
|
622
|
+
if units:
|
|
623
|
+
metadata['unit'] = units
|
|
550
624
|
|
|
551
625
|
return DataFile(path, detector, times, ylabels, vals, metadata)
|
|
552
626
|
|
|
@@ -556,6 +630,7 @@ METADATA PARSING METHOD
|
|
|
556
630
|
|
|
557
631
|
"""
|
|
558
632
|
|
|
633
|
+
|
|
559
634
|
def parse_metadata(path):
|
|
560
635
|
"""
|
|
561
636
|
Parses metadata from a Waters .raw directory.
|
|
@@ -588,4 +663,4 @@ def parse_metadata(path):
|
|
|
588
663
|
value = line.split(': ')[1]
|
|
589
664
|
if not value.isspace():
|
|
590
665
|
metadata['vialpos'] = value
|
|
591
|
-
return metadata
|
|
666
|
+
return metadata
|
|
@@ -44,6 +44,23 @@ class TestAgilent(tests.datatester.DataTester):
|
|
|
44
44
|
"""
|
|
45
45
|
self._DataTester__test_data_directory("green", "D")
|
|
46
46
|
|
|
47
|
+
def test_brown(self):
|
|
48
|
+
"""
|
|
49
|
+
Tests a directory containing:
|
|
50
|
+
- 31-version UV spectrum
|
|
51
|
+
- 4 30-version MS traces (LC format)
|
|
52
|
+
|
|
53
|
+
"""
|
|
54
|
+
self._DataTester__test_data_directory("brown", "D")
|
|
55
|
+
|
|
56
|
+
def test_pink(self):
|
|
57
|
+
"""
|
|
58
|
+
Tests a directory containing:
|
|
59
|
+
- UV spectrum (131 OL format)
|
|
60
|
+
- 6 MS channels (179 OL format)
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
self._DataTester__test_data_directory("pink", "D")
|
|
47
64
|
|
|
48
65
|
if __name__ == '__main__':
|
|
49
66
|
unittest.main()
|
|
@@ -36,6 +36,14 @@ class TestWaters(tests.datatester.DataTester):
|
|
|
36
36
|
"""
|
|
37
37
|
self._DataTester__test_data_directory("violet", "raw")
|
|
38
38
|
|
|
39
|
+
def test_white(self):
|
|
40
|
+
"""
|
|
41
|
+
Tests a directory containing:
|
|
42
|
+
- 6 UV spectrum (4-byte format)
|
|
43
|
+
- 2 analog channels
|
|
44
|
+
|
|
45
|
+
"""
|
|
46
|
+
self._DataTester__test_data_directory("white", "raw")
|
|
39
47
|
|
|
40
48
|
if __name__ == '__main__':
|
|
41
49
|
unittest.main()
|
|
@@ -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
|