Coverage for biobb_flexserv/flexserv/nma_run.py: 71%

58 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 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 

11 

12 

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. 

18 

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. 

30 

31 Examples: 

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

33 

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) 

42 

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 

51 

52 """ 

53 

54 def __init__(self, input_pdb_path: str, output_log_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_pdb_path': input_pdb_path}, 

66 'out': {'output_log_path': output_log_path, 

67 'output_crd_path': output_crd_path} 

68 } 

69 

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) 

75 

76 # Check the properties 

77 self.check_properties(properties) 

78 self.check_arguments() 

79 

80 @launchlogger 

81 def launch(self): 

82 """Launches the execution of the FlexServ NMARun module.""" 

83 

84 # Setup Biobb 

85 if self.check_restart(): 

86 return 0 

87 self.stage_files() 

88 

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"] 

100 

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 ] 

127 

128 # Run Biobb block 

129 self.run_biobb() 

130 

131 # Copy files to host 

132 self.copy_to_host() 

133 

134 # remove temporary folder(s) 

135 # self.tmp_files.extend([ 

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

137 # ]) 

138 self.remove_tmp_files() 

139 

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

141 

142 return self.return_code 

143 

144 

145def nma_run(input_pdb_path: str, 

146 output_log_path: str, output_crd_path: str, 

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

148 """Create :class:`NMARun <flexserv.nma_run.NMARun>`flexserv.nma_run.NMARun class and 

149 execute :meth:`launch() <flexserv.nma_run.NMARun.launch>` method""" 

150 

151 return NMARun(input_pdb_path=input_pdb_path, 

152 output_log_path=output_log_path, 

153 output_crd_path=output_crd_path, 

154 properties=properties).launch() 

155 

156 nma_run.__doc__ = NMARun.__doc__ 

157 

158 

159def main(): 

160 parser = argparse.ArgumentParser(description='Generates protein conformational structures using the Normal Mode Analysis method.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

162 

163 # Specific args 

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

165 required_args.add_argument('--input_pdb_path', required=True, help='Input PDB file. Accepted formats: pdb.') 

166 required_args.add_argument('--output_log_path', required=True, help='Output log file. Accepted formats: log, out, txt.') 

167 required_args.add_argument('--output_crd_path', required=True, help='Output ensemble file. Accepted formats: crd, mdcrd, inpcrd.') 

168 

169 args = parser.parse_args() 

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

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

172 

173 # Specific call 

174 nma_run(input_pdb_path=args.input_pdb_path, 

175 output_log_path=args.output_log_path, 

176 output_crd_path=args.output_crd_path, 

177 properties=properties) 

178 

179 

180if __name__ == '__main__': 

181 main()