Medhat
Medhat

Reputation: 1652

Snakemake Using expand with dictionary

I am writing this rule:

rule process_files:
    input: 
        dataout=expand("{{dataset}}/{{sample}}.{{ref}}.{{state}}.{{case}}.myresult.{name}.tsv", name=my_list[wildcards.ref]) 
    output:
        "{dataset}/{sample}.{ref}.{state}.{case}.endresult.tsv"
    shell:
        do something ...

Were expand will get value from dictionary my_dictionary based on the ref value. I used wildcards like this my_dictionary[wildcards.ref]. But it ends up with this error name 'wildcards' is not defined

my_dictionary something like: {A:[1,2,3], B:[s1,s2..].....}

I could use

def myfun(wildcards):
    return expand("{{dataset}}/{{sample}}.{{ref}}.{{state}}.{{case}}.myresult.{name}.tsv", name=my_dictionary[wildcards.ref])

and use myfun as input , but this does not answer why I can not use expand in place directly

Any suggestion how to fix it?

Upvotes: 2

Views: 2727

Answers (2)

JohnnyBD
JohnnyBD

Reputation: 161

As @dariober mentioned there is the wildcards objects but this is only accesible in the run/shell portion but can be accessed using an input function in input.

Here is an example implementation that will expand the input based on the wildcards.ref:

rule all:
    input: expand("{dataset}/{sample}.{ref}.{state}.{case}.endresult.tsv", dataset=["D1", "D2"], sample=["S1", "S2"], ref=["R1", "R2"], state=["STATE1", "STATE2"], case=["C1", "C2"])


my_list = {"R1": [1, 2, 3], "R2": ["s1", "s2"]}

rule process_files:
    input:
        lambda wildcards: expand(
            "{{dataset}}/{{sample}}.{{ref}}.{{state}}.{{case}}.myresult.{name}.tsv", name=my_list[wildcards.ref])
    output:
        "{dataset}/{sample}.{ref}.{state}.{case}.endresult.tsv"
    shell:
        "echo '{input}' > {output}"

If you implement it as the lambda function example above, it should resolve the issue you mention:

The function worked but it did not resolve the variable between double curly braces so it will ask for input for {dataset}/{sample}.{ref}.{state}.{case}and raise an error.

Upvotes: 2

dariober
dariober

Reputation: 9062

Your question seems similar to snakemake wildcards or expand command and the bottom line is that wildcards is not defined in the input. So your solution of using an input function (or a lambda function) seems correct.

(As to why wildcards is not defined in input, I don't know...)

Upvotes: 0

Related Questions