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

52 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-05-28 07:02 +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 Path, PurePath 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools.file_utils import launchlogger 

9 

10 

11class ImodImc(BiobbObject): 

12 """ 

13 | biobb_flexdyn imod_imc 

14 | Wrapper of the imc tool 

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

16 

17 Args: 

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

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

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

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

22 * **binary_path** (*str*) - ("imc") iMODS imc binary path to be used. 

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 * **container_path** (*str*) - (None) Path to the binary executable of your container. 

30 * **container_image** (*str*) - ("cmip/cmip:latest") Container Image identifier. 

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

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

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

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

35 

36 Examples: 

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

38 

39 from biobb_flexdyn.flexdyn.imod_imc import imod_imc 

40 prop = { 

41 'num_structs' : 500 

42 } 

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

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

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

46 properties=prop) 

47 

48 Info: 

49 * wrapped_software: 

50 * name: iMODS 

51 * version: >=1.0.4 

52 * license: other 

53 * ontology: 

54 * name: EDAM 

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

56 

57 """ 

58 

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

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

61 

62 properties = properties or {} 

63 

64 # Call parent class constructor 

65 super().__init__(properties) 

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

67 

68 # Input/Output files 

69 self.io_dict = { 

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

71 'out': {'output_traj_path': output_traj_path} 

72 } 

73 

74 # Properties specific for BB 

75 self.properties = properties 

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

77 

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

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

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

81 

82 # Check the properties 

83 self.check_properties(properties) 

84 self.check_arguments() 

85 

86 @launchlogger 

87 def launch(self): 

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

89 

90 # Setup Biobb 

91 if self.check_restart(): 

92 return 0 

93 self.stage_files() 

94 

95 # Determine working directory (host unique_dir or container volume path) 

96 if self.container_path: 

97 working_dir = self.container_volume_path if self.container_volume_path else "/data" 

98 else: 

99 working_dir = self.stage_io_dict.get('unique_dir', '') 

100 

101 # Output temporary file 

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

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

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

105 out_file = "imod_ensemble.pdb" 

106 

107 # Command line 

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

109 # self.cmd = [self.binary_path, 

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

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

112 # "-o", str(out_file_prefix) 

113 # ] 

114 

115 self.cmd = ['cd', working_dir, ';', 

116 self.binary_path, 

117 PurePath(self.stage_io_dict["in"]["input_pdb_path"]).name, 

118 PurePath(self.stage_io_dict["in"]["input_dat_path"]).name, 

119 '-o', out_file_prefix 

120 ] 

121 

122 # Properties 

123 if self.num_structs: 

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

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

126 

127 if self.num_modes: 

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

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

130 

131 if self.amplitude: 

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

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

134 

135 # Run Biobb block 

136 self.run_biobb() 

137 

138 # Rename generated output file to staged output path inside the sandbox. 

139 # stage_io_dict output paths are container-internal when running in containers. 

140 generated_output = Path(self.stage_io_dict.get('unique_dir', '')).joinpath(out_file) 

141 staged_output = Path(self.stage_io_dict.get('unique_dir', '')).joinpath( 

142 Path(self.stage_io_dict["out"]["output_traj_path"]).name 

143 ) 

144 shutil.copy2(generated_output, staged_output) 

145 

146 # Copy files to host 

147 self.copy_to_host() 

148 

149 # remove temporary folder(s) 

150 self.remove_tmp_files() 

151 

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

153 

154 return self.return_code 

155 

156 

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

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

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

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

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

162 

163 

164imod_imc.__doc__ = ImodImc.__doc__ 

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

166 

167if __name__ == '__main__': 

168 main()