Diff of /docs/ondisk.Rmd [000000] .. [413088]

Switch to unified view

a b/docs/ondisk.Rmd
1
---
2
title: "ondisk"
3
output: 
4
  html_document:
5
    toc: true
6
    toc_float:
7
      collapsed: false
8
      smooth_scroll: false
9
---
10
11
<style>
12
.title{
13
  display: none;
14
}
15
body {
16
  text-align: justify
17
}
18
.center {
19
  display: block;
20
  margin-left: auto;
21
  margin-right: auto;
22
}
23
</style>
24
25
```{css, echo=FALSE}
26
.watch-out {
27
  color: black;
28
}
29
```
30
31
```{r setup, include=FALSE}
32
# use rmarkdown::render_site(envir = knitr::knit_global())
33
knitr::opts_chunk$set(highlight = TRUE, echo = TRUE)
34
```
35
36
<br>
37
38
## Import VisiumHD Data
39
40
We first have to download some packages that are necessary to import datasets from `.parquet` and `.h5` files provided by the VisiumHD readouts.
41
42
```{r class.source="watch-out", eval = FALSE}
43
install.packages("arrow")
44
BiocManager::install("rhdf5")
45
library(arrow)
46
library(rhdf5)
47
```
48
49
We use the **importVisiumHD** function to start analyzing the data. The data has 393401 spots which we will use OnDisk-backed methods to efficiently manipulate, analyze and visualize these spots.
50
51
The VisiumHD readouts provide multiple bin sizes which are aggregated versions of the original 2$\mu$m $x$ 2$\mu$m capture spots. The default bin sizes are **(i)** 2$\mu$m $x$ 2$\mu$m, **(ii)** 8$\mu$m $x$ 8$\mu$m and **(iii)** 16$\mu$m $x$ 16$\mu$m.
52
53
```{r class.source="watch-out", eval = FALSE}
54
hddata <- importVisiumHD(dir.path = "VisiumHD/outs/", 
55
                         bin.size = "8", 
56
                         resolution_level = "hires")
57
```
58
59
<br>
60
61
## Saving/Loading VoltRon Objects
62
63
We use **BPCells** and **ImageArray** packages to accelerate operations of feature matrices and images. Here **BPCells** allows users access and operate on large feature matrices or clustering/spatial analysis, while **ImageArray** provides [pyramids images](https://en.wikipedia.org/wiki/Pyramid_(image_processing)) to allow fast access to large microscopy images. You can download these package from GitHub using **devtools**.
64
65
```{r class.source="watch-out", eval = FALSE}
66
devtools::install_github("bnprks/BPCells/r")
67
devtools::install_github("BIMSBbioinfo/ImageArray")
68
library(BPCells)
69
library(ImageArray)
70
```
71
72
We can now save the VoltRon object to disk, large matrices and images will be written to either **hdf5** or **zarr** files depending on the **format** arguement, and the rest of the R object would be written to an `.rds` file, both under the designated **output**.
73
74
```{r class.source="watch-out", eval = FALSE}
75
hddata <- saveVoltRon(hddata, format = "HDF5VoltRon", output = "data/VisiumHD")
76
```
77
78
If you want you can load the VoltRon object from the same path as you have saved.
79
80
```{r class.source="watch-out", eval = FALSE}
81
hddata <- loadVoltRon("data/VisiumHD/")
82
```
83
84
<br>
85
86
## Cell/Spot Analysis
87
88
The **BPCells** package provides fast methods to achieve operations common to single cell analysis such as filtering, normalization and dimensionality reduction. Here we have an example of single-cell like clustering of VisiumHD bins which is efficiently clustered.
89
90
```{r class.source="watch-out", eval = FALSE}
91
spatialpoints <- vrSpatialPoints(hddata)[as.vector(Metadata(hddata)$Count > 10)]
92
hddata <- subset(hddata, spatialpoints = spatialpoints)
93
hddata <- normalizeData(hddata, sizefactor = 10000)
94
hddata <- getFeatures(hddata, n = 3000)
95
selected_features <- getVariableFeatures(hddata)
96
hddata <- getPCA(hddata, features = selected_features, dims = 30)
97
hddata <- getUMAP(hddata, dims = 1:30)
98
```
99
100
We can now visualized genes over embedding or spatial plots.
101
102
```{r class.source="watch-out", eval = FALSE}
103
vrEmbeddingFeaturePlot(hddata, features = "Nrgn", embedding = "umap")
104
vrSpatialFeaturePlot(hddata, features = "Nrgn")
105
```
106
  
107
<table>
108
<tbody>
109
  <tr style = "vertical-align: center">
110
  <td style = "width:50%; vertical-align: center"> <img src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/ondisk_embeddingfeature.png" class="center"></td>
111
  <td style = "width:55%; vertical-align: center"> <img src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/ondisk_spatialfeature.png" class="center"></td>
112
  </tr>
113
</tbody>
114
</table>
115
116
<br>
117
118
## Spatial Data Alignment
119
120
The image registration workflow in the [Spatial Data Alignment](registration.html) tutorial can also be conducted using disk-backed methods of the VoltRon package. 
121
122
```{r class.source="watch-out", eval = FALSE}
123
library(VoltRon)
124
Xen_R1 <- importXenium("Xenium_R1/outs", sample_name = "XeniumR1", resolution_level = 3)
125
Xen_R1_image <- importImageData("Xenium_FFPE_Human_Breast_Cancer_Rep1_he_image.tif",
126
                                sample_name = "XeniumR1image",
127
                                image_name = "H&E")
128
```
129
130
<br>
131
132
We can save both Xenium and H&E (image) datasets to disk before using the mini Shiny app for registration
133
134
```{r class.source="watch-out", eval = FALSE}
135
Xen_R1_disk <- saveVoltRon(Xen_R1, 
136
                           format = "HDF5VoltRon", 
137
                           output = "data/Xen_R1_h5", replace = TRUE)
138
Xen_R1_image_disk <- saveVoltRon(Xen_R1_image,
139
                                 format = "HDF5VoltRon", 
140
                                 output = "data/Xen_R1_image_h5", replace = TRUE)
141
```
142
143
<br>
144
145
These disk-based datasets can then be loaded from the disk easily. 
146
147
```{r class.source="watch-out", eval = FALSE}
148
Xen_R1_disk <- loadVoltRon("../data/OnDisk/Xen_R1_h5/")
149
Xen_R1_image_disk <- loadVoltRon("../data/OnDisk/Xen_R1_image_h5/")
150
```
151
152
<br>
153
154
VoltRon stores large images as pyramids to increase interactive visualization efficiency. This storage strategy allows shiny apps to zoom in to tissue niches in a speedy fashion. VoltRon incorporates `Image_Array` objects (https://github.com/BIMSBbioinfo/ImageArray) to define these pyramids. 
155
156
```{r class.source="watch-out", eval = FALSE}
157
vrImages(Xen_R1_image_disk, as.raster = TRUE)
158
```
159
160
```
161
Image_Array Object 
162
Series 1 of size (3,27587,20511) 
163
Series 2 of size (3,13794,10256) 
164
Series 3 of size (3,6897,5128) 
165
Series 4 of size (3,3449,2564) 
166
Series 5 of size (3,1725,1282) 
167
Series 6 of size (3,863,641) 
168
Series 7 of size (3,432,321) 
169
```
170
171
<br>
172
173
We can now visualize and align the Xenium and H&E objects. 
174
175
```{r class.source="watch-out", eval = FALSE}
176
# Align spatial data
177
xen_reg <- registerSpatialData(object_list = list(Xen_R1_disk, Xen_R1_image_disk))
178
```
179
180
<div>
181
  <video width="100%" height="100%" controls autoplay>
182
    <source src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/video_temp.mov" type="video/mp4">
183
  Your browser does not support the video tag.
184
  </video>
185
</div>
186
187
<br>
188
189
```{r class.source="watch-out", eval = FALSE}
190
# transfer aligned H&E to Xenium data
191
Xenium_reg <- xen_reg$registered_spat[[2]]
192
vrImages(Xen_R1_disk[["Assay1"]], name = "main", channel = "H&E") <- vrImages(Xenium_reg, name = "H&E_reg")
193
194
# visualize
195
vrImages(Xen_R1_disk, channel = "H&E", scale.perc = 10)
196
```
197
198
<br>
199
200
<img width="92%" height="92%" src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/ondisk_alignedHE.png" class="center">