Skip to content

Commit 54065f7

Browse files
committedMay 21, 2024·
update sessions
1 parent 4a02cba commit 54065f7

File tree

3 files changed

+427
-87
lines changed

3 files changed

+427
-87
lines changed
 

‎vignettes/Session_1_sequencing_assays.Rmd

+17-8
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ reducedDimNames(spatial_data)
463463
reducedDim(spatial_data, "PCA")[1:5, 1:5]
464464
```
465465

466-
::: note
466+
::: {.note}
467467
As for single-cell data, we need to verify that there is not significant batch effect. If so we need to adjust for it (a.k.a. integration) before calculating principal component. Many adjustment methods to output adjusted principal components directly.
468468
:::
469469

@@ -481,7 +481,7 @@ spatial_data <- scater::runUMAP(spatial_data, dimred = "PCA")
481481
scater::plotUMAP(spatial_data, colour_by = "sample_id", point_size = 0.2)
482482
```
483483

484-
::: note
484+
::: {.note}
485485
**Exercise 1.1**
486486

487487
Visualise where the two macro clusters are located spatially. We will take a very pragmatic approach and get cluster label from splitting the UMAP coordinated in two (`colData()` and `reducedDim()` will help us, see above), and then visualise it with `ggspavis`.
@@ -631,7 +631,7 @@ spe_joint <- do.call(cbind, spatial_data_list)
631631

632632
Here, we perform PCA using the BANKSY algorithm on the joint dataset. The group argument specifies how to treat different samples, ensuring that features are scaled separately per sample group to account for variations among them.
633633

634-
::: note
634+
::: {.note}
635635
Note: this step takes long time
636636
:::
637637

@@ -740,7 +740,7 @@ ggspavis::plotSpots(spatial_data, annotate = "spatialLIBD") +
740740
labs(title = "spatialLIBD regions")
741741
```
742742

743-
::: note
743+
::: {.note}
744744
**Exercise 1.2**
745745

746746
We have applied cluster smoothing using `smoothLabels`. How much do you think this operation has affected the cluster labels. To find out,
@@ -955,7 +955,7 @@ plotCorrelationMatrix(res$mat)
955955

956956

957957

958-
::: note
958+
::: {.note}
959959
**Exercise 1.3**
960960

961961
Rather than looking at the correlation matrix, overall, let's observe whether the correlation structure amongst cell types is consistent across samples. Do you think it's consistent or noticeably different?
@@ -967,7 +967,7 @@ Rather than looking at the correlation matrix, overall, let's observe whether th
967967
lapply(res_spatialLIBD, function(x) plotCorrelationMatrix(as.matrix(x[,-10])))
968968
```
969969

970-
::: note
970+
::: {.note}
971971
**Exercise 1.4**
972972

