2.6. 参数引用#
在前面的例子中,我们使用 “tar” 程序提取了一个文件。但是,该示例局限性很强,因为它假定了我们感兴趣的文件名就是 “hello.txt”, 而且这种假设直接写入了 .cwl
代码文件里。这种做法有欠妥当,因为文件名 “hello.txt” 可能会改变,也可能依赖我们实际用到的输入文件。 为了避免这类情况,我们可以在作业参数 (.yml
) 文件中指定我们想要的文件名。本示例将演示如何从其他字段动态引用输入参数值,以便指定待提取的文件名。
tar-param.cwl
##!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: [tar, --extract]
inputs:
tarfile:
type: File
inputBinding:
prefix: --file
extractfile:
type: string
inputBinding:
position: 1
outputs:
extracted_file:
type: File
outputBinding:
glob: $(inputs.extractfile)
tar-param-job.yml
#tarfile:
class: File
path: hello.tar
extractfile: goodbye.txt
创建输入文件,然后以工具描述和输入对象为命令行参数调用 cwltool
:
$ rm hello.tar || true && touch goodbye.txt && tar -cvf hello.tar goodbye.txt
$ cwltool tar-param.cwl tar-param-job.yml
INFO /opt/hostedtoolcache/Python/3.9.19/x64/bin/cwltool 3.1.20240508115724
INFO Resolved 'tar-param.cwl' to 'file:///home/runner/work/user_guide/user_guide/src/_includes/cwl/parameter-references/tar-param.cwl'
INFO [job tar-param.cwl] /tmp/bbjpnstt$ tar \
--extract \
--file \
/tmp/h43yxj9h/stgee5b8f5e-3e9b-4e7b-9372-83f325226452/hello.tar \
goodbye.txt
INFO [job tar-param.cwl] completed success
{
"extracted_file": {
"location": "file:///home/runner/work/user_guide/user_guide/src/_includes/cwl/parameter-references/goodbye.txt",
"basename": "goodbye.txt",
"class": "File",
"checksum": "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709",
"size": 0,
"path": "/home/runner/work/user_guide/user_guide/src/_includes/cwl/parameter-references/goodbye.txt"
}
}INFO Final process status is success
某些字段允许出现用 $(...)
符号括起来的参数引用,由求值的结果代入。
outputs:
extracted_out:
type: File
outputBinding:
glob: $(inputs.extractfile)
引用的编写用的是 JavaScript 语法的一个子集。 本例中,$(inputs.extractfile)
、$(inputs["extractfile"])
和 $(inputs['extractfile'])
效果相同。
“inputs” 变量的取值为 CWL 工具被调用时获得的输入对象。
请注意,由于 File
(文件)参数是对象。因此,要获取输入文件的路径,须引用文件对象上的“path”(路径)字段。例如,要引用上面例子中 tar 文件的路径,则需要写为 $(inputs.tarfile.path)
.
参数引用可以出现在哪些地方?
参数引用只能出现在有限的字段中,即:
-
arguments
valueFrom
stdin
stdout
stderr
-
format
secondaryFiles
来自
inputBinding
valueFrom
-
format
secondaryFiles
-
glob
outputEval
From
Workflow
来自InputParameter 和 WorkflowOutputParameter
format
secondaryFiles
来自
steps
-
valueFrom
-
-
expression
来自 InputParameter 和 ExpressionToolOutputParameter
format
secondaryFiles
-
coresMin
coresMax
ramMin
ramMax
tmpdirMin
tmpdirMax
outdirMin
outdirMax
-
listing
Dirent 对象中
entry
entryname
来自
EnvVarRequirement
-
envValue
-