-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.qmd
894 lines (694 loc) · 22.8 KB
/
index.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
---
title: "Just Enough Python"
subtitle: "for R users to write advanced Snakemake workflows"
date: 2022-11-14
date-modified: 2022-11-21
aliases:
- just-enough-python.html
engine: knitr
image: /images/python-snakemake-r-logos-horiz.png
---
```{r}
#| eval: true
#| echo: false
#\ message: false
# forces renv to recognize these packages as dependencies
library(reticulate) # for python
library(downlit) # for code linking
library(xml2) # for code linking
```
Snakemake is a workflow management tool implemented as extra magic on top of Python.
If you want to take your Snakemake workflows from beginner to advanced,
learning a little Python goes a long way.
This is just enough Python to understand and apply some cool tricks in your Snakemake workflows.
![](/images/python-snakemake-r-logos-horiz.png){ width=300px fig-align="center" fig-alt="The Python, R, and Snakemake logos linked together."}^[The [Python logo](https://commons.wikimedia.org/wiki/File:Python-logo-notext.svg) by the Python Software Foundation is licensed under GNU GPL v2. The [Snakemake logo](https://commons.wikimedia.org/wiki/File:Snakemake_logo_dark.png) by Johannes Köster is licensed under CC BY-SA 4.0. The [R logo](https://commons.wikimedia.org/wiki/File:R_logo.svg) by Hadley Wickham and others at RStudio is licensed under CC BY-SA 4.0.]
##### prereqs
- **Basic Snakemake knowledge.** You've gone through [the official tutorial](https://snakemake.readthedocs.io/en/stable/tutorial/tutorial.html).
Maybe you've even built your own snakemake workflow.
- Minimal or **no python knowledge**, aside from what you may have gleaned from
the snakemake docs.
- **Basic R knowledge.** Tidyverse experience is helpful but not required.
## variables
Here are the basic variable types in Python and the equivalent type in R:
### basic types
| Python | R |
|---------|-----------|
| string | character |
| integer | integer |
| float | numeric |
| boolean | logical |
| None | NULL |
```{python py-types}
#| filename: "Python"
# string
name = "Kelly"
favorite_color = 'green'
# float
height_inches = 64.0
# int
num_plants = 14
# bool
likes_cats = True
# none
invalid = None
```
```{r r-types}
#| filename: "R"
# character
name <- "Kelly"
favorite_color <- 'green'
# numeric
height_inches <- 64
# integer
num_plants <- 14L
# logical
likes_cats <- TRUE
# null
invalid = NULL
```
A key difference is that in R, all of the above objects are **vectors** of length 1.
#### type casting
You can convert variables to different types:
```{python type-cast}
#| filename: "Python"
int(32.0)
float(32)
str(32)
bool(0)
bool(1)
bool(None)
```
The equivalent R code:
```{R}
#| filename: "R"
as.integer(32)
as.numeric(32L)
as.character(32)
as.logical(0)
as.logical(1)
as.logical(NULL)
```
What similarities and differences do you notice when type-casting in Python vs. R?
#### Naming variables
The biggest difference in variable names between R and Python:
no dots are allowed in Python variable names.
All other rules from R apply. Python variable names must start
with an alphabetic character and they can contain alphanumeric characters
and underscores.
Just like in R, it is a good idea not to name a variable the same thing as a
reserved keyword or global function.
```{python}
#| filename: "Python keywords"
help("keywords")
```
#### formatting strings
You can add strings together to build new ones like this:
```{python}
#| filename: "Python"
firstname = "Marie"
lastname = "Curie"
middle_init = 'S'
prefix = "Madame"
fullname = prefix + " " + firstname + " " + middle_init + ". " + lastname
print(fullname)
```
The print function takes any number of arguments and will insert a space between
items by default:
```{python}
#| filename: "Python"
print(prefix, firstname, middle_init + ".", lastname)
```
But there's a better way!
**f-Strings** in Python work just like `glue()` in R, where curly braces in the
string are interpreted as code. They're much easier to read & write:
```{python}
#| filename: "Python"
f"{prefix} {firstname} {middle_init}. {lastname}"
```
This is equivalent to:
```{R}
#| filename: "R"
firstname <- "Marie"
lastname <- "Curie"
middle_init <- 'S'
prefix <- "Madame"
glue::glue("{prefix} {firstname} {middle_init}. {lastname}")
```
### container types
Here are a few of the most useful container types and their equivalents in R:
| Python | R |
|--------------------|----------------|
| list | vector (all same type) or list (multiple types) |
| dictionary | named list |
| DataFrame (pandas) | data.frame (base) or tibble (tidyverse) |
#### lists
Python lists are most similar to lists in R. Python lists can also be compared
to vectors in R, but unlike vectors, lists can contain objects of different types.
```{python py-lists}
#| filename: "Python"
fruit = ['apple','banana','grape']
my_list = ['pizza', 'fries', 3, -2.5, 'donuts']
fruit + my_list
fruit.append('mango')
fruit
```
##### Indexing
Indexing is 0-based in Python. You can index lists and strings.
```{python index}
#| filename: "Python"
numbers = [1,2,3,4]
len(numbers) # length
numbers[0] # first item
numbers[3] # last item
numbers[-1] # last item
```
You might find 0-based indexing unintuitive if you're used to working in
languages like R that use 1-based indexing, but this is
"not mad, just differently sane!"^[Greg Wilson quoting Terry Pratchett in
_The Tidynomicon_: <https://tidynomicon.github.io/tidynomicon/>]
Here's an illustration that shows how 0-based & 1-based indexing systems compare
in the context of DNA coordinates^[from biostars: <https://www.biostars.org/p/84686/>]:
[![DNA indexing comparison](https://i.postimg.cc/yxxngrS5/single-nucleotide-or-variant.jpg)](https://www.biostars.org/p/84686/)
```{python}
#| filename: "Python"
dna = "TACGTCA"
print(dna[0])
print(dna[6])
```
We can try to write equivalent code in R...
```{r}
#| filename: "R"
#| error: true
dna <- "TACGTCA"
print(dna[1])
print(dna[7])
```
But this doesn't work!
In the above R code, `dna` is a vector of length 1 with "TACGTCA" as the item at index 1.
You can't grab individual characters out of it.
We have to break up the string into a vector with every character as a separate item in R.
```{r}
#| filename: "R"
dna <- c('T', 'A', 'C', 'G', 'T', 'C', 'A')
print(dna[[1]])
print(dna[[7]])
```
#### dictionaries
Dictionaries consist of key-value pairs where you use the key to access the value.
Keys can be strings, integers, floats, or booleans. Values can be anything!
```{python}
#| filename: "Python"
foods = {'vegetable': ['carrot', 'eggplant'],
'fruit': 'cherry'}
foods['fruit']
foods['vegetable']
foods['vegetable'][0]
```
Dictionaries can be nested:
```{python}
#| filename: "Python"
hyperparams = {'glmnet': {'alpha': [0, 0.5, 1],
'lambda': [0.01, 0.1, 0, 1]
},
'rf': {'mtry': [128, 256, 512]}
}
hyperparams['glmnet']['alpha'][1]
```
Get a list of keys in the dictionary with:
```{python}
#| filename: "Python"
hyperparams.keys()
```
##### Snakemake config
Say you have a Snakemake workflow for training machine learning models.
You want to be able to specify different datasets, ML methods, and random seeds,
so you use f-Strings to fill in these variables:
```{python}
#| filename: "Snakefile"
#| eval: false
dataset = "OTU"
method = "rf"
seed = 1001
rule train_ml_model:
input:
R="workflow/scripts/train_ml.R",
csv=f"data/{dataset}.csv"
output:
model=f"results/{dataset}/runs/{method}_{seed}_model.Rds"
script:
"scripts/train_ml.R"
```
You can improve this by creating a configuration file in YAML format:
```{R}
#| filename: "config/otu.yml"
#| eval: false
dataset: 'OTU'
method: 'rf'
seed: 1001
```
Then specify your config file at the top of your Snakefile.
Snakemake parses your YAML config files to a dictionary called `config`.
```{python}
#| filename: "Snakefile"
#| eval: false
configfile: "config/otu.yml"
dataset = config["dataset"]
method = config['rf']
seed = config['seed']
rule train_ml_model:
input:
R="workflow/scripts/train_ml.R",
csv=f"data/{dataset}.csv"
output:
model=f"results/{dataset}/runs/{method}_{seed}_model.Rds"
script:
"scripts/train_ml.R"
```
When you run this workflow, it uses `config/otu.yml` by default:
```{bash}
#| filename: sh
#| eval: false
snakemake -n
```
```{bash}
#| eval: false
rule train_ml_model:
input: workflow/scripts/train_ml.R, data/OTU.csv
output: results/OTU/runs/rf_1001_model.Rds
jobid: 0
reason: Missing output files: results/OTU/runs/rf_1001_model.Rds
resources: tmpdir=/var/folders/9n/glrhxtfx453gl68sc1gzq0mc0000gr/T
Job stats:
job count min threads max threads
-------------- ------- ------------- -------------
train_ml_model 1 1 1
total 1 1 1
```
Now you can change the values of these variables by editing the config file,
or by creating multiple config files and specifying them on the command line:
```{R}
#| filename: "config/genus.yml"
#| eval: false
dataset: 'genus'
method: 'glmnet'
seed: 1001
```
```{bash}
#| filename: sh
#| eval: false
snakemake -n --configfile config/genus.yml
```
```{bash}
#| eval: false
rule train_ml_model:
input: workflow/scripts/train_ml.R, data/genus.csv
output: results/genus/runs/glmnet_1001_model.Rds
jobid: 0
reason: Missing output files: results/genus/runs/glmnet_1001_model.Rds
resources: tmpdir=/var/folders/9n/glrhxtfx453gl68sc1gzq0mc0000gr/T
Job stats:
job count min threads max threads
-------------- ------- ------------- -------------
train_ml_model 1 1 1
total 1 1 1
```
See this example in context [here](https://github.com/SchlossLab/just-enough-python/blob/main/Snakemake_demo/workflow/Snakefile).
## conditionals
Think of these as functions that return a boolean.
| operator | meaning | same in R |
|----------|--------------------------|-----------|
| `==` | equal | ✅ |
| `!=` | not equal | ✅ |
| `<` | less than | ✅ |
| `>` | greater than | ✅ |
| `<=` | less than or equal to | ✅ |
| `>=` | greater than or equal to | ✅ |
| or | or (inclusive) | `|` |
| and | and | `&` |
| in | in | `%in%` |
| not | not | `!` |
```{python}
3 > 2
True or False
```
### if-else statements
No curly braces here! But indentation does matter.
```{python}
#| filename: "Python"
if len(my_list) < 10:
list_description = "This list is short"
elif len(my_list) < 15:
list_description = "This list is medium length"
else:
list_description = "This list is long!"
print(list_description)
```
#### inline if-else statement
When you create a variable with an if-else statement,
you can do it all on one line:
```{python}
list_description = "short" if len(my_list) < 10 else "not short"
```
This is equivalent to:
```{python}
if len(my_list) < 10:
list_description = "short"
else:
list_description = "not short"
```
You can't make it a one-liner if you need to use `elif`.
#### nested if-else blocks
```{python}
#| filename: "Python"
num = 1
if num >= 0:
if num == 0:
print("zero")
else:
print("positive")
else:
print("negative")
```
## for loops
If a variable has a length (i.e. you can run `len(myvar)`), then you can
**iterate** over it in a for loop.
```{python}
#| filename: "Python"
postdocs = ['Allison Mason', 'Courtney Armour', 'Sarah Lucas']
for person in postdocs:
print(f"{person}, PhD")
```
### range
Use `range()` to loop over a range of numbers. If you give `range()` two arguments,
range starts counting from the first argument and stops _just before_ the second argument,
stepping by 1.
In other words, the stop argument is _exclusive_.
```{python}
#| filename: "Python"
num_bottles = 3
for i in range(1, num_bottles+1):
print(i, " bottles of pop on the wall")
```
If you give range just one argument, it starts counting from zero and uses
the argument as the stop value:
```{python}
#| filename: "Python"
num_bottles = 3
for i in range(num_bottles+1):
print(i, " bottles of pop on the wall")
```
<!-- made it to here in code club #1 -->
You can also use `range()` to create lists:
```{python}
#| filename: "Python"
mylist = list(range(3))
mylist
```
You can use range with three arguments: `start`, `stop`, and `step`:
```{python}
#| filename: "Python"
list(range(2, 11, 2))
```
Give the `step` argument a negative value and make `start` greater than `stop` to move backwards:
```{python}
#| filename: "Python"
list(range(4, -7, -2))
```
You might think it's weird that range stops just _before_ the stop value.
But it works nicely when you give it the length of the list -- now you have the list indices!
```{python}
#| filename: "Python"
numbers = [1,2,3,4]
for i in range(len(numbers)):
print(f"index {i} is {numbers[i]}")
```
### iterate over dictionaries
```{python}
#| filename: "Python"
problems = {1: "naming things", 2: "understanding binary", 3: "off-by-one errors"}
print('There are only 10 hard problems in computer science:')
for num in problems:
print(f"\t {num} {problems[num]}")
```
### list comprehensions
When you want to make a for loop that creates a new list, dictionary, or set,
use a **comprehension**. I'm only going to cover list comprehensions below,
but you can learn about dictionary and set comprehensions
[here](just-a-little-more-python.qmd#more-comprehensions).
To build a list with a for loop, you can do this:
```{python list-comp-for}
#| filename: "Python"
squares = []
for i in range(4):
squares.append(i**2)
squares
```
but a **list comprehension** is much sleeker and performs faster:
```{python list-comp}
#| filename: "Python"
squares = [i**2 for i in range(4)]
```
This is roughly equivalent to using *apply functions in R:
```{r apply}
#| filename: "R"
sapply(0:3, function(x) {x**2})
```
You can continue nesting `for x in y` statements to build complex lists like this:
```{python}
#| filename: "Python"
[f"{x}{y}{z}" for x in range(1,3) for y in range(2,4) for z in range(3,5)]
```
#### Snakemake expand
Let's say you have a Snakemake workflow with a rule that should run multiple times
with different parameters. For example, you may want to train machine learning models
with different ML methods, datasets, and random seeds.
You define these as [wildcards](https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#wildcards) in a rule to train each ML model:
```{python}
#| filename: "Snakefile"
#| eval: false
rule train_ml_model:
input:
R="workflow/scripts/train_ml.R",
csv="data/{dataset}.csv"
output:
model="results/{dataset}/runs/{method}_{seed}_model.Rds"
script:
"scripts/train_ml.R"
```
Notice that we're _not_ using **f-Strings** within the rule because we want
Snakemake to recognize these as **wildcards** and fill in different values for
them with each run of the `train_ml_model` rule.
To tell Snakemake what values to assign for these wildcards, you can create
lists with the values you want and write a target rule at the top of your Snakefile.
Here, you could use an f-String and a list comprehension to create the list of target files.
```{python}
#| filename: "Python"
datasets = ['OTU', 'genus']
methods = ['rf', 'glmnet', 'svmRadial']
seeds = range(1000, 1005)
[f"results/{dataset}/runs/{method}_{seed}_model.Rds" for dataset in datasets for method in methods for seed in seeds]
```
Here's what your Snakefile looks like now.
```{python}
#| filename: "Snakefile"
#| eval: false
datasets = ['OTU', 'genus']
methods = ['rf', 'glmnet', 'svmRadial']
seeds = range(1000, 1005)
rule targets:
input:
[f"results/{dataset}/runs/{method}_{seed}_model.Rds" for dataset in datasets for method in methods for seed in seeds]
rule train_ml_model:
input:
R="workflow/scripts/train_ml.R",
csv=f"data/{dataset}.csv"
output:
model="results/{dataset}/runs/{method}_{seed}_model.Rds"
script:
"scripts/train_ml.R"
```
The list comprehension gets harder to read the more `for` statements you add to it.
So Snakemake provides a function `expand()` to clean this up:
```{python}
#| filename: "Snakefile"
#| eval: false
datasets = ['OTU', 'genus']
methods = ['rf', 'glmnet', 'svmRadial']
seeds = range(1000, 1005)
rule targets:
input:
expand("results/{dataset}/runs/{method}_{seed}_model.Rds",
dataset = datasets, method = methods, seed = seeds)
rule train_ml_model:
input:
R="workflow/scripts/train_ml.R"
output:
model="results/{dataset}/runs/{method}_{seed}_model.Rds"
script:
"scripts/train_ml.R"
```
Using `expand()` creates the exact same list as the list comprehension, but it's
much easier to read and add variables to.
## functions
You can define your own functions
in R:
```{R r-function}
#| filename: "R"
square <- function(x) {
return(x^2)
}
square(2)
```
and in Python:
```{python py-function}
#| filename: "Python"
def square(x):
return x**2
square(2)
```
### anonymous functions
Sometimes you want to write a simple function that you only use once.
They're so inconsequential you don't even want to give them a name.
We've already used one in R^[Read more about anonymous functions in Hadley Wickham's book _Advanced R_: <http://adv-r.had.co.nz/Functional-programming.html#anonymous-functions>] inside `sapply()` above:
```{R r-anon}
#| filename: "R"
function(x) { x**2 }
```
In Python you use the `lambda` keyword:
```{python py-anon}
#| filename: "Python"
lambda x: x**2
```
Before the colon, list the arguments of the function.
After the colon, compute the value that the function should return.
#### lambda in Snakemake
When writing Snakemake workflows, lambda functions are useful for defining input
files based on the wildcards in output files.
Consider a workflow where you have several rules that plot figures for a manuscript.
When you initially conduct the analysis, you don't know how the figures will be ordered
in the manuscript.
Once you begin drafting the manuscript, you decide that your diversity plot will
be figure 1 and your error rates plot will be figure 2.
You also decide to convert the figures to a different format, so the conversion
step seems like a good opportunity to rename the figures.
Initially you write this workflow:
```{python}
#| filename: "Snakefile"
#| eval: false
rule targets:
input:
"paper/paper.pdf"
rule convert_figure_1:
input:
tiff='figures/diversity.tiff'
output:
png="paper/figure_1.png"
shell:
"""
convert {input.tiff} {output.png}
"""
rule convert_figure_2:
input:
tiff='figures/error_rates.tiff'
output:
png="paper/figure_2.png"
shell:
"""
convert {input.tiff} {output.png}
"""
rule render_paper:
input:
Rmd="paper/paper.Rmd",
R="workflow/scripts/render_rmd.R",
figures=['paper/figure_1.png', 'paper/figure_2.png']
output:
pdf="paper/paper.pdf"
script:
"scripts/render_rmd.R"
```
The rules `convert_figure_1` and `convert_figure_2` are a bit repetitive; they
only differ by the input filenames and the figure numbers in the output filenames.
Maybe this isn't so bad with only two figures, but you might actually have 5-10
figures for a full scientific paper. We can reduce the repetitive code with
a few tricks:
1. Create a dictionary `figures_dict` that maps the figure numbers to the descriptive figure file names.
1. Use a single rule to convert figures called `convert_tiff_to_png`, using a lambda function to get the input figure filenames based on the final figure numbers.
```{python}
#| filename: "Snakefile"
#| eval: false
figures_dict = {'1': 'diversity', '2': 'error_rates'}
rule targets:
input:
"paper/paper.pdf"
rule convert_tiff_to_png:
input:
tiff=lambda wildcards: f"figures/{figures_dict[wildcards.fig_num]}.tiff"
output:
png="paper/figure_{fig_num}.png"
shell:
"""
convert {input.tiff} {output.png}
"""
rule render_paper:
input:
Rmd="paper/paper.Rmd",
R="workflow/scripts/render_rmd.R",
figures=['paper/figure_1.png', 'paper/figure_2.png']
output:
pdf="paper/paper.pdf"
script:
"scripts/render_rmd.R"
```
This lambda function is equivalent to:
```{python}
def get_fig_name_from_num(wildcards):
return figures_dict[wildcards.fig_num]
```
This works because Snakemake allows you to define a function that takes the
rule's wildcards and returns a list of input filenames, rather than literally
listing the input filenames as before.
This greatly reduces the repetitiveness of the code and makes it easier to maintain.
We can improve this Snakefile even further by replacing the list of figures in
`render_paper` with a call to `expand()`:
```{python}
#| filename: "Snakefile"
#| eval: false
figures_dict = {'1': 'diversity', '2': 'error_rates'}
rule targets:
input:
"paper/paper.pdf"
rule convert_tiff_to_png:
input:
tiff=lambda wildcards: f"figures/{figures_dict[wildcards.fig_num]}.tiff"
output:
png="paper/figure_{fig_num}.png"
shell:
"""
convert {input.tiff} {output.png}
"""
rule render_paper:
input:
Rmd="paper/paper.Rmd",
R="workflow/scripts/render_rmd.R",
figures=expand(rules.convert_tiff_to_png.output.png,
fig_num = figures_dict.keys())
output:
pdf="paper/paper.pdf"
script:
"scripts/render_rmd.R"
```
You can take a look at this full example in context [here](https://github.com/kelly-sovacool/figure-convert-reprex).
Also see the Snakemake docs for more about [input functions](https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#snakefiles-input-functions)
and [params functions](https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#non-file-parameters-for-rules).
# recap
**Snakemake concepts covered**
- Use [**f-Strings**](#formatting-strings) for human-readable string formatting.
- [**Config files**](#snakemake-config) are imported as dictionaries and allow you to change workflow parameters without modifying code.
- Snakemake's [**expand()**](#snakemake-expand) function is a readable way to build lists just like list comprehensions.
- [**Lambda functions**](#lambda-in-snakemake) help define Snakemake input files based on wildcards. You can also use them to define params based on wildcards and/or output files.
# resources {.appendix}
- [The Python docs](https://docs.python.org/3/)
- [The Snakemake docs](https://snakemake.readthedocs.io/)
<!--
- [Just A Little More Python](just-a-little-more-python.qmd): topics I wish I had time to cover in this tutorial.
-->