diff --git a/DESCRIPTION b/DESCRIPTION index 57d207135..1721bc161 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: mia Type: Package -Version: 1.13.40 +Version: 1.13.41 Authors@R: c(person(given = "Felix G.M.", family = "Ernst", role = c("aut"), email = "felix.gm.ernst@outlook.com", diff --git a/NEWS b/NEWS index e596cfbc0..ba6dea68c 100644 --- a/NEWS +++ b/NEWS @@ -153,3 +153,4 @@ computation + new methods getNMF and addNMF for NMF ordination with feature loadings computation + If missing values, give informative error in *RDA/*CCA functions ++ transformAssay can apply transformation to altExp diff --git a/R/transformCounts.R b/R/transformCounts.R index 70d53cb6c..5f5209eeb 100644 --- a/R/transformCounts.R +++ b/R/transformCounts.R @@ -23,6 +23,11 @@ #' #' @param name \code{Character scalar}. A name for the column of the #' \code{colData} where results will be stored. (Default: \code{"method"}) +#' +#' @param altexp \code{Character vector} or \code{NULL}. Specifies the names +#' of alternative experiments to which the transformation should also be +#' applied. If \code{NULL}, the transformation is only applied to the main +#' experiment. (Default: \code{altExpNames(x)}). #' #' @param ... additional arguments passed on to \code{vegan:decostand}: #' \itemize{ @@ -45,8 +50,9 @@ #' #' \itemize{ #' -#' \item 'alr', 'chi.square', 'clr', 'frequency', 'hellinger', 'log', 'normalize', 'pa', 'rank', 'rclr' -#' 'relabundance', 'rrank', 'standardize', 'total': please refer to +#' \item 'alr', 'chi.square', 'clr', 'frequency', 'hellinger', 'log', +#' 'normalize', 'pa', 'rank', 'rclr' relabundance', 'rrank', 'standardize', +#' 'total': please refer to #' \code{\link[vegan:decostand]{decostand}} for details. #' #' \item 'log10': log10 transformation can be used for reducing the skewness @@ -65,14 +71,15 @@ #' #' @return #' \code{transformAssay} returns the input object \code{x}, with a new -#' transformed abundance table named \code{name} added in the \code{\link{assay}}. +#' transformed abundance table named \code{name} added in the +#' \code{\link{assay}}. #' #' @name transformAssay #' @export #' #' @examples -#' data(esophagus, package="mia") -#' tse <- esophagus +#' data(GlobalPatterns) +#' tse <- GlobalPatterns #' #' # By specifying 'method', it is possible to apply different transformations, #' # e.g. compositional transformation. @@ -109,127 +116,152 @@ #' assay(tse, "rank_average", withDimnames = FALSE) <- colRanks( #' assay(tse, "counts"), ties.method = "average", preserveShape = TRUE) #' +#' # Using altexp parameter. First agglomerate the data and then apply +#' # transformation. +#' tse <- GlobalPatterns +#' tse <- agglomerateByRanks(tse) +#' tse <- transformAssay(tse, method = "relabundance") +#' # The transformation is applied to all alternative experiments +#' altExp(tse, "Species") +#' NULL #' @rdname transformAssay #' @export setGeneric("transformAssay", signature = c("x"), - function(x, - assay.type = "counts", assay_name = NULL, - method = c("alr", "chi.square", "clr", "frequency", - "hellinger", "log", "log10", "log2", "max", - "normalize", "pa", "range", "rank", "rclr", - "relabundance", "rrank", "standardize", "total", - "z"), - MARGIN = "samples", - name = method, - pseudocount = FALSE, - ...) - standardGeneric("transformAssay")) + function(x, ...) + standardGeneric("transformAssay")) #' @rdname transformAssay #' @export setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), function(x, - assay.type = "counts", assay_name = NULL, - method = c("alr", "chi.square", "clr", "frequency", "hellinger", - "log", "log10", "log2", "max", "normalize", "pa", - "range", "rank", "rclr", "relabundance", "rrank", - "standardize", "total", "z"), - MARGIN = "samples", - name = method, - pseudocount = FALSE, - ...){ - # Input check + assay.type = "counts", assay_name = NULL, + method = c("alr", "chi.square", "clr", "css", "css.fast", "frequency", + "hellinger", "log", "log10", "log2", "max", "normalize", + "pa", "range", "rank", "rclr", "relabundance", "rrank", + "standardize", "total", "z"), + MARGIN = "samples", + name = method, + pseudocount = FALSE, + ...){ + # + x <- .transform_assay( + x = x, method = method, name = name, assay.type = assay.type, + MARGIN = MARGIN, pseudocount = pseudocount, ...) + return(x) + } +) - if (!is.null(assay_name)) { - .Deprecated(old="assay_name", new="assay.type", "Now assay_name is deprecated. Use assay.type instead.") - assay.type <- assay_name +#' @rdname transformAssay +#' @export +setMethod("transformAssay", signature = c(x = "SingleCellExperiment"), + function(x, altexp = altExpNames(x), ...){ + # Check altexp + if( !(is.null(altexp) || all(altexp %in% altExpNames(x))) ){ + stop("'altexp' should be NULL or specify names from ", + "altExpNames(x).", call. = FALSE) } + # + # Transform the main object + x <- .transform_assay(x, ...) + # Transform alternative experiments + altExps(x)[altexp] <- lapply(altExps(x)[altexp], function(y){ + .transform_assay(y, ...) + }) + return(x) + } +) - # Check assay.type - .check_assay_present(assay.type, x) - - # Check name - if(!.is_non_empty_string(name) || - name == assay.type){ - stop("'name' must be a non-empty single character value and be ", - "different from `assay.type`.", - call. = FALSE) - } - # Check method - # If method is not single string, user has not specified transform method, - # or has given e.g. a vector - if(!.is_non_empty_string(method)){ - stop("'method' must be a non-empty single character value.", - call. = FALSE) - } - method <- match.arg(method, several.ok = FALSE) - # Check that MARGIN is 1 or 2 - MARGIN <- .check_MARGIN(MARGIN) - # Check pseudocount - if( !.is_a_bool(pseudocount) && !(is.numeric(pseudocount) && length(pseudocount) == 1 && pseudocount >= 0) ){ - stop("'pseudocount' must be TRUE, FALSE or a number equal to or greater than 0.", - call. = FALSE) - } - # Input check end +########################### HELP FUNCTIONS ##################################### - # Get the method and abundance table - method <- match.arg(method) - assay <- assay(x, assay.type) - - # Apply pseudocount, if it is not 0 - assay <- .apply_pseudocount(assay, pseudocount, ...) - # Store pseudocount value and set attr equal to NULL - pseudocount <- attr(assay, "pseudocount") - attr(assay, "pseudocount") <- NULL - - # Calls help function that does the transformation - # Help function is different for mia and vegan transformations - if( method %in% c("log10", "log2") ){ - transformed_table <- .apply_transformation( - assay, method, MARGIN, ...) - } else{ - transformed_table <- .apply_transformation_from_vegan( - assay, method, MARGIN, ...) - } - - # Add pseudocount info to transformed table - attr(transformed_table, "parameters")$pseudocount <- pseudocount - - # Assign transformed table to assays - assay(x, name, withDimnames=FALSE) <- transformed_table - x +############################### .transform_assay ############################### +# A generic functon that takes SE as input and transforms the specified assay +# with specified method. By calling this generic function, we can apply same +# methods for SE and altExps of TreeSE. +.transform_assay <- function( + x, assay.type = "counts", assay_name = NULL, + method = c( + "alr", "chi.square", "clr", "css", "css.fast", "frequency", + "hellinger", "log", "log10", "log2", "max", "normalize", + "pa", "range", "rank", "rclr", "relabundance", "rrank", + "standardize", "total", "z"), + MARGIN = "samples", + name = method, + pseudocount = FALSE, + ...){ + # Input check + if(!is.null(assay_name)){ + .Deprecated(old="assay_name", new="assay.type", "Now assay_name is + deprecated. Use assay.type instead.") + assay.type <- assay_name } -) + # Check assay.type + .check_assay_present(assay.type, x) + # Check name + if(!.is_non_empty_string(name) || name == assay.type){ + stop("'name' must be a non-empty single character value and be ", + "different from `assay.type`.", call. = FALSE) + } + # If method is not single string, user has not specified transform method, + # or has given e.g. a vector + if(!.is_non_empty_string(method)){ + stop("'method' must be a non-empty single character value.", + call. = FALSE) + } + method <- match.arg(method, several.ok = FALSE) + # Check that MARGIN is 1 or 2 + MARGIN <- .check_MARGIN(MARGIN) + # Check pseudocount + if( !.is_a_bool(pseudocount) && !(is.numeric(pseudocount) && + length(pseudocount) == 1 && pseudocount >= 0) ){ + stop("'pseudocount' must be TRUE, FALSE or a number equal to or ", + "greater than 0.", call. = FALSE) + } + # Input check end + # Get the method and abundance table + method <- match.arg(method) + assay <- assay(x, assay.type) + # Apply pseudocount, if it is not 0 or FALSE + assay <- .apply_pseudocount(assay, pseudocount, ...) + # Store pseudocount value and set attr equal to NULL. The function above, + # add the used pseudocount to attributes. + pseudocount <- attr(assay, "pseudocount") + attr(assay, "pseudocount") <- NULL + # Calls help function that does the transformation + # Help function is different for mia and vegan transformations + if( method %in% c("log10", "log2", "css", "css.fast") ){ + transformed_table <- .apply_transformation( + assay, method, MARGIN, ...) + } else { + transformed_table <- .apply_transformation_from_vegan( + assay, method, MARGIN, ...) + } + # Add pseudocount info to transformed table + attr(transformed_table, "parameters")$pseudocount <- pseudocount + # Assign transformed table to assays + assay(x, name, withDimnames = FALSE) <- transformed_table + return(x) +} -###########################HELP FUNCTIONS#################################### ##############################.apply_transformation############################# # Help function for transformAssay, takes abundance table # as input and returns transformed table. This function utilizes mia's # transformation functions. .apply_transformation <- function(assay, method, MARGIN, ...){ - # Transpose if MARGIN is row if( MARGIN == 1L ){ assay <- t(assay) } - # Function is selected based on the "method" variable - FUN <- switch(method, - log10 = .calc_log, - log2 = .calc_log, - ) - + FUN <- switch(method, log10 = .calc_log, log2 = .calc_log) # Get transformed table transformed_table <- do.call( FUN, list(mat = assay, method = method, ...) ) - # Transpose back to normal if MARGIN is row if( MARGIN == 1L ){ transformed_table <- t(transformed_table) } - # Add method and margin to attributes attr(transformed_table, "mia") <- method attr(transformed_table, "parameters")$margin <- MARGIN @@ -240,20 +272,24 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), # Help function for transformAssay, takes abundance # table as input and returns transformed table. This function utilizes vegan's # transformation functions. -.apply_transformation_from_vegan <- function(mat, method, MARGIN, reference = ref_vals, - ref_vals = NA, ...){ +.apply_transformation_from_vegan <- function( + mat, method, MARGIN, reference = ref_vals, ref_vals = NA, ...) { # Input check # Check reference if( length(reference) != 1 ){ stop("'reference' must be a single value specifying the ", - "values of the reference sample.", - call. = FALSE) + "values of the reference sample.", call. = FALSE) } # Input check end - + # Ensure that the matrix has proper dimnames + if (is.null(rownames(mat))) { + rownames(mat) <- paste0("feature", seq_len(nrow(mat))) + } + if (is.null(colnames(mat))) { + colnames(mat) <- paste0("sample", seq_len(ncol(mat))) + } # Adjust method if mia-specific alias was used method <- ifelse(method == "relabundance", "total", method) - if (method == "z") { .Deprecated(old="z", new="standardize") } @@ -265,7 +301,8 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), orig_dimnames <- dimnames(mat) # Call vegan::decostand and apply transformation - transformed_table <- vegan::decostand(mat, method = method, MARGIN = MARGIN, ...) + transformed_table <- vegan::decostand( + mat, method = method, MARGIN = MARGIN, ...) # Add reference sample back if ALR if( method %in% c("alr") ){ @@ -275,9 +312,9 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), } # If table is transposed (like in chi.square), transpose back if(identical(rownames(transformed_table), colnames(mat)) && - identical(colnames(transformed_table), rownames(mat)) && - ncol(transformed_table) != ncol(mat) && - nrow(transformed_table != nrow(mat))){ + identical(colnames(transformed_table), rownames(mat)) && + ncol(transformed_table) != ncol(mat) && + nrow(transformed_table != nrow(mat))){ transformed_table <- t(transformed_table) } return(transformed_table) @@ -286,16 +323,17 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), ####################################.calc_log################################### # This function applies log transformation to abundance table. .calc_log <- function(mat, method, ...){ - # If abundance table contains zeros or negative values, gives an error, because - # it is not possible to calculate log from zeros. Otherwise, calculates log. + # If abundance table contains zeros or negative values, gives an error, + # because it is not possible to calculate log from zeros. Otherwise, + # calculates log. if ( any(mat < 0, na.rm = TRUE) ){ stop("The assay contains negative values and ", method, - " transformation is being applied without pseudocount.", + " transformation is being applied without pseudocount.", "`pseudocount` must be specified manually.", call. = FALSE) } else if ( any(mat == 0, na.rm = TRUE) ){ stop("The assay contains zeroes and ", method, - " transformation is being applied without pseudocount.", - "`pseudocount` must be set to TRUE.", call. = FALSE) + " transformation is being applied without pseudocount.", + "`pseudocount` must be set to TRUE.", call. = FALSE) } # Calculate log2 or log10 abundances if(method == "log2"){ @@ -339,8 +377,8 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), ref_dimnames <- list(var_names, reference_name) } # Reference sample as NAs or with symbols that are specified by user - reference_sample <- matrix(reference, nrow = nrow, ncol = ncol, - dimnames = ref_dimnames) + reference_sample <- matrix( + reference, nrow = nrow, ncol = ncol, dimnames = ref_dimnames) # Add reference sample/feature if(MARGIN == 1){ mat <- rbind(mat, reference_sample) @@ -352,27 +390,29 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), mat <- mat[, orig_names] } # Add those attributes that were related to calculation - attributes(mat) <- c(attributes(mat), - attributes[ !names(attributes) %in% - c("dim", "dimnames") ]) + attributes(mat) <- c( + attributes(mat), + attributes[ !names(attributes) %in% c("dim", "dimnames") ]) return(mat) } + ###############################.apply_pseudocount############################### # This function applies pseudocount to abundance table. .apply_pseudocount <- function(mat, pseudocount, na.rm = TRUE, ...){ if( .is_a_bool(pseudocount) ){ # If pseudocount TRUE and some NAs, a warning is issued if ( pseudocount && any(is.na(mat)) ){ - warning("The assay contains missing values (NAs). These will be - ignored in pseudocount calculation.", call. = FALSE) + warning("The assay contains missing values (NAs). These will be ", + "ignored in pseudocount calculation.", call. = FALSE) } - # If pseudocount TRUE but some negative values, numerical pseudocount needed + # If pseudocount TRUE but some negative values, numerical pseudocount + # needed if ( pseudocount && any(mat < 0, na.rm = TRUE) ){ stop("The assay contains negative values. ", - "'pseudocount' must be specified manually.", call. = FALSE) + "'pseudocount' must be specified manually.", call. = FALSE) } - # If pseudocount TRUE, set it to half of non-zero minimum value + # If pseudocount TRUE, set it to half of non-zero minimum value # else set it to zero. # Get min value value <- min(mat[mat > 0], na.rm = na.rm) diff --git a/man/transformAssay.Rd b/man/transformAssay.Rd index 110b3365b..a479096a0 100644 --- a/man/transformAssay.Rd +++ b/man/transformAssay.Rd @@ -3,37 +3,37 @@ \name{transformAssay} \alias{transformAssay} \alias{transformAssay,SummarizedExperiment-method} +\alias{transformAssay,SingleCellExperiment-method} \title{Transform assay} \usage{ -transformAssay( - x, - assay.type = "counts", - assay_name = NULL, - method = c("alr", "chi.square", "clr", "frequency", "hellinger", "log", "log10", - "log2", "max", "normalize", "pa", "range", "rank", "rclr", "relabundance", "rrank", - "standardize", "total", "z"), - MARGIN = "samples", - name = method, - pseudocount = FALSE, - ... -) +transformAssay(x, ...) \S4method{transformAssay}{SummarizedExperiment}( x, assay.type = "counts", assay_name = NULL, - method = c("alr", "chi.square", "clr", "frequency", "hellinger", "log", "log10", - "log2", "max", "normalize", "pa", "range", "rank", "rclr", "relabundance", "rrank", - "standardize", "total", "z"), + method = c("alr", "chi.square", "clr", "css", "css.fast", "frequency", "hellinger", + "log", "log10", "log2", "max", "normalize", "pa", "range", "rank", "rclr", + "relabundance", "rrank", "standardize", "total", "z"), MARGIN = "samples", name = method, pseudocount = FALSE, ... ) + +\S4method{transformAssay}{SingleCellExperiment}(x, altexp = altExpNames(x), ...) } \arguments{ \item{x}{\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}}.} +\item{...}{additional arguments passed on to \code{vegan:decostand}: +\itemize{ +\item \code{reference}: \code{Character scalar}. use to +to fill reference sample's column in returned assay when calculating alr. +(Default: \code{NA}) +\item \code{ref_vals} Deprecated. Use \code{reference} instead. +}} + \item{assay.type}{\code{Character scalar}. Specifies which assay to use for calculation. (Default: \code{"counts"})} @@ -57,13 +57,10 @@ When FALSE, does not add any pseudocount (pseudocount = 0). Alternatively, a user-specified numeric value can be added as pseudocount. (Default: \code{FALSE}).} -\item{...}{additional arguments passed on to \code{vegan:decostand}: -\itemize{ -\item \code{reference}: \code{Character scalar}. use to -to fill reference sample's column in returned assay when calculating alr. -(Default: \code{NA}) -\item \code{ref_vals} Deprecated. Use \code{reference} instead. -}} +\item{altexp}{\code{Character vector} or \code{NULL}. Specifies the names +of alternative experiments to which the transformation should also be +applied. If \code{NULL}, the transformation is only applied to the main +experiment. (Default: \code{altExpNames(x)}).} } \value{ \code{transformAssay} returns the input object \code{x}, with a new @@ -105,8 +102,8 @@ where \eqn{x} is a single value of data. } } \examples{ -data(esophagus, package="mia") -tse <- esophagus +data(GlobalPatterns) +tse <- GlobalPatterns # By specifying 'method', it is possible to apply different transformations, # e.g. compositional transformation. @@ -143,4 +140,12 @@ head(assay(tse, "rank")) assay(tse, "rank_average", withDimnames = FALSE) <- colRanks( assay(tse, "counts"), ties.method = "average", preserveShape = TRUE) +# Using altexp parameter. First agglomerate the data and then apply +# transformation. +tse <- GlobalPatterns +tse <- agglomerateByRanks(tse) +tse <- transformAssay(tse, method = "relabundance") +# The transformation is applied to all altternative experiments +altExp(tse, "Species") + } diff --git a/tests/testthat/test-5prevalence.R b/tests/testthat/test-5prevalence.R index 7e25fa518..8d977fd59 100644 --- a/tests/testthat/test-5prevalence.R +++ b/tests/testthat/test-5prevalence.R @@ -55,7 +55,9 @@ test_that("getPrevalence", { rownames(gp_null) <- NULL pr1 <- unname(getPrevalence(GlobalPatterns, detection=0.004, as.relative=TRUE)) - pr2 <- getPrevalence(gp_null, detection=0.004, as.relative=TRUE) + # If there are no rownames, getPrevalence returns names "featurex" where + # x denotes the index. + pr2 <- unname(getPrevalence(gp_null, detection=0.004, as.relative=TRUE)) expect_equal(pr1, pr2) pr1 <- getPrevalence(GlobalPatterns, detection=0.004, as.relative=TRUE, rank = "Family") @@ -446,7 +448,7 @@ test_that("agglomerateByPrevalence", { # Get single taxon as reference. Merge those sequences and test that it # equals to one that is output of agglomerateByPrevalence seqs_ref <- referenceSeq(se) - feature <- sample(na.omit(rowData(se)[["Genus"]]), 1) + feature <- sample(na.omit(rowData(actual)[["Genus"]]), 1) seqs_ref <- seqs_ref[ rowData(se)[["Genus"]] %in% feature ] seqs_ref <- .merge_refseq( seqs_ref, factor(rep(feature, length(seqs_ref))), rownames(seqs_ref), diff --git a/tests/testthat/test-5transformCounts.R b/tests/testthat/test-5transformCounts.R index c927fca46..9eb0008fb 100644 --- a/tests/testthat/test-5transformCounts.R +++ b/tests/testthat/test-5transformCounts.R @@ -44,7 +44,7 @@ test_that("transformAssay", { ########################### LOG10 ###################################### # Calculates log10 transformation with pseudocount. Should be equal. - tmp <- mia::transformAssay(tse, method = "log10", pseudocount = 1) + tmp <- mia::transformAssay(tse, method = "log10", pseudocount = 1) ass <- assays(tmp)$log10 expect_equal(as.matrix(ass), @@ -52,7 +52,7 @@ test_that("transformAssay", { log10(x+1) }), check.attributes = FALSE) - # Tests transformAssay(MARGIN = "features"), , calculates log10 transformation with pseudocount. + # Tests transformAssay(MARGIN = "features"), calculates log10 transformation with pseudocount. # Should be equal. tmp <- mia::transformAssay(tse, MARGIN = "features", method = "log10", pseudocount = 1) ass <- assays(tmp)$log10 @@ -76,14 +76,14 @@ test_that("transformAssay", { expect_equal(as.vector(actual), as.integer(as.matrix(assay(tse, "counts")) > 0), check.attributes = FALSE) - expect_equal(type(actual),"double") + expect_equal(typeof(actual),"double") expect_true(all(actual == 1 | actual == 0)) - # Tests transformAssay(MARGIN = "features"), , calculates pa transformation. Should be equal. + # Tests transformAssay(MARGIN = "features"), calculates pa transformation. Should be equal. actual <- assay(mia::transformAssay(tse, MARGIN = "features", method = "pa"),"pa") expect_equal(as.vector(actual), as.integer(t(as.matrix(t(assay(tse, "counts"))) > 0))) - expect_equal(type(actual),"double") + expect_equal(typeof(actual),"double") expect_true(all(actual == 1 | actual == 0)) ######################## HELLINGER ##################################### @@ -113,11 +113,11 @@ test_that("transformAssay", { mat_comp <- apply(as.matrix(relative), 2, FUN=function(x){ log(x) - mean(log(x)) }) - # Remove atributes since vegan adds additional ones + # Remove attributes since vegan adds additional ones attributes(mat) <- NULL attributes(mat_comp) <- NULL # Compare - expect_equal( mat, mat_comp ) + expect_equal(mat, mat_comp) # Tests rclr # Calc RCLRs @@ -174,7 +174,7 @@ test_that("transformAssay", { test2 <- test2[-1, ] # Expect that under 10 values are unequal. Values have only one decimal. - expect_true( sum(round(test, 1) != round(test2, 1), na.rm = TRUE) < 10 ) + expect_true(sum(round(test, 1) != round(test2, 1), na.rm = TRUE) < 10) tse <- transformAssay(tse, method = "relabundance") # Expect error when counts and zeroes @@ -194,8 +194,10 @@ test_that("transformAssay", { test3 <- tmp assay(test3, "na_values") <- assay(test3, "counts") assay(test3, "na_values")[4, 5] <- NA + expect_warning( actual <- transformAssay(test3, method = "relabundance", assay.type = "na_values", pseudocount = TRUE) + ) value <- attr(assay(actual, "relabundance"), "parameters")[["pseudocount"]] ref <- assay(actual, "na_values") ref <- min(ref[ref > 0], na.rm = TRUE)/2 @@ -206,7 +208,7 @@ test_that("transformAssay", { tse <- transformAssay(tse, assay.type = "pseudo", method = "clr", name = "clr1") tse <- transformAssay( tse, assay.type = "counts", method = "clr", name = "clr2", - pseudocount =1) + pseudocount = 1) expect_equal(assay(tse, "clr1"), assay(tse, "clr2"), check.attributes = FALSE) # Same with relabundance @@ -255,9 +257,9 @@ test_that("transformAssay", { # Calculates rank tse_rank <- transformAssay(tse, method = "rank") # Expect that assay contains count and rank table - expect_true( all(c("counts", "rank") %in% assayNames(tse_rank)) ) + expect_true(all(c("counts", "rank") %in% assayNames(tse_rank))) - for( i in c(1:10) ){ + for(i in c(1:10)){ # Gets columns from 'rank' table ranks <- assay(tse_rank, "rank")[,i] # Gets columns from 'counts' table, and calculates ranks @@ -316,7 +318,7 @@ test_that("transformAssay", { pseudocount = 4, reference = 2) # The TreeSE version maintains the row & col number including the reference - coms <- intersect(rownames(actual), rownames(compare)) + coms <- intersect(rownames(actual), rownames(compare)) expect_equal(actual[coms, -2], compare[coms, -2], check.attributes = FALSE) # hellinger @@ -336,7 +338,26 @@ test_that("transformAssay", { MARGIN = 2) compare <- t(compare) expect_equal(na.omit(actual), na.omit(compare)) - + + # Check that transformation is applied to altExps + expect_error(transformAssay(tse, altexp = "Phylum")) + expect_error(transformAssay(tse, altexp = 1)) + expect_error(transformAssay(tse, altexp = TRUE)) + expect_error(transformAssay(tse, altexp = NA)) + expect_error(transformAssay(tse, altexp = c(1, 2, 3))) + expect_error(transformAssay(tse, altexp = character(0))) + expect_warning( + tse <- agglomerateByRanks(tse) + ) + ref <- transformAssay(altExp(tse, "Phylum"), method = "relabundance") + test <- transformAssay(tse, altexp = "Phylum", method = "relabundance") + test <- altExp(test, "Phylum") + expect_equal(assay(test, "relabundance"), assay(ref, "relabundance")) + # + ref <- transformAssay(altExp(tse, "Genus"), method = "relabundance") + test <- transformAssay(tse, method = "relabundance") + test <- altExp(test, "Genus") + expect_equal(assay(test, "relabundance"), assay(ref, "relabundance")) } # TSE object @@ -346,4 +367,3 @@ test_that("transformAssay", { assay(tse,"counts") <- DelayedArray(assay(tse,"counts")) testTransformations(tse) }) -