Coverage for biobb_amber / process / process_mdout.py: 86%

77 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-12 10:52 +0000

1#!/usr/bin/env python3 

2 

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

4 

5import shutil 

6import os 

7from pathlib import Path, PurePath 

8from typing import Optional 

9 

10from biobb_common.generic.biobb_object import BiobbObject 

11from biobb_common.tools import file_utils as fu 

12from biobb_common.tools.file_utils import launchlogger 

13 

14from biobb_amber.process.common import ( 

15 _from_string_to_list, 

16 check_input_path, 

17 check_output_path, 

18) 

19 

20 

21class ProcessMDOut(BiobbObject): 

22 """ 

23 | biobb_amber.process.process_mdout ProcessMDOut 

24 | Wrapper of the `AmberTools (AMBER MD Package) process_mdout tool <https://ambermd.org/AmberTools.php>`_ module. 

25 | Parses the AMBER (sander) md output file (log) and dumps statistics that can then be plotted. Using the process_mdout.pl tool from the AmberTools MD package. 

26 

27 Args: 

28 input_log_path (str): AMBER (sander) MD output (log) file. File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/process/sander.heat.log>`_. Accepted formats: log (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

29 output_dat_path (str): Dat output file containing data from the specified terms along the minimization process. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/process/sander.md.temp.dat>`_. Accepted formats: dat (edam:format_1637), txt (edam:format_2330), csv (edam:format_3752). 

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

31 * **terms** (*list*) - (["ETOT"]) Statistics descriptors. Values: VOLUME, TSOLVENT, TSOLUTE, TEMP, PRES, ETOT, ESCF, EPTOT, EKTOT, EKCMT, DENSITY. 

32 * **binary_path** (*str*) - ("process_mdout.perl") Path to the process_mdout.perl executable binary. 

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

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

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

36 * **container_path** (*str*) - (None) Container path definition. 

37 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

38 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

39 * **container_working_dir** (*str*) - (None) Container working directory definition. 

40 * **container_user_id** (*str*) - (None) Container user_id definition. 

41 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

42 

43 Examples: 

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

45 

46 from biobb_amber.process.process_mdout import process_mdout 

47 prop = { 

48 'terms' : ['TEMP','VOLUME','DENSITY'] 

49 } 

50 process_mdout(input_log_path='/path/to/ambermd.log', 

51 output_dat_path='/path/to/newFeature.dat', 

52 properties=prop) 

53 

54 Info: 

55 * wrapped_software: 

56 * name: AmberTools process_mdout 

57 * version: >20.9 

58 * license: LGPL 2.1 

59 * ontology: 

60 * name: EDAM 

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

62 

63 """ 

64 

65 def __init__(self, input_log_path: str, output_dat_path: str, properties, **kwargs): 

66 properties = properties or {} 

67 

68 # Call parent class constructor 

69 super().__init__(properties) 

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

71 

72 # Input/Output files 

73 self.io_dict = { 

74 "in": {"input_log_path": input_log_path}, 

75 "out": {"output_dat_path": output_dat_path}, 

76 } 

77 

78 # Properties specific for BB 

79 self.properties = properties 

80 self.terms = _from_string_to_list(properties.get("terms", ["ETOT"])) 

81 self.binary_path = properties.get("binary_path", "process_mdout.perl") 

82 

83 # Check the properties 

84 self.check_properties(properties) 

85 self.check_arguments() 

86 

87 def check_data_params(self, out_log, err_log): 

88 """Checks input/output paths correctness""" 

89 

90 # Check input(s) 

91 self.io_dict["in"]["input_log_path"] = check_input_path( 

92 self.io_dict["in"]["input_log_path"], 

93 "input_log_path", 

94 False, 

95 out_log, 

96 self.__class__.__name__, 

97 ) 

98 

99 # Check output(s) 

100 self.io_dict["out"]["output_dat_path"] = check_output_path( 

101 self.io_dict["out"]["output_dat_path"], 

102 "output_dat_path", 

103 False, 

104 out_log, 

105 self.__class__.__name__, 

106 ) 

107 

108 @launchlogger 

109 def launch(self): 

