Coverage for biobb_io/api/mddb.py: 78%

50 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 16:46 +0000

1#!/usr/bin/env python 

2 

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

4 

5import argparse 

6from typing import Optional 

7 

8from biobb_common.configuration import settings 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools.file_utils import launchlogger 

11 

12from biobb_io.api.common import ( 

13 check_mandatory_property, 

14 check_output_path, 

15 download_mddb_top, 

16 write_pdb, 

17 download_mddb_trj, 

18 write_bin 

19) 

20 

21 

22class MDDB(BiobbObject): 

23 """ 

24 | biobb_io MDDB 

25 | This class is a wrapper for downloading a trajectory / topology pair from the MDDB Database. 

26 | Wrapper for the `MDDB Database <https://mmb.mddbr.eu/>`_ for downloading a trajectory and its corresponding topology. 

27 

28 Args: 

29 output_top_path (str): Path to the output toplogy file. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/output_mddb.pdb>`_. Accepted formats: pdb (edam:format_1476). 

30 output_trj_path (str): Path to the output trajectory file. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/output_mddb.xtc>`_. Accepted formats: mdcrd (edam:format_3878), trr (edam:format_3910), xtc (edam:format_3875). 

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

32 * **project_id** (*str*) - (None) Project accession or identifier. 

33 * **node_id** (*str*) - ("mmb") MDDB node identifier. 

34 * **trj_format** (*str*) - ("xtc") Trajectory format. Values: mdcrd (AMBER trajectory format), trr (Trajectory of a simulation experiment used by GROMACS), xtc (Portable binary format for trajectories produced by GROMACS package). 

35 * **frames** (*str*) - (None) Specify a frame range for the returned trajectory. Ranges are defined by dashes, and multiple ranges can be defined separated by commas. It can also be defined as the start:end:step format (ie: '10:20:2'). 

36 * **selection** (*str*) - (None) Specify a NGL-formatted selection for the returned trajectory. See here for the kind of selection that can be used: http://nglviewer.org/ngl/api/manual/usage/selection-language.html. 

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

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

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

40 

41 Examples: 

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

43 

44 from biobb_io.api.mddb import mddb 

45 prop = { 

46 'project_id': 'A0001', 

47 'trj_format': 'xtc' 

48 } 

49 mddb(output_top_path='/path/to/newTopology.pdb', 

50 output_trj_path='/path/to/newTrajectory.pdb', 

51 properties=prop) 

52 

53 Info: 

54 * wrapped_software: 

55 * name: MDDB Database 

56 * license: Apache-2.0 

57 * ontology: 

58 * name: EDAM 

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

60 

61 """ 

62 

63 def __init__(self, output_top_path, output_trj_path, properties=None, **kwargs) -> None: 

64 properties = properties or {} 

65 

66 # Call parent class constructor 

67 super().__init__(properties) 

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

69 

70 # Input/Output files 

71 self.io_dict = {"out": {"output_top_path": output_top_path, "output_trj_path": output_trj_path}} 

72 

73 # Properties specific for BB 

74 self.project_id = properties.get("project_id", None) 

75 self.node_id = properties.get("node_id", "mmb") 

76 self.trj_format = properties.get("trj_format", "xtc") 

77 self.frames = properties.get("frames", "") 

78 self.selection = properties.get("selection", "*") 

79 self.properties = properties 

80 

81 # Check the properties 

82 self.check_properties(properties) 

83 self.check_arguments() 

84 

85 def check_data_params(self, out_log, err_log): 

86 """Checks all the input/output paths and parameters""" 

87 self.output_top_path = check_output_path( 

88 self.io_dict["out"]["output_top_path"], 

89 "output_top_path", 

90 False, 

91 out_log, 

92 self.__class__.__name__, 

93 ) 

94 self.output_trj_path = check_output_path( 

95 self.io_dict["out"]["output_trj_path"], 

96 "output_trj_path", 

97 False, 

98 out_log, 

99 self.__class__.__name__, 

100 ) 

101 

102 @launchlogger 

103 def launch(self) -> int: 

104 """Execute the :class:`MDDB <api.mddb.MDDB>` api.mddb.MDDB object.""" 

105 

106 # check input/output paths and parameters 

107 self.check_data_params(self.out_log, self.err_log) 

108 

109 # Setup Biobb 

110 if self.check_restart(): 

111 return 0 

112 

113 check_mandatory_property( 

114 self.project_id, "project_id", self.out_log, self.__class__.__name__ 

115 ) 

116 

117 self.project_id = self.project_id.strip().upper() 

118 

119 # Downloading topology file 

120 top_string = download_mddb_top( 

121 self.project_id, 

122 self.node_id, 

123 self.selection, 

124 self.out_log, 

125 self.global_log, 

126 self.__class__.__name__ 

127 ) 

128 write_pdb(top_string, self.output_top_path, None, self.out_log, self.global_log) 

129 

130 # Downloading trajectory file 

131 trj_string = download_mddb_trj( 

132 self.project_id, 

133 self.node_id, 

134 self.trj_format, 

135 self.frames, 

136 self.selection, 

137 self.out_log, 

138 self.global_log, 

139 self.__class__.__name__, 

140 ) 

141 write_bin(trj_string, self.output_trj_path, self.out_log, self.global_log) 

142 

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

144 

145 return 0 

146 

147 

148def mddb(output_top_path: str, output_trj_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

149 """Execute the :class:`MDDB <api.mddb.MDDB>` class and 

150 execute the :meth:`launch() <api.mddb.MDDB.launch>` method.""" 

151 

152 return MDDB( 

153 output_top_path=output_top_path, output_trj_path=output_trj_path, properties=properties, **kwargs 

154 ).launch() 

155 

156 

157def main(): 

158 """Command line execution of this building block. Please check the command line documentation.""" 

159 parser = argparse.ArgumentParser( 

160 description="This class is a wrapper for downloading a trajectory / topology pair from the MDDB Database.", 

161 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999), 

162 ) 

163 parser.add_argument( 

164 "-c", 

165 "--config", 

166 required=False, 

167 help="This file can be a YAML file, JSON file or JSON string", 

168 ) 

169 

170 # Specific args of each building block 

171 required_args = parser.add_argument_group("required arguments") 

172 required_args.add_argument( 

173 "-o", 

174 "--output_top_path", 

175 required=True, 

176 help="Path to the output toplogy file. Accepted formats: pdb.", 

177 ) 

178 required_args.add_argument( 

179 "-t", 

180 "--output_trj_path", 

181 required=True, 

182 help="Path to the output trajectory file. Accepted formats: mdcrd, trr, xtc.", 

183 ) 

184 

185 args = parser.parse_args() 

186 config = args.config if args.config else None 

187 properties = settings.ConfReader(config=config).get_prop_dic() 

188 

189 # Specific call of each building block 

190 mddb(output_top_path=args.output_top_path, output_trj_path=args.output_trj_path, properties=properties) 

191 

192 

193if __name__ == "__main__": 

194 main()