1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
.. _tutorialWdl:
WDL Tutorial
============
This tutorial will walk you through writing a workflow in WDL that you can `run
with Toil <runWdl>`__. We're going to write a workflow for `Fizz
Buzz <https://en.wikipedia.org/wiki/Fizz_buzz>`__.
Writing the File
----------------
Let's go step by step through the process of creating a single-file WDL workflow.
Version
~~~~~~~
All WDL files need to start with a ``version`` statement (unless they
are very old ``draft-2`` files). Toil supports ``draft-2``, WDL 1.0, and
WDL 1.1, while Cromwell (another popular WDL runner used on Terra)
supports only ``draft-2`` and 1.0.
So let's start a new WDL 1.0 workflow. Open up a file named
``fizzbuzz.wdl`` and start with a version statement:
.. code-block::
version 1.0
Workflow Block
~~~~~~~~~~~~~~
Then, add an empty ``workflow`` named ``FizzBuzz``.
.. code-block::
version 1.0
workflow FizzBuzz {
}
Input Block
~~~~~~~~~~~
Workflows usually need some kind of user input, so let's give our
workflow an ``input`` section.
.. code-block::
version 1.0
workflow FizzBuzz {
input {
# How many FizzBuzz numbers do we want to make?
Int item_count
# Every multiple of this number, we produce "Fizz"
Int to_fizz = 3
# Every multiple of this number, we produce "Buzz"
Int to_buzz = 5
# Optional replacement for the string to print when a multiple of both
String? fizzbuzz_override
}
}
Notice that each input has a type, a name, and an optional default
value. If the type ends in ``?``, the value is optional, and it may be
``null``. If an input is *not* optional, and there is no default value,
then the user's inputs file *must* specify a value for it in order for
the workflow to run.
Body
~~~~
Now we'll start on the body of the workflow, to be inserted just after
the inputs section.
The first thing we're going to need to do is create an array of all the
numbers up to the ``item_count``. We can do this by calling the WDL
``range()`` function, and assigning the result to an ``Array[Int]``
variable.
.. code-block::
Array[Int] numbers = range(item_count)
WDL 1.0 has `a wide variety of functions in its standard
library <https://github.com/openwdl/wdl/blob/main/versions/1.0/SPEC.md#standard-library>`__,
and WDL 1.1 has even more.
Scattering
~~~~~~~~~~
Once we create an array of all the numbers, we can use a ``scatter`` to
operate on each. WDL does not have loops; instead it has scatters, which
work a bit like a ``map()`` in Python. The body of the scatter runs for
each value in the input array, all in parallel. We're going to increment
all the numbers, since FizzBuzz starts at 1 but WDL ``range()`` starts
at 0.
.. code-block::
Array[Int] numbers = range(item_count)
scatter (i in numbers) {
Int one_based = i + 1
}
Conditionals
~~~~~~~~~~~~
Inside the body of the scatter, we are going to put some conditionals to
determine if we should produce ``"Fizz"``, ``"Buzz"``, or
``"FizzBuzz"``. To support our ``fizzbuzz_override``, we use an array of
it and a default value, and use the WDL ``select_first()`` function to
find the first non-null value in that array.
Each execution of a scatter is allowed to declare variables, and outside
the scatter those variables are combined into arrays of all the results.
But each variable can be declared only *once* in the scatter, even with
conditionals. So we're going to use ``select_first()`` at the end and
take advantage of variables from un-executed conditionals being
``null``.
Note that WDL supports conditional *expressions* with a ``then`` and an
``else``, but conditional *statements* only have a body, not an ``else``
branch. If you need an else you will have to check the negated
condition.
So first, let's handle the special cases.
.. code-block::
Array[Int] numbers = range(item_count)
scatter (i in numbers) {
Int one_based = i + 1
if (one_based % to_fizz == 0) {
String fizz = "Fizz"
if (one_based % to_buzz == 0) {
String fizzbuzz = select_first([fizzbuzz_override, "FizzBuzz"])
}
}
if (one_based % to_buzz == 0) {
String buzz = "Buzz"
}
if (one_based % to_fizz != 0 && one_based % to_buzz != 0) {
# Just a normal number.
}
}
Calling Tasks
~~~~~~~~~~~~~
Now for the normal numbers, we need to convert our number into a string.
In WDL 1.1, and in WDL 1.0 on Cromwell, you can use a ``${}``
substitution syntax in quoted strings anywhere, not just in command line
commands. Toil technically will support this too, but it's not in the
spec, and the tutorial needs an excuse for you to call a task. So we're
going to insert a call to a ``stringify_number`` task, to be written
later.
To call a task (or another workflow), we use a ``call`` statement and
give it some inputs. Then we can fish the output values out of the task
with . access, only if we don't make a noise instead.
.. code-block::
Array[Int] numbers = range(item_count)
scatter (i in numbers) {
Int one_based = i + 1
if (one_based % to_fizz == 0) {
String fizz = "Fizz"
if (one_based % to_buzz == 0) {
String fizzbuzz = select_first([fizzbuzz_override, "FizzBuzz"])
}
}
if (one_based % to_buzz == 0) {
String buzz = "Buzz"
}
if (one_based % to_fizz != 0 && one_based % to_buzz != 0) {
# Just a normal number.
call stringify_number {
input:
the_number = one_based
}
}
String result = select_first([fizzbuzz, fizz, buzz, stringify_number.the_string])
}
We can put the code into the workflow now, and set about writing the
task.
.. code-block::
version 1.0
workflow FizzBuzz {
input {
# How many FizzBuzz numbers do we want to make?
Int item_count
# Every multiple of this number, we produce "Fizz"
Int to_fizz = 3
# Every multiple of this number, we produce "Buzz"
Int to_buzz = 5
# Optional replacement for the string to print when a multiple of both
String? fizzbuzz_override
}
Array[Int] numbers = range(item_count)
scatter (i in numbers) {
Int one_based = i + 1
if (one_based % to_fizz == 0) {
String fizz = "Fizz"
if (one_based % to_buzz == 0) {
String fizzbuzz = select_first([fizzbuzz_override, "FizzBuzz"])
}
}
if (one_based % to_buzz == 0) {
String buzz = "Buzz"
}
if (one_based % to_fizz != 0 && one_based % to_buzz != 0) {
# Just a normal number.
call stringify_number {
input:
the_number = one_based
}
}
String result = select_first([fizzbuzz, fizz, buzz, stringify_number.the_string]
}
}
Writing Tasks
~~~~~~~~~~~~~
Our task should go after the workflow in the file. It looks a lot like a
workflow except it uses ``task``.
.. code-block::
task stringify_number {
}
We're going to want it to take in an integer ``the_number``, and we're
going to want it to output a string ``the_string``. So let's fill that
in in ``input`` and ``output`` sections.
.. code-block::
task stringify_number {
input {
Int the_number
}
# ???
output {
String the_string # = ???
}
}
Now, unlike workflows, tasks can have a ``command`` section, which gives
a command to run. This section is now usually set off with triple angle
brackets, and inside it you can use ``~{}``, that is, Bash-like
substitution but with a tilde, to place WDL variables into your command
script. So let's add a command that will echo back the number so we can
see it as a string.
.. code-block::
task stringify_number {
input {
Int the_number
}
command <<<
# This is a Bash script.
# So we should do good Bash script things like stop on errors
set -e
# Now print our number as a string
echo ~{the_number}
>>>
output {
String the_string # = ???
}
}
Now we need to capture the result of the command script. The WDL
``stdout()`` returns a WDL ``File`` containing the standard output
printed by the task's command. We want to read that back into a string,
which we can do with the WDL ``read_string()`` function (which also
`removes trailing
newlines <https://github.com/openwdl/wdl/blob/main/versions/1.0/SPEC.md#string-read_stringstringfile>`__).
.. code-block::
task stringify_number {
input {
Int the_number
}
command <<<
# This is a Bash script.
# So we should do good Bash script things like stop on errors
set -e
# Now print our number as a string
echo ~{the_number}
>>>
output {
String the_string = read_string(stdout())
}
}
We're also going to want to add a ``runtime`` section to our task, to
specify resource requirements. We're also going to tell it to run in a
Docker container, to make sure that absolutely nothing can go wrong with
our delicate ``echo`` command. In a real workflow, you probably want to
set up optiopnal inputs for all the tasks to let you control the
resource requirements, but here we will just hardcode them.
.. code-block::
task stringify_number {
input {
Int the_number
}
command <<<
# This is a Bash script.
# So we should do good Bash script things like stop on errors
set -e
# Now print our number as a string
echo ~{the_number}
>>>
output {
String the_string = read_string(stdout())
}
runtime {
cpu: 1
memory: "0.5 GB"
disks: "local-disk 1 SSD"
docker: "ubuntu:24.04"
}
}
The ``disks`` section is a little weird; it isn't in the WDL spec, but
Toil supports Cromwell-style strings that ask for a ``local-disk`` of a
certain number of gigabytes, which may suggest that it be ``SSD``
storage.
Then we can put our task into our WDL file:
.. code-block::
version 1.0
workflow FizzBuzz {
input {
# How many FizzBuzz numbers do we want to make?
Int item_count
# Every multiple of this number, we produce "Fizz"
Int to_fizz = 3
# Every multiple of this number, we produce "Buzz"
Int to_buzz = 5
# Optional replacement for the string to print when a multiple of both
String? fizzbuzz_override
}
Array[Int] numbers = range(item_count)
scatter (i in numbers) {
Int one_based = i + 1
if (one_based % to_fizz == 0) {
String fizz = "Fizz"
if (one_based % to_buzz == 0) {
String fizzbuzz = select_first([fizzbuzz_override, "FizzBuzz"])
}
}
if (one_based % to_buzz == 0) {
String buzz = "Buzz"
}
if (one_based % to_fizz != 0 && one_based % to_buzz != 0) {
# Just a normal number.
call stringify_number {
input:
the_number = one_based
}
}
String result = select_first([fizzbuzz, fizz, buzz, stringify_number.the_string]
}
}
task stringify_number {
input {
Int the_number
}
command <<<
# This is a Bash script.
# So we should do good Bash script things like stop on errors
set -e
# Now print our number as a string
echo ~{the_number}
>>>
output {
String the_string = read_string(stdout())
}
runtime {
cpu: 1
memory: "0.5 GB"
disks: "local-disk 1 SSD"
docker: "ubuntu:24.04"
}
}
Output Block
~~~~~~~~~~~~
Now the only thing missing is a workflow-level ``output`` section.
Technically, in WDL 1.0 you aren't supposed to need this, but you do
need it in 1.1 and Toil doesn't actually send your outputs anywhere yet
if you don't have one, so we're going to make one. We need to collect
together all the strings that came out of the different tasks in our
scatter into an ``Array[String]``. We'll add the ``output`` section at
the end of the ``workflow`` section, above the task.
.. code-block::
version 1.0
workflow FizzBuzz {
input {
# How many FizzBuzz numbers do we want to make?
Int item_count
# Every multiple of this number, we produce "Fizz"
Int to_fizz = 3
# Every multiple of this number, we produce "Buzz"
Int to_buzz = 5
# Optional replacement for the string to print when a multiple of both
String? fizzbuzz_override
}
Array[Int] numbers = range(item_count)
scatter (i in numbers) {
Int one_based = i + 1
if (one_based % to_fizz == 0) {
String fizz = "Fizz"
if (one_based % to_buzz == 0) {
String fizzbuzz = select_first([fizzbuzz_override, "FizzBuzz"])
}
}
if (one_based % to_buzz == 0) {
String buzz = "Buzz"
}
if (one_based % to_fizz != 0 && one_based % to_buzz != 0) {
# Just a normal number.
call stringify_number {
input:
the_number = one_based
}
}
String result = select_first([fizzbuzz, fizz, buzz, stringify_number.the_string]
}
output {
Array[String] fizzbuzz_results = result
}
}
task stringify_number {
input {
Int the_number
}
command <<<
# This is a Bash script.
# So we should do good Bash script things like stop on errors
set -e
# Now print our number as a string
echo ~{the_number}
>>>
output {
String the_string = read_string(stdout())
}
runtime {
cpu: 1
memory: "0.5 GB"
disks: "local-disk 1 SSD"
docker: "ubuntu:24.04"
}
}
Because the ``result`` variable is defined inside a ``scatter``, when we
reference it outside the scatter we see it as being an array.
Running the Workflow
--------------------
Now all that remains is to run the workflow! Make an inputs file to specify the
workflow inputs::
echo '{"FizzBuzz.item_count": 20}' >fizzbuzz.json
Then run it with Toil. If you are on a Slurm cluster, and you are currently in a shared directory available on all your nodes, you can run::
toil-wdl-runner --jobStore ./fizzbuzz_store --batchSystem slurm --slurmTime 00:10:00 --caching false --batchLogsDir ./logs fizzbuzz.wdl fizzbuzz.json -o fizzbuzz_out -m fizzbuzz_out.json
If instead you want to run your workflow locally, you can run::
toil-wdl-runner fizzbuzz.wdl fizzbuzz.json -o fizzbuzz_out -m fizzbuzz_out.json
Next Steps
----------
- Try breaking your workflow up into multiple files and using ``import`` statements.
- Publish your workflow `on Dockstore`_.
- Read up on Toil-specific WDL development considerations in :ref:`devWdl`.
.. _`on Dockstore`: https://docs.dockstore.org/en/stable/getting-started/dockstore-workflows.html
|