Cri*_*uñí 4 bash bioinformatics nextflow
我有一个 nextflow 脚本,它从文本文件创建一个变量,我需要将该变量的值传递给命令行命令(这是一个 bioconda 包)。这两个过程发生在“脚本”部分内。我尝试使用“$”符号调用变量,但没有任何结果,我认为因为在 nextflow 脚本的脚本部分中使用该符号是为了调用输入部分中定义的变量。
为了让自己更清楚,这里是我想要实现的目标的代码示例:
params.gz_file = '/path/to/file.gz'
params.fa_file = '/path/to/file.fa'
params.output_dir = '/path/to/outdir'
input_file = file(params.gz_file)
fasta_file = file(params.fa_file)
process foo {
//publishDir "${params.output_dir}", mode: 'copy',
input:
path file from input_file
path fasta from fasta_file
output:
file ("*.html")
script:
"""
echo 123 > number.txt
parameter=`cat number.txt`
create_report $file $fasta --flanking $parameter
"""
}
Run Code Online (Sandbox Code Playgroud)
通过这样做,我收到的错误是:
Error executing process > 'foo'
Caused by:
Unknown variable 'parameter' -- Make sure it is not misspelt and defined somewhere in the script before using it
Run Code Online (Sandbox Code Playgroud)
有没有办法parameter在脚本中调用变量而不需要 Nextflow 将其解释为输入文件?提前致谢!
脚本块的文档在这里很有用:
由于 Nextflow 使用相同的 Bash 语法进行字符串中的变量替换,因此您需要仔细管理它们,具体取决于您是否要在 Nextflow 上下文中或在 Bash 环境执行中评估变量。
一种解决方案是通过在 shell (Bash) 变量前添加反斜杠 ( \) 字符来转义它们,如下例所示:
process foo {
script:
"""
echo 123 > number.txt
parameter="\$(cat number.txt)"
echo "\${parameter}"
"""
}
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是使用shell 块,其中美元 ( $) 变量由 shell(Bash 解释器)管理,而感叹号 ( !) 变量由 Nextflow 处理。例如:
process bar {
echo true
input:
val greeting from 'Hello', 'Hola', 'Bonjour'
shell:
'''
echo 123 > number.txt
parameter="$(cat number.txt)"
echo "!{greeting} parameter ${parameter}"
'''
}
Run Code Online (Sandbox Code Playgroud)