0ccddee1 |
#' @title Export a \link[SingleCellExperiment]{SingleCellExperiment} R object as
#' Python annData object
|
3a66a102 |
#' @description Writes all assays, colData, rowData, reducedDims, and altExps objects in a
#' \link[SingleCellExperiment]{SingleCellExperiment} to a Python annData object in the .h5ad format
#' All parameters of Anndata.write_h5ad function (https://icb-anndata.readthedocs-hosted.com/en/stable/anndata.AnnData.write_h5ad.html)
#' are available as parameters to this export function and set to defaults. Defaults can be
#' overridden at function call.
#' @param sce \link[SingleCellExperiment]{SingleCellExperiment} R object to be
#' exported.
#' @param useAssay Character. The name of assay of
#' interests that will be set as the primary matrix of the output AnnData.
|
0ccddee1 |
#' Default \code{"counts"}.
|
3a66a102 |
#' @param outputDir Path to the directory where .h5ad outputs will be written. Default is the current working directory.
#' @param prefix Prefix to use for the name of the output file. Default \code{"sample"}.
#' @param overwrite Boolean. Default \code{TRUE}.
#' @param compression If output file compression is required, this variable accepts
|
f19cfab8 |
#' 'gzip', 'lzf' or "None" as inputs. Default \code{"gzip"}.
|
3a66a102 |
#' @param compressionOpts Integer. Sets the compression level
#' @param forceDense Default \code{False} Write sparse data as a dense matrix.
|
0ccddee1 |
#' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}.
|
ce30e109 |
#' @return Generates a Python anndata object containing data from \code{inSCE}.
|
3a66a102 |
#' @examples
#' data(sce_chcl, package = "scds")
|
c802b90f |
#' \dontrun{
|
3a66a102 |
#' exportSCEtoAnnData(sce=sce_chcl, compression="gzip")
|
c802b90f |
#' }
|
3a66a102 |
#' @export
|
0ccddee1 |
exportSCEtoAnnData <- function(sce,
|
3a66a102 |
useAssay = 'counts',
outputDir = "./",
prefix = "sample",
overwrite = TRUE,
|
ef39dc24 |
compression = c('gzip','lzf', "None"),
|
3a66a102 |
compressionOpts = NULL,
|
ef39dc24 |
forceDense = FALSE){
|
3a66a102 |
compression <- match.arg(compression)
|
ef39dc24 |
#forceDense <- match.arg(forceDense)
|
3a66a102 |
if (compression == 'None'){
compression <- NULL
}
if (!reticulate::py_module_available(module = "scanpy")) {
warning("Cannot find python module 'scanpy', please install Conda and",
" run sctkPythonInstallConda() or run sctkPythonInstallVirtualEnv().",
"If one of these have been previously run to install the modules,",
"make sure to run selectSCTKConda() or selectSCTKVirtualEnvironment(),",
" respectively, if R has been restarted since the module installation.",
|
efd83303 |
" Alternatively, 'scanpy' can be installed on the local machine",
|
3a66a102 |
"with pip (e.g. pip install --user scanpy) and then the 'use_python()'",
" function from the 'reticulate' package can be used to select the",
" correct Python environment.")
return(sce)}
dir.create(outputDir, showWarnings = FALSE, recursive = TRUE)
fileName <- paste0(prefix,".h5ad")
filePath <- file.path(outputDir,fileName)
if (file.exists(filePath) && !isTRUE(overwrite)) {
stop(paste0(path, " already exists. Change 'outputDir' or set 'overwrite' to TRUE."))
|
ef39dc24 |
}
if (isTRUE(forceDense)) {
forceDense <- "True"
} else if (isFALSE(forceDense)) {
forceDense <- "False"
} else {
stop("Argument `forceDense` should be `TRUE` or `FALSE`")
}
AssayName <- SummarizedExperiment::assayNames(sce)
for (assay in AssayName){
if (!methods::is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
SummarizedExperiment::assay(sce, assay) <- .convertToMatrix(SummarizedExperiment::assay(sce, assay))
|
3a66a102 |
}
|
ef39dc24 |
}
annData <- .sce2adata(sce, useAssay)
|
3a66a102 |
annData$write_h5ad(filePath,
|
0ccddee1 |
compression = compression,
|
3a66a102 |
compression_opts = compressionOpts,
force_dense = forceDense)
}
|