Skip to content

Commit

Permalink
added has_retention_indices & has_retention_times functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
wverastegui committed Jan 11, 2024
1 parent 6a93e36 commit 25282c0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
45 changes: 45 additions & 0 deletions RIAssigner/data/Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
20 changes: 20 additions & 0 deletions RIAssigner/data/MatchMSData.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions RIAssigner/data/PandasData.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand Down Expand Up @@ -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

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

0 comments on commit 25282c0

Please sign in to comment.