diff --git a/R/generics.R b/R/generics.R index 9d34e3e..da15011 100644 --- a/R/generics.R +++ b/R/generics.R @@ -479,27 +479,32 @@ setGeneric("normPeak<-", function(x, dataset, check = TRUE, value) standardGener #' pbmc2 #' #' \donttest{ -#' sce <- SingleCellExperiment::SingleCellExperiment( -#' assays = list(counts = multiSampleMatrix) -#' ) -#' sce$sample <- pbmc$dataset -#' pbmc3 <- as.liger(sce, datasetVar = "sample") -#' pbmc3 +#' if (requireNamespace("SingleCellExperiment", quietly = TRUE)) { +#' sce <- SingleCellExperiment::SingleCellExperiment( +#' assays = list(counts = multiSampleMatrix) +#' ) +#' sce$sample <- pbmc$dataset +#' pbmc3 <- as.liger(sce, datasetVar = "sample") +#' pbmc3 +#' } #' -#' seu <- SeuratObject::CreateSeuratObject(multiSampleMatrix) -#' # Seurat creates variable "orig.ident" by identifying the cell barcode -#' # prefixes, which is indeed what we need in this case. Users might need -#' # to be careful and have it confirmed first. -#' pbmc4 <- as.liger(seu, datasetVar = "orig.ident") -#' pbmc4 +#' if (requireNamespace("Seurat", quietly = TRUE)) { +#' seu <- SeuratObject::CreateSeuratObject(multiSampleMatrix) +#' # Seurat creates variable "orig.ident" by identifying the cell barcode +#' # prefixes, which is indeed what we need in this case. Users might need +#' # to be careful and have it confirmed first. +#' pbmc4 <- as.liger(seu, datasetVar = "orig.ident") +#' pbmc4 #' -#' # As per Seurat V5 updates with layered data, specifically helpful udner the -#' # scenario of dataset integration. "counts" and etc for each datasets can be -#' # split into layers. -#' seu5 <- seu -#' seu5[["RNA"]] <- split(seu5[["RNA"]], pbmc$dataset) -#' print(SeuratObject::Layers(seu5)) -#' pbmc5 <- as.liger(seu5) +#' # As per Seurat V5 updates with layered data, specifically helpful udner the +#' # scenario of dataset integration. "counts" and etc for each datasets can be +#' # split into layers. +#' seu5 <- seu +#' seu5[["RNA"]] <- split(seu5[["RNA"]], pbmc$dataset) +#' print(SeuratObject::Layers(seu5)) +#' pbmc5 <- as.liger(seu5) +#' pbmc5 +#' } #' } as.liger <- function(object, ...) UseMethod("as.liger", object) diff --git a/R/import.R b/R/import.R index 58f6987..07110d7 100644 --- a/R/import.R +++ b/R/import.R @@ -23,7 +23,7 @@ #' common with 10X data). Default \code{"auto"} detects if matrix columns #' already has the exact prefix or not. Logical value forces the action. #' @param formatType Select preset of H5 file structure. Current available -#' options are \code{"10X"} and \code{"AnnData"}. Can be either a single +#' options are \code{"10x"} and \code{"anndata"}. Can be either a single #' specification for all datasets or a character vector that match with each #' dataset. #' @param anndataX The HDF5 path to the raw count data in an H5AD file. See @@ -59,9 +59,11 @@ #' lig <- createLiger(list(ctrl = tempPath)) #' #' # Create from other container object -#' ctrl.seu <- SeuratObject::CreateSeuratObject(ctrl.raw) -#' stim.seu <- SeuratObject::CreateSeuratObject(stim.raw) -#' pbmc2 <- createLiger(list(ctrl = ctrl.seu, stim = stim.seu)) +#' if (requireNamespace("SeuratObject", quietly = TRUE)) { +#' ctrl.seu <- SeuratObject::CreateSeuratObject(ctrl.raw) +#' stim.seu <- SeuratObject::CreateSeuratObject(stim.raw) +#' pbmc2 <- createLiger(list(ctrl = ctrl.seu, stim = stim.seu)) +#' } createLiger <- function( rawData, modal = NULL, @@ -276,7 +278,7 @@ createLigerDataset <- function( #' the AnnData was originally created. #' @param h5file Filename of an H5 file #' @param formatType Select preset of H5 file structure. Default \code{"10X"}. -#' Current available option is only \code{"10X"}. +#' Alternatively, we also support \code{"anndata"} for H5AD files. #' @param rawData,indicesName,indptrName The path in a H5 file for the raw #' sparse matrix data. These three types of data stands for the \code{x}, #' \code{i}, and \code{p} slots of a \code{\link[Matrix]{dgCMatrix-class}} @@ -304,7 +306,7 @@ createLigerDataset <- function( #' ld <- createH5LigerDataset(tempPath) createH5LigerDataset <- function( h5file, - formatType = "10X", + formatType = "10x", rawData = NULL, normData = NULL, scaleData = NULL, @@ -453,6 +455,18 @@ createH5LigerDataset <- function( #' tempPath <- tempfile(fileext = ".rds") #' saveRDS(lig, tempPath) #' lig <- readLiger(tempPath) +#' +#' \dontrun{ +#' # Read a old liger object <= 1.0.1 +#' # Assume the dimensionality reduction method applied was UMAP +#' # Assume the clustering was derived with Louvain method +#' lig <- readLiger( +#' filename = "path/to/oldLiger.rds", +#' dimredName = "UMAP", +#' clusterName = "louvain", +#' update = TRUE +#' ) +#' } readLiger <- function( filename, dimredName = "tsne_coords", diff --git a/man/as.liger.Rd b/man/as.liger.Rd index 2e0f991..9b726bd 100644 --- a/man/as.liger.Rd +++ b/man/as.liger.Rd @@ -65,26 +65,31 @@ pbmc2 <- as.liger(multiSampleMatrix, datasetVar = pbmc$dataset) pbmc2 \donttest{ -sce <- SingleCellExperiment::SingleCellExperiment( - assays = list(counts = multiSampleMatrix) -) -sce$sample <- pbmc$dataset -pbmc3 <- as.liger(sce, datasetVar = "sample") -pbmc3 +if (requireNamespace("SingleCellExperiment", quietly = TRUE)) { + sce <- SingleCellExperiment::SingleCellExperiment( + assays = list(counts = multiSampleMatrix) + ) + sce$sample <- pbmc$dataset + pbmc3 <- as.liger(sce, datasetVar = "sample") + pbmc3 +} -seu <- SeuratObject::CreateSeuratObject(multiSampleMatrix) -# Seurat creates variable "orig.ident" by identifying the cell barcode -# prefixes, which is indeed what we need in this case. Users might need -# to be careful and have it confirmed first. -pbmc4 <- as.liger(seu, datasetVar = "orig.ident") -pbmc4 +if (requireNamespace("Seurat", quietly = TRUE)) { + seu <- SeuratObject::CreateSeuratObject(multiSampleMatrix) + # Seurat creates variable "orig.ident" by identifying the cell barcode + # prefixes, which is indeed what we need in this case. Users might need + # to be careful and have it confirmed first. + pbmc4 <- as.liger(seu, datasetVar = "orig.ident") + pbmc4 -# As per Seurat V5 updates with layered data, specifically helpful udner the -# scenario of dataset integration. "counts" and etc for each datasets can be -# split into layers. -seu5 <- seu -seu5[["RNA"]] <- split(seu5[["RNA"]], pbmc$dataset) -print(SeuratObject::Layers(seu5)) -pbmc5 <- as.liger(seu5) + # As per Seurat V5 updates with layered data, specifically helpful udner the + # scenario of dataset integration. "counts" and etc for each datasets can be + # split into layers. + seu5 <- seu + seu5[["RNA"]] <- split(seu5[["RNA"]], pbmc$dataset) + print(SeuratObject::Layers(seu5)) + pbmc5 <- as.liger(seu5) + pbmc5 +} } } diff --git a/man/createH5LigerDataset.Rd b/man/createH5LigerDataset.Rd index b0adffe..a83bdef 100644 --- a/man/createH5LigerDataset.Rd +++ b/man/createH5LigerDataset.Rd @@ -6,7 +6,7 @@ \usage{ createH5LigerDataset( h5file, - formatType = "10X", + formatType = "10x", rawData = NULL, normData = NULL, scaleData = NULL, @@ -24,7 +24,7 @@ createH5LigerDataset( \item{h5file}{Filename of an H5 file} \item{formatType}{Select preset of H5 file structure. Default \code{"10X"}. -Current available option is only \code{"10X"}.} +Alternatively, we also support \code{"anndata"} for H5AD files.} \item{rawData, indicesName, indptrName}{The path in a H5 file for the raw sparse matrix data. These three types of data stands for the \code{x}, diff --git a/man/createLiger.Rd b/man/createLiger.Rd index e6e5e0d..3d9462e 100644 --- a/man/createLiger.Rd +++ b/man/createLiger.Rd @@ -55,7 +55,7 @@ common with 10X data). Default \code{"auto"} detects if matrix columns already has the exact prefix or not. Logical value forces the action.} \item{formatType}{Select preset of H5 file structure. Current available -options are \code{"10X"} and \code{"AnnData"}. Can be either a single +options are \code{"10x"} and \code{"anndata"}. Can be either a single specification for all datasets or a character vector that match with each dataset.} @@ -105,9 +105,11 @@ file.copy(from = h5Path, to = tempPath) lig <- createLiger(list(ctrl = tempPath)) # Create from other container object -ctrl.seu <- SeuratObject::CreateSeuratObject(ctrl.raw) -stim.seu <- SeuratObject::CreateSeuratObject(stim.raw) -pbmc2 <- createLiger(list(ctrl = ctrl.seu, stim = stim.seu)) +if (requireNamespace("SeuratObject", quietly = TRUE)) { + ctrl.seu <- SeuratObject::CreateSeuratObject(ctrl.raw) + stim.seu <- SeuratObject::CreateSeuratObject(stim.raw) + pbmc2 <- createLiger(list(ctrl = ctrl.seu, stim = stim.seu)) +} } \seealso{ \code{\link{createLigerDataset}}, \code{\link{createH5LigerDataset}} diff --git a/man/readLiger.Rd b/man/readLiger.Rd index fab16f4..0982412 100644 --- a/man/readLiger.Rd +++ b/man/readLiger.Rd @@ -55,4 +55,16 @@ lig <- createLiger(list(ctrl = h5tempPath)) tempPath <- tempfile(fileext = ".rds") saveRDS(lig, tempPath) lig <- readLiger(tempPath) + +\dontrun{ +# Read a old liger object <= 1.0.1 +# Assume the dimensionality reduction method applied was UMAP +# Assume the clustering was derived with Louvain method +lig <- readLiger( + filename = "path/to/oldLiger.rds", + dimredName = "UMAP", + clusterName = "louvain", + update = TRUE +) +} } diff --git a/tests/testthat/test_visualization.R b/tests/testthat/test_visualization.R index b8b461d..eee80f2 100644 --- a/tests/testthat/test_visualization.R +++ b/tests/testthat/test_visualization.R @@ -280,15 +280,18 @@ test_that("Plot spatial coordinates", { context("Sankey") test_that("PlotSankey", { + skip_if_not_installed("sankey") cellMeta(pbmcPlot, "ctrl_cluster", "ctrl") <- cellMeta(pbmcPlot, "leiden_cluster", "ctrl") cellMeta(pbmcPlot, "stim_cluster", "stim") <- cellMeta(pbmcPlot, "leiden_cluster", "stim") - grDevices::pdf(file = tempfile(pattern = "fig_", fileext = ".pdf")) + pdfName <- tempfile(pattern = "fig_", fileext = ".pdf") + grDevices::pdf(file = pdfName) expect_no_error({ plotSankey(pbmcPlot, "ctrl_cluster", "stim_cluster", titles = c("control", "LIGER", "stim"), prefixes = c("c", NA, "s")) }) grDevices::dev.off() + if (file.exists(pdfName)) unlink(pdfName) })