Skip to content

Alternatives to Bio.Application for invoking command line tools? #2877

Closed

Description

@peterjc

Issue for the following discussion started on the mailing list:

https://mailman.open-bio.org/pipermail/biopython/2020-May/016769.html

Dear Biopythoneers,

Biopython has a lot of command line tool wrappers, based around the objects
in Bio/Application/init.py, for building a command line string and
running it. Some time ago I started to think that we might actually be
better off dropping our in-house command line wrappers, and recommending a
standard or third party library approach for defining and executing command
line strings instead.

Taking an example in our tutorial, running the blastx tool from NCBI
BLAST+. Currently Biopython provides a specific object for the blastx
command, which knows all the expected command arguments, can do some
validation, and even has some basic help text included for each of them:

>>> from Bio.Blast.Applications import NcbiblastxCommandline
>>> help(NcbiblastxCommandline)
...
>>> blastx_cline = NcbiblastxCommandline(query="opuntia.fasta", db="nr",
evalue=0.001, outfmt=5, out="opuntia.xml")
>>> blastx_cline
NcbiblastxCommandline(cmd='blastx', out='opuntia.xml', outfmt=5,
query='opuntia.fasta',
db='nr', evalue=0.001)
>>> print(blastx_cline)
blastx -out opuntia.xml -outfmt 5 -query opuntia.fasta -db nr -evalue 0.001
>>> stdout, stderr = blastx_cline()

This works quite nicely, but writing a unique class for each command line
tool we wish to support is a lot of quiet tedious work, especially if
including minimal documentation for the arguments or argument validation.
This is also an on-going maintenance problem - one of the issues I think we
should fix before the next Biopython release is updating the NCBI BLAST+
wrappers as new arguments have been added.

Some tools have a rather cryptic command line API, and in those cases
perhaps our efforts are sensible. However, with tools like NCBI BLAST+
where is a clear command line API, and I don't see that our efforts
actually add a great deal over constructing the string in code and calling
subprocess:

>>> import subprocess
>>> cmd = "blastx -query opuntia.fasta -db nr -out opuntia.xml -evalue
0.001 -outfmt 5"
>>> subprocess.check_call(cmd, shell=True)

There are third party libraries which might be easier? For example, the sh
library supports our our current style with keyword arguments:

>>> from sh import blastx
>>> blastx(query="opuntia.fasta", db="nr", out="opuntia.xml",
evalue="0.001", outfmt="5", _long_prefix="-")

You can avoid repeating the extra argument due to the NCBI not following
the minus-minus prefix convention, e.g.:

>>> import sh
>>> blastx = sh.blastx.bake(_long_prefix="-")
>>> blastx(query="opuntia.fasta", db="nr", out="opuntia.xml",
evalue="0.001", outfmt="5")

See https://github.com/amoffat/sh

This is close to the same usability our wrapper offers, but with no ongoing
maintenance burden. It would need more investigation (especially commands
where the order is critical, often seen on macOS but not Linux), but
Windows support aside it seems attractive.

If there was a cross-platform system which offered this Python-like syntax
for specifying the command line arguments, that would be a tempting
alternative. I don't think plumbum (latin for lead, as used for pipes in
the past) does, and I find this form heavy:

>>> from blumbum import local
>>> cmd = local["blastx"]["-query", "opuntia.fasta", "-db", "nr", "-out",
"opuntia.xml", "-evalue", "0.001", "-outfmt", "5"]
>>> cmd()
''

See https://github.com/tomerfiliba/plumbum

What do people think? Do you have a favourite third party library for this
kind of thing?

Peter

