|
0
|
1 #!/usr/bin/env python
|
|
|
2 """Conversion of the CTD format into Galaxy XML.
|
|
|
3
|
|
|
4 The CTD parser should be reusable but is not in its own module since it is
|
|
|
5 only used here at the moment.
|
|
|
6 """
|
|
|
7
|
|
5
|
8 try:
|
|
|
9 import argparse
|
|
|
10 except ImportError:
|
|
|
11 import argparse26 as argparse
|
|
0
|
12 import operator
|
|
|
13 import sys
|
|
|
14 import xml.sax
|
|
|
15 import xml.sax.saxutils
|
|
|
16
|
|
|
17 class CTDFormatException(Exception):
|
|
|
18 """Raised when there is a format error in CTD."""
|
|
|
19
|
|
|
20
|
|
|
21 class CLIElement(object):
|
|
|
22 """Represents a <clielement> tag.
|
|
|
23
|
|
|
24 :ivar option_identifier: with parameters (e.g. --param), empty if argument.
|
|
|
25 :type option_identifier: str
|
|
|
26 :ivar is_list: whether the element is a list.
|
|
|
27 :type is_list: bool
|
|
|
28 :ivar param_node: link to ParametersNode, set after parsing, None if unset
|
|
|
29 :ivar is_list: w or not this element is a list.
|
|
|
30 :type is_list: bool
|
|
|
31 """
|
|
|
32
|
|
|
33 def __init__(self, option_identifier='', mapping_path='', is_list=False):
|
|
|
34 """Initialize object."""
|
|
|
35 self.option_identifier = option_identifier
|
|
|
36 self.param_node = None # Link to ParametersNode, set after parsing.
|
|
|
37 self.mapping_path = mapping_path
|
|
|
38 self.is_list = is_list
|
|
|
39
|
|
|
40 def __str__(self):
|
|
|
41 """String representaiton of CLIElement."""
|
|
|
42 t = (self.option_identifier, self.mapping_path, self.is_list)
|
|
|
43 return 'CLIElement(%s, %s, %s)' % tuple(map(repr, list(t)))
|
|
|
44
|
|
|
45
|
|
|
46 class ParametersNode(object):
|
|
|
47 """Represents a <NODE> tag inside the <PARAMETERS> tags.
|
|
|
48
|
|
|
49 :ivar name: name attribute of the node
|
|
|
50 :ivar description: text for description attribute of the node
|
|
|
51 :ivar value: value attribute of the node
|
|
|
52 :ivar type_: type attribute of the node
|
|
|
53 :ivar tags: tags attribute of the node
|
|
|
54 :ivar supported_formats: supported_format attribute of the node
|
|
|
55 :ivar restrictions: restrictions attribute of the node
|
|
|
56 :ivar path: the path to the node
|
|
|
57 :ivar path: list of strings
|
|
|
58 :ivar parent: link to the parent of the node
|
|
|
59 :ivar children: children of the node
|
|
|
60 :type children: dict with name to node mapping
|
|
|
61 :ivar cli_element: CLIElement that this parameter is mapped to.
|
|
|
62 """
|
|
|
63
|
|
|
64 def __init__(self, kind='', name='', description='', value='', type_='', tags='',
|
|
|
65 restrictions='', supported_formats=''):
|
|
|
66 """Initialize the object."""
|
|
|
67 self.kind = kind
|
|
|
68 self.name = name
|
|
|
69 self.description = description
|
|
|
70 self.value = value
|
|
|
71 self.type_ = type_
|
|
|
72 self.tags = tags
|
|
|
73 self.supported_formats = supported_formats
|
|
|
74 self.restrictions = restrictions
|
|
|
75 self.path = None # root if is None
|
|
|
76 self.parent = None # not set, usually a list
|
|
|
77 self.children = {}
|
|
|
78 self.cli_element = None
|
|
|
79
|
|
|
80 def computePath(self, is_root=True, path=[]):
|
|
|
81 """Compute path entry from parent links.
|
|
|
82
|
|
|
83 :param is_root: whether or not this is the root node
|
|
|
84 :type is_root: bool
|
|
|
85 :param path: path to this node, excluding root
|
|
|
86 :type path: list of strings
|
|
|
87 """
|
|
|
88 self.path = list(path)
|
|
|
89 if not is_root:
|
|
|
90 self.path.append(self.name)
|
|
|
91 if not self.children:
|
|
|
92 return # nothing to do: early exit.
|
|
|
93 for name, child in self.children.items():
|
|
|
94 child.computePath(False, self.path)
|
|
|
95
|
|
|
96 def applyFunc(self, f):
|
|
|
97 """Apply f to self and all children."""
|
|
|
98 f(self)
|
|
|
99 for c in self.children.values():
|
|
|
100 c.applyFunc(f)
|
|
|
101
|
|
|
102 def find(self, path):
|
|
|
103 """Return ParametersNode object at the path below the node."""
|
|
|
104 if not path:
|
|
|
105 return self
|
|
|
106 if not self.children.get(path[0]):
|
|
|
107 return None
|
|
|
108 return self.children[path[0]].find(path[1:])
|
|
|
109
|
|
|
110 def __str__(self):
|
|
|
111 """Return string representation."""
|
|
|
112 t = (self.name, self.description, self.value, self.type_, self.tags,
|
|
|
113 self.supported_formats, self.children, self.path)
|
|
|
114 return 'ParametersNode(%s, %s, %s, %s, %s, %s, %s, path=%s)' % tuple(map(repr, t))
|
|
|
115
|
|
|
116 def __repr__(self):
|
|
|
117 """Return programmatic representation, same as __str__()."""
|
|
|
118 return str(self)
|
|
|
119
|
|
|
120
|
|
|
121 class Tool(object):
|
|
|
122 """Represents the top-level <tool> tag from a CTD file.
|
|
|
123
|
|
|
124 :ivar name: name attribute value
|
|
|
125 :type name: str
|
|
|
126 :ivar executable_name: executableName attribute value
|
|
|
127 :type executable_name: str
|
|
|
128 :ivar version: version attribute value
|
|
|
129 :type version: str
|
|
|
130 :ivar description: description attribute value
|
|
|
131 :type description: str
|
|
|
132 :ivar manual: manual attribute value
|
|
|
133 :type manual: str
|
|
|
134 :ivar doc_url: docurl attribute value
|
|
|
135 :type doc_url: str
|
|
|
136 :ivar category: category attribute value
|
|
|
137 :type category: str
|
|
|
138 :ivar cli_elements: list of CLIElement objects
|
|
|
139 :ivar parameters: root parameters node
|
|
|
140 :type parameters: ParametersNode
|
|
|
141 """
|
|
|
142
|
|
|
143 def __init__(self, name='', executable_name='', version='',
|
|
|
144 description='', manual='', doc_url='',
|
|
|
145 category=''):
|
|
|
146 self.name = name
|
|
|
147 self.executable_name = executable_name
|
|
|
148 self.version = version
|
|
|
149 self.description = description
|
|
|
150 self.manual = manual
|
|
|
151 self.doc_url = doc_url
|
|
|
152 self.category = category
|
|
|
153 self.cli_elements = []
|
|
|
154 self.parameters = None
|
|
|
155
|
|
|
156 def parsingDone(self):
|
|
|
157 """Called after parsing is done.
|
|
|
158
|
|
|
159 The method will compute the paths of the parameter nodes and link the
|
|
|
160 CLIElement objects in self.cli_elements to the ParameterNode objects.
|
|
|
161 """
|
|
|
162 self.parameters.computePath()
|
|
|
163 for ce in self.cli_elements:
|
|
|
164 if not ce.option_identifier:
|
|
|
165 continue # Skip arguments
|
|
|
166 path = ce.mapping_path.split('.')
|
|
|
167 node = self.parameters.find(path)
|
|
|
168 if not node:
|
|
|
169 raise CTDFormatException('Unknown parameter %s' % '.'.join(path))
|
|
|
170 ce.param_node = node
|
|
|
171 node.cli_element = ce
|
|
|
172
|
|
|
173 def __str__(self):
|
|
|
174 t = (self.name, self.executable_name, self.version, self.description,
|
|
|
175 self.manual, self.doc_url, self.category)
|
|
|
176 return 'Tool(%s, %s, %s, %s, %s, %s, %s)' % tuple(map(repr, list(t)))
|
|
|
177
|
|
|
178
|
|
|
179
|
|
|
180 class CTDHandler(xml.sax.handler.ContentHandler):
|
|
|
181 def __init__(self):
|
|
|
182 self.result = None
|
|
|
183 # A stack of tag names that are currently open.
|
|
|
184 self.stack = []
|
|
|
185 # The current parameter to append nodes below.
|
|
|
186 self.parameter_node = None
|
|
|
187
|
|
|
188 def startElement(self, name, attrs):
|
|
|
189 """Handle start of element."""
|
|
|
190 # Maintain a stack of open tags.
|
|
|
191 self.stack.append(name)
|
|
|
192 # Handle the individual cases. The innermost tag is self.stack[-1].
|
|
|
193 if self.stack == ['tool']:
|
|
|
194 # Create the top level Tool object.
|
|
|
195 self.tool = Tool()
|
|
|
196 self.result = self.tool
|
|
|
197 elif self.stack == ['tool', 'cli', 'clielement']:
|
|
|
198 # Create a new CLIElement object for a <clieelement> tag.
|
|
|
199 if not attrs.get('isList'):
|
|
|
200 raise CTDFormatException('No attribute isList in <clielement>.')
|
|
|
201 if attrs.get('optionIdentifier') is None:
|
|
|
202 raise CTDFormatException('no attribute optionIdentifier in <clielement>.')
|
|
|
203 is_list = (attrs.get('isList') == 'false')
|
|
|
204 option_identifier = attrs.get('optionIdentifier')
|
|
|
205 self.tool.cli_elements.append(CLIElement(option_identifier=option_identifier, is_list=is_list))
|
|
|
206 elif self.stack == ['tool', 'cli', 'clielement', 'mapping']:
|
|
|
207 # Handle a <mapping> sub entry of a <clieelement> tag.
|
|
|
208 if not attrs.get('referenceName'):
|
|
|
209 raise CTDFormatException('no attribute referenceName in <mapping>')
|
|
|
210 self.tool.cli_elements[-1].mapping_path = attrs['referenceName']
|
|
|
211 elif self.stack == ['tool', 'PARAMETERS']:
|
|
|
212 # Handle the <PARAMETERS> entry by creating a new top parameters node.
|
|
|
213 self.tool.parameters = ParametersNode(kind='node', name='<root>')
|
|
|
214 self.parameter_node = self.tool.parameters
|
|
|
215 elif self.stack[:2] == ['tool', 'PARAMETERS'] and self.stack[-1] == 'NODE':
|
|
|
216 # Create a new node ParametersNode for the <PARAMETERS> entry.
|
|
|
217 if not attrs.get('name'):
|
|
|
218 raise CTDFormatException('no attribute name in <NODE>')
|
|
|
219 name = attrs.get('name')
|
|
|
220 node = ParametersNode(kind='node', name=name)
|
|
|
221 node.parent = self.parameter_node
|
|
|
222 self.parameter_node.children[name] = node
|
|
|
223 self.parameter_node = node
|
|
|
224 elif self.stack[:2] == ['tool', 'PARAMETERS'] and self.stack[-1] == 'ITEM':
|
|
|
225 # Create a new item ParametersNode for the <ITEM> entry.
|
|
|
226 if not attrs.get('name'):
|
|
|
227 raise CTDFormatException('no attribute name in <ITEM>')
|
|
|
228 name = attrs.get('name')
|
|
|
229 value = attrs.get('value')
|
|
|
230 type_ = attrs.get('type')
|
|
|
231 tags = attrs.get('tags')
|
|
|
232 description = attrs.get('description')
|
|
|
233 restrictions = attrs.get('restrictions')
|
|
|
234 supported_formats = attrs.get('supported_formats')
|
|
|
235 child = ParametersNode(
|
|
|
236 kind='item', name=name, description=description, value=value,
|
|
|
237 type_=type_, tags=tags, supported_formats=supported_formats,
|
|
|
238 restrictions=restrictions)
|
|
|
239 self.parameter_node.children[name] = child
|
|
|
240
|
|
|
241 def endElement(self, name):
|
|
|
242 """Handle closing tag."""
|
|
|
243 # Maintain stack.
|
|
|
244 self.stack.pop()
|
|
|
245 # Go up one node in the parameters tree if </NODE>
|
|
|
246 if name == 'NODE':
|
|
|
247 self.parameter_node = self.parameter_node.parent
|
|
|
248
|
|
|
249 def characters(self, content):
|
|
|
250 """Handle characters in XML file."""
|
|
|
251 if self.stack == ['tool', 'name']:
|
|
|
252 self.tool.name += content
|
|
|
253 elif self.stack == ['tool', 'executableName']:
|
|
|
254 self.tool.executable_name += content
|
|
|
255 elif self.stack == ['tool', 'version']:
|
|
|
256 self.tool.version += content
|
|
|
257 elif self.stack == ['tool', 'description']:
|
|
|
258 self.tool.description += content
|
|
|
259 elif self.stack == ['tool', 'manual']:
|
|
|
260 self.tool.manual += content
|
|
|
261 elif self.stack == ['tool', 'docurl']:
|
|
|
262 self.tool.doc_url += content
|
|
|
263 elif self.stack == ['tool', 'category']:
|
|
|
264 self.tool.category += content
|
|
|
265
|
|
|
266
|
|
|
267 class CTDParser(object):
|
|
|
268 """Parser for CTD files."""
|
|
|
269
|
|
|
270 def __init__(self):
|
|
|
271 self.handler = CTDHandler()
|
|
|
272
|
|
|
273 def parse(self, path):
|
|
|
274 # Parse XML into Tool object.
|
|
|
275 parser = xml.sax.make_parser()
|
|
|
276 parser.setContentHandler(self.handler)
|
|
|
277 parser.parse(path)
|
|
|
278 # Compute paths for tool's parameters.
|
|
|
279 self.handler.result.parsingDone()
|
|
|
280 return self.handler.result
|
|
|
281
|
|
|
282
|
|
|
283 class XMLWriter(object):
|
|
|
284 """Base class for XML writers.
|
|
|
285
|
|
|
286
|
|
|
287 :ivar result: list of strings that are joined for the final XML
|
|
|
288 :ivar indent_level: int with the indentation level
|
|
|
289 """
|
|
|
290
|
|
|
291 def __init__(self):
|
|
|
292 self.result = []
|
|
|
293 self.indent_level = 0
|
|
|
294
|
|
|
295 def indent(self):
|
|
|
296 """Return indentation whitespace."""
|
|
|
297 return ' ' * self.indent_level
|
|
|
298
|
|
|
299 def appendTag(self, tag, text='', args={}):
|
|
|
300 """Append a tag to self.result with text content only or no content at all."""
|
|
|
301 e = xml.sax.saxutils.quoteattr
|
|
|
302 args_str = ' '.join('%s=%s' % (key, e(str(value))) for key, value in args.items())
|
|
|
303 if args_str:
|
|
|
304 args_str = ' '+ args_str
|
|
|
305 vals = {'indent': self.indent(),
|
|
|
306 'tag': tag,
|
|
|
307 'text': text.strip(),
|
|
|
308 'args': args_str}
|
|
|
309 if text:
|
|
|
310 self.result.append('%(indent)s<%(tag)s%(args)s>%(text)s</%(tag)s>\n' % vals)
|
|
|
311 else:
|
|
|
312 self.result.append('%(indent)s<%(tag)s%(args)s />\n' % vals)
|
|
|
313
|
|
|
314 def openTag(self, tag, args={}):
|
|
|
315 """Append an opening tag to self.result."""
|
|
|
316 e = xml.sax.saxutils.quoteattr
|
|
|
317 args_str = ' '.join('%s=%s' % (key, e(str(value))) for key, value in args.items())
|
|
|
318 if args_str:
|
|
|
319 args_str = ' ' + args_str
|
|
|
320 vals = {'indent': self.indent(),
|
|
|
321 'tag': tag,
|
|
|
322 'args': args_str}
|
|
|
323 self.result.append('%(indent)s<%(tag)s%(args)s>\n' % vals)
|
|
|
324
|
|
|
325 def closeTag(self, tag):
|
|
|
326 """Append a closing tag to self.result."""
|
|
|
327 vals = {'indent': self.indent(), 'tag': tag}
|
|
|
328 self.result.append('%(indent)s</%(tag)s>\n' % vals)
|
|
|
329
|
|
|
330 def handleParameters(self, node):
|
|
|
331 """Recursion for appending tags for ParametersNode."""
|
|
|
332 for pn in node.children.values():
|
|
|
333 if pn.kind == 'item':
|
|
|
334 args = {'name': pn.name,
|
|
|
335 'value': pn.value,
|
|
|
336 'type': pn.type_,
|
|
|
337 'description': pn.description,
|
|
|
338 'restrictions': pn.restrictions,
|
|
|
339 'tags': pn.tags}
|
|
|
340 self.appendTag('ITEM', args=args)
|
|
|
341 else: # node.kind == 'node'
|
|
|
342 args = {'name': pn.name,
|
|
|
343 'description': pn.description}
|
|
|
344 self.openTag('NODE', args=args)
|
|
|
345 self.indent_level += 1
|
|
|
346 self.handleParameters(pn)
|
|
|
347 self.indent_level -= 1
|
|
|
348 self.closeTag('NODE')
|
|
|
349
|
|
|
350
|
|
|
351 class CTDWriter(XMLWriter):
|
|
|
352 """Write a Tool to CTD format."""
|
|
|
353
|
|
|
354 def run(self, tool, f):
|
|
|
355 """Write the given Tool to file f."""
|
|
|
356 self.result.append('<?xml version="1.0" encoding="UTF-8"?>\n')
|
|
|
357 self.openTag('tool')
|
|
|
358 self.indent_level += 1
|
|
|
359 self.appendTag('name', tool.name)
|
|
|
360 self.appendTag('executableName', tool.executable_name)
|
|
|
361 self.appendTag('version', tool.version)
|
|
|
362 self.appendTag('description', tool.description)
|
|
|
363 self.appendTag('manual', tool.manual)
|
|
|
364 self.appendTag('docurl', tool.doc_url)
|
|
|
365 self.appendTag('category', tool.category)
|
|
|
366 # <cli> and <clielement> group
|
|
|
367 self.openTag('cli')
|
|
|
368 self.indent_level += 1
|
|
|
369 for ce in tool.cli_elements:
|
|
|
370 self.openTag('clielement', args={'optionIdentifier': ce.option_identifier,
|
|
|
371 'isList': {True: 'true', False: 'false'}[ce.is_list]})
|
|
|
372 self.indent_level += 1
|
|
|
373 self.appendTag('mapping', args={'referenceName': ce.mapping_path})
|
|
|
374 self.indent_level -= 1
|
|
|
375 self.closeTag('clielement')
|
|
|
376 self.indent_level -= 1
|
|
|
377 self.closeTag('cli')
|
|
|
378 # <PARAMETERS>, <NODE>, <ITEM> group
|
|
|
379 self.openTag('PARAMETERS', args={'version': 1.4,
|
|
|
380 'xsi:noNamespaceSchemaLocation': 'http://open-ms.sourceforge.net/schemas/Param_1_4.xsd',
|
|
|
381 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance'})
|
|
|
382 self.indent_level += 1
|
|
|
383 self.handleParameters(tool.parameters)
|
|
|
384 self.indent_level -= 1
|
|
|
385 self.closeTag('PARAMETERS')
|
|
|
386 self.indent_level -= 1
|
|
|
387 self.closeTag('tool')
|
|
|
388 # Write result
|
|
|
389 for x in self.result:
|
|
|
390 f.write(x)
|
|
|
391
|
|
|
392
|
|
|
393 class GalaxyWriter(XMLWriter):
|
|
|
394 """Write a Tool to the Galaxy format."""
|
|
|
395
|
|
|
396 def run(self, tool, f):
|
|
|
397 """Write the given Tool to file f."""
|
|
|
398 self.result.append('<?xml version="1.0" encoding="UTF-8"?>\n')
|
|
|
399 self.openTag('tool', {'id': tool.executable_name, 'name': tool.name})
|
|
|
400 self.indent_level += 1
|
|
|
401 self.addCommandTag(tool)
|
|
|
402 self.appendTag('description', text=tool.description)
|
|
|
403 self.openTag('inputs')
|
|
|
404 self.indent_level += 1
|
|
|
405 tool.parameters.applyFunc(lambda x: self.addInputParam(x))
|
|
|
406 self.indent_level -= 1
|
|
|
407 self.closeTag('inputs')
|
|
|
408 self.openTag('outputs')
|
|
|
409 self.indent_level += 1
|
|
|
410 tool.parameters.applyFunc(lambda x: self.addOutputParam(x))
|
|
|
411 self.indent_level -= 1
|
|
|
412 self.closeTag('outputs')
|
|
|
413 self.openTag('stdio')
|
|
|
414 self.indent_level += 1
|
|
|
415 self.appendTag('exit_code', args={'range': '1:', 'level': 'fatal'})
|
|
|
416 self.appendTag('exit_code', args={'range': ':-1', 'level': 'fatal'})
|
|
|
417 self.indent_level -= 1
|
|
|
418 self.closeTag('stdio')
|
|
|
419 self.indent_level -= 1
|
|
|
420 self.closeTag('tool')
|
|
|
421 # Write result
|
|
|
422 for x in self.result:
|
|
|
423 f.write(x)
|
|
|
424
|
|
|
425 def addInputParam(self, param_node):
|
|
|
426 """Add a ParametersNode object if it is to go to <inputs>."""
|
|
|
427 if param_node.tags and 'output file' in param_node.tags.split(','):
|
|
|
428 return # Skip output files
|
|
|
429 if param_node.kind != 'item':
|
|
|
430 return # Skip if not item.
|
|
|
431 args = {}
|
|
|
432 if param_node.tags and 'input file' in param_node.tags.split(','):
|
|
|
433 args['type'] = 'data'
|
|
|
434 args['format'] = ','.join([x.replace('*', '').replace('.', '')
|
|
|
435 for x in param_node.supported_formats.split(',')])
|
|
|
436 args['name'] = '_'.join(param_node.path).replace('-', '_').replace('.', '_')
|
|
|
437 args['label'] = param_node.description
|
|
|
438 args['type'] = 'data'
|
|
|
439 self.appendTag('param', args=args)
|
|
|
440 else:
|
|
|
441 TYPE_MAP = {
|
|
|
442 'string': 'text',
|
|
|
443 'double': 'float',
|
|
|
444 'int': 'integer'
|
|
|
445 }
|
|
|
446 args['type'] = TYPE_MAP[param_node.type_]
|
|
|
447 args['name'] = '_'.join(param_node.path).replace('-', '_').replace('.', '_')
|
|
|
448 args['label'] = param_node.description
|
|
|
449 if param_node.type_ == 'string' and param_node.restrictions and \
|
|
|
450 sorted(param_node.restrictions.split(',')) == ['false', 'true']:
|
|
|
451 args['type'] = 'boolean'
|
|
|
452 if param_node.value == 'true':
|
|
|
453 args['checked'] = 'true'
|
|
|
454 args['truevalue'] = param_node.cli_element.option_identifier
|
|
|
455 args['falsevalue'] = ''
|
|
|
456 self.appendTag('param', args=args)
|
|
|
457 return
|
|
|
458 args['value'] = param_node.value
|
|
|
459 if param_node.type_ == 'string' and param_node.restrictions:
|
|
|
460 args['type'] = 'select'
|
|
|
461 self.openTag('param', args=args)
|
|
|
462 self.indent_level += 1
|
|
|
463 for v in param_node.restrictions.split(','):
|
|
|
464 self.appendTag('option', v, {'value': v})
|
|
|
465 self.indent_level -= 1
|
|
|
466 self.closeTag('param')
|
|
|
467 else:
|
|
|
468 self.appendTag('param', args=args)
|
|
|
469
|
|
|
470 def addOutputParam(self, param_node):
|
|
|
471 """Add a ParametersNode object if it is to go to <inputs>."""
|
|
|
472 if not param_node.tags or not 'output file' in param_node.tags.split(','):
|
|
|
473 return # Only add for output files.
|
|
|
474 args = {}
|
|
|
475 if '.' in param_node.supported_formats:
|
|
|
476 args['format'] = param_node.supported_formats.split(',')[0].split('.')[-1]
|
|
|
477 else:
|
|
|
478 args['format'] = param_node.supported_formats.split(',')[0].split('*')[-1]
|
|
|
479 args['name'] = '_'.join(param_node.path).replace('-', '_').replace('.', '_')
|
|
|
480 args['label'] = param_node.description
|
|
|
481 self.appendTag('data', args=args)
|
|
|
482
|
|
|
483 def addCommandTag(self, tool):
|
|
|
484 """Write <command> tag to self.result."""
|
|
|
485 lst = []
|
|
|
486 for ce in tool.cli_elements:
|
|
|
487 bool_param = False
|
|
|
488 if ce.param_node.type_ == 'string' and ce.param_node.restrictions and \
|
|
|
489 sorted(ce.param_node.restrictions.split(',')) == ['false', 'true']:
|
|
|
490 bool_param = True
|
|
|
491 if not bool_param and ce.option_identifier:
|
|
|
492 lst.append(ce.option_identifier)
|
|
|
493 # The path mapping is not ideal but should work OK.
|
|
|
494 lst.append('$' + ce.mapping_path.replace('-', '_').replace('.', '_'))
|
|
|
495 txt = [tool.executable_name] + lst
|
|
|
496 self.appendTag('command', text=' '.join(txt))
|
|
|
497
|
|
|
498
|
|
|
499 def main():
|
|
|
500 """Main function."""
|
|
|
501 # Setup argument parser.
|
|
|
502 parser = argparse.ArgumentParser(description='Convert CTD to Galaxy XML')
|
|
|
503 parser.add_argument('-i', '--in-file', metavar='FILE',
|
|
|
504 help='CTD file to read.', dest='in_file',
|
|
|
505 required=True)
|
|
|
506 parser.add_argument('-o', '--out-file', metavar='FILE',
|
|
|
507 help='File to write. Output type depends on extension.',
|
|
|
508 dest='out_file', required=True)
|
|
|
509
|
|
|
510 args = parser.parse_args()
|
|
|
511
|
|
|
512 # Parse input.
|
|
|
513 sys.stderr.write('Parsing %s...\n' % args.in_file)
|
|
|
514 ctd_parser = CTDParser()
|
|
|
515 tool = ctd_parser.parse(args.in_file)
|
|
|
516
|
|
|
517 # Write output.
|
|
|
518 sys.stderr.write('Writing to %s...\n' % args.out_file)
|
|
|
519 if args.out_file.endswith('.ctd'):
|
|
|
520 writer = CTDWriter()
|
|
|
521 else:
|
|
|
522 writer = GalaxyWriter()
|
|
|
523 with open(args.out_file, 'wb') as f:
|
|
|
524 writer.run(tool, f)
|
|
|
525
|
|
|
526 return 0
|
|
|
527
|
|
|
528
|
|
|
529 if __name__ == '__main__':
|
|
|
530 sys.exit(main())
|