Coverage for biobb_flexdyn/flexdyn/imod_imc.py: 81%

62 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-04 11:11 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6import shutil 

7from pathlib import PurePath 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.configuration import settings 

11from biobb_common.tools.file_utils import launchlogger 

12 

13 

14class ImodImc(BiobbObject): 

15 """ 

16 | biobb_flexdyn imod_imc 

17 | Wrapper of the imc tool 

18 | Compute a Monte-Carlo IC-NMA based conformational ensemble using the imc tool from the iMODS package. 

19 

20 Args: 

21 input_pdb_path (str): Input PDB file. File type: input. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/data/flexdyn/structure_cleaned.pdb>`_. Accepted formats: pdb (edam:format_1476). 

22 input_dat_path (str): Input dat with normal modes. File type: input. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/data/flexdyn/imod_imode_evecs.dat>`_. Accepted formats: dat (edam:format_1637), txt (edam:format_2330). 

23 output_traj_path (str): Output multi-model PDB file with the generated ensemble. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/reference/flexdyn/imod_imc_output.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

25 * **num_structs** (*int*) - (500) Number of structures to be generated 

26 * **num_modes** (*int*) - (5) Number of eigenvectors to be employed 

27 * **amplitude** (*int*) - (1) Amplitude linear factor to scale motion 

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

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

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

31 

32 Examples: 

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

34 

35 from biobb_flexdyn.flexdyn.imod_imc import imod_imc 

36 prop = { 

37 'num_structs' : 500 

38 } 

39 imod_imc( input_pdb_path='/path/to/structure.pdb', 

40 input_dat_path='/path/to/input_evecs.dat', 

41 output_traj_path='/path/to/output_ensemble.pdb', 

42 properties=prop) 

43 

44 Info: 

45 * wrapped_software: 

46 * name: iMODS 

47 * version: >=1.0.4 

48 * license: other 

49 * ontology: 

50 * name: EDAM 

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

52 

53 """ 

54 

55 def __init__(self, input_pdb_path: str, input_dat_path: str, output_traj_path: str, 

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

57 

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_pdb_path': input_pdb_path, 'input_dat_path': input_dat_path}, 

67 'out': {'output_traj_path': output_traj_path} 

68 } 

69 

70 # Properties specific for BB 

71 self.properties = properties 

72 self.binary_path = properties.get('binary_path', 'imc') 

73 

74 self.num_structs = properties.get('num_structs', 500) 

75 self.num_modes = properties.get('num_modes', 5) 

76 self.amplitude = properties.get('amplitude', 1.0) 

77 

78 # Check the properties 

79 self.check_properties(properties) 

80 self.check_arguments() 

81 

82 @launchlogger 

83 def launch(self): 

84 """Launches the execution of the FlexDyn iMOD imc module.""" 

85 

86 # Setup Biobb 

87 if self.check_restart(): 

88 return 0 

89 # self.stage_files() 

90 

91 # Manually creating a Sandbox to avoid issues with input parameters buffer overflow: 

92 # Long strings defining a file path makes Fortran or C compiled programs crash if the string 

93 # declared is shorter than the input parameter path (string) length. 

94 # Generating a temporary folder and working inside this folder (sandbox) fixes this problem. 

95 # The problem was found in Galaxy executions, launching Singularity containers (May 2023). 

96 

97 # Creating temporary folder 

98 self.tmp_folder = fu.create_unique_dir() 

99 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log) 

100 

101 shutil.copy2(self.io_dict["in"]["input_pdb_path"], self.tmp_folder) 

102 shutil.copy2(self.io_dict["in"]["input_dat_path"], self.tmp_folder) 

103 

104 # Output temporary file 

105 # out_file_prefix = Path(self.stage_io_dict.get("unique_dir", "")).joinpath("imod_ensemble") 

106 # out_file = Path(self.stage_io_dict.get("unique_dir", "")).joinpath("imod_ensemble.pdb") 

