Coverage for biobb_flexdyn/flexdyn/concoord_dist.py: 76%

84 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 concoord_dist class and the command line interface.""" 

4import argparse 

5from typing import Optional 

6import os 

7import shutil 

8from pathlib import Path 

9from biobb_common.tools import file_utils as fu 

10from biobb_common.generic.biobb_object import BiobbObject 

11from biobb_common.configuration import settings 

12from biobb_common.tools.file_utils import launchlogger 

13 

14 

15class ConcoordDist(BiobbObject): 

16 """ 

17 | biobb_flexdyn ConcoordDist 

18 | Wrapper of the Dist tool from the Concoord package. 

19 | Structure interpretation and bond definitions from a PDB/GRO file. 

20 

21 Args: 

22 input_structure_path (str): Input structure 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), gro (edam:format_2033). 

23 output_pdb_path (str): Output pdb file. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/reference/flexdyn/dist.pdb>`_. Accepted formats: pdb (edam:format_1476). 

24 output_gro_path (str): Output gro file. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/reference/flexdyn/dist.gro>`_. Accepted formats: gro (edam:format_2033). 

25 output_dat_path (str): Output dat with structure interpretation and bond definitions. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/reference/flexdyn/dist.dat>`_. Accepted formats: dat (edam:format_1637), txt (edam:format_2330). 

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

27 * **binary_path** (*str*) - ("dist") Concoord dist binary path to be used. 

28 * **vdw** (*int*) - (1) Select a set of Van der Waals parameters. Values: 1 (OPLS-UA -united atoms- parameters), 2 (OPLS-AA -all atoms- parameters), 3 (PROLSQ repel parameters), 4 (Yamber2 parameters), 5 (Li et al. parameters), 6 (OPLS-X parameters -recommended for NMR structure determination-). 

29 * **bond_angle** (*int*) - (1) Select a set of bond/angle parameters. Values: 1 (Concoord default parameters), 2 (Engh-Huber parameters). 

30 * **retain_hydrogens** (*bool*) - (False) Retain hydrogen atoms 

31 * **nb_interactions** (*bool*) - (False) Try to find alternatives for non-bonded interactions (by default the native contacts will be preserved) 

32 * **cutoff** (*float*) - (4.0) cut-off radius (Angstroms) for non-bonded interacting pairs (the cut-off distances are additional to the sum of VDW radii) 

33 * **min_distances** (*int*) - (50) Minimum number of distances to be defined for each atom 

34 * **damp** (*float*) - (1.0) Multiply each distance margin by this value 

35 * **fixed_atoms** (*bool*) - (False) Interpret zero occupancy as atoms to keep fixed 

36 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

37 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

38 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

39 

40 Examples: 

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

42 

43 from biobb_flexdyn.flexdyn.concoord_dist import concoord_dist 

44 prop = { 

45 'vdw' : 4, 

46 'bond_angle' : 1 

47 } 

48 concoord_dist( input_structure_path='/path/to/structure.pdb', 

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

50 output_gro_path='/path/to/output.gro', 

51 output_dat_path='/path/to/output.dat', 

52 properties=prop) 

53 

54 Info: 

55 * wrapped_software: 

56 * name: Concoord 

57 * version: >=2.1.2 

58 * license: other 

59 * ontology: 

60 * name: EDAM 

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

62 

63 """ 

64 

65 def __init__(self, input_structure_path: str, output_pdb_path: str, 

66 output_gro_path: str, output_dat_path: str, properties: Optional[dict] = None, **kwargs) -> None: 

67 

68 properties = properties or {} 

69 

70 # Call parent class constructor 

71 super().__init__(properties) 

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

73 

74 # Input/Output files 

75 self.io_dict = { 

76 'in': {'input_structure_path': input_structure_path}, 

77 'out': {'output_pdb_path': output_pdb_path, 

78 'output_gro_path': output_gro_path, 

79 'output_dat_path': output_dat_path} 

80 } 

81 

82 # Properties specific for BB 

83 self.properties = properties 

84 self.binary_path = properties.get('binary_path', 'dist') 

85 self.retain_hydrogens = properties.get('retain_hydrogens', False) 

86 self.nb_interactions = properties.get('nb_interactions', False) 

87 self.cutoff = properties.get('cutoff', 4.0) 

88 self.min_distances = properties.get('min_distances', 50) 

89 self.damp = properties.get('damp', 1.0) 

90 self.fixed_atoms = properties.get('fixed_atoms', False) 

91 

92 self.vdw = properties.get('vdw', 1) 

93 self.bond_angle = properties.get('bond_angle', 1) 

94 

95 # Check the properties 

96 self.check_properties(properties) 

97 self.check_arguments() 

98 

99 @launchlogger 

100 def launch(self): 

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

102 

103 # Set input params 

104 self.io_dict['in']['stdin_file_path'] = fu.create_stdin_file(f'{self.vdw}\n{self.bond_angle}\n') 

105 

106 # Setup Biobb 

107 if self.check_restart(): 

108 return 0 

109 self.stage_files() 

110 

111 # Copy auxiliary file (HBONDS) to the working dir 

112 concoord_lib = os.getenv("CONCOORDLIB") 

113 

114 hbonds_file = str(concoord_lib) + "/HBONDS.DAT" 

115 shutil.copy2(hbonds_file, self.stage_io_dict.get("unique_dir", "")) 

116 

117 # Command line 

118 # (concoord) OROZCO67:biobb_flexdyn hospital$ dist -p biobb_flexdyn/test/data/flexdyn/structure.pdb 

119 # -op dist.pdb -og dist.gro -od dist.dat 

120 # Select a set of Van der Waals parameters: 

121 # 1: OPLS-UA (united atoms) parameters 

122 # 2: OPLS-AA (all atoms) parameters 

123 # 3: PROLSQ repel parameters 

124 # 4: Yamber2 parameters 

125 # 5: Li et al. parameters 

126 # 6: OPLS-X parameters (recommended for NMR structure determination) 

127 # 2 

128 # Selected parameter set 2 

129 # copying /opt/anaconda3/envs/concoord/share/concoord/lib/ATOMS_oplsaa.DAT to ATOMS.DAT in current working directory 

130 # copying /opt/anaconda3/envs/concoord/share/concoord/lib/MARGINS_oplsaa.DAT to MARGINS.DAT in current working directory 

131 # Select a set of bond/angle parameters: 

132 # 1: Concoord default parameters 

133 # 2: Engh-Huber parameters 

134 # 1 

135 # Selected parameter set 1 

136 # copying /opt/anaconda3/envs/concoord/share/concoord/lib/BONDS.DAT.noeh to BONDS.DAT in current working directory 

137 

138 self.cmd = ["cd ", self.stage_io_dict.get('unique_dir', ''), ";", self.binary_path, 

139 # "-op", self.stage_io_dict["out"]["output_pdb_path"], 

140 # "-og", self.stage_io_dict["out"]["output_gro_path"], 

141 # "-od", self.stage_io_dict["out"]["output_dat_path"] 

142 "-op", str(Path(self.stage_io_dict["out"]["output_pdb_path"]).relative_to(Path(self.stage_io_dict.get('unique_dir', '')))), 

143 "-og", str(Path(self.stage_io_dict["out"]["output_gro_path"]).relative_to(Path(self.stage_io_dict.get('unique_dir', '')))), 

144 "-od", str(Path(self.stage_io_dict["out"]["output_dat_path"]).relative_to(Path(self.stage_io_dict.get('unique_dir', '')))) 

145 ] 

146 # If input structure in pdb format: 

147 file_extension = Path(self.stage_io_dict["in"]["input_structure_path"]).suffix 

148 if file_extension == ".pdb": 

149 self.cmd.append('-p') 

150 # self.cmd.append(self.stage_io_dict["in"]["input_structure_path"]) 

151 self.cmd.append(str(Path(self.stage_io_dict["in"]["input_structure_path"]).relative_to(Path(self.stage_io_dict.get('unique_dir', ''))))) 

152 

153 elif file_extension == ".gro": 

154 self.cmd.append('-g') 

155 # self.cmd.append(self.stage_io_dict["in"]["input_structure_path"]) 

156 self.cmd.append(str(Path(self.stage_io_dict["in"]["input_structure_path"]).relative_to(Path(self.stage_io_dict.get('unique_dir', ''))))) 

157 

158 else: 

159 fu.log("ERROR: input_structure_path ({}) must be a PDB or a GRO formatted file ({})".format(self.io_dict["in"]["input_structure_path"], file_extension), self.out_log, self.global_log) 

160 

161 if self.retain_hydrogens: 

162 self.cmd.append('-r') 

163 

164 if self.nb_interactions: 

165 self.cmd.append('-nb') 

166 

167 if self.fixed_atoms: 

168 self.cmd.append('-q') 

169 

170 if self.cutoff: 

171 self.cmd.append('-c') 

172 self.cmd.append(str(self.cutoff)) 

173 

174 if self.min_distances: 

175 self.cmd.append('-m') 

176 self.cmd.append(str(self.min_distances)) 

177 

178 if self.damp: 

179 self.cmd.append('-damp') 

180 self.cmd.append(str(self.damp)) 

181 

182 # Add stdin input file 

183 self.cmd.append('<') 

184 # self.cmd.append(self.stage_io_dict["in"]["stdin_file_path"]) 

185 self.cmd.append(str(Path(self.stage_io_dict["in"]["stdin_file_path"]).relative_to(Path(self.stage_io_dict.get('unique_dir', ''))))) 

186 

187 # Run Biobb block 

188 self.run_biobb() 

189 

190 # Copy files to host 

191 self.copy_to_host() 

192 

193 # remove temporary folder(s) 

194 self.tmp_files.extend([ 

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

196 self.io_dict['in'].get("stdin_file_path", "") 

197 ]) 

198 self.remove_tmp_files() 

199 

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

201 

202 return self.return_code 

203 

204 

205def concoord_dist(input_structure_path: str, 

206 output_pdb_path: str, output_gro_path: str, output_dat_path: str, 

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

208 """Create :class:`ConcoordDist <flexdyn.concoord_dist.ConcoordDist>`flexdyn.concoord_dist.ConcoordDist class and 

209 execute :meth:`launch() <flexdyn.concoord_dist.ConcoordDist.launch>` method""" 

210 

211 return ConcoordDist(input_structure_path=input_structure_path, 

212 output_pdb_path=output_pdb_path, 

213 output_gro_path=output_gro_path, 

214 output_dat_path=output_dat_path, 

215 properties=properties).launch() 

216 

217 

218def main(): 

219 parser = argparse.ArgumentParser(description='Structure interpretation and bond definitions from a PDB/GRO file.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

221 

222 # Specific args 

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

224 required_args.add_argument('--input_structure_path', required=True, help='Input structure file. Accepted formats: pdb, gro.') 

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

226 required_args.add_argument('--output_gro_path', required=True, help='Output gro file. Accepted formats: gro.') 

227 required_args.add_argument('--output_dat_path', required=True, help='Output dat file with structure interpretation and bond definitions. Accepted formats: dat, txt.') 

228 

229 args = parser.parse_args() 

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

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

232 

233 # Specific call 

234 concoord_dist(input_structure_path=args.input_structure_path, 

235 output_pdb_path=args.output_pdb_path, 

236 output_gro_path=args.output_gro_path, 

237 output_dat_path=args.output_dat_path, 

238 properties=properties) 

239 

240 

241if __name__ == '__main__': 

242 main()