nextflow.py is a Python wrapper around the Nextflow pipeline framework. It lets you run Nextflow pipelines from Python code.
>>> import nextflow >>> execution = nextflow.run(path="main.nf", params={"param1": "123"}) >>> print(execution.status)
nextflow.py can be installed using pip:
$ pip install nextflow
If you get permission errors, try using sudo
:
$ sudo pip install nextflow
The package is available via Bioconda:
$ conda install -c bioconda python-nextflow
The repository for nextflow.py, containing the most recent iteration, can be found here. To clone the nextflow.py repository directly from there, use:
$ git clone git://github.com/goodwright/nextflow.py.git
nextflow.py requires the Nextflow executable to be installed and in your PATH. Instructions for installing Nextflow can be found at their website.
To test a local version of nextflow.py, cd to the nextflow.py directory and run:
$ python -m unittest discover tests
You can opt to only run unit tests or integration tests:
$ python -m unittest discover tests.unit $ python -m unittest discover tests.integration
The starting point for any nextflow.py pipeline is the Pipeline
object. This is initialised with a path to the file in question, and,
optionally, the location of an accompanying config file:
>>> pipeline1 = nextflow.Pipeline("pipelines/my-pipeline.nf") >>> pipeline2 = nextflow.Pipeline("main.nf", config="nextflow.config")
To actually execute the pipeline, the run
method is used:
>>> execution = pipeline.run()
This will return an Execution
object, which represents the pipeline
execution that just took place. You can customise the execution with various
options:
>>> execution = pipeline.run(location="./rundir", params={"param1": "123"}, profile=["docker", "test"], version="22.0.1")
This sets the execution to take place in a different location, passes
--param1=123
as a command line argument when the pipeline is run, uses the
Nextflow profiles 'docker' and 'test', and runs with Nextflow version 22.0.1
(regardless of what version of Nextflow is installed).
An Execution
represents a single execution of a
Pipeline
. It has properties for:
id
- The unique ID of that run, generated by Nextflow.started
- When the pipeline ran (as a UNIX timestamp).started_dt
- When the pipeline ran (as a Python datetime).duration
- how long the execution took in seconds.status
- the status Nextflow reports on completion.command
- the command used to run the pipeline.stdout
- the stdout of the execution process.stderr
- the stderr of the execution process.log
- the full text of the log file produced.returncode
- the exit code of the run - usually 0 or 1.pipeline
- thePipeline
that created the execution.
It also has a process_executions
property, which is a list of
ProcessExecution
objects. Nextflow processes data by chaining
together isolated 'processes', and each of these has a
ProcessExecution
object representing its execution. These have the
following properties:
hash
- The unique ID generated by Nextflow, of the formxx/xxxxxx
.process
- The name of the process that spawned the process execution.name
- The name of this specific process execution.status
- the status Nextflow reports on completion.stdout
- the stdout of the process execution.stderr
- the stderr of the process execution.started
- When the process execution ran (as a UNIX timestamp).started_dt
- When the process execution ran (as a Python datetime).duration
- how long the process execution took in seconds.returncode
- the exit code of the process execution - usually 0 or 1.
Process executions can have various files passed to them, and will create files during their execution too. These can be obtained as follows:
>>> process_execution.input_data() # Full absolute paths >>> process_execution.input_data(include_path=False) # Just file names >>> process_execution.all_output_data() # Full absolute paths >>> process_execution.all_output_data(include_path=False) # Just file names
Note
Nextflow makes a distinction between process output files which were 'published' via some channel, and those which weren't. It is not possible to distinguish these once execution is complete, so nextflow.py reports all output files, not just those which are 'published'.
The method described above will run the pipeline and wait while it does, with
the completed Execution
being returned only at the end.
An alternate method is to use run_and_poll
, which returns an
Execution
object every few seconds representing the state of the
pipeline execution at that moment in time, as a generator:
for execution in pipeline.run_and_poll(sleep=2, location="./rundir", params={"param1": "123"}, profile=["docker", "test"], version="22.0.1"): print("Processing intermediate execution")
By default, an Execution
will be returned every 5 seconds, but you
can adjust this as required with the sleep
paramater. This is useful if you
want to get information about the progress of the pipeline execution as it
proceeds.
If you just want to run a single pipeline without initialising a
Pipeline
object first, you can run
or
run_and_poll
directly, without needing to create a
Pipeline
:
>>> import nextflow >>> execution = nextflow.run(path="pipeline.nf", config="settings.config", params={"param1": "123"})
13th July, 2022
- Process executions now report their input files as paths.
- Process executions now report all their output files as paths.
- Executions now have properties for their originating pipeline.
- Removed schema functionality.
15th June, 2022
- Process polling now accesses stdout and stderr while process is ongoing.
4th June, 2022
- Allow module-level run methods for directly running pipelines.
- Allow for running pipelines with different Nextflow versions.
- Improved datetime parsing.
- Simplified process execution parsing.
- Fixed concatenation of process executions with no parentheses.
- Tests now check compatability with different Nextflow versions.
21st March, 2022
- Log outputs now have ANSI codes removed.
19th February, 2022
- Execution polling now handles unready execution directory.
- Better detection of failed process executions mid execution.
14th February, 2022
- Added method for running while continuously polling pipeline execution.
- Optimised process execution object creation from file state.
12th January, 2022
- Pipeline command generation no longer applies quotes if there are already quotes.
24th November, 2021
- Fixed Windows file separator issues.
- Renamed NextflowProcess -> ProcessExecution.
3rd November, 2021
- Better handling of missing Nextflow executable.
29th October, 2021
- Renamed nextflow_processes to process_executions.
- Added quotes around paths to handle spaces in paths.
18th October, 2021
- Basic Pipeline object.
- Basic Execution object.
- Basic ProcessExecution object.