rainbow-api 1.3.0__tar.gz → 1.4.0__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.
Files changed (35) hide show
  1. {rainbow_api-1.3.0/rainbow_api.egg-info → rainbow_api-1.4.0}/PKG-INFO +3 -1
  2. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/pyproject.toml +6 -1
  3. rainbow_api-1.4.0/rainbow/agilent/__init__.py +118 -0
  4. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/agilent/_msprofile.pyx +7 -11
  5. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/agilent/masshunter.py +604 -8
  6. {rainbow_api-1.3.0 → rainbow_api-1.4.0/rainbow_api.egg-info}/PKG-INFO +3 -1
  7. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow_api.egg-info/requires.txt +3 -0
  8. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/tests/test_binning.py +7 -7
  9. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/tests/test_masshunter.py +510 -0
  10. rainbow_api-1.3.0/rainbow/agilent/__init__.py +0 -82
  11. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/COPYING +0 -0
  12. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/COPYING.LESSER +0 -0
  13. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/MANIFEST.in +0 -0
  14. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/README.md +0 -0
  15. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/__init__.py +0 -0
  16. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/_binning.py +0 -0
  17. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/agilent/_chdelta.pyx +0 -0
  18. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/agilent/_uvdelta.pyx +0 -0
  19. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/agilent/chemstation.py +0 -0
  20. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/agilent/openlab.py +0 -0
  21. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/datadirectory.py +0 -0
  22. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/datafile.py +0 -0
  23. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/waters/__init__.py +0 -0
  24. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow/waters/masslynx.py +0 -0
  25. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow_api.egg-info/SOURCES.txt +0 -0
  26. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow_api.egg-info/dependency_links.txt +0 -0
  27. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/rainbow_api.egg-info/top_level.txt +0 -0
  28. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/setup.cfg +0 -0
  29. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/setup.py +0 -0
  30. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/tests/test_accelerator.py +0 -0
  31. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/tests/test_agilent.py +0 -0
  32. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/tests/test_datadirectory.py +0 -0
  33. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/tests/test_datafile.py +0 -0
  34. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/tests/test_read.py +0 -0
  35. {rainbow_api-1.3.0 → rainbow_api-1.4.0}/tests/test_waters.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rainbow-api
3
- Version: 1.3.0
3
+ Version: 1.4.0
4
4
  Summary: Read chromatography and mass spectrometry binary files
5
5
  Author: Evan Shi and Eugene Kwan
6
6
  Author-email: evanyshi@cmu.edu
@@ -17,6 +17,8 @@ Requires-Dist: lxml
17
17
  Requires-Dist: pandas
18
18
  Provides-Extra: test
19
19
  Requires-Dist: pytest; extra == "test"
20
+ Provides-Extra: hrms
21
+ Requires-Dist: python-lzf; extra == "hrms"
20
22
  Dynamic: license-file
21
23
 
22
24
  # rainbow
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "rainbow-api"
7
- version = "1.3.0"
7
+ version = "1.4.0"
8
8
  description = "Read chromatography and mass spectrometry binary files"
9
9
  readme = "README.md"
10
10
  license = {text = "COPYING.LESSER"}
