pythonwildcardsnakemake

Snakemake expand a string saved in a variable


My question is very simple, but I can't find how to do it in the Snakemake documentation.

Let's say I have a very long string to expand, like :

rule all:
  input:
expand("sim_files/test_nGen{ngen}_N{N}_S{S}_NR{NR}_DG{DG}_SS{SS}/test_nGen{ngen}_N{N}_S{S}_NR{NR}_DG{DG}_SS{SS}.yaml",
        ngen=list_ngen, N=list_N, S=list_S, NR=list_NR, DG=list_DG, SS=list_SS)

I want to save my long string in a variable, like this:

prefix = "test_nGen{ngen}_N{N}_S{S}_NR{NR}_DG{DG}_SS{SS}"

rule all:
  input:
    expand("sim_files/{prefix}/{prefix}.yaml",ngen=list_ngen, N=list_N, S=list_S, NR=list_NR, DG=list_DG, SS=list_SS)

But when I do that, Snakemake doesn't expand inside the wildcard 'prefix':

WildcardError in file /home/bunelpau/Travail/DossierSync/test_snt/Snakefile, line 13:
No values given for wildcard 'prefix'.

Do I have to use this super long string, or is there a way to save it in some variable like I want to?


Solution

  • Use an f-string to format the string before expansion:

    prefix = "test_nGen{ngen}_N{N}_S{S}_NR{NR}_DG{DG}_SS{SS}"
    
    rule all:
      input:
        expand(f"sim_files/{prefix}/{prefix}.yaml",ngen=list_ngen, N=list_N, S=list_S, NR=list_NR, DG=list_DG, SS=list_SS)