Coverage for biobb_analysis/ambertools/cpptraj_input.py: 79%

53 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-14 14:38 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the Cpptraj Input 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 import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_analysis.ambertools.common import get_default_value, check_in_path 

11 

12 

13class CpptrajInput(BiobbObject): 

14 """ 

15 | biobb_analysis CpptrajInput 

16 | Wrapper of the Ambertools Cpptraj module for performing multiple analysis and trajectory operations of a given trajectory. 

17 | Cpptraj (the successor to ptraj) is the main program in Ambertools for processing coordinate trajectories and data files. The parameter names and defaults are the same as the ones in the official `Cpptraj manual <https://amber-md.github.io/cpptraj/CPPTRAJ.xhtml>`_. 

18 

19 Args: 

20 input_instructions_path (str): Path of the instructions file. File type: input. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/data/ambertools/cpptraj.in>`_. Accepted formats: in (edam:format_2033). 

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

22 * **binary_path** (*str*) - ("cpptraj") Path to the cpptraj executable binary. 

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

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

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

26 

27 Examples: 

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

29 

30 from biobb_analysis.ambertools.cpptraj_input import cpptraj_input 

31 prop = { } 

32 cpptraj_input(input_instructions_path='/path/to/myInstructions.in', 

33 properties=prop) 

34 

35 Info: 

36 * wrapped_software: 

37 * name: Ambertools Cpptraj 

38 * version: >=20.0 

39 * license: GNU 

40 * ontology: 

41 * name: EDAM 

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

43 

44 """ 

45 

46 def __init__(self, input_instructions_path=None, properties=None, **kwargs) -> None: 

47 properties = properties or {} 

48 

49 # Call parent class constructor 

50 super().__init__(properties) 

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

52 

53 # Properties specific for BB 

54 self.input_instructions_path = input_instructions_path 

55 self.input_top_path = kwargs.get('input_top_path') 

56 self.input_traj_path = kwargs.get('input_traj_path') 

57 self.output_cpptraj_path = kwargs.get('output_cpptraj_path') 

58 self.properties = properties 

59 self.binary_path = properties.get('binary_path', 'cpptraj') 

60 

61 # Check the properties 

62 self.check_properties(properties) 

63 

64 def create_instrucions_file(self): 

65 """ Creates an input file using paths provideed in the configuration file (only used for test purposes) """ 

66 instructions_list = [] 

67 output_instructions_path = fu.create_name(prefix=self.prefix, step=self.step, name=get_default_value("instructions_file")) 

68 

69 instructions_list.append('parm ' + str(self.input_top_path)) 

70 instructions_list.append('trajin ' + str(self.input_traj_path)) 

71 instructions_list.append('trajout ' + str(self.output_cpptraj_path) + ' ' + get_default_value("format")) 

72 

73 with open(output_instructions_path, 'w') as mdp: 

74 for line in instructions_list: 

75 mdp.write(line.strip() + '\n') 

76 

77 return output_instructions_path 

78 

79 @launchlogger 

80 def launch(self) -> int: 

81 """Execute the :class:`CpptrajInput <ambertools.cpptraj_input.CpptrajInput>` ambertools.cpptraj_input.CpptrajInput object.""" 

82 

83 # Get local loggers from launchlogger decorator 

84 

85 # Setup Biobb 

86 if self.check_restart(): 

87 return 0 

88 self.stage_files() 

89 

90 output_instructions_path = self.create_instrucions_file() if not self.input_instructions_path else self.input_instructions_path 

91 check_in_path(output_instructions_path, self.out_log, self.__class__.__name__) 

92 

93 # create cmd and launch execution 

94 self.cmd = [self.binary_path, '-i', output_instructions_path] 

95 

96 # Run Biobb block 

97 self.run_biobb() 

98 

99 # Copy files to host 

100 self.copy_to_host() 

101 

102 return self.return_code 

103 

104 

105def cpptraj_input(input_instructions_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

106 """Execute the :class:`CpptrajInput <ambertools.cpptraj_input.CpptrajInput>` class and 

107 execute the :meth:`launch() <ambertools.cpptraj_input.CpptrajInput.launch>` method.""" 

108 

109 return CpptrajInput(input_instructions_path=input_instructions_path, 

110 properties=properties, **kwargs).launch() 

111 

112 

113def main(): 

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

115 parser = argparse.ArgumentParser(description="Performs multiple analysis and trajectory operations of a given trajectory.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

117 

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

119 required_args.add_argument('--input_instructions_path', required=True, help='Path of the instructions file.') 

120 

121 args = parser.parse_args() 

122 args.config = args.config or "{}" 

123 properties = settings.ConfReader(config=args.config).get_prop_dic() 

124 

125 # Specific call of each building block 

126 cpptraj_input(input_instructions_path=args.input_instructions_path, 

127 properties=properties) 

128 

129 

130if __name__ == '__main__': 

131 main()