Coverage for biobb_flexdyn/flexdyn/imod_imove.py: 77%

53 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 ImodImove(BiobbObject): 

15 """ 

16 | biobb_flexdyn imod_imove 

17 | Wrapper of the imove tool 

18 | Compute the normal modes of a macromolecule using the imove 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_pdb_path (str): Output multi-model PDB file with the generated animation by Principal Component. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/reference/flexdyn/imod_imove_output.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

25 * **pc** (*int*) - (1) Principal Component. 

26 * **num_frames** (*int*) - (11) Number of frames to be generated 

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

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

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

30 

31 Examples: 

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

33 

34 from biobb_flexdyn.flexdyn.imod_imove import imod_imove 

35 prop = { 

36 'pc' : 1 

37 } 

38 imod_imove( input_pdb_path='/path/to/structure.pdb', 

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

40 output_pdb_path='/path/to/output_anim.pdb', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: iMODS 

46 * version: >=1.0.4 

47 * license: other 

48 * ontology: 

49 * name: EDAM 

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

51 

52 """ 

53 

54 def __init__(self, input_pdb_path: str, input_dat_path: str, output_pdb_path: str, 

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

56 

57 properties = properties or {} 

58 

59 # Call parent class constructor 

60 super().__init__(properties) 

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

62 

63 # Input/Output files 

64 self.io_dict = { 

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

66 'out': {'output_pdb_path': output_pdb_path} 

67 } 

68 

69 # Properties specific for BB 

70 self.properties = properties 

71 self.binary_path = properties.get('binary_path', 'imove') 

72 

73 self.pc = properties.get('pc', 1) 

74 self.num_frames = properties.get('num_frames', 11) 

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 imove 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 # Command line 

103 # imove 1ake_backbone.pdb 1ake_backbone_evecs.dat -o 1ake_backbone.ensemble.pdb 1 -c 500 

104 # self.cmd = [self.binary_path, 

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

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

107 # str(Path(self.stage_io_dict["out"]["output_pdb_path"]).relative_to(Path.cwd())), 

108 # str(self.pc) 

109 # ] 

110 

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

112 self.binary_path, 

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

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

115 PurePath(self.io_dict["out"]["output_pdb_path"]).name, 

116 str(self.pc) 

117 ] 

118 

119 # Properties 

120 if self.num_frames: 

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

122 self.cmd.append(str(self.num_frames)) 

123 

124 # Run Biobb block 

125 self.run_biobb() 

126 

127 # Copy outputs from temporary folder to output path 

128 shutil.copy2(PurePath(self.tmp_folder).joinpath(PurePath(self.io_dict["out"]["output_pdb_path"]).name), PurePath(self.io_dict["out"]["output_pdb_path"])) 

129 

130 # Copy files to host 

131 # self.copy_to_host() 

132 

133 # remove temporary folder(s) 

134 self.tmp_files.extend([ 

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

136 self.tmp_folder 

137 ]) 

138 self.remove_tmp_files() 

139 

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

141 

142 return self.return_code 

143 

144 

145def imod_imove(input_pdb_path: str, input_dat_path: str, output_pdb_path: str, 

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

147 """Create :class:`ImodImove <flexdyn.imod_imove.ImodImove>`flexdyn.imod_imove.ImodImove class and 

148 execute :meth:`launch() <flexdyn.imod_imove.ImodImove.launch>` method""" 

149 

150 return ImodImove(input_pdb_path=input_pdb_path, 

151 input_dat_path=input_dat_path, 

152 output_pdb_path=output_pdb_path, 

153 properties=properties).launch() 

154 

155 

156def main(): 

157 parser = argparse.ArgumentParser(description='Animate the normal modes of a macromolecule using the imove tool from the iMODS package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

159 

160 # Specific args 

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

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

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

164 required_args.add_argument('--output_pdb_path', required=True, help='Output pdb file. Accepted formats: pdb.') 

165 

166 args = parser.parse_args() 

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

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

169 

170 # Specific call 

171 imod_imove(input_pdb_path=args.input_pdb_path, 

172 input_dat_path=args.input_dat_path, 

173 output_pdb_path=args.output_pdb_path, 

174 properties=properties) 

175 

176 

177if __name__ == '__main__': 

178 main()