Coverage for mddb_workflow/tools/fix_gromacs_masses.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-29 15:48 +0000

1from os import environ 

2from pathlib import Path 

3 

4from mddb_workflow.utils.file import File 

5 

6# Replace the default gromacs masses file (atommass.dat) by our custom masses file 

7# This file includes relevant atoms also in all caps letters 

8# e.g. Zn and ZN 

9# This way we avoid gormacs not finding atoms because names are in caps 

10 

11# LORE: This was done with a symlink before 

12# In the gromacs data directory from the conda enviornment we created a relative symlink to the workflow atommass.dat 

13# This worked fine until we had a very specific issue with a cluster (gpucluster at IRB Barcelona) 

14# In this cluster there was two different paths to reach the home directory: /home/username and /orozco/homes/username 

15# The python __file__ values were using the /orozco/homes/username root despite we were calling it from /home/username 

16# Thus relative paths were all passing through the root and the different number of jumps was breaking the paths 

17# I spent half a day trying to get __file__ values using the /home/username root and I did not succeed 

18# Then we decided to just write the masses file in the conda enviornment every time 

19 

20# LORE: This was done by modifying the conda enviornment before 

21# However this lead to problems when containerizing the environment 

22# In one hand, there was no CONDA_PREFIX environmental variable 

23# But the real problem was that writting in the enviornment was not allowed 

24# Otherwise the container could not be shared between different user in a cluster 

25 

26# Set the absolute path to the workflow resources directory 

27resources = str(Path(__file__).parent.parent / "resources") 

28 

29# Replace the original file by a symlink to our custom file if it is not done yet 

30def fix_gromacs_masses (): 

31 

32 # Set the source file 

33 # WARNING: Note that this must be done here, not outside the function 

34 # Otherwise a workflow called with a '-dir' parameter would have a wrong relative path 

35 source_custom_masses_file = File(resources + '/atommass.dat') 

36 

37 # Set the path to a local copy of the workflow custom masses file 

38 # According to Justin Lemkul: 

39 # "All GROMACS programs will read relevant database files from the working directory  

40 # before referencing them from $GMXLIB." 

41 local_custom_masses_file = File('atommass.dat') 

42 

43 # Check if the backup file exists and, if not, then rename the current masses file as the backup 

44 if not local_custom_masses_file.exists: 

45 # If it does not exists then it means it is a symlink pointing to a not valid direction 

46 # This may happend when working in different machines 

47 # Simply remove the old symlink 

48 if local_custom_masses_file.is_symlink(): local_custom_masses_file.remove() 

49 local_custom_masses_file.set_symlink_to(source_custom_masses_file)