Coverage for biobb_io/api/ligand.py: 94%

34 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 Ligand 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_ligand, 

13 write_pdb, 

14) 

15 

16 

17class Ligand(BiobbObject): 

18 """ 

19 | biobb_io Ligand 

20 | This class is a wrapper for downloading a PDB ligand from the Protein Data Bank. 

21 | Wrapper for the `Protein Data Bank in Europe <https://www.ebi.ac.uk/pdbe/>`_ and the `MMB PDB mirror <http://mmb.irbbarcelona.org/api/>`_ for downloading a single PDB ligand. 

22 

23 Args: 

24 output_pdb_path (str): Path to the output PDB ligand file. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/output_ligand.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

26 * **ligand_code** (*str*) - (None) RSCB PDB ligand code. 

27 * **api_id** (*str*) - ("pdbe") Identifier of the PDB REST API from which the PDB structure will be downloaded. Values: pdbe (`PDB in Europe REST API <https://www.ebi.ac.uk/pdbe/pdbe-rest-api>`_), mmb (`MMB PDB mirror API <http://mmb.irbbarcelona.org/api/>`_). 

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

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

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

31 

32 Examples: 

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

34 

35 from biobb_io.api.ligand import ligand 

36 prop = { 

37 'ligand_code': 'CPB', 

38 'api_id': 'pdbe' 

39 } 

40 ligand(output_pdb_path='/path/to/newLigand.pdb', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: Protein Data Bank 

46 * license: Apache-2.0 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

53 def __init__(self, output_pdb_path, 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_pdb_path": output_pdb_path}} 

62 

63 # Properties specific for BB 

64 self.api_id = properties.get("api_id", "pdbe") 

65 self.ligand_code = properties.get("ligand_code", None) 

66 self.properties = properties 

67 

68 # Check the properties 

69 self.check_properties(properties) 

70 self.check_arguments() 

71 

72 def check_data_params(self, out_log, err_log): 

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

74 self.output_pdb_path = check_output_path( 

75 self.io_dict["out"]["output_pdb_path"], 

76 "output_pdb_path", 

77 False, 

78 out_log, 

79 self.__class__.__name__, 

80 ) 

81 

82 @launchlogger 

83 def launch(self) -> int: 

84 """Execute the :class:`Ligand <api.ligand.Ligand>` api.ligand.Ligand object.""" 

85 

86 # check input/output paths and parameters 

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

88 

89 # Setup Biobb 

90 if self.check_restart(): 

91 return 0 

92 

93 check_mandatory_property( 

94 self.ligand_code, "ligand_code", self.out_log, self.__class__.__name__ 

95 ) 

96 

97 self.ligand_code = self.ligand_code.strip().lower() 

98 

99 # Downloading PDB file 

100 pdb_string = download_ligand( 

101 self.ligand_code, self.api_id, self.out_log, self.global_log 

102 ) 

103 write_pdb(pdb_string, self.output_pdb_path, None, self.out_log, self.global_log) 

104 

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

106 

107 return 0 

108 

109 

110def ligand(output_pdb_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

111 """Execute the :class:`Ligand <api.ligand.Ligand>` class and 

112 execute the :meth:`launch() <api.ligand.Ligand.launch>` method.""" 

113 return Ligand(**dict(locals())).launch() 

114 

115 

116ligand.__doc__ = Ligand.__doc__ 

117main = Ligand.get_main(ligand, "Wrapper for the Protein Data Bank in Europe (https://www.ebi.ac.uk/pdbe/) and the MMB PDB mirror (http://mmb.irbbarcelona.org/api/) for downloading a single PDB ligand.") 

118 

119if __name__ == "__main__": 

120 main()