R/correctUnmix.R

Defines functions correctUnmix

Documented in correctUnmix

#' Correct defects in spectral unmixing by compensation
#'
#'
#' This function provides a way to reduce the defects in the spectral unmixing,
#' by creating a secondary correction matrix, which is symmetrical.
#' @param unmixFlowObj A flowframe or flowset post unmixing.
#' @param correcMat A correction matrix. If this is the first round, the
#' executionof this function needs to be preceeded by the generation of this
#' matrix, for example by using the \code{\link{correcMatCreate}} function.
#' @param transCoFacs If transformation should be performed, the transformation
#' cofactors can be added here. Three possible inputs: a vector with specific
#' cofactors for each variable, a set value that will be used for all variables,
#' and FALSE.
#' Note: It might be good to set this to FALSE in the final round, to optimize
#' the transoformations externally. See \code{\link{arcTrans}}.
#' @seealso \code{\link{specUnmix}}, \code{\link{arcTrans}}
#' @examples
#' # Load some data
#' data(fullPanel)
#'
#' # Load a spectral matrix
#' data(specMat)
#'
#' # Select the rows that should be compensated
#' fluoExprs <- fullPanel[, grepl("[VBRY]", colnames(fullPanel))]
#'
#' # Unmix this dataset
#' dataUnmixed <- specUnmix(fluoExprs, specMat)
#'
#' # Now correct the data with this.
#' correcData <- correctUnmix(dataUnmixed)
#'
#' # In this first run, an empty correction matrix will be created,
#' # but in later iterations, the correcMat can be changed to include the
#' # corrections needed.
#'
#' # For example, Qdot705 is "overcompensated" to AF700 in the spectral unmixing
#' # process. For this reason, a correction is included:
#' correcMat["Qdot705", "AF700"] <- 0.1
#'
#' # And now, a new correction is made
#' correcData <- correctUnmix(dataUnmixed, correcMat)
#'
#' # And so on, and so forth.
#' @export correctUnmix
correctUnmix <- function(unmixFlowObj, correcMat, transCoFacs = 200) {

    corrUnmixFlowObj <- specUnmix(unmixFlowObj, correcMat)

    if (is.logical(transCoFacs) && transCoFacs == FALSE) {
        return(corrUnmixFlowObj)
    } else if(class(transCoFacs) == "numeric"){
        if(length(transCoFacs) == 1){
            transCoFacs <- rep(transCoFacs, ncol(correcMat))
        }
        transUnmixFlowObj <- arcTrans(flowObj = corrUnmixFlowObj,
                                      transNames = colnames(correcMat),
                                      transCoFacs = transCoFacs)
        return(transUnmixFlowObj)
    }
}
jtheorell/theFlowSpec documentation built on Aug. 22, 2019, 3:33 a.m.