Skip to content

Commit

Permalink
Styler and spell check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene233 committed Mar 26, 2024
1 parent 02d615d commit 751531f
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 193 deletions.
2 changes: 1 addition & 1 deletion R/AllClasses.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
## Class definitions
setClassUnion(name = 'AnyMatrix', members = c("matrix", "dgCMatrix"))
setClassUnion(name = "AnyMatrix", members = c("matrix", "dgCMatrix"))
3 changes: 1 addition & 2 deletions R/gs_score-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ setMethod(
),
function(data,
features = NULL) {

## compute overall score
score <- gs_score_init(score = data, features = features)

Expand All @@ -32,7 +31,7 @@ setMethod(

## compute overall score
score <- sapply(names(features), \(i)
gs_score(data = data, features = features[[i]])) |>
gs_score(data = data, features = features[[i]])) |>
data.frame()
## set colnames
colnames(score) <- paste(names(features), suffix, sep = ".")
Expand Down
91 changes: 55 additions & 36 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ plot_mm <- function(mixmdl, dist = c("norm", "gamma")) {

p <- ggplot(data.frame(x = mixmdl$x), aes(x = x)) +
geom_histogram()
if(dist == "norm") {
if (dist == "norm") {
p <- p +
mapply(function(mean, sd, lambda, col) {
stat_function(
fun = function(x) {
(dnorm(x, mean = mean, sd = sd)) * lambda
},
col = col
)},
mapply(
function(mean, sd, lambda, col) {
stat_function(
fun = function(x) {
(dnorm(x, mean = mean, sd = sd)) * lambda
},
col = col
)
},
mean = mixmdl$mu,
sd = mixmdl$sigma,
lambda = mixmdl$lambda,
Expand All @@ -23,18 +25,20 @@ plot_mm <- function(mixmdl, dist = c("norm", "gamma")) {
geom_vline(xintercept = mixmdl$mu, col = seq_along(mixmdl$mu) + 1) +
labs(x = "score", y = "density") +
theme_bw()
}else {
} else {
## compute mu
mixmdl$mu <- mixmdl$gamma.pars[1,] * mixmdl$gamma.pars[2,] # alpha * beta
mixmdl$mu <- mixmdl$gamma.pars[1, ] * mixmdl$gamma.pars[2, ] # alpha * beta

p <- p +
mapply(function(mean, alpha, beta, lambda, col) {
stat_function(
fun = function(x) {
(dgamma(x, shape = alpha, rate = beta)) * lambda
},
col = col
)},
mapply(
function(mean, alpha, beta, lambda, col) {
stat_function(
fun = function(x) {
(dgamma(x, shape = alpha, rate = beta)) * lambda
},
col = col
)
},
mean = mixmdl$mu,
alpha = mixmdl$gamma.pars["alpha", ],
beta = mixmdl$gamma.pars["beta", ],
Expand Down Expand Up @@ -86,8 +90,10 @@ plot_mm_clust <- function(score, clust) {
sin_score_boxplot <- function(data, features = NULL,
ref.group, label,
method = "t.test") {
if(is.null(features)) features <- rownames(data)
data[features,] |> as.matrix() |> as.data.frame() |>
if (is.null(features)) features <- rownames(data)
data[features, ] |>
as.matrix() |>
as.data.frame() |>
dplyr::add_rownames("Gene") |>
setNames(c("Gene", as.character(label))) |>
tidyr::pivot_longer(-"Gene", names_to = "Type", values_to = "Score") |>
Expand All @@ -97,8 +103,10 @@ sin_score_boxplot <- function(data, features = NULL,
geom_violin() +
stat_summary(fun = median, geom = "crossbar") +
facet_wrap(~Gene, scales = "free") +
ggpubr::stat_compare_means(label = "p.signif", method = method,
ref.group = ref.group, label.y.npc = 1) +
ggpubr::stat_compare_means(
label = "p.signif", method = method,
ref.group = ref.group, label.y.npc = 1
) +
theme_classic() +
theme(axis.text.x = element_blank())
}
Expand All @@ -116,13 +124,17 @@ sin_score_boxplot <- function(data, features = NULL,
ova_score_boxplot <- function(data, features,
ref.group, label,
method = "t.test") {
data.frame(Score = gs_score_init(data, features = features),
Group = label) |>
data.frame(
Score = gs_score_init(data, features = features),
Group = label
) |>
ggplot(aes(x = Group, y = Score, col = Group)) +
geom_boxplot() +
# geom_jitter(aes(col = Group), alpha = 0.2) +
ggpubr::stat_compare_means(label = "p.signif", method = method,
ref.group = ref.group, label.y.npc = 1) +
ggpubr::stat_compare_means(
label = "p.signif", method = method,
ref.group = ref.group, label.y.npc = 1
) +
theme_classic() +
theme(axis.text.x = element_blank())
}
Expand All @@ -143,8 +155,9 @@ ova_score_boxplot <- function(data, features,
#' score_barplot(top_n)
score_barplot <- function(top_markers, column = ".dot", f_list, n = 30) {
## set f_list as features label for color
if(missing(f_list))
if (missing(f_list)) {
f_list <- list(Features = top_markers$Genes)
}

# ## get top n markers
# top_markers <- top_markers(
Expand All @@ -158,22 +171,28 @@ score_barplot <- function(top_markers, column = ".dot", f_list, n = 30) {
top_markers <- dplyr::slice_max(top_markers, Scores, n = n)

## add markers type
top_markers <- merge(top_markers, stack(f_list), by.x = "Genes",
by.y = "values", all.x = TRUE)
top_markers <- merge(top_markers, stack(f_list),
by.x = "Genes",
by.y = "values", all.x = TRUE
)

## plot
ggplot(top_markers,
aes(
y = tidytext::reorder_within(Genes, Scores, !!ggplot2::sym(column)),
x = Scores,
fill = ind
)) +
ggplot(
top_markers,
aes(
y = tidytext::reorder_within(Genes, Scores, !!ggplot2::sym(column)),
x = Scores,
fill = ind
)
) +
geom_bar(stat = "identity") +
facet_wrap(ggplot2::sym(column), scales = "free") +
labs(y = "Genes", fill = "Markers Type") +
tidytext::scale_y_reordered() +
theme_classic()
}

utils::globalVariables(c("Group", "Score", "x", "Scores", "Comp", "Genes",
"..density..", "stack", "values", "ind", "Type"))
utils::globalVariables(c(
"Group", "Score", "x", "Scores", "Comp", "Genes",
"..density..", "stack", "values", "ind", "Type"
))
2 changes: 1 addition & 1 deletion R/scale_mgm.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ scale_mgm <- function(expr, label) {

## compute group means
mgm <- sapply(unique(label), \(i)
sparseMatrixStats::rowMeans2(expr[, label == i], na.rm = TRUE)) |> # get mean of each group
sparseMatrixStats::rowMeans2(expr[, label == i], na.rm = TRUE)) |> # get mean of each group
rowMeans(na.rm = TRUE) # get mean of group mean

## scale
Expand Down
5 changes: 2 additions & 3 deletions R/score-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ setMethod(
iae = "prob",
par.idf = NULL,
par.iae = NULL) {

score <- cal_score_init(
expr = data,
tf = tf,
Expand Down Expand Up @@ -42,10 +41,10 @@ setMethod(
## get expr
expr <- SummarizedExperiment::assay(data, i = slot)
## get label
if(!is.null(par.idf) & !is.null(par.idf$label)) {
if (!is.null(par.idf) & !is.null(par.idf$label)) {
par.idf$label <- data@colData[[par.idf$label]]
}
if(!is.null(par.iae) & !is.null(par.iae$label)) {
if (!is.null(par.iae) & !is.null(par.iae$label)) {
par.iae$label <- data@colData[[par.iae$label]]
}

Expand Down
35 changes: 21 additions & 14 deletions R/score.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ NULL
#' data <- matrix(rnorm(100), 10, dimnames = list(1:10))
#' gs_score_init(data, 1:5)
gs_score_init <- function(score, features = NULL) {
if(is.null(features)) features <- rownames(score)
if (is.null(features)) features <- rownames(score)

## check features
if(!all(features %in% rownames(score)))
warning(sprintf("Feature %s is not in score!\n",
setdiff(features, rownames(score))))
if (!all(features %in% rownames(score))) {
warning(sprintf(
"Feature %s is not in score!\n",
setdiff(features, rownames(score))
))
}
features <- intersect(rownames(score), features)
stopifnot("less than 2 features are in score rows!" = length(features) > 1)

Expand All @@ -36,10 +39,12 @@ gs_score_init <- function(score, features = NULL) {
#' @examples
#' idf_iae_methods()
idf_iae_methods <- function() {
return(sort(c("label probability" = 'prob', "label relative frequency" = 'rf',
"label IGM" = 'igm', "null" = 'null',
"unlabel max" = 'm', "unlabel SD" = 'sd',
"unlabel HDBSCAN" = "hdb", "unlabel standard" = 'standard')))
return(sort(c(
"label probability" = "prob", "label relative frequency" = "rf",
"label IGM" = "igm", "null" = "null",
"unlabel max" = "m", "unlabel SD" = "sd",
"unlabel HDBSCAN" = "hdb", "unlabel standard" = "standard"
)))
}

#' Calculate score for each feature in each cell
Expand All @@ -58,8 +63,10 @@ idf_iae_methods <- function() {
#' @examples
#' data <- matrix(rpois(100, 2), 10, dimnames = list(1:10))
#' label <- sample(c("A", "B"), 10, replace = TRUE)
#' smartid:::cal_score_init(data, par.idf = list(label = label),
#' par.iae = list(label = label))
#' smartid:::cal_score_init(data,
#' par.idf = list(label = label),
#' par.iae = list(label = label)
#' )
cal_score_init <- function(expr, tf = c("logtf", "tf"),
idf = "prob", iae = "prob",
par.idf = NULL, par.iae = NULL) {
Expand All @@ -76,17 +83,17 @@ cal_score_init <- function(expr, tf = c("logtf", "tf"),
tf <- tf(expr, log = (tf == "logtf"))

## compute idf
if(idf == "null") {
if (idf == "null") {
idf <- 1
}else {
} else {
idf <- ifelse(idf == "standard", "idf", paste0("idf_", idf))
idf <- do.call(idf, c(list(expr = expr), par.idf))
}

## compute iae
if(iae == "null") {
if (iae == "null") {
iae <- 1
}else {
} else {
iae <- ifelse(iae == "standard", "iae", paste0("iae_", iae))
iae <- do.call(iae, c(list(expr = expr), par.iae))
}
Expand Down
Loading

0 comments on commit 751531f

Please sign in to comment.