Coverage for biobb_flexserv/flexserv/nma_run.py: 72%
58 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-18 09:16 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-18 09:16 +0000
1#!/usr/bin/env python3
3"""Module containing the nma_run class and the command line interface."""
4import argparse
5from typing import Optional
6import os
7from pathlib import Path
8from biobb_common.generic.biobb_object import BiobbObject
9from biobb_common.configuration import settings
10from biobb_common.tools.file_utils import launchlogger
13class NMARun(BiobbObject):
14 """
15 | biobb_flexserv NMARun
16 | Wrapper of the Normal Mode Analysis tool from the FlexServ module.
17 | Generates protein conformational structures using the Normal Mode Analysis (NMA) method.
19 Args:
20 input_pdb_path (str): Input PDB file. File type: input. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/data/flexserv/structure.ca.pdb>`_. Accepted formats: pdb (edam:format_1476).
21 output_log_path (str): Output log file. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/reference/flexserv/nma_run_out.log>`_. Accepted formats: log (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330).
22 output_crd_path (str): Output ensemble. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexserv/raw/master/biobb_flexserv/test/reference/flexserv/nma_run_out.crd>`_. Accepted formats: crd (edam:format_3878), mdcrd (edam:format_3878), inpcrd (edam:format_3878).
23 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
24 * **binary_path** (*str*) - ("diaghess") NMA binary path to be used.
25 * **frames** (*int*) - (1000) Number of frames in the final ensemble
26 * **nvecs** (*int*) - (50) Number of vectors to take into account for the ensemble generation
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.
31 Examples:
32 This is a use example of how to use the building block from Python::
34 from biobb_flexserv.flexserv.bd_run import bd_run
35 prop = {
36 'binary_path': 'diaghess'
37 }
38 flexserv_run(input_pdb_path='/path/to/nma_input.pdb',
39 output_log_path='/path/to/nma_log.log',
40 output_crd_path='/path/to/nma_ensemble.crd',
41 properties=prop)
43 Info:
44 * wrapped_software:
45 * name: FlexServ Normal Mode Analysis
46 * version: >=1.0
47 * license: Apache-2.0
48 * ontology:
49 * name: EDAM
50 * schema: http://edamontology.org/EDAM.owl
52 """
54 def __init__(self, input_pdb_path: str, output_log_path: str,
55 output_crd_path: str, properties: Optional[dict] = None, **kwargs) -> None:
57 properties = properties or {}
59 # Call parent class constructor
60 super().__init__(properties)
61 self.locals_var_dict = locals().copy()
63 # Input/Output files
64 self.io_dict = {
65 'in': {'input_pdb_path': input_pdb_path},
66 'out': {'output_log_path': output_log_path,
67 'output_crd_path': output_crd_path}
68 }
70 # Properties specific for BB
71 self.properties = properties
72 self.binary_path = properties.get('binary_path', 'diaghess')
73 self.frames = properties.get('frames', 1000)
74 self.nvecs = properties.get('nvecs', 50)
76 # Check the properties
77 self.check_properties(properties)
78 self.check_arguments()
80 @launchlogger
81 def launch(self):
82 """Launches the execution of the FlexServ NMARun module."""
84 # Setup Biobb
85 if self.check_restart():
86 return 0
87 self.stage_files()
89 # Internal file paths
90 try:
91 # Using rel paths to shorten the amount of characters due to fortran path length limitations
92 input_pdb = str(Path(self.stage_io_dict["in"]["input_pdb_path"]).relative_to(Path.cwd()))
93 output_crd = str(Path(self.stage_io_dict["out"]["output_crd_path"]).relative_to(Path.cwd()))
94 output_log = str(Path(self.stage_io_dict["out"]["output_log_path"]).relative_to(Path.cwd()))
95 except ValueError:
96 # Container or remote case
97 input_pdb = self.stage_io_dict["in"]["input_pdb_path"]
98 output_crd = self.stage_io_dict["out"]["output_crd_path"]
99 output_log = self.stage_io_dict["out"]["output_log_path"]
101 # Command line
102 # nmanu.pl structure.ca.pdb hessian.dat 1 0 40
103 # diaghess
104 # mc-eigen.pl eigenvec.dat > file.proj
105 # pca_anim_mc.pl -pdb structure.ca.pdb -evec eigenvec.dat -i file.proj -n 50 -pout traj.crd
106 conda_path = os.getenv("CONDA_PREFIX")
107 nmanu = str(conda_path) + "/bin/nmanu.pl"
108 nma_eigen = str(conda_path) + "/bin/mc-eigen-mdweb.pl"
109 pca_anim = str(conda_path) + "/bin/pca_anim_mc.pl"
110 self.cmd = ["perl ",
111 nmanu,
112 input_pdb,
113 "hessian.dat 1 0 40;",
114 self.binary_path,
115 "; perl",
116 nma_eigen,
117 "eigenvec.dat ", str(self.frames), " > file.proj",
118 "; perl",
119 pca_anim,
120 "-pdb",
121 input_pdb,
122 " -evec eigenvec.dat -i file.proj -n ",
123 str(self.nvecs),
124 " -pout", output_crd,
125 '>', output_log
126 ]
128 # Run Biobb block
129 self.run_biobb()
131 # Copy files to host
132 self.copy_to_host()
134 # Remove temporary folder(s)
135 self.remove_tmp_files()
136 self.check_arguments(output_files_created=True, raise_exception=False)
138 return self.return_code
141def nma_run(input_pdb_path: str,
142 output_log_path: str, output_crd_path: str,
143 properties: Optional[dict] = None, **kwargs) -> int:
144 """Create :class:`NMARun <flexserv.nma_run.NMARun>`flexserv.nma_run.NMARun class and
145 execute :meth:`launch() <flexserv.nma_run.NMARun.launch>` method"""
147 return NMARun(input_pdb_path=input_pdb_path,
148 output_log_path=output_log_path,
149 output_crd_path=output_crd_path,
150 properties=properties).launch()
153nma_run.__doc__ = NMARun.__doc__
156def main():
157 parser = argparse.ArgumentParser(description='Generates protein conformational structures using the Normal Mode Analysis method.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
158 parser.add_argument('--config', required=False, help='Configuration file')
160 # Specific args
161 required_args = parser.add_argument_group('required arguments')
162 required_args.add_argument('--input_pdb_path', required=True, help='Input PDB file. Accepted formats: pdb.')
163 required_args.add_argument('--output_log_path', required=True, help='Output log file. Accepted formats: log, out, txt.')
164 required_args.add_argument('--output_crd_path', required=True, help='Output ensemble file. Accepted formats: crd, mdcrd, inpcrd.')
166 args = parser.parse_args()
167 args.config = args.config or "{}"
168 properties = settings.ConfReader(config=args.config).get_prop_dic()
170 # Specific call
171 nma_run(input_pdb_path=args.input_pdb_path,
172 output_log_path=args.output_log_path,
173 output_crd_path=args.output_crd_path,
174 properties=properties)
177if __name__ == '__main__':
178 main()