Exemplo n.º 1
0
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
Exemplo n.º 2
0
        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))
    tool.append(make_version_command(plugin))
    tool.append(make_config())
    tool.append(inputs)
    tool.append(outputs)
    tool.append(make_tests(action))
    tool.append(make_help(plugin, action))
    tool.append(make_citations(plugin, action))
    tool.append(make_requirements(conda_meta, plugin.project_name))
    return tool


def make_tests(action):
    tests = XMLNode('tests')
    for idx, example in enumerate(action.examples.values()):
        use = GalaxyTestUsage(example_path=(action, idx))
        example(use)
        tests.append(use.xml)

    return tests


def make_filename(name, spec):
    if sdk.util.is_visualization_type(spec.qiime_type):
Exemplo n.º 3
0
def make_builtin_export(meta, tool_id):
    pm = sdk.PluginManager()
    inputs = XMLNode('inputs')

    # This also works for qzvs even though the format just says qza so. . .
    qza_input = 'input'
    inputs.append(XMLNode('param', format="qza", name=qza_input, type='data',
                          label='input: The path to the artifact you '
                          'want to export'))

    type_peek = XMLNode('param', type='select', name='type_peek',
                        display='radio',
                        label="The type of your input qza is:")
    type_peek_opts = XMLNode('options')
    type_peek_filter = XMLNode('filter', type='data_meta', ref=qza_input,
                               key='semantic_type')
    type_peek_opts.append(type_peek_filter)
    type_peek.append(type_peek_opts)
    inputs.append(type_peek)

    fmt_peek = XMLNode('param', type='select', name='fmt_peek',
                       display='radio', label="The current QIIME 2 format is:")
    fmt_peek_opts = XMLNode('options')
    fmt_peek_filter = XMLNode('filter', type='data_meta', ref=qza_input,
                              key='format')
    fmt_peek_opts.append(fmt_peek_filter)
    fmt_peek.append(fmt_peek_opts)
    inputs.append(fmt_peek)

    conditional = XMLNode('conditional', name='fmt_finder')
    inputs.append(conditional)

    type_ = XMLNode('param', name='type', type='select',
                    label=('To change the format, select the type indicated'
                           ' above:'))
    type_.append(XMLNode('option', 'export as is (no conversion)',
                         value='None', selected='true'))
    conditional.append(type_)

    when = XMLNode('when', value="None")
    select = XMLNode('param', type='select', name='output_format',
                     label="QIIME 2 file format to convert to:")

    select.append(XMLNode('option', 'export as is (no conversion)',
                          value='None', selected='true'))
    when.append(select)
    conditional.append(when)

    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)))
        select = XMLNode('param', type='select', name='output_format',
                         label="QIIME 2 file format to convert to:")

        select.append(XMLNode('option', 'export as is (no conversion)',
                      value='None', selected='true'))

        for fmt_rec in sorted(
                pm.get_formats(filter=GetFormatFilters.EXPORTABLE,
                               semantic_type=record.semantic_type).values(),
                key=lambda x: x.format.__name__):
            plugins.add(fmt_rec.plugin)
            known_formats.add(fmt_rec.format)

            if not issubclass(fmt_rec.format,
                              model.SingleFileDirectoryFormatBase):
                option = XMLNode('option', pretty_fmt_name(fmt_rec.format),
                                 value=galaxy_esc(fmt_rec.format.__name__))
                select.append(option)

        when.append(select)
        conditional.append(when)

    outputs = XMLNode('outputs')

    # default collection:
    collection = XMLNode('collection', name='exported', type='list',
                         label='${tool.name} on ${on_string} as ${fmt_peek}')

    _filter_set = repr({galaxy_esc(x.__name__) for x in known_formats})
    collection.append(XMLNode('filter', "fmt_finder['output_format'] == 'None'"
                              f" and fmt_peek not in {_filter_set}"))
    collection.append(XMLNode('discover_datasets', visible='false',
                              pattern='__designation_and_ext__'))
    outputs.append(collection)

    for fmt in sorted(known_formats, key=lambda x: x.__name__):
        esc_fmt = galaxy_esc(fmt.__name__)
        filter_exp = (f"fmt_finder['output_format'] == '{esc_fmt}'"
                      " or (fmt_finder['output_format'] == 'None' and fmt_peek"
                      f" == '{esc_fmt}')")
        label = '${tool.name} on ${on_string} as ' + fmt.__name__
        if issubclass(fmt, model.DirectoryFormat):
            dyn_data = None
            for field in fmt._fields:  # file attrs of the dirfmt
                file_attr = getattr(fmt, field)
                pattern, has_ext = pathspec_to_galaxy_regex(file_attr.pathspec)
                extras = {}
                if not has_ext:
                    if issubclass(file_attr.format, model.TextFileFormat):
                        extras['ext'] = 'txt'
                    else:
                        extras['ext'] = 'data'

                if isinstance(file_attr, model.FileCollection):
                    col = XMLNode('collection', type='list',
                                  name='_'.join([esc_fmt, file_attr.name]),
                                  label=label + f' ({file_attr.name})')
                    col.append(XMLNode('filter', filter_exp))
                    col.append(XMLNode('discover_datasets', visible='false',
                                       pattern=pattern, **extras))
                    outputs.append(col)
                else:
                    if dyn_data is None:
                        # init a root for all of the discoverable datasets
                        dyn_data = XMLNode('data', name=esc_fmt, label=label)
                        dyn_data.append(XMLNode('filter', filter_exp))
                        # only the first one, will take over the history item
                        extras['assign_primary_output'] = 'true'

                    dyn_data.append(XMLNode('discover_datasets',
                                            visible='true', pattern=pattern,
                                            **extras))
            if dyn_data is not None:
                outputs.append(dyn_data)
        else:
            # as it is not a directory, we don't know much about the original
            # filename anymore, so we will let galaxy sort it out
            # .gz may be considered the format instead of fastq.gz, but users
            # can always re-sniff a history item for the format
            collection = XMLNode('data', name=esc_fmt, label=label)
            collection.append(XMLNode('filter', filter_exp))
            collection.append(XMLNode('discover_datasets', visible='true',
                                      assign_primary_output='true',
                                      pattern='__designation_and_ext__'))
            outputs.append(collection)

    tool = XMLNode('tool', id=tool_id, name=make_tool_name_from_id(tool_id),
                   version=make_builtin_version(plugins))
    tool.append(XMLNode('description', 'Export data from QIIME 2 artifacts'))
    tool.append(XMLNode('command', "q2galaxy run tools export '$inputs'"))
    tool.append(make_config())
    tool.append(inputs)
    tool.append(outputs)
    tool.append(make_requirements(meta, *[p.project_name for p in plugins]))
    tool.append(XMLNode('help', ''))

    return tool