Coverage for biobb_flexserv / pcasuite / pcz_lindemann.py: 94%
49 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 PCZlindemann class and the command line interface."""
4from typing import Optional
5import shutil
6import json
7from pathlib import PurePath
8from biobb_common.tools import file_utils as fu
9from biobb_common.generic.biobb_object import BiobbObject
10from biobb_common.tools.file_utils import launchlogger
13class PCZlindemann(BiobbObject):
14 """
15 | biobb_flexserv PCZlindemann
16 | Extract Lindemann coefficient (an estimate of the solid-liquid behaviour of a protein) from a compressed PCZ file.
17 | Wrapper of the pczdump tool from the PCAsuite FlexServ module.
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 Eigen Vectors. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/reference/pcasuite/pcz_lindemann.json>`_. Accepted formats: json (edam:format_3464).
22 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
23 * **binary_path** (*str*) - ("pczdump") pczdump binary path to be used.
24 * **mask** (*str*) - ("all atoms") Residue mask, in the format ":resnum1, resnum2, resnum3" (e.g. ":10,21,33"). See https://mmb.irbbarcelona.org/software/pcasuite/ for the complete format specification.
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_lindemann import pcz_lindemann
33 prop = {
34 'mask': ':10,12,15'
35 }
36 pcz_lindemann( input_pcz_path='/path/to/pcazip_input.pcz',
37 output_json_path='/path/to/lindemann_report.json',
38 properties=prop)
40 Info:
41 * wrapped_software:
42 * name: FlexServ PCAsuite
43 * version: >=1.0
44 * license: Apache-2.0
45 * ontology:
46 * name: EDAM
47 * schema: http://edamontology.org/EDAM.owl
49 """
51 def __init__(self, input_pcz_path: str,
52 output_json_path: str, properties: Optional[dict] = None, **kwargs) -> None:
54 properties = properties or {}
56 # Call parent class constructor
57 super().__init__(properties)
58 self.locals_var_dict = locals().copy()
60 # Input/Output files
61 self.io_dict = {
62 'in': {'input_pcz_path': input_pcz_path},
63 'out': {'output_json_path': output_json_path}
64 }
66 # Properties specific for BB
67 self.properties = properties
68 self.binary_path = properties.get('binary_path', 'pczdump')
69 self.mask = properties.get('mask', '')
71 # Check the properties
72 self.check_properties(properties)
73 self.check_arguments()
75 @launchlogger
76 def launch(self):
77 """Launches the execution of the FlexServ pcz_lindemann module."""
79 # Setup Biobb
80 if self.check_restart():
81 return 0
82 # self.stage_files()
84 # Internal file paths
85 # try:
86 # # Using rel paths to shorten the amount of characters due to fortran path length limitations
87 # input_pcz = str(Path(self.stage_io_dict["in"]["input_pcz_path"]).relative_to(Path.cwd()))
88 # output_json = str(Path(self.stage_io_dict["out"]["output_json_path"]).relative_to(Path.cwd()))
89 # except ValueError:
90 # # Container or remote case
91 # input_pcz = self.stage_io_dict["in"]["input_pcz_path"]
92 # output_json = self.stage_io_dict["out"]["output_json_path"]
94 # Manually creating a Sandbox to avoid issues with input parameters buffer overflow:
95 # Long strings defining a file path makes Fortran or C compiled programs crash if the string
96 # declared is shorter than the input parameter path (string) length.
97 # Generating a temporary folder and working inside this folder (sandbox) fixes this problem.
98 # The problem was found in Galaxy executions, launching Singularity containers (May 2023).
100 # Creating temporary folder
101 tmp_folder = fu.create_unique_dir()
102 fu.log('Creating %s temporary folder' % tmp_folder, self.out_log)
104 shutil.copy2(self.io_dict["in"]["input_pcz_path"], tmp_folder)
106 # Temporary output
107 # temp_out = str(Path(self.stage_io_dict.get("unique_dir", "")).joinpath("output.dat"))
108 temp_out = "output.dat"
109 temp_json = "output.json"
111 # Command line
112 # pczdump -i structure.ca.std.pcz --lindemann -M ":2-86" -o lindemann_report.txt
113 # self.cmd = [self.binary_path,
114 # "-i", input_pcz,
115 # "-o", temp_out,
116 # "--lindemann"
117 # ]
119 self.cmd = ['cd', tmp_folder, ';',
120 self.binary_path,
121 "-i", PurePath(self.io_dict["in"]["input_pcz_path"]).name,
122 "-o", temp_out,
123 "--lindemann"
124 ]
126 if self.mask:
127 self.cmd.append("-M {}".format(self.mask))
129 # Run Biobb block
130 self.run_biobb()
132 # Parse output Lindemann
133 # 0.132891
134 info_dict = {}
135 with open(PurePath(tmp_folder).joinpath(temp_out), 'r') as file:
136 for line in file:
137 info = float(line.strip())
138 info_dict['lindemann'] = info
140 with open(PurePath(tmp_folder).joinpath(temp_json), 'w') as out_file:
141 out_file.write(json.dumps(info_dict, indent=4))
143 # Copy outputs from temporary folder to output path
144 shutil.copy2(PurePath(tmp_folder).joinpath(temp_json), PurePath(self.io_dict["out"]["output_json_path"]))
146 # Copy files to host
147 # self.copy_to_host()
149 # Remove temporary folder(s)
150 self.tmp_files.append(tmp_folder)
151 self.remove_tmp_files()
153 self.check_arguments(output_files_created=True, raise_exception=False)
155 return self.return_code
158def pcz_lindemann(input_pcz_path: str, output_json_path: str,
159 properties: Optional[dict] = None, **kwargs) -> int:
160 """Create :class:`PCZlindemann <flexserv.pcasuite.pcz_lindemann>`flexserv.pcasuite.PCZlindemann class and
161 execute :meth:`launch() <flexserv.pcasuite.pcz_lindemann.launch>` method"""
162 return PCZlindemann(**dict(locals())).launch()
165pcz_lindemann.__doc__ = PCZlindemann.__doc__
166main = PCZlindemann.get_main(pcz_lindemann, "Extract Lindemann coefficients from a compressed PCZ file.")
168if __name__ == '__main__':
169 main()