R/importMultipleSources.R
31390a70
 
 #' Imports samples from different sources and compiles them into a list of SCE objects
 #' @param allImportEntries object containing the sources and parameters of all the samples being imported (from the UI)
ffbb2af1
 #' @param delayedArray Boolean. Whether to read the expression matrix as
969718f9
 #'  \link{DelayedArray} object or not. Default \code{FALSE}.
31390a70
 #' @return A list of \link[SingleCellExperiment]{SingleCellExperiment} object containing
 #' the droplet or cell data or both,depending on the dataType that users provided.
 #' @export
ac173765
 importMultipleSources <- function(allImportEntries, delayedArray = FALSE) {
31390a70
   sceObjs <- list()
   for (entry in allImportEntries$samples) {
     if (entry$type == "cellRanger2") {
       if (is.null(entry$params$cellRangerDirs)) {
         newSce <- importCellRangerV2Sample(
           dataDir = entry$params$dataDir,
           sampleName = entry$params$sampleName,
ac173765
           delayedArray = delayedArray
31390a70
         )
       } else {
         newSce <- importCellRangerV2(
           cellRangerDirs = entry$params$cellRangerDirs,
           sampleDirs = entry$params$sampleDirs,
           sampleNames = entry$params$sampleNames,
a765c34b
           delayedArray = delayedArray,
           reference = entry$params$reference
31390a70
         )
       }
ea2808fc
 
31390a70
     } else if (entry$type == "cellRanger3") {
       if (is.null(entry$params$cellRangerDirs)) {
         newSce <- importCellRangerV3Sample(
           dataDir = entry$params$dataDir,
           sampleName = entry$params$sampleName,
ac173765
           delayedArray = delayedArray
31390a70
         )
       } else {
         newSce <- importCellRangerV3(
           cellRangerDirs = entry$params$cellRangerDirs,
           sampleDirs = entry$params$sampleDirs,
           sampleNames = entry$params$sampleNames,
ac173765
           delayedArray = delayedArray
31390a70
         )
       }
     } else if (entry$type == "starSolo") {
       newSce <- importSTARsolo(
         STARsoloDirs = entry$params$STARsoloDirs,
976db869
         samples = entry$params$samples,
ac173765
         delayedArray = delayedArray
31390a70
       )
     } else if (entry$type == "busTools") {
       newSce <- importBUStools(
         BUStoolsDirs = entry$params$BUStoolsDirs,
         samples = entry$params$samples,
ac173765
         delayedArray = delayedArray
31390a70
       )
     } else if (entry$type == "seqc") {
       newSce <- importSEQC(
         seqcDirs = entry$params$seqcDirs,
         samples = entry$params$samples,
         prefix = entry$params$prefix,
ac173765
         delayedArray = delayedArray
31390a70
       )
     } else if (entry$type == "optimus") {
       newSce <- importOptimus(
         OptimusDirs = entry$params$OptimusDirs,
ac173765
         samples = entry$params$samples,
         delayedArray = delayedArray
31390a70
       )
     } else if (entry$type == "files") {
       newSce <- importFromFiles(assayFile = entry$params$assayFile,
                                 annotFile = entry$params$annotFile,
                                 featureFile = entry$params$featureFile,
ac173765
                                 assayName = entry$params$assayName,
                                 delayedArray = delayedArray)
31390a70
     } else if (entry$type == "example") {
ac173765
       newSce <- importExampleData(dataset = entry$params$dataset,
                                   delayedArray = delayedArray)
31390a70
     } else if (entry$type == "rds") {
       importedrds <- readRDS(entry$params$rdsFile)
       if (base::inherits(importedrds, "SummarizedExperiment")) {
         newSce <- importedrds
       } else if (base::inherits(importedrds, "Seurat")) {
         newSce <- convertSeuratToSCE(importedrds)
       } else {
         stop("The '.rds' file should contain a 'SingleCellExperiment' or 'Seurat' object.")
       }
ac173765
 
       for(assay in SummarizedExperiment::assayNames(newSce)) {
cc7d9f64
         if(!base::inherits(SummarizedExperiment::assay(newSce, assay), "dgCMatrix") && !isTRUE(delayedArray)) {
           SummarizedExperiment::assay(newSce, assay) <- .convertToMatrix(SummarizedExperiment::assay(newSce, assay))
ac173765
         }
 
cc7d9f64
         if(!base::inherits(SummarizedExperiment::assay(newSce, assay), "DelayedArray") && isTRUE(delayedArray)) {
           SummarizedExperiment::assay(newSce, assay) <- DelayedArray::DelayedArray(SummarizedExperiment::assay(newSce, assay))
ac173765
         }
       }
ea2808fc
 
31390a70
     }
976db869
 
5b1a0160
     # Begin Set Tags
     if(entry$type %in% c("cellRanger2", "cellRanger3", "starSolo", "busTools", "seqc", "optimus", "example")){
       newSce <- expSetDataTag(
         inSCE = newSce,
         assayType = "raw",
208bb946
         assays = SummarizedExperiment::assayNames(newSce))
5b1a0160
     }
     else if(entry$type %in% c("rds", "files")){
       # Check if tags already stored in uploaded rds/files
3077c99e
       if(is.null(S4Vectors::metadata(newSce)$assayType)){
5b1a0160
         try({
           counts(newSce)
           newSce <- expSetDataTag(
             inSCE = newSce,
             assayType = "raw",
             assays = "counts")
         }, silent = TRUE)
976db869
 
5b1a0160
         try({
           logcounts(newSce)
           newSce <- expSetDataTag(
             inSCE = newSce,
             assayType = "transformed",
             assays = "logcounts")
         }, silent = TRUE)
976db869
 
5b1a0160
         try({
           normcounts(newSce)
           newSce <- expSetDataTag(
             inSCE = newSce,
             assayType = "transformed",
             assays = "normcounts")
         }, silent = TRUE)
976db869
 
5b1a0160
         try({
208bb946
           celda::decontXcounts(newSce)
5b1a0160
           newSce <- expSetDataTag(
             inSCE = newSce,
             assayType = "raw",
             assays = "decontXcounts")
         }, silent = TRUE)
976db869
 
208bb946
         untaggedAssays <- SummarizedExperiment::assayNames(newSce)
5b1a0160
         untaggedAssays <- untaggedAssays[! untaggedAssays %in% c('counts', 'logcounts', 'normcounts', 'decontX')]
976db869
 
5b1a0160
         newSce <- expSetDataTag(
           inSCE = newSce,
           assayType = "uncategorized",
976db869
           assays = untaggedAssays)
5b1a0160
       }
       # End Set Tags
     }
976db869
 
31390a70
     sceObjs = c(sceObjs, list(newSce))
   }
976db869
 
31390a70
   return(combineSCE(sceList = sceObjs,
ac173765
                     by.r = Reduce(base::intersect, lapply(sceObjs, function(x) { colnames(rowData(x))})),
                     by.c = Reduce(base::intersect, lapply(sceObjs, function(x) { colnames(colData(x))})),
9cafccf8
                     combined = TRUE)
31390a70
   )
 }