973973
Now let's observe whether the correlation structure is consistent across spatial regions, irrespectively of the sample of origin. Do you think they are consistent or noticably different?
@@ -980,18 +980,27 @@ lapply(res_spatialLIBD, function(x) plotCorrelationMatrix(as.matrix(x[,-10])))
980980
981981
```
982982

983-
::: note
983+
::: {.note}
984984
**Exercise 1.5**
985985

986-
Some of the most positive correlations involve the end of cells with Oligodendrocytes and Leptomeningeal cells.
986+
Some of the most positive correlations involve the endothelial cells with Oligodendrocytes and Leptomeningeal cells.
987987

988988
Leptomeningeal cells refer to the cells that make up the leptomeninges, which consist of two of the three layers olet's meninges surrounding the brain and spinal cord: the arachnoid mater and the pia mater. These layers play a critical role in protecting the central nervous system and assisting in various physiological processes.
989989

990990
Oligodendrocytes are a type of glial cell in the central nervous system (CNS) of vertebrates, including humans and mouse. These cells are crucial for the formation and maintenance of the myelin sheath, a fatty layer that encases the axons of many neurons.
991991

992992
Let's try to visualise the pixel where these cell types most occur.
993+
994+
995+
- Label pixel that have > 10% (> 0.1) endothelial_cell and leptomeningeal_cell
996+
- Label pixels that have > 40% (> 0.4) across these two cells
997+
- Plot pixels colouring by the new label
998+
993999
:::
9941000

1001+
```{r}
1002+
mat_df = as.data.frame(res$mat)
1003+
```
9951004

9961005
**Session Information**
9971006

‎vignettes/Session_2_Tidy_spatial_analyses.Rmd

+273-42
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ style="width:100%; height:600px;"
4444
frameborder="0">
4545
</iframe>
4646

47+
#### Installation
48+
49+
let's make sure we get the latest packages available on github
50+
51+
```{r, eval=FALSE}
52+
53+
# In May 2024, the following packages should be installed from github repositories, to use the latest features. In case you have them pre installed, run the following command
54+
BiocManager::install(c("lmweber/ggspavis",
55+
"stemangiola/tidySummarizedExperiment",
56+
"stemangiola/tidySingleCellExperiment",
57+
"william-hutchison/tidySpatialExperiment",
58+
"stemangiola/tidybulk",
59+
"stemangiola/tidygate",
60+
"stemangiola/CuratedAtlasQueryR"),
61+
update = FALSE)
62+
63+
```
64+
65+
**Then please restart your R session** to make sure the packages we will load will be the ones we intalled mode recently.
66+
4767
Let's load the libraries needed for this session
4868

4969
```{r, message = FALSE}
@@ -87,6 +107,9 @@ spatial_data <-
87107
# Clear the reductions
88108
reducedDims(spatial_data) = NULL
89109
110+
# Make cell ID unique
111+
colnames(spatial_data) = paste0(colnames(spatial_data), colData(spatial_data)$sample_id)
112+
90113
# Display the object
91114
spatial_data
92115
```
@@ -120,6 +143,10 @@ options("restore_SpatialExperiment_show" = FALSE)
120143
spatial_data
121144
```
122145

146+
::: {.note}
147+
Note that **rows** in this context refers to rows of the abstraction, not **rows** of the SpatialExperiment which correspond to genes **tidySpatialExperiment** prioritizes cells as the units of observation in the abstraction, while the full dataset, including measurements of expression of all genes, is still available "in the background".
148+
:::
149+
123150
#### Original behaviour is preserved
124151

