| 1 |
Advanced Task/ToolContract Types |
| 2 |
================================ |
| 3 |
|
| 4 |
|
| 5 |
To enable pipeline scaling, "Chunking" of files two new Tool Contract types extend the base Tool Contract data model. |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
Scattering/Chunking Tool Contract |
| 10 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 11 |
|
| 12 |
|
| 13 |
Tasks/ToolContract that take a any file type(s) and emit a **single** scatter.chunk.json file. |
| 14 |
|
| 15 |
|
| 16 |
At a high level, the Scatter Tool Contract data model extends the core Tool Contract model and adds two fields, `chunk_keys` and `nchunks`. |
| 17 |
|
| 18 |
- `chunk_keys` is the expected key(s) that will be written to the `PipelineChunk` data model (defined below) |
| 19 |
- `nchunks` mirrors the `nproc` model of using a symbol `$max_nchunks` or an int to define the absolute upper bound on the number of chunks that should be created. If this value is exceeded, the pipeline engine will immediately fail the execution. |
| 20 |
|
| 21 |
|
| 22 |
Example Tool Contract |
| 23 |
|
| 24 |
.. literalinclude:: ../../tests/data/tool-contracts/dev_scatter_fasta_app_tool_contract.json |
| 25 |
:language: javascript |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
PipelineChunk Data Model |
| 30 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 31 |
|
| 32 |
|
| 33 |
The `PipelineChunk` data model is defined in `pbcommand.models` and the companion IO layers (`load_pipeline_chunks_from_json` and `write_pipeline_chunks` are in `pbcommand.pb_io`. |
| 34 |
|
| 35 |
Each input file **must** be mapped to a `chunk_key` that can then be mapped to the input of the original `unchunked` task. |
| 36 |
|
| 37 |
For example, if there's a single input file (e.g., FileTypes.FASTA), then the Scatter ToolContract should define a `chunk_key` of "fasta_id". `chunk_key`(s) that do NOT start with `$chunk.` will considered to be extra metadata that will be passed through. This is useful for adding chunk specific metadata, such as the number of contigs or average contig length. |
| 38 |
|
| 39 |
Minimal example of reading and writing `PipelineChunk(s)` data model. |
| 40 |
|
| 41 |
.. ipython:: |
| 42 |
|
| 43 |
|
| 44 |
In [1]: from pbcommand.models import PipelineChunk |
| 45 |
|
| 46 |
|
| 47 |
In [5]: c0 = PipelineChunk("scattered-fasta_0", **{"$chunk.fasta_id":"/path/to/chunk-0.fasta"}) |
| 48 |
|
| 49 |
In [6]: c1 = PipelineChunk("scattered-fasta_1", **{"$chunk.fasta_id":"/path/to/chunk-1.fasta"}) |
| 50 |
|
| 51 |
In [7]: chunks = [c0, c1] |
| 52 |
|
| 53 |
In [8]: from pbcommand.pb_io import write_pipeline_chunks |
| 54 |
|
| 55 |
In [10]: write_pipeline_chunks(chunks, "test-scatter.chunk.json", "Test comment") |
| 56 |
|
| 57 |
In [11]: from pbcommand.pb_io import load_pipeline_chunks_from_json |
| 58 |
|
| 59 |
In [12]: load_pipeline_chunks_from_json("test-scatter.chunk.json") |
| 60 |
Out[12]: |
| 61 |
[<PipelineChunk id='scattered-fasta_0' chunk keys=$chunk.fasta_id >, |
| 62 |
<PipelineChunk id='scattered-fasta_1' chunk keys=$chunk.fasta_id >] |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
Defining a Scatter Tool Contract |
| 69 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 70 |
|
| 71 |
Currently, python is the only language that is supported for writing CHUNK JSON files. |
| 72 |
|
| 73 |
The python Scatter tool contract API follows similar to base Tool Contract API, |
| 74 |
|
| 75 |
|
| 76 |
Simple example of Scattering/Chunking a single Fasta file. The notable points are adding the required `chunk_keys` and `nchunks` to the scattering specific pbparser. |
| 77 |
|
| 78 |
|
| 79 |
.. literalinclude:: ../../pbcommand/cli/examples/dev_scatter_fasta_app.py |
| 80 |
:language: python |
| 81 |
|
| 82 |
|
| 83 |
Advanced Scattering/Chunking Patterns |
| 84 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 85 |
|
| 86 |
For more advanced scattering/chunks usecases, such as chunking multiple input files (e.g., task input signature SubreadSet and ReferenceSet XML), this will require writing a chunk key for each input. Specifically, `$chunk.subreadset_id` and `$chunk.referenceset_id` to the `PipelineChunk`. |
| 87 |
|
| 88 |
This enables the chunking pattern of a specific task to be completely decoupled from the workflow level. The chunking pattern is communicated in the chunk(s) in `PipelineChunk` defined by the chunking task. In this specific chunking pattern, the SubreadSet is chunked into N files, while the ReferenceSet is passed unchunked. |
| 89 |
|
| 90 |
These chunk keys combined with the chunk operator (defined in pbsmrtpipe_) communicates to the workflow engine how to pass `$chunk.subreadset_id` to the first input of N-chunked instances of unchunked task. Similarly, the `$chunk.referenceset_id` to the second input of the N-chunked task instance. |
| 91 |
|
| 92 |
|
| 93 |
See the pbsmrtpipe_ docs and the testkit-data jobs in pbsmrtpipe for more details. |
| 94 |
|
| 95 |
|
| 96 |
Gather ToolContract |
| 97 |
~~~~~~~~~~~~~~~~~~~ |
| 98 |
|
| 99 |
|
| 100 |
A Gather Tool Contract takes a **single** CHUNK Json file type as input and emits a **single** output file of any type. |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
Example: |
| 105 |
|
| 106 |
.. literalinclude:: ../../tests/data/tool-contracts/dev_gather_fasta_app_tool_contract.json |
| 107 |
:language: javascript |
| 108 |
|
| 109 |
|
| 110 |
The Gather task doesn't extend the base ToolContract and add new properties. However, it will restrict the the input type to `FileTypes.CHUNK` and the output type signature **must only be one file type**. |
| 111 |
|
| 112 |
|
| 113 |
Example Gather Tool: |
| 114 |
|
| 115 |
.. literalinclude:: ../../pbcommand/cli/examples/dev_gather_fasta_app.py |
| 116 |
:language: python |
| 117 |
|
| 118 |
|
| 119 |
For Gather'ing a task that has a multiple N outputs, N gather tasks must be defined. |
| 120 |
|
| 121 |
See the pbsmrtpipe_ docs for details of constructing a chunked pipeline. |
| 122 |
|
| 123 |
|
| 124 |
More examples of scatter/chunking and gather tasks are in pbcoretools_. |
| 125 |
|
| 126 |
.. _pbsmrtpipe: http://pbsmrtpipe.readthedocs.io |
| 127 |
|
| 128 |
.. _pbcoretools: https://github.com/PacificBiosciences/pbcoretools/tree/master/pbcoretools/tasks |