Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions inst/book/more-reddim.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -470,50 +470,50 @@ ggplot() +
One downside of t$-$SNE and UMAP is that they preserve the neighbourhood structure of the data while neglecting the local density of the data.
This can result in seemingly compact clusters on a t-SNE or UMAP plot that correspond to very heterogeneous groups in the original data.
The dens-SNE and densMAP algorithms mitigate this effect by incorporating information about the average distance to the nearest neighbours when creating the embedding [@narayan2021densvis].
We demonstrate below by applying these approaches on the PCs of the Zeisel dataset using the `r Biocpkg("densvis")` wrapper package.
We demonstrate this by applying t-SNE on the PCs of the Zeisel dataset using the `r Biocpkg("densvis")` wrapper package.

```{r}
set.seed(123)
library(densvis)
dt <- densne(reducedDim(sce.zeisel, "PCA"), dens_frac = 0.4, dens_lambda = 0.2)
dt <- densne(reducedDim(sce.zeisel, "PCA"), dens_frac = 0.4, dens_lambda = 0.4)
reducedDim(sce.zeisel, "dens-SNE") <- dt
dm <- densmap(reducedDim(sce.zeisel, "PCA"), dens_frac = 0.4, dens_lambda = 0.2)
reducedDim(sce.zeisel, "densMAP") <- dm
sce.zeisel <- runUMAP(sce.zeisel) # for comparison
sce.zeisel <- runTSNE(sce.zeisel) # for comparison

```


```{r, echo=FALSE}
set.seed(123)
ds <- reducedDim(sce.zeisel, "dens-SNE")
ts <- reducedDim(sce.zeisel, "TSNE")
du <- reducedDim(sce.zeisel, "densMAP")
tu <- reducedDim(sce.zeisel, "UMAP")
ds <- scale(ds)
ds <- scale(dt)
ts <- scale(ts)
du <- scale(du)
tu <- scale(tu)

vars_ds <- colVars(ds[sce.zeisel$level1class == "astrocytes_ependymal", ])
vars_ts <- colVars(ts[sce.zeisel$level1class == "astrocytes_ependymal", ])
vars_ds <- colVars(ds[sce.zeisel$level1class == "endothelial-mural", ])
vars_ts <- colVars(ts[sce.zeisel$level1class == "endothelial-mural", ])
stopifnot(mean(vars_ds) > mean(vars_ts))

vars_du <- colVars(du[sce.zeisel$level1class == "astrocytes_ependymal", ])
vars_tu <- colVars(tu[sce.zeisel$level1class == "astrocytes_ependymal", ])
stopifnot(mean(vars_du) > mean(vars_tu))
## disabled UMAP because of platform inconsistencies (namely mac ARM64)
# du <- densmap(reducedDim(sce.zeisel, "PCA"), dens_frac = 0.4, dens_lambda = 0.4)
# reducedDim(sce.zeisel, "densMAP") <- du
# sce.zeisel <- runUMAP(sce.zeisel) # for comparison
# tu <- reducedDim(sce.zeisel, "UMAP")
# du <- scale(dm)
# tu <- scale(tu)
# vars_du <- colVars(du[sce.zeisel$level1class == "endothelial-mural", ])
# vars_tu <- colVars(tu[sce.zeisel$level1class == "endothelial-mural", ])
# plotReducedDim(sce.zeisel, "UMAP", colour_by="level1class") + ggtitle("UMAP"),
# plotReducedDim(sce.zeisel, "densMAP", colour_by="level1class") + ggtitle("densMAP"),
# stopifnot(mean(vars_du) > mean(vars_tu))
```

These methods provide more information about transcriptional heterogeneity within clusters (Figure \@ref(fig:densne)),
with the astrocyte cluster being less compact in the density-preserving versions.
with the endothelial cluster being less compact in the density-preserving versions.
This excessive compactness can imply a lower level of within-population heterogeneity.

```{r densne, fig.width=10, fig.height=6, fig.cap="$t$-SNE, UMAP, dens-SNE and densMAP embeddings for the Zeisel brain data."}
```{r densne, fig.width=10, fig.height=6, fig.cap="$t$-SNE and dens-SNE embeddings for the Zeisel brain data."}

gridExtra::grid.arrange(
plotReducedDim(sce.zeisel, "TSNE", colour_by="level1class") + ggtitle("t-SNE"),
plotReducedDim(sce.zeisel, "dens-SNE", colour_by="level1class") + ggtitle("dens-SNE"),
plotReducedDim(sce.zeisel, "UMAP", colour_by="level1class") + ggtitle("UMAP"),
plotReducedDim(sce.zeisel, "densMAP", colour_by="level1class") + ggtitle("densMAP"),
ncol=2
)
```
Expand Down
Loading