From d75a2b18801aaef8a1d4eca8f1f5d98928f15aa2 Mon Sep 17 00:00:00 2001 From: skpalan Date: Thu, 2 Mar 2023 13:56:42 -0500 Subject: [PATCH] fix matirx pkg import issue --- DESCRIPTION | 5 +++-- R/analysis.r | 30 +++++++++++++++++------------- R/objects.r | 2 +- R/plotting.r | 14 +++++++------- R/utility.r | 6 +++--- man/imputeNiche.CytoSignal.Rd | 2 +- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7b352fb..aae2c7d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,10 +14,11 @@ Depends: methods, Rcpp, RcppArmadillo, - stats + stats, + ggplot2, + cowplot Imports: dbscan, - ggplot2, ggsci, paletteer, RANN, diff --git a/R/analysis.r b/R/analysis.r index 648e3ef..7d68e9b 100644 --- a/R/analysis.r +++ b/R/analysis.r @@ -361,7 +361,7 @@ imputeNiche.dgCMatrix <- function( x.list = rep(1, length(i.list)) do.norm <- T } else if (weights == "counts") { # use total counts in each bead as weights - x.list = colSums(dge.raw)[i.list] + x.list = Matrix::colSums(dge.raw)[i.list] do.norm <- T } else if (weights == "dist") { x.list = exp(-as.numeric(names(nb.dist.fac))) # exp(-dist) as similarities @@ -378,7 +378,7 @@ imputeNiche.dgCMatrix <- function( # re-weight the weight mtx across each niche (column) if (do.norm) { - weights.mtx@x = weights.mtx@x / rep.int(colSums(weights.mtx), diff(weights.mtx@p)) + weights.mtx@x = weights.mtx@x / rep.int(Matrix::colSums(weights.mtx), diff(weights.mtx@p)) } dge.raw.imputed = dge.raw %*% weights.mtx @@ -397,7 +397,7 @@ imputeNiche.dgCMatrix <- function( #' @export imputeNiche.CytoSignal <- function(object, nn.type = NULL, - weights = c("mean", "counts", "dist", "gaussian"), + weights = c("mean", "counts", "dist", "none"), save.obj = TRUE ){ dge.raw <- object@counts @@ -407,6 +407,10 @@ imputeNiche.CytoSignal <- function(object, nn.type <- object@imputation[["default"]] } + if (!weights %in% c("mean", "counts", "dist", "none")){ + stop("Incorret weighting method!\n") + } + message(paste0("Imputing using ", nn.type, "...")) if (nn.type %in% names(object@imputation)){ @@ -470,10 +474,10 @@ normCounts.dgCMatrix <- function( method = c("default", "cpm") ){ if (method == "default"){ - mat@x <- mat@x / rep.int(colSums(mat), diff(mat@p)) + mat@x <- mat@x / rep.int(Matrix::colSums(mat), diff(mat@p)) mat@x <- log1p(mat@x * 1e4) } else if (method == "cpm"){ - mat@x <- mat@x / rep.int(colSums(mat), diff(mat@p)) + mat@x <- mat@x / rep.int(Matrix::colSums(mat), diff(mat@p)) mat@x <- log1p(mat@x * 1e6) } else { stop("Method not found.") @@ -538,10 +542,10 @@ normCounts.CytoSignal <- function( # mat_list_norm <- lapply(object@imputation[[slot.use]], function(mat) { # if (method == "default") { -# mat@x = mat@x / rep.int(colSums(mat), diff(mat@p)) +# mat@x = mat@x / rep.int(Matrix::colSums(mat), diff(mat@p)) # mat@x = lop1p(mat@x * 1e4) # } else if (method == "cpm") { -# mat@x = mat@x / rep.int(colSums(mat), diff(mat@p)) +# mat@x = mat@x / rep.int(Matrix::colSums(mat), diff(mat@p)) # mat@x = lop1p(mat@x * 1e6) # } else { # stop("Method not found.") @@ -676,7 +680,7 @@ changeUniprot.CytoSignal <- function( dge.raw <- object@imputation[[slot.use]]@imp.norm.data # check duplicate items exists in database - unpt.list <- changeUniprot.dgCMatrix(dge.raw, gene_to_uniprot, verbose = verbose) + unpt.list <- changeUniprot.matrix_like(dge.raw, gene_to_uniprot, verbose = verbose) object@imputation[[slot.use]]@intr.data <- unpt.list[[1]] object@intr.valid[["symbols"]][[slot.use]] <- unpt.list[[2]] @@ -918,7 +922,7 @@ permuteImpLR <- function( } null.dge.eps = normCounts.dgCMatrix(null.dge.eps, "default"); gc() - null.dge.eps.unpt = changeUniprot.dgCMatrix(null.dge.eps, object@intr.valid[["gene_to_uniprot"]])[[1]] + null.dge.eps.unpt = changeUniprot.matrix_like(null.dge.eps, object@intr.valid[["gene_to_uniprot"]])[[1]] rm(null.dge.eps, null.dge.raw); gc() return(null.dge.eps.unpt) @@ -972,9 +976,9 @@ permuteScoreLR <- function( null.lrscore.mtx = graphNicheLR.dgCMatrix(null.dge.eps.unpt, null.dge.dt.unpt, null.nb.fac[["id"]], intr.valid[["ligands"]], intr.valid[["receptors"]]); gc() - if (sum(colSums(null.lrscore.mtx) == 0) != 0){ - message("A total of ", sum(colSums(null.lrscore.mtx) == 0), " intr are empty in NULL scores.") - null.lrscore.mtx = null.lrscore.mtx[, !colSums(null.lrscore.mtx) == 0] + if (sum(Matrix::colSums(null.lrscore.mtx) == 0) != 0){ + message("A total of ", sum(Matrix::colSums(null.lrscore.mtx) == 0), " intr are empty in NULL scores.") + null.lrscore.mtx = null.lrscore.mtx[, !Matrix::colSums(null.lrscore.mtx) == 0] } intr.both <- intersect(colnames(null.lrscore.mtx), colnames(object@lrscore[[slot.use]]@score)) @@ -1197,7 +1201,7 @@ runPears.std <- function( lr.mtx <- object@lrscore[[score.slot]]@score[, intr.hq] lt.mtx.imp <- dataImpKNN(lr.mtx, object@cells.loc, k = k, weight = weight) - lt.mtx.imp <- as.matrix(t(lt.mtx.imp)) + lt.mtx.imp <- as.matrix(Matrix::t(lt.mtx.imp)) intr.order <- as.double(pearson_col_cpp(lt.mtx.imp, lr.mtx)) * as.double(stdMat_cpp(lt.mtx.imp)) / as.double(stdMat_cpp(lr.mtx)) names(intr.order) <- colnames(lr.mtx) diff --git a/R/objects.r b/R/objects.r index d1e3b00..92e7b56 100644 --- a/R/objects.r +++ b/R/objects.r @@ -451,7 +451,7 @@ removeLowQuality <- function(object, counts.thresh = 300, gene.thresh = 50) { cells.loc <- object@cells.loc del.genes = filterGene(dge.raw.filter, object@intr.valid[["gene_to_uniprot"]], gene.thresh) - del.cells = which(colSums(dge.raw.filter) < counts.thresh) + del.cells = which(Matrix::colSums(dge.raw.filter) < counts.thresh) # Removed 13017 / 33611 low quality cells. if (length(del.cells) == 0 & length(del.genes) != 0){ diff --git a/R/plotting.r b/R/plotting.r index 4697313..51f4d21 100644 --- a/R/plotting.r +++ b/R/plotting.r @@ -49,7 +49,7 @@ plotSignif <- function(object, num.plot, res_dir, plot.details = T, slot.use = N dge.recep <- object@imputation[[recep.slot]]@intr.data null.dge.gau <- object@imputation[[lig.slot]]@intr.data.null null.dge.dt <- object@imputation[[recep.slot]]@intr.data.null - dge.raw <- changeUniprot.dgCMatrix(object@counts, object@intr.valid[["gene_to_uniprot"]])[[1]] + dge.raw <- changeUniprot.matrix_like(object@counts, object@intr.valid[["gene_to_uniprot"]])[[1]] clusters <- object@clusters score.mtx <- score.obj@score @@ -107,12 +107,12 @@ plotSignif <- function(object, num.plot, res_dir, plot.details = T, slot.use = N receps.ori = dge.raw[names(intr.db[[3]][intr.db[[3]] == res.intr.list[i]]), ] receps.null = null.dge.dt[names(intr.db[[3]][intr.db[[3]] == res.intr.list[i]]), ] - if (!is.null(nrow(ligands))){ligands = colSums(ligands)} - if (!is.null(nrow(receps))){receps = colSums(receps)} - if (!is.null(nrow(ligands.ori))){ligands.ori = colSums(ligands.ori)} - if (!is.null(nrow(ligands.null))){ligands.null = colSums(ligands.null)} - if (!is.null(nrow(receps.ori))){receps.ori = colSums(receps.ori)} - if (!is.null(nrow(receps.null))){receps.null = colSums(receps.null)} + if (!is.null(nrow(ligands))){ligands = Matrix::colSums(ligands)} + if (!is.null(nrow(receps))){receps = Matrix::colSums(receps)} + if (!is.null(nrow(ligands.ori))){ligands.ori = Matrix::colSums(ligands.ori)} + if (!is.null(nrow(ligands.null))){ligands.null = Matrix::colSums(ligands.null)} + if (!is.null(nrow(receps.ori))){receps.ori = Matrix::colSums(receps.ori)} + if (!is.null(nrow(receps.null))){receps.null = Matrix::colSums(receps.null)} sub.df$ligands = ligands sub.df$ligands_ori = ligands.ori diff --git a/R/utility.r b/R/utility.r index a334dc7..2747ea5 100644 --- a/R/utility.r +++ b/R/utility.r @@ -9,7 +9,7 @@ filterGene <- function(dge.raw, gene_to_uniprot, thresh = 50){ mt_idx <- grep("MT-",rownames(dge.raw)) # find low quality gene index - low_quality_idx <- which(rowSums(dge.raw) < thresh) + low_quality_idx <- which(Matrix::rowSums(dge.raw) < thresh) cat(paste0("Number of low-quality intr genes: ", sum(intr.genes.index %in% low_quality_idx), "\n")) @@ -242,7 +242,7 @@ graphSpatialFDR <- function(nb.fac, pval.mtx, spatial.coords=NULL, weighting='DT # within each cell. This function filters out interactions that have: 1) low total counts # in the cells, 2) low number of significant interactions. filterRes <- function(dge.raw, res.list, intr.db, gene_to_uniprot, reads.thresh = 100, sig.thresh = 100){ - low_genes = diff(t(dge.raw)@p) + low_genes = diff(Matrix::t(dge.raw)@p) low_genes = toupper(rownames(dge.raw)[which(low_genes < reads.thresh)]) low_genes = intersect(low_genes, gene_to_uniprot$gene_name) @@ -324,7 +324,7 @@ dataImpKNN <- function( # dge.intr.imp <- dge.intr %*% dist.graph # dge.mix.imp <- rbind(dge.intr.imp, dge.other) - data.imp <- crossprod(data, dist.graph) + data.imp <- Matrix::crossprod(data, dist.graph) data.imp <- as(data.imp, "CsparseMatrix") cat("Finished!\n") diff --git a/man/imputeNiche.CytoSignal.Rd b/man/imputeNiche.CytoSignal.Rd index 7e6c9c2..9f6ca6d 100644 --- a/man/imputeNiche.CytoSignal.Rd +++ b/man/imputeNiche.CytoSignal.Rd @@ -7,7 +7,7 @@ \method{imputeNiche}{CytoSignal}( object, nn.type = NULL, - weights = c("mean", "counts", "dist", "gaussian"), + weights = c("mean", "counts", "dist", "none"), save.obj = TRUE ) }