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

56 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2024-06-14 19:03 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from biobb_common.configuration import settings 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_common.tools import file_utils as fu 

9from biobb_structure_utils.utils.common import check_input_path, check_output_path_json 

10 

11 

12class StructureCheck(BiobbObject): 

13 """ 

14 | biobb_structure_utils StructureCheck 

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

16 | 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. 

17 

18 Args: 

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

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

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

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

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

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

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

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

27 

28 Examples: 

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

30 

31 from biobb_structure_utils.utils.structure_check import structure_check 

32 prop = { 

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

34 } 

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

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

37 properties=prop) 

38 

39 Info: 

40 * wrapped_software: 

41 * name: Structure Checking from MDWeb 

42 * version: >=3.0.3 

43 * license: Apache-2.0 

44 * ontology: 

45 * name: EDAM 

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

47 

48 """ 

49 

50 def __init__(self, input_structure_path, output_summary_path, properties=None, **kwargs) -> None: 

51 properties = properties or {} 

52 

53 # Call parent class constructor 

54 super().__init__(properties) 

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

56 

57 # Input/Output files 

58 self.io_dict = { 

59 "in": {"input_structure_path": input_structure_path}, 

60 "out": {"output_summary_path": output_summary_path} 

61 } 

62 

63 # Properties specific for BB 

64 self.binary_path = properties.get('binary_path', 'check_structure') 

65 self.features = properties.get('features', None) 

66 self.properties = properties 

67 

68 # Check the properties 

69 self.check_properties(properties) 

70 self.check_arguments() 

71 

72 @launchlogger 

73 def launch(self) -> int: 

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

75 

76 self.io_dict['in']['input_structure_path'] = check_input_path(self.io_dict['in']['input_structure_path'], 

77 self.out_log, self.__class__.__name__) 

78 self.io_dict['out']['output_summary_path'] = check_output_path_json(self.io_dict['out']['output_summary_path'], 

79 self.out_log, self.__class__.__name__) 

80 

81 # Setup Biobb 

82 if self.check_restart(): 

83 return 0 

84 self.stage_files() 

85 

86 tmp_folder = None 

87 

88 if not self.features or self.features is None or self.features == 'None': 

89 fu.log('No features provided, all features will be computed: %s' % 'models, chains, altloc, metals, ligands, chiral, getss, cistransbck, backbone, amide, clashes', self.out_log) 

90 

91 self.cmd = [self.binary_path, 

92 '-i', self.stage_io_dict['in']['input_structure_path'], 

93 '--json', self.stage_io_dict['out']['output_summary_path'], 

94 '--check_only', 

95 '--non_interactive', 

96 'checkall'] 

97 else: 

98 fu.log('Computing features: %s' % ', '.join(self.features), self.out_log) 

99 

100 # create temporary folder 

101 tmp_folder = fu.create_unique_dir() 

102 fu.log('Creating %s temporary folder' % tmp_folder, self.out_log) 

103 

104 command_list = tmp_folder + '/command_list.lst' 

105 

106 with open(command_list, 'w') as f: 

107 for item in self.features: 

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

109 

110 self.cmd = [self.binary_path, 

111 '-i', self.stage_io_dict['in']['input_structure_path'], 

112 '--json', self.stage_io_dict['out']['output_summary_path'], 

113 '--check_only', 

114 '--non_interactive', 

115 'command_list', 

116 '--list', 

117 command_list] 

118 

119 # Run Biobb block 

120 self.run_biobb() 

121 

122 # Copy files to host 

123 self.copy_to_host() 

124 

125 # Remove temporal files 

126 self.tmp_files.extend([ 

127 self.stage_io_dict.get("unique_dir"), 

128 tmp_folder 

129 ]) 

130 self.remove_tmp_files() 

131 

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

133 

134 return self.return_code 

135 

136 

137def structure_check(input_structure_path: str, output_summary_path: str, properties: dict = None, **kwargs) -> int: 

138 """Execute the :class:`StructureCheck <utils.structure_check.StructureCheck>` class and 

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

140 

141 return StructureCheck(input_structure_path=input_structure_path, 

142 output_summary_path=output_summary_path, 

143 properties=properties, **kwargs).launch() 

144 

145 

146def main(): 

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

148 parser = argparse.ArgumentParser(description="This class is a wrapper of the Structure Checking tool to generate summary checking results on a json file.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

149 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string") 

150 

151 # Specific args of each building block 

152 required_args = parser.add_argument_group('required arguments') 

153 required_args.add_argument('-i', '--input_structure_path', required=True, help="Input structure file path. Accepted formats: pdb.") 

154 required_args.add_argument('-o', '--output_summary_path', required=True, help="Output summary checking results. Accepted formats: json.") 

155 

156 args = parser.parse_args() 

157 config = args.config if args.config else None 

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

159 

160 # Specific call of each building block 

161 structure_check(input_structure_path=args.input_structure_path, 

162 output_summary_path=args.output_summary_path, 

163 properties=properties) 

164 

165 

166if __name__ == '__main__': 

167 main()