This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how to specify a specific conda environment in a specific rule with snakemake?

Hi!

I met a question about snakemake when I try to specify a conda env within a specific rule, but not influence other rules which should be run in default conda env.

configfile:
    "config.json"

SAMPLES, = glob_wildcards(config['data']+"/{id}_L001_R1_001.fastq.gz")

rule align_sort:
    input:
        config['data']+"/{sample}_L001_R1_001.fastq.gz",
        config['data']+"/{sample}_L001_R2_001.fastq.gz"
    output:
        temp("{sample}.mapped.bam")
    params:
        rg = "@RG\tID:{sample}\tPL:ILLUMINA\tSM:{sample}",
        ref = config['genome']
    shell:
        "bwa mem -R '{params.rg}' -M {params.ref} {input} | samtools sort -o {output} -"

rule removeClipping:
    input:
        "{sample}.mapped.bam"
    output:
        temp("{sample}.clipped.bam")
    conda:
        "bio2"
    shell:
        "bamutils removeclipping {input} {output}"
......

In my snakemake file, I have only one rule named removeClipping which has to be run in another conda environment named bio2.

But the remaining rule should be run in the default conda environment.

So I do not sure about the conda: setting in snakemake. Is this means that only this specific rule will be run in bio env, and the remaining rule will still be run in the default env?

Is anyone clear on this issue and would like help me figure this out? Many thanks!

linux snakemake conda

2 answers

You specify the tool and version you want to use in a yaml. For example, in bio2.yaml,

name: bio2
channels: 
  - bioconda
  - conda-forge
dependencies:
  - tool=version

Then you define the yaml in your rule. For example,

conda:
    "bio2.yaml"

Then you run snakemake with the --use-conda flag.

Hope this helps.

Thank you for responding so quickly!!

This helps me a lot.

Given the Snakefile you posted, you are asking whether rule removeClipping will run in conda env bio2 while the other rules will run in the default environment, right? If so, the answer is yes. Remember to add --use-conda to the snakemake command line (it's easy to forget!)

Log in to answer this question.