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

46 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-05-28 11:28 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5import os 

6from pathlib import PurePath 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools.file_utils import launchlogger 

9 

10 

11class NMARun(BiobbObject): 

12 """ 

13 | biobb_flexserv NMARun 

14 | Wrapper of the Normal Mode Analysis tool from the FlexServ module. 

15 | Generates protein conformational structures using the Normal Mode Analysis (NMA) method. 

16 

17 Args: 

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

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

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

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

22 * **binary_path** (*str*) - ("diaghess") NMA binary path to be used. 

23 * **frames** (*int*) - (1000) Number of frames in the final ensemble 

24 * **nvecs** (*int*) - (50) Number of vectors to take into account for the ensemble generation 

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. 

28 * **container_path** (*str*) - (None) Container path definition. 

29 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

30 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

31 * **container_working_dir** (*str*) - (None) Container working directory definition. 

32 * **container_user_id** (*str*) - (None) Container user_id definition. 

33 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

34 

35 Examples: 

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

37 

38 from biobb_flexserv.flexserv.bd_run import bd_run 

39 prop = { 

40 'binary_path': 'diaghess' 

41 } 

42 flexserv_run(input_pdb_path='/path/to/nma_input.pdb', 

43 output_log_path='/path/to/nma_log.log', 

44 output_crd_path='/path/to/nma_ensemble.crd', 

45 properties=prop) 

46 

47 Info: 

48 * wrapped_software: 

49 * name: FlexServ Normal Mode Analysis 

50 * version: >=1.0 

51 * license: Apache-2.0 

52 * ontology: 

53 * name: EDAM 

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

55 

56 """ 

57 

58 def __init__(self, input_pdb_path: str, output_log_path: str, 

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

60 

61 properties = properties or {} 

62 

63 # Call parent class constructor 

64 super().__init__(properties) 

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

66 

67 # Input/Output files 

68 self.io_dict = { 

69 'in': {'input_pdb_path': input_pdb_path}, 

70 'out': {'output_log_path': output_log_path, 

71 'output_crd_path': output_crd_path} 

72 } 

73 

74 # Properties specific for BB 

75 self.properties = properties 

76 self.binary_path = properties.get('binary_path', 'diaghess') 

77 self.frames = properties.get('frames', 1000) 

78 self.nvecs = properties.get('nvecs', 50) 

79 

80 # Check the properties 

81 self.check_properties(properties) 

82 self.check_arguments() 

83 

84 @launchlogger 

85 def launch(self): 

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

87 

88 # Setup Biobb 

89 if self.check_restart(): 

90 return 0 

91 self.stage_files() 

92 

93 if self.container_path: 

94 working_dir = self.container_volume_path if self.container_volume_path else "/data" 

95 else: 

96 working_dir = self.stage_io_dict.get("unique_dir", "") 

97 

98 input_pdb = PurePath(self.stage_io_dict["in"]["input_pdb_path"]).name 

99 output_crd = PurePath(self.stage_io_dict["out"]["output_crd_path"]).name 

100 output_log = PurePath(self.stage_io_dict["out"]["output_log_path"]).name 

101 

102 # Command line 

103 # nmanu.pl structure.ca.pdb hessian.dat 1 0 40 

104 # diaghess 

105 # mc-eigen.pl eigenvec.dat > file.proj 

106 # pca_anim_mc.pl -pdb structure.ca.pdb -evec eigenvec.dat -i file.proj -n 50 -pout traj.crd 

107 if self.container_path: 

108 conda_path = "/usr/local" 

109 else: 

110 conda_path = os.getenv("CONDA_PREFIX") 

111 nmanu = str(conda_path) + "/bin/nmanu.pl" 

112 nma_eigen = str(conda_path) + "/bin/mc-eigen-mdweb.pl" 

113 pca_anim = str(conda_path) + "/bin/pca_anim_mc.pl" 

114 self.cmd = ["cd", working_dir, ";", 

115 "perl", 

116 nmanu, 

117 input_pdb, 

118 "hessian.dat", "1", "0", "40", ";", 

119 self.binary_path, 

120 ";", "perl", 

121 nma_eigen, "eigenvec.dat", str(self.frames), ">", "file.proj", 

122 ";", "perl", 

123 pca_anim, 

124 "-pdb", 

125 input_pdb, 

126 "-evec", "eigenvec.dat", "-i", "file.proj", "-n", str(self.nvecs), 

127 "-pout", output_crd, 

128 '>', output_log 

129 ] 

130 

131 # Run Biobb block 

132 self.run_biobb() 

133 

134 # Copy files to host 

135 self.copy_to_host() 

136 

137 # Remove temporary folder(s) 

138 self.remove_tmp_files() 

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

140 

141 return self.return_code 

142 

143 

144def nma_run(input_pdb_path: str, 

145 output_log_path: str, output_crd_path: str, 

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

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

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

149 return NMARun(**dict(locals())).launch() 

150 

151 

152nma_run.__doc__ = NMARun.__doc__ 

153main = NMARun.get_main(nma_run, "Generates protein conformational structures using the Normal Mode Analysis method.") 

154 

155if __name__ == '__main__': 

156 main()