Coverage for biobb_flexserv / pcasuite / pcz_collectivity.py: 96%

48 statements  

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

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5import shutil 

6from pathlib import PurePath 

7from biobb_common.tools import file_utils as fu 

8import json 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools.file_utils import launchlogger 

11 

12 

13class PCZcollectivity(BiobbObject): 

14 """ 

15 | biobb_flexserv PCZcollectivity 

16 | Extract PCA collectivity (numerical measure of how many atoms are affected by a given mode) from a compressed PCZ file. 

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

18 

19 Args: 

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

21 output_json_path (str): Output json file with PCA Collectivity indexes per mode. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/reference/pcasuite/pcz_collectivity.json>`_. Accepted formats: json (edam:format_3464). 

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

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

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

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 

29 Examples: 

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

31 

32 from biobb_flexserv.pcasuite.pcz_collectivity import pcz_collectivity 

33 

34 prop = { 

35 'eigenvector': 1 

36 } 

37 

38 pcz_collectivity( input_pcz_path='/path/to/pcazip_input.pcz', 

39 output_json_path='/path/to/pcz_collectivity.json', 

40 properties=prop) 

41 

42 Info: 

43 * wrapped_software: 

44 * name: FlexServ PCAsuite 

45 * version: >=1.0 

46 * license: Apache-2.0 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

53 def __init__(self, input_pcz_path: str, 

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

55 

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': {'input_pcz_path': input_pcz_path}, 

65 'out': {'output_json_path': output_json_path} 

66 } 

67 

68 # Properties specific for BB 

69 self.properties = properties 

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

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

72 

73 # Check the properties 

74 self.check_properties(properties) 

75 self.check_arguments() 

76 

77 @launchlogger 

78 def launch(self): 

79 """Launches the execution of the FlexServ pcz_collectivity module.""" 

80 

81 # Setup Biobb 

82 if self.check_restart(): 

83 return 0 

84 # self.stage_files() 

85 

86 # Internal file paths 

87 # try: 

88 # # Using rel paths to shorten the amount of characters due to fortran path length limitations 

89 # input_pcz = str(Path(self.stage_io_dict["in"]["input_pcz_path"]).relative_to(Path.cwd())) 

90 # output_json = str(Path(self.stage_io_dict["out"]["output_json_path"]).relative_to(Path.cwd())) 

91 # except ValueError: 

92 # # Container or remote case 

93 # input_pcz = self.stage_io_dict["in"]["input_pcz_path"] 

94 # output_json = self.stage_io_dict["out"]["output_json_path"] 

95 

96 # Manually creating a Sandbox to avoid issues with input parameters buffer overflow: 

97 # Long strings defining a file path makes Fortran or C compiled programs crash if the string 

98 # declared is shorter than the input parameter path (string) length. 

99 # Generating a temporary folder and working inside this folder (sandbox) fixes this problem. 

100 # The problem was found in Galaxy executions, launching Singularity containers (May 2023). 

101 

102 # Creating temporary folder 

103 tmp_folder = fu.create_unique_dir() 

104 fu.log('Creating %s temporary folder' % tmp_folder, self.out_log) 

105 

106 shutil.copy2(self.io_dict["in"]["input_pcz_path"], tmp_folder) 

107 

108 # Temporary output 

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

110 temp_out = "output.dat" 

111 temp_json = "output.json" 

112 

113 # Command line 

114 # pczdump -i structure.ca.std.pcz --collectivity -o pcz.collectivity 

115 # self.cmd = [self.binary_path, 

116 # "-i", input_pcz, 

117 # "-o", temp_out, 

118 # "--collectivity={}".format(self.eigenvector) 

119 # ] 

120 

121 self.cmd = ['cd', tmp_folder, ';', 

122 self.binary_path, 

123 '-i', PurePath(self.io_dict["in"]["input_pcz_path"]).name, 

124 '-o', temp_out, 

125 "--collectivity={}".format(self.eigenvector) 

126 ] 

127 

128 # Run Biobb block 

129 self.run_biobb() 

130 

131 # Parse output collectivity 

132 # 0.132891 

133 # 0.165089 

134 # 0.147202 

135 info_dict = {} 

136 info_dict['collectivity'] = [] 

137 with open(PurePath(tmp_folder).joinpath(temp_out), 'r') as file: 

138 for line in file: 

139 info = float(line.strip()) 

140 info_dict['collectivity'].append(info) 

141 

142 with open(PurePath(tmp_folder).joinpath(temp_json), 'w') as out_file: 

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

144 

145 # Copy outputs from temporary folder to output path 

146 shutil.copy2(PurePath(tmp_folder).joinpath(temp_json), PurePath(self.io_dict["out"]["output_json_path"])) 

147 

148 # Copy files to host 

149 # self.copy_to_host() 

150 

151 # Remove temporary folder(s) 

152 self.tmp_files.append(tmp_folder) 

153 self.remove_tmp_files() 

154 

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

156 

157 return self.return_code 

158 

159 

160def pcz_collectivity(input_pcz_path: str, output_json_path: str, 

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

162 """Create :class:`PCZcollectivity <flexserv.pcasuite.pcz_collectivity>`flexserv.pcasuite.PCZcollectivity class and 

163 execute :meth:`launch() <flexserv.pcasuite.pcz_collectivity.launch>` method""" 

164 return PCZcollectivity(**dict(locals())).launch() 

165 

166 

167pcz_collectivity.__doc__ = PCZcollectivity.__doc__ 

168main = PCZcollectivity.get_main(pcz_collectivity, "Extract PCA collectivity (numerical measure of how many atoms are affected by a given mode) from a compressed PCZ file.") 

169 

170if __name__ == '__main__': 

171 main()