125152
The tidy representation behaves exactly as a native `SpatialExperiment`. It can be interacted with using [SpatialExperiment commands](https://www.bioconductor.org/packages/release/bioc/vignettes/SpatialExperiment/inst/doc/SpatialExperiment.html)
@@ -133,10 +160,46 @@ assays(spatial_data)
133160

134161
We can also interact with our object as we do with any tidyverse tibble. We can use `tidyverse` commands, such as `filter`, `select` and `mutate` to explore the `tidySpatialExperiment` object. Some examples are shown below and more can be seen at the `tidySpatialExperiment` [website](https://stemangiola.github.io/tidySpatialExperiment/articles/introduction.html#tidyverse-commands-1).
135162

163+
#### Select
164+
165+
We can use `select` to view columns, for example, to see the filename, total cellular RNA abundance and cell phase.
166+
167+
If we use `select` we will also get any view-only columns returned, such as the UMAP columns generated during the preprocessing.
168+
169+
```{r}
170+
spatial_data |> select(.cell, sample_id, in_tissue, spatialLIBD)
171+
```
172+
173+
::: {.note}
174+
Note that some columns are always displayed no matter whet. These column include special slots in the objects such as reduced dimensions, spatial coordinates (mandatory for `SpatialExperiment`), and sample identifier (mandatory for `SpatialExperiment`).
175+
:::
176+
177+
Although the select operation can be used as a display tool, to explore our object, it updates the `SpatialExperiment` metadata, subsetting the desired columns.
178+
179+
```{r}
180+
spatial_data |>
181+
select(.cell, sample_id, in_tissue, spatialLIBD) |>
182+
colData()
183+
```
184+
185+
To select columns of interest, we can use `tidyverse` powerful pattern-matching tools. For example, using the method `contains` to select
186+
187+
```{r}
188+
189+
spatial_data |>
190+
select(.cell, contains("sum"))
191+
```
192+
193+
136194
#### Filter
137195

138-
We can use `filter` to choose rows, for example, to select our three samples we are going to work with.
196+
We can use `filter` to subset rows, for example, to keep our three samples we are going to work with.
197+
198+
We just display the dimensions of the dataset before filtering
139199

200+
```{r}
201+
nrow(spatial_data)
202+
```
140203

141204
```{r}
142205
spatial_data =
@@ -146,6 +209,12 @@ spatial_data =
146209
spatial_data
147210
```
148211

212+
Here we confirm that the tidy R manipulation has changed the underlining object.
213+
214+
```{r}
215+
nrow(spatial_data)
216+
```
217+
149218
In comparison the base-R method recalls the variable multiple times
150219

151220
```{r, eval=FALSE}
@@ -155,35 +224,69 @@ spatial_data = spatial_data[,spatial_data$sample_id %in% c("151673", "151675", "
155224
Or for example, to see just the rows for the cells in G1 cell-cycle stage.
156225

157226
```{r}
158-
spatial_data |> dplyr::filter(spatialLIBD == "L1")
227+
spatial_data |> dplyr::filter(sample_id == "151673", spatialLIBD == "L1")
159228
```
160229

161-
:::: {.note}
162-
Note that **rows** in this context refers to rows of the abstraction, not **rows** of the SpatialExperiment which correspond to genes **tidySpatialExperiment** prioritizes cells as the units of observation in the abstraction, while the full dataset, including measurements of expression of all genes, is still available "in the background".
163-
::::
230+
Flexible, more powerful filters with `stringr`
164231

165-
#### Select
232+
```{r}
166233
167-
We can use `select` to view columns, for example, to see the filename, total cellular RNA abundance and cell phase.
234+
spatial_data |>
235+
dplyr::filter(
236+
subject |> str_detect("Br[0-9]1"),
237+
spatialLIBD == "L1"
238+
)
168239
169-
If we use `select` we will also get any view-only columns returned, such as the UMAP columns generated during the preprocessing.
240+
```
241+
242+
#### Summarise
243+
244+
The integration of all spot/pixel/cell-related information in one table abstraction is very powerful to speed-up data exploration ana analysis.
170245

171246
```{r}
172-
spatial_data |> select(.cell, sample_id, in_tissue, spatialLIBD)
247+
248+
spatial_data |>
249+
filter(sum_umi < 1000) |>
250+
count(sample_id)
251+
173252
```
174253

175254
#### Mutate
176255

177256
We can use `mutate` to create a column. For example, we could create a new `Phase_l` column that contains a lower-case version of `Phase`.
178257

179-
In this case, three columns that are view only (`sample_id`, `pxl_col_in_fullres`, `pxl_row_in_fullres`, `PC*`) will be always included in the tidy representation because they cannot be omitted from the data container (is opposed to metadata)
258+
::: {.note}
259+
Note that the special columns `sample_id`, `pxl_col_in_fullres`, `pxl_row_in_fullres`, `PC*` are view only and cannot be mutated.
260+
:::
180261

181262
```{r message=FALSE}
182263
spatial_data |>
183264
mutate(spatialLIBD_lower = tolower(spatialLIBD)) |>
184265
select(.cell, spatialLIBD, spatialLIBD_lower)
185266
```
186267

268+
We can update the underlying `SpatialExperiment` object, for future analyses. And confirm that the `SpatialExperiment` metadata has been mutated.
269+
270+
```{r message=FALSE}
271+
spatial_data =
272+
spatial_data |>
273+
mutate(spatialLIBD_lower = tolower(spatialLIBD))
274+
275+
spatial_data |>
276+
colData() |>
277+
_[,c("spatialLIBD", "spatialLIBD_lower")]
278+
```
279+
280+
We can mutate columns for on-the-fly analyses and exploration. Let's suppose one column has capitalisation inconsistencies, and we want to apply a unique filter.
281+
282+
```{r message=FALSE}
283+
spatial_data |>
284+
mutate(spatialLIBD = tolower(spatialLIBD)) |>
285+
filter(spatialLIBD == "wm")
286+
```
287+
288+
#### Extract
289+
187290
We can use tidyverse commands to polish an annotation column. We will extract the sample, and group information from the file name column into separate columns.
188291

189292
```{r message=FALSE}
@@ -196,8 +299,6 @@ spatial_data = spatial_data |> mutate(file_path = glue("../data/single_cell/{sa
196299
spatial_data |> select(.cell, file_path)
197300
```
198301

199-
#### Extract
200-
201302
Extract specific identifiers from complex data paths, simplifying the dataset by isolating crucial metadata. This process allows for clearer identification of samples based on their file paths, improving data organization.
202303

203304
```{r}
@@ -222,7 +323,6 @@ spatial_data <- spatial_data |> unite("sample_subject", sample_id, subject, remo
222323
spatial_data |> select(.cell, sample_id, sample_subject, subject)
223324
```
224325

225-
226326
### 3. Advanced filtering/gating and pseudobulk
227327

228328
`tidySpatialExperiment` provide a interactive advanced tool for gating region of interest for streamlined exploratory analyses.
@@ -233,67 +333,172 @@ Let's draw an arbitrary gate interactively
233333

234334
```{r, eval=FALSE}
235335
236-
spatial_data =
336+
spatial_data_gated =
237337
spatial_data |>
238338
239339
# Filter one sample
240340
filter(in_tissue, sample_id=="151673") |>
241341
242342
# Gate based on tissue morphology
243-
tidySpatialExperiment::gate_spatial(opacity = 0.2, image_index = 1)
343+
tidySpatialExperiment::gate_spatial(alpha = 0.1)
344+
```
345+
346+
347+
`tidySpatialExperiment` added a `.gate` column to the `SpatialExperiment` object. We can see this column in its tibble abstraction.
348+
349+
```{r, eval=FALSE}
350+
spatial_data_gated |> select(.cell, .gate)
244351
```
245352

246353
This is recorded in the `.gate` column
247354

248355
```{r, eval=FALSE}
249356
250-
spatial_data |> select(.cell, .gate)
357+
spatial_data_gated |> select(.cell, .gate)
251358
```
252359

253360
We can count how many pixels we selected with simple `tidyverse` grammar
254361

255362
```{r, eval=FALSE}
256-
spatial_data |> count(.gate)
363+
spatial_data_gated |> count(.gate)
257364
```
258365

259-
We can visualise the gating
260-
366+
To have a visual feedback of our selection we can plot the slide annotating by our newly created column.
261367

262368
```{r, eval=FALSE, fig.width=7, fig.height=8}
263-
spatial_data |>
264-
265-
# Plot our gate
266-
ggspavis::plotSpots(annotate = ".gate") +
267-
scale_color_manual(values = libd_layer_colors |> str_remove("ayer")) +
268-
labs(title = ".gate regions")
269-
369+
spatial_data_gated |>
370+
ggspavis::plotVisium(annotate = ".gate")
270371
```
271372

373+
272374
```{r, echo=FALSE, out.width="300px"}
273375
knitr::include_graphics(here("inst/images/tidySPE_gate.png"))
274376
```
275377

276-
And filter, for further analyses
378+
We can also filter, for further analyses
277379

278380
```{r, eval=FALSE}
279-
spatial_data |>
381+
spatial_data_gated |>
280382
filter(.gate)
281383
```
282384

283-
#### Summarisation/aggregation
385+
::: {.note}
386+
**Exercise 2.1**
387+
Gate roughly the white matter layer of the tissue (bottom-left) and visualise in UMAP reduced dimensions where this manual gate is distributed.
388+
389+
- Calculate UMAPs as we did for Sesison 1
390+
- Plot UMAP dimensions according to the gating
391+
:::
392+
393+
### 4. Work with features
394+
395+
By default `tidySpatialExperiment` (as well as `tidySingleCellExperiment`) focus their tidy abstraction on pixels and cells, as this is the key analysis and visualisation unit in sopatial and single-cell data. This has proven to be a practican solution to achieve elegant `tidy` analyses and visualisation.
396+
397+
In contrast, bulk data focuses to features/genes for analysis. In this case its tidy representation with `tidySummarizedExperiment` prioritise features, exposing them to the user.
398+
399+
If you want to interact with features, the method `join_features` will be helpful. For example, we can add one or more features of interest to our abstraction.
400+
401+
Let's add the astrocyte marker GFAP
402+
403+
Find out ENSEMBL ID
404+
405+
```{r}
406+
rowData(spatial_data) |>
407+
as_tibble() |>
408+
filter( gene_name == "GFAP")
409+
```
410+
411+
Join the feature to the metadata
412+
413+
```{r}
414+
spatial_data =
415+
spatial_data |>
416+
join_features("ENSG00000131095", shape="wide")
417+
418+
spatial_data |>
419+
select(.cell, ENSG00000131095)
420+
421+
```
422+
423+
424+
::: {.note}
425+
**Exercise 2.2**
426+
Join the endothelial marker PECAM1 (CD31, look for ENSEMBL ID), and plot in space the pixel that are in the 0.75 percentile of EPCAM1 expression. Are the PECAM1-positive pixels (endothelial?) spatially clustered?
427+
428+
- Get the ENSEMBL ID
429+
- Join the feature to the tidy data abstraction
430+
- Calculate the 0.75 quantile across all pixels
431+
- Label the cells with high PECAM1
432+
- Plot the slide colouring for the new label
433+
:::
434+
435+
436+
### 5. Summarisation/aggregation
437+
438+
#### Distinct
439+
440+
We can quickly explore the elements of a variable with distinct
441+
442+
```{r}
443+
spatial_data |>
444+
distinct(sample_id)
445+
```
446+
We can `distinct` across multiple variables
447+
448+
```{r}
449+
spatial_data |>
450+
distinct(sample_id, Cluster)
451+
```
284452

285-
The gated cells can then be divided into pseudobulks within a SummarizedExperiment object using tidySpatialExperiment’s aggregate_cells utility function.
453+
#### Count
286454

287-
```{r , eval=FALSE}
455+
We can gather more information counting the instances of a variable
456+
457+
```{r}
458+
spatial_data |>
459+
count(Cluster) |>
460+
arrange(desc(n))
461+
```
462+
463+
We calculate summary statistics of a subset of data
464+
465+
```{r}
466+
spatial_data |>
467+
filter(Cluster==1) |>
468+
count(sample_id) |>
469+
arrange(desc(n))
470+
471+
```
472+
473+
#### Aggregate
474+
475+
For summarised analyses, we can aggregate pixels/cells as pseudobulk with the function `aggregate_cells`. This also works for `SingleCellExeriment`.We obtain a `SummarizedExperiment`.
476+
477+
```{r}
288478
spe_regions_aggregated <-
289-
spe_regions |>
290-
aggregate_cells(c(sample_id, region))
479+
spatial_data |>
480+
aggregate_cells(c(sample_id, spatialLIBD))
291481
292482
spe_regions_aggregated
293483
```
294484

485+
`tidyomics` allows to cross spatial, single-cell (Bioconductor and seurat), and bulk keeping a consistent interface.
486+
487+
```{r}
488+
library(tidySummarizedExperiment)
489+
490+
spe_regions_aggregated
491+
492+
```
295493

296-
### 4. tidyfying your workflow
494+
You will be able to apply the familiar `tidyverse` operations
495+
496+
```{r}
497+
spe_regions_aggregated |>
498+
filter(sample_id == "151673")
499+
```
500+
501+
### 6. tidyfying your workflow
297502

298503
We will take workflow used in **Session 2**, performed using mostly base R syntax and convert it to tidy R syntax. We will show you how the readability and modularity of your workflow will improve.
299504

@@ -321,11 +526,11 @@ The `tidyverse` approach inherently supports chaining further operations without
321526

322527
#### Manipulating feature information
323528

324-
:::: {.note}
529+
::: {.note}
325530
For `SingleCellExperiment` there is no tidy API for manipulating feature wise data yet, on the contrary for `SummarizedExperiment`, because gene-centric the abstraction allow for direct gene information manipulation. Currently, `tidySingleCellExperiment` and `tidySpatialExperiment` do not prioritize the manipulation of features (genes).
326531

327532
While these functions can employ genes for cell manipulation and visualisation, as demonstrated in `join_features()`, they lack tools for altering feature-related information. Instead, their primary focus is on cell information, which serves as the main observational unit in single-cell data. This contrasts with bulk RNA sequencing data, where features are more central.
328-
::::
533+
:::
329534

330535
The tidy API for `SingleCellExperiment` has feature-manipulation API among our plans. See [tidyomics challenges](https://github.com/orgs/tidyomics/projects/1)
331536

@@ -495,7 +700,7 @@ spatial_data_filtered =
495700
**Maintainability:** Fewer and self-explanatory lines of code and no need for intermediate steps make the code easier to maintain and modify, especially when conditions change or additional filters are needed.
496701

497702

498-
### 5. Visualisation
703+
### 7. Visualisation
499704

500705
Here, we will show how to use ad-hoc spatial visualisation, as well as `ggplot` to explore spatial data we will show how `tidySpatialExperiment` allowed to alternate between tidyverse visualisation, and any visualisation compatible with `SpatialExperiment`.
501706

@@ -506,15 +711,14 @@ Let’s visualise the regions that spatialLIBD labelled across three Visium 10X
506711
```{r, fig.width=7, fig.height=8}
507712
spatial_data_filtered |>
508713
ggspavis::plotSpots(annotate = "spatialLIBD") +
714+
facet_wrap(~sample_id) +
509715
scale_color_manual(values = libd_layer_colors |> str_remove("ayer")) +
510716
theme(legend.position = "none") +
511717
labs(title = "spatialLIBD regions")
512718
```
513719

514720
#### Custom visualisation: Plotting the regions
515721

516-
517-
518722
```{r, fig.width=7, fig.height=8}
519723
spatial_data |>
520724
ggplot(aes(array_row, array_col)) +
@@ -589,16 +793,43 @@ spatial_data_filtered |>
589793

590794
As you can appreciate, the relationship between the number of genes, probed Purcell and their mitochondrial prescription abundance it's quite consistent.
591795

592-
:::: {.note}
593-
**Excercise 2.1**
796+
::: {.note}
797+
**Excercise 2.3**
594798

595799
To to practice the use of `tidyomics` on spatial data, we propose a few exercises that connect manipulation, calculations and visualisation. These exercises are just meant to be simple use cases that exploit tidy R streamlined language.
596800

597801

598802
We assume that the cells we filtered as non-alive or damaged, characterised by being reached uniquely for mitochondrial, genes, and genes, linked to up ptosis. it is good practice to check these assumption. This exercise aims to estimate what genes are differentially expressed between filtered and unfiltered cells. Then visualise the results
599803

600804
Use `tidyomic`s/`tidyverse` tools to label dead cells and perform differential expression within each region. Some of the comments you can use are: `mutate`, `nest`, `aggregate_cells`.
601-
::::
805+
:::
806+
807+
::: {.note}
808+
**Excercise 2.4**
809+
810+
Inspired by our audience, let's try to use `tidyomics` to identify potential Amyloid Plaques.
811+
812+
Amyloid plaques are extracellular deposits primarily composed of aggregated amyloid-beta (Aβ) peptides. They are a hallmark of Alzheimer's disease (AD) and are also found in certain other neurodegenerative conditions.
813+
814+
Amyloid plaques can be found in the brains of mice, particularly in transgenic mouse models that are engineered to develop Alzheimer's disease-like pathology.
815+
816+
Although amyloid plaques themselves are extracellular, the presence and formation of these plaques are associated with specific gene expression changes in the surrounding and involved cells. These gene markers are indicative of the processes that contribute to amyloid plaque formation, as well as the cellular response to these plaques ([Ranman et al., 2021](https://molecularneurodegeneration.biomedcentral.com/articles/10.1186/s13024-021-00465-0).)
817+
818+
```{r}
819+
marker_genes_of_amyloid_plaques = c("APP", "PSEN1", "PSEN2", "CLU", "APOE", "CD68", "ITGAM", "AIF1")
820+
821+
rownames(spatial_data) = rowData(spatial_data)$gene_name
822+
823+
```
824+
825+
The excercise includes
826+
- Join the features
827+
- Rescaling
828+
- Summarising signature (sum), `mutate()`
829+
- Plotting colousing by the signature
830+
831+
# Plotting
832+
:::
602833

603834

604835
**Session Information**

‎vignettes/Solutions.Rmd

+137-37
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ knitr::opts_chunk$set(echo = TRUE, cache = FALSE)
1616
```
1717

1818

19-
::: note
19+
::: {.note}
2020
**Exercise 1.1**
2121
:::
2222

@@ -35,7 +35,7 @@ ggspavis::plotVisium(
3535
```
3636

3737

38-
::: note
38+
::: {.note}
3939
**Exercise 1.2**
4040
:::
4141

@@ -63,9 +63,123 @@ plotSpotQC(
6363
facet_wrap(~sample_id)
6464
```
6565

66-
:::: {.note}
67-
**Excercise 2.1**
68-
::::
66+
67+
::: {.note}
68+
**Excercise 1.3**
69+
:::
70+
71+
72+
```{r, fig.width=7, fig.height=8}
73+
74+
res_spatialLIBD = split(data.frame(res$mat), colData(spatial_data_gene_name)$sample_id )
75+
76+
lapply(res_spatialLIBD, function(x) plotCorrelationMatrix(as.matrix(x[,-10])))
77+
78+
```
79+
80+
::: {.note}
81+
**Excercise 1.4**
82+
:::
83+
84+
85+
```{r, fig.width=7, fig.height=8}
86+
87+
res_spatialLIBD = split(data.frame(res$mat), colData(spatial_data_gene_name)$spatialLIBD )
88+
89+
lapply(res_spatialLIBD, function(x) plotCorrelationMatrix(as.matrix(x[,-10])))
90+
```
91+
92+
::: {.note}
93+
**Excercise 1.5**
94+
:::
95+
96+
97+
```{r, fig.width=7, fig.height=8}
98+
99+
100+
101+
is_endothelial_leptomeningeal = mat_df$endothelial_cell >0.1 & mat_df$leptomeningeal_cell>0.1 & mat_df$endothelial_cell + mat_df$leptomeningeal_cell > 0.4
102+
103+
spatial_data$is_endothelial_leptomeningeal = is_endothelial_leptomeningeal
104+
105+
ggspavis::plotSpots(spatial_data, annotate = "is_endothelial_leptomeningeal") +
106+
facet_wrap(~sample_id) +
107+
scale_color_manual(values = c("TRUE"= "red", "FALSE" = "grey"))
108+
theme(legend.position = "none") +
109+
labs(title = "endothelial + leptomeningeal")
110+
111+
112+
113+
114+
is_endothelial_oligodendrocytes = mat_df$endothelial_cell >0.1 & mat_df$oligodendrocyte>0.05 & mat_df$endothelial_cell + mat_df$oligodendrocyte > 0.4
115+
116+
spatial_data$is_endothelial_oligodendrocyte = is_endothelial_oligodendrocytes
117+
118+
ggspavis::plotSpots(spatial_data, annotate = "is_endothelial_oligodendrocyte") +
119+
facet_wrap(~sample_id) +
120+
scale_color_manual(values = c("TRUE"= "blue", "FALSE" = "grey"))
121+
theme(legend.position = "none") +
122+
labs(title = "endothelial + oligodendrocyte")
123+
124+
```
125+
126+
127+
128+
::: {.note}
129+
**Exercise 2.1**
130+
:::
131+
132+
```{r}
133+
# Get top variable genes
134+
genes <- !grepl(pattern = "^Rp[l|s]|Mt", x = rownames(spatial_data))
135+
hvg = scran::modelGeneVar(spatial_data, subset.row = genes, block = spatial_data$sample_id)
136+
hvg = scran::getTopHVGs(dec, n = 1000)
137+
138+
# Calculate PCA
139+
spatial_data <-
140+
spatial_data |>
141+
scuttle::logNormCounts() |>
142+
scater::runPCA(subset_row = hvg) |>
143+
144+
# Calculate UMAP
145+
scater::runUMAP(dimred = "PCA") |>
146+
147+
# Plot
148+
scater::plotUMAP(colour_by = ".gate")
149+
```
150+
151+
152+
::: {.note}
153+
**Exercise 2.2**
154+
:::
155+
156+
157+
```{r}
158+
rowData(spatial_data) |>
159+
as_tibble() |>
160+
filter( gene_name == "PECAM1")
161+
162+
spatial_data |>
163+
164+
# Join the feature
165+
join_features("ENSG00000261371", shape="wide") |>
166+
167+
# Calculate the quantile
168+
mutate(my_quantile = quantile(ENSG00000261371, 0.75)) |>
169+
170+
# Label the pixels
171+
mutate(PECAM1_positive = ENSG00000261371 > my_quantile) |>
172+
173+
# Plot
174+
ggspavis::plotSpots(annotate = "PECAM1_positive") +
175+
facet_wrap(~sample_id)
176+
177+
```
178+
179+
180+
::: {.note}
181+
**Excercise 2.3**
182+
:::
69183

70184
```{r}
71185
library(tidySummarizedExperiment)
@@ -102,45 +216,31 @@ differential_analysis |>
102216
```
103217

104218

219+
::: {.note}
220+
**Excercise 2.4**
221+
:::
105222

223+
```{r}
224+
rownames(spatial_data) = rowData(spatial_data)$gene_name
106225
107-
```{r, fig.width=7, fig.height=8}
108-
109-
res_spatialLIBD = split(data.frame(res$mat), colData(spatial_data_gene_name)$sample_id )
110-
111-
lapply(res_spatialLIBD, function(x) plotCorrelationMatrix(as.matrix(x[,-10])))
112-
113-
```
114-
115-
116-
```{r, fig.width=7, fig.height=8}
117-
118-
res_spatialLIBD = split(data.frame(res$mat), colData(spatial_data_gene_name)$spatialLIBD )
119-
120-
lapply(res_spatialLIBD, function(x) plotCorrelationMatrix(as.matrix(x[,-10])))
121-
```
122-
226+
marker_genes_of_amyloid_plaques = c("APP", "PSEN1", "PSEN2", "CLU", "APOE", "CD68", "ITGAM", "AIF1")
123227
124-
```{r, fig.width=7, fig.height=8}
228+
spatial_data |>
125229
126-
mat_df = as.data.frame(res$mat)
230+
# Join the features
231+
join_features(marker_genes_of_amyloid_plaques, shape = "wide") |>
127232
128-
is_endothelial_leptomeningeal = mat_df$endothelial_cell >0.1 & mat_df$leptomeningeal_cell>0.1 & mat_df$endothelial_cell + mat_df$leptomeningeal_cell > 0.4
129-
is_endothelial_oligodendrocytes = mat_df$endothelial_cell >0.1 & mat_df$oligodendrocyte>0.05 & mat_df$endothelial_cell + mat_df$oligodendrocyte > 0.4
233+
# Rescaling
234+
mutate(across(any_of(marker_genes_of_amyloid_plaques), scales::rescale)) |>
130235
131-
spatial_data$is_endothelial_leptomeningeal = is_endothelial_leptomeningeal
132-
spatial_data$is_endothelial_oligodendrocyte = is_endothelial_oligodendrocytes
236+
# Summarising signature
237+
mutate(amyloid_plaques_signature = APP + PSEN1 + PSEN2 + CLU + APOE + CD68 + ITGAM + AIF1) |>
133238
134-
ggspavis::plotSpots(spatial_data, annotate = "is_endothelial_leptomeningeal") +
135-
facet_wrap(~sample_id) +
136-
scale_color_manual(values = c("TRUE"= "red", "FALSE" = "grey"))
137-
theme(legend.position = "none") +
138-
labs(title = "endothelial + leptomeningeal")
239+
# Plotting
240+
ggspavis::plotSpots(
241+
annotate = "amyloid_plaques_signature"
242+
) +
243+
facet_wrap(~sample_id)
139244
140-
ggspavis::plotSpots(spatial_data, annotate = "is_endothelial_oligodendrocyte") +
141-
facet_wrap(~sample_id) +
142-
scale_color_manual(values = c("TRUE"= "blue", "FALSE" = "grey"))
143-
theme(legend.position = "none") +
144-
labs(title = "endothelial + oligodendrocyte")
145245
146246
```

0 commit comments

Comments
 (0)
Please sign in to comment.