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

add reference samples to divergence outputs #100

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 16 additions & 18 deletions R/getBaselineDivergence.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@
#' \code{colData} that identifies the baseline samples to be used.
#' (Default: \code{NULL})
#'
#' @param name \code{Character scalar}. Specifies a column name for storing
#' divergence results. (Default: \code{"divergence"})
#'
#' @param name.time \code{Character scalar}. Specifies a column name for storing
#' time differences. (Default: \code{"time_diff"})
#' @param name \code{Character vector}. Specifies a column name for storing
#' divergence results.
#' (Default: \code{c("divergence", "time_diff", "ref_samples")})
#'
#' @param ... Optional arguments passed into
#' \code{\link[mia:addDivergence]{mia::addDivergence()}}.
Expand Down Expand Up @@ -86,8 +84,8 @@
#' reference = "reference",
#' group = "subject",
#' time.col = "time",
#' name = "divergence_from_baseline",
#' name.time = "time_from_baseline",
#' name = c("divergence_from_baseline",
#' "time_from_baseline", "reference_samples"),
#' assay.type = "relabundance",
#' method = "bray")
#'
Expand Down Expand Up @@ -153,20 +151,21 @@ setMethod("getBaselineDivergence", signature = c(x = "SummarizedExperiment"),
reference <- args[["reference"]]
time_res <- .get_time_difference(x, time.col, reference)
# Create a DF to return
res <- .convert_divergence_to_df(x, res, time_res, ...)
res <- .convert_divergence_to_df(x, res, time_res, reference, ...)
return(res)
}
)

#' @rdname addBaselineDivergence
#' @export
setMethod("addBaselineDivergence", signature = c(x = "SummarizedExperiment"),
function(x, name = "divergence", name.time = "time_diff", ...){
function(
x, name = c("divergence", "time_diff", "ref_samples"), ...){
# Calculate divergence
res <- getBaselineDivergence(x, ...)
# Add to colData
res <- as.list(res) |> unname()
x <- .add_values_to_colData(x, res, list(name, name.time), ...)
x <- .add_values_to_colData(x, res, name, ...)
return(x)
}
)
Expand Down Expand Up @@ -317,7 +316,7 @@ setMethod("addBaselineDivergence", signature = c(x = "SummarizedExperiment"),
return(res)
}

# This function get time difference between a sample and its referene sample
# This function get time difference between a sample and its reference sample
.get_time_difference <- function(x, time.col, reference){
# Get timepoints
time_point <- x[[time.col]]
Expand All @@ -330,13 +329,12 @@ setMethod("addBaselineDivergence", signature = c(x = "SummarizedExperiment"),

# This function converts time divergence results to DF object
.convert_divergence_to_df <- function(
x, res, time_res, name = "divergence", name.time = "time_diff", ...){
#
temp <- .check_input(name, list("character scalar"))
#
temp <- .check_input(name.time, list("character scalar"))
x, res, time_res, reference,
name = c("divergence", "time_diff", "ref_samples"), ...){
# Validate 'name' param
temp <- .check_input(name, list("character vector"), length = 3)
#
df <- DataFrame(res, time_res, row.names = colnames(x))
colnames(df) <- c(name, name.time)
df <- DataFrame(res, time_res, x[[reference]], row.names = colnames(x))
colnames(df) <- name
return(df)
}
7 changes: 4 additions & 3 deletions R/getStepwiseDivergence.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,21 @@ setMethod("getStepwiseDivergence", signature = c(x = "ANY"),
reference <- args[["reference"]]
time_res <- .get_time_difference(x, time.col, reference)
# Create a DF to return
res <- .convert_divergence_to_df(x, res, time_res, ...)
res <- .convert_divergence_to_df(x, res, time_res, reference, ...)
return(res)
}
)

#' @rdname addStepwiseDivergence
#' @export
setMethod("addStepwiseDivergence", signature = c(x = "SummarizedExperiment"),
function(x, name = "divergence", name.time = "time_diff", ...){
function(
x, name = c("divergence", "time_diff", "ref_samples"), ...){
# Calculate divergence
res <- getStepwiseDivergence(x, ...)
# Add to colData
res <- as.list(res) |> unname()
x <- .add_values_to_colData(x, res, list(name, name.time), ...)
x <- .add_values_to_colData(x, res, name, ...)
return(x)
}
)
11 changes: 10 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Borrowed from HoloFoodR.
.check_input <- function(
variable, supported_class, supported_values = NULL, limits = NULL,
variable_name = .get_name_in_parent(variable)){
length = NULL, variable_name = .get_name_in_parent(variable)){
# Convert supported classes to character
classes_char <- lapply(supported_class, function(class){
if( is.null(class) ){
Expand Down Expand Up @@ -54,6 +54,11 @@
msg <- paste0(msg, ")")
}

# If length was provided
if( !is.null(length) ){
msg <- paste0(msg, " The length must be ", length, ".")
}

# List all the input types. Run the check if the variable must be that type.
# If correct type was found, change the result to TRUE.
input_correct <- FALSE
Expand Down Expand Up @@ -128,6 +133,10 @@
input_correct <- FALSE
}
}
# Check length if provided
if( !is.null(length) && length(variable) != length ){
input_correct <- FALSE
}
# Give error if variable was not correct type
if( !input_correct ){
stop(msg, call. = FALSE)
Expand Down
18 changes: 10 additions & 8 deletions man/addBaselineDivergence.Rd

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

14 changes: 8 additions & 6 deletions man/addStepwiseDivergence.Rd

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

4 changes: 2 additions & 2 deletions man/miaTime-package.Rd

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

Loading
Loading