Coverage for biobb_pmx/pmxbiobb/pmxanalyse.py: 82%

66 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-03 15:52 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the PMX analyse class and the command line interface.""" 

4 

5from pathlib import Path 

6from typing import Optional 

7 

8from biobb_common.generic.biobb_object import BiobbObject, launcher 

9from biobb_common.tools import file_utils as fu 

10from biobb_pmx.pmxbiobb.common import check_binary 

11 

12 

13class Pmxanalyse(BiobbObject): 

14 """ 

15 | biobb_pmx Pmxanalyse 

16 | Wrapper class for the `PMX analyse <https://github.com/deGrootLab/pmx>`_ module. 

17 | Analyze the work values from the dgdl.xvg files of the A and B states to calculate the free energy difference between two states. 

18 

19 Args: 

20 input_a_xvg_zip_path (str): Path the zip file containing the dgdl.xvg files of the A state. File type: input. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/data/pmx/xvg_A.zip>`_. Accepted formats: zip (edam:format_3987). 

21 input_b_xvg_zip_path (str): Path the zip file containing the dgdl.xvg files of the B state. File type: input. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/data/pmx/xvg_B.zip>`_. Accepted formats: zip (edam:format_3987). 

22 output_result_path (str): Path to the TXT results file. File type: output. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/reference/pmx/ref_result.txt>`_. Accepted formats: txt (edam:format_2330). 

23 output_work_plot_path (str): Path to the PNG plot results file. File type: output. `Sample file <https://github.com/bioexcel/biobb_pmx/raw/master/biobb_pmx/test/reference/pmx/ref_plot.png>`_. Accepted formats: png (edam:format_3603). 

24 properties (dic): 

