Skip to content

Commit

Permalink
Replace sapply with vapply and add drop=F in [].
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene233 committed Mar 26, 2024
1 parent baa33a1 commit 3002a02
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 94 deletions.
7 changes: 5 additions & 2 deletions R/gs_score-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ setMethod(
)

## compute overall score
score <- sapply(names(features), \(i)
gs_score(data = data, features = features[[i]])) |>
score <- vapply(
names(features), \(i)
gs_score(data = data, features = features[[i]]),
rep(1, ncol(data))
) |>
data.frame()
## set colnames
colnames(score) <- paste(names(features), suffix, sep = ".")
Expand Down
10 changes: 1 addition & 9 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ sin_score_boxplot <- function(data, features = NULL,
ref.group, label,
method = "t.test") {
if (is.null(features)) features <- rownames(data)
data[features, ] |>
data[features, , drop = FALSE] |>
as.matrix() |>
as.data.frame() |>
dplyr::add_rownames("Gene") |>
Expand Down Expand Up @@ -159,14 +159,6 @@ score_barplot <- function(top_markers, column = ".dot", f_list, n = 30) {
f_list <- list(Features = top_markers$Genes)
}

# ## get top n markers
# top_markers <- top_markers(
# data[features, ],
# label = label,
# n = n,
# ...
# )

## extract top n markers
top_markers <- dplyr::slice_max(top_markers, Scores, n = n)

Expand Down
9 changes: 7 additions & 2 deletions R/scale_mgm.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ scale_mgm <- function(expr, label) {
# colnames(sds) <- unique(label)

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

## scale
Expand Down
2 changes: 1 addition & 1 deletion R/score.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ gs_score_init <- function(score, features = NULL) {
stopifnot("less than 2 features are in score rows!" = length(features) > 1)

## calculate mean score of features
m_score <- colMeans(score[features, ], na.rm = TRUE)
m_score <- colMeans(score[features, , drop = FALSE], na.rm = TRUE)
return(m_score)
}

Expand Down
168 changes: 94 additions & 74 deletions R/tf_idf_iae_wrappers.R

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions R/top_markers.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ top_markers_glm <- function(data, label, n = 10,
# betas <- apply(betas, 2, \(x) x - (sum(x) - x)/(length(x) - 1))

## compute logFC (1 vs max excluding self) for each group
betas <- sapply(seq_len(nrow(betas)), \(i)
betas[i, ] - sparseMatrixStats::colMaxs(matrix(betas[-i, ], ncol = ncol(betas)))) |>
betas <- vapply(
seq_len(nrow(betas)), \(i)
betas[i, ] - sparseMatrixStats::colMaxs(betas[-i, , drop = FALSE]),
rep(1, ncol(betas))
) |>
t()
rownames(betas) <- levels(label)

Expand Down
6 changes: 4 additions & 2 deletions man/cal_score_init.Rd

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

2 changes: 1 addition & 1 deletion man/iae_hdb.Rd

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

2 changes: 1 addition & 1 deletion man/idf_hdb.Rd

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

0 comments on commit 3002a02

Please sign in to comment.