Coverage for biobb_flexserv/pcasuite/pcz_animate.py: 76%

51 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-19 15:08 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6import shutil 

7from pathlib import PurePath 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.configuration import settings 

11from biobb_common.tools.file_utils import launchlogger 

12 

13 

14class PCZanimate(BiobbObject): 

15 """ 

16 | biobb_flexserv PCZanimate 

17 | Extract PCA animations from a compressed PCZ file. 

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

19 

20 Args: 

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

22 output_crd_path (str): Output PCA animated trajectory file. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/reference/pcasuite/pcazip_anim1.pdb>`_. Accepted formats: crd (edam:format_3878), mdcrd (edam:format_3878), inpcrd (edam:format_3878), pdb (edam:format_1476). 

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

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

25 * **eigenvector** (*int*) - (1) Eigenvector to be used for the animation 

26 * **pdb** (*bool*) - (False) Use PDB format for output trajectory 

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

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

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

30 

31 Examples: 

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

33 

34 from biobb_flexserv.pcasuite.pcz_animate import pcz_animate 

35 prop = { 

36 'eigenvector': 1, 

37 'pdb': True 

38 } 

39 pcz_animate( input_pcz_path='/path/to/pcazip_input.pcz', 

40 output_crd_path='/path/to/animated_traj.pdb', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: FlexServ PCAsuite 

46 * version: >=1.0 

47 * license: Apache-2.0 

48 * ontology: 

49 * name: EDAM 

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

51 

52 """ 

53 

54 def __init__(self, input_pcz_path: str, 

55 output_crd_path: str, properties: Optional[dict] = None, **kwargs) -> None: 

56 

57 properties = properties or {} 

58 

59 # Call parent class constructor 

60 super().__init__(properties) 

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

62 

63 # Input/Output files 

64 self.io_dict = { 

65 'in': {'input_pcz_path': input_pcz_path}, 

66 'out': {'output_crd_path': output_crd_path} 

67 } 

68 

69 # Properties specific for BB 

70 self.properties = properties 

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

72 self.eigenvector = properties.get('eigenvector', 1) 

73 self.pdb = properties.get('pdb', False) 

74 

75 # Check the properties 

76 self.check_properties(properties) 

77 self.check_arguments() 

78 

79 @launchlogger 

80 def launch(self): 

81 """Launches the execution of the FlexServ pcz_animate module.""" 

82 

83 # Setup Biobb 

84 if self.check_restart(): 

85 return 0 

86 # self.stage_files() 

87 

88 # # Internal file paths 

89 # try: 

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

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

92 # output_crd = str(Path(self.stage_io_dict["out"]["output_crd_path"]).relative_to(Path.cwd())) 

93 # except ValueError: 

94 # # Container or remote case 

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

96 # output_crd = self.stage_io_dict["out"]["output_crd_path"] 

97 

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

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

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

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

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

103 

104 # Creating temporary folder 

105 self.tmp_folder = fu.create_unique_dir() 

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

107 

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

109 

110 # Command line 

111 # pczdump -i structure.ca.std.pcz --anim=1 --pdb -o anim_1.pdb 

112 # self.cmd = [self.binary_path, 

113 # "-i", input_pcz, 

114 # "-o", output_crd, 

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

116 # ] 

117 

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

119 self.binary_path, 

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

121 '-o', PurePath(self.io_dict["out"]["output_crd_path"]).name, 

122 "--anim={}".format(self.eigenvector) 

123 ] 

124 

125 if self.pdb: 

126 self.cmd.append('--pdb') 

127 

128 # Run Biobb block 

129 self.run_biobb() 

130 

131 # Copy outputs from temporary folder to output path 

132 shutil.copy2(PurePath(self.tmp_folder).joinpath(PurePath(self.io_dict["out"]["output_crd_path"]).name), PurePath(self.io_dict["out"]["output_crd_path"])) 

133 

134 # Copy files to host 

135 # self.copy_to_host() 

136 

137 # remove temporary folder(s) 

138 self.tmp_files.extend([ 

139 # self.stage_io_dict.get("unique_dir", ""), 

140 self.tmp_folder 

141 ]) 

142 self.remove_tmp_files() 

143 

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

145 

146 return self.return_code 

147 

148 

149def pcz_animate(input_pcz_path: str, output_crd_path: str, 

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

151 """Create :class:`PCZanimate <flexserv.pcasuite.pcz_animate>`flexserv.pcasuite.PCZanimate class and 

152 execute :meth:`launch() <flexserv.pcasuite.pcz_animate.launch>` method""" 

153 

154 return PCZanimate(input_pcz_path=input_pcz_path, 

155 output_crd_path=output_crd_path, 

156 properties=properties).launch() 

157 

158 pcz_animate.__doc__ = PCZanimate.__doc__ 

159 

160 

161def main(): 

162 parser = argparse.ArgumentParser(description='Extract PCA animations from a compressed PCZ file.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

164 

165 # Specific args 

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

167 required_args.add_argument('--input_pcz_path', required=True, help='Input compressed trajectory file. Accepted formats: pcz.') 

168 required_args.add_argument('--output_crd_path', required=True, help='Output animated trajectory file. Accepted formats: crd, mdcrd, inpcrd, pdb.') 

169 

170 args = parser.parse_args() 

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

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

173 

174 # Specific call 

175 pcz_animate(input_pcz_path=args.input_pcz_path, 

176 output_crd_path=args.output_crd_path, 

177 properties=properties) 

178 

179 

180if __name__ == '__main__': 

181 main()