Browse code

Fix exporting bugs

Yichen Wang authored on 13/04/2021 06:04:25
Showing 6 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,
... ...
@@ -10,7 +10,7 @@
10 10
 #' @param gzipped Boolean. \code{TRUE} if the output files are to be
11 11
 #'  gzip compressed. \code{FALSE} otherwise. Default
12 12
 #'  \code{TRUE}.
13
-#' @param sample Name of the sample. It will be used as the prefix of file names.
13
+#' @param prefix Prefix of file names.
14 14
 #' @return Generates text files containing data from \code{inSCE}.
15 15
 #' @examples
16 16
 #' data(sce_chcl, package = "scds")
... ...
@@ -23,14 +23,17 @@ exportSCEtoFlatFile <- function(sce,
23 23
                                 outputDir = "./",
24 24
                                 overwrite = TRUE,
25 25
                                 gzipped = TRUE,
26
-                                sample = 'sample') {
27
-
28
-  .writeAssays(sce, outputDir, overwrite, gzipped, sample)
29
-  .writeColData(sce, outputDir, overwrite, gzipped, sample)
30
-  .writeRowData(sce, outputDir, overwrite, gzipped, sample)
31
-  .writeMetaData(sce, outputDir, overwrite, sample)
32
-  .writeReducedDims(sce, outputDir, overwrite, gzipped, sample)
33
-  .writeAltExps(sce, outputDir, overwrite, gzipped, sample)
26
+                                prefix = 'SCE') {
27
+  path <- file.path(outputDir, prefix)
28
+  if (!file.exists(path)){
29
+    dir.create(path, showWarnings = FALSE, recursive = TRUE)
30
+  }
31
+  .writeAssays(sce, path, overwrite, gzipped, prefix)
32
+  .writeColData(sce, path, overwrite, gzipped, prefix)
33
+  .writeRowData(sce, path, overwrite, gzipped, prefix)
34
+  .writeMetaData(sce, path, overwrite, prefix)
35
+  .writeReducedDims(sce, path, overwrite, gzipped, prefix)
36
+  .writeAltExps(sce, path, overwrite, gzipped, prefix)
34 37
 
35 38
 }
36 39
 
... ...
@@ -54,7 +57,7 @@ exportSCEtoFlatFile <- function(sce,
54 57
   } else {
55 58
     filename <- paste0(path, ".txt")
56 59
   }
57
-  print(filename)
60
+  message(date(), " .. Writing '", filename)
58 61
   .checkOverwrite(filename, overwrite)
59 62
   data.table::fwrite(x = data, file = filename, nThread = 1, row.names = FALSE)
60 63
 }
... ...
@@ -71,16 +74,18 @@ exportSCEtoFlatFile <- function(sce,
71 74
       assayNames <- paste0("assay", seq(SummarizedExperiment::assays(sce)))
72 75
     }
73 76
     for (i in seq_along(SummarizedExperiment::assays(sce))) {
74
-      message(date(), " .. Writing assay '", assayNames[i], "'")
77
+
75 78
       filename <- paste(sample, paste0(assayNames[i], ".mtx"), sep="_")
76 79
       assaypath <- file.path(assaysFolder, filename)
77 80
 
78 81
       .checkOverwrite(assaypath, overwrite)
79 82
       mat <- .convertToMatrix(SummarizedExperiment::assays(sce)[[i]])
83
+      message(date(), " .. Writing assay '", assayNames[i], "' to ", assaypath)
80 84
       out <- Matrix::writeMM(mat, assaypath)
81 85
 
82 86
       if(isTRUE(gzipped)) {
83 87
         .checkOverwrite(paste0(assaypath, ".gz"), overwrite)
88
+        message(date(), " .. Compressing into ", paste0(assaypath, ".gz"))
84 89
         R.utils::gzip(filename = assaypath, overwrite = overwrite)
85 90
       }
86 91
     }
