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

48 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 PCZcollectivity class and the command line interface.""" 

4from typing import Optional 

5from pathlib import Path, PurePath 

6import json 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools.file_utils import launchlogger 

9 

10 

11class PCZcollectivity(BiobbObject): 

12 """ 

13 | biobb_flexserv PCZcollectivity 

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

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

16 

17 Args: 

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

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

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

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

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

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

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

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

26 * **container_path** (*str*) - (None) Container path definition. 

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

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

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

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

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

32 

33 Examples: 

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

35 

36 from biobb_flexserv.pcasuite.pcz_collectivity import pcz_collectivity 

37 

38 prop = { 

39 'eigenvector': 1 

40 } 

41 

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

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

44 properties=prop) 

45 

46 Info: 

47 * wrapped_software: 

48 * name: FlexServ PCAsuite 

49 * version: >=1.0 

50 * license: Apache-2.0 

51 * ontology: 

52 * name: EDAM 

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

54 

55 """ 

56 

57 def __init__(self, input_pcz_path: str, 

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

59 

60 properties = properties or {} 

61 

62 # Call parent class constructor 

63 super().__init__(properties) 

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

65 

66 # Input/Output files 

67 self.io_dict = { 

68 'in': {'input_pcz_path': input_pcz_path}, 

69 'out': {'output_json_path': output_json_path} 

70 } 

71 

72 # Properties specific for BB 

73 self.properties = properties 

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

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

76 

77 # Check the properties 

78 self.check_properties(properties) 

79 self.check_arguments() 

80 

81 @launchlogger 

82 def launch(self): 

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

84 

85 # Setup Biobb 

86 if self.check_restart(): 

87 return 0 

88 self.stage_files() 

89 

90 if self.container_path: 

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

92 else: 

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

94 

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

96 

97 # Temporary output 

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

99 temp_out = "output.dat" 

100 temp_out_path = unique_dir.joinpath(temp_out) 

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

102 

103 # Command line 

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

105 # self.cmd = [self.binary_path, 

106 # "-i", input_pcz, 

107 # "-o", temp_out, 

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

109 # ] 

110 

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

112 self.binary_path, 

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

114 '-o', temp_out, 

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

116 ] 

117 

118 # Run Biobb block 

119 self.run_biobb() 

120 

121 # Parse output collectivity 

122 # 0.132891 

123 # 0.165089 

124 # 0.147202 

125 info_dict = {} 

126 info_dict['collectivity'] = [] 

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

128 for line in file: 

129 info = float(line.strip()) 

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

131 

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

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

134 

135 # Copy files to host 

136 self.copy_to_host() 

137 

138 # Remove temporary folder(s) 

139 self.remove_tmp_files() 

140 

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

142 

143 return self.return_code 

144 

145 

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

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

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

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

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

151 

152 

153pcz_collectivity.__doc__ = PCZcollectivity.__doc__ 

154main = 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.") 

155 

156if __name__ == '__main__': 

157 main()