Skip to content

Commit 62bb10a

Browse files
Addition of cache removal classmethod
1 parent 0dfd57e commit 62bb10a

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

mumble/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__version__ = "0.2.0"
22
__all__ = ["PSMHandler"]
33

4-
from mumble.mumble import PSMHandler
4+
from mumble.mumble import PSMHandler, _ModificationCache

mumble/__main__.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import click
22
import logging
3+
import sys
34

45
from rich.logging import RichHandler
56

6-
from mumble import PSMHandler
7+
from mumble import PSMHandler, _ModificationCache
78

89

910
# setup logging
@@ -96,11 +97,23 @@
9697

9798

9899
@click.command("cli", context_settings={"show_default": True})
99-
@click.argument("input_file", type=click.Path(exists=True), default=None)
100-
def main(**kwargs):
100+
@click.argument("input_file", type=click.Path(exists=True), default=None, required=False)
101+
@click.option(
102+
"--clear-cache/--no-clear-cache",
103+
is_flag=True,
104+
default=False,
105+
help="Remove the modification cache file and exit early.",
106+
)
107+
def main(clear_cache, **kwargs):
101108
"""
102109
Finding the perfect match for your mass shift.
103110
"""
111+
# if the user just wants to clear the cache, do it and quit
112+
if clear_cache:
113+
_ModificationCache._remove_cache()
114+
logging.info("Exiting Mumble. You will find your match another time.")
115+
sys.exit(0)
116+
104117
# Set the logging level based on the CLI option
105118
log_level = kwargs.get("log_level", "INFO").upper()
106119
logging.getLogger().setLevel(log_level)

mumble/mumble.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ def __init__(
417417
exclude_mutations=exclude_mutations,
418418
modification_file=unimod_modification_file,
419419
)
420+
self.cache.load_cache()
420421

421422
self.modification_df = self.cache.modification_df
422423
self.monoisotopic_masses = self.cache.monoisotopic_masses
@@ -780,11 +781,32 @@ def __init__(
780781
self.modifications_names = []
781782
self.modification_df = None
782783

783-
# Load or generate data
784-
cache_file = self._get_cache_file_path()
785-
self._load_or_generate_data(cache_file, force_reload=False)
784+
# get cache file path
785+
self.cache_file = self._get_cache_file_path()
786786

787-
def _get_cache_file_path(self):
787+
@classmethod
788+
def _remove_cache(cls):
789+
"""
790+
Remove the cache file for modifications.
791+
"""
792+
cache_file = cls._get_cache_file_path()
793+
if os.path.exists(cache_file):
794+
os.remove(cache_file)
795+
logger.info("Modification cache removed.")
796+
else:
797+
logger.warning("Modification cache file does not exist.")
798+
799+
def load_cache(self, force_reload=False):
800+
"""
801+
Load the cache or generate it if it doesn't exist.
802+
803+
Args:
804+
force_reload (bool, optional): If True, regenerate the cache even if it exists. Defaults to False.
805+
"""
806+
self._load_or_generate_data(self.cache_file, force_reload=force_reload)
807+
808+
@classmethod
809+
def _get_cache_file_path(cls):
788810
"""
789811
Get path to cache file for combinations of modifications.
790812
@@ -828,16 +850,16 @@ def _load_or_generate_data(self, cache_file: str, force_reload: bool = False) ->
828850
if cache_data["metadata"] == (
829851
self.combination_length,
830852
self.exclude_mutations,
831-
self.modification_file,
832853
self.modification_file_hash,
833854
):
855+
logger.debug("Cache metadata matches current configuration")
834856
try:
835-
logger.info("Loading cache data")
857+
logger.info("Using cached modifcation data")
836858
self.modification_df = cache_data["modification_df"]
837859
self.monoisotopic_masses = cache_data["monoisotopic_masses"]
838860
self.modifications_names = cache_data["modifications_names"]
839861
except KeyError:
840-
logger.info("Cache data missing")
862+
logger.info("Cached data invalid or incomplete, regenerating cache")
841863
self._regenerate_and_save_cache(cache_file)
842864
else:
843865
self._regenerate_and_save_cache(cache_file)
@@ -986,15 +1008,14 @@ def _regenerate_and_save_cache(self, cache_file: str) -> None:
9861008
self._generate_modifications_combinations_lists(self.combination_length)
9871009
)
9881010
logger.debug(
989-
f"New cache metadata: {self.combination_length}, {self.exclude_mutations}, {self.modification_file}, {self.modification_file_hash}"
1011+
f"New cache metadata: \ncombination length {self.combination_length}, \nexclude_mutations {self.exclude_mutations},\nmodification file hash {self.modification_file_hash}",
9901012
)
9911013
with open(cache_file, "wb") as f:
9921014
pickle.dump(
9931015
{
9941016
"metadata": (
9951017
self.combination_length,
9961018
self.exclude_mutations,
997-
self.modification_file,
9981019
self.modification_file_hash,
9991020
),
10001021
"modification_df": self.modification_df,

0 commit comments

Comments
 (0)