25 * **method** (*str*) - ("CGI BAR JARZ") Choose one or more estimators to use. Values: CGI (Crooks Gaussian Intersection), BAR (Bennet Acceptance Ratio), JARZ (Jarzynski's estimator). 

26 * **temperature** (*float*) - (298.15) [0~1000|0.05] Temperature in Kelvin. 

27 * **nboots** (*int*) - (0) [0~1000|1] Number of bootstrap samples to use for the bootstrap estimate of the standard errors. 

28 * **nblocks** (*int*) - (1) [0~1000|1] Number of blocks to divide the data into for an estimate of the standard error. 

29 * **integ_only** (*bool*) - (False) Whether to do integration only. 

30 * **reverseB** (*bool*) - (False) Whether to reverse the work values for the backward (B->A) transformation. 

31 * **skip** (*int*) - (1) [0~1000|1] Skip files. 

32 * **slice** (*str*) - (None) Subset of trajectories to analyze. Provide list slice, e.g. "10 50" will result in selecting dhdl_files[10:50]. 

33 * **rand** (*int*) - (None) [0~1000|1] Take a random subset of trajectories. Default is None (do not take random subset). 

34 * **index** (*str*) - (None) Zero-based index of files to analyze (e.g. "0 10 20 50 60"). It keeps the dhdl.xvg files according to their position in the list, sorted according to the filenames. 

35 * **prec** (*int*) - (2) [0~100|1] The decimal precision of the screen/file output. 

36 * **units** (*str*) - ("kJ") The units of the output. Values: kJ (Kilojoules), kcal (Kilocalories), kT (the product of the Boltzmann constant k and the temperature). 

37 * **no_ks** (*bool*) - (False) Whether to do a Kolmogorov-Smirnov test to check whether the Gaussian assumption for CGI holds. 

38 * **nbins** (*int*) - (20) [0~1000|1] Number of histograms bins for the plot. 

39 * **dpi** (*int*) - (300) [72~2048|1] Resolution of the plot. 

40 * **binary_path** (*str*) - ("pmx") Path to the PMX command line interface. 

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

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

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

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

45 * **container_image** (*str*) - ("gromacs/gromacs:latest") Container Image identifier. 

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

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

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

49 

50 Examples: 

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

52 

53 from biobb_pmx.pmxbiobb.pmxanalyse import pmxanalyse 

54 prop = { 

55 'method': 'CGI BAR JARZ', 

56 'temperature': 298.15, 

57 'dpi': 600 

58 } 

59 pmxanalyse(input_a_xvg_zip_path='/path/to/myAStateFiles.zip', 

60 input_b_xvg_zip_path='/path/to/myBStateFiles.zip', 

61 output_result_path='/path/to/newResults.txt', 

62 output_work_plot_path='/path/to/newResults.png', 

63 properties=prop) 

64 

65 Info: 

66 * wrapped_software: 

67 * name: PMX analyse 

68 * version: >=1.0.1 

69 * license: GNU 

70 * ontology: 

71 * name: EDAM 

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

73 

74 """ 

75 

76 def __init__( 

77 self, 

78 input_a_xvg_zip_path: str, 

79 input_b_xvg_zip_path: str, 

80 output_result_path: str, 

81 output_work_plot_path: str, 

82 properties: Optional[dict] = None, 

83 **kwargs, 

84 ) -> None: 

85 super().__init__(**locals()) 

86 

87 # Should not be copied inside container 

88 self.input_a_xvg_zip_path = self.io_dict["in"].pop("input_a_xvg_zip_path") 

89 self.input_b_xvg_zip_path = self.io_dict["in"].pop("input_b_xvg_zip_path") 

90 

91 def prepare(self) -> None: 

92 """Check that the pmx executable is installed.""" 

93 check_binary(self.binary_path, self.container_path) 

94 

95 def run(self) -> None: 

96 """Execute the :class:`Pmxanalyse <pmx.pmxanalyse.Pmxanalyse>` pmx.pmxanalyse.Pmxanalyse object.""" 

97 

98 # Two folders of the sandbox with the xvg files 

99 list_a_dir = self.create_tmp_dir() 

100 list_b_dir = self.create_tmp_dir() 

101 list_a = list( 

102 filter( 

103 lambda f: Path(f).exists() and Path(f).stat().st_size > 10, 

104 fu.unzip_list(self.input_a_xvg_zip_path, list_a_dir, self.out_log), 

105 ) 

106 ) 

107 list_b = list( 

108 filter( 

109 lambda f: Path(f).exists() and Path(f).stat().st_size > 10, 

110 fu.unzip_list(self.input_b_xvg_zip_path, list_b_dir, self.out_log), 

111 ) 

112 ) 

113 

114 # Paths of the xvg files for the command (including frame*/ subfolders) 

115 string_a = " ".join(self.staged_path(path) for path in list_a) 

116 string_b = " ".join(self.staged_path(path) for path in list_b) 

117 

118 self.cmd = [ 

119 self.binary_path, 

120 "analyse", 

121 "-fA", 

122 string_a, 

123 "-fB", 

124 string_b, 

125 "-o", 

126 self.stage_io_dict["out"]["output_result_path"], 

127 "-w", 

128 self.stage_io_dict["out"]["output_work_plot_path"], 

129 ] 

130 

131 if self.method: 

132 self.cmd.append("-m") 

133 self.cmd.append(self.method) 

134 if self.temperature: 

135 self.cmd.append("-t") 

136 self.cmd.append(str(self.temperature)) 

137 if self.nboots: 

138 self.cmd.append("-b") 

139 self.cmd.append(str(self.nboots)) 

140 if self.nblocks: 

141 self.cmd.append("-n") 

142 self.cmd.append(str(self.nblocks)) 

143 if self.integ_only: 

144 self.cmd.append("--integ_only") 

145 if self.reverseB: 

146 self.cmd.append("--reverseB") 

147 if self.skip: 

148 self.cmd.append("--skip") 

149 self.cmd.append(str(self.skip)) 

150 if self.slice: 

151 self.cmd.append("--slice") 

152 self.cmd.append(self.slice) 

153 if self.rand: 

154 self.cmd.append("--rand") 

155 self.cmd.append(str(self.rand)) 

156 if self.index: 

157 self.cmd.append("--index") 

158 self.cmd.append(self.index) 

159 if self.prec: 

160 self.cmd.append("--prec") 

161 self.cmd.append(str(self.prec)) 

162 if self.units: 

163 self.cmd.append("--units") 

164 self.cmd.append(self.units) 

165 if self.no_ks: 

166 self.cmd.append("--no_ks") 

167 if self.nbins: 

168 self.cmd.append("--nbins") 

169 self.cmd.append(str(self.nbins)) 

170 if self.dpi: 

171 self.cmd.append("--dpi") 

172 self.cmd.append(str(self.dpi)) 

173 

174 # Run Biobb block 

175 self.run_biobb() 

176 

177 

178pmxanalyse = launcher(Pmxanalyse) 

179 

180if __name__ == "__main__": 

181 Pmxanalyse.main()