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

34 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 SortGroResidues class and the command line interface.""" 

4 

5from typing import Optional 

6 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools.file_utils import launchlogger 

9 

10from biobb_structure_utils.gro_lib.gro import Gro 

11from biobb_structure_utils.utils.common import _from_string_to_list 

12 

13 

14class SortGroResidues(BiobbObject): 

15 """ 

16 | biobb_structure_utils SortGroResidues 

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

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

19 

20 Args: 

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

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

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

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

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

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

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

28 

29 Examples: 

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

31 

32 from biobb_structure_utils.utils.sort_gro_residues import sort_gro_residues 

33 prop = { 

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

35 } 

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

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

38 properties=prop) 

39 

40 Info: 

41 * wrapped_software: 

42 * name: In house 

43 * license: Apache-2.0 

44 * ontology: 

45 * name: EDAM 

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

47 

48 """ 

49 

50 def __init__( 

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

52 ) -> None: 

53 properties = properties or {} 

54 

55 # Call parent class constructor 

56 super().__init__(properties) 

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

58 

59 # Input/Output files 

60 self.io_dict = { 

61 "in": {"input_gro_path": input_gro_path}, 

62 "out": {"output_gro_path": output_gro_path}, 

63 } 

64 

65 # Properties specific for BB 

66 self.residue_name_list = _from_string_to_list( 

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

68 ) 

69 

70 # Check the properties 

71 self.check_properties(properties) 

72 self.check_arguments() 

73 

74 @launchlogger 

75 def launch(self) -> int: 

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

77 

78 # Setup Biobb 

79 if self.check_restart(): 

80 return 0 

81 self.stage_files() 

82 

83 # Business code 

84 in_gro = Gro() 

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

86 in_gro.sort_residues2(self.residue_name_list) 

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

88 self.return_code = 0 

89 ########## 

90 

91 # Copy files to host 

92 self.copy_to_host() 

93 

94 # Remove temporal files 

95 self.remove_tmp_files() 

96 

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

98 

99 return self.return_code 

100 

101 

102def sort_gro_residues( 

103 input_gro_path: str, 

104 output_gro_path: str, 

105 properties: Optional[dict] = None, 

106 **kwargs, 

107) -> int: 

108 """Create the :class:`SortGroResidues <utils.sort_gro_residues.SortGroResidues>` class and 

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

110 return SortGroResidues(**dict(locals())).launch() 

111 

112 

113sort_gro_residues.__doc__ = SortGroResidues.__doc__ 

114main = SortGroResidues.get_main(sort_gro_residues, "Renumber atoms and residues from a 3D structure.") 

115 

116if __name__ == "__main__": 

117 main()