Coverage for biobb_amber/parmed/parmed_hmassrepartition.py: 76%

59 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 ParmedHMassRepartition 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.parmed.common import check_input_path, check_output_path 

11 

12 

13class ParmedHMassRepartition(BiobbObject): 

14 """ 

15 | biobb_amber ParmedHMassRepartition 

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

17 | Performs a Hydrogen Mass Repartition from an AMBER topology file using parmed tool from the AmberTools MD package. 

18 

19 Args: 

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

21 output_top_path (str): Output topology file (AMBER ParmTop). File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/parmed/output.hmass.prmtop>`_. Accepted formats: top (edam:format_3881), parmtop (edam:format_3881), prmtop (edam:format_3881). 

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

23 * **binary_path** (*str*) - ("parmed") Path to the parmed 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 * **container_path** (*str*) - (None) Container path definition. 

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

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

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

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

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

32 

33 Examples: 

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

35 

36 from biobb_amber.parmed.parmed_hmassrepartition import parmed_hmassrepartition 

37 parmed_hmassrepartition(input_top_path='/path/to/topology.top', 

38 output_top_path='/path/to/newTopology.top') 

39 

40 Info: 

41 * wrapped_software: 

42 * name: AmberTools parmed 

43 * version: >20.9 

44 * license: LGPL 2.1 

45 * ontology: 

46 * name: EDAM 

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

48 

49 """ 

50 

51 def __init__(self, input_top_path, output_top_path, properties=None, **kwargs) -> None: 

52 

53 properties = properties or {} 

54 

55 # Call parent class constructor 

56 super().__init__(properties) 

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

58 

59 # Input/Output files 

60 self.io_dict = { 

61 'in': {'input_top_path': input_top_path}, 

62 'out': {'output_top_path': output_top_path} 

63 } 

64 

65 # Properties specific for BB 

66 self.properties = properties 

67 self.binary_path = properties.get('binary_path', 'parmed') 

68 

69 # Check the properties 

70 self.check_properties(properties) 

71 self.check_arguments() 

72 

73 def check_data_params(self, out_log, err_log): 

74 """ Checks input/output paths correctness """ 

75 

76 # Check input(s) 

77 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__) 

78 

79 # Check output(s) 

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

81 

82 @launchlogger 

83 def launch(self): 

84 """Launches the execution of the ParmedHMassRepartition module.""" 

85 

86 # check input/output paths and parameters 

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

88 

89 # Setup Biobb 

90 if self.check_restart(): 

91 return 0 

92 self.stage_files() 

93 

94 # Creating temporary folder & Parmed configuration (instructions) file 

95 if self.container_path: 

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

97 instructions_file_path = str(PurePath(self.container_volume_path).joinpath("parmed.in")) 

98 self.tmp_folder = None 

99 else: 

100 self.tmp_folder = fu.create_unique_dir() 

101 instructions_file = str(PurePath(self.tmp_folder).joinpath("parmed.in")) 

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

103 instructions_file_path = instructions_file 

104 

105 with open(instructions_file, 'w') as parmedin: 

106 parmedin.write("hmassrepartition\n") 

107 parmedin.write("outparm " + self.stage_io_dict['out']['output_top_path'] + "\n") 

108 

109 self.cmd = [self.binary_path, 

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

111 '-i', instructions_file_path, 

112 '-O' # Overwrite output files 

113 ] 

114 

115 # Run Biobb block 

116 self.run_biobb() 

117 

118 # Copy files to host 

119 self.copy_to_host() 

120 

121 # remove temporary folder(s) 

122 self.tmp_files.extend([ 

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

124 self.tmp_folder 

125 ]) 

126 self.remove_tmp_files() 

127 

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

129 

130 return self.return_code 

131 

132 

133def parmed_hmassrepartition(input_top_path: str, 

134 output_top_path: str = None, 

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

136 """Create :class:`ParmedHMassRepartition <parmed.parmed_hmassrepartition.ParmedHMassRepartition>`parmed.parmed_hmassrepartition.ParmedHMassRepartition class and 

137 execute :meth:`launch() <parmed.parmed_hmassrepartition.ParmedHMassRepartition.launch>` method""" 

138 

139 return ParmedHMassRepartition(input_top_path=input_top_path, 

140 output_top_path=output_top_path, 

141 properties=properties).launch() 

142 

143 

144def main(): 

145 parser = argparse.ArgumentParser(description='Performs a Hydrogen Mass Repartition from an AMBER topology file using parmed tool from the AmberTools MD package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

147 

148 # Specific args 

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

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

151 required_args.add_argument('--output_top_path', required=False, help='Output topology file (AMBER ParmTop). Accepted formats: top, parmtop, prmtop.') 

152 

153 args = parser.parse_args() 

154 config = args.config if args.config else None 

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

156 

157 # Specific call 

158 parmed_hmassrepartition(input_top_path=args.input_top_path, 

159 output_top_path=args.output_top_path, 

160 properties=properties) 

161 

162 

163if __name__ == '__main__': 

164 main()