Activity

  1. peterjc commented on May 12, 2020

    @peterjc
    MemberAuthor

    My summary of the email discussion so far: No objections to removing our wrappers. No clear winner amongst third party libraries (and no new suggestions), people seem to be OK with using subprocess directly for executing applications.

    Perhaps the next step would be to replace all their usage in the tutorial with direct command line building and running via subprocess, and mark the wrappers as obsolete (see https://biopython.org/wiki/Deprecation_policy - this would precede deprecation).

    The replacement examples will need cross platform testing, especially the Windows vs Unix/Linux/macOS differences. That may be harder than it should be with subprocess, see e.g. the logic we have in place in the wrappers for when to use shell=True:

    https://github.com/biopython/biopython/blob/biopython-176/Bio/Application/__init__.py#L497

    By re-writing the examples to call subprocess directly, we may conclude for cross platform compatibility some sort of wrapper or helper function is still preferable?

  2. benfulton commented on May 12, 2020

    @benfulton
    Contributor

    What is the plan for discontinuing support for Windows versions?

  3. peterjc commented on May 12, 2020

    @peterjc
    MemberAuthor

    I don't want to drop anything for Windows - as far as I know our current application wrappers work fine there (calling subprocess internally for execution), for command line tools available on Windows.

    The lack of Windows support is the main reason why I don't want to use the sh library, which otherwise looks worth following up. Discussion on their issues makes it clear even partial Windows support is not a priority for them, sadly.

    I was/am hoping there is something like the sh library which just does the elegant command line building, but leaves out all the infrastructure for invoking commands.

    I note sh is under the MIT license. Perhaps is it not impossible to consider porting the elegant command line building from sh into a much simpler to extend Bio/Application/__init__.py? Might be worth an afternoon of experimentation... but could be a daft idea.

  4. benfulton commented on May 12, 2020

    @benfulton
    Contributor

    It appears that use_shell will always evaluate to "True" though, excepting perhaps very old versions of Windows. Since the str() method always creates a space-separated, single-string command line, I suspect that use_shell=False wouldn't work anyway.

  5. SchwarzMarek commented on May 13, 2020

    @SchwarzMarek

    Hello,
    I'm running 3rd party command-line tools quite often and I'm almost exclusively relying directly on subprocess module.

    My reasoning is mainly:

    • only one documentation (that for the tool)
    • fine control over the process

    IMHO I think that the Bio.Application is not needed.

  6. peterjc commented on May 13, 2020

    @peterjc
    MemberAuthor

    @SchwarzMarek how do you build the command line itself (e.g. string or list; hardcoded or template or ...)? Also, do you worry about Windows vs Linux vs macOS?

  7. SchwarzMarek commented on May 13, 2020

    @SchwarzMarek

    Usually I use lists of strings. If I need to change parameters often, then I use the .format function for that particular argument in list. I'm trying to avoid the shell=True if I can - If I need pipes, then I use subprocess.Popen or write simple bash script and call that script with arguments needed from python.
    I don't have experience with subprocess on windows or macOS.

  8. alvyjudy commented on Oct 16, 2020

    @alvyjudy

    I'm wondering if it would be feasible to dynamically generate the command line wrapper? I saw this implementation from google api's client libraries. It reads a JSON file and builds the wrapper object on the fly.

    I made a gist demonstrating the idea: https://gist.github.com/alvyjudy/19ce45452d99648eaaf178eca1518444

    Ultimately, I hope to achieve the following:

    >>> from CmdWrapper import build
    >>> muscle = build("muscle")
    >>> muscle.help()
    >>> muscle({'-in':'path/1', '-out':'path/2'})
    
    

    This reduces the efforts to maintaining just a single JSON file and lowers the barrier for contributors too. It also allows us to keep the current wrapper interfaces and add in new ones with minimal efforts. I'm willing to work out the details if this seems to be a feasible idea (I hope I didn't underestimate the difficulty....)

    Open to suggestions/criticism/discussion:)

  9. peterjc commented on Oct 16, 2020

    @peterjc
    MemberAuthor

    Hi @alvyjudy,

    I don't see that JSON is much easier than a Python list - which is the core of the work in adding a new application wrapper: the parameters are a list of switches, options, and positional arguments. The trouble is there's quite a lot of special cases that the framework can handle, and optional bits to try to make life easier for the user, so it can look complicated.

    We could simplify the wrappers, dropping all the command line argument validation for example (currently done with an optional function, often a lambda function), incompatible argument checking, and perhaps even the docstrings (a lot of copy-and-paste work which then can become out of date)? If that lowered the entry level barrier, it might be worthwhile?

    How does that strike you?

    Your JSON idea reminded me of another approach. It might be interesting to tap into any broader command line wrapper definition language - there have been efforts here for workflow engines which I am tangentially aware of though work on Galaxy, which has an XML and Python-like templating approach to building command lines.

  10. alvyjudy commented on Oct 17, 2020

    @alvyjudy

    @peterjc you are right. I read a bit more on those cmd tools and saw that my approach definitely understated the complexity, although I was hoping to accomplish some "separation of concern" (how the program works should be separated from how the wrapper was constructed) which should make it easier to add support for more cmd programs, even by less seasoned developers. I experimented a bit more and realize my approach ultimately brings it closer to just educating users to use "subprocess" directly, but also recognize this might disrupt cross-platform compatibility.

    Maybe we could monkeypatch subprocess? Or make a clone of "sh" that works for windows?

    I'm not sure how difficult it would be but since I'm graduating soon so I probably have enough time to experiment with that:) no guarantee though😅

  11. peterjc commented on Oct 17, 2020

    @peterjc
    MemberAuthor

    Having looked at the sh issues, I think the developer is right that full Windows compatibility is ambitious. However, the core of building a command line string and executing it might be... but it would be hard to justify having that in Biopython. It would be better as a standalone small library sh_lite or something.

  12. alvyjudy commented on Oct 17, 2020

    @alvyjudy

    In that case, how about monkeypatching subprocess? Instead of importing from the standard library, users import from biopython. And this version of subprocess simply add in the switches and required params depending on the environment. That way cross platform code can be achieved

  13. peterjc commented on Oct 17, 2020

    @peterjc
    MemberAuthor

    I'm not following. Monkey patching the standard library doesn't sound like a good idea, but what would your goal be?

  14. alvyjudy commented on Oct 18, 2020

    @alvyjudy

    Sorry that I didn't explain well. I'm under the impression that subprocess requires quite different syntax/considerations under windows and posix and thought monkey patching could unify the behaviour. If we use the standard library's subprocess, then the following needs to be considered and explained in the tutorial:

    On windows, a single command line string is the common interface and there are only few cases shell=true is needed. On Unix, however, shell=true needs to be set to make it work with arguments supplied as a string. If shell=false on linux, then arguments need to be supplied in a list, which doesn't work consistently on windows. shell=true also brings security considerations, which could be an issue for some.

    Users might end up having to write different versions of their code to support both environments.

    If we make a clone of subprocess, it would just set the shell=true flag for users when under linux environment, and make sure that args are supplied as a string. I think this would be useful for those that just want to get things to work. Later, if they figured out details, they can always switch back to the standard library version.

  15. added a commit that references this issue on Nov 10, 2020
  16. peterjc commented on Jan 25, 2021

    @peterjc
    MemberAuthor

    Closed via #3344 declaring Bio.Application obsolete, suggesting using subprocess directly.

  17. added a commit that references this issue on Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions