Friday, May 25, 2007

Interactive Gating with flowCore

One of the biggest hurdles for flowCore adoption is probably the lack of interactive gating. People really like it and it can be useful even to us statistician types that are suspicious of the entire exercise. R and flowCore, unfortunately, aren't particularly good at interactive graphics so this presents something of a problem.

To help with this, here's a quick function for getting a 2D polygon gate with an optional transform:


interactiveGate = function(fcs,x,y,filterId="Picked", trans=NULL,...) {
if(is.function(trans)) {
tnf = structure(list(trans,trans),names=c(x,y))
trans = do.call("transform",tnf)
}
plot(exprs(if(is.null(trans)) fcs else trans %on% fcs)[,c(x,y)],
pch=20,cex=.5,main=filterId,...)
points = locator(type="n")
polygon(points)
points = cbind(points$x,points$y)
colnames(points) = c(x,y)
r = polygonGate(filterId,points)
if(is.null(trans)) r else r %on% trans
}


The fcs parameter is a flowFrame and the x and y arguments should be pretty obvious. The trans argument can be one of three things: NULL, a function, or a transform object. If it is a transform object (created by transform(...)) then it is simply applied. If it's a function, a transform will be created for both parameters and also applied to the subsequent gate.

e.g.


interactiveGate(fcs,"APC-Cy7-A","PacOrange-565-A","Control",function(x) asinh(x/32))


Just a little thing, but pretty useful.