110 """Launches the execution of the ProcessMDOut module.""" 

111 

112 # check input/output paths and parameters 

113 self.check_data_params(self.out_log, self.err_log) 

114 

115 # Setup Biobb 

116 if self.check_restart(): 

117 return 0 

118 self.stage_files() 

119 

120 is_docker = self.container_path and os.path.basename(str(self.container_path)).lower() == 'docker' 

121 

122 if not self.container_path or not is_docker: 

123 # No container or Singularity: cd to tmp_folder so summary.* files land there 

124 tmp_folder = fu.create_unique_dir() 

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

126 self.cmd = [ 

127 "cd", 

128 tmp_folder, 

129 ";", 

130 self.binary_path, 

131 str(Path(self.stage_io_dict["in"]["input_log_path"]).resolve()), 

132 ] 

133 else: 

134 # Docker: working dir is handled via container_working_dir 

135 tmp_folder = None 

136 self.cmd = [self.binary_path, self.stage_io_dict["in"]["input_log_path"]] 

137 

138 if self.container_path: 

139 if not self.container_working_dir: 

140 fu.log('WARNING: container_working_dir property was not set. Defining it with the same value as container_volume_path', self.out_log, self.global_log) 

141 self.container_working_dir = self.container_volume_path 

142 

143 # Run Biobb block 

144 self.run_biobb() 

145 

146 # Copy files to host 

147 self.copy_to_host() 

148 

149 if len(self.terms) == 1: 

150 if self.container_path and is_docker: 

151 shutil.copy( 

152 PurePath(self.stage_io_dict["unique_dir"]).joinpath( 

153 "summary." + self.terms[0] 

154 ), 

155 self.io_dict["out"]["output_dat_path"], 

156 ) 

157 else: 

158 shutil.copy( 

159 PurePath(str(tmp_folder)).joinpath("summary." + self.terms[0]), 

160 self.io_dict["out"]["output_dat_path"], 

161 ) 

162 else: 

163 if self.container_path and is_docker: 

164 tmp = self.stage_io_dict["unique_dir"] 

165 else: 

166 tmp = tmp_folder 

167 

168 ene_dict = {} 

169 for term in self.terms: 

170 with open(str(tmp) + "/summary." + term) as fp: 

171 for line in fp: 

172 x = line.split() 

173 if x: 

174 if len(x) > 1: 

175 ene_dict.setdefault(float(x[0]), {})[term] = x[1] 

176 else: 

177 ene_dict.setdefault(float(x[0]), {})[term] = "-" 

178 

179 with open(self.io_dict["out"]["output_dat_path"], "w") as fp_out: 

180 fp_out.write("# TIME ") 

181 for term in self.terms: 

182 fp_out.write(term + " ") 

183 fp_out.write("\n") 

184 for key in sorted(ene_dict.keys()): 

185 fp_out.write(str(key) + " ") 

186 for term in self.terms: 

187 fp_out.write(ene_dict[key][term] + " ") 

188 fp_out.write("\n") 

189 

190 # remove temporary folder(s) 

191 self.tmp_files.extend([ 

192 str(tmp_folder) 

193 ] + list(Path().glob("summary*")) 

194 ) 

195 self.remove_tmp_files() 

196 

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

198 

199 return self.return_code 

200 

201 

202def process_mdout( 

203 input_log_path: str, 

204 output_dat_path: str, 

205 properties: Optional[dict] = None, 

206 **kwargs, 

207) -> int: 

208 """Create :class:`ProcessMDOut <process.process_mdout.ProcessMDOut>`process.process_mdout.ProcessMDOut class and 

209 execute :meth:`launch() <process.process_mdout.ProcessMDOut.launch>` method""" 

210 return ProcessMDOut(**dict(locals())).launch() 

211 

212 

213process_mdout.__doc__ = ProcessMDOut.__doc__ 

214 

215main = ProcessMDOut.get_main(process_mdout, "Parses the AMBER (sander) MD output file (log) and dumps statistics that can then be plotted. Using the process_mdout.pl tool from the AmberTools MD package.") 

216 

217if __name__ == "__main__": 

218 main()