From 7cc28edc0f1df625c41922058cc48a7714513e9b Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Wed, 5 Nov 2025 10:06:02 +0000 Subject: [PATCH 1/7] Address lingering seed and variable use issues From #24 --- inst/book/more-reddim.Rmd | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/inst/book/more-reddim.Rmd b/inst/book/more-reddim.Rmd index 811f5b7..d713722 100644 --- a/inst/book/more-reddim.Rmd +++ b/inst/book/more-reddim.Rmd @@ -473,6 +473,7 @@ The dens-SNE and densMAP algorithms mitigate this effect by incorporating inform We demonstrate below by applying these approaches 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) reducedDim(sce.zeisel, "dens-SNE") <- dt @@ -484,14 +485,11 @@ 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) +ds <- scale(dm) tu <- scale(tu) vars_ds <- colVars(ds[sce.zeisel$level1class == "astrocytes_ependymal", ]) From 794faaa2d988eaf512ea01749f80f31675256fa9 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Wed, 5 Nov 2025 10:06:43 +0000 Subject: [PATCH 2/7] Update more-reddim.Rmd --- inst/book/more-reddim.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/book/more-reddim.Rmd b/inst/book/more-reddim.Rmd index d713722..78e14fc 100644 --- a/inst/book/more-reddim.Rmd +++ b/inst/book/more-reddim.Rmd @@ -489,7 +489,7 @@ ts <- reducedDim(sce.zeisel, "TSNE") tu <- reducedDim(sce.zeisel, "UMAP") ds <- scale(dt) ts <- scale(ts) -ds <- scale(dm) +du <- scale(dm) tu <- scale(tu) vars_ds <- colVars(ds[sce.zeisel$level1class == "astrocytes_ependymal", ]) From 356c15d37bb2bbea47b2d02e55a9128adc92dd03 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Thu, 6 Nov 2025 07:33:00 +0000 Subject: [PATCH 3/7] Sum var not mean --- inst/book/more-reddim.Rmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/book/more-reddim.Rmd b/inst/book/more-reddim.Rmd index 78e14fc..7f31a39 100644 --- a/inst/book/more-reddim.Rmd +++ b/inst/book/more-reddim.Rmd @@ -494,11 +494,11 @@ tu <- scale(tu) vars_ds <- colVars(ds[sce.zeisel$level1class == "astrocytes_ependymal", ]) vars_ts <- colVars(ts[sce.zeisel$level1class == "astrocytes_ependymal", ]) -stopifnot(mean(vars_ds) > mean(vars_ts)) +stopifnot(sum(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)) +stopifnot(sum(vars_du) > mean(vars_tu)) ``` These methods provide more information about transcriptional heterogeneity within clusters (Figure \@ref(fig:densne)), From 9860b6b57e1f661a7d17e99e62f58c112fe130b1 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Thu, 6 Nov 2025 08:36:36 +0000 Subject: [PATCH 4/7] Dens lamba --- inst/book/more-reddim.Rmd | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/book/more-reddim.Rmd b/inst/book/more-reddim.Rmd index 7f31a39..469a82f 100644 --- a/inst/book/more-reddim.Rmd +++ b/inst/book/more-reddim.Rmd @@ -475,9 +475,9 @@ We demonstrate below by applying these approaches on the PCs of the Zeisel datas ```{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) +dm <- densmap(reducedDim(sce.zeisel, "PCA"), dens_frac = 0.4, dens_lambda = 0.4) reducedDim(sce.zeisel, "densMAP") <- dm sce.zeisel <- runUMAP(sce.zeisel) # for comparison sce.zeisel <- runTSNE(sce.zeisel) # for comparison @@ -494,11 +494,11 @@ tu <- scale(tu) vars_ds <- colVars(ds[sce.zeisel$level1class == "astrocytes_ependymal", ]) vars_ts <- colVars(ts[sce.zeisel$level1class == "astrocytes_ependymal", ]) -stopifnot(sum(vars_ds) > mean(vars_ts)) +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(sum(vars_du) > mean(vars_tu)) +stopifnot(mean(vars_du) > mean(vars_tu)) ``` These methods provide more information about transcriptional heterogeneity within clusters (Figure \@ref(fig:densne)), From f2539bf0b2cd68280133dc3a5d862f65a800d186 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Tue, 11 Nov 2025 09:02:39 +0000 Subject: [PATCH 5/7] Use endothelial rather than astrocytes --- inst/book/more-reddim.Rmd | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/inst/book/more-reddim.Rmd b/inst/book/more-reddim.Rmd index 469a82f..f811338 100644 --- a/inst/book/more-reddim.Rmd +++ b/inst/book/more-reddim.Rmd @@ -492,17 +492,17 @@ ts <- scale(ts) du <- scale(dm) 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", ]) +vars_du <- colVars(du[sce.zeisel$level1class == "endothelial-mural", ]) +vars_tu <- colVars(tu[sce.zeisel$level1class == "endothelial-mural", ]) 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."} From 1924f808b29db72e520ec5e680deb58d5bf76abf Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Tue, 11 Nov 2025 09:47:07 +0000 Subject: [PATCH 6/7] Only show tSNE and denSNE --- inst/book/more-reddim.Rmd | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/inst/book/more-reddim.Rmd b/inst/book/more-reddim.Rmd index f811338..59fc934 100644 --- a/inst/book/more-reddim.Rmd +++ b/inst/book/more-reddim.Rmd @@ -477,41 +477,43 @@ set.seed(123) library(densvis) 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.4) -reducedDim(sce.zeisel, "densMAP") <- dm -sce.zeisel <- runUMAP(sce.zeisel) # for comparison sce.zeisel <- runTSNE(sce.zeisel) # for comparison + ``` ```{r, echo=FALSE} ts <- reducedDim(sce.zeisel, "TSNE") -tu <- reducedDim(sce.zeisel, "UMAP") ds <- scale(dt) ts <- scale(ts) -du <- scale(dm) -tu <- scale(tu) 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 == "endothelial-mural", ]) -vars_tu <- colVars(tu[sce.zeisel$level1class == "endothelial-mural", ]) -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 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 ) ``` From 8556c5eb49a11a47907466116c8d6ccf800f3518 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Tue, 11 Nov 2025 10:01:44 +0000 Subject: [PATCH 7/7] Text only t-SNE --- inst/book/more-reddim.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/book/more-reddim.Rmd b/inst/book/more-reddim.Rmd index 59fc934..25c232a 100644 --- a/inst/book/more-reddim.Rmd +++ b/inst/book/more-reddim.Rmd @@ -470,7 +470,7 @@ 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)