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

51 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-16 13:07 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5import shutil 

6from pathlib import PurePath 

7from biobb_common.tools import file_utils as fu 

8from biobb_common.generic.biobb_object import BiobbObject 

9from biobb_common.tools.file_utils import launchlogger 

10 

11 

12class ImodImc(BiobbObject): 

13 """ 

14 | biobb_flexdyn imod_imc 

15 | Wrapper of the imc tool 

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

17 

18 Args: 

19 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). 

20 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). 

21 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). 

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

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

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

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

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 

30 Examples: 

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

32 

33 from biobb_flexdyn.flexdyn.imod_imc import imod_imc 

34 prop = { 

35 'num_structs' : 500 

36 } 

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

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

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

40 properties=prop) 

41 

42 Info: 

43 * wrapped_software: 

44 * name: iMODS 

45 * version: >=1.0.4 

46 * license: other 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

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

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

55 

56 properties = properties or {} 

57 

58 # Call parent class constructor 

59 super().__init__(properties) 

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

61 

62 # Input/Output files 

63 self.io_dict = { 

64 'in': {'input_pdb_path': input_pdb_path, 'input_dat_path': input_dat_path}, 

65 'out': {'output_traj_path': output_traj_path} 

66 } 

67 

68 # Properties specific for BB 

69 self.properties = properties 

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

71 

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

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

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

75 

76 # Check the properties 

77 self.check_properties(properties) 

78 self.check_arguments() 

79 

80 @launchlogger 

81 def launch(self): 

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

83 

84 # Setup Biobb 

85 if self.check_restart(): 

86 return 0 

87 # self.stage_files() 

88 

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

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

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

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

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

94 

95 # Creating temporary folder 

96 self.tmp_folder = fu.create_unique_dir() 

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

98 

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

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

101 

102 # Output temporary file 

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

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

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

106 out_file = "imod_ensemble.pdb" 

107 

108 # Command line 

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

110 # self.cmd = [self.binary_path, 

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

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

113 # "-o", str(out_file_prefix) 

114 # ] 

115 

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

117 self.binary_path, 

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

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

120 '-o', out_file_prefix 

121 ] 

122 

123 # Properties 

124 if self.num_structs: 

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

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

127 

128 if self.num_modes: 

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

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

131 

132 if self.amplitude: 

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

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

135 

136 # Run Biobb block 

137 self.run_biobb() 

138 

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

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

141 

142 # Copy outputs from temporary folder to output path 

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

144 

145 # Copy files to host 

146 # self.copy_to_host() 

147 

148 # remove temporary folder(s) 

149 self.tmp_files.extend([ 

150 self.tmp_folder 

151 ]) 

152 self.remove_tmp_files() 

153 

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

155 

156 return self.return_code 

157 

158 

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

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

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

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

163 return ImodImc(**dict(locals())).launch() 

164 

165 

166imod_imc.__doc__ = ImodImc.__doc__ 

167main = ImodImc.get_main(imod_imc, "Compute a Monte-Carlo IC-NMA based conformational ensemble using the imc tool from the iMODS package.") 

168 

169if __name__ == '__main__': 

170 main()