Skip to content

Commit

Permalink
new fun: perplexityPermute
Browse files Browse the repository at this point in the history
  • Loading branch information
ningbioinfo committed Aug 13, 2024
1 parent 3e64531 commit 4dadbc1
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(clustByHood)
export(findNearCells)
export(mergeByGroup)
export(mergeHoodSpe)
export(perplexityPermute)
export(plotColocal)
export(plotHoodMat)
export(plotProbDist)
Expand Down
74 changes: 74 additions & 0 deletions R/calc_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,77 @@ calcMetrics <- function(spe, pm = NA, pm_cols = NA,

return(spe)
}

.get_perplexity <- function(pm){
p <- calculate_metrics(pm)[,"perplexity"]
return(p)
}

#' Compute p-value for perplexity via permutation
#'
#' @param spe A SpatialExperiment object.
#' @param pm Optional. The probability matrix.
#' @param pm_cols The colnames of probability matrix. This is requires for
#' SpatialExperiment input. Assuming that the probability is
#' stored in the colData.
#' @param n_perm Integer number. The number of permutation. 1000 by default.
#'
#' @return A SpatialExperiment object. Calculated P-value and adjusted P-value
#' are saved as columns in the colData of the SpatialExperiment object.
#' P-value and adjusted P-value are calculated based on permutation test and
#' Benjamini Hochberg correction.
#'
#' @export
#'
#' @examples
#'
#' data("spe_test")
#'
#' spe <- readHoodData(spe, anno_col = "celltypes")
#'
#' fnc <- findNearCells(spe, k = 100)
#'
#' pm <- scanHoods(fnc$distance)
#'
#' pm2 <- mergeByGroup(pm, fnc$cells)
#'
#' spe <- mergeHoodSpe(spe, pm2)
#'
#' spe <- perplexityPermute(spe, pm_cols = colnames(pm2))
perplexityPermute <- function(spe, pm = NA, pm_cols = NA, n_perm = 1000) {
if (!is(spe, "SpatialExperiment")){
stop("The input spe must be a SpatialExperiment object.")
}
if (is(pm, "logical")) {
if (is(pm_cols, "logical")) {
stop("Need to input either the pm or pm_cols parameters.")
} else {
pm <- as.data.frame(colData(spe),
optional = TRUE)[, pm_cols] |>
as.matrix()
}
} else {
pm <- pm
}

observed_perplexity <- .get_perplexity(pm)

permuted_perplexities <- matrix(NA, nrow(pm), n_perm)

for (i in 1:n_perm) {
permuted_matrix <- pm[sample(1:nrow(pm)), ]
permuted_perplexities[, i] <- .get_perplexity(permuted_matrix)
}

p_values <- apply(as.matrix(observed_perplexity), 1, function(obs) {
mean(permuted_perplexities >= obs)
})

adjp <- stats::p.adjust(p_values, method = "BH")

colData(spe)[,"perplexity_p"] <- p_values
colData(spe)[,"perplexity_adjp"] <- adjp

return(spe)
}

44 changes: 44 additions & 0 deletions man/perplexityPermute.Rd

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

0 comments on commit 4dadbc1

Please sign in to comment.