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

huangapple go评论60阅读模式
英文:

Error when using Snakemake variable in R script

问题

The issue you're encountering is related to how you're trying to access the elements of the snakemake object in your R script. It seems like you're using HTML-encoded characters like " instead of the actual double quotes ".

To fix this, you should use double quotes directly when accessing elements of the snakemake object. For example:

Instead of:

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

Use:

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

Make sure you replace all instances of " with actual double quotes " in your R script where you access elements of the snakemake object. This should resolve the "object not found" error you're encountering.

英文:

I have got the following Snakemake rule:

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"

The part of that R script that the rule runs that gives an error is when I save a variable to a file name in the output section of the snakemake variable:

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

Earlier in the script I access the params part of the snakemake variable in a similar way, but without any errors:

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

When I print the snakemake variable I get this (only partial,relevant output):

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"

I have also saved the work space to a file for debugging as suggest by the snakemake web site. When I load this into R I can do the following things:

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

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

But when the above code is include in the proper script I get the object not found (see above) error.

What am I doing wrong?

答案1

得分: 1

未经测试,但我认为您想要的是:

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


在您的代码中,`snakemake@output[[2]]` 出现为要保存的对象,而不是要保存到的文件。从`?save` 中相关的部分应该是:

用法:

 save(..., list = character(),
      file = stop("必须指定 'file'"),
      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)

参数:

 ...: 要保存的对象的名称(作为符号或字符字符串)。

list: 包含要保存的对象名称的字符向量。

file: 一个可写的二进制模式连接或要保存数据的文件的名称(进行波浪线扩展时)。必须是 'save.image' 或 'version = 1' 的文件名。


<details>
<summary>英文:</summary>

Not tested, but I think you want:

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


In your code, `snakemake@output[[2]]` appears as an object to be saved, not as the file to save to. The relevant bit from `?save` should be:

Usage:

 save(..., list = character(),
      file = stop(&quot;&#39;file&#39; must be specified&quot;),
      ascii = FALSE, version = NULL, envir = parent.frame(),
      compress = isTRUE(!ascii), compression_level,
      eval.promises = TRUE, precheck = TRUE)
 
 save.image(file = &quot;.RData&quot;, 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’.

</details>



huangapple
  • 本文由 发表于 2023年6月26日 15:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定