Skip to content

Conditional rule with conda environment #163

@mevers

Description

@mevers

Hi.

Following advice given by Johannes Köster in Add complex conditional file dependency
on how to include a conditional I've got the following conditional rule

rule bowtie2_align_reads:
    input:
        idx = IDX,
        reads = lambda wildcards: expand(join(
            config["fastqdir"],
            "{file}"),
            file = config["units"][wildcards.sample])
    output:
        join(
            config["analysisdir"],
            "{reference_version}/alignment/{sample}.bam")
    log:
        "logs/bowtie2_{reference_version}_{sample}.log"
    params:
        cmd      = config["bowtie2"]["cmd"],
        ref      = re.sub(".fa", "", REF),
        in_fmt   = config["bowtie2"]["in_fmt"],
        phred    = config["bowtie2"]["phred"],
        maxins   = config["bowtie2"]["maxins"],
        mismatch = config["bowtie2"]["mismatch"],
        threads  = config["bowtie2"]["threads"]
    version: "1.0"
    run:
        if (len(input.reads) == 2):
            shell(" \
                {params.cmd} \
                {params.phred} \
                --no-mixed \
                --no-discordant \
                --maxins {params.maxins} \
                -N {params.mismatch} \
                --threads {params.threads} \
                -x {params.ref} \
                -1 {input.reads[0]} \
                -2 {input.reads[1]} \
                2> {log} \
                | samtools view -bS -F4 - > {output} \
            ")
        elif (len(input.reads) == 1):
            shell(" \
                {params.cmd} \
                {params.phred} \
                -N {params.mismatch} \
                --threads {params.threads} \
                -x {params.ref} \
                -U {input.reads} \
                2> {log} \
                | samtools view -bS -F4 - > {output} \
            ")

In an attempt to "robustify" the workflow I am trying to use conda environments per rule. However, when I add

conda:
    "envs/bowtie2.yaml"

I get the following error

RuleException in line 42 of /home/mevers/Projects/ChIPseq_UBF_reanalysis/snakemake/rules/alignment.smk:
Conda environments are only allowed with shell, script, or wrapper directives (not with run).

My questions are:

  1. Is this a bug or intended behaviour? The conditional rule uses two shell commands within an if-elif construct nested in a run directive. So while the top-level directive is run, the actual rule decides on one of two shell calls.

  2. If this is intended behaviour, what would be the canonical way to include a conditional rule while making use of a conda environment?

Thanks,
Maurits

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions