From cc0792f5b0461c4d1dcd9cc659b7479ac5381676 Mon Sep 17 00:00:00 2001 From: josef0731 Date: Fri, 19 Jan 2024 14:30:10 +0000 Subject: [PATCH 1/2] changed from SeuratDisk to sceasy for convertSeuratToH5ad --- R/transitions.R | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/R/transitions.R b/R/transitions.R index 83b62bf..72b45b2 100644 --- a/R/transitions.R +++ b/R/transitions.R @@ -315,7 +315,7 @@ getCSRpotential <- function(SeuratObj, ighc_count_assay_name = "IGHC", #' #' @import reticulate #' @importFrom Seurat Assays -#' @importFrom SeuratDisk SaveH5Seurat Convert +#' @importFrom sceasy convertFormat #' @export convertSeuratToH5ad convertSeuratToH5ad <- function(SeuratObj, assays, h5ad_filename, conda_env = 'scicsr'){ @@ -329,22 +329,15 @@ convertSeuratToH5ad <- function(SeuratObj, assays, h5ad_filename, } if( ! grepl(".h5ad$", h5ad_filename) ) stop("The filename given in 'h5ad_filename' should end with the extension '.h5ad'.") - SeuratDisk::SaveH5Seurat(SeuratObj, gsub(".h5ad$", ".h5Seurat", h5ad_filename), overwrite = TRUE) - out_files = c() + use_condaenv(conda_env, required = TRUE) + out_files <- c() for(assay in assays){ message(paste0("Writing assay '", assay, "' into .h5ad file.")) if( ! assay %in% Seurat::Assays( SeuratObj )) - stop("The assay name given in 'ighc_count_assay_name' is not found in SeuatObj.") + stop(paste0("The assay name '", assay, "'given in 'assays' is not found in SeuatObj.")) out_file <- gsub(".h5ad$", paste0("_assay-", assay, ".h5ad"), h5ad_filename) - SeuratDisk::Convert(gsub(".h5ad$", ".h5Seurat", h5ad_filename), out_file, - assay = assay, overwrite = TRUE) - # see https://github.com/theislab/scvelo/issues/255 - # here read the adata in and perform the fix - use_condaenv(conda_env, required = TRUE) - py_run_string("import scanpy as sc") - py_run_string(paste0("adata = sc.read_h5ad('", out_file, "')")) - py_run_string("adata.__dict__['_raw'].__dict__['_var'] = adata.__dict__['_raw'].__dict__['_var'].rename(columns={'_index': 'features'})") - py_run_string(paste0("adata.write_h5ad('", out_file, "')")) + sceasy::convertFormat(SeuratObj, from = "seurat", to = "anndata", outFile = out_file, + assay = assay, main_layer = "scale.data") out_files <- c(out_files, out_file) } return( out_files ) @@ -654,8 +647,6 @@ splitAnnData <- function(anndata_file, split.by, levels, conda_env = 'scicsr') py_run_string(paste0("adata_subset = adata[adata.obs['", split.by, "'] == '", lvl, "']")) subset_filename <- paste0(gsub(".h5ad", "", basename(anndata_file)), "_", lvl, ".h5ad") subset_filename <- paste0(dirname(anndata_file), "/", fs::path_sanitize(subset_filename)) - # see https://github.com/theislab/scvelo/issues/255 - py_run_string("adata_subset.__dict__['_raw'].__dict__['_var'] = adata_subset.__dict__['_raw'].__dict__['_var'].rename(columns={'_index': 'features'})") py_run_string(paste0("adata_subset.write_h5ad('", subset_filename, "')")) out_fn <- c(out_fn, subset_filename) } From 2dbcc3d1b7ed91a65620d4de8aa23a144d7676a1 Mon Sep 17 00:00:00 2001 From: josef0731 Date: Fri, 19 Jan 2024 17:39:39 +0000 Subject: [PATCH 2/2] to test compilation of both vignettes & pkgdown --- DESCRIPTION | 4 ++-- NAMESPACE | 3 +-- R/transitions.R | 3 +++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5cadd83..819655b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: sciCSR Type: Package Title: Single-cell Inference of Class Switch Recombination -Version: 0.3.1 +Version: 0.3.2 Authors@R: c( person( given = "Joseph", family = "Ng", email = "joseph.chifung.ng@gmail.com", role = c( "aut", "cre" ) ) ) Author: Joseph Ng [aut, cre] @@ -21,7 +21,7 @@ Imports: GenomeInfoDb, scales, stringr, Seurat (>= 4.0), - SeuratDisk (>= 0.0.0.9020), + sceasy, cowplot, fs, igraph, diff --git a/NAMESPACE b/NAMESPACE index 7a65af9..6ef7aa4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -55,8 +55,6 @@ importFrom(Seurat,Assays) importFrom(Seurat,Cells) importFrom(Seurat,CreateAssayObject) importFrom(Seurat,ScaleData) -importFrom(SeuratDisk,Convert) -importFrom(SeuratDisk,SaveH5Seurat) importFrom(cowplot,theme_cowplot) importFrom(grid,grid.newpage) importFrom(grid,grid.raster) @@ -79,6 +77,7 @@ importFrom(reshape2,dcast) importFrom(reshape2,melt) importFrom(reticulate,py_config) importFrom(reticulate,use_condaenv) +importFrom(sceasy,convertFormat) importFrom(stats,as.formula) importFrom(stats,p.adjust) importFrom(stats,quantile) diff --git a/R/transitions.R b/R/transitions.R index 72b45b2..31fc5ae 100644 --- a/R/transitions.R +++ b/R/transitions.R @@ -335,6 +335,9 @@ convertSeuratToH5ad <- function(SeuratObj, assays, h5ad_filename, message(paste0("Writing assay '", assay, "' into .h5ad file.")) if( ! assay %in% Seurat::Assays( SeuratObj )) stop(paste0("The assay name '", assay, "'given in 'assays' is not found in SeuatObj.")) + SeuratObj@assays[[assay]]@meta.features <- SeuratObj@assays[[assay]]@meta.features[ + rownames(SeuratObj@assays[[assay]]@meta.features) %in% rownames(SeuratObj@assays[[assay]]@scale.data), + ] out_file <- gsub(".h5ad$", paste0("_assay-", assay, ".h5ad"), h5ad_filename) sceasy::convertFormat(SeuratObj, from = "seurat", to = "anndata", outFile = out_file, assay = assay, main_layer = "scale.data")