Coverage for biobb_gromacs / gromacs / gmxselect.py: 79%
58 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-05 08:26 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-05 08:26 +0000
1#!/usr/bin/env python3
3"""Module containing the Select class and the command line interface."""
4from typing import Optional
5from pathlib import Path
6from biobb_common.generic.biobb_object import BiobbObject
7from biobb_common.tools import file_utils as fu
8from biobb_common.tools.file_utils import launchlogger
9from biobb_gromacs.gromacs.common import get_gromacs_version
12class Gmxselect(BiobbObject):
13 """
14 | biobb_gromacs Gmxselect
15 | Wrapper of the `GROMACS select <http://manual.gromacs.org/current/onlinehelp/gmx-select.html>`_ module.
16 | The GROMACS select module writes out basic data about dynamic selections. It can be used for some simple analyses, or the output can be combined with output from other programs and/or external analysis programs to calculate more complex things.
18 Args:
19 input_structure_path (str): Path to the input GRO/PDB/TPR file. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/data/gromacs/make_ndx.tpr>`_. Accepted formats: pdb (edam:format_1476), gro (edam:format_2033), tpr (edam:format_2333).
20 output_ndx_path (str): Path to the output index NDX file. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_select.ndx>`_. Accepted formats: ndx (edam:format_2033).
21 input_ndx_path (str) (Optional): Path to the input index NDX file. File type: input. Accepted formats: ndx (edam:format_2033).
22 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
23 * **selection** (*str*) - ("a CA C N O") Heavy atoms. Atom selection string.
24 * **append** (*bool*) - (False) Append the content of the input_ndx_path to the output_ndx_path.
25 * **binary_path** (*str*) - ("gmx") Path to the GROMACS executable binary.
26 * **gmx_lib** (*str*) - (None) Path set GROMACS GMXLIB environment variable.
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 * **container_path** (*str*) - (None) Path to the binary executable of your container.
31 * **container_image** (*str*) - ("gromacs/gromacs:latest") Container Image identifier.
32 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container.
33 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container.
34 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container.
35 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell.
37 Examples:
38 This is a use example of how to use the building block from Python::
40 from biobb_gromacs.gromacs.gmxselect import gmxselect
41 prop = { 'selection': '"Mynewgroup" group "Protein-H" and not same residue as within 0.4 of resname ARG' }
42 gmxselect(input_structure_path='/path/to/myStructure.gro',
43 output_ndx_path='/path/to/newIndex.ndx',
44 properties=prop)
46 Info:
47 * wrapped_software:
48 * name: GROMACS Gmxselect
49 * version: 2025.2
50 * license: LGPL 2.1
51 * ontology:
52 * name: EDAM
53 * schema: http://edamontology.org/EDAM.owl
54 """
56 def __init__(self, input_structure_path: str, output_ndx_path: str, input_ndx_path: Optional[str] = None,
57 properties: Optional[dict] = None, **kwargs) -> None:
58 properties = properties or {}
60 # Call parent class constructor
61 super().__init__(properties)
62 self.locals_var_dict = locals().copy()
64 # Input/Output files
65 self.io_dict = {
66 "in": {"input_structure_path": input_structure_path, "input_ndx_path": input_ndx_path},
67 "out": {"output_ndx_path": output_ndx_path}
68 }
70 # Properties specific for BB
71 self.selection = properties.get('selection', "a CA C N O")
72 self.append = properties.get('append', False)
74 # Properties common in all GROMACS BB
75 self.gmx_lib = properties.get('gmx_lib', None)
76 self.binary_path = properties.get('binary_path', 'gmx')
77 self.gmx_nobackup = properties.get('gmx_nobackup', True)
78 self.gmx_nocopyright = properties.get('gmx_nocopyright', True)
79 if self.gmx_nobackup:
80 self.binary_path += ' -nobackup'
81 if self.gmx_nocopyright:
82 self.binary_path += ' -nocopyright'
83 if not self.container_path:
84 self.gmx_version = get_gromacs_version(self.binary_path)
86 # Check the properties
87 self.check_properties(properties)
88 self.check_arguments()
90 @launchlogger
91 def launch(self) -> int:
92 """Execute the :class:`Gmxselect <gromacs.gmxselect.Gmxselect>` object."""
94 # Setup Biobb
95 if self.check_restart():
96 return 0
97 self.stage_files()
99 self.cmd = [self.binary_path, 'select',
100 '-s', self.stage_io_dict["in"]["input_structure_path"],
101 '-on', self.stage_io_dict["out"]["output_ndx_path"]
102 ]
104 if self.stage_io_dict["in"].get("input_ndx_path") and Path(
105 self.stage_io_dict["in"].get("input_ndx_path")).exists():
106 self.cmd.append('-n')
107 self.cmd.append(self.stage_io_dict["in"].get("input_ndx_path"))
109 self.cmd.append('-select')
110 self.cmd.append("\'"+self.selection+"\'")
112 if self.gmx_lib:
113 self.env_vars_dict['GMXLIB'] = self.gmx_lib
115 # Run Biobb block
116 self.run_biobb()
118 # Copy files to host
119 self.copy_to_host()
121 if self.io_dict["in"].get("input_ndx_path"):
122 if self.append:
123 fu.log(f"Appending {self.io_dict['in'].get('input_ndx_path')} to {self.io_dict['out']['output_ndx_path']}", self.out_log, self.global_log)
124 with open(self.io_dict["out"]["output_ndx_path"], 'a') as out_ndx_file:
125 out_ndx_file.write('\n')
126 with open(self.io_dict["in"].get("input_ndx_path", '')) as in_ndx_file:
127 for line in in_ndx_file:
128 out_ndx_file.write(line)
130 # Remove temporal files
131 self.remove_tmp_files()
133 self.check_arguments(output_files_created=True, raise_exception=False)
134 return self.return_code
137def gmxselect(input_structure_path: str, output_ndx_path: str,
138 input_ndx_path: Optional[str] = None, properties: Optional[dict] = None,
139 **kwargs) -> int:
140 """Create :class:`Gmxselect <gromacs.gmxselect.Gmxselect>` class and
141 execute the :meth:`launch() <gromacs.gmxselect.Gmxselect.launch>` method."""
142 return Gmxselect(**dict(locals())).launch()
145gmxselect.__doc__ = Gmxselect.__doc__
146main = Gmxselect.get_main(gmxselect, "Wrapper for the GROMACS select module.")
149if __name__ == '__main__':
150 main()