diff --git a/RIAssigner/data/Data.py b/RIAssigner/data/Data.py index c373e3c..e4d1513 100644 --- a/RIAssigner/data/Data.py +++ b/RIAssigner/data/Data.py @@ -118,6 +118,51 @@ def retention_indices(self, value: Iterable[RetentionIndexType]): """ ... + @property + @abstractmethod + def has_retention_indices(self) -> Iterable[RetentionIndexType]: + """Getter for `has_retention_indices` property.""" + ... + + def check_ri_values(self) -> bool: + """ + Check if all retention indices in the spectra exist. + + This method iterates over the retention indices in the spectra. If it encounters a value that is None, + it immediately returns False. If it iterates over all retention indices without finding a None value, + it returns True. + + Returns: + bool: True if all retention indices exist, False otherwise. + """ + for value in self.has_retention_indices: + if value is None: + return False + return True + + @property + @abstractmethod + def has_retention_times(self) -> Iterable[RetentionTimeType]: + """Getter for `has_retention_times` property.""" + ... + + def check_rt_values(self) -> bool: + """ + Check if all retention times in the spectra exist. + + This method iterates over the retention times in the spectra. If it encounters a value that is None, + it immediately returns False. If it iterates over all retention times without finding a None value, + it returns True. + + Returns: + bool: True if all retention times exist, False otherwise. + """ + for value in self.has_retention_times: + if value is None: + return False + return True + + @property @abstractmethod def comment(self) -> Iterable[CommentFieldType]: diff --git a/RIAssigner/data/MatchMSData.py b/RIAssigner/data/MatchMSData.py index 0193c7d..210f5ec 100644 --- a/RIAssigner/data/MatchMSData.py +++ b/RIAssigner/data/MatchMSData.py @@ -123,6 +123,26 @@ def comment(self) -> Iterable[Data.CommentFieldType]: content = [spectrum.get(self.comment_keys, default=None) for spectrum in self._spectra] return content + @property + def has_retention_indices(self) -> Iterable[Data.RetentionIndexType]: + """ Check if retention indices exist in spectra.""" + for spectrum in self._spectra: + ri = spectrum.get(self._ri_key, default=None) + if ri is None: + raise ValueError("Retention index does not exist in spectrum") + return self._spectra + + @property + def has_retention_times(self) -> Iterable[Data.RetentionTimeType]: + """ Check if retention times exist in spectra.""" + for spectrum in self._spectra: + ri = spectrum.get(self._rt_key, default=None) + if ri is None: + raise ValueError("Retention time does not exist in spectrum") + return self._spectra + + + def safe_read_key(spectrum: Spectrum, key: str) -> Optional[float]: """ Read key from spectrum and convert to float or return 'None'. Tries to read the given key from the spectrum metadata and convert it to a float. diff --git a/RIAssigner/data/PandasData.py b/RIAssigner/data/PandasData.py index ec68bc3..fbd1bd9 100644 --- a/RIAssigner/data/PandasData.py +++ b/RIAssigner/data/PandasData.py @@ -13,6 +13,7 @@ class PandasData(Data): def __init__(self, filename: str, filetype: str, rt_unit: str): super().__init__(filename, filetype, rt_unit) self._read() + self._rt_key = 'rt' def _read(self): """ Load content from file into PandasData object. """ @@ -126,5 +127,16 @@ def comment(self) -> Iterable[Data.CommentFieldType]: """ Get comments.""" self._comment_keys = "comment" content = self._data[self._comment_keys].tolist() - return content - \ No newline at end of file + return content + + @property + def has_retention_indices(self) -> Iterable[Data.RetentionIndexType]: + pass + + @property + def has_retention_times(self) -> Iterable[Data.RetentionTimeType]: + """ Check if retention times exist in data.""" + rt = self._data[self._rt_key].tolist() + if rt is None: + raise ValueError("Retention time does not exist in data") + return self._data \ No newline at end of file