Browse code

Fix bugs in exportSCEtoSeurat. Add parallel param to scDblFinder. Fix typo in warning message in exportSCEtoAnndata

rz2333 authored on 13/04/2022 15:19:13
Showing1 changed files
... ...
@@ -44,7 +44,7 @@ exportSCEtoAnnData <- function(sce,
44 44
             "If one of these have been previously run to install the modules,",
45 45
             "make sure to run selectSCTKConda() or selectSCTKVirtualEnvironment(),",
46 46
             " respectively, if R has been restarted since the module installation.",
47
-            " Alternatively, Scrublet can be installed on the local machine",
47
+            " Alternatively, 'scanpy' can be installed on the local machine",
48 48
             "with pip (e.g. pip install --user scanpy) and then the 'use_python()'",
49 49
             " function from the 'reticulate' package can be used to select the",
50 50
             " correct Python environment.")
Browse code

Minor fixes

Yichen Wang authored on 13/04/2021 07:25:29
Showing1 changed files
... ...
@@ -14,7 +14,7 @@
14 14
 #' @param prefix Prefix to use for the name of the output file. Default \code{"sample"}.
15 15
 #' @param overwrite Boolean. Default \code{TRUE}.
16 16
 #' @param compression If output file compression is required, this variable accepts
17
-#' 'gzip', 'lzf' or "None" as inputs. Default \code{gzip"}.
17
+#' 'gzip', 'lzf' or "None" as inputs. Default \code{"gzip"}.
18 18
 #' @param compressionOpts Integer. Sets the compression level
19 19
 #' @param forceDense Default \code{False} Write sparse data as a dense matrix.
20 20
 #' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}.
Browse code

Fix exporting bugs

Yichen Wang authored on 13/04/2021 06:04:25
Showing1 changed files
... ...
@@ -14,7 +14,7 @@
14 14
 #' @param prefix Prefix to use for the name of the output file. Default \code{"sample"}.
15 15
 #' @param overwrite Boolean. Default \code{TRUE}.
16 16
 #' @param compression If output file compression is required, this variable accepts
17
-#' 'gzip' or 'lzf' as inputs. Default \code{None}.
17
+#' 'gzip', 'lzf' or "None" as inputs. Default \code{gzip"}.
18 18
 #' @param compressionOpts Integer. Sets the compression level
19 19
 #' @param forceDense Default \code{False} Write sparse data as a dense matrix.
20 20
 #' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}.
