-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaminr.Rmd
240 lines (176 loc) · 6.15 KB
/
laminr.Rmd
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
---
title: "Get started"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Get started}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
# whether or not this code will be used to
# actually upload results to the LaminDB instance
# -> testuser1 is a test account that cannot upload results
submit_eval <- laminr:::.get_user_settings()$handle != "testuser1"
```
This vignette introduces the basic **{laminr}** workflow.
# Setup
Install **{laminr}** from CRAN:
```r
install.packages("laminr", dependencies = TRUE)
```
Set up the Python environment:
```r
laminr::install_lamindb()
```
Set the default LaminDB instance:
```r
laminr::lamin_connect("<owner>/<name>")
```
This instance acts as the default instance for everything that follows.
Any data and tracking information will be added to it.
See `vignette("setup", package = "laminr")` for more details.
# Start your analysis
Load **{laminr}** to get started.
```{r library}
library(laminr)
```
Create your default database `db` object for this R session:
```{r connect-default}
db <- connect()
```
It is used to manage all datasets and metadata entities.
LaminDB tracks which code is used to create data.
To track the current source code, run:
```{r track, eval = submit_eval}
db$track("I8BlHXFXqZOG0000")
```
<div class="alert alert-info" role="alert">
**Tip**
The UID (here `"I8BlHXFXqZOG0000"`) is obtained by running `db$track()` and copying the UID from the output.
</div>
# Connect to other instances
It is possible to connect to any LaminDB instance for reading data.
Connect to the public CELLxGENE instance:
```{r connect-cellxgene}
cellxgene <- connect("laminlabs/cellxgene")
cellxgene
```
# Download a dataset
Artifacts are objects that bundle data and associated metadata.
An artifact can be any file or folder but is typically a dataset.
```{r get-artifact}
artifact <- cellxgene$Artifact$get("7dVluLROpalzEh8mNyxk")
artifact
```
<div class="alert alert-info" role="alert">
**Tip**
You can view detailed information about this dataset on LaminHub: https://lamin.ai/laminlabs/cellxgene/artifact/7dVluLROpalzEh8mNyxk.
You can search and query more CELLxGENE datasets here: https://lamin.ai/laminlabs/cellxgene/artifacts.
</div>
To download the dataset and load it into memory, run:
```{r load-artifact}
adata <- artifact$load()
adata
```
This artifact contains an [`AnnData`](https://anndata.readthedocs.io) object.
<div class="alert alert-info" role="alert">
**Tip**
If you prefer a path to a local file or folder, call `path <- artifact$cache()`.
</div>
# Work with the dataset
Once you have loaded a dataset you can perform any analysis with it as you would normally.
Here, marker genes are calculated for each of the provided cell type labels using [**{Seurat}**](https://satijalab.org/seurat/).
```{r create-seurat}
library(Seurat)
# Create a Seurat object
seurat_obj <- CreateSeuratObject(
counts = as(Matrix::t(adata$X), "CsparseMatrix"),
meta.data = adata$obs
)
# Add gene metadata
seurat_obj[["RNA"]] <- AddMetaData(
GetAssay(seurat_obj), adata$var
)
# Set cell identities to the provided cell type annotation
Idents(seurat_obj) <- "cell_type"
# Normalise the data
seurat_obj <- NormalizeData(seurat_obj)
# Test for marker genes (the output is a data.frame)
markers <- FindAllMarkers(
seurat_obj,
features = Features(seurat_obj)[1:100] # Only test a few features for speed
)
# Display the marker genes
knitr::kable(markers)
# Plot the marker genes
DotPlot(seurat_obj, features = unique(markers$gene)) +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5))
```
# Slice a TileDB-SOMA array store
When artifacts contain TileDB-SOMA array stores they can be opened and sliced using the [**{tiledbsoma}** package](https://single-cell-data.github.io/TileDB-SOMA/index.html).
```{r slice-tiledbsoma, eval = requireNamespace("tiledbsoma", quietly = TRUE)}
# Set some environment variables to avoid an issue with {tiledbsoma}
# https://github.com/chanzuckerberg/cellxgene-census/issues/1261
Sys.setenv(TILEDB_VFS_S3_REGION = "us-west-2")
Sys.setenv(AWS_DEFAULT_REGION = "us-west-2")
Sys.setenv(TILEDB_VFS_S3_NO_SIGN_REQUEST = "true")
# Define a filter to select specific cells
value_filter <- paste(
"tissue == 'brain' &&",
"cell_type %in% c('microglial cell', 'neuron') &&",
"suspension_type == 'cell' &&",
"assay == '10x 3\\' v3'"
)
# Get the artifact containing the CELLxGENE Census TileDB-SOMA store
census_artifact <- cellxgene$Artifact$get("FYMewVq5twKMDXVy0001")
# Open the SOMACollection
soma_collection <- census_artifact$open()
# Slice the store to get a SOMADataFrame containing metadata for the cells of interest
cell_metadata <- soma_collection$get("census_data")$get("homo_sapiens")$obs$read(value_filter = value_filter)
# Concatenate the results to an arrow::Table
cell_metadata <- cell_metadata$concat()
# Convert to a data.frame
cell_metadata <- cell_metadata$to_data_frame()
cell_metadata
```
# Save the results
Save results as new artifacts to the default LaminDB instance.
```{r save-results, eval = submit_eval}
seurat_path <- tempfile(fileext = ".rds")
saveRDS(seurat_obj, seurat_path)
db$Artifact$from_df(
markers,
description = "Marker genes for renal cell carcinoma dataset"
)$save()
db$Artifact$from_path(
seurat_path,
description = "Seurat object for renal cell carcinoma dataset"
)$save()
```
# Mark the analysis as finished
Mark the analysis run as finished to create a time stamp and upload source code to the hub.
```{r finish, eval = submit_eval}
db$finish()
```
## Save a notebook report (not needed for `.R` scripts)
Save a run report of your notebook (`.Rmd` or `.qmd` file) to your instance:
1. Render the notebook to HTML
- In RStudio, click the "Knit" button
- **OR** From the command line, run:
```bash
Rscript -e 'rmarkdown::render("laminr.Rmd")'
```
- **OR** Use the `rmarkdown` package in R:
```r
rmarkdown::render("laminr.Rmd")
```
2. Save it to your LaminDB instance using the `lamin` CLI:
```bash
lamin save laminr.Rmd
```
# Further reading
For more details about how **{laminr}** works see `vignette("concepts_features", package = "laminr")`.