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

53 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-06 15:22 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the Cpptraj Input class and the command line interface.""" 

4import argparse 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.configuration import settings 

7from biobb_common.tools import file_utils as fu 

8from biobb_common.tools.file_utils import launchlogger 

9from biobb_analysis.ambertools.common import get_default_value, check_in_path 

10 

11 

12class CpptrajInput(BiobbObject): 

13 """ 

14 | biobb_analysis CpptrajInput 

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

16 | 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>`_. 

17 

18 Args: 

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

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

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

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

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

24 

25 Examples: 

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

27 

28 from biobb_analysis.ambertools.cpptraj_input import cpptraj_input 

29 prop = { } 

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

31 properties=prop) 

32 

33 Info: 

34 * wrapped_software: 

35 * name: Ambertools Cpptraj 

36 * version: >=20.0 

37 * license: GNU 

38 * ontology: 

39 * name: EDAM 

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

41 

42 """ 

43 

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

45 properties = properties or {} 

46 

47 # Call parent class constructor 

48 super().__init__(properties) 

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

50 

51 # Properties specific for BB 

52 self.input_instructions_path = input_instructions_path 

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

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

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

56 self.properties = properties 

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

58 

59 # Check the properties 

60 self.check_properties(properties) 

61 

62 def create_instrucions_file(self): 

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

64 instructions_list = [] 

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

66 

67 instructions_list.append('parm ' + self.input_top_path) 

68 instructions_list.append('trajin ' + self.input_traj_path) 

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

70 

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

72 for line in instructions_list: 

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

74 

75 return output_instructions_path 

76 

77 @launchlogger 

78 def launch(self) -> int: 

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

80 

81 # Get local loggers from launchlogger decorator 

82 

83 # Setup Biobb 

84 if self.check_restart(): 

85 return 0 

86 self.stage_files() 

87 

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

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

90 

91 # create cmd and launch execution 

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

93 

94 # Run Biobb block 

95 self.run_biobb() 

96 

97 # Copy files to host 

98 self.copy_to_host() 

99 

100 return self.return_code 

101 

102 

103def cpptraj_input(input_instructions_path: str, properties: dict = None, **kwargs) -> int: 

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

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

106 

107 return CpptrajInput(input_instructions_path=input_instructions_path, 

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

109 

110 

111def main(): 

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

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

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

115 

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

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

118 

119 args = parser.parse_args() 

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

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

122 

123 # Specific call of each building block 

124 cpptraj_input(input_instructions_path=args.input_instructions_path, 

125 properties=properties) 

126 

127 

128if __name__ == '__main__': 

129 main()