From e8921d52c870f36a905bbe0c4cee0418c55797fe Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Wed, 26 Mar 2014 11:58:46 -0700 Subject: [PATCH] Fix some small bugs that snuck into COMPASSContainerFromGatingSet, start optimizing re: #29 --- R/GatingSetToCOMPASS.R | 4 ++-- R/GetThresholdedIntensities.R | 9 ++++----- examples/GetThresholdedIntensities.R | 24 ++++++++++++++++++++++-- man/GetThresholdedIntensities.Rd | 24 ++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/R/GatingSetToCOMPASS.R b/R/GatingSetToCOMPASS.R index f381372..71dd21a 100644 --- a/R/GatingSetToCOMPASS.R +++ b/R/GatingSetToCOMPASS.R @@ -283,11 +283,11 @@ COMPASSContainerFromGatingSet <- function(gs = NULL, node = NULL, filter.fun = N map <- map[markers, ] } message("We will map the following nodes to markers:") - kable(map) + kable(as.data.frame(map)) # construct the map - mp <- map[, desc] + mp <- as.character(map[, desc]) names(mp) <- map[, node] mp <- as.list(mp) } diff --git a/R/GetThresholdedIntensities.R b/R/GetThresholdedIntensities.R index 3b85ab3..b6cb31c 100644 --- a/R/GetThresholdedIntensities.R +++ b/R/GetThresholdedIntensities.R @@ -98,8 +98,7 @@ GetThresholdedIntensities <- function(gs, node, map) { ## Try to guess whether we should be pulling names from the 'desc' ## column or the 'name' column of the flowSets - dat <- flowWorkspace::getData(gs, use.exprs=FALSE) - ff <- get( objects(dat@frames)[[1]], envir=dat@frames ) + ff <- flowWorkspace::getData(gs[[1]], use.exprs=FALSE) params <- parameters(ff)@data ## First, check for a perfect match using a basic regex @@ -151,9 +150,9 @@ GetThresholdedIntensities <- function(gs, node, map) { } else { node_path <- file.path(path, cNode) } - gate <- flowWorkspace::getGate(x, node_path) - thresh <- gate@min - exprs[, expr_nms[i]][ exprs[, expr_nms[i]] < thresh ] <- 0 + ## find out what cells didn't fall into the gate + ind <- !flowWorkspace::getIndices(x, node_path) + exprs[ind, expr_nms[i]] <- 0 } exprs <- exprs[ rowSums(exprs) > 0, , drop=FALSE] return(exprs) diff --git a/examples/GetThresholdedIntensities.R b/examples/GetThresholdedIntensities.R index 2c75910..02cbef6 100644 --- a/examples/GetThresholdedIntensities.R +++ b/examples/GetThresholdedIntensities.R @@ -28,13 +28,25 @@ if (require(flowWorkspace)) { fs <- flowSet( lapply(data, flowFrame) ) gs <- GatingSet(fs) + ## Add some dummy metadata + meta <- pData(gs) + meta$PTID <- 1:10 + pData(gs) <- meta + + gate <- rectangleGate( list(TNFa=c(-Inf,Inf))) + add(gs, gate, parent="root", name="dummy") + + ## Add dummy gate + ## Make some gates, and apply them invisible(lapply(marker_names, function(marker) { .gate <- setNames( list( c( rexp(1, runif(1, 1E-5, 1E-3)), Inf) ), marker ) gate <- rectangleGate(.gate=.gate) - add(gs, gate, parent="root", name=paste0(marker, "+")) + add(gs, gate, parent="dummy", name=paste0(marker, "+")) })) + recompute(gs) + ## Map node names to channel names map=list( "TNFa+"="TNFa", @@ -44,7 +56,15 @@ if (require(flowWorkspace)) { ) ## Pull out the data as a COMPASS-friendly dataset - output <- GetThresholdedIntensities(gs, "root", map) + node <- "dummy" + map <- map + system.time( + output <- GetThresholdedIntensities(gs, "dummy", map) + ) + + system.time( + output <- COMPASSContainerFromGatingSet(gs, "dummy", individual_id="PTID", sample_id="name") + ) str(output) } diff --git a/man/GetThresholdedIntensities.Rd b/man/GetThresholdedIntensities.Rd index 1cad982..467d37c 100644 --- a/man/GetThresholdedIntensities.Rd +++ b/man/GetThresholdedIntensities.Rd @@ -61,13 +61,25 @@ if (require(flowWorkspace)) { fs <- flowSet( lapply(data, flowFrame) ) gs <- GatingSet(fs) + ## Add some dummy metadata + meta <- pData(gs) + meta$PTID <- 1:10 + pData(gs) <- meta + + gate <- rectangleGate( list(TNFa=c(-Inf,Inf))) + add(gs, gate, parent="root", name="dummy") + + ## Add dummy gate + ## Make some gates, and apply them invisible(lapply(marker_names, function(marker) { .gate <- setNames( list( c( rexp(1, runif(1, 1E-5, 1E-3)), Inf) ), marker ) gate <- rectangleGate(.gate=.gate) - add(gs, gate, parent="root", name=paste0(marker, "+")) + add(gs, gate, parent="dummy", name=paste0(marker, "+")) })) + recompute(gs) + ## Map node names to channel names map=list( "TNFa+"="TNFa", @@ -77,7 +89,15 @@ if (require(flowWorkspace)) { ) ## Pull out the data as a COMPASS-friendly dataset - output <- GetThresholdedIntensities(gs, "root", map) + node <- "dummy" + map <- map + system.time( + output <- GetThresholdedIntensities(gs, "dummy", map) + ) + + system.time( + output <- COMPASSContainerFromGatingSet(gs, "dummy", individual_id="PTID", sample_id="name") + ) str(output) }