Skip to content

Instantly share code, notes, and snippets.

@kieranrcampbell
Created August 17, 2020 21:02
Show Gist options
  • Save kieranrcampbell/2b2b1aeb58ce58c16e5443360aa3299a to your computer and use it in GitHub Desktop.
Save kieranrcampbell/2b2b1aeb58ce58c16e5443360aa3299a to your computer and use it in GitHub Desktop.
#' Sort the results of a gsva() call so that the
#' pathway activation always correlates positively
#' on average with the gene set listed
#' @param g Result of call to gsva() (pathway x cell)
#' @param genesets List of genes corresponding to genesets of g
#' @param sce A SingleCellExperiment with a logcounts assay
fix_gsva_direction <- function(g, genesets, sce) {
stopifnot(nrow(g) == length(genesets))
genesets <- lapply(genesets, intersect, rownames(sce))
signs <- sapply(seq_along(genesets), function(i) {
gg <- g[i,]
cors <- apply(
logcounts(sce)[genesets[[i]],],
1,
cor,
gg
)
sign(mean(cors, na.rm=TRUE))
})
signs * g
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment