Coverage for biobb_flexdyn/flexdyn/prody_anm.py: 80%

54 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-04 11:11 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6import prody # type: ignore 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.configuration import settings 

9from biobb_common.tools.file_utils import launchlogger 

10 

11 

12class ProdyANM(BiobbObject): 

13 """ 

14 | biobb_flexdyn ProdyANM 

15 | Wrapper of the ANM tool from the Prody package. 

16 | Generate an ensemble of structures using the Prody Anisotropic Network Model (ANM), for coarse-grained NMA. 

17 

18 Args: 

19 input_pdb_path (str): Input PDB file. File type: input. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/data/flexdyn/structure.pdb>`_. Accepted formats: pdb (edam:format_1476). 

20 output_pdb_path (str): Output multi-model PDB file with the generated ensemble. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/reference/prody/prody_output.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

22 * **num_structs** (*int*) - (500) Number of structures to be generated 

23 * **selection** (*str*) - (calpha) Atoms selection (Prody syntax: http://prody.csb.pitt.edu/manual/reference/atomic/select.html) 

24 * **cutoff** (*float*) - (15.0) Cutoff distance (Å) for pairwise interactions, minimum is 4.0 Å 

25 * **gamma** (*float*) - (1.0) Spring constant 

26 * **rmsd** (*float*) - (1.0) Average RMSD that the conformations will have with respect to the initial conformation 

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_flexdyn.flexdyn.prody_anm import prody_anm 

35 prop = { 

36 'num_structs' : 20, 

37 'rmsd' : 4.0 

38 } 

39 prody_anm( input_pdb_path='/path/to/structure.pdb', 

40 output_pdb_path='/path/to/output.pdb', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: Prody 

46 * version: >=2.2.0 

47 * license: MIT 

48 * ontology: 

49 * name: EDAM 

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

51 

52 """ 

53 

54 def __init__(self, input_pdb_path: str, output_pdb_path: str, 

55 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_pdb_path': output_pdb_path} 

67 } 

68 

69 # Properties specific for BB 

70 self.properties = properties 

71 

72 self.num_structs = properties.get('num_structs', 500) 

73 self.selection = properties.get('selection', 'calpha') 

74 self.cutoff = properties.get('cutoff', 15.0) 

75 self.gamma = properties.get('gamma', 1.0) 

76 self.rmsd = properties.get('rmsd', 1.0) 

77 

78 # Check the properties 

79 self.check_properties(properties) 

80 self.check_arguments() 

81 

82 @launchlogger 

83 def launch(self): 

84 """Launches the execution of the FlexDyn ConcoordDist module.""" 

85 

86 # Setup Biobb 

87 if self.check_restart(): 

88 return 0 

89 self.stage_files() 

90 

91 prot = prody.parsePDB(self.stage_io_dict["in"]["input_pdb_path"],) 

92 

93 # http://prody.csb.pitt.edu/manual/reference/atomic/select.html 

94 prot_sel = prot.select(self.selection) # type: ignore 

95 

96 enm = prody.ANM('BioBB_flexdyn Prody ANM ensemble generator') 

97 

98 enm.buildHessian(prot_sel, cutoff=self.cutoff, gamma=self.gamma) 

99 

100 enm.calcModes() 

101 

102 bb_enm, bb_atoms = prody.extendModel(enm, prot_sel, prot_sel) 

103 

104 ensemble = prody.sampleModes(bb_enm[:3], bb_atoms, n_confs=self.num_structs, rmsd=self.rmsd) 

105 

106 nmastruct = bb_atoms.copy() 

107 nmastruct.addCoordset(ensemble) 

108 

109 prody.writePDB(self.stage_io_dict["out"]["output_pdb_path"], nmastruct) 

110 

111 # Copy files to host 

112 self.copy_to_host() 

113 

114 # remove temporary folder(s) 

115 self.tmp_files.extend([ 

116 self.stage_io_dict.get("unique_dir", "") 

117 ]) 

118 self.remove_tmp_files() 

119 

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

121 

122 return self.return_code 

123 

124 

125def prody_anm(input_pdb_path: str, output_pdb_path: str, 

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

127 """Create :class:`ProdyANM <flexdyn.prody_anm.ProdyANM>`flexdyn.prody_anm.ProdyANM class and 

128 execute :meth:`launch() <flexdyn.prody_anm.ProdyANM.launch>` method""" 

129 

130 return ProdyANM(input_pdb_path=input_pdb_path, 

131 output_pdb_path=output_pdb_path, 

132 properties=properties).launch() 

133 

134 

135def main(): 

136 parser = argparse.ArgumentParser(description='Generate an ensemble of structures using the Prody Anisotropic Network Model (ANM), for coarse-grained NMA.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

138 

139 # Specific args 

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

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

142 required_args.add_argument('--output_pdb_path', required=True, help='Output pdb file. Accepted formats: pdb.') 

143 

144 args = parser.parse_args() 

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

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

147 

148 # Specific call 

149 prody_anm(input_pdb_path=args.input_pdb_path, 

150 output_pdb_path=args.output_pdb_path, 

151 properties=properties) 

152 

153 

154if __name__ == '__main__': 

155 main()