Coverage for biobb_structure_utils/utils/sort_gro_residues.py: 74%
43 statements
« prev ^ index » next coverage.py v7.5.3, created at 2024-06-14 19:03 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2024-06-14 19:03 +0000
1#!/usr/bin/env python3
3"""Module containing the SortGroResidues 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_structure_utils.gro_lib.gro import Gro
11class SortGroResidues(BiobbObject):
12 """
13 | biobb_structure_utils SortGroResidues
14 | Class to sort the selected residues from a GRO 3D structure.
15 | Sorts the selected residues from a GRO 3D structure.
17 Args:
18 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).
19 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).
20 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
21 * **residue_name_list** (*list*) - (["NA", "CL", "SOL"]) Ordered residue name list.
22 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
23 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
24 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
26 Examples:
27 This is a use example of how to use the building block from Python::
29 from biobb_structure_utils.utils.sort_gro_residues import sort_gro_residues
30 prop = {
31 'residue_name_list': ['NA', 'CL', 'SOL']
32 }
33 sort_gro_residues(input_gro_path='/path/to/myInputStr.gro',
34 output_gro_path='/path/to/newStructure.gro',
35 properties=prop)
37 Info:
38 * wrapped_software:
39 * name: In house
40 * license: Apache-2.0
41 * ontology:
42 * name: EDAM
43 * schema: http://edamontology.org/EDAM.owl
45 """
47 def __init__(self, input_gro_path, output_gro_path, properties=None, **kwargs) -> None:
48 properties = properties or {}
50 # Call parent class constructor
51 super().__init__(properties)
52 self.locals_var_dict = locals().copy()
54 # Input/Output files
55 self.io_dict = {
56 "in": {"input_gro_path": input_gro_path},
57 "out": {"output_gro_path": output_gro_path}
58 }
60 # Properties specific for BB
61 self.residue_name_list = properties.get('residue_name_list', ["NA", "CL", "SOL"])
63 # Check the properties
64 self.check_properties(properties)
65 self.check_arguments()
67 @launchlogger
68 def launch(self) -> int:
69 """Execute the :class:`SortGroResidues <utils.sort_gro_residues.SortGroResidues>` utils.sort_gro_residues.SortGroResidues object."""
71 # Setup Biobb
72 if self.check_restart():
73 return 0
74 self.stage_files()
76 # Business code
77 in_gro = Gro()
78 in_gro.read_gro_file(self.stage_io_dict['in']['input_gro_path'])
79 in_gro.sort_residues2(self.residue_name_list)
80 in_gro.write_gro_file(self.stage_io_dict['out']['output_gro_path'])
81 self.return_code = 0
82 ##########
84 # Copy files to host
85 self.copy_to_host()
87 # Remove temporal files
88 self.tmp_files.append(self.stage_io_dict.get("unique_dir"))
89 self.remove_tmp_files()
91 self.check_arguments(output_files_created=True, raise_exception=False)
93 return self.return_code
96def sort_gro_residues(input_gro_path: str, output_gro_path: str, properties: dict = None, **kwargs) -> int:
97 """Execute the :class:`SortGroResidues <utils.sort_gro_residues.SortGroResidues>` class and
98 execute the :meth:`launch() <utils.sort_gro_residues.SortGroResidues.launch>` method."""
100 return SortGroResidues(input_gro_path=input_gro_path,
101 output_gro_path=output_gro_path,
102 properties=properties, **kwargs).launch()
105def main():
106 """Command line execution of this building block. Please check the command line documentation."""
107 parser = argparse.ArgumentParser(description="Renumber atoms and residues from a 3D structure.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
108 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string")
110 # Specific args of each building block
111 required_args = parser.add_argument_group('required arguments')
112 required_args.add_argument('-i', '--input_gro_path', required=True, help="Input GRO file name")
113 required_args.add_argument('-o', '--output_gro_path', required=True, help="Output sorted GRO file name")
115 args = parser.parse_args()
116 config = args.config if args.config else None
117 properties = settings.ConfReader(config=config).get_prop_dic()
119 # Specific call of each building block
120 sort_gro_residues(input_gro_path=args.input_gro_path,
121 output_gro_path=args.output_gro_path,
122 properties=properties)
125if __name__ == '__main__':
126 main()