Skip to content

Commit 39b7d07

Browse files
authored
Merge pull request #6 from lukenoaa/master
adding Example 5 with conditional statements in Snakefile
2 parents ac383eb + e523a1a commit 39b7d07

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

snakemake/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,65 @@ Finished job 0.
449449
1 of 1 steps (100%) done
450450
Complete log: /Users/luke/git/tutorials/snakemake/example4/.snakemake/log/2019-11-17T192742.013029.snakemake.log
451451
```
452+
453+
### Example 5
454+
455+
This example uses shell **conditional statement** (if/then/elif/else) in the Snakefile to select one of several commands to run, which is specified in the **config file**.
456+
457+
#### config.yaml
458+
459+
```
460+
# parameters
461+
option: a # choose from: a, b, c
462+
```
463+
464+
The parameter `option` is a string that will be used in a string comparison in the Snakefile.
465+
466+
#### Snakefile
467+
468+
```
469+
configfile: "config.yaml"
470+
471+
rule choose_from_options:
472+
params:
473+
option=config["option"]
474+
shell:
475+
"if [ {params.option} == 'a' ]; then "
476+
" echo 'Doing option a...'; "
477+
"elif [ {params.option} == 'b' ]; then "
478+
" echo 'Doing option b...'; "
479+
"elif [ {params.option} == 'c' ]; then "
480+
" echo 'Doing option c...'; "
481+
"else "
482+
" echo 'Doing default option'; "
483+
"fi"
484+
```
485+
486+
At the top of the Snakefile, we define the config file.
487+
488+
The rule `choose_from_options` gets the `option` parameter from the config file. The shell command then uses a conditional statement (if/then/elif/else) to execute different code for each of the accepted options, or the default code if an unaccepted option is provided.
489+
490+
Run the code using the basic Snakemake command:
491+
492+
```
493+
snakemake
494+
```
495+
496+
#### Output
497+
498+
```
499+
$ snakemake
500+
Provided cores: 1
501+
Rules claiming more threads will be scaled down.
502+
Job counts:
503+
count jobs
504+
1 merge_describe
505+
1
506+
507+
rule merge_describe:
508+
jobid: 0
509+
510+
Doing option a...
511+
Finished job 0.
512+
1 of 1 steps (100%) done
513+
```

snakemake/example5/Snakefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
configfile: "config.yaml"
2+
3+
rule choose_from_options:
4+
params:
5+
option=config["option"]
6+
shell:
7+
"if [ {params.option} == 'a' ]; then "
8+
" echo 'Doing option a...'; "
9+
"elif [ {params.option} == 'b' ]; then "
10+
" echo 'Doing option b...'; "
11+
"elif [ {params.option} == 'c' ]; then "
12+
" echo 'Doing option c...'; "
13+
"else "
14+
" echo 'Doing default option'; "
15+
"fi"

snakemake/example5/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# parameters
2+
option: a # choose from: a, b, c

0 commit comments

Comments
 (0)