Search code examples
pythonpandasshellsnakemake

snakemake parameter exploration: how to pass options to a command in shell directive?


I'm performing parameter exploration using Paramspace utility as described here. I've read parameters in a pandas dataframe and next I wish to pass these as values of options of a shell command but can't figure out how.

In the below minimal example, I wish to pass parameter s (read in dataframe df) as the value of option -n for head command in the shell directive.

from snakemake.utils import Paramspace
import pandas as pd

df = pd.DataFrame(
    {'s' : [1, 2, 3]},
    index = [1, 2, 3]
    )

paramspace_empty = Paramspace(df)

rule all:
    input:
        expand("results/{params}.tsv", params=paramspace_empty.instance_patterns)

rule simulate_empty:
    output:
        f"results/{paramspace_empty.wildcard_pattern}.tsv"
    params:
        simulation=paramspace_empty.instance
    shell: """
        head input.txt > {output}    
    """

I tried the below and similar variations but nothing worked.

shell: """
    head -n {params.simulation['s']} input.txt > {output}
"""

The above example is extracted (and modified a bit) from the Snakefile here which tests paramspace utility.

I seem to be missing something fundamental or trivial. Thank you in advance for any help.


Solution

  • You're almost there, you do not need to quote the dictionary key. Here's a slightly modified working version:

    import pandas as pd
    from snakemake.utils import Paramspace
    
    df = pd.DataFrame({"s": [1, 2, 3]}, index=[1, 2, 3])
    
    paramspace_empty = Paramspace(df, filename_params="*")
    
    
    rule all:
        input:
            expand("{params}.tsv", params=paramspace_empty.instance_patterns),
    
    
    rule simulate_empty:
        output:
            f"{paramspace_empty.wildcard_pattern}.tsv",
        params:
            simulation=paramspace_empty.instance,
        shell:
            """
            seq 10 | head -n {params.simulation[s]} > {output}
            """