Mercurial > repos > rv43 > tomo
comparison workflow/__main__.py @ 69:fba792d5f83b draft
planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
author | rv43 |
---|---|
date | Fri, 10 Mar 2023 16:02:04 +0000 |
parents | |
children | 1cf15b61cd83 |
comparison
equal
deleted
inserted
replaced
68:ba5866d0251d | 69:fba792d5f83b |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 import logging | |
4 logging.getLogger(__name__) | |
5 | |
6 import argparse | |
7 import pathlib | |
8 import sys | |
9 | |
10 from .models import TomoWorkflow as Workflow | |
11 try: | |
12 from deepdiff import DeepDiff | |
13 except: | |
14 pass | |
15 | |
16 parser = argparse.ArgumentParser(description='''Operate on representations of | |
17 Tomo data workflows saved to files.''') | |
18 parser.add_argument('-l', '--log', | |
19 # type=argparse.FileType('w'), | |
20 default=sys.stdout, | |
21 help='Logging stream or filename') | |
22 parser.add_argument('--log_level', | |
23 choices=logging._nameToLevel.keys(), | |
24 default='INFO', | |
25 help='''Specify a preferred logging level.''') | |
26 subparsers = parser.add_subparsers(title='subcommands', required=True)#, dest='command') | |
27 | |
28 | |
29 # CONSTRUCT | |
30 def construct(args:list) -> None: | |
31 if args.template_file is not None: | |
32 wf = Workflow.construct_from_file(args.template_file) | |
33 wf.cli() | |
34 else: | |
35 wf = Workflow.construct_from_cli() | |
36 wf.write_to_file(args.output_file, force_overwrite=args.force_overwrite) | |
37 | |
38 construct_parser = subparsers.add_parser('construct', help='''Construct a valid Tomo | |
39 workflow representation on the command line and save it to a file. Optionally use | |
40 an existing file as a template and/or preform the reconstruction or transfer to Galaxy.''') | |
41 construct_parser.set_defaults(func=construct) | |
42 construct_parser.add_argument('-t', '--template_file', | |
43 type=pathlib.Path, | |
44 required=False, | |
45 help='''Full or relative template file path for the constructed workflow.''') | |
46 construct_parser.add_argument('-f', '--force_overwrite', | |
47 action='store_true', | |
48 help='''Use this flag to overwrite the output file if it already exists.''') | |
49 construct_parser.add_argument('-o', '--output_file', | |
50 type=pathlib.Path, | |
51 help='''Full or relative file path to which the constructed workflow will be written.''') | |
52 | |
53 | |
54 # VALIDATE | |
55 def validate(args:list) -> bool: | |
56 try: | |
57 wf = Workflow.construct_from_file(args.input_file) | |
58 logger.info(f'Success: {args.input_file} represents a valid Tomo workflow configuration.') | |
59 return(True) | |
60 except BaseException as e: | |
61 logger.error(f'{e.__class__.__name__}: {str(e)}') | |
62 logger.info(f'''Failure: {args.input_file} does not represent a valid Tomo workflow | |
63 configuration.''') | |
64 return(False) | |
65 | |
66 validate_parser = subparsers.add_parser('validate', | |
67 help='''Validate a file as a representation of a Tomo workflow (this is most useful | |
68 after a .yaml file has been manually edited).''') | |
69 validate_parser.set_defaults(func=validate) | |
70 validate_parser.add_argument('input_file', | |
71 type=pathlib.Path, | |
72 help='''Full or relative file path to validate as a Tomo workflow.''') | |
73 | |
74 | |
75 # CONVERT | |
76 def convert(args:list) -> None: | |
77 wf = Workflow.construct_from_file(args.input_file) | |
78 wf.write_to_file(args.output_file, force_overwrite=args.force_overwrite) | |
79 | |
80 convert_parser = subparsers.add_parser('convert', help='''Convert one Tomo workflow | |
81 representation to another. File format of both input and output files will be | |
82 automatically determined from the files' extensions.''') | |
83 convert_parser.set_defaults(func=convert) | |
84 convert_parser.add_argument('-f', '--force_overwrite', | |
85 action='store_true', | |
86 help='''Use this flag to overwrite the output file if it already exists.''') | |
87 convert_parser.add_argument('-i', '--input_file', | |
88 type=pathlib.Path, | |
89 required=True, | |
90 help='''Full or relative input file path to be converted.''') | |
91 convert_parser.add_argument('-o', '--output_file', | |
92 type=pathlib.Path, | |
93 required=True, | |
94 help='''Full or relative file path to which the converted input will be written.''') | |
95 | |
96 | |
97 # DIFF / COMPARE | |
98 def diff(args:list) -> bool: | |
99 raise ValueError('diff not tested') | |
100 # wf1 = Workflow.construct_from_file(args.file1).dict_for_yaml() | |
101 # wf2 = Workflow.construct_from_file(args.file2).dict_for_yaml() | |
102 # diff = DeepDiff(wf1,wf2, | |
103 # ignore_order_func=lambda level:'independent_dimensions' not in level.path(), | |
104 # report_repetition=True, | |
105 # ignore_string_type_changes=True, | |
106 # ignore_numeric_type_changes=True) | |
107 diff_report = diff.pretty() | |
108 if len(diff_report) > 0: | |
109 logger.info(f'The configurations in {args.file1} and {args.file2} are not identical.') | |
110 print(diff_report) | |
111 return(True) | |
112 else: | |
113 logger.info(f'The configurations in {args.file1} and {args.file2} are identical.') | |
114 return(False) | |
115 | |
116 diff_parser = subparsers.add_parser('diff', aliases=['compare'], help='''Print a comparison of | |
117 two Tomo workflow representations stored in files. The files may have different formats.''') | |
118 diff_parser.set_defaults(func=diff) | |
119 diff_parser.add_argument('file1', | |
120 type=pathlib.Path, | |
121 help='''Full or relative path to the first file for comparison.''') | |
122 diff_parser.add_argument('file2', | |
123 type=pathlib.Path, | |
124 help='''Full or relative path to the second file for comparison.''') | |
125 | |
126 | |
127 # LINK TO GALAXY | |
128 def link_to_galaxy(args:list) -> None: | |
129 from .link_to_galaxy import link_to_galaxy | |
130 link_to_galaxy(args.input_file, galaxy=args.galaxy, user=args.user, | |
131 password=args.password, api_key=args.api_key) | |
132 | |
133 link_parser = subparsers.add_parser('link_to_galaxy', help='''Construct a Galaxy history and link | |
134 to an existing Tomo workflow representations in a NeXus file.''') | |
135 link_parser.set_defaults(func=link_to_galaxy) | |
136 link_parser.add_argument('-i', '--input_file', | |
137 type=pathlib.Path, | |
138 required=True, | |
139 help='''Full or relative input file path to the existing Tomo workflow representations as | |
140 a NeXus file.''') | |
141 link_parser.add_argument('-g', '--galaxy', | |
142 required=True, | |
143 help='Target Galaxy instance URL/IP address') | |
144 link_parser.add_argument('-u', '--user', | |
145 default=None, | |
146 help='Galaxy user email address') | |
147 link_parser.add_argument('-p', '--password', | |
148 default=None, | |
149 help='Password for the Galaxy user') | |
150 link_parser.add_argument('-a', '--api_key', | |
151 default=None, | |
152 help='Galaxy admin user API key (required if not defined in the tools list file)') | |
153 | |
154 | |
155 # RUN THE RECONSTRUCTION | |
156 def run_tomo(args:list) -> None: | |
157 from .run_tomo import run_tomo | |
158 run_tomo(args.input_file, args.output_file, args.modes, center_file=args.center_file, | |
159 num_core=args.num_core, output_folder=args.output_folder, save_figs=args.save_figs) | |
160 | |
161 tomo_parser = subparsers.add_parser('run_tomo', help='''Construct and add reconstructed tomography | |
162 data to an existing Tomo workflow representations in a NeXus file.''') | |
163 tomo_parser.set_defaults(func=run_tomo) | |
164 tomo_parser.add_argument('-i', '--input_file', | |
165 required=True, | |
166 type=pathlib.Path, | |
167 help='''Full or relative input file path containing raw and/or reduced data.''') | |
168 tomo_parser.add_argument('-o', '--output_file', | |
169 required=True, | |
170 type=pathlib.Path, | |
171 help='''Full or relative input file path containing raw and/or reduced data.''') | |
172 tomo_parser.add_argument('-c', '--center_file', | |
173 type=pathlib.Path, | |
174 help='''Full or relative input file path containing the rotation axis centers info.''') | |
175 #tomo_parser.add_argument('-f', '--force_overwrite', | |
176 # action='store_true', | |
177 # help='''Use this flag to overwrite any existing reduced data.''') | |
178 tomo_parser.add_argument('-n', '--num_core', | |
179 type=int, | |
180 default=-1, | |
181 help='''Specify the number of processors to use.''') | |
182 tomo_parser.add_argument('--output_folder', | |
183 type=pathlib.Path, | |
184 default='.', | |
185 help='Full or relative path to an output folder') | |
186 tomo_parser.add_argument('-s', '--save_figs', | |
187 choices=['yes', 'no', 'only'], | |
188 default='no', | |
189 help='''Specify weather to display ('yes' or 'no'), save ('yes'), or only save ('only').''') | |
190 tomo_parser.add_argument('--reduce_data', | |
191 dest='modes', | |
192 const='reduce_data', | |
193 action='append_const', | |
194 help='''Use this flag to create and add reduced data to the input file.''') | |
195 tomo_parser.add_argument('--find_center', | |
196 dest='modes', | |
197 const='find_center', | |
198 action='append_const', | |
199 help='''Use this flag to find and add the calibrated center axis info to the input file.''') | |
200 tomo_parser.add_argument('--reconstruct_data', | |
201 dest='modes', | |
202 const='reconstruct_data', | |
203 action='append_const', | |
204 help='''Use this flag to create and add reconstructed data data to the input file.''') | |
205 tomo_parser.add_argument('--combine_datas', | |
206 dest='modes', | |
207 const='combine_datas', | |
208 action='append_const', | |
209 help='''Use this flag to combine reconstructed data data and add to the input file.''') | |
210 | |
211 | |
212 if __name__ == '__main__': | |
213 args = parser.parse_args(sys.argv[1:]) | |
214 | |
215 # Set log configuration | |
216 # When logging to file, the stdout log level defaults to WARNING | |
217 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | |
218 level = logging.getLevelName(args.log_level) | |
219 if args.log is sys.stdout: | |
220 logging.basicConfig(format=logging_format, level=level, force=True, | |
221 handlers=[logging.StreamHandler()]) | |
222 else: | |
223 if isinstance(args.log, str): | |
224 logging.basicConfig(filename=f'{args.log}', filemode='w', | |
225 format=logging_format, level=level, force=True) | |
226 elif isinstance(args.log, io.TextIOWrapper): | |
227 logging.basicConfig(filemode='w', format=logging_format, level=level, | |
228 stream=args.log, force=True) | |
229 else: | |
230 raise ValueError(f'Invalid argument --log: {args.log}') | |
231 stream_handler = logging.StreamHandler() | |
232 logging.getLogger().addHandler(stream_handler) | |
233 stream_handler.setLevel(logging.WARNING) | |
234 stream_handler.setFormatter(logging.Formatter(logging_format)) | |
235 | |
236 args.func(args) |