Repository navigation
Description
Issue for the following discussion started on the mailing list:
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()
''What do people think? Do you have a favourite third party library for this
kind of thing?
Peter
Activity
peterjc commentedon May 12, 2020
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?
benfulton commentedon May 12, 2020
What is the plan for discontinuing support for Windows versions?
peterjc commentedon May 12, 2020
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
shlibrary, 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
shlibrary which just does the elegant command line building, but leaves out all the infrastructure for invoking commands.I note
shis under the MIT license. Perhaps is it not impossible to consider porting the elegant command line building fromshinto a much simpler to extendBio/Application/__init__.py? Might be worth an afternoon of experimentation... but could be a daft idea.benfulton commentedon May 12, 2020
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.
SchwarzMarek commentedon May 13, 2020
Hello,
I'm running 3rd party command-line tools quite often and I'm almost exclusively relying directly on
subprocessmodule.My reasoning is mainly:
IMHO I think that the
Bio.Applicationis not needed.peterjc commentedon May 13, 2020
@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?
SchwarzMarek commentedon May 13, 2020
Usually I use lists of strings. If I need to change parameters often, then I use the
.formatfunction for that particular argument in list. I'm trying to avoid theshell=Trueif I can - If I need pipes, then I usesubprocess.Popenor write simple bash script and call that script with arguments needed from python.I don't have experience with
subprocesson windows or macOS.alvyjudy commentedon Oct 16, 2020
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:
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:)
peterjc commentedon Oct 16, 2020
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.
alvyjudy commentedon Oct 17, 2020
@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😅
peterjc commentedon Oct 17, 2020
Having looked at the
shissues, 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 librarysh_liteor something.alvyjudy commentedon Oct 17, 2020
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
peterjc commentedon Oct 17, 2020
I'm not following. Monkey patching the standard library doesn't sound like a good idea, but what would your goal be?
alvyjudy commentedon Oct 18, 2020
Sorry that I didn't explain well. I'm under the impression that
subprocessrequires 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=trueis needed. On Unix, however,shell=trueneeds to be set to make it work with arguments supplied as a string. Ifshell=falseon linux, then arguments need to be supplied in a list, which doesn't work consistently on windows.shell=truealso 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 theshell=trueflag 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.Declare Bio.Application obsolete
Declare Bio.Application obsolete
Declare Bio.Application obsolete
peterjc commentedon Jan 25, 2021
Closed via #3344 declaring Bio.Application obsolete, suggesting using subprocess directly.
Declare Bio.Application obsolete