1. 问题
    2. 如何编写有条件的工作流?
    3. 目标
    4. 学习如何构造包含条件步骤的工作流。

    此工作流包含一个条件步骤,并根据输入执行。这允许工作流基于程序开始时给定的输入参数或前面的步骤跳过额外的步骤。
    conditional-workflow.cwl

    1. class: Workflow
    2. cwlVersion: v1.2
    3. inputs:
    4. val: int
    5. steps:
    6. step1:
    7. in:
    8. in1: val
    9. a_new_var: val
    10. run: foo.cwl
    11. when: $(inputs.in1 < 1)
    12. out: [out1]
    13. step2:
    14. in:
    15. in1: val
    16. a_new_var: val
    17. run: foo.cwl
    18. when: $(inputs.a_new_var > 2)
    19. out: [out1]
    20. outputs:
    21. out1:
    22. type: string
    23. outputSource:
    24. - step1/out1
    25. - step2/out1
    26. pickValue: first_non_null
    27. requirements:
    28. InlineJavascriptRequirement: {}
    29. MultipleInputFeatureRequirement: {}

    您将注意到的第一件事是,此工作流仅适用于版本1.2或更高版本的CWL标准。

    1. class: Workflow
    2. cwlVersion: v1.2

    工作流程的第一步(step1)包含两个输入属性,当条件满足时将执行foo.cwl。新属性when是条件验证发生的位置。在这种情况下,只有当来自工作流的in1包含值< 1时,该步骤才会执行。

    1. steps:
    2. step1:
    3. in:
    4. in1: val
    5. a_new_var: val
    6. run: foo.cwl
    7. when: $(inputs.in1 < 1)
    8. out: [out1]

    使用如下命令cwltool cond-wf-003.1.cwl —val 0,该值将通过第一个条件步,并因此被执行,并在日志中通过INFO [step step1] start显示,而第二个步骤被跳过,因为INFO [step step2]将被跳过。

    1. INFO [workflow ] start
    2. INFO [workflow ] starting step step1
    3. INFO [step step1] start
    4. INFO [job step1] /private/tmp/docker_tmpdcyoto2d$ echo
    5. INFO [job step1] completed success
    6. INFO [step step1] completed success
    7. INFO [workflow ] starting step step2
    8. INFO [step step2] will be skipped
    9. INFO [step step2] completed skipped
    10. INFO [workflow ] completed success
    11. {
    12. "out1": "foo 0"
    13. }
    14. INFO Final process status is success

    当给出值3时,第一个条件步骤将不会执行,但第二个步骤将cwltool cond-wf-003.1.cwl —val 3。

    1. INFO [workflow ] start
    2. INFO [workflow ] starting step step1
    3. INFO [step step1] will be skipped
    4. INFO [step step1] completed skipped
    5. INFO [workflow ] starting step step2
    6. INFO [step step2] start
    7. INFO [job step2] /private/tmp/docker_tmpqwr93mxx$ echo
    8. INFO [job step2] completed success
    9. INFO [step step2] completed success
    10. INFO [workflow ] completed success
    11. {
    12. "out1": "foo 3"
    13. }
    14. INFO Final process status is success

    如果不满足任何条件,例如在使用——val 2时,工作流将引发一个permanentFail。

    1. cwltool cond-wf-003.1.cwl --val 2
    2. INFO [workflow ] start
    3. INFO [workflow ] starting step step1
    4. INFO [step step1] will be skipped
    5. INFO [step step1] completed skipped
    6. INFO [workflow ] starting step step2
    7. INFO [step step2] will be skipped
    8. INFO [step step2] completed skipped
    9. ERROR [workflow ] Cannot collect workflow output: All sources for 'out1' are null
    10. INFO [workflow ] completed permanentFail
    11. WARNING Final process status is permanentFail