[BioC] Gating with an ellipsoidGate

Josef Spidlen jspidlen at bccrc.ca
Wed Sep 15 20:52:10 CEST 2010

Hi Roger,
your calculation of the covariance matrix seems fine to me.
All seems working fine if your data is on linear axis. I tried the
following:

library(flowCore)
library(flowViz)
cov <- cov.matrix(150, 50, 45)
mean <- c(200, 100)
names(mean) <- colnames(x)[1:2]
colnames(cov) <- colnames(x)[1:2]
rownames(cov) <- colnames(x)[1:2]
cells <- ellipsoidGate(filterId="CellGate", .gate=cov, mean=mean)
xyplot(`FSC-H` ~ `SSC-H`, x, filter=cells)

http://flowcyt.sf.net/temp/A01.fcs
and cov.matrix is from your code, namely:

cov.matrix <- function (a, b, angle) {
theta <- angle * (pi/180)
c1 <- ((cos(theta)^2)/a^2) + ((sin(theta)^2)/b^2)
c2 <- sin(theta) * cos(theta) * ((1/a^2) - (1/b^2))
c3 <- ((sin(theta)^2)/a^2) + ((cos(theta)^2)/b^2)
m1 <- matrix(c(c1, c2, c2, c3), byrow=TRUE, ncol=2)
m2 <- solve(m1)
m2
}

This is working well for different angles and - by-eye - the rotation of
the ellipse corresponds to the one requested.

I am thinking whether your issue is a bug in flowCore/flowViz or just an
artifact of having completely different scales for x and y. What I mean
is that your ellipse is essentially 50,000x longer in the x dimension
than in y dimension and flowViz "adjusts" for that in the display -
scaling x and y completely different. If you rotate it even only a tiny
bit, everything changes drastically and the new ellipse now needs a
completely different scales to show up reasonably.

My feeling is that rotating an ellipse in a space that consist of one
linear and one log dimension may not be very meaningful and may create
weird artifacts. Could you maybe scale the data somehow so that x/y get
on similar value range and try if that would work

Cheers,
Josef

On 10-09-15 04:37 AM, Roger Leigh wrote:
> On 15/09/2010 12:24, Roger Leigh wrote:
>
>> This appears to work well when rotation is not involved. However, as
>> soon as I try to add rotation, it blows up:
>>
>> library(flowCore)
>> library(flowViz)
>> library(flowUtils)
>>
>> cov.matrix<- function (a, b, angle) {
>> theta<- angle * (pi/180)
>>
>> c1<- ((cos(theta)^2)/a^2) + ((sin(theta)^2)/b^2)
>> c2<- sin(theta) * cos(theta) * ((1/a^2) - (1/b^2))
>> c3<- ((sin(theta)^2)/a^2) + ((cos(theta)^2)/b^2)
>>
>> m1<- matrix(c(c1, c2, c2, c3), byrow=TRUE, ncol=2)
>> m2<- solve(m1)
>>
>> m2
>> }
>>
>> d<- transform(d, `SS.Log` = log10(`SS.Log`))
>>
>> # No rotation
>> cov<- cov.matrix(20000, 0.4, 0)
>> colnames(cov)<- c("FS.Lin", "SS.Log")
>> rownames(cov)<- c("FS.Lin", "SS.Log")
>> mean<- c("FS.Lin"=40000, "SS.Log"=2.8)
>> cells<- ellipsoidGate(filterId="CellGate", .gate=cov, mean=mean)
>> print(cov)
>> pdf("test.pdf", width=8, height=8, pointsize=12)
>> print(xyplot(`SS.Log` ~ `FS.Lin`, d, filter=cells, xlab="FS",
>> ylab=expression(log~(SS))))
>> dev.off()
>>
>> # Now repeat with a 20 degree rotation...
>> cov<- cov.matrix(20000, 0.4, 20)
>> colnames(cov)<- c("FS.Lin", "SS.Log")
>> rownames(cov)<- c("FS.Lin", "SS.Log")
>> mean<- c("FS.Lin"=40000, "SS.Log"=2.8)
>> cells<- ellipsoidGate(filterId="CellGate", .gate=cov, mean=mean)
>> print(cov)
>>
>> pdf("test-rotate.pdf", width=8, height=8, pointsize=12)
>> print(xyplot(`SS.Log` ~ `FS.Lin`, d, filter=cells, xlab="FS",
>> ylab=expression(log~(SS))))
>> dev.off()
>>
>> I've put the source data, script and results at:
>> http://www-users.york.ac.uk/~rl522/flowcore-test/
>>
>> http://www-users.york.ac.uk/~rl522/flowcore-test/test.pdf
>> http://www-users.york.ac.uk/~rl522/flowcore-test/test-rotate.pdf
>>
>> You can see that while an ellipse is drawn correctly in the first
>> instance, when we add a 20 degree rotation, it's completely screwed. I'm
>> not sure if this is due to the extreme differences in the x and y
>> dimensions, which alter the length of a and b, or for some other reason.
>>
> cov<- cov.matrix(20000, 0.4, 20)
>
> While this gives what looks like an almost entirely vertical line, using
> a much smaller angle
>
> cov<- cov.matrix(20000, .25, 0.0005)
>
> actually results in a sensible rotation, of about 20 degrees.  So it
> looks like it works, but the scaling of the angle is wrong--it's far too
> sensitive.  I'm correctly converting from degrees to radians, so I'm not
> sure why 8.7e-6 radians actually results in a rotation of ~0.35 radians!
>
>
> Regards,
> Roger
>
>

--
Josef Spidlen, Ph.D.
Terry Fox Laboratory, BC Cancer Agency
675 West 10th Avenue, V5Z 1L3 Vancouver, BC, Canada

Tel: +1 (604) 675-8000 x 7755
http://www.terryfoxlab.ca/people/rbrinkman/josef.aspx