Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 150 plotCoregulationSpatial #160

Merged
merged 5 commits into from
Oct 8, 2024

Conversation

vdsukhov
Copy link
Member

@vdsukhov vdsukhov commented Aug 31, 2024

Adjusted the plotCoregulationSpatial function to support multiple samples, as outlined in issue #150.

I checked the refactored function with two example (code snippet is attached in the end).

I also added ellipsis (...) to allow passing extra arguments to Seurat::SpatialFeaturePlot. This should address some display issues with spatial data that users have encountered after upgrading to Seurat 5, as noted in this issue, by enabling manual adjustment of pt.size.factor.

library(fgsea)
library(Seurat)
library(SeuratData)
library(ggplot2)
library(patchwork)
library(dplyr)
library(msigdbr)

# ----------------------------------------------------------------------------------------------------------------
# Example From Seurat vignette
# ----------------------------------------------------------------------------------------------------------------


InstallData("stxBrain")

brain <- LoadData("stxBrain", type = "anterior1")



brain <- SCTransform(brain, assay = "Spatial", verbose = FALSE)

brain2 <- LoadData("stxBrain", type = "posterior1")
brain2 <- SCTransform(brain2, assay = "Spatial", verbose = FALSE)

brain.merge <- merge(brain, brain2)

DefaultAssay(brain.merge) <- "SCT"
VariableFeatures(brain.merge) <- c(VariableFeatures(brain), VariableFeatures(brain2))
brain.merge <- RunPCA(brain.merge, verbose = FALSE)
brain.merge <- FindNeighbors(brain.merge, dims = 1:30)
brain.merge <- FindClusters(brain.merge, verbose = FALSE)
brain.merge <- RunUMAP(brain.merge, dims = 1:30)


brain.merge <- RunPCA(brain.merge, assay = "SCT", verbose = FALSE,
                      rev.pca = TRUE, reduction.name = "pca.rev",
                      reduction.key="PCR_", npcs = 50)

E <- brain.merge@reductions$pca.rev@feature.loadings


pathwaysDF <- msigdbr("mouse", category="H")
pathways <- split(pathwaysDF$gene_symbol, pathwaysDF$gs_name)

set.seed(1)
gesecaRes <- geseca(pathways, E, minSize = 15, maxSize = 500, center = FALSE)


topPathways <- gesecaRes[, pathway] |> head(4)
titles <- sub("HALLMARK_", "", topPathways)



ps <- plotCoregulationProfileSpatial(pathways[topPathways], 
                                     brain.merge, 
                                     title=titles,
                                     pt.size.factor=2.5,
                                     stroke=0.1)

ps <- lapply(ps, function(p) p + theme_minimal(base_size = 6))
ps <- cowplot::plot_grid(plotlist = ps, ncol=2)
ps


# ----------------------------------------------------------------------------------------------------------------
# Example From GESECA vignette
# ----------------------------------------------------------------------------------------------------------------
obj <- readRDS(url("https://alserglab.wustl.edu/files/fgsea/275_T_seurat.rds"))
obj <- UpdateSeuratObject(obj)


obj <- SCTransform(obj, assay = "Spatial", verbose = FALSE, variable.features.n = 10000)

obj <- RunPCA(obj, assay = "SCT", verbose = FALSE,
              rev.pca = TRUE, reduction.name = "pca.rev",
              reduction.key="PCR_", npcs = 50)

E <- obj@reductions$pca.rev@feature.loadings

pathwaysDF <- msigdbr("human", category="H")
pathways <- split(pathwaysDF$gene_symbol, pathwaysDF$gs_name)

set.seed(1)
gesecaRes <- geseca(pathways, E, minSize = 15, maxSize = 500, center = FALSE)

topPathways <- gesecaRes[, pathway] |> head(4)
titles <- sub("HALLMARK_", "", topPathways)

sfactors <- ScaleFactors(obj@images$slice1)
psf <- 1.5 * sfactors$fiducial / sfactors$hires

ps <- plotCoregulationProfileSpatial(pathways[topPathways], obj,
                                     title=titles, 
                                     pt.size.factor=psf,
                                     stroke=0.2)
cowplot::plot_grid(plotlist=ps, ncol=2)

@assaron assaron merged commit e639750 into ctlab:master Oct 8, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants