Coverage for biobb_gromacs/gromacs/convert_tpr.py: 89%

54 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-05-28 06:50 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the Convert_tpr class and the command line interface.""" 

4from pathlib import PurePath 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_gromacs.gromacs.common import get_gromacs_version 

9 

10 

11class ConvertTpr(BiobbObject): 

12 """ 

13 | biobb_gromacs ConvertTpr 

14 | Wrapper of the `GROMACS convert-tpr <https://manual.gromacs.org/current/onlinehelp/gmx-convert-tpr.html>`_ module. 

15 | The GROMACS convert-tpr module can edit run input files (.tpr) 

16 

17 Args: 

18 input_tpr_path (str): Path to the input portable binary run file TPR. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_grompp.tpr>`_. Accepted formats: tpr (edam:format_2333). 

19 output_tpr_path (str): Path to the output portable binary run file TPR. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_grompp.tpr>`_. Accepted formats: tpr (edam:format_2333). 

20 properties (dict - Python dictionary object containing the tool parameters, not input/output files): 

21 * **extend** (*int*) - (0) Extend the runtime by this amount (ps). 

22 * **until** (*int*) - (0) Extend the runtime until this ending time (ps). 

23 * **nsteps** (*int*) - (0) Change the number of steps remaining to be made. 

24 * **gmx_lib** (*str*) - (None) Path set GROMACS GMXLIB environment variable. 

25 * **binary_path** (*str*) - ("gmx") Path to the GROMACS executable binary. 

26 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

27 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

28 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

29 * **container_path** (*str*) - (None) Path to the binary executable of your container. 

30 * **container_image** (*str*) - ("gromacs/gromacs:latest") Container Image identifier. 

31 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container. 

32 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container. 

33 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container. 

34 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell. 

35 

36 Examples: 

37 This is a use example of how to use the building block from Python:: 

38 

39 from biobb_gromacs.gromacs.convert_tpr import convert_tpr 

40 

41 prop = { 'extend': 100000} 

42 convert_tpr(input_tpr_path='/path/to/myStructure.tpr', 

43 output_tpr_path='/path/to/newCompiledBin.tpr', 

44 properties=prop) 

45 

46 Info: 

47 * wrapped_software: 

48 * name: GROMACS Convert-tpr 

49 * version: 2025.2 

50 * license: LGPL 2.1 

51 * ontology: 

52 * name: EDAM 

53 * schema: http://edamontology.org/EDAM.owl 

54 """ 

55 

56 def __init__(self, input_tpr_path: str, output_tpr_path: str, 

57 properties: Optional[dict] = None, **kwargs) -> None: 

58 properties = properties or {} 

59 

60 # Call parent class constructor 

61 super().__init__(properties) 

62 self.locals_var_dict = locals().copy() 

63 

64 # Input/Output files 

65 self.io_dict = { 

66 "in": {"input_tpr_path": input_tpr_path}, 

67 "out": {"output_tpr_path": output_tpr_path} 

68 } 

69 

70 # Properties specific for BB 

71 self.extend = properties.get('extend') 

72 self.until = properties.get('until') 

73 self.nsteps = properties.get('nsteps') 

74 

75 # Properties common in all GROMACS BB 

76 self.gmx_lib = properties.get('gmx_lib', None) 

77 self.binary_path = properties.get('binary_path', 'gmx') 

78 self.gmx_nobackup = properties.get('gmx_nobackup', True) 

79 self.gmx_nocopyright = properties.get('gmx_nocopyright', True) 

80 if self.gmx_nobackup: 

81 self.binary_path += ' -nobackup' 

82 if self.gmx_nocopyright: 

83 self.binary_path += ' -nocopyright' 

84 if not self.container_path: 

85 self.gmx_version = get_gromacs_version(self.binary_path) 

86 

87 # Check the properties 

88 self.check_properties(properties) 

89 self.check_arguments() 

90 

91 @launchlogger 

92 def launch(self) -> int: 

93 """Execute the :class:`ConvertTpr <gromacs.convert_tpr.ConvertTpr>` object.""" 

94 

95 # Setup Biobb 

96 if self.check_restart(): 

97 return 0 

98 self.stage_files() 

99 

100 if self.container_path: 

101 working_dir = self.container_volume_path if self.container_volume_path else "/data" 

102 else: 

103 working_dir = self.stage_io_dict.get('unique_dir', '') 

104 

105 self.cmd = ["cd", working_dir, ";", self.binary_path, 'convert-tpr', 

106 '-s', PurePath(self.stage_io_dict["in"]["input_tpr_path"]).name, 

107 '-o', PurePath(self.stage_io_dict["out"]["output_tpr_path"]).name 

108 ] 

109 

110 if self.extend: 

111 self.cmd.extend(['-extend', str(self.extend)]) 

112 if self.until: 

113 self.cmd.extend(['-until', str(self.until)]) 

114 if self.nsteps: 

115 self.cmd.extend(['-nsteps', str(self.nsteps)]) 

116 

117 if self.gmx_lib: 

118 self.env_vars_dict['GMXLIB'] = self.gmx_lib 

119 

120 # Run Biobb block 

121 self.run_biobb() 

122 

123 # Copy files to host 

124 self.copy_to_host() 

125 

126 self.remove_tmp_files() 

127 

128 self.check_arguments(output_files_created=True, raise_exception=False) 

129 return self.return_code 

130 

131 

132def convert_tpr(input_tpr_path: str, output_tpr_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

133 """Create :class:`ConvertTpr <gromacs.convert_tpr.ConvertTpr>` class and 

134 execute the :meth:`launch() <gromacs.convert_tpr.ConvertTpr.launch>` method.""" 

135 return ConvertTpr(**dict(locals())).launch() 

136 

137 

138convert_tpr.__doc__ = ConvertTpr.__doc__ 

139main = ConvertTpr.get_main( 

140 convert_tpr, "Wrapper of the GROMACS convert-tpr module.") 

141 

142 

143if __name__ == '__main__': 

144 main()