Coverage for biobb_flexdyn/flexdyn/imod_imode.py: 93%

41 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 ImodImode(BiobbObject): 

12 """ 

13 | biobb_flexdyn imod_imode 

14 | Wrapper of the imode tool 

15 | Compute the normal modes of a macromolecule using the imode 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.pdb>`_. Accepted formats: pdb (edam:format_1476). 

19 output_dat_path (str): Output dat with normal modes. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/reference/flexdyn/imod_imode_evecs.dat>`_. Accepted formats: dat (edam:format_1637), txt (edam:format_2330). 

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

21 * **binary_path** (*str*) - ("imode_gcc") iMODS imode binary path to be used. 

22 * **cg** (*int*) - (2) Coarse-Grained model. Values: 0 (CA), 1 (C5), 2 (Heavy atoms). 

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

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

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

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

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

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

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

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

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

32 

33 Examples: 

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

35 

36 from biobb_flexdyn.flexdyn.imod_imode import imod_imode 

37 prop = { 

38 'cg' : 2 

39 } 

40 imod_imode( input_pdb_path='/path/to/structure.pdb', 

41 output_dat_path='/path/to/output_evecs.dat', 

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, output_dat_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}, 

67 'out': {'output_dat_path': output_dat_path} 

68 } 

69 

70 # Properties specific for BB 

71 self.properties = properties 

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

73 

74 self.cg = properties.get('cg', 2) 

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 imode module.""" 

83 

84 # Setup Biobb 

85 if self.check_restart(): 

86 return 0 

87 self.stage_files() 

88 

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

90 if self.container_path: 

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

92 else: 

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

94 

95 # Output temporary file 

96 # out_file_prefix = Path(self.stage_io_dict.get("unique_dir", "")).joinpath("imods_evecs") 

97 # out_file = Path(self.stage_io_dict.get("unique_dir", "")).joinpath("imods_evecs_ic.evec") 

98 out_file_prefix = "imods_evecs" # Needed as imod is appending the _ic.evec extension 

99 out_file = "imods_evecs_ic.evec" 

100 

101 # Command line 

102 # imode_gcc 1ake_backbone.pdb -m 0 -o patata.evec 

103 # self.cmd = [self.binary_path, 

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

105 # "-o", str(out_file_prefix), 

106 # "-m", str(self.cg) 

107 # ] 

108 

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

110 self.binary_path, 

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

112 '-o', out_file_prefix, 

113 '-m', str(self.cg) 

114 ] 

115 

116 # Run Biobb block 

117 self.run_biobb() 

118 

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

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

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

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

123 Path(self.stage_io_dict["out"]["output_dat_path"]).name 

124 ) 

125 shutil.copy2(generated_output, staged_output) 

126 

127 # Copy files to host 

128 self.copy_to_host() 

129 

130 # remove temporary folder(s) 

131 self.remove_tmp_files() 

132 

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

134 

135 return self.return_code 

136 

137 

138def imod_imode(input_pdb_path: str, output_dat_path: str, 

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

140 """Create :class:`ImodImode <flexdyn.imod_imode.ImodImode>`flexdyn.imod_imode.ImodImode class and 

141 execute :meth:`launch() <flexdyn.imod_imode.ImodImode.launch>` method""" 

142 return ImodImode(**dict(locals())).launch() 

143 

144 

145imod_imode.__doc__ = ImodImode.__doc__ 

146main = ImodImode.get_main(imod_imode, "Compute the normal modes of a macromolecule using the imode tool from the iMODS package.") 

147 

148if __name__ == '__main__': 

149 main()