diff --git a/R/BulkProjection.R b/R/BulkProjection.R index 882dd9a4..c60f4e10 100644 --- a/R/BulkProjection.R +++ b/R/BulkProjection.R @@ -81,7 +81,7 @@ projectBulkATAC <- function( # Create Bulk Matrix ################################################## bulkMat <- .safeSubset( - mat = .getAssay(subATAC, "counts"), + mat = as.matrix(.getAssay(subATAC, "counts")), subsetRows = paste0("f", seq_along(rDGR)) ) .logThis(bulkMat, "bulkATACMat", logFile = logFile) @@ -145,13 +145,15 @@ projectBulkATAC <- function( if(!is.null(corCutOff)){ if(scaleDims){ corToDepth <- rD$corToDepth$scaled - dimsToUse <- dimsToUse[corToDepth < corCutOff] + dimsToUse <- dimsToUse[intersect(dimsToUse, which(corToDepth < corCutOff))] }else{ corToDepth <- rD$corToDepth$none - dimsToUse <- dimsToUse[corToDepth < corCutOff] + dimsToUse <- dimsToUse[intersect(dimsToUse, which(corToDepth < corCutOff))] } } + simRD <- simRD[, dimsToUse, drop = FALSE] + if(embedding$params$nc != ncol(simRD)){ .logMessage("Error! Inconsistency found with matching LSI dimensions to those used in addUMAP or addTSNE", "\nReturning with simulated reduced dimension coordinates...", verbose = TRUE, logFile = logFile) @@ -161,8 +163,6 @@ projectBulkATAC <- function( return(out) } - simRD <- simRD[, dimsToUse, drop = FALSE] - } ################################################## diff --git a/R/Embedding.R b/R/Embedding.R index c71026f4..f32930b5 100644 --- a/R/Embedding.R +++ b/R/Embedding.R @@ -170,7 +170,7 @@ addUMAP <- function( ArchRProj@embeddings[[name]] <- SimpleList( df = dfEmbedding, - params = c( + params = list( embeddingParams, dimsToUse = dimsToUse, scaleDims = scaleDims, @@ -189,7 +189,7 @@ addUMAP <- function( embeddingParams$X <- NULL ArchRProj@embeddings[[name]] <- SimpleList( df = dfEmbedding, - params = c( + params = list( embeddingParams, dimsToUse = dimsToUse, scaleDims = scaleDims, diff --git a/R/HiddenUtils.R b/R/HiddenUtils.R index 57fdad35..f5129b98 100644 --- a/R/HiddenUtils.R +++ b/R/HiddenUtils.R @@ -188,8 +188,18 @@ idxNotIn <- which(subsetRows %ni% rownames(mat)) if(length(idxNotIn) > 0){ subsetNamesNotIn <- subsetRows[idxNotIn] - matNotIn <- Matrix::sparseMatrix(i=1,j=1,x=0,dims=c(length(idxNotIn), ncol = ncol(mat))) + + #check matrix class and make a matching empty matrix for the idxNotIn rows + if (class(mat)[[1]] == "dgCMatrix") { + matNotIn <- Matrix::sparseMatrix(i = 1, j = 1, x = 0, dims = c(length(idxNotIn), ncol = ncol(mat))) + } else if (class(mat)[[1]] == "matrix") { + matNotIn <- matrix(nrow = length(idxNotIn), ncol = ncol(mat), 0) + } else{ + stop("Error! Argument 'mat' in .safeSubset is not either of class matrix or class dgCMatrix!") + } + rownames(matNotIn) <- subsetNamesNotIn + colnames(matNotIn) <- colnames(mat) mat <- rbind(mat, matNotIn) } mat <- mat[subsetRows,] @@ -199,7 +209,16 @@ idxNotIn <- which(subsetCols %ni% colnames(mat)) if(length(idxNotIn) > 0){ subsetNamesNotIn <- subsetCols[idxNotIn] - matNotIn <- Matrix::sparseMatrix(i=1,j=1,x=0,dims=c(nrow(mat), ncol = length(idxNotIn))) + + #check matrix class and make a matching empty matrix for the idxNotIn rows + if (class(mat)[[1]] == "dgCMatrix") { + matNotIn <- Matrix::sparseMatrix(i = 1, j = 1, x = 0, dims = c(nrow(mat), ncol = length(idxNotIn))) + } else if (class(mat)[[1]] == "matrix") { + matNotIn <- matrix(nrow = nrow(mat), ncol = length(idxNotIn), 0) + } else{ + stop("Error! Argument 'mat' in .safeSubset is not either of class matrix or class dgCMatrix!") + } + rownames(matNotIn) <- rownames(mat) colnames(matNotIn) <- subsetNamesNotIn mat <- cbind(mat, matNotIn) }