masster 0.2.4__py3-none-any.whl → 0.3.0__py3-none-any.whl
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.
Potentially problematic release.
This version of masster might be problematic. Click here for more details.
- masster/__init__.py +27 -27
- masster/_version.py +17 -17
- masster/chromatogram.py +497 -503
- masster/data/examples/2025_01_14_VW_7600_LpMx_DBS_CID_2min_TOP15_030msecMS1_005msecReac_CE35_DBS-ON_3.featureXML +199787 -0
- masster/data/examples/2025_01_14_VW_7600_LpMx_DBS_CID_2min_TOP15_030msecMS1_005msecReac_CE35_DBS-ON_3.sample5 +0 -0
- masster/logger.py +318 -244
- masster/sample/__init__.py +9 -9
- masster/sample/defaults/__init__.py +15 -15
- masster/sample/defaults/find_adducts_def.py +325 -325
- masster/sample/defaults/find_features_def.py +366 -366
- masster/sample/defaults/find_ms2_def.py +285 -285
- masster/sample/defaults/get_spectrum_def.py +314 -318
- masster/sample/defaults/sample_def.py +374 -378
- masster/sample/h5.py +1321 -1297
- masster/sample/helpers.py +833 -364
- masster/sample/lib.py +762 -0
- masster/sample/load.py +1220 -1187
- masster/sample/parameters.py +131 -131
- masster/sample/plot.py +1610 -1622
- masster/sample/processing.py +1402 -1416
- masster/sample/quant.py +209 -0
- masster/sample/sample.py +391 -387
- masster/sample/sample5_schema.json +181 -181
- masster/sample/save.py +737 -719
- masster/sample/sciex.py +1213 -0
- masster/spectrum.py +1287 -1319
- masster/study/__init__.py +9 -9
- masster/study/defaults/__init__.py +21 -19
- masster/study/defaults/align_def.py +267 -267
- masster/study/defaults/export_def.py +41 -40
- masster/study/defaults/fill_chrom_def.py +264 -264
- masster/study/defaults/fill_def.py +260 -0
- masster/study/defaults/find_consensus_def.py +256 -256
- masster/study/defaults/find_ms2_def.py +163 -163
- masster/study/defaults/integrate_chrom_def.py +225 -225
- masster/study/defaults/integrate_def.py +221 -0
- masster/study/defaults/merge_def.py +256 -0
- masster/study/defaults/study_def.py +272 -269
- masster/study/export.py +674 -287
- masster/study/h5.py +1398 -886
- masster/study/helpers.py +1650 -433
- masster/study/helpers_optimized.py +317 -0
- masster/study/load.py +1201 -1078
- masster/study/parameters.py +99 -99
- masster/study/plot.py +632 -645
- masster/study/processing.py +1057 -1046
- masster/study/save.py +149 -134
- masster/study/study.py +606 -522
- masster/study/study5_schema.json +247 -241
- {masster-0.2.4.dist-info → masster-0.3.0.dist-info}/METADATA +15 -10
- masster-0.3.0.dist-info/RECORD +59 -0
- {masster-0.2.4.dist-info → masster-0.3.0.dist-info}/licenses/LICENSE +661 -661
- masster-0.2.4.dist-info/RECORD +0 -50
- {masster-0.2.4.dist-info → masster-0.3.0.dist-info}/WHEEL +0 -0
- {masster-0.2.4.dist-info → masster-0.3.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,285 +1,285 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Find MS2 Parameters Module
|
|
3
|
-
|
|
4
|
-
This module defines parameters for MS2 spectrum linking in mass spectrometry data.
|
|
5
|
-
It consolidates all parameters used in the find_ms2() method with type checking,
|
|
6
|
-
validation, and comprehensive descriptions.
|
|
7
|
-
|
|
8
|
-
Classes:
|
|
9
|
-
find_ms2_defaults: Configuration parameters for the find_ms2() method.
|
|
10
|
-
"""
|
|
11
|
-
|
|
12
|
-
from __future__ import annotations
|
|
13
|
-
|
|
14
|
-
from dataclasses import dataclass, field
|
|
15
|
-
from typing import Any
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
@dataclass
|
|
19
|
-
class find_ms2_defaults:
|
|
20
|
-
"""
|
|
21
|
-
Parameters for MS2 spectrum linking functionality.
|
|
22
|
-
|
|
23
|
-
This class consolidates all parameters used in the find_ms2() method including
|
|
24
|
-
m/z tolerance, centroiding options, and processing flags.
|
|
25
|
-
It provides type checking, validation, and comprehensive parameter descriptions.
|
|
26
|
-
|
|
27
|
-
MS2 Linking Parameters:
|
|
28
|
-
mz_tol: m/z tolerance for MS2 precursor matching.
|
|
29
|
-
centroid: Whether to centroid the returned spectrum.
|
|
30
|
-
deisotope: Whether to deisotope the returned spectrum.
|
|
31
|
-
dia_stats: Whether to collect DIA-related statistics.
|
|
32
|
-
features: Specific feature UID(s) to process, or None for all.
|
|
33
|
-
mz_tol_ztscan: m/z tolerance for ztscan/DIA file types.
|
|
34
|
-
|
|
35
|
-
Available Methods:
|
|
36
|
-
- validate(param_name, value): Validate a single parameter value
|
|
37
|
-
- validate_all(): Validate all parameters at once
|
|
38
|
-
- to_dict(): Convert parameters to dictionary
|
|
39
|
-
- set_from_dict(param_dict, validate=True): Update multiple parameters from dict
|
|
40
|
-
- set(param_name, value, validate=True): Set parameter value with validation
|
|
41
|
-
- get(param_name): Get parameter value
|
|
42
|
-
- get_description(param_name): Get parameter description
|
|
43
|
-
- get_info(param_name): Get full parameter metadata
|
|
44
|
-
- list_parameters(): Get list of all parameter names
|
|
45
|
-
- get_mz_tolerance(file_type): Get appropriate m/z tolerance based on file type
|
|
46
|
-
"""
|
|
47
|
-
|
|
48
|
-
# Core MS2 linking parameters
|
|
49
|
-
mz_tol: float = 0.5
|
|
50
|
-
centroid: bool = True
|
|
51
|
-
deisotope: bool = False
|
|
52
|
-
dia_stats: bool = False
|
|
53
|
-
|
|
54
|
-
# Feature selection parameters - can be None, int, or list of ints
|
|
55
|
-
features: int | list[int] | None = field(default_factory=list)
|
|
56
|
-
|
|
57
|
-
# File type specific adjustments
|
|
58
|
-
mz_tol_ztscan: float = 4.0
|
|
59
|
-
|
|
60
|
-
# Parameter metadata for validation and description
|
|
61
|
-
_param_metadata: dict[str, dict[str, Any]] = field(
|
|
62
|
-
default_factory=lambda: {
|
|
63
|
-
"mz_tol": {
|
|
64
|
-
"dtype": float,
|
|
65
|
-
"description": "m/z tolerance for MS2 precursor matching",
|
|
66
|
-
"min_value": 0.01,
|
|
67
|
-
"max_value": 10.0,
|
|
68
|
-
},
|
|
69
|
-
"centroid": {
|
|
70
|
-
"dtype": bool,
|
|
71
|
-
"description": "Whether to centroid the returned spectrum",
|
|
72
|
-
},
|
|
73
|
-
"deisotope": {
|
|
74
|
-
"dtype": bool,
|
|
75
|
-
"description": "Whether to deisotope the returned spectrum",
|
|
76
|
-
},
|
|
77
|
-
"dia_stats": {
|
|
78
|
-
"dtype": bool,
|
|
79
|
-
"description": "Whether to collect DIA-related statistics",
|
|
80
|
-
},
|
|
81
|
-
"features": {
|
|
82
|
-
"dtype": "Union[int, List[int], None]",
|
|
83
|
-
"description": "Specific feature UID(s) to process, or None for all",
|
|
84
|
-
},
|
|
85
|
-
"mz_tol_ztscan": {
|
|
86
|
-
"dtype": float,
|
|
87
|
-
"description": "m/z tolerance for ztscan/DIA file types",
|
|
88
|
-
"min_value": 0.1,
|
|
89
|
-
"max_value": 20.0,
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
def get_info(self, param_name: str) -> dict[str, Any]:
|
|
95
|
-
"""
|
|
96
|
-
Get information about a specific parameter.
|
|
97
|
-
|
|
98
|
-
Args:
|
|
99
|
-
param_name: Name of the parameter
|
|
100
|
-
|
|
101
|
-
Returns:
|
|
102
|
-
Dictionary containing parameter metadata
|
|
103
|
-
|
|
104
|
-
Raises:
|
|
105
|
-
KeyError: If parameter name is not found
|
|
106
|
-
"""
|
|
107
|
-
if param_name not in self._param_metadata:
|
|
108
|
-
raise KeyError(f"Parameter '{param_name}' not found")
|
|
109
|
-
return self._param_metadata[param_name]
|
|
110
|
-
|
|
111
|
-
def get_description(self, param_name: str) -> str:
|
|
112
|
-
"""
|
|
113
|
-
Get description for a specific parameter.
|
|
114
|
-
|
|
115
|
-
Args:
|
|
116
|
-
param_name: Name of the parameter
|
|
117
|
-
|
|
118
|
-
Returns:
|
|
119
|
-
Parameter description string
|
|
120
|
-
"""
|
|
121
|
-
return str(self.get_info(param_name)["description"])
|
|
122
|
-
|
|
123
|
-
def validate(self, param_name: str, value: Any) -> bool:
|
|
124
|
-
"""
|
|
125
|
-
Validate a parameter value against its constraints.
|
|
126
|
-
|
|
127
|
-
Args:
|
|
128
|
-
param_name: Name of the parameter
|
|
129
|
-
value: Value to validate
|
|
130
|
-
|
|
131
|
-
Returns:
|
|
132
|
-
True if value is valid, False otherwise
|
|
133
|
-
"""
|
|
134
|
-
if param_name not in self._param_metadata:
|
|
135
|
-
return False
|
|
136
|
-
|
|
137
|
-
metadata = self._param_metadata[param_name]
|
|
138
|
-
expected_dtype = metadata["dtype"]
|
|
139
|
-
|
|
140
|
-
# Handle Union types for features parameter
|
|
141
|
-
if param_name == "features":
|
|
142
|
-
if value is None or isinstance(value, (int, list)):
|
|
143
|
-
if isinstance(value, list):
|
|
144
|
-
return all(isinstance(item, int) for item in value)
|
|
145
|
-
return True
|
|
146
|
-
return False
|
|
147
|
-
|
|
148
|
-
# Type checking for non-Union types
|
|
149
|
-
if expected_dtype is float:
|
|
150
|
-
if not isinstance(value, (int, float)):
|
|
151
|
-
try:
|
|
152
|
-
value = float(value)
|
|
153
|
-
except (ValueError, TypeError):
|
|
154
|
-
return False
|
|
155
|
-
elif expected_dtype is bool:
|
|
156
|
-
if not isinstance(value, bool):
|
|
157
|
-
return False
|
|
158
|
-
|
|
159
|
-
# Range checking for numeric values
|
|
160
|
-
if isinstance(value, (int, float)) and value is not None:
|
|
161
|
-
if "min_value" in metadata and value < metadata["min_value"]:
|
|
162
|
-
return False
|
|
163
|
-
if "max_value" in metadata and value > metadata["max_value"]:
|
|
164
|
-
return False
|
|
165
|
-
|
|
166
|
-
return True
|
|
167
|
-
|
|
168
|
-
def set(self, param_name: str, value: Any, validate: bool = True) -> bool:
|
|
169
|
-
"""
|
|
170
|
-
Set a parameter value with optional validation.
|
|
171
|
-
|
|
172
|
-
Args:
|
|
173
|
-
param_name: Name of the parameter
|
|
174
|
-
value: New value for the parameter
|
|
175
|
-
validate: Whether to validate the value before setting
|
|
176
|
-
|
|
177
|
-
Returns:
|
|
178
|
-
True if parameter was set successfully, False otherwise
|
|
179
|
-
"""
|
|
180
|
-
if not hasattr(self, param_name):
|
|
181
|
-
return False
|
|
182
|
-
|
|
183
|
-
if validate and not self.validate(param_name, value):
|
|
184
|
-
return False
|
|
185
|
-
|
|
186
|
-
# Convert to expected type if needed
|
|
187
|
-
if param_name in self._param_metadata:
|
|
188
|
-
expected_dtype = self._param_metadata[param_name]["dtype"]
|
|
189
|
-
if expected_dtype is float and not isinstance(value, float):
|
|
190
|
-
try:
|
|
191
|
-
value = float(value)
|
|
192
|
-
except (ValueError, TypeError):
|
|
193
|
-
if validate:
|
|
194
|
-
return False
|
|
195
|
-
|
|
196
|
-
setattr(self, param_name, value)
|
|
197
|
-
return True
|
|
198
|
-
|
|
199
|
-
def get(self, param_name: str) -> Any:
|
|
200
|
-
"""
|
|
201
|
-
Get the value of a parameter by name.
|
|
202
|
-
|
|
203
|
-
Args:
|
|
204
|
-
param_name: Name of the parameter
|
|
205
|
-
|
|
206
|
-
Returns:
|
|
207
|
-
Current value of the parameter
|
|
208
|
-
"""
|
|
209
|
-
if not hasattr(self, param_name):
|
|
210
|
-
raise KeyError(f"Parameter '{param_name}' not found")
|
|
211
|
-
return getattr(self, param_name)
|
|
212
|
-
|
|
213
|
-
def set_from_dict(
|
|
214
|
-
self,
|
|
215
|
-
param_dict: dict[str, Any],
|
|
216
|
-
validate: bool = True,
|
|
217
|
-
) -> list[str]:
|
|
218
|
-
"""
|
|
219
|
-
Update multiple parameters from a dictionary.
|
|
220
|
-
|
|
221
|
-
Args:
|
|
222
|
-
param_dict: Dictionary of parameter names and values
|
|
223
|
-
validate: Whether to validate values before setting
|
|
224
|
-
|
|
225
|
-
Returns:
|
|
226
|
-
List of parameter names that could not be set
|
|
227
|
-
"""
|
|
228
|
-
failed_params = []
|
|
229
|
-
|
|
230
|
-
for param_name, value in param_dict.items():
|
|
231
|
-
if not self.set(param_name, value, validate):
|
|
232
|
-
failed_params.append(param_name)
|
|
233
|
-
|
|
234
|
-
return failed_params
|
|
235
|
-
|
|
236
|
-
def to_dict(self) -> dict[str, Any]:
|
|
237
|
-
"""
|
|
238
|
-
Convert parameters to dictionary, excluding metadata.
|
|
239
|
-
|
|
240
|
-
Returns:
|
|
241
|
-
Dictionary of parameter names and values
|
|
242
|
-
"""
|
|
243
|
-
return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
|
|
244
|
-
|
|
245
|
-
def list_parameters(self) -> list[str]:
|
|
246
|
-
"""
|
|
247
|
-
Get list of all parameter names.
|
|
248
|
-
|
|
249
|
-
Returns:
|
|
250
|
-
List of parameter names
|
|
251
|
-
"""
|
|
252
|
-
return [k for k in self.__dict__.keys() if not k.startswith("_")]
|
|
253
|
-
|
|
254
|
-
def validate_all(self) -> tuple[bool, list[str]]:
|
|
255
|
-
"""
|
|
256
|
-
Validate all parameters in the instance.
|
|
257
|
-
|
|
258
|
-
Returns:
|
|
259
|
-
Tuple of (all_valid, list_of_invalid_params)
|
|
260
|
-
- all_valid: True if all parameters are valid, False otherwise
|
|
261
|
-
- list_of_invalid_params: List of parameter names that failed validation
|
|
262
|
-
"""
|
|
263
|
-
invalid_params = []
|
|
264
|
-
|
|
265
|
-
for param_name in self.list_parameters():
|
|
266
|
-
if param_name in self._param_metadata:
|
|
267
|
-
current_value = getattr(self, param_name)
|
|
268
|
-
if not self.validate(param_name, current_value):
|
|
269
|
-
invalid_params.append(param_name)
|
|
270
|
-
|
|
271
|
-
return len(invalid_params) == 0, invalid_params
|
|
272
|
-
|
|
273
|
-
def get_mz_tolerance(self, file_type=None):
|
|
274
|
-
"""
|
|
275
|
-
Get the appropriate m/z tolerance based on file type.
|
|
276
|
-
|
|
277
|
-
Args:
|
|
278
|
-
file_type (str, optional): File type ('ztscan', 'dia', or other)
|
|
279
|
-
|
|
280
|
-
Returns:
|
|
281
|
-
float: Appropriate m/z tolerance value
|
|
282
|
-
"""
|
|
283
|
-
if file_type is not None and file_type.lower() in ["ztscan", "dia"]:
|
|
284
|
-
return self.get("mz_tol_ztscan")
|
|
285
|
-
return self.get("mz_tol")
|
|
1
|
+
"""
|
|
2
|
+
Find MS2 Parameters Module
|
|
3
|
+
|
|
4
|
+
This module defines parameters for MS2 spectrum linking in mass spectrometry data.
|
|
5
|
+
It consolidates all parameters used in the find_ms2() method with type checking,
|
|
6
|
+
validation, and comprehensive descriptions.
|
|
7
|
+
|
|
8
|
+
Classes:
|
|
9
|
+
find_ms2_defaults: Configuration parameters for the find_ms2() method.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class find_ms2_defaults:
|
|
20
|
+
"""
|
|
21
|
+
Parameters for MS2 spectrum linking functionality.
|
|
22
|
+
|
|
23
|
+
This class consolidates all parameters used in the find_ms2() method including
|
|
24
|
+
m/z tolerance, centroiding options, and processing flags.
|
|
25
|
+
It provides type checking, validation, and comprehensive parameter descriptions.
|
|
26
|
+
|
|
27
|
+
MS2 Linking Parameters:
|
|
28
|
+
mz_tol: m/z tolerance for MS2 precursor matching.
|
|
29
|
+
centroid: Whether to centroid the returned spectrum.
|
|
30
|
+
deisotope: Whether to deisotope the returned spectrum.
|
|
31
|
+
dia_stats: Whether to collect DIA-related statistics.
|
|
32
|
+
features: Specific feature UID(s) to process, or None for all.
|
|
33
|
+
mz_tol_ztscan: m/z tolerance for ztscan/DIA file types.
|
|
34
|
+
|
|
35
|
+
Available Methods:
|
|
36
|
+
- validate(param_name, value): Validate a single parameter value
|
|
37
|
+
- validate_all(): Validate all parameters at once
|
|
38
|
+
- to_dict(): Convert parameters to dictionary
|
|
39
|
+
- set_from_dict(param_dict, validate=True): Update multiple parameters from dict
|
|
40
|
+
- set(param_name, value, validate=True): Set parameter value with validation
|
|
41
|
+
- get(param_name): Get parameter value
|
|
42
|
+
- get_description(param_name): Get parameter description
|
|
43
|
+
- get_info(param_name): Get full parameter metadata
|
|
44
|
+
- list_parameters(): Get list of all parameter names
|
|
45
|
+
- get_mz_tolerance(file_type): Get appropriate m/z tolerance based on file type
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
# Core MS2 linking parameters
|
|
49
|
+
mz_tol: float = 0.5
|
|
50
|
+
centroid: bool = True
|
|
51
|
+
deisotope: bool = False
|
|
52
|
+
dia_stats: bool = False
|
|
53
|
+
|
|
54
|
+
# Feature selection parameters - can be None, int, or list of ints
|
|
55
|
+
features: int | list[int] | None = field(default_factory=list)
|
|
56
|
+
|
|
57
|
+
# File type specific adjustments
|
|
58
|
+
mz_tol_ztscan: float = 4.0
|
|
59
|
+
|
|
60
|
+
# Parameter metadata for validation and description
|
|
61
|
+
_param_metadata: dict[str, dict[str, Any]] = field(
|
|
62
|
+
default_factory=lambda: {
|
|
63
|
+
"mz_tol": {
|
|
64
|
+
"dtype": float,
|
|
65
|
+
"description": "m/z tolerance for MS2 precursor matching",
|
|
66
|
+
"min_value": 0.01,
|
|
67
|
+
"max_value": 10.0,
|
|
68
|
+
},
|
|
69
|
+
"centroid": {
|
|
70
|
+
"dtype": bool,
|
|
71
|
+
"description": "Whether to centroid the returned spectrum",
|
|
72
|
+
},
|
|
73
|
+
"deisotope": {
|
|
74
|
+
"dtype": bool,
|
|
75
|
+
"description": "Whether to deisotope the returned spectrum",
|
|
76
|
+
},
|
|
77
|
+
"dia_stats": {
|
|
78
|
+
"dtype": bool,
|
|
79
|
+
"description": "Whether to collect DIA-related statistics",
|
|
80
|
+
},
|
|
81
|
+
"features": {
|
|
82
|
+
"dtype": "Union[int, List[int], None]",
|
|
83
|
+
"description": "Specific feature UID(s) to process, or None for all",
|
|
84
|
+
},
|
|
85
|
+
"mz_tol_ztscan": {
|
|
86
|
+
"dtype": float,
|
|
87
|
+
"description": "m/z tolerance for ztscan/DIA file types",
|
|
88
|
+
"min_value": 0.1,
|
|
89
|
+
"max_value": 20.0,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
def get_info(self, param_name: str) -> dict[str, Any]:
|
|
95
|
+
"""
|
|
96
|
+
Get information about a specific parameter.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
param_name: Name of the parameter
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
Dictionary containing parameter metadata
|
|
103
|
+
|
|
104
|
+
Raises:
|
|
105
|
+
KeyError: If parameter name is not found
|
|
106
|
+
"""
|
|
107
|
+
if param_name not in self._param_metadata:
|
|
108
|
+
raise KeyError(f"Parameter '{param_name}' not found")
|
|
109
|
+
return self._param_metadata[param_name]
|
|
110
|
+
|
|
111
|
+
def get_description(self, param_name: str) -> str:
|
|
112
|
+
"""
|
|
113
|
+
Get description for a specific parameter.
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
param_name: Name of the parameter
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
Parameter description string
|
|
120
|
+
"""
|
|
121
|
+
return str(self.get_info(param_name)["description"])
|
|
122
|
+
|
|
123
|
+
def validate(self, param_name: str, value: Any) -> bool:
|
|
124
|
+
"""
|
|
125
|
+
Validate a parameter value against its constraints.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
param_name: Name of the parameter
|
|
129
|
+
value: Value to validate
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
True if value is valid, False otherwise
|
|
133
|
+
"""
|
|
134
|
+
if param_name not in self._param_metadata:
|
|
135
|
+
return False
|
|
136
|
+
|
|
137
|
+
metadata = self._param_metadata[param_name]
|
|
138
|
+
expected_dtype = metadata["dtype"]
|
|
139
|
+
|
|
140
|
+
# Handle Union types for features parameter
|
|
141
|
+
if param_name == "features":
|
|
142
|
+
if value is None or isinstance(value, (int, list)):
|
|
143
|
+
if isinstance(value, list):
|
|
144
|
+
return all(isinstance(item, int) for item in value)
|
|
145
|
+
return True
|
|
146
|
+
return False
|
|
147
|
+
|
|
148
|
+
# Type checking for non-Union types
|
|
149
|
+
if expected_dtype is float:
|
|
150
|
+
if not isinstance(value, (int, float)):
|
|
151
|
+
try:
|
|
152
|
+
value = float(value)
|
|
153
|
+
except (ValueError, TypeError):
|
|
154
|
+
return False
|
|
155
|
+
elif expected_dtype is bool:
|
|
156
|
+
if not isinstance(value, bool):
|
|
157
|
+
return False
|
|
158
|
+
|
|
159
|
+
# Range checking for numeric values
|
|
160
|
+
if isinstance(value, (int, float)) and value is not None:
|
|
161
|
+
if "min_value" in metadata and value < metadata["min_value"]:
|
|
162
|
+
return False
|
|
163
|
+
if "max_value" in metadata and value > metadata["max_value"]:
|
|
164
|
+
return False
|
|
165
|
+
|
|
166
|
+
return True
|
|
167
|
+
|
|
168
|
+
def set(self, param_name: str, value: Any, validate: bool = True) -> bool:
|
|
169
|
+
"""
|
|
170
|
+
Set a parameter value with optional validation.
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
param_name: Name of the parameter
|
|
174
|
+
value: New value for the parameter
|
|
175
|
+
validate: Whether to validate the value before setting
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
True if parameter was set successfully, False otherwise
|
|
179
|
+
"""
|
|
180
|
+
if not hasattr(self, param_name):
|
|
181
|
+
return False
|
|
182
|
+
|
|
183
|
+
if validate and not self.validate(param_name, value):
|
|
184
|
+
return False
|
|
185
|
+
|
|
186
|
+
# Convert to expected type if needed
|
|
187
|
+
if param_name in self._param_metadata:
|
|
188
|
+
expected_dtype = self._param_metadata[param_name]["dtype"]
|
|
189
|
+
if expected_dtype is float and not isinstance(value, float):
|
|
190
|
+
try:
|
|
191
|
+
value = float(value)
|
|
192
|
+
except (ValueError, TypeError):
|
|
193
|
+
if validate:
|
|
194
|
+
return False
|
|
195
|
+
|
|
196
|
+
setattr(self, param_name, value)
|
|
197
|
+
return True
|
|
198
|
+
|
|
199
|
+
def get(self, param_name: str) -> Any:
|
|
200
|
+
"""
|
|
201
|
+
Get the value of a parameter by name.
|
|
202
|
+
|
|
203
|
+
Args:
|
|
204
|
+
param_name: Name of the parameter
|
|
205
|
+
|
|
206
|
+
Returns:
|
|
207
|
+
Current value of the parameter
|
|
208
|
+
"""
|
|
209
|
+
if not hasattr(self, param_name):
|
|
210
|
+
raise KeyError(f"Parameter '{param_name}' not found")
|
|
211
|
+
return getattr(self, param_name)
|
|
212
|
+
|
|
213
|
+
def set_from_dict(
|
|
214
|
+
self,
|
|
215
|
+
param_dict: dict[str, Any],
|
|
216
|
+
validate: bool = True,
|
|
217
|
+
) -> list[str]:
|
|
218
|
+
"""
|
|
219
|
+
Update multiple parameters from a dictionary.
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
param_dict: Dictionary of parameter names and values
|
|
223
|
+
validate: Whether to validate values before setting
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
List of parameter names that could not be set
|
|
227
|
+
"""
|
|
228
|
+
failed_params = []
|
|
229
|
+
|
|
230
|
+
for param_name, value in param_dict.items():
|
|
231
|
+
if not self.set(param_name, value, validate):
|
|
232
|
+
failed_params.append(param_name)
|
|
233
|
+
|
|
234
|
+
return failed_params
|
|
235
|
+
|
|
236
|
+
def to_dict(self) -> dict[str, Any]:
|
|
237
|
+
"""
|
|
238
|
+
Convert parameters to dictionary, excluding metadata.
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
Dictionary of parameter names and values
|
|
242
|
+
"""
|
|
243
|
+
return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
|
|
244
|
+
|
|
245
|
+
def list_parameters(self) -> list[str]:
|
|
246
|
+
"""
|
|
247
|
+
Get list of all parameter names.
|
|
248
|
+
|
|
249
|
+
Returns:
|
|
250
|
+
List of parameter names
|
|
251
|
+
"""
|
|
252
|
+
return [k for k in self.__dict__.keys() if not k.startswith("_")]
|
|
253
|
+
|
|
254
|
+
def validate_all(self) -> tuple[bool, list[str]]:
|
|
255
|
+
"""
|
|
256
|
+
Validate all parameters in the instance.
|
|
257
|
+
|
|
258
|
+
Returns:
|
|
259
|
+
Tuple of (all_valid, list_of_invalid_params)
|
|
260
|
+
- all_valid: True if all parameters are valid, False otherwise
|
|
261
|
+
- list_of_invalid_params: List of parameter names that failed validation
|
|
262
|
+
"""
|
|
263
|
+
invalid_params = []
|
|
264
|
+
|
|
265
|
+
for param_name in self.list_parameters():
|
|
266
|
+
if param_name in self._param_metadata:
|
|
267
|
+
current_value = getattr(self, param_name)
|
|
268
|
+
if not self.validate(param_name, current_value):
|
|
269
|
+
invalid_params.append(param_name)
|
|
270
|
+
|
|
271
|
+
return len(invalid_params) == 0, invalid_params
|
|
272
|
+
|
|
273
|
+
def get_mz_tolerance(self, file_type=None):
|
|
274
|
+
"""
|
|
275
|
+
Get the appropriate m/z tolerance based on file type.
|
|
276
|
+
|
|
277
|
+
Args:
|
|
278
|
+
file_type (str, optional): File type ('ztscan', 'dia', or other)
|
|
279
|
+
|
|
280
|
+
Returns:
|
|
281
|
+
float: Appropriate m/z tolerance value
|
|
282
|
+
"""
|
|
283
|
+
if file_type is not None and file_type.lower() in ["ztscan", "dia"]:
|
|
284
|
+
return self.get("mz_tol_ztscan")
|
|
285
|
+
return self.get("mz_tol")
|