fgsea icon indicating copy to clipboard operation
fgsea copied to clipboard

Issue 150 plotCoregulationSpatial

Open vdsukhov opened this issue 5 months ago • 0 comments

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@[email protected]


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@[email protected]

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)

vdsukhov avatar Aug 31 '24 03:08 vdsukhov