Coverage for biobb_flexserv / pcasuite / pcz_animate.py: 95%
40 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-05 13:10 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-05 13:10 +0000
1#!/usr/bin/env python3
3"""Module containing the PCZanimate 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
8from biobb_common.generic.biobb_object import BiobbObject
9from biobb_common.tools.file_utils import launchlogger
12class PCZanimate(BiobbObject):
13 """
14 | biobb_flexserv PCZanimate
15 | Extract PCA animations from a compressed PCZ file.
16 | Wrapper of the pczdump tool from the PCAsuite FlexServ module.
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_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).
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*) - (1) Eigenvector to be used for the animation
24 * **pdb** (*bool*) - (False) Use PDB format for output trajectory
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.
29 Examples:
30 This is a use example of how to use the building block from Python::
32 from biobb_flexserv.pcasuite.pcz_animate import pcz_animate
33 prop = {
34 'eigenvector': 1,
35 'pdb': True
36 }
37 pcz_animate( input_pcz_path='/path/to/pcazip_input.pcz',
38 output_crd_path='/path/to/animated_traj.pdb',
39 properties=prop)
41 Info:
42 * wrapped_software:
43 * name: FlexServ PCAsuite
44 * version: >=1.0
45 * license: Apache-2.0
46 * ontology:
47 * name: EDAM
48 * schema: http://edamontology.org/EDAM.owl
50 """
52 def __init__(self, input_pcz_path: str,
53 output_crd_path: str, properties: Optional[dict] = None, **kwargs) -> None:
55 properties = properties or {}
57 # Call parent class constructor
58 super().__init__(properties)
59 self.locals_var_dict = locals().copy()
61 # Input/Output files
62 self.io_dict = {
63 'in': {'input_pcz_path': input_pcz_path},
64 'out': {'output_crd_path': output_crd_path}
65 }
67 # Properties specific for BB
68 self.properties = properties
69 self.binary_path = properties.get('binary_path', 'pczdump')
70 self.eigenvector = properties.get('eigenvector', 1)
71 self.pdb = properties.get('pdb', False)
73 # Check the properties
74 self.check_properties(properties)
75 self.check_arguments()
77 @launchlogger
78 def launch(self):
79 """Launches the execution of the FlexServ pcz_animate module."""
81 # Setup Biobb
82 if self.check_restart():
83 return 0
84 # self.stage_files()
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_crd = str(Path(self.stage_io_dict["out"]["output_crd_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_crd = self.stage_io_dict["out"]["output_crd_path"]
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).
102 # Creating temporary folder
103 tmp_folder = fu.create_unique_dir()
104 fu.log('Creating %s temporary folder' % tmp_folder, self.out_log)
106 shutil.copy2(self.io_dict["in"]["input_pcz_path"], tmp_folder)
108 # Command line
109 # pczdump -i structure.ca.std.pcz --anim=1 --pdb -o anim_1.pdb
110 # self.cmd = [self.binary_path,
111 # "-i", input_pcz,
112 # "-o", output_crd,
113 # "--anim={}".format(self.eigenvector)
114 # ]
116 self.cmd = ['cd', tmp_folder, ';',
117 self.binary_path,
118 '-i', PurePath(self.io_dict["in"]["input_pcz_path"]).name,
119 '-o', PurePath(self.io_dict["out"]["output_crd_path"]).name,
120 "--anim={}".format(self.eigenvector)
121 ]
123 if self.pdb:
124 self.cmd.append('--pdb')
126 # Run Biobb block
127 self.run_biobb()
129 # Copy outputs from temporary folder to output path
130 shutil.copy2(PurePath(tmp_folder).joinpath(PurePath(self.io_dict["out"]["output_crd_path"]).name), PurePath(self.io_dict["out"]["output_crd_path"]))
132 # Copy files to host
133 # self.copy_to_host()
135 # Remove temporary folder(s)
136 self.tmp_files.append(tmp_folder)
137 self.remove_tmp_files()
139 self.check_arguments(output_files_created=True, raise_exception=False)
141 return self.return_code
144def pcz_animate(input_pcz_path: str, output_crd_path: str,
145 properties: Optional[dict] = None, **kwargs) -> int:
146 """Create :class:`PCZanimate <flexserv.pcasuite.pcz_animate>`flexserv.pcasuite.PCZanimate class and
147 execute :meth:`launch() <flexserv.pcasuite.pcz_animate.launch>` method"""
148 return PCZanimate(**dict(locals())).launch()
151pcz_animate.__doc__ = PCZanimate.__doc__
152main = PCZanimate.get_main(pcz_animate, "Extract PCA animations from a compressed PCZ file.")
154if __name__ == '__main__':
155 main()