@@ -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: \n combination length { self .combination_length } , \n exclude_mutations { self .exclude_mutations } ,\n modification 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