Search code examples
pythonwildcardsnakemakewildcard-expansion

How to use wildcards in keyword wildcard_constraints


For example, I have the following wildcards.

dataset = ['A1', 'A2', 'A3', 'B1', 'B2', 'B3']
group = ['A', 'B']

I am trying to contrain my dataset with my group. for example, I want to create

A1/file.A.txt A2/file.A.txt A3/file.A.txt B1/file.B.txt ...

I wrote a following rule hoping to make that possible

rule complex_conversion:
    input:
        "{dataset}/inputfile"
    output:
        "{dataset}/file.{group}.txt"
    wildcard_constraints:
        dataset = {group} + '\d+'
        #dataset = {wildcards.group} + '\d+'
    shell:
        "somecommand --group {wildcards.group}  < {input}  > {output}"

oops, I got the error

TypeError:unhashable type: 'list'
#NameError: name 'wildcards' is not defined

It seems like that the {group} is regarded as a list to pass in the keywords wildcard_constraints.

Are there any methods to use wildcards in wildcards_constrain or alternative to mapped the dataset to group.


Solution

  • This doesn't answer your question but maybe it helps... If your list of output files is a combination of dataset and group, I would create that list first and then use it as list of output files:

    dataset = ['A1', 'A2', 'A3', 'B1', 'B2', 'B3']
    group = ['A', 'B']
    
    # Use a for-loop or whatever to create this list:
    datagrp = ['A1/file.A.txt','A2/file.A.txt', 'A3/file.A.txt', 'B1/file.B.txt']
    
    wildcard_constraints:
        # This prevents wildcards to be interpreted as regexes
        dataset = '|'.join([x for x in dataset]),
        group = '|'.join([x for x in group])
    
    rule all:
        input:
            datagrp,
    
    rule complex_conversion:
        input:
            "{dataset}/inputfile"
        output:
            "{dataset}/file.{group}.txt"
        shell:
            "somecommand --group {wildcards.group}  < {input}  > {output}"