def make_optional(param): name = param.get('name') help_ = param.attrib.pop('help') label = param.attrib.pop('label') conditional = XMLNode('conditional', name=galaxy_ui_var(tag='conditional', name=name)) use_default = galaxy_ui_var(value='default') use_value = galaxy_ui_var(value='provide') picker = XMLNode('param', name=galaxy_ui_var(tag='select'), type='select', help=help_, label=label) picker.append( XMLNode('option', 'None (Use default behavior)', value=use_default, selected='true')) picker.append(XMLNode('option', 'Provide a value', value=use_value)) conditional.append(picker) when = XMLNode('when', value=use_default) when.append( XMLNode('param', type='hidden', name=name, value=galaxy_esc(None))) conditional.append(when) when = XMLNode('when', value=use_value) when.append(param) conditional.append(when) return conditional
def _clean_inputs(inputs): cleaned = {} for key, value in inputs.items(): if type(value) is list: input_ = [] for elem in value: if type(elem) is dict: input_.extend(_clean_inputs(elem).values()) else: input_.append(elem) if input_ == [None]: input_ = None cleaned[key] = input_ continue # smash together nested dictionaries which are a consequence of # UI nesting if key.startswith(galaxy_ui_var()): if type(value) is dict: cleaned.update(_clean_inputs(value)) continue if type(value) == str: # Galaxy seems to escape certain strings. For instance, the where # clause from filter-table filter-samples in moving pictures goes # from "[body-site]='gut'" # to "__dq____ob__body-site__cb__=__sq__gut__sq____dq__". # This needs to be undone, so we replace that here: cleaned[key] = galaxy_unesc(value) else: cleaned[key] = value return cleaned
def _clean_inputs(inputs, collapse_single=False): if type(inputs) is str: # Galaxy seems to escape certain strings. For instance, the where # clause from filter-table filter-samples in moving pictures goes # from "[body-site]='gut'" # to "__dq____ob__body-site__cb__=__sq__gut__sq____dq__". # This needs to be undone, so we replace that here: return galaxy_unesc(inputs) elif type(inputs) is list: res = [_clean_inputs(x, collapse_single=True) for x in inputs] if res == [None]: res = None return res elif type(inputs) is dict: res = {} for key, value in inputs.items(): # smash together nested dictionaries which are a consequence of # UI nesting if key.startswith(galaxy_ui_var()): if type(value) is dict: res.update(_clean_inputs(value)) continue res[key] = _clean_inputs(value) if collapse_single and len(res) == 1: return next(iter(res.values())) else: return res return inputs
def inputs_xml(self): root = XMLNode('repeat', name=self.name, title=f'{self.name}: {str(self.spec.qiime_type)}') self.add_help(root) if not self.spec.has_default(): root.set('min', '1') conditional = XMLNode('conditional', name=galaxy_ui_var(tag='conditional', name=self.name)) select = XMLNode('param', type='select', name='type', label=f'{self.name}: {str(self.spec.qiime_type)}') select.append(XMLNode('option', 'Metadata from TSV', value='tsv', selected='true')) select.append(XMLNode('option', 'Metadata from Artifact', value='qza')) conditional.append(select) when_tsv = XMLNode('when', value='tsv') tsv = XMLNode('param', type='data', format='tabular,qiime2.tabular', name='source', label='Metadata Source') when_tsv.append(tsv) conditional.append(when_tsv) when_artifact = XMLNode('when', value='qza') artifact = XMLNode('param', type='data', format='qza', name='source', label='Metadata Source') when_artifact.append(artifact) conditional.append(when_artifact) root.append(conditional) return root
def tests_xml(self): arg = str(self.arg) if type(self.arg) is str or type(self.arg) is bool or self.arg is None: arg = galaxy_esc(self.arg) param = XMLNode('param', name=self.name, value=arg) selected_branch = None for galaxy_name, type_ in self.branches.items(): if self.arg in type_: selected_branch = galaxy_name break else: raise Exception("How did this happen?") conditional = XMLNode( 'conditional', name=galaxy_ui_var(tag='conditional', name=self.name)) select = XMLNode('param', name=galaxy_ui_var(name='select'), value=selected_branch) conditional.append(select) conditional.append(param) return conditional
def tests_xml(self): if self.arg is None: return arg = self.arg if type(self.arg) is not list: arg = [self.arg] merged = [] for type_, source in arg: repeat = XMLNode('repeat', name=self.name) cond = XMLNode('conditional', name=galaxy_ui_var(tag='conditional', name=self.name)) cond.append(XMLNode('param', name='type', value=type_)) if type_ == 'tsv': type_ = 'qiime2.tabular' cond.append(XMLNode('param', name='source', value=source, ftype=type_)) repeat.append(cond) merged.append(repeat) return merged
for case in signature_to_galaxy(signature): xml = case.inputs_xml() if case.is_advanced(): if type(xml) is list: advanced.extend(xml) else: advanced.append(xml) else: if type(xml) is list: inputs.extend(xml) else: inputs.append(xml) if advanced: section = XMLNode('section', name=galaxy_ui_var(tag='section', name='extra_opts'), title='Click here for additional options') section.extend(advanced) inputs.append(section) outputs = XMLNode('outputs') for name, spec in signature.outputs.items(): output = make_output(name, spec) outputs.append(output) tool = XMLNode('tool', id=make_tool_id(plugin.id, action.id), name=make_tool_name(plugin.name, action.id), version=f'{plugin.version}+q2galaxy.{q2galaxy.__version__}') tool.append(XMLNode('description', action.name)) tool.append(make_command(plugin, action))
def inputs_xml(self): base_types = [] for t in self.spec.qiime_type: if t.predicate is not None and is_union(t.predicate): for pred in t.predicate.unpack_union(): base_types.append(t.duplicate(predicate=pred)) else: base_types.append(t) to_add = [] for t in base_types: if ((t.name == "Str" and t.predicate is None) or t.name == 'Float' or t.name == 'Int'): to_add.append(t) root = None if to_add: root = XMLNode('conditional', name=galaxy_ui_var(tag='conditional', name=self.name)) select = XMLNode('param', type='select', name=galaxy_ui_var(tag='select')) root.append(select) else: select = XMLNode('param', type='select', name=self.name) choices = [] for t in base_types: if t.predicate is not None and t.predicate.name == 'Choices': choices.extend(t.predicate.template.choices) elif t.name == 'Bool': choices.extend([True, False]) display = None if not self.spec.has_default(): display = 'Selection required' elif self.spec.default is None: display = 'None (Use default behavior)' if display is not None: value = galaxy_esc(None) select.append( XMLNode('option', display, value=value, selected='true')) if root is not None: when = XMLNode('when', value=value) hidden = XMLNode('param', type='hidden', name=self.name, value=value) when.append(hidden) root.append(when) for choice in choices: value = galaxy_esc(choice) option = XMLNode('option', self._display_func(choice), value=value) if self.spec.has_default() and self.spec.default == choice: option.set('selected', 'true') select.append(option) if root is not None: when = XMLNode('when', value=value) hidden = XMLNode('param', type='hidden', name=self.name, value=value) when.append(hidden) root.append(when) default = self.spec.default # NOVALUE is fine for addition in to_add: value = galaxy_ui_var(value=galaxy_esc( # Galaxy will convert % to X internally and then complain # about a lack of matching cases, so we'll just do it now str(addition).replace('%', 'X'))) option = XMLNode('option', f'Provide a value ({addition})', value=value) select.append(option) when = XMLNode('when', value=value) dispatch = { 'Float': NumericCase, 'Int': NumericCase, 'Str': StrCase } try: ParamCase = dispatch[addition.name] except KeyError: raise NotImplementedError if default in addition: spec_default = default option.set('selected', 'true') else: spec_default = self.spec.NOVALUE new_spec = self.spec.duplicate(qiime_type=addition, default=spec_default) sub_ui = ParamCase(self.name, new_spec).inputs_xml() when.append(sub_ui) root.append(when) self.add_help(select) self.add_label(select) if not self.spec.has_default(): select.append( XMLNode('validator', f'value != {repr(galaxy_esc(None))}', type='expression', message='Please verify this parameter.')) if root is None: return select else: return root
def _sanitize(t): return galaxy_ui_var(value=galaxy_esc(str(t).replace('%', 'X')))
def make_builtin_import(meta, tool_id): pm = sdk.PluginManager() inputs = XMLNode('inputs') # Not a galaxy_ui_var() because this will be taken by name from the # Cheetah searchList (see `def _inline_code` below) conditional = XMLNode('conditional', name='import_root') inputs.append(conditional) type_ = XMLNode('param', name='type', type='select', label=('Type of data to import:')) type_.append( XMLNode('option', 'Select a QIIME 2 type to import.', value='None')) conditional.append(type_) when = XMLNode('when', value="None") conditional.append(when) default_formats = _get_default_formats(pm) plugins = set() known_formats = set() for record in sorted(pm.get_semantic_types().values(), key=lambda x: str(x.semantic_type)): plugins.add(record.plugin) type_option = XMLNode('option', str(record.semantic_type), value=galaxy_esc(str(record.semantic_type))) type_.append(type_option) when = XMLNode('when', value=galaxy_esc(str(record.semantic_type))) fmt_conditional = XMLNode('conditional', name=galaxy_ui_var(tag='cond', name='format')) select = XMLNode('param', type='select', name='format', label="QIIME 2 file format to import from:") fmt_conditional.append(select) when.append(fmt_conditional) default_format = default_formats[record.semantic_type] for fmt_rec in sorted(pm.get_formats( filter=GetFormatFilters.IMPORTABLE, semantic_type=record.semantic_type).values(), key=lambda x: x.format.__name__): if issubclass(fmt_rec.format, model.SingleFileDirectoryFormatBase): # These are really just noise for the galaxy UI # an implicit transformation from the backing file format # is simpler and removes the redundancy continue plugins.add(fmt_rec.plugin) known_formats.add(fmt_rec.format) option = XMLNode( 'option', pretty_fmt_name(fmt_rec.format), value=galaxy_esc(fmt_rec.format.__name__), selected=str(fmt_rec.format == default_format).lower()) select.append(option) fmt_when = XMLNode('when', value=galaxy_esc(fmt_rec.format.__name__)) fmt_conditional.append(fmt_when) _add_format_ui(fmt_when, fmt_rec) conditional.append(when) outputs = XMLNode('outputs') outputs.append( XMLNode('data', name='imported_data', format='qza', from_work_dir='imported_data.qza')) tool = XMLNode('tool', id=tool_id, name=make_tool_name_from_id(tool_id), version=make_builtin_version(plugins)) tool.append(inputs) tool.append(outputs) tool.append(XMLNode('command', "q2galaxy run tools import '$inputs'")) tool.append(_make_config()) tool.append(XMLNode('description', 'Import data into a QIIME 2 artifact')) tool.append(make_citations()) tool.append(make_requirements(meta, *[p.project_name for p in plugins])) tool.append(_make_help(known_formats)) return tool
def _add_collection_ui(root, file_attr): section = XMLNode("section", name=f'import_{file_attr.name}', expanded='true') conditional = XMLNode("conditional", name=galaxy_ui_var(tag='cond', name=file_attr.name)) select = XMLNode("param", type='select', label='Select a mechanism', name=galaxy_ui_var(tag='select', name='picker')) select.append( XMLNode("option", "Use collection to import", value='collection', selected='true')) select.append( XMLNode("option", "Associate individual files", value='individual')) conditional.append(select) when_collection = XMLNode("when", value='collection') when_collection.append( XMLNode('param', type='data_collection', name='elements', help=_format_help_text(file_attr.format) + ' Elements must match regex:' f' {_regex_xml_escape(file_attr.pathspec)}')) add_ext_cond = XMLNode("conditional", name=galaxy_ui_var(tag='cond', name='add_ext')) ext_select = XMLNode("param", type='select', label='Append an extension?', help='This is needed if your element identifiers lack' ' one.', name=galaxy_ui_var(tag='select', name='ext_pick')) ext_select.append( XMLNode("option", "No, use element identifiers as is", value="no")) ext_select.append( XMLNode("option", "Yes, append an extension", value="yes")) add_ext_cond.append(ext_select) ext_when = XMLNode("when", value="yes") ext_when.append( XMLNode("param", type='text', name='ext', label="Extension to append (e.g. '.fastq.gz')")) add_ext_cond.append(ext_when) add_ext_cond.append(XMLNode("when", value="no")) when_collection.append(add_ext_cond) when_individual = XMLNode("when", value='individual') repeat = XMLNode("repeat", name='elements', min='1') _add_data_ui(repeat, file_attr) when_individual.append(repeat) conditional.append(when_collection) conditional.append(when_individual) section.append(conditional) root.append(section)