Coverage for biobb_mem/gromacs/gmx_order.py: 69%

48 statements  

« prev     ^ index     » next       coverage.py v7.6.11, created at 2025-02-10 11:25 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the GROMACS order class and the command line interface.""" 

4import argparse 

5from pathlib import PurePath 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools.file_utils import launchlogger 

9 

10 

11class GMXOrder(BiobbObject): 

12 """ 

13 | biobb_mem GMXOrder 

14 | Wrapper of the GROMACS order module for computing lipid order parameters per atom for carbon tails. 

15 | `GROMCAS order <https://manual.gromacs.org/current/onlinehelp/gmx-order.html>`_ only works for saturated carbons and united atom force fields. 

16 

17 Args: 

18 input_top_path (str): Path to the input structure or topology file. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/ambertools/topology.tpr>`_. Accepted formats: tpr (edam:format_2333). 

19 input_traj_path (str): Path to the input trajectory to be processed. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/ambertools/trajectory.xtc>`_. Accepted formats: xtc (edam:format_3875), trr (edam:format_3910), cpt (edam:format_2333), gro (edam:format_2033), g96 (edam:format_2033), pdb (edam:format_1476), tng (edam:format_3876).. 

20 input_index_path (str): Path to the GROMACS index file. File type: input. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/ >`_. Accepted formats: ndx (edam:format_2033). 

21 output_deuter_path (str): Path to deuterium order parameters xvgr/xmgr file. File type: output. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/gromacs/deuter.xvg>`_. Accepted formats: xvg (edam:format_2330). 

22 output_order_path (str) (Optional): Path to order tensor diagonal elements xvgr/xmgr file. File type: output. `Sample file <https://github.com/bioexcel/biobb_mem/raw/main/biobb_mem/test/data/gromacs/order.xvg>`_. Accepted formats: xvg (edam:format_2330). 

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

24 * **d** (*str*) - ("z") Direction of the normal on the membrane: z, x, y. 

25 * **binary_path** (*str*) - ("cpptraj") Path to the cpptraj 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 

30 Examples: 

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

32 

33 from biobb_mem.ambertools.gmx_order import gmx_order 

34 prop = { 

35 'd': 'z' 

36 } 

37 gmx_order(input_top_path='/path/to/myTopology.top', 

38 input_traj_path='/path/to/myTrajectory.xtc', 

39 output_deuter_path='/path/to/deuterAnalysis.xvg', 

40 output_order_path='/path/to/orderAnalysis.xvg', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: GROMACS order 

46 * version: 2024.2 

47 * license: LGPL 2.1 

48 * ontology: 

49 * name: EDAM 

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

51 

52 """ 

53 

54 def __init__(self, input_top_path, input_traj_path, input_index_path=None, 

55 output_deuter_path=None, output_order_path=None, properties=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 = { 

64 "in": { 

65 "input_top_path": input_top_path, 

66 "input_traj_path": input_traj_path, 

67 "input_index_path": input_index_path 

68 }, 

69 "out": { 

70 "output_deuter_path": output_deuter_path, 

71 "output_order_path": output_order_path 

72 } 

73 } 

74 

75 # Properties specific for BB 

76 self.d = properties.get('d', 'z') 

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

78 self.properties = properties 

79 

80 # Check the properties 

81 self.check_properties(properties) 

82 self.check_arguments() 

83 

84 @launchlogger 

85 def launch(self) -> int: 

86 """Execute the :class:`GMXOrder <gromacs.gmx_order.GMXOrder>` gromacs.gmx_order.GMXOrder object.""" 

87 

88 # Setup Biobb 

89 if self.check_restart(): 

90 return 0 

91 self.stage_files() 

92 

93 # Create cmd and launch execution 

94 cmd = [self.binary_path, 'order', 

95 '-s', self.stage_io_dict["in"]["input_top_path"], 

96 '-f', self.stage_io_dict["in"]["input_traj_path"], 

97 '-n', self.stage_io_dict["in"]["input_index_path"], 

98 '-od', self.stage_io_dict["out"]["output_deuter_path"], 

99 '-d', self.d] 

100 

101 if self.stage_io_dict["out"].get("output_order_path"): 

102 cmd.extend(['-o', self.stage_io_dict["out"]["output_order_path"]]) 

103 else: 

104 cmd.extend(['-o', str(PurePath(self.stage_io_dict["unique_dir"]).joinpath('order.xvg'))]) 

105 

106 self.cmd = cmd 

107 

108 # Run Biobb block 

109 self.run_biobb() 

110 

111 # Copy files to host 

112 self.copy_to_host() 

113 

114 # Remove temporary folder(s) 

115 self.tmp_files.append(self.stage_io_dict.get("unique_dir")) 

116 

117 self.remove_tmp_files() 

118 

119 return self.return_code 

120 

121 

122def gmx_order(input_top_path: str, input_traj_path: str, input_index_path: str = None, 

123 output_deuter_path: str = None, output_order_path: str = None, 

124 properties: dict = None, **kwargs) -> int: 

125 """Create :class:`GMXOrder <gromacs.gmx_order.GMXOrder>` class and 

126 execute :meth:`launch() <gromacs.gmx_order.GMXOrder.launch>` method""" 

127 

128 return GMXOrder(input_top_path=input_top_path, 

129 input_traj_path=input_traj_path, 

130 input_index_path=input_index_path, 

131 output_deuter_path=output_deuter_path, 

132 output_order_path=output_order_path, 

133 properties=properties, **kwargs).launch() 

134 

135 

136def main(): 

137 """Command line execution of this building block. Please check the command line documentation.""" 

138 parser = argparse.ArgumentParser(description="Compute lipid order parameters using GROMACS order tool.", 

139 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

140 parser.add_argument('--config', required=False, help='Configuration file') 

141 

142 # Specific args of each building block 

143 required_args = parser.add_argument_group('required arguments') 

144 required_args.add_argument('--input_top_path', required=True, help='Path to the input structure or topology file.') 

145 required_args.add_argument('--input_traj_path', required=True, help='Path to the input trajectory to be processed.') 

146 parser.add_argument('--input_index_path', required=False, help='Path to the GROMACS index file.') 

147 parser.add_argument('--output_deuter_path', required=False, help='Path to deuterium order parameters output file.') 

148 parser.add_argument('--output_order_path', required=False, help='Path to order tensor diagonal elements output file.') 

149 

150 args = parser.parse_args() 

151 args.config = args.config or "{}" 

152 properties = settings.ConfReader(config=args.config).get_prop_dic() 

153 

154 # Specific call of each building block 

155 gmx_order(input_top_path=args.input_top_path, 

156 input_traj_path=args.input_traj_path, 

157 input_index_path=args.input_index_path, 

158 output_deuter_path=args.output_deuter_path, 

159 output_order_path=args.output_order_path, 

160 properties=properties) 

161 

162 

163if __name__ == '__main__': 

164 main()