Coverage for biobb_amber / leap / leap_build_linear_structure.py: 74%

91 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-12 10:52 +0000

1#!/usr/bin/env python3 

2 

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

4 

5import os 

6from pathlib import PurePath 

7from typing import List, Optional 

8 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools import file_utils as fu 

11from biobb_common.tools.file_utils import launchlogger 

12 

13from biobb_amber.leap.common import _from_string_to_list, check_output_path 

14 

15 

16class LeapBuildLinearStructure(BiobbObject): 

17 """ 

18 | biobb_amber.leap.leap_build_linear_structure LeapBuildLinearStructure 

19 | Wrapper of the `AmberTools (AMBER MD Package) leap tool <https://ambermd.org/AmberTools.php>`_ module. 

20 | Builds a linear (unfolded) 3D structure from an AA sequence using tLeap tool from the AmberTools MD package. 

21 

22 Args: 

23 output_pdb_path (str): Linear (unfolded) 3D structure PDB file. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/leap/structure.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

25 * **sequence** (*str*) - ("ALA GLY SER PRO ARG ALA PRO GLY") Aminoacid sequence to convert to a linear 3D structure. Aminoacids should be written in 3-letter code, with a blank space between them. 

26 * **forcefield** (*list*) - (["protein.ff14SB","DNA.bsc1","gaff"]) Forcefields to be used for the structure generation. Each item should be either a path to a leaprc file or a string with the leaprc file name if the force field is included with Amber (e.g. "/path/to/leaprc.protein.ff14SB" or "protein.ff14SB"). Default values: ["protein.ff14SB","DNA.bsc1","gaff"]. 

27 * **build_library** (*bool*) - (False) Generate AMBER lib file for the structure. 

28 * **binary_path** (*str*) - ("tleap") Path to the tleap executable binary. 

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

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

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

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

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

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

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

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

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

38 

39 Examples: 

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

41 

42 from biobb_amber.leap.leap_build_linear_structure import leap_build_linear_structure 

43 prop = { 

44 'sequence' : 'ALA PRO SER ARG LYS ASP GLU GLY GLY ALA', 

45 'build_library': False, 

46 'forcefield': ['protein.ff14SB'] 

47 } 

48 leap_build_linear_structure(output_pdb_path='/path/to/newStructure.pdb', 

49 properties=prop) 

50 

51 

52 Info: 

53 * wrapped_software: 

54 * name: AmberTools tLeap 

55 * version: >20.9 

56 * license: LGPL 2.1 

57 * ontology: 

58 * name: EDAM 

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

60 

61 """ 

62 

63 def __init__(self, output_pdb_path, properties, **kwargs): 

64 properties = properties or {} 

65 

66 # Call parent class constructor 

67 super().__init__(properties) 

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

69 

70 # Input/Output files 

71 self.io_dict = {"in": {}, "out": {"output_pdb_path": output_pdb_path}} 

72 

73 # Properties specific for BB 

74 self.properties = properties 

75 

76 # Set default forcefields 

77 if self.container_path: 

78 self.forcefield = _from_string_to_list( 

79 properties.get("forcefield", ['leaprc.protein.ff14SB', 'leaprc.DNA.bsc1', 'leaprc.gaff']) 

80 ) 

81 else: 

82 amber_home_path = os.getenv("AMBERHOME") 

83 protein_ff14SB_path = os.path.join(amber_home_path, 'dat', 'leap', 'cmd', 'leaprc.protein.ff14SB') 

84 dna_bsc1_path = os.path.join(amber_home_path, 'dat', 'leap', 'cmd', 'leaprc.DNA.bsc1') 

85 gaff_path = os.path.join(amber_home_path, 'dat', 'leap', 'cmd', 'leaprc.gaff') 

86 

87 self.forcefield = _from_string_to_list( 

88 properties.get("forcefield", [protein_ff14SB_path, dna_bsc1_path, gaff_path]) 

89 ) 

90 

91 # Find the paths of the leaprc files if only the force field names are provided 

92 self.forcefield = self.find_leaprc_paths(self.forcefield) 

93 

94 self.sequence = properties.get("sequence", "ALA GLY SER PRO ARG ALA PRO GLY") 

95 self.build_library = properties.get("build_library", False) 

96 self.binary_path = properties.get("binary_path", "tleap") 

97 

98 # Check the properties 

99 self.check_properties(properties) 

100 self.check_arguments() 

101 

102 def check_data_params(self, out_log, err_log): 

103 """Checks input/output paths correctness""" 

104 

105 # Check output(s) 

106 self.io_dict["out"]["output_pdb_path"] = check_output_path( 

107 self.io_dict["out"]["output_pdb_path"], 

108 "output_pdb_path", 

109 False, 

110 out_log, 

111 self.__class__.__name__, 

112 ) 

113 

114 def find_leaprc_paths(self, forcefields: List[str]) -> List[str]: 

115 """ 

116 Find the leaprc paths for the force fields provided. 

117 

118 For each item in the forcefields list, the function checks if the str is a path to an existing file. 

119 If not, it tries to find the file in the $AMBERHOME/dat/leap/cmd/ directory or the $AMBERHOME/dat/leap/cmd/oldff/ 

120 directory with and without the leaprc prefix. 

121 

122 Args: 

123 forcefields (List[str]): List of force fields to find the leaprc files for. 

124 

125 Returns: 

126 List[str]: List of leaprc file paths. 

127 """ 

