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

47 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-04 08:31 +0000

1#!/usr/bin/env python 

2 

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

4 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools.file_utils import launchlogger 

8 

9from biobb_io.api.common import ( 

10 check_mandatory_property, 

11 check_output_path, 

12 download_mddb_top, 

13 write_pdb, 

14 download_mddb_trj, 

15 write_bin, 

16 download_mddb_file 

17) 

18 

19 

20class MDDB(BiobbObject): 

21 """ 

22 | biobb_io MDDB 

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

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

25 

26 Args: 

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

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

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

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

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

32 * **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). 

33 * **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'). 

34 * **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. 

35 * **extra_files** (*list*) - (None) List of extra files to download. It should be a tuple with the name of the file and the path to be saved. 

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

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

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

39 

40 Examples: 

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

42 

43 from biobb_io.api.mddb import mddb 

44 prop = { 

45 'project_id': 'A0001', 

46 'trj_format': 'xtc' 

47 } 

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

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

50 properties=prop) 

51 

52 Info: 

53 * wrapped_software: 

54 * name: MDDB Database 

55 * license: Apache-2.0 

56 * ontology: 

57 * name: EDAM 

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

59 

60 """ 

61 

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

63 properties = properties or {} 

64 

65 # Call parent class constructor 

66 super().__init__(properties) 

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

68 

69 # Input/Output files 

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

71 

72 # Properties specific for BB 

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

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

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

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

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

78 self.extra_files = properties.get("extra_files", []) 

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 for (extra_file, extra_path) in self.extra_files: 

144 try: 

145 file_string = download_mddb_file( 

146 self.project_id, 

147 self.node_id, 

148 extra_file, 

149 self.out_log, 

150 self.global_log, 

151 self.__class__.__name__, 

152 ) 

153 write_bin(file_string, extra_path, self.out_log, self.global_log) 

154 except Exception: 

155 pass 

156 

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

158 

159 return 0 

160 

161 

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

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

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

165 return MDDB(**dict(locals())).launch() 

166 

167 

168mddb.__doc__ = MDDB.__doc__ 

169main = MDDB.get_main( 

170 mddb, 

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

172) 

173 

174if __name__ == "__main__": 

175 main()