在R脚本中使用Snakemake变量时出错

a7qyws3x  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(124)

下面是Snakemake规则:

rule deseq2:
    input:
        salmon=expand("salmon/{sample}/quant.sf", sample=SAMPLES)
    output:
        xlsx="deseq2/deseq2_diff_trx.xlsx",
        rdata="deseq2/dds.Rdata",
    params:
        map_with,
        genome,
        gtf,
    log:
        "logs/deseq2/deseq2.log"
    conda:
        "envs/deseq2.yml"
    threads: config["resources"]["deseq2"]["cpu"]
    resources: 
        runtime=config["resources"]["deseq2"]["time"]
    script:
        "scripts/deseq2.R"

规则运行的R脚本中给出错误的部分是当我将变量保存到snakemake变量的输出部分中的文件名时:

save(dds, snakemake@output[[2]])
Error in save(dds, snakemake@output[[2]]) : 
  object ‘snakemake@output[[2]]’ not found

在前面的脚本中,我以类似的方式访问snakemake变量的params部分,但没有任何错误:

map.with <- snakemake@params[[1]]
genome <- snakemake@params[[2]]
gtf <- snakemake@params[[3]]

当我打印snakemake变量时,我得到了以下内容(只有部分相关输出):

An object of class "Snakemake"
Slot "input":
[[1]]
[1] "salmon/Control-1/quant.sf"

[[2]]
[1] "salmon/Control-2/quant.sf"

[[3]]
[1] "salmon/Control-Hypoxia-1/quant.sf"

[[4]]
[1] "salmon/Control-Hypoxia-2/quant.sf"

$salmon
[1] "salmon/Control-1/quant.sf"         "salmon/Control-2/quant.sf"        
[3] "salmon/Control-Hypoxia-1/quant.sf" "salmon/Control-Hypoxia-2/quant.sf"

Slot "output":
[[1]]
[1] "deseq2/deseq2_diff_trx.xlsx"

[[2]]
[1] "deseq2/dds.Rdata"

$xlsx
[1] "deseq2/deseq2_diff_trx.xlsx"

$rdata
[1] "deseq2/dds.Rdata"

Slot "params":
[[1]]
[1] "salmon"

[[2]]
[1] "hg38"

[[3]]
[1] "/home/user/Documents/references/gtf/hg38/gencode.v43.annotation.gtf"

我还按照snakemake网站的建议,将工作空间保存到一个文件中,以便进行调试。当我把它加载到R中时,我可以做以下事情:

> snakemake@output[["rdata"]]
[1] "deseq2/dds.Rdata"

> snakemake@output[[2]]
[1] "deseq2/dds.Rdata"

但是当上面的代码包含在正确的脚本中时,我得到了对象未找到(见上文)错误。
我做错了什么?

lc8prwob

lc8prwob1#

没有测试,但我认为你想要:

save(dds, file = snakemake@output[[2]])

在代码中,snakemake@output[[2]]显示为要保存的对象,而不是要保存到的文件。?save的相关位应为:

Usage:

     save(..., list = character(),
          file = stop("'file' must be specified"),
          ascii = FALSE, version = NULL, envir = parent.frame(),
          compress = isTRUE(!ascii), compression_level,
          eval.promises = TRUE, precheck = TRUE)
     
     save.image(file = ".RData", version = NULL, ascii = FALSE,
                compress = !ascii, safe = TRUE)
     
Arguments:

     ...: the names of the objects to be saved (as symbols or character
          strings).

    list: A character vector containing the names of objects to be
          saved.

    file: a (writable binary-mode) connection or the name of the file
          where the data will be saved (when tilde expansion is done).
          Must be a file name for ‘save.image’ or ‘version = 1’.

相关问题