Skip to content

Commit 372dbeb

Browse files
authored
Merge pull request #63 from bigbio/copilot/sub-pr-59-another-one
Implement lazy initialization for UnimodDatabase
2 parents 465f606 + e5310ef commit 372dbeb

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

quantmsutils/diann/dianncfg.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,27 @@
1313

1414
logging.basicConfig(format="%(asctime)s [%(funcName)s] - %(message)s", level=logging.DEBUG)
1515
logger = logging.getLogger(__name__)
16-
unimod_database = UnimodDatabase()
16+
17+
# Lazy initialization of UnimodDatabase for improved testability.
18+
# The database is created on first access rather than at module import time,
19+
# which allows tests to mock or replace it more easily.
20+
_unimod_database = None
21+
22+
23+
def get_unimod_database():
24+
"""
25+
Get the UnimodDatabase instance, creating it lazily on first access.
26+
27+
This pattern improves testability by avoiding database initialization at module
28+
import time. For testing purposes, the internal _unimod_database variable can be
29+
set to None to force re-initialization on the next call.
30+
31+
:return: The UnimodDatabase instance.
32+
"""
33+
global _unimod_database
34+
if _unimod_database is None:
35+
_unimod_database = UnimodDatabase()
36+
return _unimod_database
1737

1838
# Met-loss modification constant (UniMod:765) with mass shift and site specification
1939
MET_LOSS_MODIFICATION = "UniMod:765,-131.040485,*nM"
@@ -68,7 +88,7 @@ def get_mod(mod, mod_type):
6888
modification_found = 0
6989
diann_mod_accession = None
7090
diann_mod_name = None
71-
for modification in unimod_database.modifications:
91+
for modification in get_unimod_database().modifications:
7292
if modification.get_name() == mod.split(" ")[0]:
7393
diann_mod_accession = modification.get_accession().replace("UNIMOD:", "UniMod:") + "," + str(modification._delta_mono_mass)
7494
diann_mod_name = modification.get_name()

0 commit comments

Comments
 (0)