Coverage for biobb_pmx / pmxbiobb / pmxcreate_top.py: 96%

75 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-22 17:32 +0000

1#!/usr/bin/env python3 

2 

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

4 

5import os 

6import shutil 

7import sys 

8from pathlib import Path, PurePath 

9from typing import Optional 

10 

11from biobb_common.generic.biobb_object import BiobbObject 

12from biobb_common.tools import file_utils as fu 

13from biobb_common.tools.file_utils import launchlogger 

14 

15 

16class Pmxcreate_top(BiobbObject): 

17 """ 

18 | biobb_pmx Pmxcreate_top 

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

20 | Create a complete ligand topology file from two input topology files. 

21 

22 Args: 

23 input_topology1_path (str): Path to the input topology file 1. File type: input. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/data/pmx/topo1.itp>`_. Accepted formats: itp (edam:format_3883). 

24 input_topology2_path (str): Path to the input topology file 2. File type: input. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/data/pmx/topo2.itp>`_. Accepted formats: itp (edam:format_3883). 

25 output_topology_path (str): Path to the complete ligand topology file. File type: output. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/reference/pmx/ref_hybridTopo.zip>`_. Accepted formats: zip (edam:format_3987). 

26 

27 properties (dic): 

28 * **force_field** (*str*) - ("amber99sb-star-ildn-mut.ff") Force-field to be included in the generated topology. 

29 * **water** (*str*) - ("tip3p") Water model to be included in the generated topology. 

30 * **system_name** (*str*) - ("Pmx topology") System name to be included in the generated topology. 

31 * **mols** (*list*) - ([['Protein',1],['Ligand',1]]) Molecules to be included in the generated topology. 

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

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

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

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

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

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

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

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

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

41 

42 Examples: 

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

44 

45 from biobb_pmx.pmxbiobb.pmxcreate_top import pmxcreate_top 

46 prop = { 

47 'remove_tmp' : True 

48 } 

49 pmxcreate_top(input_topology1_path='/path/to/myTopology1.itp', 

50 input_topology2_path='/path/to/myTopology2.itp', 

51 output_topology_path='/path/to/myMergedTopology.zip', 

52 properties=prop) 

53 

54 Info: 

55 * wrapped_software: 

56 * name: PMX create_top 

57 * version: >=1.0.1 

58 * license: GNU 

59 * ontology: 

60 * name: EDAM 

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

62 

63 """ 

64 

65 def __init__( 

66 self, 

67 input_topology1_path: str, 

68 input_topology2_path: str, 

69 output_topology_path: str, 

70 properties: Optional[dict] = None, 

71 **kwargs, 

72 ) -> None: 

73 properties = properties or {} 

74 

75 # Call parent class constructor 

76 super().__init__(properties) 

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

78 

79 # Input/Output files 

80 self.io_dict = { 

81 "in": { 

82 "input_topology1_path": input_topology1_path, 

83 "input_topology2_path": input_topology2_path, 

84 }, 

85 "out": {"output_topology_path": output_topology_path}, 

86 } 

87 

88 # Properties specific for BB 

89 self.force_field = properties.get("force_field", "amber99sb-star-ildn-mut.ff") 

90 self.water = properties.get("water", "tip3p") 

91 self.system_name = properties.get("system_name", "Pmx topology") 

92 self.mols = properties.get("mols", [["Protein", 1], ["Ligand", 1]]) 

93 

94 # Properties common in all PMX BB 

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

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

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

98 self.gmx_lib = str( 

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

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

101 ) 

102 ) 

103 if properties.get("container_path"): 

104 self.gmx_lib = str( 

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

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

107 ) 

108 ) 

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

110 

111 # Check the properties 

112 self.check_properties(properties) 

113 self.check_arguments() 

114 

115 @launchlogger 

116 def launch(self) -> int: 

117 """Execute the :class:`Pmxcreate_top <pmx.pmxcreate_top.Pmxcreate_top>` pmx.pmxcreate_top.Pmxcreate_top object.""" 

118 # os.chdir("/Users/hospital/BioBB/Notebooks_dev/biobb_wf_pmx_tutorial_ligands/biobb_wf_pmx_tutorial/notebooks") 

