Coverage for biobb_template/template/template_container.py: 23%
53 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-03 15:35 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-03 15:35 +0000
1#!/usr/bin/env python3
3"""Module containing the TemplateContainer 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
11# 1. Rename class as required
12class TemplateContainer(BiobbObject):
13 """
14 | biobb_template TemplateContainer
15 | Short description for the `template container <http://templatedocumentation.org>`_ module in Restructured Text (reST) syntax. Mandatory.
16 | Long description for the `template container <http://templatedocumentation.org>`_ module in Restructured Text (reST) syntax. Optional.
18 Args:
19 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).
20 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).
21 output_file_path (str): Description for the output file path. File type: output. `Sample file <https://urlto.sample>`_. Accepted formats: zip (edam:format_3987).
22 properties (dic):
23 * **boolean_property** (*bool*) - (True) Example of boolean property.
24 * **binary_path** (*str*) - ("zip") Example of executable binary property.
25 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
26 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
27 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
28 * **container_path** (*str*) - (None) Container path definition.
29 * **container_image** (*str*) - ('mmbirb/zip:latest') Container image definition.
30 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition.
31 * **container_working_dir** (*str*) - (None) Container working directory definition.
32 * **container_user_id** (*str*) - (None) Container user_id definition.
33 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container.
35 Examples:
36 This is a use example of how to use the building block from Python::
38 from biobb_template.template.template_container import template_container
40 prop = {
41 'boolean_property': True,
42 'container_path': 'docker',
43 'container_image': 'mmbirb/zip:latest',
44 'container_volume_path': '/tmp'
45 }
46 template_container(input_file_path1='/path/to/myTopology.top',
47 output_file_path='/path/to/newCompressedFile.zip',
48 input_file_path2='/path/to/mytrajectory.dcd',
49 properties=prop)
51 Info:
52 * wrapped_software:
53 * name: Zip
54 * version: >=3.0
55 * license: BSD 3-Clause
56 * ontology:
57 * name: EDAM
58 * schema: http://edamontology.org/EDAM.owl
60 """
62 # 2. Adapt input and output file paths as required. Include all files, even optional ones
63 def __init__(self, input_file_path1, output_file_path, input_file_path2=None, properties=None, **kwargs) -> None:
64 properties = properties or {}
66 # 2.0 Call parent class constructor
67 super().__init__(properties)
68 self.locals_var_dict = locals().copy()
70 # 2.1 Modify to match constructor parameters
71 # Input/Output files
72 self.io_dict = {
73 'in': {'input_file_path1': input_file_path1, 'input_file_path2': input_file_path2},
74 'out': {'output_file_path': output_file_path}
75 }
77 # 3. Include all relevant properties here as
78 # self.property_name = properties.get('property_name', property_default_value)
80 # Properties specific for BB
81 self.boolean_property = properties.get('boolean_property', True)
82 self.binary_path = properties.get('binary_path', 'zip')
83 self.properties = properties
85 # Check the properties
86 self.check_properties(properties)
87 # Check the arguments
88 self.check_arguments()
90 @launchlogger
91 def launch(self) -> int:
92 """Execute the :class:`TemplateContainer <template.template_container.TemplateContainer>` object."""
94 # 4. Setup Biobb
95 if self.check_restart():
96 return 0
97 self.stage_files()
99 # Creating temporary folder
100 self.tmp_folder = fu.create_unique_dir()
101 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log)
103 # 5. Prepare the command line parameters as instructions list
104 instructions = ['-j']
105 if self.boolean_property:
106 instructions.append('-v')
107 fu.log('Appending optional boolean property', self.out_log, self.global_log)
109 # 6. Build the actual command line as a list of items (elements order will be maintained)
110 self.cmd = [self.binary_path,
111 ' '.join(instructions),
112 self.stage_io_dict['out']['output_file_path'],
113 self.stage_io_dict['in']['input_file_path1']]
114 fu.log('Creating command line with instructions and required arguments', self.out_log, self.global_log)
116 # 7. Repeat for optional input files if provided
117 if self.stage_io_dict['in']['input_file_path2']:
118 # Append optional input_file_path2 to cmd
119 self.cmd.append(self.stage_io_dict['in']['input_file_path2'])
120 fu.log('Appending optional argument to command line', self.out_log, self.global_log)
122 # 8. Uncomment to check the command line
123 # print(' '.join(cmd))
125 # Run Biobb block
126 self.run_biobb()
128 # Copy files to host
129 self.copy_to_host()
131 # Remove temporary file(s)
132 self.tmp_files.extend([
133 self.stage_io_dict.get("unique_dir"),
134 self.tmp_folder
135 ])
136 self.remove_tmp_files()
138 # Check output arguments
139 self.check_arguments(output_files_created=True, raise_exception=False)
141 return self.return_code
144def template_container(input_file_path1: str, output_file_path: str, input_file_path2: str = None, properties: dict = None, **kwargs) -> int:
145 """Create :class:`TemplateContainer <template.template_container.TemplateContainer>` class and
146 execute the :meth:`launch() <template.template_container.TemplateContainer.launch>` method."""
148 return TemplateContainer(input_file_path1=input_file_path1,
149 output_file_path=output_file_path,
150 input_file_path2=input_file_path2,
151 properties=properties, **kwargs).launch()
154def main():
155 """Command line execution of this building block. Please check the command line documentation."""
156 parser = argparse.ArgumentParser(description='Description for the template container module.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
157 parser.add_argument('--config', required=False, help='Configuration file')
159 # 10. Include specific args of each building block following the examples. They should match step 2
160 required_args = parser.add_argument_group('required arguments')
161 required_args.add_argument('--input_file_path1', required=True, help='Description for the first input file path. Accepted formats: top.')
162 parser.add_argument('--input_file_path2', required=False, help='Description for the second input file path (optional). Accepted formats: dcd.')
163 required_args.add_argument('--output_file_path', required=True, help='Description for the output file path. Accepted formats: zip.')
165 args = parser.parse_args()
166 args.config = args.config or "{}"
167 properties = settings.ConfReader(config=args.config).get_prop_dic()
169 # 11. Adapt to match Class constructor (step 2)
170 # Specific call of each building block
171 template_container(input_file_path1=args.input_file_path1,
172 output_file_path=args.output_file_path,
173 input_file_path2=args.input_file_path2,
174 properties=properties)
177if __name__ == '__main__':
178 main()
180# 13. Complete documentation strings