Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply 'adjustment's to metadata targets #157

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
apply 'adjustment's to metadata targets
  • Loading branch information
dtm2451 committed Sep 24, 2024
commit 562cf93e1faccccf2c8e24881ab8e6e3f0e7d3b3
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ importFrom(pheatmap,pheatmap)
importFrom(stats,as.formula)
importFrom(stats,median)
importFrom(stats,sd)
importFrom(stats,setNames)
importFrom(utils,modifyList)
importFrom(utils,packageVersion)
importFrom(utils,read.table)
4 changes: 2 additions & 2 deletions R/ColorAdjustments.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' Darkens input colors by a set amount
#'
#' @description A wrapper for the darken function of the colorspace package.
#' @param colors the color(s) input. Can be a list of colors, for example, /code{dittoColors()}.
#' @param colors the color(s) input. Can be a list of colors, for example, \code{dittoColors()}.
#' @param percent.change # between 0 and 1. the percentage to darken by. Defaults to 0.25 if not given.
#' @param relative TRUE/FALSE. Whether the percentage should be a relative change versus an absolute one. Default = TRUE.
#' @return Return a darkened version of the color in hexadecimal color form (="#RRGGBB" in base 16)
Expand All @@ -23,7 +23,7 @@ Darken <- function(colors, percent.change = 0.25, relative = TRUE) {
#' Lightens input colors by a set amount
#'
#' @description A wrapper for the lighten function of the colorspace package.
#' @param colors the color(s) input. Can be a list of colors, for example, /code{dittoColors()}.
#' @param colors the color(s) input. Can be a list of colors, for example, \code{dittoColors()}.
#' @param percent.change # between 0 and 1. the percentage to darken by. Defaults to 0.25 if not given.
#' @param relative TRUE/FALSE. Whether the percentage should be a relative change versus an absolute one. Default = TRUE.
#' @return Return a lighter version of the color in hexadecimal color form (="#RRGGBB" in base 16)
Expand Down
15 changes: 13 additions & 2 deletions R/dittoDotPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ dittoDotPlot <- function(
)
}

#' @importFrom stats sd
#' @importFrom stats sd setNames
.multi_var_gather_raw <- function(
object,
vars,
Expand All @@ -623,7 +623,18 @@ dittoDotPlot <- function(
}

gets_data <- if (length(meta_gets)>0) {
getMetas(object, names.only = FALSE)[, meta_gets, drop = FALSE]
data.frame(
setNames(
lapply(
meta_gets,
function(m) {
# Individual pulls for applying adjustments
meta(m, object, adjustment, add.names = FALSE)
}
),
meta_gets),
row.names = .all_cells(object)
)
} else {
data.frame(row.names = .all_cells(object))
}
Expand Down
13 changes: 8 additions & 5 deletions R/meta-getters.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ getMetas <- function(object, names.only = TRUE){
}

#### meta: for extracting the values of a particular metadata for all cells/samples ####
#' Returns the values of a meta.data for all cells/samples
#' Returns the values of a meta.data for all cells/samples
#'
#' @param meta String, the name of the "metadata" slot to grab. OR "ident" to retireve the clustering of a Seurat \code{object}.
#' @param object A Seurat, SingleCellExperiment, or SummarizedExperiment object.
Expand All @@ -100,14 +100,15 @@ getMetas <- function(object, names.only = TRUE){
#' @param adj.fxn A function which takes a vector (of metadata values) and returns a vector of the same length.
#'
#' For example, \code{function(x) \{log2(x)\}} or \code{as.factor}
#' @return A named vector.
#' @param add.names Logical which sets whether cells'/samples' names should be added as names on the output.
#' @return A vector
#' @details
#' Retrieves the values of a metadata slot from \code{object}, or the clustering slot if \code{meta = "ident"} and the \code{object} is a Seurat.
#'
#' If \code{adjustment} or \code{adj.fxn} are provided, then these requested adjustments are applied to these values (\code{adjustment} first).
#' Note: Alterations via \code{adjustment} are only applied when metadata is numeric, but \code{adj.fxn} alterations are applied to metadata of any type.
#'
#' Lastly, outputs these values are named as the cells'/samples' names.
#' Lastly, if \code{add.names = TRUE} the values are named as the cells'/samples' names before being output.
#' @seealso
#' \code{\link{metaLevels}} for returning just the unique discrete identities that exist within a metadata slot
#'
Expand All @@ -127,7 +128,7 @@ getMetas <- function(object, names.only = TRUE){
#' @export

meta <- function(meta, object,
adjustment = NULL, adj.fxn = NULL) {
adjustment = NULL, adj.fxn = NULL, add.names = TRUE) {

if (!isMeta(meta, object)) {
stop(dQuote(meta)," is not a metadata of 'object'")
Expand Down Expand Up @@ -165,7 +166,9 @@ meta <- function(meta, object,
}

# Add names
names(values) <- .all_cells(object)
if (add.names) {
names(values) <- .all_cells(object)
}

values
}
Expand Down
2 changes: 1 addition & 1 deletion R/utils-getters.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@

if (length(var)==1 && is.character(var)) {
if (isMeta(var, object)) {
OUT <- meta(var, object)
OUT <- meta(var, object, adjustment)
} else if (isGene(var, object, assay)) {
OUT <- gene(var, object, assay, slot, adjustment)
}
Expand Down
2 changes: 1 addition & 1 deletion man/Darken.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/Lighten.Rd

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

12 changes: 7 additions & 5 deletions man/meta.Rd

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

10 changes: 8 additions & 2 deletions tests/testthat/test-getters.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Tests for visualization functions
# library(dittoSeq); library(testthat); source("setup.R"); source("../../R/utils.R"); source("../../R/utils-getters.R"); source("../../R/get.reductions.R"); source("../../R/utils-defaulting.R"); source("test-getters.R")
# library(dittoSeq); library(testthat); for (i in list.files("../../R", pattern="^utils", full.names = TRUE)) source(i); source("test-getters.R")

# Make Seurat, if can
try(seurat <- Seurat::as.Seurat(sce), silent = TRUE)
Expand Down Expand Up @@ -28,7 +28,7 @@ test_that("isMeta works for Seurat and SCE", {
expect_true(isMeta("score", seurat))
})

test_that("meta works for Seurat and SCE (+ adjustment/adj.fxn)", {
test_that("meta works for Seurat and SCE (+ adjustment/adj.fxn, add.names)", {
expect_type(
meta("score", sce),
"double")
Expand All @@ -45,6 +45,12 @@ test_that("meta works for Seurat and SCE (+ adjustment/adj.fxn)", {
factor(meta("score", sce)),
meta("score", sce, adj.fxn = function(x) {factor(x)}))

expect_true(is.null(names(meta("age", sce, add.names = FALSE))))
expect_equal(
names(meta("age", sce)),
.all_cells(sce)
)

skip_if_not(seurat_conversion_worked, message = "Seurat conversion bug")
expect_equal(
meta("score", sce),
Expand Down