如何将通配符插入shell命令?

时间:2018-10-09 17:57:32

标签: snakemake

我正在尝试建立一个Snakemake管道,但是我困惑为什么文件名通配符可用于inputoutput而不适用于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命令中命名输出文件。那么如何在{sample}命令中使用我的文件名通配符(在此处为shell)?

例如,以下操作无效:

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 $1}}

我该如何解决?



(或者,如果您真的想看一些真实的代码,这就是我正在使用的代码):

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命令中。

1 个答案:

答案 0 :(得分:2)

通配符可通过{wildcards.XXXX}使用。 source

rule align:
    input: "{sample}.txt"
    output: "{sample}.out"
    shell: "touch {wildcards.sample}.out"