Coverage for biobb_template / template / template_container.py: 26%

42 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-10 14:44 +0000

1#!/usr/bin/env python3 

2 

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

4from biobb_common.generic.biobb_object import BiobbObject 

5from biobb_common.tools import file_utils as fu 

6from biobb_common.tools.file_utils import launchlogger 

7 

8 

9# 1. Rename class as required 

10class TemplateContainer(BiobbObject): 

11 """ 

12 | biobb_template TemplateContainer 

13 | Short description for the `template container <http://templatedocumentation.org>`_ module in Restructured Text (reST) syntax. Mandatory. 

14 | Long description for the `template container <http://templatedocumentation.org>`_ module in Restructured Text (reST) syntax. Optional. 

15 

16 Args: 

17 input_file_path1 (str): Description for the first input file path. File type: input. `Sample file <https://urlto.sample>`_. Accepted formats: top (edam:format_3881). 

18 input_file_path2 (str) (Optional): Description for the second input file path (optional). File type: input. `Sample file <https://urlto.sample>`_. Accepted formats: dcd (edam:format_3878). 

19 output_file_path (str): Description for the output file path. File type: output. `Sample file <https://urlto.sample>`_. Accepted formats: zip (edam:format_3987). 

20 properties (dic): 

21 * **boolean_property** (*bool*) - (True) Example of boolean property. 

22 * **binary_path** (*str*) - ("zip") Example of executable binary property. 

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 * **container_path** (*str*) - (None) Container path definition. 

27 * **container_image** (*str*) - ('mmbirb/zip:latest') 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_template.template.template_container import template_container 

37 

38 prop = { 

39 'boolean_property': True, 

40 'container_path': 'docker', 

41 'container_image': 'mmbirb/zip:latest', 

42 'container_volume_path': '/tmp' 

43 } 

44 template_container(input_file_path1='/path/to/myTopology.top', 

45 output_file_path='/path/to/newCompressedFile.zip', 

46 input_file_path2='/path/to/mytrajectory.dcd', 

47 properties=prop) 

48 

49 Info: 

50 * wrapped_software: 

51 * name: Zip 

52 * version: >=3.0 

53 * license: BSD 3-Clause 

54 * ontology: 

55 * name: EDAM 

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

57 

58 """ 

59 

60 # 2. Adapt input and output file paths as required. Include all files, even optional ones 

61 def __init__(self, input_file_path1, output_file_path, input_file_path2=None, properties=None, **kwargs) -> None: 

62 properties = properties or {} 

63 

64 # 2.0 Call parent class constructor 

65 super().__init__(properties) 

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

67 

68 # 2.1 Modify to match constructor parameters 

69 # Input/Output files 

70 self.io_dict = { 

71 'in': {'input_file_path1': input_file_path1, 'input_file_path2': input_file_path2}, 

72 'out': {'output_file_path': output_file_path} 

73 } 

74 

75 # 3. Include all relevant properties here as 

76 # self.property_name = properties.get('property_name', property_default_value) 

77 

78 # Properties specific for BB 

79 self.boolean_property = properties.get('boolean_property', True) 

80 self.binary_path = properties.get('binary_path', 'zip') 

81 self.properties = properties 

82 

83 # Check the properties 

84 self.check_properties(properties) 

85 # Check the arguments 

86 self.check_arguments() 

87 

88 @launchlogger 

89 def launch(self) -> int: 

90 """Execute the :class:`TemplateContainer <template.template_container.TemplateContainer>` object.""" 

91 

92 # 4. Setup Biobb 

93 if self.check_restart(): 

94 return 0 

95 self.stage_files() 

96 

97 # Creating temporary folder 

98 self.tmp_folder = fu.create_unique_dir() 

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

100 

101 # 5. Prepare the command line parameters as instructions list 

102 instructions = ['-j'] 

103 if self.boolean_property: 

104 instructions.append('-v') 

105 fu.log('Appending optional boolean property', self.out_log, self.global_log) 

106 

107 # 6. Build the actual command line as a list of items (elements order will be maintained) 

108 self.cmd = [self.binary_path, 

109 ' '.join(instructions), 

110 self.stage_io_dict['out']['output_file_path'], 

111 self.stage_io_dict['in']['input_file_path1']] 

112 fu.log('Creating command line with instructions and required arguments', self.out_log, self.global_log) 

113 

114 # 7. Repeat for optional input files if provided 

115 if self.stage_io_dict['in']['input_file_path2']: 

116 # Append optional input_file_path2 to cmd 

117 self.cmd.append(self.stage_io_dict['in']['input_file_path2']) 

118 fu.log('Appending optional argument to command line', self.out_log, self.global_log) 

119 

120 # 8. Uncomment to check the command line 

121 # print(' '.join(cmd)) 

122 

123 # Run Biobb block 

124 self.run_biobb() 

125 

126 # Copy files to host 

127 self.copy_to_host() 

128 

129 # Remove temporary file(s) 

130 self.tmp_files.append(self.tmp_folder) 

131 self.remove_tmp_files() 

132 

133 # Check output arguments 

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

135 

136 return self.return_code 

137 

138 

139def template_container(input_file_path1: str, output_file_path: str, input_file_path2: str = None, properties: dict = None, **kwargs) -> int: 

140 """Create :class:`TemplateContainer <template.template_container.TemplateContainer>` class and 

141 execute the :meth:`launch() <template.template_container.TemplateContainer.launch>` method.""" 

142 return TemplateContainer(**dict(locals())).launch() 

143 

144 

145template_container.__doc__ = TemplateContainer.__doc__ 

146main = TemplateContainer.get_main(template_container, 'Description for the template container module.') 

147 

148 

149if __name__ == '__main__': 

150 main() 

151 

152# 13. Complete documentation strings