|
13 | 13 |
|
14 | 14 | logging.basicConfig(format="%(asctime)s [%(funcName)s] - %(message)s", level=logging.DEBUG) |
15 | 15 | 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 |
17 | 37 |
|
18 | 38 | # Met-loss modification constant (UniMod:765) with mass shift and site specification |
19 | 39 | MET_LOSS_MODIFICATION = "UniMod:765,-131.040485,*nM" |
@@ -68,7 +88,7 @@ def get_mod(mod, mod_type): |
68 | 88 | modification_found = 0 |
69 | 89 | diann_mod_accession = None |
70 | 90 | diann_mod_name = None |
71 | | - for modification in unimod_database.modifications: |
| 91 | + for modification in get_unimod_database().modifications: |
72 | 92 | if modification.get_name() == mod.split(" ")[0]: |
73 | 93 | diann_mod_accession = modification.get_accession().replace("UNIMOD:", "UniMod:") + "," + str(modification._delta_mono_mass) |
74 | 94 | diann_mod_name = modification.get_name() |
|
0 commit comments