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

72 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-16 13:07 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5import os 

6import shutil 

7from pathlib import Path 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools.file_utils import launchlogger 

11 

12 

13class ConcoordDist(BiobbObject): 

14 """ 

15 | biobb_flexdyn ConcoordDist 

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

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

18 

19 Args: 

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

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

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

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

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

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

26 * **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-). 

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

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

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

30 * **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) 

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

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

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

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

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

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

37 

38 Examples: 

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

40 

41 from biobb_flexdyn.flexdyn.concoord_dist import concoord_dist 

42 prop = { 

43 'vdw' : 4, 

44 'bond_angle' : 1 

45 } 

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

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

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

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

50 properties=prop) 

51 

52 Info: 

53 * wrapped_software: 

54 * name: Concoord 

55 * version: >=2.1.2 

56 * license: other 

57 * ontology: 

58 * name: EDAM 

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

60 

61 """ 

62 

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

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

65 

66 properties = properties or {} 

67 

68 # Call parent class constructor 

69 super().__init__(properties) 

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

71 

72 # Input/Output files 

73 self.io_dict = { 

74 'in': {'input_structure_path': input_structure_path}, 

75 'out': {'output_pdb_path': output_pdb_path, 

76 'output_gro_path': output_gro_path, 

77 'output_dat_path': output_dat_path} 

78 } 

79 

80 # Properties specific for BB 

81 self.properties = properties 

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

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

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

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

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

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

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

89 

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

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

92 

93 # Check the properties 

94 self.check_properties(properties) 

95 self.check_arguments() 

96 

97 @launchlogger 

98 def launch(self): 

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

100 

101 # Set input params 

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

103 

104 # Setup Biobb 

105 if self.check_restart(): 

106 return 0 

107 self.stage_files() 

108 

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

110 concoord_lib = os.getenv("CONCOORDLIB") 

111 

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

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

114 

115 # Command line 

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

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

118 # Select a set of Van der Waals parameters: 

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

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

121 # 3: PROLSQ repel parameters 

122 # 4: Yamber2 parameters 

123 # 5: Li et al. parameters 

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

125 # 2 

126 # Selected parameter set 2 

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

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

129 # Select a set of bond/angle parameters: 

130 # 1: Concoord default parameters 

131 # 2: Engh-Huber parameters 

132 # 1 

133 # Selected parameter set 1 

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

135 

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

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

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

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

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

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

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

143 ] 

144 # If input structure in pdb format: 

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

146 if file_extension == ".pdb": 

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

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

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

150 

151 elif file_extension == ".gro": 

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

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

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

155 

156 else: 

157 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) 

158 

159 if self.retain_hydrogens: 

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

161 

162 if self.nb_interactions: 

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

164 

165 if self.fixed_atoms: 

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

167 

168 if self.cutoff: 

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

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

171 

172 if self.min_distances: 

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

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

175 

176 if self.damp: 

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

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

179 

180 # Add stdin input file 

181 self.cmd.append('<') 

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

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

184 

185 # Run Biobb block 

186 self.run_biobb() 

187 

188 # Copy files to host 

189 self.copy_to_host() 

190 

191 # remove temporary folder(s) 

192 self.tmp_files.extend([ 

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

194 ]) 

195 self.remove_tmp_files() 

196 

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

198 

199 return self.return_code 

200 

201 

202def concoord_dist(input_structure_path: str, 

203 output_pdb_path: str, output_gro_path: str, output_dat_path: str, 

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

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

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

207 return ConcoordDist(**dict(locals())).launch() 

208 

209 

210concoord_dist.__doc__ = ConcoordDist.__doc__ 

211main = ConcoordDist.get_main(concoord_dist, "Structure interpretation and bond definitions from a PDB/GRO file.") 

212 

213if __name__ == '__main__': 

214 main()