To ease up the process of generating integrations using multiple
methods available through Seurat, we will define a small function :
## object A SeuratObject v5 after Seurat::IntegrateLayers
## assay The name of the `object` assay to use (default is "RNA")
## markers A vector of feature names to use as markers in the covariates weight estimation
## int_reduction The name of a dimension reduction generated by integration (See https://satijalab.org/seurat/reference/#integration)
## n_dim The number of PCA dimensions to use for the integration
## resolution The resolution of the Louvain clustering
## my_seed The seed given to the RNG
integration_describe <- function(object = NULL, assay = "RNA", markers = NULL, int_reduction = "CCAIntegration", n_dim = 20, resolution = .8, my_seed = 1337) {
## Elbow plot
object@reductions[[int_method]]@stdev <- matrixStats::colSds(Seurat::Embeddings(object = Seurat::Reductions(object = object, slot = int_method)))
ep <- Seurat::ElbowPlot(
object = object,
ndims = min(30,
ncol(Seurat::Reductions(object = object,
slot = int_reduction))))
print(ep)
## Assess covariates
### Prepare the covariates data.frame
covar_df <- if(is.null(markers)) object[[]] else cbind(object[[]], t(Seurat::GetAssayData(object = subset(x = SeuratObject::JoinLayers(object = object), features = paiva_markers), assay = "RNA", slot = "scale.data")))
### Run the helper function
EBAII.n1.SC.helper::assess_covar(
mat = t(Seurat::Embeddings(Seurat::Reductions(object = object,
slot = int_method)
)
),
covar.df = covar_df,
ndim = 10L,
center = TRUE,
scale = TRUE)
## Create UMAP
object <- Seurat::RunUMAP(
object = object,
assay = assay,
reduction = int_method,
dims = 1:n_dim,
seed.use = my_seed,
verbose = FALSE
)
## Clustering
clust_name <- paste0(int_method, "_clusters")
### Compute neighbours
object <- Seurat::FindNeighbors(object = object,
reduction = int_method,
dims = 1:n_dim,
verbose = FALSE)
### Perform Louvain clustering
object <- Seurat::FindClusters(object = object,
resolution = resolution,
cluster.name = clust_name,
random.seed = my_seed,
verbose = FALSE)
## Plot UMAP : idents
dp_ident <- Seurat::DimPlot(object = object,
dims = c(1,2),
seed = my_seed,
group.by = "orig.ident") &
Seurat::DarkTheme() &
ggplot2::ggtitle(label = int_method)
print(dp_ident)
## Plot UMAP : clusters
dp_clusters <- Seurat::DimPlot(object = object,
dims = c(1,2),
seed = my_seed,
group.by = clust_name,
label = TRUE,
repel = TRUE) &
ggplot2::ggtitle(label = int_method)
print(dp_clusters)
## Use markers for heatmap
Seurat::Idents(object = object) <- object[[clust_name]][[1]]
fam <- Seurat::FindAllMarkers(
object = SeuratObject::JoinLayers(object = object),
logfc.threshold = .5,
only.pos = TRUE,
min.pct = .5,
verbose = FALSE,
random.seed = my_seed)
## Select top10 genes when available
fam_rdx <- dplyr::group_by(.data = fam, cluster)
fam_rdx <- dplyr::filter(.data = fam_rdx, avg_log2FC > 1)
fam_rdx <- dplyr::slice_head(.data = fam_rdx, n = 10)
dh <- Seurat::DoHeatmap(object = object,
features = fam_rdx$gene,
combine = TRUE) &
ggplot2::ggtitle(label = int_method)
print(dh)
## Proportions
prop_tab <- as.data.frame.matrix(table(object[[clust_name]][[1]], object$orig.ident))
prop_tab[[paste0(colnames(prop_tab)[1], "_prop")]] <- prop_tab[,1] / sum(prop_tab[,1])
prop_tab[[paste0(colnames(prop_tab)[2], "_prop")]] <- prop_tab[,2] / sum(prop_tab[,2])
### Nice table
prop_dt <- DT::formatPercentage(table = DT::datatable(prop_tab, filter = "none", options = list(pageLength = 100)), columns = c(3,4), digits = 2)
print(prop_dt)
### Mosaic plot
vcd::mosaic(x = as.matrix(prop_tab[,c(1,2)]),
shade = TRUE,
main = int_method)
## Return
return(object)
}