pythonsnakemake

expand based on a dictionary


Considering I have the following Python dictionary:

d = {'A': ['a', 'b', 'c],
     'B': ['d', 'e', 'f']}

Considering the keys in d, I would like to produce a plot with the following pattern f"{key}/plot/{d[key]}", for every element in d[key].

In other words, considering d above the output files that I would like to have are:

f"{A}/plots/{a}.pdf"
f"{A}/plots/{b}.pdf"
f"{A}/plots/{c}.pdf"
f"{B}/plots/{d}.pdf"
f"{B}/plots/{e}.pdf"
f"{B}/plots/{f}.pdf"

How can I define a rule all that selectively expands the input based on a dictionary?


Solution

  • Your dictionary d can be mapped to a list of strings with a straightforward list comprehension with two for clauses.

    [
      f"{key}/plots/{value}.pdf" 
      for key, values in d.items() 
      for value in values
    ]