Skip to content

Commit b52ceab

Browse files
committed
Went through 1a and 1b again after Juna's changes. started to work on 1c
1 parent f8cefe3 commit b52ceab

4 files changed

Lines changed: 49 additions & 35 deletions

File tree

day1/day1-1a_SpatialExperiment.qmd

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ library(HDF5Array)
5353

5454
## Data for the course
5555

56-
We will work on a human colorectal cancer study by [Oliveira _et al_](https://www.nature.com/articles/s41588-025-02193-3). The dataset contains normal adjacent tissue (NAT) and colorectal carcinoma (CRC) from 5 patients.
56+
We will work on a human colorectal cancer study by [Oliveira _et al_](https://www.nature.com/articles/s41588-025-02193-3). The dataset contains normal adjacent tissue (NAT) and colorectal carcinoma (CRC) from 5 patients. We will focus on data from a patient called "P2", available from the [10X website](https://www.10xgenomics.com/datasets/visium-hd-cytassist-gene-expression-libraries-of-human-crc).
5757

58-
We will focus on data from a patient called "P2", available from the [10X website](https://www.10xgenomics.com/datasets/visium-hd-cytassist-gene-expression-libraries-of-human-crc).
59-
For this sample, the study provides Visium HD data at 2, 8, and 16 um binned resolutions and output from cell segmentation, as well as a matching Xenium dataset. The data were generated from serial sections of the same samples, so they are spatially matched and can be used for cross-platform comparisons.
58+
For this sample, the study provides Visium HD data at 2, 8, and 16 um resolution and the per-cell output after cell segmentation. Matching Xenium data generated from serial sections of the same sample is also available and can be used for cross-platform comparisons.
6059

6160
In this exercise we will work with **Visium HD** data and focus on the "binned output" available as an output of the Space Ranger pipeline (version 3.0). The full output is very large, so for practical reasons we use a region of interest, selected to include an interesting part of the tissue, because it contains gland-like epithelial structures together with stromal or lower-density areas:
6261

@@ -83,7 +82,7 @@ osta_roi_visium <- c(
8382
osta_roi_visium
8483
```
8584

86-
*Note 2*: The same study also generated a matching Xenium dataset from serial sections of the same samples, which we will use in `Exercise 1 B` (we subsetted to regions of interest of the P2CRC slide, approximately matching the one selected here).
85+
*Note 2*: In `Exercise 1B` we focus on the matching Xenium dataset from a serial section of the same P2CRC sample, subsetted to an approximately matching region of interest.
8786

8887

8988
```{r day1-1a-SpatialExperiment-5}
@@ -108,7 +107,7 @@ if (!dir.exists("data/Human_Colon_Cancer_P2/")) {
108107
}
109108
```
110109

111-
We will import the 16 um binned Visium HD output into a `SpatialExperiment` object and then subset it to the course region. The coordinates are in the native Visium HD image coordinate system returned by `spatialCoords()`.
110+
We will import the 16 um binned Visium HD output into a `SpatialExperiment` object and then subset it to the region of internet. The coordinates are in the native Visium HD image coordinate system returned by `spatialCoords()`.
112111

113112
```{r day1-1a-SpatialExperiment-6}
114113
spe <- TENxVisiumHD(
@@ -160,7 +159,7 @@ In this exercise, we will inspect the `SpatialExperiment` class used to store th
160159

161160
![Overview of the `SpatialExperiment` class structure](../assets/images/spe.png)
162161

163-
*Note*: The practical `Exercise 1 B` uses a Xenium dataset stored into a `SpatialFeatureExperiment` class, which is an extension of the `SpatialExperiment` incorporating geometries, allowing to store cell segmentation polygons for example.
162+
*Note*: The practical `Exercise 1B` uses a Xenium dataset stored into a `SpatialFeatureExperiment` class, which is an extension of the `SpatialExperiment` incorporating geometries, allowing to store cell segmentation polygons for example.
164163

165164

166165
::: callout-important
@@ -220,7 +219,7 @@ assay(spe)
220219
class(assay(spe))
221220
```
222221

223-
The `DelayedArray` class allows to store out-of-memory representations of count matrices, which are saved as .h5 files on the disk. This is very useful for efficient processing of large datasets (100,000s of cells or spots)
222+
The `DelayedArray` class allows to store out-of-memory representations of count matrices, which are saved as `.h5` files on the disk. This is very useful for efficient processing of large datasets (100,000s of cells or spots)
224223

225224
The `reducedDims` slot is empty at this stage:
226225

@@ -270,7 +269,7 @@ Why do we see only a specific region of the slide?
270269
::: {.callout-tip collapse="true"}
271270
## Answer
272271

273-
The full Visium HD output is much larger than needed for this introductory exercise. At the start of this exercise, we subsetted the object to one representative tissue region so that it remains fast to plot and process. This same region is also used as the anchor for selecting a comparable Xenium region in `Exercise 1 B`.
272+
The full Visium HD output is much larger than needed for this introductory exercise. At the start of this exercise, we subsetted the object to one representative tissue region so that it remains fast to plot and process. This same region is also used as the anchor for selecting a comparable Xenium region in `Exercise 1B`.
274273
:::
275274

276275
It is possible to colour bins according to a gene or a column in `colData`, for example `PIGR`. We renamed the rows to gene symbols after import, which is why we can refer to this marker as `"PIGR"` rather than by its Ensembl ID. At this stage we plot the number of UMIs since log-normalized counts will be introduced later in the course.

day1/day1-1b_SpatialFeatureExperiment.qmd

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ By the end of this exercise, you will be able to:
1616
- Explain when `SpatialFeatureExperiment` is useful compared with `SpatialExperiment`.
1717
- Access cell-level metadata and spatial coordinates from Xenium data.
1818
- Inspect geometry information such as cell or nucleus boundaries.
19-
- Compare the Xenium representation with the Visium HD representation from Exercise 1 A.
19+
- Compare the Xenium representation with the Visium HD representation from Exercise 1A.
2020

2121
## Libraries
2222

@@ -40,7 +40,7 @@ Next, will work with the **Xenium** dataset from the P2 sample (human colorectal
4040

4141
This slide is a serial section of the sample used in `Exercise 1A`, so the two datasets are biologically related but not the exact same physical tissue section.
4242

43-
The full Xenium output is very large and includes cell boundaries, nucleus boundaries, transcript locations, morphology images, and expression matrices. For this exercise, for practical reasons we use a region of interest, selected to **approximately match the region of interest used in `Exercise 1 A`**, and only include some of the layers in the imported compact course folder. This makes the two exercises comparable while avoiding a full image-registration workflow during the practical.
43+
The full Xenium output is very large and includes cell boundaries, nucleus boundaries, transcript locations, morphology images, and expression matrices. For this exercise, for practical reasons we use a region of interest, selected to **approximately match the region of interest used in `Exercise 1A`**, and only include some of the layers in the imported compact course folder. This makes the two exercises comparable while avoiding a full image-registration workflow during the practical.
4444

4545

4646
```{r day1-1b-SFE-2}
@@ -77,7 +77,7 @@ sfe <- sfe_full[, spatialCoords(sfe_full)[, 1] >= roi_xenium[["xmin"]] &
7777
spatialCoords(sfe_full)[, 1] <= roi_xenium[["xmax"]] &
7878
spatialCoords(sfe_full)[, 2] >= roi_xenium[["ymin"]] &
7979
spatialCoords(sfe_full)[, 2] <= roi_xenium[["ymax"]]]
80-
80+
rm(sfe_full)
8181
sfe
8282
```
8383

@@ -117,7 +117,7 @@ Questions:
117117

118118
- What do the columns represent?
119119
- What do the rows represent?
120-
- What differs compared to the Visium HD `spe` object in `Exercise 1 A`?
120+
- What differs compared to the Visium HD `spe` object in `Exercise 1A`?
121121
- What is stored in the main assay?
122122
:::
123123

@@ -185,7 +185,7 @@ ggplot(xy, aes(x = x, y = y)) +
185185
::: callout-important
186186
## Exercise 4
187187

188-
Compare this point-based visualization with the Visium HD plot from `Exercise 1 A`.
188+
Compare this point-based visualization with the Visium HD plot from `Exercise 1A`.
189189

190190
What does one point represent in each dataset?
191191
:::
@@ -207,17 +207,33 @@ plotSpatialFeature(sfe, "PIGR", exprs_values = "counts")
207207

208208
Choose another marker gene and visualize its spatial pattern.
209209

210-
How does the interpretation differ from plotting a gene on Visium HD bins?
211-
212-
:::
213-
<!-- JUNA: If we want them to compare to Visium HD we should not delete environment in previous exercise, or load the spe object here as part of the solution -->
214-
215-
210+
How does the interpretation differ from plotting a gene on Visium HD bins? How does the expression of the same gene compares across platforms?
216211

217212
::: {.callout-tip collapse="true"}
218213
## Answer
219214

220215
The Xenium signal is measured over segmented cells, while the Visium HD binned signal aggregates molecules within fixed spatial bins. Xenium can therefore represent cell-level heterogeneity more directly, but it measures a targeted panel rather than the whole transcriptome.
216+
217+
Here is some code to plot the expression of the same gene across platforms in a similar way:
218+
```{r day1-1b-SFE-12}
219+
xy <- data.frame(spatialCoords(sfe), colData(sfe), PIGR=counts(sfe)["PIGR",] + 1)
220+
names(xy)[1:2] <- c("x", "y")
221+
ggplot(xy, aes(x = x, y = y, color=PIGR)) +
222+
geom_point(size = 0.2) +
223+
coord_fixed() +
224+
scale_color_gradient(trans = "log", low = "white", high = "red") +
225+
theme_bw()
226+
227+
## load the object from Exercise 1A
228+
spe <- loadHDF5SummarizedExperiment(dir="results/day1/", prefix="01.1_spe_")
229+
xy <- data.frame(spatialCoords(spe), colData(spe), PIGR=counts(spe)["PIGR",] + 1)
230+
names(xy)[1:2] <- c("x", "y")
231+
ggplot(xy, aes(x = x, y = y, color=PIGR)) +
232+
geom_point(size = 1, shape=15) +
233+
coord_fixed() +
234+
scale_color_gradient(trans = "log", low = "white", high = "red") +
235+
theme_bw()
236+
```
221237
:::
222238

223239
## Exercise 6
@@ -227,7 +243,7 @@ Use the `plotSpatialFeature()` function to visualize the cell segmentation mask,
227243
::: {.callout-tip collapse="true"}
228244
## Answer
229245

230-
```{r day1-1b-SFE-12}
246+
```{r day1-1b-SFE-13}
231247
plotSpatialFeature(sfe,
232248
colGeometryName="cellSeg",
233249
features="cell_area")
@@ -237,22 +253,22 @@ plotSpatialFeature(sfe,
237253
features="nucleus_area")
238254
```
239255
:::
240-
1
256+
241257
## Save the object
242258

243-
```{r day1-1b-SFE-13}
259+
```{r day1-1b-SFE-14}
244260
dir.create("results/day1", showWarnings = FALSE, recursive = TRUE)
245261
246262
saveHDF5SummarizedExperiment(sfe,
247-
dir = "results/day1", prefix = "01.1b_sfe_xenium", replace = TRUE,
263+
dir = "results/day1", prefix = "01.1b_sfe_xenium_", replace = TRUE,
248264
chunkdim = NULL, level = NULL, as.sparse = NA,
249265
verbose = NA
250266
)
251267
```
252268

253269
Clear your environment:
254270

255-
```{r day1-1b-SFE-14}
271+
```{r day1-1b-SFE-15}
256272
#| eval: false
257273
rm(list = ls())
258274
gc()
@@ -263,6 +279,6 @@ gc()
263279
**Key Takeaways:**
264280

265281
- `SpatialFeatureExperiment` is useful for geometry-rich spatial data.
266-
- Xenium observations are cell-resolved, while the Visium HD object in Exercise 1 A is bin-resolved.
282+
- Xenium observations are cell-resolved, while the Visium HD object in Exercise 1A is bin-resolved.
267283
- The P2 CRC subsets are designed to be comparable in course scope and approximate tissue region, not identical at cell level.
268284
:::

day1/day1-1c_visium_HD_segmented.qmd

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ execute:
1313
By the end of this exercise, you will be able to:
1414

1515
- TO DEFINE
16-
<!-- Adapt depending on the final data structure -->
16+
<!-- TO DO: Adapt depending on the final data structure -->
1717

1818

1919
## Libraries
@@ -36,8 +36,7 @@ library(VisiumIO)
3636

3737
Finally, we will work with the **Visium HD segmented output**. This data corresponds to the same sample and region of interest as the binned data from `Exercise 1A`, but instead of being binned, it contains the output of cell segmentation. Since Space Ranger version 4.0, the output of cell segmentation is available as an optional output of the pipeline, and it contains the coordinates of the segmented cells and their boundaries.
3838

39-
<!-- Download the segmented output from the 10X website if it does not exist in the `data` folder. -->
40-
39+
<!-- TO DO: we need to make a smaller version of teh dataset for the course! -->
4140

4241
```{r day1-1c-visium_HD_segmented-3}
4342
options(timeout = 600)
@@ -63,20 +62,20 @@ if (!dir.exists("data/Visium_HD_Human_Colon_Cancer_segmented_outputs/")) {
6362

6463
::: callout-important
6564
## Exercise 1
66-
Why will we use SpatialFeatureExperiment to work with the segmented data instead of SpatialExperiment as we did for the binned data in Exercise 1A
67-
?
65+
Which class do you think should be used to store the Visium HD segmented data? `SpatialExperiment` as we used in Exercise 1A
66+
or `SpatialFeatureExperiment` as we used in Exercise 1B?
6867
:::
6968

7069
::: {.callout-tip collapse="true"}
7170
## Answer
72-
Since segmeneted data contains cell boundaries, the coordinates of the segmented cells are not limited to the spot coordinates as in the binned data. Therefore, we will use `SpatialFeatureExperiment` to work with the segmented data, as it allows us to store and visualize spatial features such as cell boundaries, as we did for the Xenium data in Exercise 1B.
71+
Since segmented data contains cell boundaries, the coordinates of the segmented cells are not limited to the spot coordinates as in the binned data. Therefore, we will use `SpatialFeatureExperiment` to work with the segmented data, as it allows us to store and visualize spatial features such as cell boundaries, as we did for the Xenium data in Exercise 1B.
7372
In contrast, `SpatialExperiment` is more suitable for binned data where the coordinates correspond to the spot centroids and there are no additional spatial features to store.
7473
:::
7574

7675

77-
We will create the `SpatialFeatureExperiment` object in two steps.
78-
79-
We start reading the object into a `SpatialExperiment` object and subset it to the course region, as we have done for visium HD binned data in the previous exercises.
76+
Here we will proceed in two steps:
77+
- We start reading the object into a `SpatialExperiment` object and subset it to the region of interest, as we have done for Visium HD binned data in the previous exercises. This allows a quick comparison of the two types of processing for the same slide
78+
- We then convert the object to create a `SpatialFeatureExperiment` object and add the cell segmentation information to it/
8079

8180

8281
```{r day1-1c-visium_HD_segmented-4}

day2/day2-1b_normalization_xenium.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ library(HDF5Array)
4545
We start with the Xenium object saved at the end of Exercise 1B.
4646

4747
```{r}
48-
sfe <- loadHDF5SummarizedExperiment(dir = "results/day1", prefix = "01.1b_sfe_xenium")
48+
sfe <- loadHDF5SummarizedExperiment(dir = "results/day1", prefix = "01.1b_sfe_xenium_")
4949
sfe
5050
```
5151

0 commit comments

Comments
 (0)