Skip to content

Commit

Permalink
update solutions and vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
stemangiola committed May 21, 2024
1 parent ba73300 commit 131e30b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
39 changes: 19 additions & 20 deletions vignettes/Session_2_Tidy_spatial_analyses.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,6 @@ spatial_data_gated =
spatial_data_gated |> select(.cell, .gate)
```

This is recorded in the `.gate` column

```{r, eval=FALSE}
spatial_data_gated |> select(.cell, .gate)
```

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

```{r, eval=FALSE}
Expand Down Expand Up @@ -386,13 +379,14 @@ spatial_data_gated |>
**Exercise 2.1**
Gate roughly the white matter layer of the tissue (bottom-left) and visualise in UMAP reduced dimensions where this manual gate is distributed.

- Calculate UMAPs as we did for Sesison 1
- Calculate PCA, UMAPs as we did for Session 1
- Gate the area of white matter
- Plot UMAP dimensions according to the gating
:::

### 4. Work with features

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.
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 spatial and single-cell data. This has proven to be a practical solution to achieve elegant `tidy` analyses and visualisation.

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.

Expand Down Expand Up @@ -427,7 +421,7 @@ Join the endothelial marker PECAM1 (CD31, look for ENSEMBL ID), and plot in spac

- Get the ENSEMBL ID
- Join the feature to the tidy data abstraction
- Calculate the 0.75 quantile across all pixels
- Calculate the 0.75 quantile across all pixels `mutate()`
- Label the cells with high PECAM1
- Plot the slide colouring for the new label
:::
Expand Down Expand Up @@ -498,6 +492,8 @@ spe_regions_aggregated |>
filter(sample_id == "151673")
```



### 6. tidyfying your workflow

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.
Expand Down Expand Up @@ -571,7 +567,7 @@ spatial_data <-
qc_mitochondrial_transcription = subsets_mito_percent > 30
)
spatial_data
spatial_data |> select(.cell, qc_mitochondrial_transcription)
```

Expand Down Expand Up @@ -613,9 +609,12 @@ genes <- !grepl(pattern = "^Rp[l|s]|Mt", x = rownames(spatial_data))
marker_genes =
spatial_data |>
# Grouping
group_split(sample_id) |>
map(~
.x |>
# Loop across the list elements
map(~ .x |>
scran::modelGeneVar(subset.row = genes) |>
scran::getTopHVGs(n = 1000)
) |>
Expand Down Expand Up @@ -676,6 +675,7 @@ spatial_data = spatial_data[,!colData(spatial_data)$discard ]
spatial_data_filtered =
spatial_data |>
mutate(
discard =
subsets_mito_percent > 30 |
Expand Down Expand Up @@ -712,7 +712,7 @@ Let’s visualise the regions that spatialLIBD labelled across three Visium 10X
spatial_data_filtered |>
ggspavis::plotSpots(annotate = "spatialLIBD") +
facet_wrap(~sample_id) +
scale_color_manual(values = libd_layer_colors |> str_remove("ayer")) +
scale_color_manual(values = libd_layer_colors |> str_remove("ayer")) +
theme(legend.position = "none") +
labs(title = "spatialLIBD regions")
```
Expand All @@ -721,11 +721,11 @@ spatial_data_filtered |>

```{r, fig.width=7, fig.height=8}
spatial_data |>
ggplot(aes(array_row, array_col)) +
geom_point(aes(color = spatialLIBD)) +
facet_wrap(~sample_id) +
theme(legend.position = "none") +
labs(title = "spatialLIBD regions")
ggplot(aes(array_row, array_col)) +
geom_point(aes(color = spatialLIBD)) +
facet_wrap(~sample_id) +
theme(legend.position = "none") +
labs(title = "spatialLIBD regions")
```

#### Custom visualisation: Plotting RNA output
Expand Down Expand Up @@ -798,7 +798,6 @@ As you can appreciate, the relationship between the number of genes, probed Purc

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.


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

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`.
Expand Down
20 changes: 15 additions & 5 deletions vignettes/Solutions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,26 @@ theme(legend.position = "none") +
```{r}
# Get top variable genes
genes <- !grepl(pattern = "^Rp[l|s]|Mt", x = rownames(spatial_data))
hvg = scran::modelGeneVar(spatial_data, subset.row = genes, block = spatial_data$sample_id)
hvg = scran::getTopHVGs(dec, n = 1000)
hvg =
spatial_data |>
scran::modelGeneVar(subset.row = genes, block = spatial_data$sample_id) |>
scran::getTopHVGs(n = 1000)
# Calculate PCA
spatial_data <-
spatial_data |>
spatial_data |>
scuttle::logNormCounts() |>
scater::runPCA(subset_row = hvg) |>
# Calculate UMAP
scater::runUMAP(dimred = "PCA") |>
# Filter one sample
filter(in_tissue, sample_id=="151673") |>
# Gate based on tissue morphology
tidySpatialExperiment::gate_spatial(alpha = 0.1) |>
# Plot
scater::plotUMAP(colour_by = ".gate")
```
Expand All @@ -159,10 +167,12 @@ rowData(spatial_data) |>
as_tibble() |>
filter( gene_name == "PECAM1")
gene = "ENSG00000261371"
spatial_data |>
# Join the feature
join_features("ENSG00000261371", shape="wide") |>
join_features(gene, shape="wide") |>
# Calculate the quantile
mutate(my_quantile = quantile(ENSG00000261371, 0.75)) |>
Expand Down

0 comments on commit 131e30b

Please sign in to comment.