Coverage for biobb_pmx/pmxbiobb/pmxmerge_ff.py: 79%

63 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-23 10:10 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the PMX merge_ff class and the command line interface.""" 

4 

5import argparse 

6import glob 

7import os 

8import sys 

9from pathlib import Path 

10from typing import Optional 

11 

12from biobb_common.configuration import settings 

13from biobb_common.generic.biobb_object import BiobbObject 

14from biobb_common.tools import file_utils as fu 

15from biobb_common.tools.file_utils import launchlogger 

16from pmx import ligand_alchemy # type: ignore 

17 

18 

19class Pmxmerge_ff(BiobbObject): 

20 """ 

21 | biobb_pmx Pmxmerge_ff 

22 | Wrapper class for the `PMX merge_ff <https://github.com/deGrootLab/pmx>`_ module. 

23 | Merge ligand topology files. 

24 

25 Args: 

26 input_topology_path (str): Path to the input ligand topologies as a zip file containing a list of itp files. File type: input. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/data/pmx/ligand_itps.zip>`_. Accepted formats: zip (edam:format_3987). 

27 output_topology_path (str): Path to the merged ligand topology file. File type: output. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/data/pmx/ligand.itp>`_. Accepted formats: itp (edam:format_3883). 

28 

29 properties (dic): 

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

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

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

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

34 * **container_image** (*str*) - (None) Container Image identifier. 

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

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

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

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

39 

40 Examples: 

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

42 

43 from biobb_pmx.pmxbiobb.pmxmerge_ff import pmxmerge_ff 

44 prop = { 

45 'remove_tmp' : True 

46 } 

47 pmxmerge_ff(input_topology_path='/path/to/myTopologies.zip', 

48 output_topology_path='/path/to/myMergedTopology.itp', 

49 properties=prop) 

50 

51 Info: 

52 * wrapped_software: 

53 * name: PMX merge_ff 

54 * version: >=1.0.1 

55 * license: GNU 

56 * ontology: 

57 * name: EDAM 

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

59 

60 """ 

61 

62 def __init__( 

63 self, 

64 input_topology_path: str, 

65 output_topology_path: str, 

66 properties: Optional[dict] = None, 

67 **kwargs, 

68 ) -> None: 

69 properties = properties or {} 

70 

71 # Call parent class constructor 

72 super().__init__(properties) 

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

74 

75 # Input/Output files 

76 self.io_dict = { 

77 "in": {"input_topology_path": input_topology_path}, 

78 "out": {"output_topology_path": output_topology_path}, 

79 } 

80 

81 # Properties specific for BB 

82 # None yet 

83 

84 # Properties common in all PMX BB 

85 self.gmx_lib = properties.get("gmx_lib", None) 

86 if not self.gmx_lib and os.environ.get("CONDA_PREFIX", ""): 

87 python_version = f"{sys.version_info.major}.{sys.version_info.minor}" 

88 self.gmx_lib = str( 

89 Path(os.environ.get("CONDA_PREFIX", "")).joinpath( 

90 f"lib/python{python_version}/site-packages/pmx/data/mutff/" 

91 ) 

92 ) 

93 if properties.get("container_path"): 

94 self.gmx_lib = str( 

95 Path("/usr/local/").joinpath( 

96 "lib/python3.7/site-packages/pmx/data/mutff/" 

97 ) 

98 ) 

99 self.binary_path = properties.get("binary_path", "pmx") 

100 

101 # Check the properties 

102 self.check_properties(properties) 

103 self.check_arguments() 

104 

105 @launchlogger 

106 def launch(self) -> int: 

107 """Execute the :class:`Pmxmerge_ff <pmx.pmxmerge_ff.Pmxmerge_ff>` pmx.pmxmerge_ff.Pmxmerge_ff object.""" 

108 

109 # Setup Biobb 

110 if self.check_restart(): 

111 return 0 

112 self.stage_files() 

113 

114 # Creating temporary folder 

115 self.tmp_folder = fu.create_unique_dir() 

116 fu.log("Creating %s temporary folder" % self.tmp_folder, self.out_log) 

117 

118 fu.unzip_list( 

119 self.stage_io_dict["in"]["input_topology_path"], 

120 self.tmp_folder, 

121 out_log=self.out_log, 

122 ) 

123 files = glob.glob(self.tmp_folder + "/*.itp") 

124 ffsIn_list = [] 

125 for itp in files: 

126 ffsIn_list.append(itp) 

127 

128 fu.log("Running merge_FF_files from pmx package...\n", self.out_log) 

129 ligand_alchemy._merge_FF_files( 

130 self.stage_io_dict["out"]["output_topology_path"], ffsIn=ffsIn_list 

131 ) 

132 # ffsIn=[self.stage_io_dict["in"]["input_topology1_path"],self.stage_io_dict["in"]["input_topology2_path"]] ) 

133 

134 fu.log("Exit code 0\n", self.out_log) 

135 

136 if self.gmx_lib: 

137 self.env_vars_dict["GMXLIB"] = self.gmx_lib 

138 

139 # Run Biobb block 

140 # self.run_biobb() 

141 

142 # Copy files to host 

143 self.copy_to_host() 

144 

145 # self.tmp_files.append(self.stage_io_dict.get("unique_dir", "")) 

146 self.tmp_files.extend([self.tmp_folder]) 

147 self.remove_tmp_files() 

148 

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

150 return self.return_code 

151 

152 

153def pmxmerge_ff( 

154 input_topology_path: str, 

155 output_topology_path: str, 

156 properties: Optional[dict] = None, 

157 **kwargs, 

158) -> int: 

159 """Execute the :class:`Pmxmerge_ff <pmx.pmxmerge_ff.Pmxmerge_ff>` class and 

160 execute the :meth:`launch() <pmx.pmxmerge_ff.Pmxmerge_ff.launch> method.""" 

161 

162 return Pmxmerge_ff( 

163 input_topology_path=input_topology_path, 

164 output_topology_path=output_topology_path, 

165 properties=properties, 

166 **kwargs, 

167 ).launch() 

168 

169 pmxmerge_ff.__doc__ = Pmxmerge_ff.__doc__ 

170 

171 

172def main(): 

173 """Command line execution of this building block. Please check the command line documentation.""" 

174 parser = argparse.ArgumentParser( 

175 description="Run PMX merge_ff module", 

176 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999), 

177 ) 

178 parser.add_argument( 

179 "-c", 

180 "--config", 

181 required=False, 

182 help="This file can be a YAML file, JSON file or JSON string", 

183 ) 

184 

185 # Specific args of each building block 

186 required_args = parser.add_argument_group("required arguments") 

187 required_args.add_argument( 

188 "--input_topology_path", 

189 required=True, 

190 help="Path to the input ligand topologies as a zip file containing a list of itp files.", 

191 ) 

192 required_args.add_argument( 

193 "--output_topology_path", 

194 required=True, 

195 help="Path to the merged ligand topology file", 

196 ) 

197 

198 args = parser.parse_args() 

199 config = args.config if args.config else None 

200 properties = settings.ConfReader(config=config).get_prop_dic() 

201 

202 # Specific call of each building block 

203 pmxmerge_ff( 

204 input_topology_path=args.input_topology_path, 

205 output_topology_path=args.output_topology_path, 

206 properties=properties, 

207 ) 

208 

209 

210if __name__ == "__main__": 

211 main()