如何将通配符插入到 shell 命令中?
How do I interpolate wildcards into a shell command?
我正在尝试构建 Snakemake 管道,但我很困惑为什么文件名通配符适用于 input
和 output
,但不适用于 shell
。例如,以下工作正常:
samplelist=[ "aa_S1", "bb_S2"]
rule all:
input: expand("{sample}.out", sample=samplelist)
rule align:
input: "{sample}.txt"
output: "{sample}.out"
shell: "touch {output}"
但是假设我用于 shell
的命令实际上是从我给它的字符串派生的,所以我不能直接在 shell
命令中命名输出文件。那么如何在 shell
命令中使用我的文件名通配符(此处 {sample}
)?
例如,以下内容不起作用:
samplelist=[ "aa_S1", "bb_S2"]
rule all:
input: expand("{sample}.out", sample=samplelist)
rule align:
input: "{sample}.txt"
output: "{sample}.out"
shell: "touch {sample}.out"
它给我以下错误:
RuleException in line 6 of Snakefile:
NameError: The name 'sample' is unknown in this context. Please make sure
that you defined that variable. Also note that braces not used for variable
access have to be escaped by repeating them, i.e. {{print }}
我该如何解决这个问题?
(或者如果你真的想看一些真实的代码,这就是我正在使用的代码):
samplelist=[ "aa_S1", "bb_S2"]
rule all:
input: expand("{sample}_aligned.sam", sample=samplelist)
rule align:
input: "{sample}_R1_001.trimmed.fastq.gz",
"{sample}_R2_001.trimmed.fastq.gz"
output: "{sample}_aligned.sam"
threads: 4
shell: "STAR --outFileNamePrefix {sample}_aligned --readFilesIn {input[0]} {input[1]} --readFilesCommand zcat --runMode alignReads --runThreadN {threads} --genomeDir /path/to/StarIndex"
但是报错信息基本一样。对于 shell
,我可以使用 {input}
、{output}
和 {threads}
,但不能使用 {sample}
。
我确实看过 Snakemake: How do I use a function that takes in a wildcard and returns a value?,但那似乎侧重于生成输入文件名。我的问题涉及将文件名通配符插值到 shell
命令中。
通配符可通过 {wildcards.XXXX}
获得。 source
rule align:
input: "{sample}.txt"
output: "{sample}.out"
shell: "touch {wildcards.sample}.out"
我正在尝试构建 Snakemake 管道,但我很困惑为什么文件名通配符适用于 input
和 output
,但不适用于 shell
。例如,以下工作正常:
samplelist=[ "aa_S1", "bb_S2"]
rule all:
input: expand("{sample}.out", sample=samplelist)
rule align:
input: "{sample}.txt"
output: "{sample}.out"
shell: "touch {output}"
但是假设我用于 shell
的命令实际上是从我给它的字符串派生的,所以我不能直接在 shell
命令中命名输出文件。那么如何在 shell
命令中使用我的文件名通配符(此处 {sample}
)?
例如,以下内容不起作用:
samplelist=[ "aa_S1", "bb_S2"]
rule all:
input: expand("{sample}.out", sample=samplelist)
rule align:
input: "{sample}.txt"
output: "{sample}.out"
shell: "touch {sample}.out"
它给我以下错误:
RuleException in line 6 of Snakefile:
NameError: The name 'sample' is unknown in this context. Please make sure
that you defined that variable. Also note that braces not used for variable
access have to be escaped by repeating them, i.e. {{print }}
我该如何解决这个问题?
(或者如果你真的想看一些真实的代码,这就是我正在使用的代码):
samplelist=[ "aa_S1", "bb_S2"]
rule all:
input: expand("{sample}_aligned.sam", sample=samplelist)
rule align:
input: "{sample}_R1_001.trimmed.fastq.gz",
"{sample}_R2_001.trimmed.fastq.gz"
output: "{sample}_aligned.sam"
threads: 4
shell: "STAR --outFileNamePrefix {sample}_aligned --readFilesIn {input[0]} {input[1]} --readFilesCommand zcat --runMode alignReads --runThreadN {threads} --genomeDir /path/to/StarIndex"
但是报错信息基本一样。对于 shell
,我可以使用 {input}
、{output}
和 {threads}
,但不能使用 {sample}
。
我确实看过 Snakemake: How do I use a function that takes in a wildcard and returns a value?,但那似乎侧重于生成输入文件名。我的问题涉及将文件名通配符插值到 shell
命令中。
通配符可通过 {wildcards.XXXX}
获得。 source
rule align:
input: "{sample}.txt"
output: "{sample}.out"
shell: "touch {wildcards.sample}.out"