From 6ebd7a97ad35721b3c2e05c9212dec7fb530c184 Mon Sep 17 00:00:00 2001 From: "Brian M. Schilder" <34280215+bschilder@users.noreply.github.com> Date: Wed, 2 Oct 2024 20:57:59 +0100 Subject: [PATCH] Fix GH token --- .github/workflows/rworkflows.yml | 5 +---- DESCRIPTION | 2 +- NEWS.md | 2 +- R/calc_cor.R | 4 ++-- R/get_cor.R | 4 ++-- R/get_obsm.R | 6 +++--- R/get_uns.R | 2 +- R/get_variable_features.R | 2 +- R/get_varm.R | 4 ++-- R/get_x.R | 21 ++++++++++++++++----- R/map_data_anndata.R | 2 +- R/matrices_to_assayobjects.R | 4 ++-- R/set_obs.R | 2 +- R/set_uns.R | 2 +- R/set_var.R | 2 +- R/seurat_to_anndata.R | 13 ++++++++++--- 16 files changed, 46 insertions(+), 31 deletions(-) diff --git a/.github/workflows/rworkflows.yml b/.github/workflows/rworkflows.yml index 4b1c5e3..ea37af0 100644 --- a/.github/workflows/rworkflows.yml +++ b/.github/workflows/rworkflows.yml @@ -42,10 +42,7 @@ jobs: run_pkgdown: ${{ true }} has_runit: ${{ false }} has_latex: ${{ false }} - GITHUB_TOKEN: ${{ secrets.PAT_GITHUB }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run_docker: ${{ true }} - docker_user: bschilder - docker_org: neurogenomicslab - DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }} runner_os: ${{ runner.os }} cache_version: cache-v1 diff --git a/DESCRIPTION b/DESCRIPTION index 74b33de..d58d8ed 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -65,5 +65,5 @@ Remotes: github::aertslab/SCopeLoomR, github::scverse/anndataR VignetteBuilder: knitr -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Config/testthat/edition: 3 diff --git a/NEWS.md b/NEWS.md index 953a876..e19ce0a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -27,7 +27,7 @@ and avoid conflicts with piggyback-oriented functions. - `is_class` - Add new classes: "matrix_strict", "matrix_sparse", "delayed_array" * `get_x` - - Add new args `slot` and `assay` + - Add new args `layer` and `assay` * New funcs: - `get_obs_names` - `get_var_names` diff --git a/R/calc_cor.R b/R/calc_cor.R index b79babf..e570104 100644 --- a/R/calc_cor.R +++ b/R/calc_cor.R @@ -13,7 +13,7 @@ calc_cor <- function(obj, keys = NULL, graph_key = NULL, assay = NULL, - slot = NULL, + layer = NULL, transpose = FALSE, method = "pearson", fill_na = NULL, @@ -35,7 +35,7 @@ calc_cor <- function(obj, X <- get_x( obj = obj, assay = assay, - slot = slot, + layer = layer, transpose = transpose, n = 1, verbose = verbose) diff --git a/R/get_cor.R b/R/get_cor.R index 888061a..833b4e4 100644 --- a/R/get_cor.R +++ b/R/get_cor.R @@ -24,7 +24,7 @@ get_cor <- function(obj, keys = NULL, assay = NULL, - slot = NULL, + layer = NULL, graph_key = NULL, method = "pearson", return_obj = FALSE, @@ -53,7 +53,7 @@ get_cor <- function(obj, if(is.null(Xcor)){ Xcor <- calc_cor(obj = obj, assay = assay, - slot = slot, + layer = layer, keys = keys, method = method) } diff --git a/R/get_obsm.R b/R/get_obsm.R index f70de02..c5a1bf7 100644 --- a/R/get_obsm.R +++ b/R/get_obsm.R @@ -44,7 +44,7 @@ get_obsm <- function(obj, } else if (is_class(obj,"seurat")) { ## Seurat V1 if(methods::is(obj,"seurat")){ - ### Need a way to figure which slot names are availabe a priori #### + ### Need a way to figure which layer names are availabe a priori #### # messager("Extracting obsm from Seurat (V1).",v = verbose) # obsm <- list(PCA=list(embeddings=as.matrix(obj@pca.x), # loadings=as.matrix(obj@pca.rot))) @@ -95,10 +95,10 @@ get_obsm <- function(obj, # loadings="reducedDimA") obsm <- if(methods::.hasSlot(obj,"reducedDimS") && ncol(obj@reducedDimS)==ncol(obj) ){ - list(reducedDimS=t(methods::slot(obj, "reducedDimS"))) + list(reducedDimS=t(methods::layer(obj, "reducedDimS"))) } else if (methods::.hasSlot(obj,"reducedDimA") && ncol(obj@reducedDimA)==ncol(obj) ) { - list(reducedDimA=t(methods::slot(obj, "reducedDimA"))) + list(reducedDimA=t(methods::layer(obj, "reducedDimA"))) } else { NULL } diff --git a/R/get_uns.R b/R/get_uns.R index 119a20f..316f6ed 100644 --- a/R/get_uns.R +++ b/R/get_uns.R @@ -42,7 +42,7 @@ get_uns <- function(obj, uns <- lapply(stats::setNames(methods::slotNames(obj@experimentData), methods::slotNames(obj@experimentData)), function(x){ - methods::slot(obj@experimentData,x) + methods::layer(obj@experimentData,x) }) #### list #### } else if(is_class(obj,"list")){ diff --git a/R/get_variable_features.R b/R/get_variable_features.R index aa30309..d95db39 100644 --- a/R/get_variable_features.R +++ b/R/get_variable_features.R @@ -25,7 +25,7 @@ get_variable_features <- function(obj, } #### Check if empty #### if(length(Reduce(union,feat))==0){ - messager("Warning:","Variable features slot was empty. Returning NULL.") + messager("Warning:","Variable features layer was empty. Returning NULL.") return(NULL) } #### Reduce list #### diff --git a/R/get_varm.R b/R/get_varm.R index 0881fab..5581650 100644 --- a/R/get_varm.R +++ b/R/get_varm.R @@ -42,7 +42,7 @@ get_varm <- function(obj, } else if (is_class(obj,"seurat")) { ## Seurat V1 if(methods::is(obj,"seurat")){ - ### Need a way to figure which slot names are available a priori #### + ### Need a way to figure which layer names are available a priori #### # messager("Extracting varm from Seurat (V1).",v = verbose) # varm <- list(PCA=list(embeddings=as.matrix(obj@pca.x), # loadings=as.matrix(obj@pca.rot))) @@ -105,7 +105,7 @@ get_varm <- function(obj, # loadings="reducedDimA") varm <- if (methods::.hasSlot(obj,"reducedDimA") && ncol(obj@reducedDimA)==nrow(obj) ) { - list(reducedDimA=t(methods::slot(obj, "reducedDimA"))) + list(reducedDimA=t(methods::layer(obj, "reducedDimA"))) } else { return(NULL) } diff --git a/R/get_x.R b/R/get_x.R index dbe06c1..9088d8f 100644 --- a/R/get_x.R +++ b/R/get_x.R @@ -18,7 +18,7 @@ get_x <- function(obj, n=NULL, simplify=TRUE, assay=NULL, - slot=NULL, + layer=NULL, as_sparse=FALSE, verbose=TRUE){ # devoptera::args2vars(get_x) @@ -55,16 +55,27 @@ get_x <- function(obj, data <- list(RNA.counts=obj@raw.data, RNA.data=obj@data, RNA.scale.data=obj@scale.data) - ## Seurat V2+ - } else { + ## Seurat V2-5 + } else if(packageVersion("Seurat")<"5") { assays <- obj@assays if(!is.null(assay)) assays[assays %in% assay] data <- lapply(assays,function(a){ slots <- c("counts","data","scale.data") slots <- slots[sapply(slots,function(s){methods::.hasSlot(a,s)})] - if(!is.null(slot)) slots <- slots[slots %in% slot] + if(!is.null(layer)) slots <- slots[slots %in% layer] lapply(stats::setNames(slots,slots), function(s){ - methods::slot(a,s) + methods::layer(a,s) + }) + }) |> unlist(recursive = FALSE) + ## Seurat V5+ + } else{ + assays <- obj@assays + if(!is.null(assay)) assays <- assays[names(assays) %in% assay] + data <- lapply(assays,function(a){ + layers <- SeuratObject::Layers(obj) + if(!is.null(layer)) layers <- intersect(layers,layer) + lapply(stats::setNames(layers,layers), function(s){ + SeuratObject::LayerData(a,s) }) }) |> unlist(recursive = FALSE) } diff --git a/R/map_data_anndata.R b/R/map_data_anndata.R index db636a3..deaa654 100644 --- a/R/map_data_anndata.R +++ b/R/map_data_anndata.R @@ -78,7 +78,7 @@ map_data_anndata <- function(obj, genes = rownames(assays[[1]]), original_rowdata = obj$var) #### Construct new SummarizedExperiment #### - ## Remove PC slot to avoid error: + ## Remove PC layer to avoid error: ## Error: ValueError: Value passed for key 'PCs' is of incorrect shape. obj$obsm$X_pca <- NULL obj$varm$PCs <- NULL diff --git a/R/matrices_to_assayobjects.R b/R/matrices_to_assayobjects.R index f261327..c60a725 100644 --- a/R/matrices_to_assayobjects.R +++ b/R/matrices_to_assayobjects.R @@ -3,11 +3,11 @@ #' Convert a named list of X_list to a named list of \pkg{Seurat} #' assay objects (see \link[SeuratObject]{CreateAssayObject}). #' @param X_list A named list of matrices (one per unique assay in -#' \code{X_list}) to be put in the \code{counts} slot of each +#' \code{X_list}) to be put in the \code{counts} layer of each #' \code{AssayObject}. #' @param var_features (Optional) A named list of feature metadata data.frames #' (one per unique assay in \code{X_list}) to be put in the -#' \code{meta.features} slot of each \code{AssayObject}. +#' \code{meta.features} layer of each \code{AssayObject}. #' @inheritParams converters #' @returns A named list of \pkg{Seurat} assay objects. #' diff --git a/R/set_obs.R b/R/set_obs.R index 3c78e4f..174bf3b 100644 --- a/R/set_obs.R +++ b/R/set_obs.R @@ -1,6 +1,6 @@ #' Set unstructured data #' -#' Set the sample observations slot (i.e. cell metadata) +#' Set the sample observations layer (i.e. cell metadata) #' in any single-cell object that has one. #' @param obs Observation metadata #' as a \link[base]{data.frame} with samples as row names. diff --git a/R/set_uns.R b/R/set_uns.R index effc728..d2e4f8c 100644 --- a/R/set_uns.R +++ b/R/set_uns.R @@ -1,6 +1,6 @@ #' Set unstructured data #' -#' Set the unstructured data slot in any single-cell object that has one. +#' Set the unstructured data layer in any single-cell object that has one. #' @param uns Unstructured data to be stored in object. #' @param key Name of the list element to store \code{uns} in. #' @inheritParams converters diff --git a/R/set_var.R b/R/set_var.R index bade28c..65327e0 100644 --- a/R/set_var.R +++ b/R/set_var.R @@ -1,6 +1,6 @@ #' Set unstructured data #' -#' Set the feature variable slot (i.e. gene metadata) +#' Set the feature variable layer (i.e. gene metadata) #' in any single-cell object that has one. #' @param var Variable metadata #' as a \link[base]{data.frame} with features as row names. diff --git a/R/seurat_to_anndata.R b/R/seurat_to_anndata.R index cb75b08..0016a2c 100644 --- a/R/seurat_to_anndata.R +++ b/R/seurat_to_anndata.R @@ -7,17 +7,18 @@ #' @export #' @examples #' obj <- example_obj("seurat") -#' obj2 <- seurat_to_anndata(obj) +#' obj2 <- seurat_to_anndata(obj, method="seuratdisk") seurat_to_anndata <- function(obj, reimport = TRUE, save_path = tempfile(fileext = ".h5ad"), - method = c("sceasy","anndatar"), + method = c("sceasy","anndatar","seuratdisk"), verbose = TRUE, ...){ messager_to() method <- tolower(method)[1] #### Convert #### + if(!is.null(save_path)) save_path <- path.expand(save_path) # Method: anndatar if(method=="anndatar"){ adat <- anndataR::from_Seurat( @@ -29,7 +30,7 @@ seurat_to_anndata <- function(obj, }, file = save_path ) - } else { + } else if(method=="sceasy"){ # Method: sceasy activate_conda(verbose=verbose) adat <- sceasy::convertFormat(obj = obj, @@ -48,6 +49,12 @@ seurat_to_anndata <- function(obj, save_path = save_path, verbose = verbose) } + } else{ + messager("Saving ==>",save_path) + h5Seurat_path <- gsub("\\.h5ad$",".h5Seurat",save_path) + SeuratDisk::SaveH5Seurat(obj, filename = h5Seurat_path) + SeuratDisk::Convert(h5Seurat_path, dest = "h5ad") + adat <- anndata::read_h5ad(save_path) } #### Return ### return(adat)