corems.mass_spectra.input.parserbase
1from abc import ABC, abstractmethod 2import datetime 3import numbers 4from typing import Optional, Union, List, Tuple 5 6 7class SpectraParserInterface(ABC): 8 """ 9 Interface for parsing mass spectra data into MassSpectraBase objects. 10 11 Methods 12 ------- 13 * load(). 14 Load mass spectra data. 15 * run(). 16 Parse mass spectra data. 17 * get_mass_spectra_obj(). 18 Return MassSpectraBase object with several attributes populated 19 * get_mass_spectrum_from_scan(scan_number). 20 Return MassSpecBase data object from scan number. 21 * get_scans_in_time_range(time_range). 22 Return scan numbers within specified retention time range(s). 23 24 Notes 25 ----- 26 This is an abstract class and should not be instantiated directly. 27 28 Time Range Filtering 29 -------------------- 30 Many methods support optional time_range parameter to load only scans within 31 specific retention time windows. This significantly improves performance for 32 targeted workflows. Time ranges can be specified as: 33 - Single range: (start_time, end_time) in minutes 34 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 35 """ 36 37 @abstractmethod 38 def load(self): 39 """ 40 Load mass spectra data. 41 """ 42 pass 43 44 @abstractmethod 45 def run(self): 46 """ 47 Parse mass spectra data. 48 """ 49 pass 50 51 @abstractmethod 52 def get_scan_df(self, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 53 """ 54 Return scan data as a pandas DataFrame. 55 56 Parameters 57 ---------- 58 time_range : tuple or list of tuples, optional 59 Retention time range(s) to filter scans. Can be: 60 - Single range: (start_time, end_time) in minutes 61 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 62 If None, returns all scans. 63 64 Returns 65 ------- 66 pd.DataFrame 67 DataFrame containing scan information, optionally filtered by time range. 68 """ 69 pass 70 71 @abstractmethod 72 def get_ms_raw(self, spectra, scan_df, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 73 """ 74 Return a dictionary of mass spectra data as pandas DataFrames. 75 76 Parameters 77 ---------- 78 spectra : str or dict 79 Specifies which spectra to load (e.g., 'ms1', 'ms2', or custom dict) 80 scan_df : pd.DataFrame 81 Scan information DataFrame 82 time_range : tuple or list of tuples, optional 83 Retention time range(s) to filter scans. Can be: 84 - Single range: (start_time, end_time) in minutes 85 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 86 If None, returns all scans. 87 88 Returns 89 ------- 90 dict 91 Dictionary of raw mass spectra data, optionally filtered by time range. 92 """ 93 pass 94 95 @abstractmethod 96 def get_mass_spectra_obj(self, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 97 """ 98 Return mass spectra data object. 99 100 Parameters 101 ---------- 102 time_range : tuple or list of tuples, optional 103 Retention time range(s) to load. Can be: 104 - Single range: (start_time, end_time) in minutes 105 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 106 If None, loads all scans. Useful for targeted workflows to improve performance. 107 108 Returns 109 ------- 110 MassSpectraBase 111 Mass spectra object, optionally filtered to specified time range(s). 112 """ 113 pass 114 115 @abstractmethod 116 def get_mass_spectrum_from_scan( 117 self, scan_number, spectrum_mode, auto_process=True 118 ): 119 """ 120 Return mass spectrum data object from scan number. 121 """ 122 pass 123 124 @abstractmethod 125 def get_mass_spectra_from_scan_list( 126 self, scan_list, spectrum_mode, auto_process=True 127 ): 128 """ 129 Return a list of mass spectrum data objects from a list of scan numbers. 130 """ 131 pass 132 133 @abstractmethod 134 def get_scans_in_time_range( 135 self, 136 time_range: Union[Tuple[float, float], List[Tuple[float, float]]], 137 ms_level: Optional[int] = None 138 ) -> List[int]: 139 """ 140 Return scan numbers within specified retention time range(s). 141 142 This method provides efficient filtering of scans by retention time, 143 which is particularly useful for targeted workflows where only specific 144 time windows are of interest. 145 146 Parameters 147 ---------- 148 time_range : tuple or list of tuples 149 Retention time range(s) in minutes. Can be: 150 - Single range: (start_time, end_time) 151 - Multiple ranges: [(start1, end1), (start2, end2), ...] 152 ms_level : int, optional 153 If specified, only return scans of this MS level (e.g., 1 for MS1, 2 for MS2). 154 If None, returns scans of all MS levels. 155 156 Returns 157 ------- 158 list of int 159 List of scan numbers within the specified time range(s) and MS level. 160 161 Examples 162 -------- 163 Get MS1 scans between 1.0 and 2.0 minutes: 164 165 >>> scans = parser.get_scans_in_time_range((1.0, 2.0), ms_level=1) 166 167 Get scans in multiple time windows: 168 169 >>> scans = parser.get_scans_in_time_range([(0.5, 1.5), (3.0, 4.0)]) 170 """ 171 pass 172 173 @abstractmethod 174 def get_instrument_info(self): 175 """ 176 Return instrument information. 177 178 Returns 179 ------- 180 dict 181 A dictionary with the keys 'model', and 'serial_number'. 182 """ 183 pass 184 185 @abstractmethod 186 def get_creation_time(self) -> datetime.datetime: 187 """ 188 Return the creation time of the mass spectra data. 189 190 Returns 191 ------- 192 datetime.datetime 193 The creation time of the mass spectra data. 194 """ 195 pass 196 197 @staticmethod 198 def _normalize_time_range( 199 time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] 200 ) -> Optional[List[Tuple[float, float]]]: 201 """ 202 Normalize time range input to a list of tuples. 203 204 Helper method for implementations to standardize time_range parameter. 205 Converts single tuple to list of tuples for consistent handling. 206 207 Parameters 208 ---------- 209 time_range : tuple, list of tuples, or None 210 Input time range(s) 211 212 Returns 213 ------- 214 list of tuples or None 215 Normalized time ranges as list of (start, end) tuples, or None if input is None. 216 217 Examples 218 -------- 219 >>> SpectraParserInterface._normalize_time_range((1.0, 2.0)) 220 [(1.0, 2.0)] 221 222 >>> SpectraParserInterface._normalize_time_range([(1.0, 2.0), (3.0, 4.0)]) 223 [(1.0, 2.0), (3.0, 4.0)] 224 225 >>> SpectraParserInterface._normalize_time_range(None) 226 None 227 """ 228 if time_range is None: 229 return None 230 231 # Check if it's a single tuple (two numbers) 232 if isinstance(time_range, tuple) and len(time_range) == 2: 233 # Use numbers.Number to catch int, float, and numpy scalar types 234 if isinstance(time_range[0], numbers.Number) and isinstance(time_range[1], numbers.Number): 235 # Convert to float to ensure consistency (handles numpy scalars) 236 return [(float(time_range[0]), float(time_range[1]))] 237 238 # Otherwise assume it's already a list of tuples 239 return list(time_range)
8class SpectraParserInterface(ABC): 9 """ 10 Interface for parsing mass spectra data into MassSpectraBase objects. 11 12 Methods 13 ------- 14 * load(). 15 Load mass spectra data. 16 * run(). 17 Parse mass spectra data. 18 * get_mass_spectra_obj(). 19 Return MassSpectraBase object with several attributes populated 20 * get_mass_spectrum_from_scan(scan_number). 21 Return MassSpecBase data object from scan number. 22 * get_scans_in_time_range(time_range). 23 Return scan numbers within specified retention time range(s). 24 25 Notes 26 ----- 27 This is an abstract class and should not be instantiated directly. 28 29 Time Range Filtering 30 -------------------- 31 Many methods support optional time_range parameter to load only scans within 32 specific retention time windows. This significantly improves performance for 33 targeted workflows. Time ranges can be specified as: 34 - Single range: (start_time, end_time) in minutes 35 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 36 """ 37 38 @abstractmethod 39 def load(self): 40 """ 41 Load mass spectra data. 42 """ 43 pass 44 45 @abstractmethod 46 def run(self): 47 """ 48 Parse mass spectra data. 49 """ 50 pass 51 52 @abstractmethod 53 def get_scan_df(self, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 54 """ 55 Return scan data as a pandas DataFrame. 56 57 Parameters 58 ---------- 59 time_range : tuple or list of tuples, optional 60 Retention time range(s) to filter scans. Can be: 61 - Single range: (start_time, end_time) in minutes 62 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 63 If None, returns all scans. 64 65 Returns 66 ------- 67 pd.DataFrame 68 DataFrame containing scan information, optionally filtered by time range. 69 """ 70 pass 71 72 @abstractmethod 73 def get_ms_raw(self, spectra, scan_df, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 74 """ 75 Return a dictionary of mass spectra data as pandas DataFrames. 76 77 Parameters 78 ---------- 79 spectra : str or dict 80 Specifies which spectra to load (e.g., 'ms1', 'ms2', or custom dict) 81 scan_df : pd.DataFrame 82 Scan information DataFrame 83 time_range : tuple or list of tuples, optional 84 Retention time range(s) to filter scans. Can be: 85 - Single range: (start_time, end_time) in minutes 86 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 87 If None, returns all scans. 88 89 Returns 90 ------- 91 dict 92 Dictionary of raw mass spectra data, optionally filtered by time range. 93 """ 94 pass 95 96 @abstractmethod 97 def get_mass_spectra_obj(self, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 98 """ 99 Return mass spectra data object. 100 101 Parameters 102 ---------- 103 time_range : tuple or list of tuples, optional 104 Retention time range(s) to load. Can be: 105 - Single range: (start_time, end_time) in minutes 106 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 107 If None, loads all scans. Useful for targeted workflows to improve performance. 108 109 Returns 110 ------- 111 MassSpectraBase 112 Mass spectra object, optionally filtered to specified time range(s). 113 """ 114 pass 115 116 @abstractmethod 117 def get_mass_spectrum_from_scan( 118 self, scan_number, spectrum_mode, auto_process=True 119 ): 120 """ 121 Return mass spectrum data object from scan number. 122 """ 123 pass 124 125 @abstractmethod 126 def get_mass_spectra_from_scan_list( 127 self, scan_list, spectrum_mode, auto_process=True 128 ): 129 """ 130 Return a list of mass spectrum data objects from a list of scan numbers. 131 """ 132 pass 133 134 @abstractmethod 135 def get_scans_in_time_range( 136 self, 137 time_range: Union[Tuple[float, float], List[Tuple[float, float]]], 138 ms_level: Optional[int] = None 139 ) -> List[int]: 140 """ 141 Return scan numbers within specified retention time range(s). 142 143 This method provides efficient filtering of scans by retention time, 144 which is particularly useful for targeted workflows where only specific 145 time windows are of interest. 146 147 Parameters 148 ---------- 149 time_range : tuple or list of tuples 150 Retention time range(s) in minutes. Can be: 151 - Single range: (start_time, end_time) 152 - Multiple ranges: [(start1, end1), (start2, end2), ...] 153 ms_level : int, optional 154 If specified, only return scans of this MS level (e.g., 1 for MS1, 2 for MS2). 155 If None, returns scans of all MS levels. 156 157 Returns 158 ------- 159 list of int 160 List of scan numbers within the specified time range(s) and MS level. 161 162 Examples 163 -------- 164 Get MS1 scans between 1.0 and 2.0 minutes: 165 166 >>> scans = parser.get_scans_in_time_range((1.0, 2.0), ms_level=1) 167 168 Get scans in multiple time windows: 169 170 >>> scans = parser.get_scans_in_time_range([(0.5, 1.5), (3.0, 4.0)]) 171 """ 172 pass 173 174 @abstractmethod 175 def get_instrument_info(self): 176 """ 177 Return instrument information. 178 179 Returns 180 ------- 181 dict 182 A dictionary with the keys 'model', and 'serial_number'. 183 """ 184 pass 185 186 @abstractmethod 187 def get_creation_time(self) -> datetime.datetime: 188 """ 189 Return the creation time of the mass spectra data. 190 191 Returns 192 ------- 193 datetime.datetime 194 The creation time of the mass spectra data. 195 """ 196 pass 197 198 @staticmethod 199 def _normalize_time_range( 200 time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] 201 ) -> Optional[List[Tuple[float, float]]]: 202 """ 203 Normalize time range input to a list of tuples. 204 205 Helper method for implementations to standardize time_range parameter. 206 Converts single tuple to list of tuples for consistent handling. 207 208 Parameters 209 ---------- 210 time_range : tuple, list of tuples, or None 211 Input time range(s) 212 213 Returns 214 ------- 215 list of tuples or None 216 Normalized time ranges as list of (start, end) tuples, or None if input is None. 217 218 Examples 219 -------- 220 >>> SpectraParserInterface._normalize_time_range((1.0, 2.0)) 221 [(1.0, 2.0)] 222 223 >>> SpectraParserInterface._normalize_time_range([(1.0, 2.0), (3.0, 4.0)]) 224 [(1.0, 2.0), (3.0, 4.0)] 225 226 >>> SpectraParserInterface._normalize_time_range(None) 227 None 228 """ 229 if time_range is None: 230 return None 231 232 # Check if it's a single tuple (two numbers) 233 if isinstance(time_range, tuple) and len(time_range) == 2: 234 # Use numbers.Number to catch int, float, and numpy scalar types 235 if isinstance(time_range[0], numbers.Number) and isinstance(time_range[1], numbers.Number): 236 # Convert to float to ensure consistency (handles numpy scalars) 237 return [(float(time_range[0]), float(time_range[1]))] 238 239 # Otherwise assume it's already a list of tuples 240 return list(time_range)
Interface for parsing mass spectra data into MassSpectraBase objects.
Methods
- load(). Load mass spectra data.
- run(). Parse mass spectra data.
- get_mass_spectra_obj(). Return MassSpectraBase object with several attributes populated
- get_mass_spectrum_from_scan(scan_number). Return MassSpecBase data object from scan number.
- get_scans_in_time_range(time_range). Return scan numbers within specified retention time range(s).
Notes
This is an abstract class and should not be instantiated directly.
Time Range Filtering
Many methods support optional time_range parameter to load only scans within specific retention time windows. This significantly improves performance for targeted workflows. Time ranges can be specified as:
- Single range: (start_time, end_time) in minutes
- Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes
52 @abstractmethod 53 def get_scan_df(self, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 54 """ 55 Return scan data as a pandas DataFrame. 56 57 Parameters 58 ---------- 59 time_range : tuple or list of tuples, optional 60 Retention time range(s) to filter scans. Can be: 61 - Single range: (start_time, end_time) in minutes 62 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 63 If None, returns all scans. 64 65 Returns 66 ------- 67 pd.DataFrame 68 DataFrame containing scan information, optionally filtered by time range. 69 """ 70 pass
Return scan data as a pandas DataFrame.
Parameters
- time_range (tuple or list of tuples, optional):
Retention time range(s) to filter scans. Can be:
- Single range: (start_time, end_time) in minutes
- Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes If None, returns all scans.
Returns
- pd.DataFrame: DataFrame containing scan information, optionally filtered by time range.
72 @abstractmethod 73 def get_ms_raw(self, spectra, scan_df, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 74 """ 75 Return a dictionary of mass spectra data as pandas DataFrames. 76 77 Parameters 78 ---------- 79 spectra : str or dict 80 Specifies which spectra to load (e.g., 'ms1', 'ms2', or custom dict) 81 scan_df : pd.DataFrame 82 Scan information DataFrame 83 time_range : tuple or list of tuples, optional 84 Retention time range(s) to filter scans. Can be: 85 - Single range: (start_time, end_time) in minutes 86 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 87 If None, returns all scans. 88 89 Returns 90 ------- 91 dict 92 Dictionary of raw mass spectra data, optionally filtered by time range. 93 """ 94 pass
Return a dictionary of mass spectra data as pandas DataFrames.
Parameters
- spectra (str or dict): Specifies which spectra to load (e.g., 'ms1', 'ms2', or custom dict)
- scan_df (pd.DataFrame): Scan information DataFrame
- time_range (tuple or list of tuples, optional):
Retention time range(s) to filter scans. Can be:
- Single range: (start_time, end_time) in minutes
- Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes If None, returns all scans.
Returns
- dict: Dictionary of raw mass spectra data, optionally filtered by time range.
96 @abstractmethod 97 def get_mass_spectra_obj(self, time_range: Optional[Union[Tuple[float, float], List[Tuple[float, float]]]] = None): 98 """ 99 Return mass spectra data object. 100 101 Parameters 102 ---------- 103 time_range : tuple or list of tuples, optional 104 Retention time range(s) to load. Can be: 105 - Single range: (start_time, end_time) in minutes 106 - Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes 107 If None, loads all scans. Useful for targeted workflows to improve performance. 108 109 Returns 110 ------- 111 MassSpectraBase 112 Mass spectra object, optionally filtered to specified time range(s). 113 """ 114 pass
Return mass spectra data object.
Parameters
- time_range (tuple or list of tuples, optional):
Retention time range(s) to load. Can be:
- Single range: (start_time, end_time) in minutes
- Multiple ranges: [(start1, end1), (start2, end2), ...] in minutes If None, loads all scans. Useful for targeted workflows to improve performance.
Returns
- MassSpectraBase: Mass spectra object, optionally filtered to specified time range(s).
116 @abstractmethod 117 def get_mass_spectrum_from_scan( 118 self, scan_number, spectrum_mode, auto_process=True 119 ): 120 """ 121 Return mass spectrum data object from scan number. 122 """ 123 pass
Return mass spectrum data object from scan number.
125 @abstractmethod 126 def get_mass_spectra_from_scan_list( 127 self, scan_list, spectrum_mode, auto_process=True 128 ): 129 """ 130 Return a list of mass spectrum data objects from a list of scan numbers. 131 """ 132 pass
Return a list of mass spectrum data objects from a list of scan numbers.
134 @abstractmethod 135 def get_scans_in_time_range( 136 self, 137 time_range: Union[Tuple[float, float], List[Tuple[float, float]]], 138 ms_level: Optional[int] = None 139 ) -> List[int]: 140 """ 141 Return scan numbers within specified retention time range(s). 142 143 This method provides efficient filtering of scans by retention time, 144 which is particularly useful for targeted workflows where only specific 145 time windows are of interest. 146 147 Parameters 148 ---------- 149 time_range : tuple or list of tuples 150 Retention time range(s) in minutes. Can be: 151 - Single range: (start_time, end_time) 152 - Multiple ranges: [(start1, end1), (start2, end2), ...] 153 ms_level : int, optional 154 If specified, only return scans of this MS level (e.g., 1 for MS1, 2 for MS2). 155 If None, returns scans of all MS levels. 156 157 Returns 158 ------- 159 list of int 160 List of scan numbers within the specified time range(s) and MS level. 161 162 Examples 163 -------- 164 Get MS1 scans between 1.0 and 2.0 minutes: 165 166 >>> scans = parser.get_scans_in_time_range((1.0, 2.0), ms_level=1) 167 168 Get scans in multiple time windows: 169 170 >>> scans = parser.get_scans_in_time_range([(0.5, 1.5), (3.0, 4.0)]) 171 """ 172 pass
Return scan numbers within specified retention time range(s).
This method provides efficient filtering of scans by retention time, which is particularly useful for targeted workflows where only specific time windows are of interest.
Parameters
- time_range (tuple or list of tuples):
Retention time range(s) in minutes. Can be:
- Single range: (start_time, end_time)
- Multiple ranges: [(start1, end1), (start2, end2), ...]
- ms_level (int, optional): If specified, only return scans of this MS level (e.g., 1 for MS1, 2 for MS2). If None, returns scans of all MS levels.
Returns
- list of int: List of scan numbers within the specified time range(s) and MS level.
Examples
Get MS1 scans between 1.0 and 2.0 minutes:
>>> scans = parser.get_scans_in_time_range((1.0, 2.0), ms_level=1)
Get scans in multiple time windows:
>>> scans = parser.get_scans_in_time_range([(0.5, 1.5), (3.0, 4.0)])
174 @abstractmethod 175 def get_instrument_info(self): 176 """ 177 Return instrument information. 178 179 Returns 180 ------- 181 dict 182 A dictionary with the keys 'model', and 'serial_number'. 183 """ 184 pass
Return instrument information.
Returns
- dict: A dictionary with the keys 'model', and 'serial_number'.
186 @abstractmethod 187 def get_creation_time(self) -> datetime.datetime: 188 """ 189 Return the creation time of the mass spectra data. 190 191 Returns 192 ------- 193 datetime.datetime 194 The creation time of the mass spectra data. 195 """ 196 pass
Return the creation time of the mass spectra data.
Returns
- datetime.datetime: The creation time of the mass spectra data.