... ...
@@ -30,15 +30,14 @@ exportSCEtoAnnData <- function(sce,
30 30
                                 outputDir = "./",
31 31
                                 prefix = "sample",
32 32
                                 overwrite = TRUE,
33
-                                compression = c('None','lzf','gzip'),
33
+                                compression = c('gzip','lzf', "None"),
34 34
                                 compressionOpts = NULL,
35
-                                forceDense = c('False','True')){
35
+                                forceDense = FALSE){
36 36
   compression <- match.arg(compression)
37
-  forceDense <- match.arg(forceDense)
37
+  #forceDense <- match.arg(forceDense)
38 38
   if (compression == 'None'){
39 39
     compression <- NULL
40 40
   }
41
-
42 41
   if (!reticulate::py_module_available(module = "scanpy")) {
43 42
     warning("Cannot find python module 'scanpy', please install Conda and",
44 43
             " run sctkPythonInstallConda() or run sctkPythonInstallVirtualEnv().",
... ...
@@ -50,24 +49,26 @@ exportSCEtoAnnData <- function(sce,
50 49
             " function from the 'reticulate' package can be used to select the",
51 50
             " correct Python environment.")
52 51
     return(sce)}
53
-
54
-  AssayName <- SummarizedExperiment::assayNames(sce)
55
-  for (assay in AssayName){
56
-    if (!methods::is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
57
-      SummarizedExperiment::assay(sce, assay) <- .convertToMatrix(SummarizedExperiment::assay(sce, assay))
58
-    }
59
-  }
60
-
61
-
62 52
   dir.create(outputDir, showWarnings = FALSE, recursive = TRUE)
63
-  annData <- .sce2adata(sce,useAssay)
64 53
   fileName <- paste0(prefix,".h5ad")
65 54
   filePath <- file.path(outputDir,fileName)
66
-
67 55
   if (file.exists(filePath) && !isTRUE(overwrite)) {
68 56
     stop(paste0(path, " already exists. Change 'outputDir' or set 'overwrite' to TRUE."))
57
+  }
58
+  if (isTRUE(forceDense)) {
59
+    forceDense <- "True"
60
+  } else if (isFALSE(forceDense)) {
61
+    forceDense <- "False"
62
+  } else {
63
+    stop("Argument `forceDense` should be `TRUE` or `FALSE`")
64
+  }
65
+  AssayName <- SummarizedExperiment::assayNames(sce)
66
+  for (assay in AssayName){
67
+    if (!methods::is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
68
+      SummarizedExperiment::assay(sce, assay) <- .convertToMatrix(SummarizedExperiment::assay(sce, assay))
69 69
     }
70
-
70
+  }
71
+  annData <- .sce2adata(sce, useAssay)
71 72
   annData$write_h5ad(filePath,
72 73
                      compression = compression,
73 74
                      compression_opts = compressionOpts,
Browse code

Merge upstream

Yusuke Koga authored on 16/10/2020 01:47:26
Showing0 changed files
Browse code

Put dontrun around examples that were outputting files. Removed Remotes from DESCRIPTION. Further compressed data objects

Joshua D. Campbell authored on 16/10/2020 00:52:57
Showing1 changed files
... ...
@@ -20,8 +20,9 @@
20 20
 #' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}.
21 21
 #' @examples
22 22
 #' data(sce_chcl, package = "scds")
23
+#' \dontrun{
23 24
 #' exportSCEtoAnnData(sce=sce_chcl, compression="gzip")
24
-#'
25
+#' }
25 26
 #' @export
26 27
 exportSCEtoAnnData <- function(sce,
27 28
                                 useAssay = 'counts',
Browse code

Merge branch 'master' of github.com:compbiomed/singleCellTK

Yusuke Koga authored on 16/10/2020 00:27:01
Showing0 changed files
Browse code

Add Runnable functions

Yusuke Koga authored on 15/10/2020 16:44:51
Showing1 changed files
... ...
@@ -1,5 +1,5 @@
1
-#' @title Export a \link[SingleCellExperiment]{SingleCellExperiment} R object as 
2
-#' Python annData object 
1
+#' @title Export a \link[SingleCellExperiment]{SingleCellExperiment} R object as
2
+#' Python annData object
3 3
 #' @description Writes all assays, colData, rowData, reducedDims, and altExps objects in a
4 4
 #' \link[SingleCellExperiment]{SingleCellExperiment} to a Python annData object in the .h5ad format
5 5
 #' All parameters of Anndata.write_h5ad function (https://icb-anndata.readthedocs-hosted.com/en/stable/anndata.AnnData.write_h5ad.html)
... ...
@@ -9,7 +9,7 @@
9 9
 #'  exported.
10 10
 #' @param useAssay Character. The name of assay of
11 11
 #' interests that will be set as the primary matrix of the output AnnData.
12
-#' Default \code{"counts"}. 
12
+#' Default \code{"counts"}.
13 13
 #' @param outputDir Path to the directory where .h5ad outputs will be written. Default is the current working directory.
14 14
 #' @param prefix Prefix to use for the name of the output file. Default \code{"sample"}.
15 15
 #' @param overwrite Boolean. Default \code{TRUE}.
... ...
@@ -17,15 +17,13 @@
17 17
 #' 'gzip' or 'lzf' as inputs. Default \code{None}.
18 18
 #' @param compressionOpts Integer. Sets the compression level
19 19
 #' @param forceDense Default \code{False} Write sparse data as a dense matrix.
20
-#' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}. 
20
+#' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}.
21 21
 #' @examples
22
-#' \dontrun{
23 22
 #' data(sce_chcl, package = "scds")
24 23
 #' exportSCEtoAnnData(sce=sce_chcl, compression="gzip")
25
-#' }
26
-#' 
24
+#'
27 25
 #' @export
28
-exportSCEtoAnnData <- function(sce, 
26
+exportSCEtoAnnData <- function(sce,
29 27
                                 useAssay = 'counts',
30 28
                                 outputDir = "./",
31 29
                                 prefix = "sample",
... ...
@@ -50,7 +48,7 @@ exportSCEtoAnnData <- function(sce,
50 48
             " function from the 'reticulate' package can be used to select the",
51 49
             " correct Python environment.")
52 50
     return(sce)}
53
-  
51
+
54 52
   AssayName <- SummarizedExperiment::assayNames(sce)
55 53
   for (assay in AssayName){
56 54
     if (!methods::is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
... ...
@@ -58,18 +56,18 @@ exportSCEtoAnnData <- function(sce,
58 56
     }
59 57
   }
60 58
 
61
-  
59
+
62 60
   dir.create(outputDir, showWarnings = FALSE, recursive = TRUE)
63 61
   annData <- .sce2adata(sce,useAssay)
64 62
   fileName <- paste0(prefix,".h5ad")
65 63
   filePath <- file.path(outputDir,fileName)
66
-  
64
+
67 65
   if (file.exists(filePath) && !isTRUE(overwrite)) {
68 66
     stop(paste0(path, " already exists. Change 'outputDir' or set 'overwrite' to TRUE."))
69 67
     }
70 68
 
71 69
   annData$write_h5ad(filePath,
72
-                     compression = compression, 
70
+                     compression = compression,
73 71
                      compression_opts = compressionOpts,
74 72
                      force_dense = forceDense)
75 73
 }
Browse code

Merge devel branch (Oct 5) into master branch

Yusuke Koga authored on 09/10/2020 17:57:06
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,75 @@
1
+#' @title Export a \link[SingleCellExperiment]{SingleCellExperiment} R object as 
2
+#' Python annData object 
3
+#' @description Writes all assays, colData, rowData, reducedDims, and altExps objects in a
4
+#' \link[SingleCellExperiment]{SingleCellExperiment} to a Python annData object in the .h5ad format
5
+#' All parameters of Anndata.write_h5ad function (https://icb-anndata.readthedocs-hosted.com/en/stable/anndata.AnnData.write_h5ad.html)
6
+#' are available as parameters to this export function and set to defaults. Defaults can be
7
+#' overridden at function call.
8
+#' @param sce \link[SingleCellExperiment]{SingleCellExperiment} R object to be
9
+#'  exported.
10
+#' @param useAssay Character. The name of assay of
11
+#' interests that will be set as the primary matrix of the output AnnData.
12
+#' Default \code{"counts"}. 
13
+#' @param outputDir Path to the directory where .h5ad outputs will be written. Default is the current working directory.
14
+#' @param prefix Prefix to use for the name of the output file. Default \code{"sample"}.
15
+#' @param overwrite Boolean. Default \code{TRUE}.
16
+#' @param compression If output file compression is required, this variable accepts
17
+#' 'gzip' or 'lzf' as inputs. Default \code{None}.
18
+#' @param compressionOpts Integer. Sets the compression level
19
+#' @param forceDense Default \code{False} Write sparse data as a dense matrix.
20
+#' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}. 
21
+#' @examples
22
+#' \dontrun{
23
+#' data(sce_chcl, package = "scds")
24
+#' exportSCEtoAnnData(sce=sce_chcl, compression="gzip")
25
+#' }
26
+#' 
27
+#' @export
28
+exportSCEtoAnnData <- function(sce, 
29
+                                useAssay = 'counts',
30
+                                outputDir = "./",
31
+                                prefix = "sample",
32
+                                overwrite = TRUE,
33
+                                compression = c('None','lzf','gzip'),
34
+                                compressionOpts = NULL,
35
+                                forceDense = c('False','True')){
36
+  compression <- match.arg(compression)
37
+  forceDense <- match.arg(forceDense)
38
+  if (compression == 'None'){
39
+    compression <- NULL
40
+  }
41
+
42
+  if (!reticulate::py_module_available(module = "scanpy")) {
43
+    warning("Cannot find python module 'scanpy', please install Conda and",
44
+            " run sctkPythonInstallConda() or run sctkPythonInstallVirtualEnv().",
45
+            "If one of these have been previously run to install the modules,",
46
+            "make sure to run selectSCTKConda() or selectSCTKVirtualEnvironment(),",
47
+            " respectively, if R has been restarted since the module installation.",
48
+            " Alternatively, Scrublet can be installed on the local machine",
49
+            "with pip (e.g. pip install --user scanpy) and then the 'use_python()'",
50
+            " function from the 'reticulate' package can be used to select the",
51
+            " correct Python environment.")
52
+    return(sce)}
53
+  
54
+  AssayName <- SummarizedExperiment::assayNames(sce)
55
+  for (assay in AssayName){
56
+    if (!methods::is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
57
+      SummarizedExperiment::assay(sce, assay) <- .convertToMatrix(SummarizedExperiment::assay(sce, assay))
58
+    }
59
+  }
60
+
61
+  
62
+  dir.create(outputDir, showWarnings = FALSE, recursive = TRUE)
63
+  annData <- .sce2adata(sce,useAssay)
64
+  fileName <- paste0(prefix,".h5ad")
65
+  filePath <- file.path(outputDir,fileName)
66
+  
67
+  if (file.exists(filePath) && !isTRUE(overwrite)) {
68
+    stop(paste0(path, " already exists. Change 'outputDir' or set 'overwrite' to TRUE."))
69
+    }
70
+
71
+  annData$write_h5ad(filePath,
72
+                     compression = compression, 
73
+                     compression_opts = compressionOpts,
74
+                     force_dense = forceDense)
75
+}
Browse code

Revert "Sctk documentation "

Joshua D. Campbell authored on 09/06/2020 23:22:05 • GitHub committed on 09/06/2020 23:22:05
Showing1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,75 +0,0 @@
1
-#' @title Export a \link[SingleCellExperiment]{SingleCellExperiment} R object as 
2
-#' Python annData object 
3
-#' @description Writes all assays, colData, rowData, reducedDims, and altExps objects in a
4
-#' \link[SingleCellExperiment]{SingleCellExperiment} to a Python annData object in the .h5ad format
5
-#' All parameters of Anndata.write_h5ad function (https://icb-anndata.readthedocs-hosted.com/en/stable/anndata.AnnData.write_h5ad.html)
6
-#' are available as parameters to this export function and set to defaults. Defaults can be
7
-#' overridden at function call.
8
-#' @param sce \link[SingleCellExperiment]{SingleCellExperiment} R object to be
9
-#'  exported.
10
-#' @param useAssay Character. The name of assay of
11
-#' interests that will be set as the primary matrix of the output AnnData.
12
-#' Default \code{"counts"}. 
13
-#' @param outputDir Path to the directory where .h5ad outputs will be written. Default is the current working directory.
14
-#' @param prefix Prefix to use for the name of the output file. Default \code{"sample"}.
15
-#' @param overwrite Boolean. Default \code{TRUE}.
16
-#' @param compression If output file compression is required, this variable accepts
17
-#' 'gzip' or 'lzf' as inputs. Default \code{None}.
18
-#' @param compressionOpts Integer. Sets the compression level
19
-#' @param forceDense Default \code{False} Write sparse data as a dense matrix.
20
-#' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}. 
21
-#' @examples
22
-#' \dontrun{
23
-#' data(sce_chcl, package = "scds")
24
-#' exportSCEtoAnnData(sce=sce_chcl, compression="gzip")
25
-#' }
26
-#' 
27
-#' @export
28
-exportSCEtoAnnData <- function(sce, 
29
-                                useAssay = 'counts',
30
-                                outputDir = "./",
31
-                                prefix = "sample",
32
-                                overwrite = TRUE,
33
-                                compression = c('None','lzf','gzip'),
34
-                                compressionOpts = NULL,
35
-                                forceDense = c('False','True')){
36
-  compression <- match.arg(compression)
37
-  forceDense <- match.arg(forceDense)
38
-  if (compression == 'None'){
39
-    compression <- NULL
40
-  }
41
-
42
-  if (!reticulate::py_module_available(module = "scanpy")) {
43
-    warning("Cannot find python module 'scanpy', please install Conda and",
44
-            " run sctkPythonInstallConda() or run sctkPythonInstallVirtualEnv().",
45
-            "If one of these have been previously run to install the modules,",
46
-            "make sure to run selectSCTKConda() or selectSCTKVirtualEnvironment(),",
47
-            " respectively, if R has been restarted since the module installation.",
48
-            " Alternatively, Scrublet can be installed on the local machine",
49
-            "with pip (e.g. pip install --user scanpy) and then the 'use_python()'",
50
-            " function from the 'reticulate' package can be used to select the",
51
-            " correct Python environment.")
52
-    return(sce)}
53
-  
54
-  AssayName <- SummarizedExperiment::assayNames(sce)
55
-  for (assay in AssayName){
56
-    if (!methods::is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
57
-      SummarizedExperiment::assay(sce, assay) <- .convertToMatrix(SummarizedExperiment::assay(sce, assay))
58
-    }
59
-  }
60
-
61
-  
62
-  dir.create(outputDir, showWarnings = FALSE, recursive = TRUE)
63
-  annData <- .sce2adata(sce,useAssay)
64
-  fileName <- paste0(prefix,".h5ad")
65
-  filePath <- file.path(outputDir,fileName)
66
-  
67
-  if (file.exists(filePath) && !isTRUE(overwrite)) {
68
-    stop(paste0(path, " already exists. Change 'outputDir' or set 'overwrite' to TRUE."))
69
-    }
70
-
71
-  annData$write_h5ad(filePath,
72
-                     compression = compression, 
73
-                     compression_opts = compressionOpts,
74
-                     force_dense = forceDense)
75
-}
Browse code

Merge conflict

rz2333 authored on 04/05/2020 02:25:09
Showing1 changed files
... ...
@@ -53,7 +53,7 @@ exportSCEtoAnnData <- function(sce,
53 53
   
54 54
   AssayName <- SummarizedExperiment::assayNames(sce)
55 55
   for (assay in AssayName){
56
-    if (!is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
56
+    if (!methods::is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
57 57
       SummarizedExperiment::assay(sce, assay) <- .convertToMatrix(SummarizedExperiment::assay(sce, assay))
58 58
     }
59 59
   }
Browse code

Fixed and merge conflict from compbiomed-devel

rz2333 authored on 01/05/2020 03:00:48
Showing1 changed files
... ...
@@ -7,29 +7,32 @@
7 7
 #' overridden at function call.
8 8
 #' @param sce \link[SingleCellExperiment]{SingleCellExperiment} R object to be
9 9
 #'  exported.
10
-#'  @param useAssay Character, default `"counts"`. The name of assay of
10
+#' @param useAssay Character. The name of assay of
11 11
 #' interests that will be set as the primary matrix of the output AnnData.
12
-#' @param outputDir Path to the directory where .h5ad outputs will be written
12
+#' Default \code{"counts"}. 
13
+#' @param outputDir Path to the directory where .h5ad outputs will be written. Default is the current working directory.
14
+#' @param prefix Prefix to use for the name of the output file. Default \code{"sample"}.
13 15
 #' @param overwrite Boolean. Default \code{TRUE}.
14
-#' @param compression Default \code{None}.If output file compression is required, this variable accepts
15
-#' 'gzip' or 'lzf' as inputs.
16
-#' @param compression_opts Integer. Default \code{NULL} Sets the compression level
16
+#' @param compression If output file compression is required, this variable accepts
17
+#' 'gzip' or 'lzf' as inputs. Default \code{None}.
18
+#' @param compressionOpts Integer. Sets the compression level
17 19
 #' @param forceDense Default \code{False} Write sparse data as a dense matrix.
18
-#' Refer anndata.write_h5ad documentation for details
20
+#' Refer \code{anndata.write_h5ad} documentation for details. Default \code{NULL}. 
19 21
 #' @examples
22
+#' \dontrun{
20 23
 #' data(sce_chcl, package = "scds")
21 24
 #' exportSCEtoAnnData(sce=sce_chcl, compression="gzip")
22
-#'
25
+#' }
26
+#' 
23 27
 #' @export
24
-
25 28
 exportSCEtoAnnData <- function(sce, 
26
-                                useAssay='counts',
27
-                                outputDir="./",
28
-                                sample = "sample",
29
-                                overwrite=TRUE,
30
-                                compression= c('None','lzf','gzip'),
29
+                                useAssay = 'counts',
30
+                                outputDir = "./",
31
+                                prefix = "sample",
32
+                                overwrite = TRUE,
33
+                                compression = c('None','lzf','gzip'),
31 34
                                 compressionOpts = NULL,
32
-                                forceDense= c('False','True')){
35
+                                forceDense = c('False','True')){
33 36
   compression <- match.arg(compression)
34 37
   forceDense <- match.arg(forceDense)
35 38
   if (compression == 'None'){
... ...
@@ -58,12 +61,12 @@ exportSCEtoAnnData <- function(sce,
58 61
   
59 62
   dir.create(outputDir, showWarnings = FALSE, recursive = TRUE)
60 63
   annData <- .sce2adata(sce,useAssay)
61
-  fileName <- paste0(sample,".h5ad")
64
+  fileName <- paste0(prefix,".h5ad")
62 65
   filePath <- file.path(outputDir,fileName)
63
-
66
+  
64 67
   if (file.exists(filePath) && !isTRUE(overwrite)) {
65 68
     stop(paste0(path, " already exists. Change 'outputDir' or set 'overwrite' to TRUE."))
66
-  }
69
+    }
67 70
 
68 71
   annData$write_h5ad(filePath,
69 72
                      compression = compression, 
Browse code

Fix exportSCEtoAnndata.R

rz2333 authored on 22/04/2020 17:34:30
Showing1 changed files
... ...
@@ -48,9 +48,13 @@ exportSCEtoAnnData <- function(sce,
48 48
             " correct Python environment.")
49 49
     return(sce)}
50 50
   
51
-  if (!is(SummarizedExperiment::assay(sce), 'dgCMatrix')) {
52
-    SummarizedExperiment::assay(sce) <- .convertToMatrix(SummarizedExperiment::assay(sce))
51
+  AssayName <- SummarizedExperiment::assayNames(sce)
52
+  for (assay in AssayName){
53
+    if (!is(SummarizedExperiment::assay(sce, assay), 'dgCMatrix')) {
54
+      SummarizedExperiment::assay(sce, assay) <- .convertToMatrix(SummarizedExperiment::assay(sce, assay))
55
+    }
53 56
   }
57
+
54 58
   
55 59
   dir.create(outputDir, showWarnings = FALSE, recursive = TRUE)
56 60
   annData <- .sce2adata(sce,useAssay)
Browse code

Fix directory not found issue in exportSCEtoAnndata.R

rz2333 authored on 21/04/2020 15:28:15
Showing1 changed files
... ...
@@ -51,14 +51,15 @@ exportSCEtoAnnData <- function(sce,
51 51
   if (!is(SummarizedExperiment::assay(sce), 'dgCMatrix')) {
52 52
     SummarizedExperiment::assay(sce) <- .convertToMatrix(SummarizedExperiment::assay(sce))
53 53
   }
54
-
54
+  
55
+  dir.create(outputDir, showWarnings = FALSE, recursive = TRUE)
55 56
   annData <- .sce2adata(sce,useAssay)
56 57
   fileName <- paste0(sample,".h5ad")
57 58
   filePath <- file.path(outputDir,fileName)
58
-  
59
+
59 60
   if (file.exists(filePath) && !isTRUE(overwrite)) {
60 61
     stop(paste0(path, " already exists. Change 'outputDir' or set 'overwrite' to TRUE."))
61
-    }
62
+  }
62 63
 
63 64
   annData$write_h5ad(filePath,
64 65
                      compression = compression, 
Browse code

Update exportSCEtoAnndata.R to support exporting dgTMatrix and DelayedArray

rz2333 authored on 20/04/2020 16:14:31
Showing1 changed files
... ...
@@ -48,6 +48,10 @@ exportSCEtoAnnData <- function(sce,
48 48
             " correct Python environment.")
49 49
     return(sce)}
50 50
   
51
+  if (!is(SummarizedExperiment::assay(sce), 'dgCMatrix')) {
52
+    SummarizedExperiment::assay(sce) <- .convertToMatrix(SummarizedExperiment::assay(sce))
53
+  }
54
+
51 55
   annData <- .sce2adata(sce,useAssay)
52 56
   fileName <- paste0(sample,".h5ad")
53 57
   filePath <- file.path(outputDir,fileName)
Browse code

Resolve conflict from upstream devel

rz2333 authored on 17/04/2020 18:43:57
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,63 @@
1
+#' @title Export a \link[SingleCellExperiment]{SingleCellExperiment} R object as 
2
+#' Python annData object 
3
+#' @description Writes all assays, colData, rowData, reducedDims, and altExps objects in a
4
+#' \link[SingleCellExperiment]{SingleCellExperiment} to a Python annData object in the .h5ad format
5
+#' All parameters of Anndata.write_h5ad function (https://icb-anndata.readthedocs-hosted.com/en/stable/anndata.AnnData.write_h5ad.html)
6
+#' are available as parameters to this export function and set to defaults. Defaults can be
7
+#' overridden at function call.
8
+#' @param sce \link[SingleCellExperiment]{SingleCellExperiment} R object to be
9
+#'  exported.
10
+#'  @param useAssay Character, default `"counts"`. The name of assay of
11
+#' interests that will be set as the primary matrix of the output AnnData.
12
+#' @param outputDir Path to the directory where .h5ad outputs will be written
13
+#' @param overwrite Boolean. Default \code{TRUE}.
14
+#' @param compression Default \code{None}.If output file compression is required, this variable accepts
15
+#' 'gzip' or 'lzf' as inputs.
16
+#' @param compression_opts Integer. Default \code{NULL} Sets the compression level
17
+#' @param forceDense Default \code{False} Write sparse data as a dense matrix.
18
+#' Refer anndata.write_h5ad documentation for details
19
+#' @examples
20
+#' data(sce_chcl, package = "scds")
21
+#' exportSCEtoAnnData(sce=sce_chcl, compression="gzip")
22
+#'
23
+#' @export
24
+
25
+exportSCEtoAnnData <- function(sce, 
26
+                                useAssay='counts',
27
+                                outputDir="./",
28
+                                sample = "sample",
29
+                                overwrite=TRUE,
30
+                                compression= c('None','lzf','gzip'),
31
+                                compressionOpts = NULL,
32
+                                forceDense= c('False','True')){
33
+  compression <- match.arg(compression)
34
+  forceDense <- match.arg(forceDense)
35
+  if (compression == 'None'){
36
+    compression <- NULL
37
+  }
38
+
39
+  if (!reticulate::py_module_available(module = "scanpy")) {
40
+    warning("Cannot find python module 'scanpy', please install Conda and",
41
+            " run sctkPythonInstallConda() or run sctkPythonInstallVirtualEnv().",
42
+            "If one of these have been previously run to install the modules,",
43
+            "make sure to run selectSCTKConda() or selectSCTKVirtualEnvironment(),",
44
+            " respectively, if R has been restarted since the module installation.",
45
+            " Alternatively, Scrublet can be installed on the local machine",
46
+            "with pip (e.g. pip install --user scanpy) and then the 'use_python()'",
47
+            " function from the 'reticulate' package can be used to select the",
48
+            " correct Python environment.")
49
+    return(sce)}
50
+  
51
+  annData <- .sce2adata(sce,useAssay)
52
+  fileName <- paste0(sample,".h5ad")
53
+  filePath <- file.path(outputDir,fileName)
54
+  
55
+  if (file.exists(filePath) && !isTRUE(overwrite)) {
56
+    stop(paste0(path, " already exists. Change 'outputDir' or set 'overwrite' to TRUE."))
57
+    }
58
+
59
+  annData$write_h5ad(filePath,
60
+                     compression = compression, 
61
+                     compression_opts = compressionOpts,
62
+                     force_dense = forceDense)
63
+}