@@ -24,6 +24,11 @@ dependencies = [
24
24
 
25
25
  [project.optional-dependencies]
26
26
  test = ["pytest"]
27
+ # Agilent MassHunter profile data (MSProfile.bin) can carry LZF-compressed
28
+ # segments whose decode needs python-lzf. It is only imported when such a
29
+ # segment is actually encountered, so the base install stays pure-Python;
30
+ # install rainbow-api[hrms] to parse those files (read(..., hrms=True)).
31
+ hrms = ["python-lzf"]
27
32
 
28
33
  [project.urls]
29
34
  Repository = "https://github.com/evanyeyeye/rainbow"
@@ -0,0 +1,118 @@
1
+ import os
2
+
3
+ from rainbow.agilent import chemstation
4
+ from rainbow.datadirectory import DataDirectory
5
+
6
+
7
+ def read(path, precision='auto', hrms=False, requested_files=None,
8
+ telemetry=False, centroid=False, bin_width=None):
9
+ """
10
+ Reads an Agilent .D directory or .dx archive.
11
+
12
+ Args:
13
+ path (str): Path of the directory or .dx file.
14
+ precision (int or 'auto', optional): Number of decimals to round masses.
15
+ ``'auto'`` picks 4 for high-resolution data and 0 otherwise, per file.
16
+ hrms (bool, optional): Flag for parsing the MassHunter profile
17
+ spectrum (MSProfile.bin).
18
+ requested_files (list, optional): List of filenames to parse.
19
+ telemetry (bool, optional): Flag for parsing instrument telemetry
20
+ traces, in a .dx archive or alongside a MassHunter DAD's signals.
21
+ centroid (bool, optional): Flag for parsing the MassHunter centroid
22
+ spectrum (MSPeak.bin).
23
+ bin_width (float, optional): Shared-grid bin width in daltons for the
24
+ HRMS profile. Omit it (the default) to keep the per-scan
25
+ representation (one
26
+ :class:`~rainbow.agilent.masshunter.ProfileDataFile` per flight-time
27
+ grid); pass a width to project onto the shared m/z grid.
28
+
29
+ Returns:
30
+ DataDirectory representing the Agilent data.
31
+
32
+ """
33
+ if os.path.splitext(path)[1].lower() == '.dx':
34
+ from rainbow.agilent import openlab
35
+ return openlab.read(path, precision, requested_files, telemetry)
36
+
37
+ datafiles = []
38
+ datafiles.extend(chemstation.parse_allfiles(path, precision, requested_files))
39
+ # MassHunter is always consulted, not only under the MS flags: a .d may also
40
+ # hold DAD data, which is parsed unconditionally the way the Chemstation UV
41
+ # formats are. The MS parsing inside stays gated on hrms/centroid.
42
+ from rainbow.agilent import masshunter
43
+ datafiles.extend(masshunter.parse_allfiles(
44
+ path, precision, hrms, centroid, bin_width, telemetry,
45
+ requested_files))
46
+
47
+ metadata = chemstation.parse_metadata(path, datafiles)
48
+
49
+ # Surface unread MassHunter profile/centroid data so the hrms and centroid
50
+ # flags are discoverable: a .D with MSProfile.bin holds a profile spectrum
51
+ # parsed only when hrms=True, and MSPeak.bin a centroid spectrum parsed only
52
+ # when centroid=True. Otherwise read() returns an empty DataDirectory with
53
+ # no hint that the flag is what's missing.
54
+ if not hrms and os.path.isfile(
55
+ os.path.join(path, "AcqData", "MSProfile.bin")):
56
+ metadata['hrms_available'] = True
57
+ if not centroid and os.path.isfile(
58
+ os.path.join(path, "AcqData", "MSPeak.bin")):
59
+ metadata['centroid_available'] = True
60
+
61
+ return DataDirectory(path, datafiles, metadata)
62
+
63
+
64
+ def read_metadata(path):
65
+ """
66
+ Reads metadata from an Agilent .D directory.
67
+
68
+ Args:
69
+ path (str): Path of the directory.
70
+
71
+ Returns:
72
+ Dictionary containing a list of datafiles and the metadata.
73
+
74
+ """
75
+ if os.path.splitext(path)[1].lower() == '.dx':
76
+ from rainbow.agilent import openlab
77
+ return openlab.read_metadata(path)
78
+
79
+ datafiles = []
80
+ metadata = chemstation.parse_metadata(path, datafiles)
81
+
82
+ # MassHunter acquisitions (a .D with an AcqData subfolder) keep their data
83
+ # in MSProfile.bin (profile/HRMS) and/or MSPeak.bin (centroid), which the
84
+ # Chemstation .uv/.ch/.ms scan below never finds - so they used to come
85
+ # back with an empty datafile list. Mirror masshunter.parse_allfiles'
86
+ # detection to surface the datafile names and the flags needed to read
87
+ # them (hrms / centroid), without parsing the binaries.
88
+ acqdata_path = os.path.join(path, "AcqData")
89
+ if os.path.isdir(acqdata_path):
90
+ from rainbow.agilent import masshunter
91
+ acqdata_files = set(os.listdir(acqdata_path))
92
+ mh_datafiles = []
93
+ # A diode-array detector writes its chromatograms and spectra here too,
94
+ # and read() returns those with no flag needed - so a run holding them
95
+ # must not be reported as MS-only.
96
+ for name in sorted(acqdata_files):
97
+ stem, ext = os.path.splitext(name)
98
+ if (ext.lower() in ('.cg', '.sp')
99
+ and stem[:3].upper() in masshunter._DAD_DEVICES):
100
+ mh_datafiles.append(name)
101
+ if {"MSScan.xsd", "MSScan.bin"} <= acqdata_files:
102
+ if "MSProfile.bin" in acqdata_files:
103
+ mh_datafiles.append("MSProfile.bin")
104
+ metadata['hrms_available'] = True
105
+ if "MSPeak.bin" in acqdata_files:
106
+ mh_datafiles.append("MSPeak.bin")
107
+ metadata['centroid_available'] = True
108
+ if mh_datafiles:
109
+ return {'datafiles': mh_datafiles, 'metadata': metadata}
110
+
111
+ if len(metadata) == 1:
112
+ datadir = read(path)
113
+ if datadir:
114
+ return {'datafiles': datadir.datafiles + datadir.analog, 'metadata': datadir.metadata}
115
+ return None
116
+ # Masshunter datafiles are not located.
117
+ datafiles = [fn for fn in os.listdir(path) if fn[-3:].lower() in ('.uv', '.ch', '.ms')]
118
+ return {'datafiles': datafiles, 'metadata': metadata}
@@ -24,11 +24,12 @@ from libc.string cimport memcpy
24
24
 
25
25
 
26
26
  cdef inline int _width_size(int flag) except -1:
27
- """Bytes for an RLE width flag (1/2/3/4 -> 1/2/4/8); raise on anything else.
27
+ """Bytes for an RLE width flag (1/2/3 -> 1/2/4); raise on anything else.
28
28
 
29
- A zero or out-of-range flag means a corrupt stream; raising here mirrors
30
- the ValueError the pure-Python reference reports rather than reading at a
31
- bogus width (this module disables Cython's automatic bounds checks).
29
+ The flag is ``(-value) % 4``, so it is always 0-3; 0 means a corrupt
30
+ stream. A zero or out-of-range flag raises here, mirroring the ValueError
31
+ the pure-Python reference reports rather than reading at a bogus width
32
+ (this module disables Cython's automatic bounds checks).
32
33
  """
33
34
  if flag == 1:
34
35
  return 1
@@ -36,8 +37,6 @@ cdef inline int _width_size(int flag) except -1:
36
37
  return 2
37
38
  elif flag == 3:
38
39
  return 4
39
- elif flag == 4:
40
- return 8
41
40
  raise ValueError("Malformed MSProfile.bin RLE segment.")
42
41
 
43
42
 
@@ -88,23 +87,20 @@ def decompress_inten_list(const unsigned char[::1] comp_view, int num_mz):
88
87
  cdef signed char v1
89
88
  cdef short v2
90
89
  cdef int v4
91
- cdef long long v8
92
90
 
93
91
  while off < n:
94
92
  if off + cur_size > n:
95
93
  raise ValueError("Malformed MSProfile.bin RLE segment.")
94
+ # _width_size guarantees cur_size is 1, 2, or 4.
96
95
  if cur_size == 1:
97
96
  memcpy(&v1, &comp_view[off], 1)
98
97
  value = v1
99
98
  elif cur_size == 2:
100
99
  memcpy(&v2, &comp_view[off], 2)
101
100
  value = v2
102
- elif cur_size == 4:
101
+ else:
103
102
  memcpy(&v4, &comp_view[off], 4)
104
103
  value = v4
105
- else:
106
- memcpy(&v8, &comp_view[off], 8)
107
- value = v8
108
104
  off += cur_size
109
105
 
110
106
  if value >= 0: