Browse code

Update exportSCEtoAnndata.R to support exporting dgTMatrix and DelayedArray

rz2333 authored on 20/04/2020 16:14:31
Showing 3 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)
... ...
@@ -269,11 +269,23 @@ distinctColors <- function(n, hues = c("red", "cyan", "orange", "blue",
269 269
 .convertToMatrix <- function(x) {
270 270
   cn <- colnames(x)
271 271
   rn <- rownames(x)
272
+  limit <- (2^32/2-1)
273
+  dimN <- dim(x)
274
+  chuS <- floor(floor(limit/dimN[1]))
275
+  chuN <- ceiling(dimN[2]/chuS)
276
+  Mat <- list()
272 277
   
273
-  x <- methods::as(x, "dgCMatrix")
278
+  for (i in 1:chuN) {
279
+    startT <- Sys.time()
280
+    start <- (i-1)*chuS + 1
281
+    end <- min(i*chuS, dimN[2])
282
+    Mat[[i]] <- methods::as(x[, start:end], "Matrix")
283
+    endT <- Sys.time()
284
+    print(endT - startT)
285
+  }
286
+  x <- do.call(base::cbind, Mat)
274 287
   colnames(x) <- cn
275 288
   rownames(x) <- rn
276
-  
277 289
   return(x)
278 290
 }
279 291
 
... ...
@@ -214,6 +214,10 @@ if (!is.null(mergedDropletSCE)) {
214 214
     ## Export to flatfile
215 215
     fn <- file.path(directory, samplename, "FlatFile", "Droplets")
216 216
     exportSCEtoFlatFile(mergedDropletSCE, outputDir = fn)
217
+
218
+    ## Export to Python AnnData
219
+    fn <- file.path(directory, samplename, "Python", "Droplets")
220
+    exportSCEtoAnnData(mergedDropletSCE, outputDir=fn, compression='gzip', sample=samplename)
217 221
 }
218 222
 if (!is.null(mergedFilteredSCE)) {
219 223
     ## Export to R        
... ...
@@ -223,4 +227,9 @@ if (!is.null(mergedFilteredSCE)) {
223 227
     ## Export to flatfile    
224 228
     fn <- file.path(directory, samplename, "FlatFile", "FilteredCells")
225 229
     exportSCEtoFlatFile(mergedFilteredSCE, outputDir = fn)
230
+
231
+    ## Export to Python AnnData
232
+    fn <- file.path(directory, samplename, "Python", "FilteredCells")
233
+    exportSCEtoAnnData(mergedFilteredSCE, outputDir=fn, compression='gzip', sample=samplename)
234
+
226 235
 }    
227 236
\ No newline at end of file