Coverage for biobb_flexserv/flexserv/bd_run.py: 70%

54 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-19 15:08 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6from pathlib import Path 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.configuration import settings 

9from biobb_common.tools.file_utils import launchlogger 

10 

11 

12class BDRun(BiobbObject): 

13 """ 

14 | biobb_flexserv BDRun 

15 | Wrapper of the Browian Dynamics tool from the FlexServ module. 

16 | Generates protein conformational structures using the Brownian Dynamics (BD) method. 

17 

18 Args: 

19 input_pdb_path (str): Input PDB file. File type: input. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/data/flexserv/structure.ca.pdb>`_. Accepted formats: pdb (edam:format_1476). 

20 output_log_path (str): Output log file. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/reference/flexserv/bd_run_out.log>`_. Accepted formats: log (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

21 output_crd_path (str): Output ensemble. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/reference/flexserv/bd_run_out.crd>`_. Accepted formats: crd (edam:format_3878), mdcrd (edam:format_3878), inpcrd (edam:format_3878). 

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

23 * **binary_path** (*str*) - ("bd") BD binary path to be used. 

24 * **time** (*int*) - (1000000) Total simulation time (ps) 

25 * **dt** (*float*) - (1e-15) Integration time (ps) 

26 * **wfreq** (*int*) - (1000) Writing frequency (ps) 

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_flexserv.flexserv.bd_run import bd_run 

35 prop = { 

36 'binary_path': 'bd' 

37 } 

38 flexserv_run(input_pdb_path='/path/to/bd_input.pdb', 

39 output_log_path='/path/to/bd_log.log', 

40 output_crd_path='/path/to/bd_ensemble.crd', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: FlexServ Brownian Dynamics 

46 * version: >=1.0 

47 * license: Apache-2.0 

48 * ontology: 

49 * name: EDAM 

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

51 

52 """ 

53 

54 def __init__(self, input_pdb_path: str, output_log_path: str, 

55 output_crd_path: str, 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}, 

66 'out': {'output_log_path': output_log_path, 

67 'output_crd_path': output_crd_path} 

68 } 

69 

70 # Properties specific for BB 

71 self.properties = properties 

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

73 self.time = properties.get('time', 1000000) 

74 self.dt = properties.get('dt', 1e-15) 

75 self.wfreq = properties.get('wfreq', 1000) 

76 

77 # Check the properties 

78 self.check_properties(properties) 

79 self.check_arguments() 

80 

81 @launchlogger 

82 def launch(self): 

83 """Launches the execution of the FlexServ BDRun module.""" 

84 

85 # Setup Biobb 

86 if self.check_restart(): 

87 return 0 

88 self.stage_files() 

89 

90 # Internal file paths 

91 try: 

92 # Using rel paths to shorten the amount of characters due to fortran path length limitations 

93 input_pdb = str(Path(self.stage_io_dict["in"]["input_pdb_path"]).relative_to(Path.cwd())) 

94 output_crd = str(Path(self.stage_io_dict["out"]["output_crd_path"]).relative_to(Path.cwd())) 

95 output_log = str(Path(self.stage_io_dict["out"]["output_log_path"]).relative_to(Path.cwd())) 

96 except ValueError: 

97 # Container or remote case 

98 input_pdb = self.stage_io_dict["in"]["input_pdb_path"] 

99 output_crd = self.stage_io_dict["out"]["output_crd_path"] 

100 output_log = self.stage_io_dict["out"]["output_log_path"] 

101 

102 # Command line 

103 # bd structure.ca.pdb 1000000 1e-15 1000 40 3.8 traj.crd > bd.log 

104 # itempsmax, dt, itsnap, const, r0 

105 self.cmd = [self.binary_path, 

106 input_pdb, 

107 str(self.time), 

108 str(self.dt), 

109 str(self.wfreq), 

110 "40", # Hardcoded "Const", see https://mmb.irbbarcelona.org/gitlab/adam/FlexServ/blob/master/bd/bd2.f#L51 

111 "3.8", # Hardcoded "r0", see https://mmb.irbbarcelona.org/gitlab/adam/FlexServ/blob/master/bd/bd2.f#L52 

112 output_crd, 

113 '>', output_log 

114 ] 

115 

116 # Run Biobb block 

117 self.run_biobb() 

118 

119 # Copy files to host 

120 self.copy_to_host() 

121 

122 # remove temporary folder(s) 

123 self.tmp_files.extend([ 

124 self.stage_io_dict.get("unique_dir", "") 

125 ]) 

126 self.remove_tmp_files() 

127 

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

129 

130 return self.return_code 

131 

132 

133def bd_run(input_pdb_path: str, 

134 output_log_path: str, output_crd_path: str, 

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

136 """Create :class:`BDRun <flexserv.bd_run.BDRun>`flexserv.bd_run.BDRun class and 

137 execute :meth:`launch() <flexserv.bd_run.BDRun.launch>` method""" 

138 

139 return BDRun(input_pdb_path=input_pdb_path, 

140 output_log_path=output_log_path, 

141 output_crd_path=output_crd_path, 

142 properties=properties).launch() 

143 

144 

145def main(): 

146 parser = argparse.ArgumentParser(description='Generates protein conformational structures using the Brownian Dynamics method.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

148 

149 # Specific args 

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

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

152 required_args.add_argument('--output_log_path', required=True, help='Output log file. Accepted formats: log, out, txt.') 

153 required_args.add_argument('--output_crd_path', required=True, help='Output ensemble file. Accepted formats: crd, mdcrd, inpcrd.') 

154 

155 args = parser.parse_args() 

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

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

158 

159 # Specific call 

160 bd_run(input_pdb_path=args.input_pdb_path, 

161 output_log_path=args.output_log_path, 

162 output_crd_path=args.output_crd_path, 

163 properties=properties) 

164 

165 

166if __name__ == '__main__': 

167 main()