在我的 Snakemake 项目中,我有一个 config.yaml 文件,它允许用户运行或不运行管道的某些步骤,例如:
DEG :
exec : True
Run Code Online (Sandbox Code Playgroud)
因此,在 Snakefile 中,我包含了与 DEG 相关的规则:
if config["DEG"]["exec"]:
include: "rules/classic_mapping.smk"
include: "rules/counts.smk"
include: "rules/run_DESeq2.smk"
Run Code Online (Sandbox Code Playgroud)
问题是,现在我想在“all”规则中动态指定输出文件,以便Snakemake知道根据用户输入的参数生成哪些文件。例如,我想按如下方式进行:
rule all:
input:
if config["DEG"]["exec"]:
"DEG/DEG.txt"
if config["DTU"]["exec"]:
"DTU/DTU.txt"
Run Code Online (Sandbox Code Playgroud)
但它不起作用:如果在规则定义中,则 Unexpected 关键字的第 58 行出现 SyntaxError (Snakefile,第 58 行)
我需要外部观点来找到替代方案,因为 Snakemake 不应该以这种方式工作
提前致谢
问题:
我有一个很大的工作流程,它在某个时刻创建任意数量的文件{sample},命名为例如test1.txt,test2.txt等。
然后我需要使用这些文件进行进一步处理。下一条规则的输入文件是{sample}/test1.txt、{sample}/test2.txt等。因此test1,test2等成为通配符。
数据结构为:
---data
---sample1
---test1.txt
---test2.txt
---test3.txt
---sample2
---test1.txt
---test2.txt
Snakefile
Run Code Online (Sandbox Code Playgroud)
我正在努力如何使用snakemake来解决此类问题。我研究过该函数glob_wildcards,但不知道如何使用它。
直觉上,我会做这样的事情:
samples = ['sample1', 'sample2']
rule append_hello:
input:
glob_wildcards('data/{sample}/{id}.txt')
output:
'data/{sample}/{id}_2.txt'
shell:
" echo {input} 'hello' >> {output} "
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
rule all来运行它。任何进一步阅读的意见或提示将不胜感激。
我认为这与通配符限制有关。当我跑步时:
assemblies = []
for filename in glob_wildcards(os.path.join("data/{sample}", "{i}.txt")):
assemblies.append(filename)
print(assemblies)
Run Code Online (Sandbox Code Playgroud)
我得到两个相应索引匹配的列表:
[['sample1', 'sample1', 'sample1', 'sample2', 'sample2'], ['test1', 'test2', 'test3', 'test5', …Run Code Online (Sandbox Code Playgroud) 有没有什么方法可以轻松保存来自snakemake规则使用script指令执行python脚本的日志?该脚本使用的库已经有一些集成的日志记录,我想存储它们的日志。我不想使用shell或run指令,因为在使用 python 脚本时它们都不太舒服。另外,我希望答案会要求对 python 脚本进行最小的更改,主要更改在文件中Snakemake。这是代码示例:
rule correcting_ids:
input:
"assembly_{type}/{sample}/{dataset}/final.fasta"
output:
"assembly_{type}/{sample}/{dataset}/final.corrected.fasta"
log:
"logs/{sample}/correct_{type}_ids_{dataset}.log"
script:
"scripts/correcting_ids.py"```
Run Code Online (Sandbox Code Playgroud) 我发现 Snakemake文档非常简洁。因为我被问过几次,所以我想发布这个问题和我自己的答案。
如何在 Snakemake 规则中执行或集成此 R 脚本?
my-script.R
CUTOFF <- 25
dat <- read.table('input-table.txt')
out <- dat[dat$V1 > CUTOFF, ]
write.table(out, 'output-table.txt')
Run Code Online (Sandbox Code Playgroud)
蛇文件:
rule one:
input:
txt= 'input-table.txt',
output:
out= 'output-table.txt',
params:
cutoff= 25,
# now what?
Run Code Online (Sandbox Code Playgroud) 我需要处理输入文件值,将它们转换为逗号分隔的字符串(而不是空格),以便将它们传递给 CLI 程序。为此,我想通过 Python 函数运行输入文件。如何在同一规则的 params 部分引用该规则的输入文件?
这是我尝试过的,但它不起作用:
rule a:
input:
foo="a.txt",
bar=expand({build}.txt,build=config["build"]),
output:
baz=result.txt,
params:
joined_bar=lambda w: ",".join(input.bar), # this doesn't work
shell:
"""
qux --comma-separated-files {params.joined_bar} \
--foo {input.foo} \
>{output.baz}
"""
Run Code Online (Sandbox Code Playgroud)
它失败了:
InputFunctionException:
AttributeError: 'builtin_function_or_method' object has no attribute 'bar'
Run Code Online (Sandbox Code Playgroud)
潜在相关但(过于)复杂的问题:
如何使用扩展输入定义 Snakemake 规则的参数
Is Snakemake params 函数在输入文件存在之前评估?
标准化工作流程中的 Snakemake 规则使用指令运行 Python 脚本script,例如此模板规则:
rule XXXXX:
input:
...,
output:
....,
params:
...,
conda:
"../envs/python.yaml"
script:
"../scripts/XXXX.py"
Run Code Online (Sandbox Code Playgroud)
然后在脚本中,可以使用snakemake对象。然而,脚本与该规则紧密耦合,这似乎是一个很大的缺点。
为什么这种方法比使用调用脚本的 shell 的方法更受青睐,例如在本规则中?
rule XXXXX:
input:
...,
output:
....,
params:
absolute_script_path = ..., # get
argument1 = ...,
conda:
"../envs/python.yaml"
shell:
"python {params.absolute_script_path} {input} {params.argument1} > {output}"
Run Code Online (Sandbox Code Playgroud)
在这种方法中,python 脚本与 Snakemake 规则解耦。而且它看起来更有凝聚力,因为调用的参数在规则中很清楚,而不是隐藏在脚本中。我刚刚开始编写 Snakemake 工作流程,所以我只是一个初学者。我不明白为什么第一种方法比第二种方法更受青睐(或在标准化 Snakemake 工作流程中使用)?我错过了什么吗?第二种方法有问题吗?非常感谢您的解答!
我刚刚开始使用snakemake并且想知道在同一个文件上运行一组参数的"正确"方法是什么以及这对于规则的链接有什么作用?
因此,例如,当我想要多个规范化方法时,接下来让我们说一个具有不同数量的k个聚类的聚类规则.这样做的最佳方法是什么,以便运行所有组合?
我开始这样做:
INFILES = ["mytable"]
rule preprocess:
input:
bam=expand("data/{sample}.csv", sample=INFILES, param=config["normmethod"])
output:
bamo=expand("results/{sample}_pp_{param}.csv", sample=INFILES, param=config["normmethod"])
script:
"scripts/preprocess.py"
Run Code Online (Sandbox Code Playgroud)
然后通过以下方式调用脚本:
snakemake --config normmethod =中位数
但是,在工作流程的后期,这并没有真正扩展到更多选项.例如,我如何自动合并这些选项?
normmethods= ["Median", "Quantile"]
kclusters= [1,3,5,7,10]
Run Code Online (Sandbox Code Playgroud) 有没有办法在.yaml文件中定义snakemake配置字符串,以便它可以包含{wildcard}和{param}值,并且当在shell命令中使用该字符串时,{<name>}值将替换为"<name>"的实际值?
例如,假设您希望配置字符串定义要作为参数传递给程序的字符串格式:
RG:"ID:{ID} REP:{REP}"
其中上面是.yaml文件,ID和REP是通配符,shell命令会将展开的字符串作为参数传递给程序.
我从 github 下载了一个 conda 包进行了一些修改,并想在 conda 环境中构建这个本地包并测试我的更改。问题是配方的构建失败了,因为 conda 有一个conda.exceptions.ResolvePackageNotFound错误,指出它没有检测到 2 个包,snakemake而fuzzywuzzy.
这是 meta.yaml 文件:
package:
name: snakepipes
version: 1.2.1
source:
path: ../
build:
number: 0
noarch: python
requirements:
build:
- python >=3
run:
- python >=3
- pandas
- graphviz
- pyyaml >=5.1
- wget
- snakemake >=5.2.3
- fuzzywuzzy
test:
commands:
- DNA-mapping --help
about:
home: 'https://snakepipes.readthedocs.org'
license: GPL3
summary: NGS processing pipelines from the MPI-IE
license_file: LICENSE.txt
Run Code Online (Sandbox Code Playgroud)
我试图将其更改noarch为“通用”,在requirements: build …
在执行量化(kallisto/salmon)后,我正在使用一些快速的 R 脚本来 cbind 文件。问题是,我收到一个 R 错误,说我的输入文件长度不同,所以 cbind() 不起作用。
这显然不是这种情况,它们都是 16887 行(用 bash wc 检查),并且在没有snakenmake 的 R 中工作得很好。
还值得一提的是,我确实得到了随机数样本(~ 1 到 4)的输出。
这是R代码:
sample <- read.table(snakemake@input[[1]], sep = "\t", header = TRUE)
if (!file.exists(snakemake@params[[1]])) {
merge <- data.frame(sample)
} else {
merge1 <- read.table(snakemake@params[[1]], sep = "\t", header = TRUE)
merge <- cbind(merge1, sample[,2, drop=F])
}
write.table(merge, snakemake@params[[1]], sep = "\t", quote = F, row.names = F)
file.create(snakemake@output[[1]])
Run Code Online (Sandbox Code Playgroud)
而蛇形规则:
rule merge_quantif:
input:
QUANTIF+"/{sample}/quantif.txt"
output:
QUANTIF+"/{sample}/merge.done"
params:
QUANTIF+"/all_sample_quantified.txt"
script:
"Tools/merge_quantif.R"
Run Code Online (Sandbox Code Playgroud)
我的文件是这样的: …