Coverage for biobb_amber/ambpdb/amber_to_pdb.py: 73%

48 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-28 08:28 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools.file_utils import launchlogger 

9from biobb_amber.ambpdb.common import check_input_path, check_output_path 

10 

11 

12class AmberToPDB(BiobbObject): 

13 """ 

14 | biobb_amber AmberToPDB 

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

16 | Generates a PDB structure from AMBER topology (parmtop) and coordinates (crd) files, using the ambpdb tool from the AmberTools MD package. 

17 

18 Args: 

19 input_top_path (str): AMBER topology file. File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/ambpdb/structure.leap.top>`_. Accepted formats: top (edam:format_3881), parmtop (edam:format_3881), prmtop (edam:format_3881). 

20 input_crd_path (str): AMBER coordinates file. File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/ambpdb/structure.leap.crd>`_. Accepted formats: crd (edam:format_3878), mdcrd (edam:format_3878), inpcrd (edam:format_3878), rst (edam:format_3886). 

21 output_pdb_path (str): Structure PDB file. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/ambpdb/structure.ambpdb.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

23 * **binary_path** (*str*) - ("ambpdb") Path to the ambpdb executable binary. 

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

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

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

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

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

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

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

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

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

33 

34 Examples: 

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

36 

37 from biobb_amber.ambpdb.amber_to_pdb import amber_to_pdb 

38 prop = { 

39 'remove_tmp': True 

40 } 

41 amber_to_pdb(input_top_path='/path/to/topology.top', 

42 input_crd_path='/path/to/coordinates.crd', 

43 output_pdb_path='/path/to/newStructure.pdb', 

44 properties=prop) 

45 

46 Info: 

47 * wrapped_software: 

48 * name: AmberTools ambpdb 

49 * version: >20.9 

50 * license: LGPL 2.1 

51 * ontology: 

52 * name: EDAM 

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

54 

55 """ 

56 

57 def __init__(self, input_top_path, input_crd_path, output_pdb_path, properties: Optional[dict] = None, **kwargs): 

58 properties = properties or {} 

59 

60 # Call parent class constructor 

61 super().__init__(properties) 

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

63 

64 # Input/Output files 

65 self.io_dict = { 

66 'in': {'input_top_path': input_top_path, 

67 'input_crd_path': input_crd_path}, 

68 'out': {'output_pdb_path': output_pdb_path} 

69 } 

70 

71 # Properties specific for BB 

72 self.properties = properties 

73 self.binary_path = properties.get('binary_path', 'ambpdb') 

74 

75 # Check the properties 

76 self.check_properties(properties) 

77 self.check_arguments() 

78 

79 def check_data_params(self, out_log, err_log): 

80 """ Checks input/output paths correctness """ 

81 

82 # Check input(s) 

83 self.io_dict["in"]["input_top_path"] = check_input_path(self.io_dict["in"]["input_top_path"], "input_top_path", False, out_log, self.__class__.__name__) 

84 self.io_dict["in"]["input_crd_path"] = check_input_path(self.io_dict["in"]["input_crd_path"], "input_crd_path", False, out_log, self.__class__.__name__) 

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 

92 # check input/output paths and parameters 

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

94 

95 # Setup Biobb 

96 if self.check_restart(): 

97 return 0 

98 self.stage_files() 

99 

100 # Command line 

101 self.cmd = [self.binary_path, 

102 '-p', self.stage_io_dict['in']['input_top_path'], 

103 '-c', self.stage_io_dict['in']['input_crd_path'], 

104 '> ', self.stage_io_dict['out']['output_pdb_path'] 

105 ] 

106 

107 # Run Biobb block 

108 self.run_biobb() 

109 

110 # Copy files to host 

111 self.copy_to_host() 

112 

113 # Remove temporary file(s) 

114 # self.tmp_files.extend([ 

115 # self.stage_io_dict.get("unique_dir", "") 

116 # ]) 

117 self.remove_tmp_files() 

118 

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

120 

121 return self.return_code 

122 

123 

124def amber_to_pdb(input_top_path: str, input_crd_path: str, output_pdb_path: str, 

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

126 """Create :class:`AmberToPDB <amber.amber_to_pdb.AmberToPDB>`amber.amber_to_pdb.AmberToPDB class and 

127 execute :meth:`launch() <amber.amber_to_pdb.AmberToPDB.launch>` method""" 

128 

129 return AmberToPDB(input_top_path=input_top_path, 

130 input_crd_path=input_crd_path, 

131 output_pdb_path=output_pdb_path, 

132 properties=properties).launch() 

133 

134 amber_to_pdb.__doc__ = AmberToPDB.__doc__ 

135 

136 

137def main(): 

138 """Command line execution of this building block. Please check the command line documentation.""" 

139 parser = argparse.ArgumentParser(description='Generates a PDB structure from AMBER topology (parmtop) and coordinates (crd) files, using the ambpdb tool from the AmberTools MD package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

141 

142 # Specific args 

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

144 required_args.add_argument('--input_top_path', required=True, help='AMBER topology file. Accepted formats: top, parmtop, prmtop.') 

145 required_args.add_argument('--input_crd_path', required=True, help='AMBER coordinates file. Accepted formats: crd, mdcrd, inpcrd.') 

146 required_args.add_argument('--output_pdb_path', required=True, help='Structure PDB file. Accepted formats: pdb.') 

147 

148 args = parser.parse_args() 

149 config = args.config if args.config else None 

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

151 

152 # Specific call 

153 amber_to_pdb(input_top_path=args.input_top_path, 

154 input_crd_path=args.input_crd_path, 

155 output_pdb_path=args.output_pdb_path, 

156 properties=properties) 

157 

158 

159if __name__ == '__main__': 

160 main()