Coverage for biobb_io/api/memprotmd_sim.py: 75%

40 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-10 15:33 +0000

1#!/usr/bin/env python 

2 

3"""Module containing the MemProtMDSim 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 get_memprotmd_sim, 

16) 

17 

18 

19class MemProtMDSim(BiobbObject): 

20 """ 

21 | biobb_io MemProtMDSim 

22 | This class is a wrapper of the MemProtMD to download a simulation using its REST API. 

23 | Wrapper for the `MemProtMD DB REST API <http://memprotmd.bioch.ox.ac.uk/>`_ to download a simulation. 

24 

25 Args: 

26 output_simulation (str): Path to the output simulation in a ZIP file. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/output_sim.zip>`_. Accepted formats: zip (edam:format_3987). 

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

28 * **pdb_code** (*str*) - (None) RSCB PDB code. 

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

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

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

32 

33 Examples: 

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

35 

36 from biobb_io.api.memprotmd_sim import memprotmd_sim 

37 prop = { 

38 'pdb_code': '2VGB' 

39 } 

40 memprotmd_sim(output_simulation='/path/to/newSimulation.zip', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: MemProtMD DB 

46 * license: Creative Commons 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

53 def __init__(self, output_simulation, properties=None, **kwargs) -> None: 

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 = {"out": {"output_simulation": output_simulation}} 

62 

63 # Properties specific for BB 

64 self.pdb_code = properties.get("pdb_code", None) 

65 self.properties = properties 

66 

67 # Check the properties 

68 self.check_properties(properties) 

69 self.check_arguments() 

70 

71 def check_data_params(self, out_log, err_log): 

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

73 self.output_simulation = check_output_path( 

74 self.io_dict["out"]["output_simulation"], 

75 "output_simulation", 

76 False, 

77 out_log, 

78 self.__class__.__name__, 

79 ) 

80 

81 @launchlogger 

82 def launch(self) -> int: 

83 """Execute the :class:`MemProtMDSim <api.memprotmd_sim.MemProtMDSim>` api.memprotmd_sim.MemProtMDSim object.""" 

84 

85 # check input/output paths and parameters 

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

87 

88 # Setup Biobb 

89 if self.check_restart(): 

90 return 0 

91 

92 check_mandatory_property( 

93 self.pdb_code, "pdb_code", self.out_log, self.__class__.__name__ 

94 ) 

95 

96 # get simulation files and save to output 

97 get_memprotmd_sim( 

98 self.pdb_code, self.output_simulation, self.out_log, self.global_log 

99 ) 

100 

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

102 

103 return 0 

104 

105 

106def memprotmd_sim( 

107 output_simulation: str, properties: Optional[dict] = None, **kwargs 

108) -> int: 

109 """Execute the :class:`MemProtMDSim <api.memprotmd_sim.MemProtMDSim>` class and 

110 execute the :meth:`launch() <api.memprotmd_sim.MemProtMDSim.launch>` method.""" 

111 

112 return MemProtMDSim( 

113 output_simulation=output_simulation, properties=properties, **kwargs 

114 ).launch() 

115 

116 

117def main(): 

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

119 parser = argparse.ArgumentParser( 

120 description="Wrapper for the MemProtMD DB REST API (http://memprotmd.bioch.ox.ac.uk/) to download a simulation.", 

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

122 ) 

123 parser.add_argument( 

124 "-c", 

125 "--config", 

126 required=False, 

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

128 ) 

129 

130 # Specific args of each building block 

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

132 required_args.add_argument( 

133 "-o", 

134 "--output_simulation", 

135 required=True, 

136 help="Path to the output simulation in a ZIP file. Accepted formats: zip.", 

137 ) 

138 

139 args = parser.parse_args() 

140 config = args.config if args.config else None 

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

142 

143 # Specific call of each building block 

144 memprotmd_sim(output_simulation=args.output_simulation, properties=properties) 

145 

146 

147if __name__ == "__main__": 

148 main()