This is a test version of Biostars. For the public version, visit https://www.biostars.org.
If/else statement in Snakemake rule

Hi!

I am building a pipeline for the processing of 4C data. I am trying to give a few options in the config file. The one problem I'm hung up on now is the alignment option. In the end, I would like to specifiy in the config file if the alignment should be done end2end or locally.

For this, I have read about using an if/else statement in the rule using wildcard objects. However, I would like to specify the parameter in my config-file and therefore cannot call it using the wildcards object. Can I call the config option in the params section of the rule and evaluate the outcome using the run section?

My config file states:

alignment: "local"

And this is the rule:

rule bowtie2_map:
input:
    "{sample}_trimmed_cut.fastq.gz"
output:
    "{sample}_trimmed_mapped.bam"
params:
    alignment={config[alignment]}
log:
    "logs/bowtie2_map/{sample}.log"
run:
    if params.alignment == "end2end"
        shell("bowtie2 -k 1 -p 6 -x hg19 -U {input} 2> {log} |  samtools view -S -u -@ 4 - > {output}")
        print("Sequences will be aligned end2end!")
    if params.alignment == "local"
        shell("bowtie2 -k 1 -p 6 -x hg19/hg19 --local -U {input} 2> {log} | samtools view -S -u -@ 4 - > {output}")
        print("Sequences will be aligned locally!")
    else:
        print("Define option for alignment in config file!")

Unfortunately, Snakemake only gives my a Syntax Error in the line of the first if statement. Does anyone know how to call the parameter from a config file for an if statement in a rule?

Thanks in advance for any help!

snakemake if

1 answer

Hi,

Within a run block, you have to write python compatible code. Here, the SyntaxError comes from Python itself and not Snakemake.

Your if statements should end with a : at the end of the line:

run:
    if params.alignment == "end2end":
        shell("bowtie2 -k 1 -p 6 -x hg19 -U {input} 2> {log} |  samtools view -S -u -@ 4 - > {output}")
        print("Sequences will be aligned end2end!")
    if params.alignment == "local":
        shell("bowtie2 -k 1 -p 6 -x hg19/hg19 --local -U {input} 2> {log} | samtools view -S -u -@ 4 - > {output}")
        print("Sequences will be aligned locally!")
    else:
        print("Define option for alignment in config file!")

Alternatively, the snakemake wrappers already include a nice wrapper for botwtie2.

Log in to answer this question.