Skip to content

Commit

Permalink
fix matirx pkg import issue
Browse files Browse the repository at this point in the history
  • Loading branch information
skpalan committed Mar 2, 2023
1 parent 0e023e0 commit d75a2b1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ Depends:
methods,
Rcpp,
RcppArmadillo,
stats
stats,
ggplot2,
cowplot
Imports:
dbscan,
ggplot2,
ggsci,
paletteer,
RANN,
Expand Down
30 changes: 17 additions & 13 deletions R/analysis.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)){
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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]]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/objects.r
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
14 changes: 7 additions & 7 deletions R/plotting.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions R/utility.r
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion man/imputeNiche.CytoSignal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d75a2b1

Please sign in to comment.