Coverage for biobb_structure_utils / utils / structure_check.py: 91%

47 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-22 13:23 +0000

1#!/usr/bin/env python3 

2 

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

4 

5from typing import Optional 

6 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10 

11from biobb_structure_utils.utils.common import ( 

12 _from_string_to_list, 

13 check_input_path, 

14 check_output_path_json, 

15) 

16 

17 

18class StructureCheck(BiobbObject): 

19 """ 

20 | biobb_structure_utils StructureCheck 

21 | This class is a wrapper of the Structure Checking tool to generate summary checking results on a json file. 

22 | Wrapper for the `Structure Checking <https://github.com/bioexcel/biobb_structure_checking>`_ tool to generate summary checking results on a json file from a given structure and a list of features. 

23 

24 Args: 

25 input_structure_path (str): Input structure file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/2vgb.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476). 

26 output_summary_path (str): Output summary checking results. File type: output. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/reference/utils/summary.json>`_. Accepted formats: json (edam:format_3464). 

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

28 * **features** (*list*) - (None) Features to summarize. If None, all the features will be computed. Values: models (multiple molecules or coordinate sets in a single file), chains (multiple chains in a single file), altloc (atom alternative conformation given an alternate location indicator and occupancy), metals (metals present in the structure), ligands (heteroatoms present in the structure), chiral (to say that a structure is chiral is to say that its mirror image is not the same as it self), getss (detect SS bonds or disulfides), cistransbck (detact cis/trans backbone), backbone (detect backbone breaks), amide (detect too close amides), clashes (detect clashes). 

29 * **binary_path** (*string*) - ("check_structure") path to the check_structure application 

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

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

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

33 

34 Examples: 

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

36 

37 from biobb_structure_utils.utils.structure_check import structure_check 

38 prop = { 

39 'features': ['models', 'chains', 'ligands'] 

40 } 

41 structure_check(input_structure_path='/path/to/myInputStr.pdb', 

42 output_summary_path='/path/to/newSummary.json', 

43 properties=prop) 

44 

45 Info: 

46 * wrapped_software: 

47 * name: Structure Checking from MDWeb 

48 * version: >=3.0.3 

49 * license: Apache-2.0 

50 * ontology: 

51 * name: EDAM 

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

53 

54 """ 

55 

56 def __init__( 

57 self, input_structure_path, output_summary_path, properties=None, **kwargs 

58 ) -> None: 

59 properties = properties or {} 

60 

61 # Call parent class constructor 

62 super().__init__(properties) 

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

64 

65 # Input/Output files 

66 self.io_dict = { 

67 "in": {"input_structure_path": input_structure_path}, 

68 "out": {"output_summary_path": output_summary_path}, 

69 } 

70 

71 # Properties specific for BB 

72 self.binary_path = properties.get("binary_path", "check_structure") 

73 self.features = _from_string_to_list(properties.get("features", None)) 

74 self.properties = properties 

75 

76 # Check the properties 

77 self.check_properties(properties) 

78 self.check_arguments() 

79 

80 @launchlogger 

81 def launch(self) -> int: 

82 """Execute the :class:`StructureCheck <utils.structure_check.StructureCheck>` utils.structure_check.StructureCheck object.""" 

83 

84 self.io_dict["in"]["input_structure_path"] = check_input_path( 

85 self.io_dict["in"]["input_structure_path"], 

86 self.out_log, 

87 self.__class__.__name__, 

88 ) 

89 self.io_dict["out"]["output_summary_path"] = check_output_path_json( 

90 self.io_dict["out"]["output_summary_path"], 

91 self.out_log, 

92 self.__class__.__name__, 

93 ) 

94 

95 # Setup Biobb 

96 if self.check_restart(): 

97 return 0 

98 self.stage_files() 

99 

100 tmp_folder = None 

101 

102 if not self.features or self.features is None or self.features == "None": 

103 fu.log( 

104 "No features provided, all features will be computed: %s" 

105 % "models, chains, altloc, metals, ligands, chiral, getss, cistransbck, backbone, amide, clashes", 

106 self.out_log, 

107 ) 

108 

109 self.cmd = [ 

110 self.binary_path, 

111 "-i", 

112 self.stage_io_dict["in"]["input_structure_path"], 

113 "--json", 

114 self.stage_io_dict["out"]["output_summary_path"], 

115 "--check_only", 

116 "--non_interactive", 

117 "checkall", 

118 ] 

119 else: 

120 fu.log("Computing features: %s" % ", ".join(self.features), self.out_log) 

121 

122 # create temporary folder 

123 tmp_folder = fu.create_unique_dir() 

124 fu.log("Creating %s temporary folder" % tmp_folder, self.out_log) 

125 

126 command_list = tmp_folder + "/command_list.lst" 

127 

128 with open(command_list, "w") as f: 

129 for item in self.features: 

130 f.write("%s\n" % item) 

131 

132 self.cmd = [ 

133 self.binary_path, 

134 "-i", 

135 self.stage_io_dict["in"]["input_structure_path"], 

136 "--json", 

137 self.stage_io_dict["out"]["output_summary_path"], 

138 "--check_only", 

139 "--non_interactive", 

140 "command_list", 

141 "--list", 

142 command_list, 

143 ] 

144 

145 # Run Biobb block 

146 self.run_biobb() 

147 

148 # Copy files to host 

149 self.copy_to_host() 

150 

151 # Remove temporal files 

152 self.tmp_files.append(tmp_folder) 

153 self.remove_tmp_files() 

154 

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

156 

157 return self.return_code 

158 

159 

160def structure_check( 

161 input_structure_path: str, 

162 output_summary_path: str, 

163 properties: Optional[dict] = None, 

164 **kwargs, 

165) -> int: 

166 """Create the :class:`StructureCheck <utils.structure_check.StructureCheck>` class and 

167 execute the :meth:`launch() <utils.structure_check.StructureCheck.launch>` method.""" 

168 return StructureCheck(**dict(locals())).launch() 

169 

170 

171structure_check.__doc__ = StructureCheck.__doc__ 

172main = StructureCheck.get_main(structure_check, "This class is a wrapper of the Structure Checking tool to generate summary checking results on a json file.") 

173 

174if __name__ == "__main__": 

175 main()