Skip to content

Commit

Permalink
fixes leadingEdge for positive pathways (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
assaron committed Sep 20, 2024
1 parent eecbf6b commit 82f17c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
11 changes: 2 additions & 9 deletions R/fgsea.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,15 @@ calcGseaStat <- function(stats,
res <- c(res, list(tops=tops, bottoms=bottoms))
}
if (returnLeadingEdge) {
leadingEdge <- if (maxP > -minP) {
S[seq_along(S) <= which.max(bottoms)]
} else if (maxP < -minP) {
rev(S[seq_along(S) >= which.min(bottoms)])
} else {
NULL
}
switch(scoreType,
std = leadingEdge <- if (maxP > -minP) {
S[seq_along(S) <= which.max(bottoms)]
S[seq_along(S) <= which.max(tops)]
} else if (maxP < -minP) {
rev(S[seq_along(S) >= which.min(bottoms)])
} else {
NULL
},
pos = leadingEdge <- S[seq_along(S) <= which.max(bottoms)],
pos = leadingEdge <- S[seq_along(S) <= which.max(tops)],
neg = leadingEdge <- rev(S[seq_along(S) >= which.min(bottoms)]))

res <- c(res, list(leadingEdge=leadingEdge))
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test_gsea_stat.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ test_that("calcGseaStats(returnLeadingEdge=TRUE) works", {
expect_false(10 %in% gseaRes$leadingEdge)
})

test_that("leading edge is consistent", {
stats <- exampleRanks
pathway <- examplePathways[[1]]

set.seed(1)
gseaRes <- fgseaSimple(list(p=pathway), stats, nperm=2)
gseaResRev <- fgseaSimple(list(p=pathway), -stats, nperm=2)

expect_identical(gseaRes$leadingEdge, gseaResRev$leadingEdge)

gseaResPos <- fgseaSimple(list(p=pathway), stats, nperm=2, scoreType = "pos")
expect_identical(gseaRes$leadingEdge, gseaResPos$leadingEdge)
})

test_that("calcGseaStats returns zero when both sides are equally enriched", {
stats <- 10:-10
sample <- 10:12
Expand Down

0 comments on commit 82f17c9

Please sign in to comment.