From cd0fe470129d4ba0f0c8f463b02db54158760722 Mon Sep 17 00:00:00 2001 From: ake123 Date: Tue, 30 Jul 2024 17:32:03 +0300 Subject: [PATCH] Batch transformations. #429 --- R/transformCounts.R | 80 ++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/R/transformCounts.R b/R/transformCounts.R index cd7bcf737..7134394a7 100644 --- a/R/transformCounts.R +++ b/R/transformCounts.R @@ -122,7 +122,8 @@ setGeneric("transformAssay", signature = c("x"), "z"), MARGIN = "samples", name = method, - pseudocount = FALSE, + pseudocount = FALSE, + altexp = NULL, ...) standardGeneric("transformAssay")) @@ -138,6 +139,7 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), MARGIN = "samples", name = method, pseudocount = FALSE, + altexp = NULL, ...){ # Input check @@ -173,32 +175,36 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), } # Input check end - # Get the method and abundance table - method <- match.arg(method) - assay <- assay(x, assay.type) + # List of available altExps + available_altexp <- altExpNames(x) - # 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, ...) + # Create missing altexp assays + if (!is.null(altexp)) { + for (alt in altexp) { + if (!(alt %in% available_altexp)) { + # Create a new altExp assay with the same dimensions as the main assay + altExp(x, alt) <- SummarizedExperiment( + assays = SimpleList(counts = matrix(NA, nrow = nrow(x), ncol = ncol(x))), + rowData = rowData(x), + colData = colData(x) + ) + } + } } - # Add pseudocount info to transformed table - attr(transformed_table, "parameters")$pseudocount <- pseudocount + # Perform transformation on the main assay + x <- .perform_transformation(x, assay.type, method, MARGIN, pseudocount, name, ...) + + # Perform transformations on alternative experiments if specified + if (!is.null(altexp) && length(altexp) > 0) { + for (alt in altexp) { + alt_exp <- altExp(x, alt) + transformed_alt_exp <- .perform_transformation(alt_exp, assay.type, method, MARGIN, pseudocount, name, ...) + altExp(x, alt) <- transformed_alt_exp + } + } - # Assign transformed table to assays - assay(x, name, withDimnames=FALSE) <- transformed_table - x + return(x) } ) @@ -397,3 +403,31 @@ setMethod("transformAssay", signature = c(x = "SummarizedExperiment"), attr(mat, "pseudocount") <- pseudocount return(mat) } + +.perform_transformation <- function(x, assay.type, method, MARGIN, pseudocount, name, ...) { + # Check that the assay is present + .check_assay_present(assay.type, x) + + # Get the assay data + assay <- assay(x, assay.type) + + # Apply pseudocount + assay <- .apply_pseudocount(assay, pseudocount) + pseudocount_value <- attr(assay, "pseudocount") + attr(assay, "pseudocount") <- NULL + + # Apply transformation + 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_value + + # Assign transformed table to assays + assay(x, name, withDimnames=FALSE) <- transformed_table + + return(x) +} \ No newline at end of file