RGLab / openCyto

A package that provides data analysis pipeline for flow cytometry.
GNU Affero General Public License v3.0
75 stars 29 forks source link

plugin function 'filter' from flowClust package for usage in .csv gating template #120

Closed anaraserikbek closed 8 years ago

anaraserikbek commented 8 years ago

Hi everyone, I have created a Plugin-function filter(), that belongs to flowClust package & needs a tmixFilter(). I've written the function as follows:

.filter <- function(fr, pp_res, channels = NA, ...){
   flowClust:::.filter(fr, channels = channels, ...)}
registerPlugins(fun = .filter, methodName = "filter", dep = "flowClust", "gating")

And, I have written the tmixFilter parameters as such before using filter() in the gating template: filter1=tmixFilter("f1",c("Green-D-610/20-A","Red-A-780/60-A"),K=3) And, my gating template line is as follows: alias pop parent dims gating_method gating_args

However, when i apply this gating_template to my gatinset, i get the following error when the gating reaches this line with my plugin function:

Skip gating! Population 'LT CD4+' already exists.
done.
Gating for '*'
Error in (function (fs, pp_res, gFunc, popAlias, channels, gFunc_args)  : 
  failed at Alcina TregMAIT_AV 10-1974 P1 1.fcs
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : 
  object '.filter' not found

And, when I do traceback, i receive:

12: stop("failed at ", paste0(sn), "\n", filterRes)
11: (function (fs, pp_res, gFunc, popAlias, channels, gFunc_args) 
    {
        require(openCyto)
        sn <- sampleNames(fs)
        fr <- as(fs, "flowFrame")
        total <- nrow(fr)
        subSample <- gFunc_args[["subSample"]]
        gFunc_args[["subSample"]] <- NULL
        if (!is.null(subSample)) {
            if (is.numeric(subSample)) {
                if (subSample > 1) {
                    samp.ind <- sample.int(total, subSample)
                    fr <- fr[samp.ind]
                }
                else if (subSample > 0) {
                    samp.ind <- sample.int(total, subSample * total)
                    fr <- fr[samp.ind]
                }
                else stop("invalid 'subSample' argument: ", subSample)
            }
            else stop("invalid 'subSample' argument: ", subSample)
        }
        openCyto.options <- getOption("openCyto")
        minEvents <- openCyto.options[["gating"]][["minEvents"]]
        if (is.null(minEvents)) 
            minEvents <- 0
        if (nrow(fr) <= minEvents) {
            warning(paste(sn, collapse = ","), ": Not enough events to proceed the data-driven gating!Returning a dummy gate instead.")
            channels <- as.vector(na.omit(channels))
            nDim <- length(channels)
            if (nDim == 1) 
                gate_coordinates <- list(c(-Inf, -Inf))
            else if (nDim == 2) 
                gate_coordinates <- list(c(-Inf, -Inf), c(-Inf, -Inf))
            else stop(nDim, " dimensional gating is not supported yet!")
            names(gate_coordinates) <- channels
            filterRes <- rectangleGate(gate_coordinates)
            if (grepl("flowClust\\.[12]d", gFunc)) 
                filterRes <- fcRectangleGate(filterRes, priors = list(), 
                    posts = list())
            nPop <- length(popAlias)
            filterResType <- ifelse(nPop == 1, "filter", "filters")
            if (filterResType == "filters") {
                filterRes <- filters(lapply(1:nPop, function(i) filterRes))
            }
        }
        else {
            if (!.isRegistered(gFunc)) {
                stop(sprintf("Can't gate using unregistered method %s", 
                    gFunc))
            }
            thisCall <- substitute(f(fr = fr, pp_res = pp_res, channels = channels), 
                list(f = as.symbol(gFunc)))
            filterRes <- try(do.call(gFunc, c(list(fr = fr, pp_res = pp_res, 
                channels = channels), gFunc_args)), silent = TRUE)
        }
        if (extends(class(filterRes), "filter") || extends(class(filterRes), 
            "filters")) {
            list(sapply(sampleNames(fs), function(i) filterRes, simplify = FALSE))
        }
        else {
            stop("failed at ", paste0(sn), "\n", filterRes)
        }
    })(dots[[1L]][[1L]], dots[[2L]][[1L]], gFunc = ".filter", popAlias = "*", 
        channels = c("Green-E-582/15-A", "Violet-C-670/30-A"), gFunc_args = list(
            filter1, K = 3))
10: mapply(list(`Alcina TregMAIT_AV 10-1974 P1 1.fcs` = <S4 object of class "flowSet">, 
        `Alcina TregMAIT_AV 10-1974 P1.fcs` = <S4 object of class "flowSet">), 
        list(`Alcina TregMAIT_AV 10-1974 P1 1.fcs` = NULL, `Alcina TregMAIT_AV 10-1974 P1.fcs` = NULL), 
        FUN = .gating_adaptor, MoreArgs = list(gFunc = ".filter", 
            popAlias = "*", channels = c("Green-E-582/15-A", "Violet-C-670/30-A"
            ), gFunc_args = list(filter1, K = 3)))
9: eval(expr, envir, enclos)
8: eval(thisCall)
7: .gating_gtMethod(x, y, ...)
6: gating(x = this_gate, y, parent = parent, gtPop = gt_node_pop, 
       pp_res = pp_res, ...)
5: gating(x = this_gate, y, parent = parent, gtPop = gt_node_pop, 
       pp_res = pp_res, ...)
4: .gating_gatingTemplate(x, y, env_fct, ...)
3: .local(x, y, ...)
2: gating(gating_template, gs)
1: gating(gating_template, gs)

I wonder if this is due to the fact that filter() needs tmixFilter for specification, because in filtering 1 FCS file, the following works:

filter1=tmixFilter("f1",c("Green-D-610/20-A","Red-A-780/60-A"),K=3)
filterresults=filter(ncfs_trans[[1]],filter1)
plot(ncfs_trans[[1]],filterresults,level=1)
summary(filterresults)
mikejiang commented 8 years ago

This is not related openCyto , but I'd still like to point it out:

Also, I'd like to suggest not to create the new issue for the same problem repeatedly. See #118