How to use dict value and key in Snakemake rules

雨燕双飞 提交于 2021-01-29 13:32:01

问题


I stuck in a Snakemake issue for a lone while.

Propose I have a dict like:

dict_A = {A:"id1","id2","id3", B:"id2","id3","id4","id5", C:"id1","id4","id5"} 

and I want to write rules like:

input:
    "{dict_A.keys()}/{dict_A[key]}_R1.txt"
output:
    "{dict_A.keys()}/{dict_A[key]}_R1_filter.txt"
shell:
    "XXX {input} > {output}"

While, I try to search on google and StackOverflow but I can't figure out this problem. Really hope there is someone can help me !

Really thanks!


回答1:


Snakemake works by providing it a final output we expect it to generate, and rules how to produce output from certain input. When we do not define any output on the command line, snakemake will try to generate the input of the first rule in your Snakefile. So we define a rule all with as input what we want to generate:

dict_A = {'A': ["id1","id2","id3"], 
          'B': ["id2","id3","id4","id5"], 
          'C': ["id1","id4","id5"]}

all_input = []
for key, values in dict_A.items():
    for value in values:
        all_input.append(f"{key}/{value}_R1_filter.txt")

rule all:
    input:
        all_input

Now we have to change our rule so that it can fill in the correct wildcards for the output it wants to generate:

rule do_sth_cool:
    input:
        "{key_wc}/{value_wc}_R1.txt"
    output:
        "{key_wc}/{value_wc}_R1_filter.txt"
    shell:
        "XXX {input} > {output}"

Take a look again at the docs and make yourself familiar with the basic concepts of snakemake.

edit:

rule all:
    input:
        [f"{key}/{value}_R1_filter.txt" for key, values in dict_A.items() for value in values]


来源:https://stackoverflow.com/questions/58848521/how-to-use-dict-value-and-key-in-snakemake-rules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!