#' @title Convert Seurat v5 Object to Seurat v4 Object
#' @description This function converts a Seurat v5 object to a Seurat v4 object. It checks the required package versions and installs Seurat v4 if necessary, to ensure compatibility.
#' @author Zaoqu Liu; Email: liuzaoqu@163.com
#' @param srt A Seurat v5 object. This object should be the output of a Seurat v5 analysis.
#' @return A Seurat v4 object, compatible with earlier versions of Seurat.
#' @details The function performs the following steps:
#' 1. Checks if the `Seurat` package is installed and whether its version is `4.4.0`. If not, it installs the correct version.
#' 2. Extracts the expression matrix and metadata from the Seurat v5 object.
#' 3. Creates a new Seurat v4 object using the extracted data.
#' 4. Returns the newly created Seurat v4 object.
#' @export
srt.v5_to_v4 <- function(srt) {
# Check and install 'remotes' package if not installed
if (!requireNamespace("remotes", quietly = TRUE)) {
utils::install.packages("remotes")
}
# Check and install Seurat v4 if necessary
if (!requireNamespace("Seurat", quietly = TRUE)) {
remotes::install_version("Seurat", "4.4.0",
dependencies = <
5FC7
span class="pl-c1">FALSE,
repos = c("https://satijalab.r-universe.dev", getOption("repos"))
)
} else {
if (packageVersion("Seurat") != "4.4.0") {
remotes::install_version("Seurat", "4.4.0",
dependencies = FALSE,
repos = c("https://satijalab.r-universe.dev", getOption("repos"))
)
}
}
# Extract data from the Seurat v5 object
expr_matrix <- Seurat::GetAssayData(srt, slot = "counts")
metadata <- srt@meta.data
# Create a Seurat v4 object using extracted data
srt_v4 <- Seurat::CreateSeuratObject(counts = expr_matrix, meta.data = metadata)
return(srt_v4)
}
library(Seurat)
library(SeuratData)
data("pbmc3k")
pbmc3k <- srt.v5_to_v4(pbmc3k)
# for demonstration, split the object into reference and query
pbmc.reference <- pbmc3k[, 1:1350]
pbmc.query <- pbmc3k[, 1351:2700]
# perform standard preprocessing on each object
pbmc.reference <- NormalizeData(pbmc.reference)
pbmc.reference <- FindVariableFeatures(pbmc.reference)
pbmc.reference <- ScaleData(pbmc.reference)
pbmc.query <- NormalizeData(pbmc.query)
pbmc.query <- FindVariableFeatures(pbmc.query)
pbmc.query <- ScaleData(pbmc.query)
# find anchors
anchors <- FindTransferAnchors(reference = pbmc.reference, query = pbmc.query)
# transfer labels
predictions <- TransferData(
anchorset = anchors,
refdata = pbmc.reference$seurat_annotations
)
pbmc.query <- AddMetaData(object = pbmc.query, metadata = predictions)
-
Notifications
You must be signed in to change notification settings - Fork 0
License
Zaoqu-Liu/CAF
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published