根据字典输入和输出零食



我正在尝试重命名snakemake管道中的一些文件。假设我有三个文件:"FileA.txt""FileB.txt""FileC.txt",我希望根据字典dict = {"A": "0", "B": "1", "C": "2"}重命名它们,得到"RenamedFile0.txt""RenamedFile1.txt""RenamedFile2.txt"。人们将如何为此制定规则?

这就是我的管道的样子(我尝试过一个功能,但不起作用(

SAMPLES = ["A", "B", "C"]
RENAMED_SAMPLES = ["0", "1", "2"]
rename = {"0": "A", "1": "B", "2": "C"}
def mapFile(wildcards):
file = "results/EditedFile" + str(rename[wildcards]) + ".txt"
return(file)
rule all:
input:
"results/Combined.txt"
rule cut:
input:
"data/File{sample}.txt"
output:
"results/EditedFile{sample}.txt"
shell:
"cut -f1 {input} > {output}"
rule rename:
input:
mapFile
output:
"results/RenamedFile{renamedSample}.txt"
shell:
"cp {input} {output}"

rule combine:
input:
expand("results/RenamedFile{renamedSample}.txt", renamedSample = RENAMED_SAMPLES)
output:
"results/Combined.txt"
shell:
"cat {input} > {output}"

我得到以下错误:

KeyError: ['2']
Wildcards:
renamedSample=2

谢谢!!!

运行自定义扩展时,应指定通配符的名称:

def mapFile(wildcards):
file = "results/EditedFile" + rename[wildcards.renamedSample] + ".txt"
return(file)

在这种特定情况下,也可以将逻辑集成到规则本身中:

rule rename:
input:
lambda wildcards: f"results/EditedFile{rename[wildcards.renamedSample]}.txt"
output:
"results/RenamedFile{renamedSample}.txt"
shell:
"cp {input} {output}"

相关内容

  • 没有找到相关文章

最新更新