-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintroduction.Rmd
More file actions
367 lines (255 loc) · 9.05 KB
/
Copy pathintroduction.Rmd
File metadata and controls
367 lines (255 loc) · 9.05 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
---
title: "Introduction"
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r prepare, include = FALSE}
lc <- laminr::import_module("lamin_cli")
# Restore the current user at the end of the vignette
current_user <- laminr::get_current_lamin_user()
lc$logout()
withr::defer({
lc <- laminr::import_module("lamin_cli")
lc$login(current_user)
})
# Set up a temporary test instance
laminr::use_temporary_instance(name = "laminr-intro", modules = c("bionty"))
```
This vignette introduces the **{laminr}** workflow but reproducing examples from the LaminDB documentation.
To learn more about LaminDB, see [docs.lamin.ai](https://docs.lamin.ai).
# Introduction
## Setup
Install the **{laminr}** package.
```r
install.packages("laminr", dependencies = TRUE)
```
Create a LaminDB instance:
```
lc <- laminr::import_module("lamin_cli")
lc$init(storage = "./lamin-tutorial", modules = c("bionty"))
```
Or if you have write access to an instance, login and connect to it:
```
lc$login()
lc$connect("<account>/<instance>")
```
Alternatively, connect after importing the **lamindb** module:
```
ln <- laminr::import_module("lamindb")
ln$connect("<account>/<instance>")
```
## Quickstart
In an R session, transfer an scRNA-seq dataset from the `laminlabs/cellxgene` instance, compute marker genes with **{Seurat}**, and save results.
```{r file = "../../test-docs/r-quickstart.R", eval = FALSE}
```
If you did not use RStudio’s notebook mode, create an html export and then run the following.
```r
lc$save("my-analyis.Rmd") # save source code and html report for a `.qmd` or `.Rmd` file
```
# Tutorial
See https://docs.lamin.ai/tutorial
## Track notebooks & scripts
See https://docs.lamin.ai/tutorial#track-notebooks-scripts
```{r track}
library("laminr")
ln <- import_module("lamindb")
ln$track()
ln$Transform$to_dataframe()
ln$Run$to_dataframe()
```
## Manage artifacts
See https://docs.lamin.ai/tutorial#manage-artifacts
### Create an artifact
See https://docs.lamin.ai/tutorial#create-an-artifact
```{r create-artifact}
df <- ln$examples$datasets$mini_immuno$get_dataset1(with_typo = TRUE)
df
artifact <- ln$Artifact$from_dataframe(df, key = "my_datasets/rnaseq1.parquet")$save() # create and save
artifact$describe() # describe
```
### Access artifacts
See https://docs.lamin.ai/tutorial#access-artifacts
```{r access-artifacts}
artifact <- ln$Artifact$get(key = "my_datasets/rnaseq1.parquet")
artifact$load()
artifact$cache()
```
### Trace data lineage
See https://docs.lamin.ai/tutorial#trace-data-lineage
```{r trace-lineage}
artifact$transform
artifact$run
artifact$view_lineage()
# ln$finish() # nolint
```
### Annotate an artifact
See https://docs.lamin.ai/tutorial#annotate-an-artifact
```{r annotate-artifact}
# create a label
my_experiment <- ln$Record(name = "My experiment")$save()
# annotate the artifact with a label
artifact$records$add(my_experiment)
# describe the artifact
artifact$describe()
ln$Artifact$filter(records = my_experiment)$to_dataframe()
bt <- import_module("bionty")
# create a cell type label from the source ontology
cell_type <- bt$CellType$from_source(name = "effector T cell")$save()
# annotate the artifact with a cell type
artifact$cell_types$add(cell_type)
# describe the artifact
artifact$describe()
ln$Artifact$filter(cell_types = cell_type)$to_dataframe()
# define the "temperature" & "experiment" features
ln$Feature(name = "temperature", dtype = "float")$save()
ln$Feature(name = "experiment", dtype = ln$Record)$save()
# annotate the artifact
artifact$features$add_values(
list(temperature = 21.6, experiment = "My experiment")
)
# describe the artifact
artifact$describe()
ln$Artifact$filter(temperature = 21.6)$to_dataframe()
```
### Validate an artifact
See https://docs.lamin.ai/tutorial#validate-an-artifact
```{r validate-artifact}
bt <- import_module("bionty") # <-- use bionty to access registries with imported public ontologies
# define a few more valid labels
ln$Record(name = "DMSO")$save()
ln$Record(name = "IFNG")$save()
# define a few more valid features
ln$Feature(name = "perturbation", dtype = ln$Record)$save()
ln$Feature(name = "cell_type_by_model", dtype = bt$CellType)$save()
ln$Feature(name = "cell_type_by_expert", dtype = bt$CellType)$save()
ln$Feature(name = "assay_oid", dtype = bt$ExperimentalFactor$ontology_id)$save()
ln$Feature(name = "donor", dtype = "str", nullable = TRUE)$save()
ln$Feature(name = "concentration", dtype = "str")$save()
ln$Feature(name = "treatment_time_h", dtype = "num", coerce_dtype = TRUE)$save()
# define a schema that merely enforces a feature identifier type
schema <- ln$Schema(itype = ln$Feature)$save()
testthat::expect_error(
artifact <- ln$Artifact$from_dataframe(df, key = "my_datasets/rnaseq1.parquet", schema = schema)
)
```
### Make a new version of an artifact
See https://docs.lamin.ai/tutorial#make-a-new-version-of-an-artifact
```{r version-artifact}
# fix the "IFNJ" typo
levels(df$perturbation) <- c("DMSO", "IFNG")
df["sample2", "perturbation"] <- "IFNG"
# create a new version
artifact <- ln$Artifact$from_dataframe(df, key = "my_datasets/rnaseq1.parquet", schema = schema)$save()
# see the annotations
artifact$describe()
# simplest way to check that artifact was validated
artifact$schema
# see all versions of an artifact
artifact$versions$to_dataframe()
```
## Query & search registries
See https://docs.lamin.ai/tutorial#query-search-registries
```{r query-registries}
ln$Artifact$to_dataframe()
ln$Artifact$to_dataframe(include = "features")
ln$Artifact
ln$view()
# get a single record (here the current notebook)
transform <- ln$Transform$get(key = "introduction.Rmd")
# get a set of records by filtering for a directory (LaminDB treats directories
# like AWS S3, as the prefix of the storage key)
ln$Artifact$filter(key__startswith = "my_datasets/")$to_dataframe()
# query all artifacts ingested from a transform
artifacts <- ln$Artifact$filter(transform = transform)$all()
# query all artifacts ingested from a notebook with "tutor" in the description
artifacts <- ln$Artifact$filter(
transform__description__icontains = "tutor",
)$all()
# search artifacts
ln$Artifact$search("iris")$to_dataframe()
# search transforms
ln$Transform$search("tutor")$to_dataframe()
# look up records with auto-complete
ulabels <- ln$Record$lookup()
```
## Manage files & folders
See https://docs.lamin.ai/tutorial#manage-files-folders
```{r manage-files}
# we use anon=True here in case no aws credentials are configured
ln$UPath("s3://lamindata/iris_studies", anon = TRUE)$view_tree()
artifact <- ln$Artifact("s3://lamindata/iris_studies/study0_raw_images")$save()
artifact
artifact$path
ln$Storage$to_dataframe()
```
## Manage biological registries
See https://docs.lamin.ai/tutorial#manage-biological-registries
```{r manage-registries}
bt <- import_module("bionty")
cell_type_ontology <- bt$CellType$public()
cell_type_ontology
cell_type_ontology$search("gamma-delta T cell") |> head(n = 2)
# create an ontology-coupled cell type record and save it
neuron <- bt$CellType$from_source(name = "neuron")$save()
# create a record to track a new cell state
new_cell_state <- bt$CellType(
name = "my neuron cell state", description = "explains X"
)$save()
# express that it's a neuron state
new_cell_state$parents$add(neuron)
# view ontological hierarchy
new_cell_state$view_parents(distance = 2)
```
## Manage AnnData object
See https://docs.lamin.ai/tutorial#manage-anndata-objects
```{r manage-anndata}
# define var schema
var_schema <- ln$Schema(itype = bt$Gene$ensembl_gene_id, dtype = "int")$save()
# define composite schema
anndata_schema <- ln$Schema(
otype = "AnnData", slots = list("obs" = schema, "var.T" = var_schema)
)$save()
ad <- import_module("anndata")
# store the dataset as an AnnData object to distinguish data from metadata
adata <- ad$AnnData(df[, 1:3], obs = df[, 4:(ncol(df) - 1)])
# save curated artifact
artifact <- ln$Artifact$from_anndata(
adata,
key = "my_datasets/my_rnaseq1.h5ad", schema = anndata_schema
)$save()
artifact$describe()
# query for all schemas that contain CD8A
schemas <- ln$Schema$filter(genes__symbol = "CD8A")$all()
# query for all artifacts linked to these feature sets
ln$Artifact$filter(schemas__in = schemas)$to_dataframe()
```
## Scale learning
See https://docs.lamin.ai/tutorial#scale-learning
```{r scale-learning}
# a new dataset
df2 <- ln$examples$datasets$mini_immuno$get_dataset2(otype = "DataFrame")
adata <- ad$AnnData(df2[, 1:3], obs = df2[, 4:(ncol(df2) - 1)])
artifact2 <- ln$Artifact$from_anndata(
adata,
key = "my_datasets/my_rnaseq2.h5ad", schema = anndata_schema
)$save()
collection <- ln$Collection(list(artifact, artifact2), key = "my-RNA-seq-collection")$save()
collection$describe()
collection$view_lineage()
# if it's small enough, you can load the entire collection into memory as if it was one
collection$load()
# typically, it's too big, hence, open it for streaming (if the backend allows it)
# collection.open() # nolint
# or iterate over its artifacts
collection$artifacts$all()
# or look at a DataFrame listing the artifacts
collection$artifacts$to_dataframe()
```
# Finish
```{r finish}
ln$finish()
```