-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaking_code_ready.qmd
More file actions
414 lines (291 loc) · 9.49 KB
/
making_code_ready.qmd
File metadata and controls
414 lines (291 loc) · 9.49 KB
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
---
title: "Making Code Ready for Publication"
format: html
---
## Outline
- **Why** make your code ready for publication?
- **What** do you need to make it ready?
- **How** and where do you make it available?
## Who this is for
- Majority of us want to share analyses, not software
- Leverage some principles from software packaging to share scripts and notebooks
- Software packaging is its own topic
## Why?

## Why: Documentation is Important
- Do it for Future You
- Others in your lab
- Others in your field
## Why: Reproducibility Matters
- [Duke Medicine biomarker scandal](https://www.science.org/content/blog-post/duke-cancer-scandal-and-personalized-medicine)
- Software is an important output
## Reproducibility is a Spectrum
- Do what you can
- Providing a good framework for running analyses
## Why: Languages are Moving Targets
- Packages may depend on certain versions of Python/R
- Dependency Hell
- Need a way to "freeze" or "pin" versions used in analysis
## Why: Reproducibility is an iterative process
- When possible, start from the beginning
- Use package management and environments from the start
- `rv` / `uv` (in Package management session)
- Test out running scripts and notebooks as you go
# What: Parts of a Reproducible Project
## What: Minimum Information for Analyses
- Focus on Data Analysis in R / Python
- Genomic analysis: future workshop?
- Organize your analysis in a folder and share in a repository
- [ ] Notebook(s) / Scripts
- [ ] Lockfile
- [ ] Data (if small enough)
- [ ] README
## Project Example:
```
my_project/ ## Top level
├── data/ ## Data directory
│ └── my_data.vcf
├─- output/ ## Share output
└── 01_preprocessing.R ## Scripts in order
└── 02_deseq2_analysis.qmd
└── 03_visualization.ipynb
├── renv.lock ## R Packages
├── requirements.txt ## Python Packages
└── README.md
```
## What: Notebooks / Analysis Files
```
my_project/ ## Top level
├── 01_preprocessing.R ## Scripts in order
├── 02_deseq2_analysis.qmd
└── 03_visualization.ipynb
```
- Easiest: place in your root folder
- Number in order
- `01_preprocessing.R`
- `02_deseq2_analysis.qmd`
- Be sure to include a random seed for reproducibility
## Relative Paths
- Everything should be runnable from the top folder of the project. Put data in `data/` folder. Use relative paths from the top project folder:
```r
my_data <- readr::read_csv("data/datafile.csv")
```
- Ensures portability of project
Example of a reproducible project:
[https://github.com/biodev/beataml2_manuscript](https://github.com/biodev/beataml2_manuscript)
## What: Data in a Project
```
my_project/ ## Top level
├── data ## Data directory
│ └── my_data.vcf
```
- Genomic and omics data is large
- raw data is not practical for GitHub (100 Mb limit)
- Store raw files in required respositories
- Supply intermediate formats used to do the analysis:
- MAF
- VCF
- CSV
## What: A Note about data
[Reproducibility in the Genomics Era](https://pmc.ncbi.nlm.nih.gov/articles/PMC11312195/)
- Large genomic data - sharing raw data is impractical in a project
- Use Intermediate formats instead, such as VCF files, CSV files
- With code, share metadata - list the files you processed
- JSON files from workflows
- Metadata / Experimental Design
- Where does each sample fit into Experimental Design?
## Reproducible Environments
In order of complexity:
```{mermaid}
graph TD
A[Lockfile] --> B
B[Binder Ready] --> C
C[Dockerfile]
```
## What: Lockfile
```
my_project/ ## Top level
├── renv.lock
├── requirements.txt
```
- List of packages and versions that you used in analysis
- Post analysis:
- Python: `pip freeze > requirements.txt`
- R: `renv::snapshot()`
- Talk about `rv` and `uv` in Package management session
## Lockfile Examples
::: {.panel-tabset}
## R
```r
{
"R": {
"Version": "4.2.3",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://cloud.r-project.org"
}
]
},
"Packages": {
"markdown": {
"Package": "markdown",
"Version": "1.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "4584a57f565dd7987d59dda3a02cfb41"
},
"mime": {
"Package": "mime",
"Version": "0.7",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "908d95ccbfd1dd274073ef07a7c93934"
}
}
}
```
## Python
```python
anyio==4.11.0
appnope==0.1.4
argon2-cffi==25.1.0
argon2-cffi-bindings==25.1.0
arrow==1.3.0
asttokens==3.0.0
async-lru==2.0.5
attrs==25.3.0
babel==2.17.0
beautifulsoup4==4.13.5
bleach==6.2.0
certifi==2025.8.3
cffi==2.0.0
[...]
```
:::
## What: Reproducing environment from lockfile
Give zipped folder to someone else or host on github
::: {.panel-tabset}
## Python
```python
venv
source venv/bin/activate
pip install -r requirements.txt
```
## R
```r
install.packages("renv")
renv::restore()
```
:::
## What: Making a Lockfile from your current project
::: {.panel-tabset}
## Python
```python
import session-info as si
# put this at the end of your notebook
si.show(na=True, os=True, cpu=False, jupyter=None, dependencies=None, write_req_file=True, req_file_name="requirements.txt")
```
- Install the session-info module:
## R
```r
renv::snapshot()
```
- Can use the `renv` package to generate a current list of packages
- Does not require a virtual environment to be initialized
:::
## Why not Conda?
Anaconda is charging institutions for using their forge - be aware that you will need to pay charges or change your forge to the Fred Hutch version.
For more info: <https://conda-forge.fredhutch.org/>
## What: Binder Ready Repository
A special way to share your analysis
- Put your project on GitHub
- Can plug your repository link into `mybinder.org`
- Generates JupyterLab / RStudio / Shiny Server instance
## What: Binder.org

## How does Binder work?
- Uses `requirements.txt` (Python) or `install.R` (R) or Dockerfiles in your repository
- Installs relevant packages
- Launches your analysis in a container (Dockerizes your analysis)
## Some Cons about Binder
- Currently limited to 1 Gb of memory for an instance
-
## Dockerfile
Takes a lot of work, builds on the work of others
A precise list of instructions to install your computational environment.
More useful if you are distributing software.
## Dockerfile Tips
- Don’t try to create Dockerfiles from scratch
- Community Images: BioC, Rocker Project, WILDS Docker Library
- Use https://repo2docker.readthedocs.io/en/latest/
- https://repo2docker.readthedocs.io/en/latest/configuration/#config-files
## Nix
- Currently investigating Nix as a language agnostic reproducibility framework
## What: README
```
my_project/ ## Top level
└── README.md
```

- First thing that people will see
- https://github.com/biodev/HNSCC_Notebook
- Document the basic workflow of processing
- How does the data come together in the analysis?
# How
## How and Where: Testing your shared project
- Try downloading and installing on a different computer to make sure that you can rerun analyses
- Take someone else through the process and test out the notebooks
- If making binder ready: test the repository on Binder
## How: Review Opportunities
- Review Opportunities
- Data House Calls
- PyOpenSci and ROpenSci for code review if you decide to package your code
- WILDS WDL Library
- WILDS Docker Library (?)
- If you’re making a package
## How and Where: Sharing Your Analyses
- GitHub (for code)
- Open Science Framework (for code + data)
- Field specific databases
- Social Media: LinkedIn, Bluesky, etc.
## Where should you share code?
Share in a public repository:
- GitHub
- Codeberg
- ReadtheDocs
Be aware of file size limitations!
## How: Data Repositories
- Data repositories
- Open Science Framework
- Required databases (dbGAP)
- Be aware that you will need to provide metadata
- Experimental design
- Be careful when sharing human subjects data
- If unsure, schedule a Data Governance House Call
## References
- <https://book.the-turing-way.org/reproducible-research/compendia/>
- https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005510
- https://openscapes.github.io/series/core-lessons/coding-strategies.html
- https://hutchdatascience.org/Tools_for_Reproducible_Workflows_in_R/index.html
- https://www.nature.com/articles/s41467-021-25974-w
- https://journals.plos.org/plosone/s/materials-software-and-code-sharing - Materials, Software and Code
- https://osf.io/cb7z8/files/numa5 - AGILE Reproducible Paper Guidelines
- https://binderhub.readthedocs.io/en/latest/overview.html
Source: https://book.the-turing-way.org/reproducible-research/code-reuse
## Outline (old)
- Defining reproducibility
- https://osf.io/numa5 reproducible paper checklist
- [NIH Best Practices for Sharing Software](https://datascience.nih.gov/tools-and-analytics/best-practices-for-sharing-research-software-faq#:~:text=Making%20software%20and%20code%20%E2%80%9Copen,accessible%20repository%20with%20version%20control)
- Repeatable
- The GitHub Ecosystem + Zenodo
- Open Science Framework
- Re-runnable
- Documentation
- Functions
- Packaging
- Portable
- Containerization
- Starting points
- BinderHub
- GitHub Codespaces