107 out_file_prefix = "imod_ensemble" # Needed as imod is appending the .pdb extension 

108 out_file = "imod_ensemble.pdb" 

109 

110 # Command line 

111 # imc 1ake_backbone.pdb 1ake_backbone_evecs.dat -o 1ake_backbone.ensemble.pdb -c 500 

112 # self.cmd = [self.binary_path, 

113 # str(Path(self.stage_io_dict["in"]["input_pdb_path"]).relative_to(Path.cwd())), 

114 # str(Path(self.stage_io_dict["in"]["input_dat_path"]).relative_to(Path.cwd())), 

115 # "-o", str(out_file_prefix) 

116 # ] 

117 

118 self.cmd = ['cd', self.tmp_folder, ';', 

119 self.binary_path, 

120 PurePath(self.io_dict["in"]["input_pdb_path"]).name, 

121 PurePath(self.io_dict["in"]["input_dat_path"]).name, 

122 '-o', out_file_prefix 

123 ] 

124 

125 # Properties 

126 if self.num_structs: 

127 self.cmd.append('-c') 

128 self.cmd.append(str(self.num_structs)) 

129 

130 if self.num_modes: 

131 self.cmd.append('-n') 

132 self.cmd.append(str(self.num_modes)) 

133 

134 if self.amplitude: 

135 self.cmd.append('-a') 

136 self.cmd.append(str(self.amplitude)) 

137 

138 # Run Biobb block 

139 self.run_biobb() 

140 

141 # Copying generated output file to the final (user-given) file name 

142 # shutil.copy2(out_file, self.stage_io_dict["out"]["output_traj_path"]) 

143 

144 # Copy outputs from temporary folder to output path 

145 shutil.copy2(PurePath(self.tmp_folder).joinpath(out_file), PurePath(self.io_dict["out"]["output_traj_path"])) 

146 

147 # Copy files to host 

148 # self.copy_to_host() 

149 

150 # remove temporary folder(s) 

151 self.tmp_files.extend([ 

152 # self.stage_io_dict.get("unique_dir", "") 

153 self.tmp_folder 

154 ]) 

155 self.remove_tmp_files() 

156 

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

158 

159 return self.return_code 

160 

161 

162def imod_imc(input_pdb_path: str, input_dat_path: str, output_traj_path: str, 

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

164 """Create :class:`ImodImc <flexdyn.imod_imc.ImodImc>`flexdyn.imod_imc.ImodImc class and 

165 execute :meth:`launch() <flexdyn.imod_imc.ImodImc.launch>` method""" 

166 

167 return ImodImc(input_pdb_path=input_pdb_path, 

168 input_dat_path=input_dat_path, 

169 output_traj_path=output_traj_path, 

170 properties=properties).launch() 

171 

172 

173def main(): 

174 parser = argparse.ArgumentParser(description='Compute a Monte-Carlo IC-NMA based conformational ensemble using the imc tool from the iMODS package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

175 parser.add_argument('--config', required=False, help='Configuration file') 

176 

177 # Specific args 

178 required_args = parser.add_argument_group('required arguments') 

179 required_args.add_argument('--input_pdb_path', required=True, help='Input structure file. Accepted formats: pdb') 

180 required_args.add_argument('--input_dat_path', required=True, help='Input evecs file. Accepted formats: dat') 

181 required_args.add_argument('--output_traj_path', required=True, help='Output traj file. Accepted formats: pdb.') 

182 

183 args = parser.parse_args() 

184 args.config = args.config or "{}" 

185 properties = settings.ConfReader(config=args.config).get_prop_dic() 

186 

187 # Specific call 

188 imod_imc(input_pdb_path=args.input_pdb_path, 

189 input_dat_path=args.input_dat_path, 

190 output_traj_path=args.output_traj_path, 

191 properties=properties) 

192 

193 

194if __name__ == '__main__': 

195 main()