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

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

15 """ 

16 | biobb_flexdyn imod_imode 

17 | Wrapper of the imode tool 

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

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

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

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

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

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

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

28 

29 Examples: 

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

31 

32 from biobb_flexdyn.flexdyn.imod_imode import imod_imode 

33 prop = { 

34 'cg' : 2 

35 } 

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

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

38 properties=prop) 

39 

40 Info: 

41 * wrapped_software: 

42 * name: iMODS 

43 * version: >=1.0.4 

44 * license: other 

45 * ontology: 

46 * name: EDAM 

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

48 

49 """ 

50 

51 def __init__(self, input_pdb_path: str, output_dat_path: str, 

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

53 

54 properties = properties or {} 

55 

56 # Call parent class constructor 

57 super().__init__(properties) 

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

59 

60 # Input/Output files 

61 self.io_dict = { 

62 'in': {'input_pdb_path': input_pdb_path}, 

63 'out': {'output_dat_path': output_dat_path} 

64 } 

65 

66 # Properties specific for BB 

67 self.properties = properties 

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

69 

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

71 

72 # Check the properties 

73 self.check_properties(properties) 

74 self.check_arguments() 

75 

76 @launchlogger 

77 def launch(self): 

78 """Launches the execution of the FlexDyn iMOD imode module.""" 

79 

80 # Setup Biobb 

81 if self.check_restart(): 

82 return 0 

83 # self.stage_files() 

84 

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

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

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

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

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

90 

91 # Creating temporary folder 

92 self.tmp_folder = fu.create_unique_dir() 

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

94 

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

96 

97 # Output temporary file 

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

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

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

101 out_file = "imods_evecs_ic.evec" 

102 

103 # Command line 

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

105 # self.cmd = [self.binary_path, 

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

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

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

109 # ] 

110 

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

112 self.binary_path, 

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

114 '-o', out_file_prefix, 

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

116 ] 

117 

118 # Run Biobb block 

119 self.run_biobb() 

120 

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

122 # shutil.copy2(out_file, self.stage_io_dict["out"]["output_dat_path"]) 

123 

124 # Copy outputs from temporary folder to output path 

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

126 

127 # Copy files to host 

128 # self.copy_to_host() 

129 

130 # remove temporary folder(s) 

131 self.tmp_files.extend([ 

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

133 self.tmp_folder 

134 ]) 

135 self.remove_tmp_files() 

136 

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

138 

139 return self.return_code 

140 

141 

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

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

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

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

146 

147 return ImodImode(input_pdb_path=input_pdb_path, 

148 output_dat_path=output_dat_path, 

149 properties=properties).launch() 

150 

151 

152def main(): 

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

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

155 

156 # Specific args 

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

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

159 required_args.add_argument('--output_dat_path', required=True, help='Output evecs dat file. Accepted formats: dat.') 

160 

161 args = parser.parse_args() 

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

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

164 

165 # Specific call 

166 imod_imode(input_pdb_path=args.input_pdb_path, 

167 output_dat_path=args.output_dat_path, 

168 properties=properties) 

169 

170 

171if __name__ == '__main__': 

172 main()