... ...
@@ -99,6 +104,7 @@ exportSCEtoFlatFile <- function(sce,
99 104
     for (i in altExpNames) {
100 105
       sceAltExp <- SingleCellExperiment::altExp(sce, i, withColData = FALSE)
101 106
       altExpPath <- file.path(path, i)
107
+
102 108
       message(date(), " .. Writing altExp '", i, "'")
103 109
 
104 110
       assaysFolder <- file.path(altExpPath, "/assays")
... ...
@@ -126,7 +132,6 @@ exportSCEtoFlatFile <- function(sce,
126 132
   if(ncol(rowData(sce)) > 0) {
127 133
     data <- SummarizedExperiment::rowData(sce)
128 134
     rowDataPath <-  file.path(path, paste(sample, "rowData", sep="_"))
129
-    print(rowDataPath)
130 135
     .writeSCEFile(data, rowDataPath, overwrite, gzipped)
131 136
   }
132 137
 }
... ...
@@ -141,9 +146,9 @@ exportSCEtoFlatFile <- function(sce,
141 146
     if (length(reducedDimNames(sce)) > 0) {
142 147
       reducedDimNames <- SingleCellExperiment::reducedDimNames(sce)
143 148
       for (i in reducedDimNames) {
144
-        message(date(), " .. Writing reducedDim '", i, "'")
145 149
         data <- SingleCellExperiment::reducedDim(sce, i, withDimnames = TRUE)
146 150
         reducedDimNamePath <- file.path(reducedDimsFolder, paste(sample, i, sep="_"))
151
+        message(date(), " .. Writing reducedDim '", i, "' to", reducedDimNamePath)
147 152
         .writeSCEFile(data, reducedDimNamePath, overwrite, gzipped)
148 153
       }
149 154
     }
... ...
@@ -158,6 +163,7 @@ exportSCEtoFlatFile <- function(sce,
158 163
 
159 164
     filename <- file.path(metadataFolder, paste(sample, "metadata.rds", sep="_"))
160 165
     .checkOverwrite(filename, overwrite)
166
+    message(date(), " .. Writing metadata to ", filename)
161 167
     saveRDS(object = S4Vectors::metadata(sce), file = filename)
162 168
   }
163 169
 }
... ...
@@ -924,10 +924,10 @@ shinyServer(function(input, output, session) {
924 924
 
925 925
   updateSeuratUIFromRDS <- function(inSCE){
926 926
     if(!is.null(metadata(inSCE)$seurat$plots)){
927
-      showNotification(HTML("Computation from Seurat Report detected in the input object, therefore the toolkit will now populate the Seurat tab with computated data & plots for further inspection. Click on the button below to directly go the the Seurat tab of the toolkit now! <br><br>"), 
927
+      showNotification(HTML("Computation from Seurat Report detected in the input object, therefore the toolkit will now populate the Seurat tab with computated data & plots for further inspection. Click on the button below to directly go the the Seurat tab of the toolkit now! <br><br>"),
928 928
                        type = "message", duration = 0, action = actionBttn(
929 929
         inputId = "goToSeurat",
930
-        label = "Go to Seurat", 
930
+        label = "Go to Seurat",
931 931
         style = "bordered",
932 932
         color = "royal",
933 933
         size = "s",
... ...
@@ -942,7 +942,7 @@ shinyServer(function(input, output, session) {
942 942
       }
943 943
     }
944 944
   }
945
-  
945
+
946 946
   observeEvent(input$goToSeurat,{
947 947
     updateTabsetPanel(session, "navbar",
948 948
                       selected = "Seurat")
... ...
@@ -8013,6 +8013,27 @@ shinyServer(function(input, output, session) {
8013 8013
     }
8014 8014
   )
8015 8015
 
8016
+  output$exportFileName <- renderUI({
8017
+    defaultName <- paste0("SCE-", strftime(Sys.time(), format = "%y%m%d_%H%M"))
8018
+    if (input$exportChoice == "rds") {
8019
+      extName <- ".rds"
8020
+    } else if (input$exportChoice == "annData") {
8021
+      extName <- ".h5ad"
8022
+    } else if (input$exportChoice == "textfile") {
8023
+      extName <- ".txt"
8024
+    }
8025
+    tags$div(
8026
+      div(style = "display: inline-block;vertical-align:top; width: 160px;",
8027
+          textInput("exportPrefix", label = NULL,
8028
+                    value = defaultName, placeholder = "Required!",
8029
+                    width = '160px')),
8030
+      div(
8031
+        style = "display: inline-block;vertical-align:top; width: 50px;",
8032
+        p(extName, style = "margin-top: 8px; margin-left: 2px; font-size: 16px;")
8033
+      )
8034
+    )
8035
+  })
8036
+
8016 8037
   addPopover(session, 'exportAssayLabel', '', "The name of assay of interests that will be set as the primary matrix of the output AnnData.", 'right')
8017 8038
   addPopover(session, 'compressionLabel', '', "If output file compression is required, this variable accepts 'gzip' or 'lzf' as inputs", 'right')
8018 8039
   addPopover(session, 'compressionOptsLabel', '', "Sets the compression level", 'right')
... ...
@@ -8025,39 +8046,26 @@ shinyServer(function(input, output, session) {
8025 8046
     withBusyIndicatorServer("exportData", {
8026 8047
       if (is.null(vals$counts) && is.null(vals$original)) {
8027 8048
         shinyalert::shinyalert("Error!", "Upload data first.", type = "error")
8028
-        return
8029
-      }
8030
-
8031
-      if (input$exportChoice == "rds") {
8032
-        filename = paste0("SCE_", strftime(Sys.time(), format = "%y%m%d_%H%m"),
8033
-                         ".rds")
8034
-        saveRDS(vals$counts, paste0(exportPath, "/", filename))
8035
-      } else if (input$exportChoice == "annData") {
8036
-        exportassay <- input$exportAssay
8037
-        compression <- input$compression
8038
-        compressionOpts = input$compressionOpts
8039
-        forceDense <- input$forceDense
8040
-        overwrite <- if(input$overwrite == 'True') TRUE else FALSE
8041
-        exportSCEtoAnnData(sce=vals$counts,
8042
-                           useAssay = exportassay,
8043
-                           outputDir=exportPath,
8044
-                           prefix = paste0("SCE-",
8045
-                                           strftime(Sys.time(),
8046
-                                                    format = "%y%m%d_%H%m")),
8047
-                           overwrite=overwrite,
8048
-                           compression = compression,
8049
-                           compressionOpts = compressionOpts,
8050
-                           forceDense = forceDense)
8051
-      } else if (input$exportChoice == "textfile") {
8052
-        overwrite <- if(input$overwrite == 'True') TRUE else FALSE
8053
-        gzipped <- if(input$gzip == 'True') TRUE else FALSE
8054
-        exportSCEtoFlatFile(sce = vals$counts,
8055
-                            outputDir=exportPath,
8056
-                            overwrite=overwrite,
8057
-                            gzipped=gzipped,
8058
-                            sample = paste0("SCE-",
8059
-                                            strftime(Sys.time(),
8060
-                                                     format = "%y%m%d_%H%m")))
8049
+      } else {
8050
+        if (input$exportChoice == "rds") {
8051
+          filename <- paste0(input$exportPrefix, ".rds")
8052
+          saveRDS(vals$counts, paste0(exportPath, "/", filename))
8053
+        } else if (input$exportChoice == "annData") {
8054
+          exportSCEtoAnnData(sce=vals$counts,
8055
+                             useAssay = input$exportAssay,
8056
+                             outputDir = exportPath,
8057
+                             prefix = input$exportPrefix,
8058
+                             overwrite = input$exportOverwrite,
8059
+                             compression = "gzip",
8060
+                             compressionOpts = input$compressionOpts,
8061
+                             forceDense = input$forceDense)
8062
+        } else if (input$exportChoice == "textfile") {
8063
+          exportSCEtoFlatFile(sce = vals$counts,
8064
+                              outputDir = exportPath,
8065
+                              overwrite = input$exportOverwrite,
8066
+                              gzipped = input$exportFlatGzip,
8067
+                              prefix = input$exportPrefix)
8068
+        }
8061 8069
       }
8062 8070
     })
8063 8071
   })
... ...
@@ -23,6 +23,8 @@ shinyPanelExport <- fluidPage(
23 23
           "Flat text files" = "textfile"
24 24
         )
25 25
       ),
26
+      tags$label(id="exportFileNameLabel", "File Name"),
27
+      uiOutput("exportFileName"),
26 28
       actionButton("exportData", "Download")
27 29
     ),
28 30
     column(
... ...
@@ -30,25 +32,22 @@ shinyPanelExport <- fluidPage(
30 32
       conditionalPanel(
31 33
         condition = "input.exportChoice === 'textfile'",
32 34
         tags$h5(style = "font-weight: bold; margin-bottom: 15px", "Set export specifications"),
33
-        tags$label(id="gzipLabel", "Gzip"),
34
-        selectInput("gzip", label=NULL, c("True", "False"), width = '140px')
35
+        checkboxInput("exportFlatGzip", "Gzip Compress", value = TRUE)
35 36
       ),
36 37
       conditionalPanel(
37 38
         condition = "input.exportChoice === 'annData'",
38 39
         tags$h5(style = "font-weight: bold; margin-bottom: 15px", "Set export specifications"),
39 40
         tags$label(id="exportAssayLabel", "Assay"),
40
-        selectInput("exportAssay", label=NULL, c(""), width='140px'),
41
+        uiOutput("exportAssay"),
41 42
         tags$label(id="compressionLabel", "Compression"),
42
-        selectInput("compression", label = NULL, c("None", "lzf", "gzip"), width='140px'),
43
+        # selectInput("compression", label = NULL, c("None", "lzf", "gzip"), width='140px'),
43 44
         tags$label(id="compressionOptsLabel", "Compression Opts"),
44
-        numericInput("compressionOpts", label = NULL, 0, min = 1, max = 100, width='140px'),
45
-        tags$label(id="forceDenseLabel", "Force Dense"),
46
-        selectInput("forceDense", label = NULL, c("False", "True"), width='140px'),
45
+        numericInput("compressionOpts", label = NULL, 1, min = 1, max = 100, width='140px'),
46
+        checkboxInput("forceDense", "Force Dense", value = FALSE)
47 47
       ),
48 48
       conditionalPanel(
49 49
         condition = "input.exportChoice === 'textfile' || input.exportChoice === 'annData'",
50
-        tags$label(id="overwriteLabel", "Overwrite"),
51
-        selectInput("overwrite", label = NULL,c("True", "False"), width = '140px'),
50
+        checkboxInput("exportOverwrite", "Overwrite", value = TRUE)
52 51
       )
53 52
     )
54 53
     )
... ...
@@ -11,9 +11,9 @@ exportSCEtoAnnData(
11 11
   outputDir = "./",
12 12
   prefix = "sample",
13 13
   overwrite = TRUE,
14
-  compression = c("None", "lzf", "gzip"),
14
+  compression = c("gzip", "lzf", "None"),
15 15
   compressionOpts = NULL,
16
-  forceDense = c("False", "True")
16
+  forceDense = FALSE
17 17
 )
18 18
 }
19 19
 \arguments{
... ...
@@ -31,7 +31,7 @@ Default \code{"counts"}.}
31 31
 \item{overwrite}{Boolean. Default \code{TRUE}.}
32 32
 
33 33
 \item{compression}{If output file compression is required, this variable accepts
34
-'gzip' or 'lzf' as inputs. Default \code{None}.}
34
+'gzip', 'lzf' or "None" as inputs. Default \code{gzip"}.}
35 35
 
36 36
 \item{compressionOpts}{Integer. Sets the compression level}
37 37
 
... ...
@@ -9,7 +9,7 @@ exportSCEtoFlatFile(
9 9
   outputDir = "./",
10 10
   overwrite = TRUE,
11 11
   gzipped = TRUE,
12
-  sample = "sample"
12
+  prefix = "SCE"
13 13
 )
14 14
 }
15 15
 \arguments{
... ...
@@ -25,7 +25,7 @@ exported.}
25 25
 gzip compressed. \code{FALSE} otherwise. Default
26 26
 \code{TRUE}.}
27 27
 
28
-\item{sample}{Name of the sample. It will be used as the prefix of file names.}
28
+\item{prefix}{Prefix of file names.}
29 29
 }
30 30
 \value{
31 31
 Generates text files containing data from \code{inSCE}.