119 # Setup Biobb 

120 if self.check_restart(): 

121 return 0 

122 self.stage_files() 

123 

124 fu.log("Running create_top from pmx package...\n", self.out_log) 

125 

126 # Creating temporary folder 

127 tmp_folder = fu.create_unique_dir() 

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

129 

130 itp = os.path.basename( 

131 os.path.normpath(self.stage_io_dict["in"]["input_topology1_path"]) 

132 ) 

133 fu.log("Creating %s itp file in temporary folder" % itp, self.out_log) 

134 itp_local = str(PurePath(tmp_folder).joinpath(itp)) 

135 shutil.copyfile(self.io_dict["in"]["input_topology1_path"], itp_local) 

136 

137 itp2 = os.path.basename( 

138 os.path.normpath(self.stage_io_dict["in"]["input_topology2_path"]) 

139 ) 

140 fu.log("Creating %s itp file in temporary folder" % itp2, self.out_log) 

141 itp2_local = str(PurePath(tmp_folder).joinpath(itp2)) 

142 shutil.copyfile(self.io_dict["in"]["input_topology2_path"], itp2_local) 

143 

144 top_local = str(PurePath(tmp_folder).joinpath("topology.top")) 

145 

146 # _create_top function, taken from the pmx AZ tutorial: 

147 # https://github.com/deGrootLab/pmx/blob/develop/tutorials/AZtutorial.py 

148 fp = open(top_local, "w") 

149 # BioBB signature 

150 fp.write("; Topology generated by the BioBB pmxcreate_top building block\n\n") 

151 # ff itp 

152 fp.write('#include "%s/forcefield.itp"\n\n' % self.force_field) 

153 # additional itp 

154 # for i in self.itps: 

155 # fp.write('#include "%s"\n' % i) 

156 fp.write('#include "%s"\n' % itp) 

157 fp.write('#include "%s"\n\n' % itp2) 

158 # water itp 

159 fp.write('#include "%s/%s.itp"\n' % (self.force_field, self.water)) 

160 # ions 

161 fp.write('#include "%s/ions.itp"\n\n' % self.force_field) 

162 # system 

163 fp.write("[ system ]\n") 

164 fp.write("{0}\n\n".format(self.system_name)) 

165 # molecules 

166 fp.write("[ molecules ]\n") 

167 for mol in self.mols: 

168 fp.write("%s %s\n" % (mol[0], mol[1])) 

169 fp.close() 

170 

171 # zip topology 

172 current_cwd = os.getcwd() 

173 top_final = str( 

174 PurePath(current_cwd).joinpath(self.io_dict["out"]["output_topology_path"]) 

175 ) 

176 

177 os.chdir(tmp_folder) 

178 fu.log("Compressing topology to: %s" % top_final, self.out_log, self.global_log) 

179 fu.zip_top(zip_file=top_final, top_file="topology.top", out_log=self.out_log, remove_original_files=self.remove_tmp) 

180 os.chdir(current_cwd) 

181 

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

183 

184 # Run Biobb block 

185 # self.run_biobb() 

186 

187 # Copy files to host 

188 self.copy_to_host() 

189 

190 self.tmp_files.append(tmp_folder) 

191 self.remove_tmp_files() 

192 

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

194 return self.return_code 

195 

196 

197def pmxcreate_top( 

198 input_topology1_path: str, 

199 input_topology2_path: str, 

200 output_topology_path: str, 

201 properties: Optional[dict] = None, 

202 **kwargs, 

203) -> int: 

204 """Create the :class:`Pmxcreate_top <pmx.pmxcreate_top.Pmxcreate_top>` class and 

205 execute the :meth:`launch() <pmx.pmxmcreate_top.Pmxmcreate_top.launch> method.""" 

206 return Pmxcreate_top(**dict(locals())).launch() 

207 

208 

209pmxcreate_top.__doc__ = Pmxcreate_top.__doc__ 

210main = Pmxcreate_top.get_main(pmxcreate_top, "Run PMX create_top module") 

211 

212if __name__ == "__main__": 

213 main()