Search code examples
snakemake

Custom patterns with snakemake's paramspace


Is it possible to use a custom wildcard_pattern and instance_patterns with snakemake.utils.Paramspace?

Example:

Say the Paramspace looks like this

import snakemake
import pandas as pd

df = pd.DataFrame([
    ["default","2030"],
    ["default","2050"],
    ], columns=["scenario","year"])
paramspace = snakemake.utils.Paramspace(df)

Then the wildcard_pattern and instance_pattern look like this

print(paramsapce.wildcard_pattern)
# 'scenario~{scenario}/year~{year}'


print(list(paramspace.instance_patterns))
# ['scenario~default/year~2030', 'scenario~default/year~2050']

What I want to do is have both patterns without the name of the wildcard prepended, i.e. I would like it to look like this:

print(paramsapce.wildcard_pattern)
# '{scenario}/{year}'


print(list(paramspace.instance_patterns))
# ['default/2030', 'default/2050']

Solution

  • There isn't a public function, and the code formats based on position instead of name, so changing the pattern won't cut it.

    I would recommend just writing your own helper function to format the pattern you want. The code uses iterrows and row.items in it's format call.

    pattern = '{scenario}/{year}'
    instance_patterns = [
        pattern.format(**dict(i for i in row.items())) 
        for _, row in df.iterrows()]
    

    You could also open an issue requesting a change to either suppress having the parameter name or provide a custom formatter. Currently, you can change the param separator (default ~).