Coverage for biobb_gromacs / gromacs / trjcat.py: 89%

56 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-05 08:26 +0000

1#!/usr/bin/env python3 

2 

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

4import shutil 

5from typing import Optional 

6from pathlib import Path 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_gromacs.gromacs.common import get_gromacs_version 

11 

12 

13class Trjcat(BiobbObject): 

14 """ 

15 | biobb_gromacs Trjcat 

16 | Wrapper class for the `GROMACS trjcat <http://manual.gromacs.org/current/onlinehelp/gmx-trjcat.html>`_ module. 

17 | The GROMACS solvate module generates a box around the selected structure. 

18 

19 Args: 

20 input_trj_zip_path (str): Path the input GROMACS trajectories (xtc, trr, cpt, gro, pdb, tng) to concatenate in zip format. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/data/gromacs/trjcat.zip>`_. Accepted formats: zip (edam:format_3987). 

21 output_trj_path (str): Path to the output trajectory file. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_trjcat.trr>`_. Accepted formats: pdb (edam:format_1476), gro (edam:format_2033), xtc (edam:format_3875), trr (edam:format_3910), tng (edam:format_3876). 

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

23 * **concatenate** (*bool*) - (True) Only concatenate the files without removal of frames with identical timestamps. 

24 * **gmx_lib** (*str*) - (None) Path set GROMACS GMXLIB environment variable. 

25 * **binary_path** (*str*) - ("gmx") Path to the GROMACS executable binary. 

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

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

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

29 * **container_path** (*str*) - (None) Path to the binary executable of your container. 

30 * **container_image** (*str*) - (None) Container Image identifier. 

31 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container. 

32 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container. 

33 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container. 

34 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell. 

35 

36 Examples: 

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

38 

39 from biobb_gromacs.gromacs.trjcat import trjcat 

40 prop = { 'concatenate': True } 

41 trjcat(input_trj_zip_path='/path/to/trajectory_bundle.zip', 

42 output_gro_path='/path/to/concatenated_trajectories.xtc', 

43 properties=prop) 

44 

45 Info: 

46 * wrapped_software: 

47 * name: GROMACS trjcat 

48 * version: 2025.2 

49 * license: LGPL 2.1 

50 * ontology: 

51 * name: EDAM 

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

53 """ 

54 

55 def __init__(self, input_trj_zip_path: str, output_trj_path: str, properties: Optional[dict] = None, **kwargs) -> None: 

56 properties = properties or {} 

57 

58 # Call parent class constructor 

59 super().__init__(properties) 

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

61 

62 # Input/Output files 

63 self.io_dict: dict = { 

64 "in": {}, 

65 "out": {"output_trj_path": output_trj_path} 

66 } 

67 

68 # Should not be copied inside container 

69 self.input_trj_zip_path = input_trj_zip_path 

70 

71 # Properties specific for BB 

72 self.concatenate: bool = properties.get('concatenate', True) 

73 

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) 

85 

86 # Check the properties 

87 self.check_properties(properties) 

88 self.check_arguments() 

89 

90 @launchlogger 

91 def launch(self) -> int: 

92 """Execute the :class:`Trjcat <gromacs.trjcat.Trjcat>` object.""" 

93 

94 # Setup Biobb 

95 if self.check_restart(): 

96 return 0 

97 self.stage_files() 

98 

99 # Unzip trajectory bundle 

100 trj_dir: str = fu.create_unique_dir() 

101 trj_list: list[str] = fu.unzip_list(self.input_trj_zip_path, trj_dir, self.out_log) 

102 

103 # Copy trajectories to container 

104 if self.container_path: 

105 for index, trajectory_file_path in enumerate(trj_list): 

106 shutil.copy2(trajectory_file_path, self.stage_io_dict.get("unique_dir", "")) 

107 trj_list[index] = str(Path(self.container_volume_path).joinpath(Path(trajectory_file_path).name)) 

108 

109 # Create command line 

110 self.cmd = [self.binary_path, 'trjcat', 

111 '-f', " ".join(trj_list), 

112 '-o', self.stage_io_dict["out"]["output_trj_path"]] 

113 

114 if self.concatenate: 

115 self.cmd.append('-cat') 

116 fu.log('Only concatenate the files without removal of frames with identical timestamps.', self.out_log, self.global_log) 

117 

118 if self.gmx_lib: 

119 self.env_vars_dict['GMXLIB'] = self.gmx_lib 

120 

121 # Run Biobb block 

122 self.run_biobb() 

123 

124 # Copy files to host 

125 self.copy_to_host() 

126 

127 # Remove temporal files 

128 self.tmp_files.append(trj_dir) 

129 self.remove_tmp_files() 

130 

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

132 return self.return_code 

133 

134 

135def trjcat(input_trj_zip_path: str, output_trj_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

136 """Create :class:`Trjcat <gromacs.trjcat.Trjcat>` class and 

137 execute the :meth:`launch() <gromacs.trjcat.Trjcat.launch>` method.""" 

138 return Trjcat(**dict(locals())).launch() 

139 

140 

141trjcat.__doc__ = Trjcat.__doc__ 

142main = Trjcat.get_main(trjcat, "Wrapper for the GROMACS trjcat module.") 

143 

144 

145if __name__ == '__main__': 

146 main()