2.7. Outputs#

2.7.1. 出力ファイルの回収#

ツールのoutputs は、ツールの実行後に返されるべき出力パラメータのリストです。 各パラメーターには、パラメーター名を示すid と、そのパラメーターに有効な値の型を示すtype があります。

CWLでツールを実行する場合、実行を開始したディレクトリが出力ディレクトリになります。 基礎となるツールやスクリプトは、出力ディレクトリに作成されるファイルの形でその結果を記録しなければなりません。 CWLツールによって返される出力パラメータは、出力ファイルそのものであるか、またはそれらのファイルの内容を調べることによって得られるものです。

次の例は、tarファイルから抽出されたファイルを返す方法を示しています。

Tip

baseCommand に必須引数を渡します。

これまでの例では、baseCommand は単なる文字列で、引数はCWL入力として渡していました。単一の文字列の代わりに、文字列の配列 を使用できます。 配列の最初の要素は実行するコマンドで、それ以降の要素は必須のコマンドライン引数です

tar.cwl#
#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: [tar, --extract]
inputs:
  tarfile:
    type: File
    inputBinding:
      prefix: --file
outputs:
  example_out:
    type: File
    outputBinding:
      glob: hello.txt
tar-job.yml#
tarfile:
  class: File
  path: hello.tar

次に、サンプルとなるtarファイルを作成します。

$ touch hello.txt && tar -cvf hello.tar hello.txt
hello.txt

そして、今度はコマンドラインにツール定義と入力オブジェクトを指定してcwltool を呼び出します:

$ cwltool tar.cwl tar-job.yml
INFO /home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/envs/latest/bin/cwltool 3.1.20240508115724
INFO Resolved 'tar.cwl' to 'file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/tar.cwl'
INFO [job tar.cwl] /tmp/dq1lwf94$ tar \
    --extract \
    --file \
    /tmp/ufwybv34/stgb95f64ff-6199-4087-8dd8-44f83ac73da5/hello.tar
INFO [job tar.cwl] completed success
{
    "example_out": {
        "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/hello.txt",
        "basename": "hello.txt",
        "class": "File",
        "checksum": "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709",
        "size": 0,
        "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/hello.txt"
    }
}INFO Final process status is success

outputBinding フィールドには、各出力パラメータの値の設定方法が記述されています。

outputs:
  example_out:
    type: File
    outputBinding:
      glob: hello.txt

glob フィールドは、出力ディレクトリ内のファイル名で構成されます。前もって出力ファイル名が分からない場合は、glob: '*.txt' のようにワイルドカードパターンを使用できます。

2.7.2. 標準出力をキャプチャする#

ツールの標準出力を保存するには、stdout フィールドに、標準出力を保存したいファイル名を追加します。 次に、対応する出力パラメータにtype: stdout を追加します。

stdout.cwl#
#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: echo
stdout: output.txt
inputs:
  message:
    type: string
    inputBinding:
      position: 1
outputs:
  example_out:
    type: stdout
echo-job.yml#
message: Hello world!

ここで、コマンドラインにツール定義と入力オブジェクトを指定し cwltool を起動します:

$ cwltool stdout.cwl echo-job.yml
INFO /home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/envs/latest/bin/cwltool 3.1.20240508115724
INFO Resolved 'stdout.cwl' to 'file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/stdout.cwl'
INFO [job stdout.cwl] /tmp/jcmqckdy$ echo \
    'Hello world!' > /tmp/jcmqckdy/output.txt
INFO [job stdout.cwl] completed success
{
    "example_out": {
        "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/output.txt",
        "basename": "output.txt",
        "class": "File",
        "checksum": "sha1$47a013e660d408619d894b20806b1d5086aab03b",
        "size": 13,
        "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/output.txt"
    }
}INFO Final process status is success

2.7.3. 出力の配列#

また、glob を使って、複数の出力ファイルを配列に取り込むことができます。

array-outputs.cwl#
#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: touch
inputs:
  touchfiles:
    type:
      type: array
      items: string
    inputBinding:
      position: 1
outputs:
  output:
    type:
      type: array
      items: File
    outputBinding:
      glob: "*.txt"
array-outputs-job.yml#
touchfiles:
  - foo.txt
  - bar.dat
  - baz.txt

ここで、コマンドラインにツール定義と入力オブジェクトを指定し cwltool を起動します:

$ cwltool array-outputs.cwl array-outputs-job.yml
INFO /home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/envs/latest/bin/cwltool 3.1.20240508115724
INFO Resolved 'array-outputs.cwl' to 'file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/array-outputs.cwl'
INFO [job array-outputs.cwl] /tmp/bec4p4uw$ touch \
    foo.txt \
    bar.dat \
    baz.txt
INFO [job array-outputs.cwl] completed success
{
    "output": [
        {
            "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/baz.txt",
            "basename": "baz.txt",
            "class": "File",
            "checksum": "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709",
            "size": 0,
            "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/baz.txt"
        },
        {
            "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/foo.txt",
            "basename": "foo.txt",
            "class": "File",
            "checksum": "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709",
            "size": 0,
            "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-language-user-guide-ja/checkouts/latest/src/_includes/cwl/outputs/foo.txt"
        }
    ]
}INFO Final process status is success

YAML Guide で説明されているように、期待される出力の配列はarray-outputs-job.yml で指定され、配列のエントリーはそれぞれ先頭の- で示されています。この書き方は、CWL定義で配列のエントリーを示すために使用することもでき、今後のいくつかのセクションでデモがあります。