Skip to main content

Instrument for forming Galaxy XML and CWL tool descriptions from argparse arguments

Project description

PyPI Build Status

This project aims to provide a drop-in replacement for argparse which allows generating Galaxy XML and CWL Tools.

It is quite literally a drop-in replacement. You (or the upstream tool author) can use argparse completely as normal

import argparse

When this package is installed and PYTHONPATH=$(argparse2tool) is set the dropin replacement of argparse is used ahead of the system argparse, argparse2tool will capture all argparse function calls, and process them specially.

This information captured in this process is used to produce Galaxy Tool XML when it’s requested with the --generate_galaxy_xml flag, or CWL Tools when requested with the --generate_cwl_tool flag.

For our example python script you can see the generated Galaxy XML and CWL Tools.

Running

To generate XML or CWL, run your tool with the appropriate command line flag

$ PYTHONPATH=$(argparse2tool) <tool command> --generate_galaxy_xml <other options> > tool.xml
$ PYTHONPATH=$(argparse2tool) <tool command> --generate_cwl_tool <other options> > tool.cwl

The project includes a sample example.py file which uses as many argparse features as possible. CWL and Galaxy XML support different portions feature sets which will be visible in the generated outputs.

$ PYTHONPATH=$(argparse2tool) python example.py --generate_galaxy_xml
$ PYTHONPATH=$(argparse2tool) python example.py --generate_cwl_tool

CWL Specific Functionality

Example for CNVkit toolkit

$ cnvkit.py batch --generate_cwl_tool -d ~/cnvkit-tools/ --generate_outputs

If there are subcommands in the provided command, all possible tools will be generated, for instance, for CNVkit

$ cnvkit.py --generate_cwl_tool

will produce CWL tool descriptions for cnvkit.py batch, cnvkit.py access, cnvkit.py export bed, cnvkit.py export cdt and all other subcommands.

Other options (which work only with --generate_cwl_tool provided, except for help message) are:

  • -o FILENAME, --output_section FILENAME: File with manually filled output section which is put to a formed CWL tool. argparse2tool is not very good at generating outputs, it recognizes output files only if they have type argparse.FileType('w'), so output section is often blank and should be filled manually.

  • -go, --generate_outputs: flag for generating outputs not only from arguments that are instances of argparse.FileType('w'), but also from every argument which contains output keyword in its name. For instance, argument --output-file with no type will also be placed to output section. However, ‘–output-directory’ argument will also be treated like File, so generated tools must be checked carefully if when this option is selected.

  • -b, basecommand: command which appears in basecommand field in a resulting tool. It is handy to use this option when you run tool with shebang, but want python to be in basecommand field and the file amidst arguments. Example:

    $ .search.py --generate_cwl_tool -b python.

Basecommand of the formed tool will be ['python'], and search will be a positional argument on position 0.

  • -d, --directory: directory for storing tool descriptions.

  • --help_arg2cwl: prints this help message.

How it works

Internally, argparse2tool, masquerading as argparse attempts to find and import the real argparse. It then stores a reference to the code module for the system argparse, and presents the user with all of the functions that stdlib’s argparse provides. Every function call is passed through the system argparse. However, argparse2tool captures the details of those calls and when Tool XML or CWL is requested, it builds up the tool definition and prints it out to standard output.

Examples

You can see the example.py file for an example with numerous types of arguments and options that you might see in real tools. Accordingly there is an example.xml file with the output.

It doesn’t work!!

If you are not able to use the --generate_galaxy_xml/--generate_cwl_tool flags after installing, it is probably because of module load order. argparse2tool must precede argparse in the path.

To easily correct this, run the tool argparse2tool which is installed as part of this package. Correctly functioning paths will produce the following:

$ argparse2tool
PATH_TO_THE_DROPINS

while incorrectly ordered paths will produce a helpful error message:

$ argparse2tool
no dropins dir...

This is intended to be used inline:

user@host:$ PYTHONPATH=$(argparse2tool) python my_script.py --generate_galaxy_xml

Limitations

This code doesn’t cover the entirety of the argparse API yet, and there are some bugs to work out on the XML generation side:

  • argparse

    • groups not supported (in galaxy, everything should still work in argparse)

    • some features like templating of the version string (please submit bugs)

  • galaxyxml

    • bugs in conditionals/whens (probably)

  • argparse2tool Galaxy XML Output

    • support declaring output files in an argparse-esque manner

  • argparse2tool CWL Output

    • Some of argparse features can not be ported to CWL.

      1. nargs=N. Number of arguments can not be specified in CWL (yet).

      2. const argument of add_argument(). All constants must be specified in job files.

      3. Custom types and custom actions are not supported.

      4. Argument groups don’t work in CWL as arguments are sorted with a special algorithm

      5. Mutual exclusion is not supported.

License

Apache License, v2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

argparse2tool-0.4.9.tar.gz (24.5 kB view details)

Uploaded Source

Built Distributions

argparse2tool-0.4.9-py3.6.egg (40.7 kB view details)

Uploaded Source

argparse2tool-0.4.9-py2.py3-none-any.whl (27.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file argparse2tool-0.4.9.tar.gz.

File metadata

  • Download URL: argparse2tool-0.4.9.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.7

File hashes

Hashes for argparse2tool-0.4.9.tar.gz
Algorithm Hash digest
SHA256 ecd7142c1ce60bf9e1416443ce393907b7f99a9396a0530277c110dcb7106579
MD5 38ec5a02d030c237255b4234dde2c1a5
BLAKE2b-256 bc921acb55f0d6e1b77ff3a23af168f8bf1cd79b01e7b42bec8b2024c5c4ef21

See more details on using hashes here.

File details

Details for the file argparse2tool-0.4.9-py3.6.egg.

File metadata

  • Download URL: argparse2tool-0.4.9-py3.6.egg
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.7

File hashes

Hashes for argparse2tool-0.4.9-py3.6.egg
Algorithm Hash digest
SHA256 3c73ebc5b925720a81679cd88bb16b285e227a590f26c46c45911e9976d2445e
MD5 1bcd30bf600060bded583d464e78a1a2
BLAKE2b-256 39838084d378d77f7f52efa51ad3f1e845f3b0c1d9e02e0de0cfe44661f0f740

See more details on using hashes here.

File details

Details for the file argparse2tool-0.4.9-py2.py3-none-any.whl.

File metadata

  • Download URL: argparse2tool-0.4.9-py2.py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.7

File hashes

Hashes for argparse2tool-0.4.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2edb9e86a9efb081f72145c53bf57672aa684320ed06c10add523252e4a2d9bc
MD5 0f380bfeaa8b4bbd613e63f53703575d
BLAKE2b-256 5418363e288f51893e8a2aa976b5a56272e1c2fcda4193d9c545e9d9d89cb230

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page