comparison toolfactory/galaxyxml/tool/import_xml.py @ 35:5d38cb3d9be8 draft

added patched galaxyxml code temporarily until PR accepted
author fubar
date Sat, 08 Aug 2020 19:55:55 -0400
parents
children ce2b1f8ea68d
comparison
equal deleted inserted replaced
34:5052ac89c036 35:5d38cb3d9be8
1 import logging
2 import xml.etree.ElementTree as ET
3 import galaxyxml.tool as gxt
4 import galaxyxml.tool.parameters as gxtp
5
6 logging.basicConfig(level=logging.INFO)
7 logger = logging.getLogger(__name__)
8
9
10 class GalaxyXmlParser(object):
11 """
12 Class to import content from an existing Galaxy XML wrapper.
13 """
14
15 def _init_tool(self, xml_root):
16 """
17 Init tool from existing xml tool.
18
19 :param xml_root: root of the galaxy xml file.
20 :type xml_root: :class:`xml.etree._Element`
21 """
22 version_cmd = None
23 description = None
24 for child in xml_root:
25 if child.tag == 'description':
26 description = child.text
27 elif child.tag == 'command':
28 executable = child.text.split()[0]
29 command = child.text
30 elif child.tag == 'version_command':
31 version_cmd = child.text
32
33 tool = gxt.Tool(xml_root.attrib['name'],
34 xml_root.attrib['id'],
35 xml_root.attrib.get('version', None),
36 description,
37 executable,
38 hidden=xml_root.attrib.get('hidden', False),
39 tool_type=xml_root.attrib.get('tool_type', None),
40 URL_method=xml_root.attrib.get('URL_method', None),
41 workflow_compatible=xml_root.attrib.get('workflow_compatible', True),
42 version_command=version_cmd)
43 tool.command = command
44 return tool
45
46 def _load_description(self, tool, desc_root):
47 """
48 <description> is already loaded during initiation.
49
50 :param tool: Tool object from galaxyxml.
51 :type tool: :class:`galaxyxml.tool.Tool`
52 :param desc_root: root of <description> tag.
53 :type desc_root: :class:`xml.etree._Element`
54 """
55 logger.info("<description> is loaded during initiation of the object.")
56
57 def _load_version_command(self, tool, vers_root):
58 """
59 <version_command> is already loaded during initiation.
60
61 :param tool: Tool object from galaxyxml.
62 :type tool: :class:`galaxyxml.tool.Tool`
63 :param vers_root: root of <version_command> tag.
64 :type vers_root: :class:`xml.etree._Element`
65 """
66 logger.info("<version_command> is loaded during initiation of the object.")
67
68 def _load_stdio(self, tool, stdio_root):
69 """
70 So far, <stdio> is automatically generated by galaxyxml.
71
72 :param tool: Tool object from galaxyxml.
73 :type tool: :class:`galaxyxml.tool.Tool`
74 :param desc_root: root of <stdio> tag.
75 :type desc_root: :class:`xml.etree._Element`
76 """
77 logger.info("<stdio> is not loaded but automatically generated by galaxyxml.")
78
79 def _load_command(self, tool, desc_root):
80 """
81 <command> is already loaded during initiation.
82
83 :param tool: Tool object from galaxyxml.
84 :type tool: :class:`galaxyxml.tool.Tool`
85 :param desc_root: root of <command> tag.
86 :type desc_root: :class:`xml.etree._Element`
87 """
88 logger.info("<command> is loaded during initiation of the object.")
89
90 def _load_help(self, tool, help_root):
91 """
92 Load the content of the <help> into the tool.
93
94 :param tool: Tool object from galaxyxml.
95 :type tool: :class:`galaxyxml.tool.Tool`
96 :param requirements_root: root of <help> tag.
97 :type requirements_root: :class:`xml.etree._Element`
98 """
99 tool.help = help_root.text
100
101 def _load_requirements(self, tool, requirements_root):
102 """
103 Add <requirements> to the tool.
104
105 :param tool: Tool object from galaxyxml.
106 :type tool: :class:`galaxyxml.tool.Tool`
107 :param requirements_root: root of <requirements> tag.
108 :type requirements_root: :class:`xml.etree._Element`
109 """
110 tool.requirements = gxtp.Requirements()
111 for req in requirements_root:
112 req_type = req.attrib['type']
113 value = req.text
114 if req.tag == 'requirement':
115 version = req.attrib.get('version', None)
116 tool.requirements.append(gxtp.Requirement(req_type, value, version=version))
117 elif req.tag == 'container':
118 tool.requirements.append(gxtp.Container(req_type, value))
119 else:
120 logger.warning(req.tag + ' is not a valid tag for requirements child')
121
122 def _load_edam_topics(self, tool, topics_root):
123 """
124 Add <edam_topics> to the tool.
125
126 :param tool: Tool object from galaxyxml.
127 :type tool: :class:`galaxyxml.tool.Tool`
128 :param topics_root: root of <edam_topics> tag.
129 :type topics_root: :class:`xml.etree._Element`
130 """
131 tool.edam_topics = gxtp.EdamTopics()
132 for edam_topic in topics_root:
133 tool.edam_topics.append(gxtp.EdamTopic(edam_topic.text))
134
135 def _load_edam_operations(self, tool, operations_root):
136 """
137 Add <edam_operations> to the tool.
138
139 :param tool: Tool object from galaxyxml.
140 :type tool: :class:`galaxyxml.tool.Tool`
141 :param operations_root: root of <edam_operations> tag.
142 :type operations_root: :class:`xml.etree._Element`
143 """
144 tool.edam_operations = gxtp.EdamOperations()
145 for edam_op in operations_root:
146 tool.edam_operations.append(gxtp.EdamOperation(edam_op.text))
147
148 def _load_configfiles(self, tool, configfiles_root):
149 """
150 Add <configfiles> to the tool.
151
152 :param tool: Tool object from galaxyxml.
153 :type tool: :class:`galaxyxml.tool.Tool`
154 :param configfiles_root: root of <configfiles> tag.
155 :type configfiles_root: :class:`xml.etree._Element`
156 """
157 tool.configfiles = gxtp.Configfiles()
158 for conf in configfiles_root:
159 name = conf.attrib['name']
160 value = conf.text
161 tool.configfiles.append(gxtp.Configfile(name, value))
162
163 def _load_citations(self, tool, citations_root):
164 """
165 Add <citations> to the tool.
166
167 :param tool: Tool object from galaxyxml.
168 :type tool: :class:`galaxyxml.tool.Tool`
169 :param citations_root: root of <citations> tag.
170 :type citations_root: :class:`xml.etree._Element`
171 """
172 tool.citations = gxtp.Citations()
173 for cit in citations_root:
174 cit_type = cit.attrib['type']
175 value = cit.text
176 tool.citations.append(gxtp.Citation(cit_type, value))
177
178 def _load_inputs(self, tool, inputs_root):
179 """
180 Add <inputs> to the tool using the :class:`galaxyxml.tool.import_xml.InputsParser` object.
181
182 :param tool: Tool object from galaxyxml.
183 :type tool: :class:`galaxyxml.tool.Tool`
184 :param inputs_root: root of <inputs> tag.
185 :type inputs_root: :class:`xml.etree._Element`
186 """
187 tool.inputs = gxtp.Inputs()
188 inp_parser = InputsParser()
189 inp_parser.load_inputs(tool.inputs, inputs_root)
190
191 def _load_outputs(self, tool, outputs_root):
192 """
193 Add <outputs> to the tool using the :class:`galaxyxml.tool.import_xml.OutputsParser` object.
194
195 :param tool: Tool object from galaxyxml.
196 :type tool: :class:`galaxyxml.tool.Tool`
197 :param outputs_root: root of <outputs> tag.
198 :type outputs_root: :class:`xml.etree._Element`
199 """
200 tool.outputs = gxtp.Outputs()
201 out_parser = OutputsParser()
202 out_parser.load_outputs(tool.outputs, outputs_root)
203
204 def _load_tests(self, tool, tests_root):
205 """
206 Add <tests> to the tool using the :class:`galaxyxml.tool.import_xml.TestsParser` object.
207
208 :param tool: Tool object from galaxyxml.
209 :type tool: :class:`galaxyxml.tool.Tool`
210 :param tests_root: root of <tests> tag.
211 :type tests_root: :class:`xml.etree._Element`
212 """
213 tool.tests = gxtp.Tests()
214 tests_parser = TestsParser()
215 tests_parser.load_tests(tool.tests, tests_root)
216
217 def import_xml(self, xml_path):
218 """
219 Load existing xml into the :class:`galaxyxml.tool.Tool` object.
220
221 :param xml_path: Path of the XML to be loaded.
222 :type xml_path: STRING
223 :return: XML content in the galaxyxml model.
224 :rtype: :class:`galaxyxml.tool.Tool`
225 """
226 xml_root = ET.parse(xml_path).getroot()
227 tool = self._init_tool(xml_root)
228 # Now we import each tag's field
229 for child in xml_root:
230 try:
231 getattr(self, '_load_{}'.format(child.tag))(tool, child)
232 except AttributeError:
233 logger.warning(child.tag + " tag is not processed.")
234 return tool
235
236
237 class InputsParser(object):
238 """
239 Class to parse content of the <inputs> tag from a Galaxy XML wrapper.
240 """
241
242 def _load_text_param(self, root, text_param):
243 """
244 Add <param type="text" /> to the root.
245
246 :param root: root to append the param to.
247 :param text_param: root of <param> tag.
248 :type text_param: :class:`xml.etree._Element`
249 """
250 root.append(gxtp.TextParam(text_param.attrib['name'],
251 optional=text_param.get('optional', None),
252 label=text_param.get('label', None),
253 help=text_param.get('help', None),
254 value=text_param.get('value', None)))
255
256 def _load_data_param(self, root, data_param):
257 """
258 Add <param type="data" /> to the root.
259
260 :param root: root to append the param to.
261 :param data_param: root of <param> tag.
262 :type data_param: :class:`xml.etree._Element`
263 """
264 root.append(gxtp.DataParam(data_param.attrib['name'],
265 optional=data_param.attrib.get('optional', None),
266 label=data_param.attrib.get('label', None),
267 help=data_param.attrib.get('help', None),
268 format=data_param.attrib.get('format', None),
269 multiple=data_param.attrib.get('multiple', None)))
270
271 def _load_boolean_param(self, root, bool_param):
272 """
273 Add <param type="boolean" /> to the root.
274
275 :param root: root to append the param to.
276 :param bool_param: root of <param> tag.
277 :type bool_param: :class:`xml.etree._Element`
278 """
279 root.append(gxtp.BooleanParam(bool_param.attrib['name'],
280 optional=bool_param.attrib.get('optional', None),
281 label=bool_param.attrib.get('label', None),
282 help=bool_param.attrib.get('help', None),
283 checked=bool_param.attrib.get('checked', False),
284 truevalue=bool_param.attrib.get('truevalue', None),
285 falsevalue=bool_param.attrib.get('falsevalue', None)))
286
287 def _load_integer_param(self, root, int_param):
288 """
289 Add <param type="integer" /> to the root.
290
291 :param root: root to append the param to.
292 :param int_param: root of <param> tag.
293 :type int_param: :class:`xml.etree._Element`
294 """
295 root.append(gxtp.IntegerParam(int_param.attrib['name'],
296 int_param.attrib.get('value', None),
297 optional=int_param.attrib.get('optional', None),
298 label=int_param.attrib.get('label', None),
299 help=int_param.attrib.get('help', None),
300 min=int_param.attrib.get('min', None),
301 max=int_param.attrib.get('max', None)))
302
303 def _load_float_param(self, root, float_param):
304 """
305 Add <param type="float" /> to the root.
306
307 :param root: root to append the param to.
308 :param float_param: root of <param> tag.
309 :type float_param: :class:`xml.etree._Element`
310 """
311 root.append(gxtp.FloatParam(float_param.attrib['name'],
312 float_param.attrib.get('value', None),
313 optional=float_param.attrib.get('optional', None),
314 label=float_param.attrib.get('label', None),
315 help=float_param.attrib.get('help', None),
316 min=float_param.attrib.get('min', None),
317 max=float_param.attrib.get('max', None)))
318
319 def _load_option_select(self, root, option):
320 """
321 Add <option> to the root (usually <param type="select" />).
322
323 :param root: root to append the param to.
324 :param option: root of <option> tag.
325 :type float_param: :class:`xml.etree._Element`
326 """
327 root.append(gxtp.SelectOption(option.attrib.get('value', None),
328 option.text,
329 selected=option.attrib.get('selected', False)))
330
331 def _load_column_options(self, root, column):
332 """
333 Add <column> to the root (usually <options>).
334
335 :param root: root to append the param to.
336 :param option: root of <column> tag.
337 :type float_param: :class:`xml.etree._Element`
338 """
339 root.append(gxtp.Column(column.attrib['name'], column.attrib['index']))
340
341 def _load_filter_options(self, root, filter):
342 """
343 Add <filter> to the root (usually <options>).
344
345 :param root: root to append the param to.
346 :param option: root of <filter> tag.
347 :type float_param: :class:`xml.etree._Element`
348 """
349 root.append(gxtp.Filter(filter.attrib['type'],
350 column=filter.attrib.get('column', None),
351 name=filter.attrib.get('name', None),
352 ref=filter.attrib.get('ref', None),
353 key=filter.attrib.get('key', None),
354 multiple=filter.attrib.get('multiple', None),
355 separator=filter.attrib.get('separator', None),
356 keep=filter.attrib.get('keep', None),
357 value=filter.attrib.get('value', None),
358 ref_attribute=filter.attrib.get('ref_attribute', None),
359 index=filter.attrib.get('index', None)))
360
361 def _load_options_select(self, root, options):
362 """
363 Add <options> to the root (usually <param type="select" />).
364
365 :param root: root to append the param to.
366 :param option: root of <options> tag.
367 :type float_param: :class:`xml.etree._Element`
368 """
369 opts = gxtp.Options(from_dataset=options.attrib.get('from_dataset', None),
370 from_file=options.attrib.get('from_file', None),
371 from_data_table=options.attrib.get('from_data_table', None),
372 from_parameter=options.attrib.get('from_parameter', None))
373 # Deal with child nodes (usually filter and column)
374 for opt_child in options:
375 try:
376 getattr(self, '_load_{}_options'.format(opt_child.tag))(opts, opt_child)
377 except AttributeError:
378 logger.warning(opt_child.tag + " tag is not processed for <options>.")
379 root.append(opts)
380
381 def _load_select_param(self, root, sel_param):
382 """
383 Add <param type="select" /> to the root.
384
385 :param root: root to append the param to.
386 :param sel_param: root of <param> tag.
387 :type sel_param: :class:`xml.etree._Element`
388 """
389 select_param = gxtp.SelectParam(sel_param.attrib['name'],
390 optional=sel_param.attrib.get('optional', None),
391 label=sel_param.attrib.get('label', None),
392 help=sel_param.attrib.get('help', None),
393 data_ref=sel_param.attrib.get('data_ref', None),
394 display=sel_param.attrib.get('display', None),
395 multiple=sel_param.attrib.get('multiple', None))
396 # Deal with child nodes (usually option and options)
397 for sel_child in sel_param:
398 try:
399 getattr(self, '_load_{}_select'.format(sel_child.tag))(select_param, sel_child)
400 except AttributeError:
401 logger.warning(sel_child.tag + " tag is not processed for <param type='select'>.")
402 root.append(select_param)
403
404 def _load_param(self, root, param_root):
405 """
406 Method to select which type of <param> is being added to the root.
407
408 :param root: root to attach param to.
409 :param param_root: root of <param> tag.
410 :type param_root: :class:`xml.etree._Element`
411 """
412 param_type = param_root.attrib['type']
413 try:
414 getattr(self, '_load_{}_param'.format(param_type))(root, param_root)
415 except AttributeError:
416 logger.warning(param_type + " tag is not processed for <param>.")
417
418 def _load_when(self, root, when_root):
419 """
420 Add <when> to the root (usually <conditional>).
421
422 :param root: root to append when to.
423 :param when_root: root of <when> tag.
424 :type when_root: :class:`xml.etree._Element`
425 """
426 when = gxtp.When(when_root.attrib['value'])
427 # Deal with child nodes
428 self.load_inputs(when, when_root)
429 root.append(when)
430
431 def _load_conditional(self, root, conditional_root):
432 """
433 Add <conditional> to the root.
434
435 :param root: root to append conditional to.
436 :param conditional_root: root of <conditional> tag.
437 :type conditional_root: :class:`xml.etree._Element`
438 """
439 value_ref_in_group = conditional_root.attrib.get('value_ref_in_group', None)
440 # Other optional parameters need to be added to conditional object
441 conditional = gxtp.Conditional(conditional_root.attrib['name'],
442 value_from=conditional_root.attrib.get('value_from', None),
443 value_ref=conditional_root.attrib.get('value_ref', None),
444 value_ref_in_group=value_ref_in_group,
445 label=conditional_root.attrib.get('label', None))
446 # Deal with child nodes
447 self.load_inputs(conditional, conditional_root)
448 root.append(conditional)
449
450 def _load_section(self, root, section_root):
451 """
452 Add <section> to the root.
453
454 :param root: root to append conditional to.
455 :param section_root: root of <section> tag.
456 :type section_root: :class:`xml.etree._Element`
457 """
458 section = gxtp.Section(section_root.attrib['name'],
459 section_root.attrib['title'],
460 expanded=section_root.attrib.get('expanded', None),
461 help=section_root.attrib.get('help', None))
462 # Deal with child nodes
463 self.load_inputs(section, section_root)
464 root.append(section)
465
466 def _load_repeat(self, root, repeat_root):
467 """
468 Add <repeat> to the root.
469
470 :param root: root to append repeat to.
471 :param repeat_root: root of <repeat> tag.
472 :param repeat_root: :class:`xml.etree._Element`
473 """
474 repeat = gxtp.Repeat(repeat_root.attrib['name'],
475 repeat_root.attrib['title'],
476 min=repeat_root.attrib.get('min', None),
477 max=repeat_root.attrib.get('max', None),
478 default=repeat_root.attrib.get('default', None))
479 # Deal with child nodes
480 self.load_inputs(repeat, repeat_root)
481 root.append(repeat)
482
483 def load_inputs(self, root, inputs_root):
484 """
485 Add <inputs.tag> to the root (it can be any tags with children such as
486 <inputs>, <repeat>, <section> ...)
487
488 :param root: root to attach inputs to (either <inputs> or <when>).
489 :param inputs_root: root of <inputs> tag.
490 :type inputs_root: :class:`xml.etree._Element`
491 """
492 for inp_child in inputs_root:
493 try:
494 getattr(self, '_load_{}'.format(inp_child.tag))(root, inp_child)
495 except AttributeError:
496 logger.warning(inp_child.tag + " tag is not processed for <" +
497 inputs_root.tag + "> tag.")
498
499
500 class OutputsParser(object):
501 """
502 Class to parse content of the <outputs> tag from a Galaxy XML wrapper.
503 """
504
505 def _load_data(self, outputs_root, data_root):
506 """
507 Add <data> to <outputs>.
508
509 :param outputs_root: <outputs> root to append <data> to.
510 :param data_root: root of <data> tag.
511 :param data_root: :class:`xml.etree._Element`
512 """
513 data = gxtp.OutputData(data_root.attrib.get('name', None),
514 data_root.attrib.get('format', None),
515 format_source=data_root.attrib.get('format_source', None),
516 metadata_source=data_root.attrib.get('metadata_source', None),
517 label=data_root.attrib.get('label', None),
518 from_work_dir=data_root.attrib.get('from_work_dir', None),
519 hidden=data_root.attrib.get('hidden', False))
520 # Deal with child nodes
521 for data_child in data_root:
522 try:
523 getattr(self, '_load_{}'.format(data_child.tag))(data, data_child)
524 except AttributeError:
525 logger.warning(data_child.tag + " tag is not processed for <data>.")
526 outputs_root.append(data)
527
528 def _load_change_format(self, root, chfmt_root):
529 """
530 Add <change_format> to root (<data>).
531
532 :param root: root to append <change_format> to.
533 :param chfm_root: root of <change_format> tag.
534 :param chfm_root: :class:`xml.etree._Element`
535 """
536 change_format = gxtp.ChangeFormat()
537 for chfmt_child in chfmt_root:
538 change_format.append(gxtp.ChangeFormatWhen(chfmt_child.attrib['input'],
539 chfmt_child.attrib['format'],
540 chfmt_child.attrib['value']))
541 root.append(change_format)
542
543 def _load_collection(self, outputs_root, coll_root):
544 """
545 Add <collection> to <outputs>.
546
547 :param outputs_root: <outputs> root to append <collection> to.
548 :param coll_root: root of <collection> tag.
549 :param coll_root: :class:`xml.etree._Element`
550 """
551 collection = gxtp.OutputCollection(coll_root.attrib['name'],
552 type=coll_root.attrib.get('type', None),
553 label=coll_root.attrib.get('label', None),
554 format_source=coll_root.attrib.get('format_source',
555 None),
556 type_source=coll_root.attrib.get('type_source', None),
557 structured_like=coll_root.attrib.get('structured_like',
558 None),
559 inherit_format=coll_root.attrib.get('inherit_format',
560 None))
561 # Deal with child nodes
562 for coll_child in coll_root:
563 try:
564 getattr(self, '_load_{}'.format(coll_child.tag))(collection, coll_child)
565 except AttributeError:
566 logger.warning(coll_child.tag + " tag is not processed for <collection>.")
567 outputs_root.append(collection)
568
569 def _load_discover_datasets(self, root, disc_root):
570 """
571 Add <discover_datasets> to root (<collection>).
572
573 :param root: root to append <collection> to.
574 :param disc_root: root of <discover_datasets> tag.
575 :param disc_root: :class:`xml.etree._Element`
576 """
577 root.append(gxtp.DiscoverDatasets(disc_root.attrib['pattern'],
578 directory=disc_root.attrib.get('directory', None),
579 format=disc_root.attrib.get('format', None),
580 ext=disc_root.attrib.get('ext', None),
581 visible=disc_root.attrib.get('visible', None)))
582
583 def _load_filter(self, root, filter_root):
584 """
585 Add <filter> to root (<collection> or <data>).
586
587 :param root: root to append <collection> to.
588 :param coll_root: root of <filter> tag.
589 :param coll_root: :class:`xml.etree._Element`
590 """
591 root.append(gxtp.OutputFilter(filter_root.text))
592
593 def load_outputs(self, root, outputs_root):
594 """
595 Add <outputs> to the root.
596
597 :param root: root to attach <outputs> to (<tool>).
598 :param tests_root: root of <outputs> tag.
599 :type tests_root: :class:`xml.etree._Element`
600 """
601 for out_child in outputs_root:
602 try:
603 getattr(self, '_load_{}'.format(out_child.tag))(root, out_child)
604 except AttributeError:
605 logger.warning(out_child.tag + " tag is not processed for <outputs>.")
606
607
608 class TestsParser(object):
609 """
610 Class to parse content of the <tests> tag from a Galaxy XML wrapper.
611 """
612
613 def _load_param(self, test_root, param_root):
614 """
615 Add <param> to the <test>.
616
617 :param root: <test> root to append <param> to.
618 :param repeat_root: root of <param> tag.
619 :param repeat_root: :class:`xml.etree._Element`
620 """
621 test_root.append(gxtp.TestParam(param_root.attrib['name'],
622 value=param_root.attrib.get('value', None),
623 ftype=param_root.attrib.get('ftype', None),
624 dbkey=param_root.attrib.get('dbkey', None)))
625
626 def _load_output(self, test_root, output_root):
627 """
628 Add <output> to the <test>.
629
630 :param root: <test> root to append <output> to.
631 :param repeat_root: root of <output> tag.
632 :param repeat_root: :class:`xml.etree._Element`
633 """
634 test_root.append(gxtp.TestOutput(name=output_root.attrib.get('name', None),
635 file=output_root.attrib.get('file', None),
636 ftype=output_root.attrib.get('ftype', None),
637 sort=output_root.attrib.get('sort', None),
638 value=output_root.attrib.get('value', None),
639 md5=output_root.attrib.get('md5', None),
640 checksum=output_root.attrib.get('checksum', None),
641 compare=output_root.attrib.get('compare', None),
642 lines_diff=output_root.attrib.get('lines_diff', None),
643 delta=output_root.attrib.get('delta', None)))
644
645 def load_tests(self, root, tests_root):
646 """
647 Add <tests> to the root.
648
649 :param root: root to attach <tests> to (<tool>).
650 :param tests_root: root of <tests> tag.
651 :type tests_root: :class:`xml.etree._Element`
652 """
653 for test_root in tests_root:
654 test = gxtp.Test()
655 for test_child in test_root:
656 try:
657 getattr(self, '_load_{}'.format(test_child.tag))(test, test_child)
658 except AttributeError:
659 logger.warning(test_child.tag + " tag is not processed within <test>.")
660 root.append(test)