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

63 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-07 08:11 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from pathlib import PurePath 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_amber.leap.common import check_output_path 

11 

12 

13class LeapBuildLinearStructure(BiobbObject): 

14 """ 

15 | biobb_amber.leap.leap_build_linear_structure LeapBuildLinearStructure 

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

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

18 

19 Args: 

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

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

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

23 * **forcefield** (*list*) - (["protein.ff14SB","DNA.bsc1","gaff"]) Forcefield to be used for the structure generation. Values: protein.ff14SB, protein.ff19SB, DNA.bsc1, DNA.OL15, RNA.OL3, gaff. 

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

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

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

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

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_amber.leap.leap_build_linear_structure import leap_build_linear_structure 

39 prop = { 

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

41 'build_library': False, 

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

43 } 

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

45 properties=prop) 

46 

47 

48 Info: 

49 * wrapped_software: 

50 * name: AmberTools tLeap 

51 * version: >20.9 

52 * license: LGPL 2.1 

53 * ontology: 

54 * name: EDAM 

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

56 

57 """ 

58 

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

60 properties = properties or {} 

61 

62 # Call parent class constructor 

63 super().__init__(properties) 

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

65 

66 # Input/Output files 

67 self.io_dict = { 

68 'in': {}, 

69 'out': {'output_pdb_path': output_pdb_path} 

70 } 

71 

72 # Properties specific for BB 

73 self.properties = properties 

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

75 self.forcefield = properties.get('forcefield', ["protein.ff14SB", "DNA.bsc1", "gaff"]) 

76 self.build_library = properties.get('build_library', False) 

77 self.binary_path = properties.get('binary_path', 'tleap') 

78 

79 # Check the properties 

80 self.check_properties(properties) 

81 self.check_arguments() 

82 

83 def check_data_params(self, out_log, err_log): 

84 """ Checks input/output paths correctness """ 

85 

86 # Check output(s) 

87 self.io_dict["out"]["output_pdb_path"] = check_output_path(self.io_dict["out"]["output_pdb_path"], "output_pdb_path", False, out_log, self.__class__.__name__) 

88 

89 @launchlogger 

90 def launch(self): 

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

92 

93 # check input/output paths and parameters 

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

95 

96 # Setup Biobb 

97 if self.check_restart(): 

98 return 0 

99 self.stage_files() 

100 

101 # create .in file 

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

103 # savepdb TC5b TC5b_linear.pdb 

104 # quit 

105 

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

107 if self.container_path: 

108 instructions_file = str(PurePath(self.stage_io_dict['unique_dir']).joinpath("leap.in")) 

109 instructions_file_path = str(PurePath(self.container_volume_path).joinpath("leap.in")) 

110 self.tmp_folder = None 

111 else: 

112 self.tmp_folder = fu.create_unique_dir() 

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

114 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log) 

115 instructions_file_path = instructions_file 

116 

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

118 with open(instructions_file, 'w') as leapin: 

119 

120 # Forcefields loaded from input forcefield property 

121 for t in self.forcefield: 

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

123 

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

125 leapin.write("savepdb struct " + self.stage_io_dict['out']['output_pdb_path'] + "\n") 

126 leapin.write("quit \n") 

127 

128 # Command line 

129 self.cmd = [self.binary_path, 

130 '-f', instructions_file_path 

131 ] 

132 

133 # Run Biobb block 

134 self.run_biobb() 

135 

136 # Copy files to host 

137 self.copy_to_host() 

138 

139 # remove temporary folder(s) 

140 self.tmp_files.extend([ 

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

142 self.tmp_folder, 

143 "leap.log" 

144 ]) 

145 self.remove_tmp_files() 

146 

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

148 

149 return self.return_code 

150 

151 

152def leap_build_linear_structure(output_pdb_path: str, 

153 properties: dict = None, **kwargs) -> int: 

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

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

156 

157 return LeapBuildLinearStructure(output_pdb_path=output_pdb_path, 

158 properties=properties).launch() 

159 

160 

161def main(): 

162 parser = argparse.ArgumentParser(description='Building a linear (unfolded) 3D structure from an AA sequence.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

164 

165 # Specific args 

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

167 required_args.add_argument('--output_pdb_path', required=True, help='Linear (unfolded) 3D structure PDB file. Accepted formats: pdb.') 

168 

169 args = parser.parse_args() 

170 config = args.config if args.config else None 

171 properties = settings.ConfReader(config=config).get_prop_dic() 

172 

173 # Specific call 

174 leap_build_linear_structure(output_pdb_path=args.output_pdb_path, 

175 properties=properties) 

176 

177 

178if __name__ == '__main__': 

179 main()