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

44 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-08 10:47 +0000

1#!/usr/bin/env python3 

2 

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

4 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

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://raw.githubusercontent.com/Amber-MD/cpptraj/master/doc/CpptrajManual.pdf>`_. 

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 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

25 

26 Examples: 

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

28 

29 from biobb_analysis.ambertools.cpptraj_input import cpptraj_input 

30 prop = { } 

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

32 properties=prop) 

33 

34 Info: 

35 * wrapped_software: 

36 * name: Ambertools Cpptraj 

37 * version: >=22.5 

38 * license: GNU 

39 * ontology: 

40 * name: EDAM 

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

42 

43 """ 

44 

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

46 properties = properties or {} 

47 

48 # Call parent class constructor 

49 super().__init__(properties) 

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

51 

52 # Input/Output files 

53 self.io_dict = { 

54 "in": {"input_instructions_path": input_instructions_path}, 

55 } 

56 

57 # Properties specific for BB 

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

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

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

61 self.properties = properties 

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

63 

64 # Check the properties 

65 self.check_init(properties) 

66 

67 def create_instrucions_file(self): 

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

69 instructions_list = [] 

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

71 

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

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

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

75 

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

77 for line in instructions_list: 

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

79 

80 return output_instructions_path 

81 

82 @launchlogger 

83 def launch(self) -> int: 

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

85 

86 # Get local loggers from launchlogger decorator 

87 

88 # Setup Biobb 

89 if self.check_restart(): 

90 return 0 

91 self.stage_files() 

92 

93 output_instructions_path = self.stage_io_dict.get('input_instructions_path', self.create_instrucions_file()) 

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

95 

96 # create cmd and launch execution 

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

98 # Run Biobb block 

99 self.run_biobb() 

100 # Copy files to host 

101 self.copy_to_host() 

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 return CpptrajInput(**dict(locals())).launch() 

109 

110 

111cpptraj_input.__doc__ = CpptrajInput.__doc__ 

112main = CpptrajInput.get_main(cpptraj_input, "Performs multiple analysis and trajectory operations of a given trajectory.") 

113 

114if __name__ == '__main__': 

115 main()