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

Batch transformations. #429 #618

Open
wants to merge 29 commits into
base: devel
Choose a base branch
from
Open

Batch transformations. #429 #618

wants to merge 29 commits into from

Conversation

ake123
Copy link
Collaborator

@ake123 ake123 commented Jul 30, 2024

Batch transformations. #429

Copy link
Contributor

@TuomasBorman TuomasBorman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too complex, instead you could consider own method for SingleCellExperiment. Something like this

SCE_method <- function(x, altexp = altExpNames(tse), ...){
# Check that specified altexps are found from x
# Check that assay.type are found from specified altexps
lapply(altExps(x)[altexp], .check_assay_present)
# Apply transformation to x
x <- .perform_transformation(x)
# If altexp is not NULL, apply transformation to altexps
altExps(x)[altexp] <- bplapply(altExps(x)[altexp], ...)
}

SE_method <- function(){
# Input checks
# Apply transformation
x <- .perform_transformation(x)
}

@antagomir
Copy link
Member

I agree that direct support for SCE is probably the way to go when altExps are supported.

This in the above example mixes concepts: altexp = reducedDimanames(tse)? Should it be altexp = altExpNames(tse)?

@TuomasBorman
Copy link
Contributor

I agree that direct support for SCE is probably the way to go when altExps are supported.

This in the above example mixes concepts: altexp = reducedDimanames(tse)? Should it be altexp = altExpNames(tse)?

Yep, I fixed the comment

Copy link

codecov bot commented Aug 18, 2024

Codecov Report

Attention: Patch coverage is 62.50000% with 12 lines in your changes missing coverage. Please review.

Please upload report for BASE (devel@dcaa110). Learn more about missing BASE report.

Files Patch % Lines
R/transformCounts.R 62.50% 12 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             devel     #618   +/-   ##
========================================
  Coverage         ?   67.50%           
========================================
  Files            ?       44           
  Lines            ?     5364           
  Branches         ?        0           
========================================
  Hits             ?     3621           
  Misses           ?     1743           
  Partials         ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@antagomir
Copy link
Member

Status of this one?

@ake123
Copy link
Collaborator Author

ake123 commented Aug 28, 2024

rewriting and debugging it as suggested

R/transformCounts.R Outdated Show resolved Hide resolved
R/transformCounts.R Outdated Show resolved Hide resolved
@TuomasBorman
Copy link
Contributor

TuomasBorman commented Sep 1, 2024

Can you revert the already made modifications? Something like this creates a good start point

#' @rdname transformAssay
#' @export
setGeneric("transformAssay", signature = c("x"),
    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", "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)
    }
)

.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)
    }
    # 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
    #
    # 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", "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
    x
}

#' @rdname transformAssay
#' @export
setMethod("transformAssay", signature = c(x = "SingleCellExperiment"),
    function(x, altexp = altExpNames(x), ...){
        # Check altexp
        if( !(is.null(altexp) || (!is.null(altexp) && all(altexp %in% altExpNames(x)))) ){
            stop(".", call. = FALSE)
        }
        browser()
        # Transform the main object
        x <- .transform_assay(x, ...)
        altExps(x)[altexp] <- lapply(altExps(x)[altexp], function(y){
            .transform_assay(y, ...)
        })
        return(x)
    }
)

Copy link
Contributor

@TuomasBorman TuomasBorman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you still add unit tests, that altexp works as expected?

Comment on lines 117 to 121
#' #using altexp parameter
#' data("GlobalPatterns", package="mia")
#' tse <- GlobalPatterns
#' altExp(tse,"species") <- transformAssay(tse, method = "relabundance",rank = "species", na.rm = FALSE)
#' altExp(tse,"species")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space after #

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the changes, see those

#' #using altexp parameter
#' data("GlobalPatterns", package="mia")
#' tse <- GlobalPatterns
#' altExp(tse,"species") <- transformAssay(tse, method = "relabundance",rank = "species", na.rm = FALSE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no rank parameter in transformAssay()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the changes

Comment on lines 244 to 253
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)
ref_vals = NA, ...) {
# Ensure that the matrix has proper dimnames
if (is.null(rownames(mat))) {
rownames(mat) <- paste0("feature", seq_len(nrow(mat)))
}
# Input check end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this was removed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants