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

53 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-05-28 06:50 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5from pathlib import PurePath 

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 

10 

11 

12class Trjcat(BiobbObject): 

13 """ 

14 | biobb_gromacs Trjcat 

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

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

17 

18 Args: 

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

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

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

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

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

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

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 * **container_path** (*str*) - (None) Path to the binary executable of your container. 

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

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

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

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

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

34 

35 Examples: 

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

37 

38 from biobb_gromacs.gromacs.trjcat import trjcat 

39 prop = { 'concatenate': True } 

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

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

42 properties=prop) 

43 

44 Info: 

45 * wrapped_software: 

46 * name: GROMACS trjcat 

47 * version: 2025.2 

48 * license: LGPL 2.1 

49 * ontology: 

50 * name: EDAM 

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

52 """ 

53 

54 def __init__(self, input_trj_zip_path: str, output_trj_path: str, properties: Optional[dict] = None, **kwargs) -> 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: dict = { 

63 "in": {}, 

64 "out": {"output_trj_path": output_trj_path} 

65 } 

66 

67 # Should not be copied inside container 

68 self.input_trj_zip_path = input_trj_zip_path 

69 

70 # Properties specific for BB 

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

72 

73 # Properties common in all GROMACS BB 

74 self.gmx_lib = properties.get('gmx_lib', None) 

75 self.binary_path = properties.get('binary_path', 'gmx') 

76 self.gmx_nobackup = properties.get('gmx_nobackup', True) 

77 self.gmx_nocopyright = properties.get('gmx_nocopyright', True) 

78 if self.gmx_nobackup: 

79 self.binary_path += ' -nobackup' 

80 if self.gmx_nocopyright: 

81 self.binary_path += ' -nocopyright' 

82 if not self.container_path: 

83 self.gmx_version = get_gromacs_version(self.binary_path) 

84 

85 # Check the properties 

86 self.check_properties(properties) 

87 self.check_arguments() 

88 

89 @launchlogger 

90 def launch(self) -> int: 

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

92 

93 # Setup Biobb 

94 if self.check_restart(): 

95 return 0 

96 self.stage_files() 

97 

98 # Unzip trajectory bundle directly into unique_dir so basenames work after cd 

99 trj_list: list[str] = fu.unzip_list(self.input_trj_zip_path, self.stage_io_dict.get("unique_dir", ""), self.out_log) 

100 trj_list = [PurePath(p).name for p in trj_list] 

101 

102 if self.container_path: 

103 working_dir = self.container_volume_path if self.container_volume_path else "/data" 

104 else: 

105 working_dir = self.stage_io_dict.get('unique_dir', '') 

106 

107 # Create command line 

108 self.cmd = ["cd", working_dir, ";", 

109 self.binary_path, 'trjcat', 

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

111 '-o', PurePath(self.stage_io_dict["out"]["output_trj_path"]).name] 

112 

113 if self.concatenate: 

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

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

116 

117 if self.gmx_lib: 

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

119 

120 # Run Biobb block 

121 self.run_biobb() 

122 

123 # Copy files to host 

124 self.copy_to_host() 

125 

126 # Remove temporal files 

127 self.remove_tmp_files() 

128 

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

130 return self.return_code 

131 

132 

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

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

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

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

137 

138 

139trjcat.__doc__ = Trjcat.__doc__ 

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

141 

142 

143if __name__ == '__main__': 

144 main()