application subpackage#

Beyond Biotite - The application subpackage#

Although you can achieve a lot with Biotite, there are still a lot of things which are not implemented in this Python package. But wait, this is what the biotite.application package is for: It contains interfaces for popular external software. The usage of these interfaces is seamless: Rather than writing input files and reading output files, you simply put in your Python objects (e.g. instances of Sequence or AtomArray) and the interface returns Python objects (e.g. an Alignment object).

Note

Note that in order to use an interface in biotite.application the corresponding software must be installed. These programs are not shipped with the Biotite package.

Running an application#

Each interface is a subclass of Application. An Application object is merely a handle to the installed software. For example, creating a Muscle3App only looks up the muscle executable, but does not run it yet. A run is launched by calling the respective method of the handle, in this case Muscle3App.run(). It immediately returns a Future, while the software is still running in the background.

import biotite.sequence as seq
import biotite.application.muscle as muscle

sequences = [
    seq.ProteinSequence("BIQTITE"),
    seq.ProteinSequence("TITANITE"),
    seq.ProteinSequence("BISMITE"),
    seq.ProteinSequence("IQLITE"),
]
app = muscle.Muscle3App()
future = app.run(sequences)
# The application is running in the background,
# so we can run other code in the meantime
# Wait for the application to finish and get the result
result = future.result()
print(result.alignment)
BIQT-ITE
TITANITE
BISM-ITE
-IQL-ITE

The lines between the method call and Future.result() can be used to run any other code while the application is running, including starting another Application in parallel. Future.result() blocks until the run has finished and returns the result object. The Future mirrors concurrent.futures.Future, hence you can also check via Future.done() whether the run has finished or abort it via Future.cancel(). As the handle does not keep any state of the run, the same Application object can be used for multiple runs.

Command line options#

Under the hood the method call is converted into a command line invocation of the software. The input objects are written into temporary files and the parameters of the method are translated into the corresponding command line options. The command is available via LocalProcessFuture.command.

print(future.command)
/home/runner/work/biotite/biotite/.pixi/envs/doc/bin/muscle -quiet -in /tmp/tmp9feyeckz.fa -out /tmp/tmplq7ulj6i.fa -tree1 /tmp/tmpcxu68whg.tree -tree2 /tmp/tmpg_gbofce.tree -seqtype protein

Most programs have more options than the method parameters cover. These can be passed as additional keyword arguments, that are named like the respective command line option, with underscores instead of dashes. The value is formatted automatically: For example, a number or string is passed as the option value, None turns the option into a flag without value and a list repeats the option for each element.

future = app.run(sequences, maxiters=2, diags=None)
print(future.command)
print(future.result().alignment)
/home/runner/work/biotite/biotite/.pixi/envs/doc/bin/muscle -quiet -diags -in /tmp/tmp37kb3d16.fa -out /tmp/tmpbm9tz4we.fa -tree1 /tmp/tmpqpkhjyom.tree -tree2 /tmp/tmp5qfv_x6d.tree -seqtype protein -maxiters 2
BIQT-ITE
TITANITE
BISM-ITE
-IQL-ITE

However, not any option is accepted: Options that conflict with the ones the interface sets by itself (e.g. the input and output files) or that would change the output format are rejected.

app.run(sequences, quiet=None)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 app.run(sequences, quiet=None)

File ~/work/biotite/biotite/.pixi/envs/doc/lib/python3.12/site-packages/biotite/application/localapp.py:631, in command.<locals>.decorator.<locals>.wrapper(self, *args, **kwargs)
    629 for key, value in option_kwargs.items():
    630     if key not in allowed:
--> 631         raise ValueError(f"'{key}' is not an allowed command line option")
    632     if key in existing_keys:
    633         raise ValueError(
    634             f"The command line option '{key}' is already set by the "
    635             f"application and cannot be given as keyword argument"
    636         )

ValueError: 'quiet' is not an allowed command line option

The following chapters will give you an overview of the different applications interfaced in the biotite.application subpackage.