128 

129 leaprc_paths = [] 

130 

131 for forcefield in forcefields: 

132 

133 num_paths = len(leaprc_paths) 

134 

135 # Check if the forcefield is a path to an existing file 

136 if os.path.exists(forcefield): 

137 leaprc_paths.append(forcefield) 

138 continue 

139 

140 # Check if the forcefield is in the leaprc directory 

141 leaprc_path = os.path.join(os.environ.get('AMBERHOME', ''), 'dat', 'leap', 'cmd', f"leaprc.{forcefield}") 

142 if os.path.exists(leaprc_path): 

143 leaprc_paths.append(leaprc_path) 

144 continue 

145 

146 # Check if the forcefield is in the oldff directory 

147 leaprc_path = os.path.join(os.environ.get('AMBERHOME', ''), 'dat', 'leap', 'cmd', 'oldff', f"leaprc.{forcefield}") 

148 if os.path.exists(leaprc_path): 

149 leaprc_paths.append(leaprc_path) 

150 continue 

151 

152 # Check if the forcefield is in the leaprc directory without the leaprc prefix 

153 leaprc_path = os.path.join(os.environ.get('AMBERHOME', ''), 'dat', 'leap', 'cmd', f"{forcefield}") 

154 if os.path.exists(leaprc_path): 

155 leaprc_paths.append(leaprc_path) 

156 continue 

157 

158 # Check if the forcefield is in the oldff directory without the leaprc prefix 

159 leaprc_path = os.path.join(os.environ.get('AMBERHOME', ''), 'dat', 'leap', 'cmd', 'oldff', f"{forcefield}") 

160 if os.path.exists(leaprc_path): 

161 leaprc_paths.append(leaprc_path) 

162 continue 

163 

164 new_num_paths = len(leaprc_paths) 

165 

166 if new_num_paths == num_paths: 

167 raise ValueError(f"Force field {forcefield} not found. Check the $AMBERHOME/dat/leap/cmd/ directory for available force fields or provide the path to an existing leaprc file.") 

168 

169 return leaprc_paths 

170 

171 @launchlogger 

172 def launch(self): 

173 """Launches the execution of the LeapBuildLinearStructure module.""" 

174 

175 # check input/output paths and parameters 

176 self.check_data_params(self.out_log, self.err_log) 

177 

178 # Setup Biobb 

179 if self.check_restart(): 

180 return 0 

181 self.stage_files() 

182 

183 # create .in file 

184 # TC5b = sequence { NASN LEU TYR ILE GLN TRP LEU LYS ASP GLY GLY PRO SER SER GLY ARG PRO PRO PRO CSER } 

185 # savepdb TC5b TC5b_linear.pdb 

186 # quit 

187 

188 # Creating temporary folder & Leap configuration (instructions) file 

189 if self.container_path: 

190 instructions_file = str( 

191 PurePath(self.stage_io_dict["unique_dir"]).joinpath("leap.in") 

192 ) 

193 instructions_file_path = str( 

194 PurePath(self.container_volume_path).joinpath("leap.in") 

195 ) 

196 tmp_folder = None 

197 else: 

198 tmp_folder = fu.create_unique_dir() 

199 instructions_file = str(PurePath(tmp_folder).joinpath("leap.in")) 

200 fu.log("Creating %s temporary folder" % tmp_folder, self.out_log) 

201 instructions_file_path = instructions_file 

202 

203 # instructions_file = str(PurePath(tmp_folder).joinpath("leap.in")) 

204 with open(instructions_file, "w") as leapin: 

205 # Forcefields loaded from input forcefield property 

206 for t in self.forcefield: 

207 if self.container_path: 

208 leapin.write("source leaprc.{}\n".format(t)) 

209 else: 

210 leapin.write("source {}\n".format(t)) 

211 

212 leapin.write("struct = sequence {" + self.sequence + " } \n") 

213 leapin.write( 

214 "savepdb struct " + self.stage_io_dict["out"]["output_pdb_path"] + "\n" 

215 ) 

216 leapin.write("quit \n") 

217 

218 # Command line 

219 self.cmd = [self.binary_path, "-f", instructions_file_path] 

220 

221 # Run Biobb block 

222 self.run_biobb() 

223 

224 # Copy files to host 

225 self.copy_to_host() 

226 

227 # remove temporary folder(s) 

228 self.tmp_files.extend([str(tmp_folder), "leap.log"]) 

229 self.remove_tmp_files() 

230 

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

232 

233 return self.return_code 

234 

235 

236def leap_build_linear_structure( 

237 output_pdb_path: str, properties: Optional[dict] = None, **kwargs 

238) -> int: 

239 """Create the :class:`LeapBuildLinearStructure <leap.leap_build_linear_structure.LeapBuildLinearStructure>` class and 

240 execute the :meth:`launch() <leap.leap_build_linear_structure.LeapBuildLinearStructure.launch>` method.""" 

241 return LeapBuildLinearStructure(**dict(locals())).launch() 

242 

243 

244leap_build_linear_structure.__doc__ = LeapBuildLinearStructure.__doc__ 

245main = LeapBuildLinearStructure.get_main(leap_build_linear_structure, "Building a linear (unfolded) 3D structure from an AA sequence.") 

246 

247if __name__ == "__main__": 

248 main()