Coverage for biobb_structure_utils/utils/sort_gro_residues.py: 73%

45 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-28 11:54 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the SortGroResidues 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_structure_utils.gro_lib.gro import Gro 

13from biobb_structure_utils.utils.common import _from_string_to_list 

14 

15 

16class SortGroResidues(BiobbObject): 

17 """ 

18 | biobb_structure_utils SortGroResidues 

19 | Class to sort the selected residues from a GRO 3D structure. 

20 | Sorts the selected residues from a GRO 3D structure. 

21 

22 Args: 

23 input_gro_path (str): Input GRO file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/WT_aq4_md_1.gro>`_. Accepted formats: gro (edam:format_2033). 

24 output_gro_path (str): Output sorted GRO file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/reference/utils/WT_aq4_md_sorted.gro>`_. Accepted formats: gro (edam:format_2033). 

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

26 * **residue_name_list** (*list*) - (["NA", "CL", "SOL"]) Ordered residue name list. 

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

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

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

30 

31 Examples: 

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

33 

34 from biobb_structure_utils.utils.sort_gro_residues import sort_gro_residues 

35 prop = { 

36 'residue_name_list': ['NA', 'CL', 'SOL'] 

37 } 

38 sort_gro_residues(input_gro_path='/path/to/myInputStr.gro', 

39 output_gro_path='/path/to/newStructure.gro', 

40 properties=prop) 

41 

42 Info: 

43 * wrapped_software: 

44 * name: In house 

45 * license: Apache-2.0 

46 * ontology: 

47 * name: EDAM 

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

49 

50 """ 

51 

52 def __init__( 

53 self, input_gro_path, output_gro_path, properties=None, **kwargs 

54 ) -> None: 

55 properties = properties or {} 

56 

57 # Call parent class constructor 

58 super().__init__(properties) 

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

60 

61 # Input/Output files 

62 self.io_dict = { 

63 "in": {"input_gro_path": input_gro_path}, 

64 "out": {"output_gro_path": output_gro_path}, 

65 } 

66 

67 # Properties specific for BB 

68 self.residue_name_list = _from_string_to_list( 

69 properties.get("residue_name_list", ["NA", "CL", "SOL"]) 

70 ) 

71 

72 # Check the properties 

73 self.check_properties(properties) 

74 self.check_arguments() 

75 

76 @launchlogger 

77 def launch(self) -> int: 

78 """Execute the :class:`SortGroResidues <utils.sort_gro_residues.SortGroResidues>` utils.sort_gro_residues.SortGroResidues object.""" 

79 

80 # Setup Biobb 

81 if self.check_restart(): 

82 return 0 

83 self.stage_files() 

84 

85 # Business code 

86 in_gro = Gro() 

87 in_gro.read_gro_file(self.stage_io_dict["in"]["input_gro_path"]) 

88 in_gro.sort_residues2(self.residue_name_list) 

89 in_gro.write_gro_file(self.stage_io_dict["out"]["output_gro_path"]) 

90 self.return_code = 0 

91 ########## 

92 

93 # Copy files to host 

94 self.copy_to_host() 

95 

96 # Remove temporal files 

97 # self.tmp_files.append(self.stage_io_dict.get("unique_dir", "")) 

98 self.remove_tmp_files() 

99 

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

101 

102 return self.return_code 

103 

104 

105def sort_gro_residues( 

106 input_gro_path: str, 

107 output_gro_path: str, 

108 properties: Optional[dict] = None, 

109 **kwargs, 

110) -> int: 

111 """Execute the :class:`SortGroResidues <utils.sort_gro_residues.SortGroResidues>` class and 

112 execute the :meth:`launch() <utils.sort_gro_residues.SortGroResidues.launch>` method.""" 

113 

114 return SortGroResidues( 

115 input_gro_path=input_gro_path, 

116 output_gro_path=output_gro_path, 

117 properties=properties, 

118 **kwargs, 

119 ).launch() 

120 

121 sort_gro_residues.__doc__ = SortGroResidues.__doc__ 

122 

123 

124def main(): 

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

126 parser = argparse.ArgumentParser( 

127 description="Renumber atoms and residues from a 3D structure.", 

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

129 ) 

130 parser.add_argument( 

131 "-c", 

132 "--config", 

133 required=False, 

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

135 ) 

136 

137 # Specific args of each building block 

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

139 required_args.add_argument( 

140 "-i", "--input_gro_path", required=True, help="Input GRO file name" 

141 ) 

142 required_args.add_argument( 

143 "-o", "--output_gro_path", required=True, help="Output sorted GRO file name" 

144 ) 

145 

146 args = parser.parse_args() 

147 config = args.config if args.config else None 

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

149 

150 # Specific call of each building block 

151 sort_gro_residues( 

152 input_gro_path=args.input_gro_path, 

153 output_gro_path=args.output_gro_path, 

154 properties=properties, 

155 ) 

156 

157 

158if __name__ == "__main__": 

159 main()