Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference input in params section of snakemake rule?

I need to process my input file values, turning them into a comma-separated string (instead of white space) in order to pass them to a CLI program. To do this, I want to run the input files through a Python function. How can I reference the input files of a rule in the params section of the same rule?

This is what I've tried, but it doesn't work:

rule a:
    input:
        foo="a.txt",
        bar=expand({build}.txt,build=config["build"]),
    output:
        baz=result.txt,
    params:
        joined_bar=lambda w: ",".join(input.bar),  # this doesn't work
    shell:
        """
        qux --comma-separated-files {params.joined_bar} \
            --foo {input.foo} \
            >{output.baz}
        """

It fails with:

InputFunctionException:
   AttributeError: 'builtin_function_or_method' object has no attribute 'bar'

Potentially related but (over-)complicated questions:
How to define parameters for a snakemake rule with expand input
Is Snakemake params function evaluated before input file existence?

like image 586
Cornelius Roemer Avatar asked Oct 30 '25 12:10

Cornelius Roemer


1 Answers

Turns out I need to explicitly add input to the lambda w: part:

rule a:
    input:
        foo="a.txt",
        bar=expand({build}.txt,build=config["build"]),
    output:
        baz=result.txt,
    params:
        joined_bar=lambda w, input: ",".join(input.bar),  # ', input' was added
    shell:
        """
        qux --comma-separated-files {params.joined_bar} \
            --foo {input.foo} \
            >{output.baz}
        """

Interestingly, I found that one needs to use input in the lambda w, input. In my testing, lambda w, i did not work.

And alternative is to refer to the rule input in the standard way: rules.a.input.bar:

rule a:
    input:
        foo="a.txt",
        bar=expand({build}.txt,build=config["build"]),
    output:
        baz=result.txt,
    params:
        joined_bar=lambda w: ",".join(rules.a.input.bar),  # 'rules.a.' was added
    shell:
        """
        qux --comma-separated-files {params.joined_bar} \
            --foo {input.foo} \
            >{output.baz}
        """

Also see http://biolearnr.blogspot.com/2017/11/snakemake-using-inputoutput-values-in.html for a discussion.

like image 174
Cornelius Roemer Avatar answered Nov 02 '25 02:11

Cornelius Roemer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!