Skip to content

Commit

Permalink
update batch param in top_markers function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene233 committed Nov 13, 2024
1 parent 4836630 commit c7cb658
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ setGeneric(
label,
n = 10,
use.glm = TRUE,
batch = NULL,
scale = TRUE,
use.mgm = TRUE,
softmax = TRUE,
Expand Down
13 changes: 12 additions & 1 deletion R/top_markers-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ setMethod(
label,
n = 10,
use.glm = TRUE,
batch = NULL,
scale = TRUE,
use.mgm = TRUE,
softmax = TRUE,
Expand All @@ -20,6 +21,8 @@ setMethod(
"label must be atomic and the same length as data column number" =
is.atomic(label) & length(label) == ncol(data),
"use.glm must be logical" = is.logical(use.glm),
"batch must be atomic and the same length as data column number" =
(is.atomic(batch) & length(batch) == ncol(data)) | is.null(batch),
"scale must be logical" = is.logical(scale),
"use.mgm must be logical" = is.logical(use.mgm),
"softmax must be logical" = is.logical(softmax)
Expand All @@ -30,6 +33,7 @@ setMethod(
label = label,
n = n,
use.glm = use.glm,
batch = batch,
scale = scale,
use.mgm = use.mgm,
softmax = softmax,
Expand All @@ -49,6 +53,7 @@ setMethod(
label,
n = 10,
use.glm = TRUE,
batch = NULL,
scale = TRUE,
use.mgm = TRUE,
softmax = TRUE,
Expand All @@ -57,19 +62,25 @@ setMethod(
## check
stopifnot(
"label must be a single character for se object" =
is.character(label) & length(label) == 1
is.character(label) & length(label) == 1,
"batch must be NA or a single character for se object" =
is.null(batch) | (is.character(batch) & length(batch) == 1)
)

## get expr
expr <- SummarizedExperiment::assay(data, i = slot)
## get label
label <- SummarizedExperiment::colData(data)[[label]]
## get batch
if(!is.null(batch))
batch <- SummarizedExperiment::colData(data)[[batch]]

top_m <- top_markers(
data = expr,
label = label,
n = n,
use.glm = use.glm,
batch = batch,
scale = scale,
use.mgm = use.mgm,
softmax = softmax,
Expand Down
4 changes: 4 additions & 0 deletions R/top_markers.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' top_markers_init(data, label = rep(c("A", "B"), 5))
top_markers_init <- function(data, label, n = 10,
use.glm = TRUE,
batch = NULL,
scale = TRUE,
use.mgm = TRUE,
softmax = TRUE,
Expand All @@ -22,6 +23,7 @@ top_markers_init <- function(data, label, n = 10,
data <- top_markers_glm(
data = data,
label = label,
batch = batch,
n = n,
scale = scale,
use.mgm = use.mgm,
Expand Down Expand Up @@ -151,6 +153,8 @@ top_markers_glm <- function(data, label, n = 10,
## model with group label only
betas <- apply(data, 1, \(s) glm(s ~ 0 + label, family = family)$coef)
} else {
## factorize batch label
batch <- factor(batch)
## model with both group and batch label
betas <- apply(data, 1, \(s) glm(s ~ 0 + label + batch, family = family)$coef)
## only extract betas of group label
Expand Down
5 changes: 5 additions & 0 deletions man/top_markers.Rd

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

3 changes: 3 additions & 0 deletions man/top_markers_init.Rd

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

13 changes: 12 additions & 1 deletion tests/testthat/test-top_markers.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ test_that("top markers works", {
## sim data
expr <- matrix(rgamma(100, 2), 10, dimnames = list(1:10))
label <- rep(c("A", "B"), 5)
batch <- sample(seq_len(2), 10, replace = TRUE)

se <- SummarizedExperiment::SummarizedExperiment(
assays = list(counts = expr),
colData = data.frame(group = label)
colData = data.frame(group = label, batch = batch)
)

## test matrix
Expand Down Expand Up @@ -50,4 +51,14 @@ test_that("top markers works", {
slot = "counts"
)
expect_s3_class(res, "data.frame")

## test batch
res <- top_markers(
data = se,
label = "group",
batch = "batch",
n = 3,
slot = "counts"
)
expect_s3_class(res, "data.frame")
})

0 comments on commit c7cb658

Please sign in to comment.