Coverage for biobb_flexserv/pcasuite/pcz_stiffness.py: 95%

64 statements  

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

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5import json 

6import math 

7from pathlib import Path, PurePath 

8from biobb_common.generic.biobb_object import BiobbObject 

9from biobb_common.tools.file_utils import launchlogger 

10 

11 

12class PCZstiffness(BiobbObject): 

13 """ 

14 | biobb_flexserv PCZstiffness 

15 | Extract PCA stiffness from a compressed PCZ file. 

16 | Wrapper of the pczdump tool from the PCAsuite FlexServ module. 

17 

18 Args: 

19 input_pcz_path (str): Input compressed trajectory file. File type: input. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/data/pcasuite/pcazip.pcz>`_. Accepted formats: pcz (edam:format_3874). 

20 output_json_path (str): Output json file with PCA Stiffness. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/reference/pcasuite/pcz_stiffness.json>`_. Accepted formats: json (edam:format_3464). 

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

22 * **binary_path** (*str*) - ("pczdump") pczdump binary path to be used. 

23 * **eigenvector** (*int*) - (0) PCA mode (eigenvector) from which to extract stiffness. 

24 * **temperature** (*int*) - (300) Temperature with which compute the apparent stiffness. 

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) Container path definition. 

29 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

30 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

31 * **container_working_dir** (*str*) - (None) Container working directory definition. 

32 * **container_user_id** (*str*) - (None) Container user_id definition. 

33 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

34 

35 Examples: 

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

37 

38 from biobb_flexserv.pcasuite.pcz_stiffness import pcz_stiffness 

39 

40 prop = { 

41 'eigenvector': 1 

42 } 

43 

44 pcz_stiffness( input_pcz_path='/path/to/pcazip_input.pcz', 

45 output_json_path='/path/to/pcz_stiffness.json', 

46 properties=prop) 

47 

48 Info: 

49 * wrapped_software: 

50 * name: FlexServ PCAsuite 

51 * version: >=1.0 

52 * license: Apache-2.0 

53 * ontology: 

54 * name: EDAM 

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

56 

57 """ 

58 

59 def __init__(self, input_pcz_path: str, 

60 output_json_path: str, properties: Optional[dict] = None, **kwargs) -> None: 

61 

62 properties = properties or {} 

63 

64 # Call parent class constructor 

65 super().__init__(properties) 

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

67 

68 # Input/Output files 

69 self.io_dict = { 

70 'in': {'input_pcz_path': input_pcz_path}, 

71 'out': {'output_json_path': output_json_path} 

72 } 

73 

74 # Properties specific for BB 

75 self.properties = properties 

76 self.binary_path = properties.get('binary_path', 'pczdump') 

77 self.eigenvector = properties.get('eigenvector', 0) 

78 self.temperature = properties.get('temperature', 300) 

79 

80 # Check the properties 

81 self.check_properties(properties) 

82 self.check_arguments() 

83 

84 @launchlogger 

85 def launch(self): 

86 """Launches the execution of the FlexServ pcz_stiffness module.""" 

87 

88 # Setup Biobb 

89 if self.check_restart(): 

90 return 0 

91 self.stage_files() 

92 

93 if self.container_path: 

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

95 else: 

96 working_dir = self.stage_io_dict.get("unique_dir", "") 

97 

98 unique_dir = Path(self.stage_io_dict.get("unique_dir", "")) 

99 

100 # Temporary output 

101 # temp_out = str(Path(self.stage_io_dict.get("unique_dir", "")).joinpath("output.dat")) 

102 temp_out = "output.dat" 

103 temp_out_path = unique_dir.joinpath(temp_out) 

104 staged_output_json_path = unique_dir.joinpath(Path(self.stage_io_dict["out"]["output_json_path"]).name) 

105 

106 # Command line 

107 # pczdump -i structure.ca.std.pcz --stiffness -o pcz.stiffness 

108 # self.cmd = [self.binary_path, 

109 # "-i", input_pcz, 

110 # "-o", temp_out, 

111 # "--stiffness={}".format(self.eigenvector), 

112 # "--temperature={}".format(self.temperature) 

113 # ] 

114 

115 self.cmd = ['cd', working_dir, ';', 

116 self.binary_path, 

117 "-i", PurePath(self.stage_io_dict["in"]["input_pcz_path"]).name, 

118 "-o", temp_out, 

119 "--stiff={}".format(self.eigenvector), 

120 "--temperature={}".format(self.temperature) 

121 ] 

122 

123 # Run Biobb block 

124 self.run_biobb() 

125 

126 # Parse output stiffness 

127 info_dict = {} 

128 info_dict['stiffness'] = [] 

129 info_dict['stiffness_log'] = [] 

130 row = 0 

131 with open(temp_out_path, 'r') as file: 

132 for line in file: 

133 info = line.strip().split(',') 

134 line_array = [] 

135 line_array_log = [] 

136 for nums in info: 

137 if nums: 

138 line_array.append(float(nums)) 

139 if float(nums) != 0: 

140 line_array_log.append(math.log10(float(nums))) 

141 else: 

142 line_array_log.append(float(nums)) 

143 

144 info_dict['stiffness'].append(line_array) 

145 info_dict['stiffness'][row][row] = float('inf') 

146 info_dict['stiffness_log'].append(line_array_log) 

147 info_dict['stiffness_log'][row][row] = float('inf') 

148 row += 1 

149 

150 with open(staged_output_json_path, 'w') as out_file: 

151 out_file.write(json.dumps(info_dict, indent=4)) 

152 

153 # Copy files to host 

154 self.copy_to_host() 

155 

156 # Remove temporary folder(s) 

157 self.remove_tmp_files() 

158 

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

160 

161 return self.return_code 

162 

163 

164def pcz_stiffness(input_pcz_path: str, output_json_path: str, 

165 properties: Optional[dict] = None, **kwargs) -> int: 

166 """Create :class:`PCZstiffness <flexserv.pcasuite.pcz_stiffness>`flexserv.pcasuite.PCZstiffness class and 

167 execute :meth:`launch() <flexserv.pcasuite.pcz_stiffness.launch>` method""" 

168 return PCZstiffness(**dict(locals())).launch() 

169 

170 

171pcz_stiffness.__doc__ = PCZstiffness.__doc__ 

172main = PCZstiffness.get_main(pcz_stiffness, "Extract PCA Stiffness from a compressed PCZ file.") 

173 

174if __name__ == '__main__': 

175 main()