If/else statement in Snakemake rule
1
0
Entering edit mode
4.4 years ago

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 • 4.5k views
ADD COMMENT
2
Entering edit mode
4.4 years ago
Thibault D. ▴ 700

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.

ADD COMMENT

Login before adding your answer.

Traffic: 2536 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6