RGLab / flowWorkspace

flowWorkspace
GNU Affero General Public License v3.0
44 stars 21 forks source link

gs_pop_get_gate with inverse.transform #353

Closed Close-your-eyes closed 3 years ago

Close-your-eyes commented 3 years ago

Dear flowWorkspace team,

is there a way to pull out the gate coordinates from gates set with FlowJo (using gs_pop_get_gate) with inverse.transform set to TRUE? Like expression values can be retrieved in the original FlowJo scale (gs_get_singlecell_expression_by_gate with inverse.transform = T) I expected to be able to also get the gate coordinates in the original FlowJo scale. But apparently I could not figure out how to do it.

When parsing the workspace with flowjo_to_gatingset I tried (i) to set inverse.transform = T and (ii) transform = F, is_gating = F but when applying gs_pop_get_gate to the resulting gatingSet the coordinates are always returned as if inverse.transform = F.

Thanks you.

gfinak commented 3 years ago

You can retrieve the inverse transform and apply it to the gate coordinates but there is an important subtlety here so I really don't recommend it, at least not without better understanding your context. flowJo doesn't really apply the gates on the raw data scale. They actually transform the axes for visualization as you know, so the relationship between the gate coordinates is non linear. If you were to take the gates on the raw scale and apply it to the data on the raw scale you won't be gating the right cells. Ggcyto does all this for you automatically. If you could provide more context as to why and what you're aiming to do I can perhaps point you in the right direction.

Close-your-eyes commented 3 years ago

Okay, so, I wanted to have complete freedom to create plots with ggplot. Thats why I wanted the raw data whose scale is also more familiar to many scientists. The transformed data will be okay for most purposes. I will check out again ggcyto. Thank your. How to apply the inverse.transform (or the other way around) manually I could not find out yet, though.

gfinak commented 3 years ago

Do have a look at ggcyto, it already does probably all of what you need, especially the familiar scales. I will have to get back to you on the manual inverse transform. I'm on mobile and don't want to lead you astray.

mikejiang commented 3 years ago

Here is the doc on transform argument (probably I should drop and data part)

> ?flowjo_to_gatingset
...
transform    logical to enable/disable transformation of gates and data. 
                  Default is TRUE. It is mainly for debug purpose (when the raw gates need to be parsed.),
                  and only valid when execute is FALSE.

So basically it is used together with execute = FALSE, i.e. Parsing xml without loading FCS, see doc. If all the gates in your workspace are defined as polygon or rectangle, you should be able to get raw gate that way, but data is not loaded into gs, you will have to manually load fcs into cytoset and used ggcyto to plot geom_gate layers on top of the cs. But if you have ellipse gate, it won't work, since flowJo writes out ellipse definition differently (i.e. in transformed scale instead of raw scale as polygon, due to the reason @gfinak mentioned)

As @gfinak said, it is better to let ggcyto take care of axis/scale. e.g. try 'axis_inverse_trans' layer

mikejiang commented 3 years ago

Regarding to inverse gate scale, again, it is not always applicable (e.g. ellipse gate ), so we don't have that option available for gs_pop_get_gate API. If you really want to manually endeavor this (for polygonGate), here is the example

> dataDir <- system.file("extdata",package="flowWorkspaceData")
> wsfile <- list.files(dataDir, pattern="manual.xml",full=TRUE)
> gs <- flowjo_to_gatingset(ws, name = 4, subset = 1)
> g <- gs_pop_get_gate(gs, "CD4/38+ DR-")[[1]]
> g@boundaries
       <R660-A>   <V545-A>
[1,]   1892.233    1968.89
[2,]   1892.233 -279569.10
[3,] 128144.432 -279569.10
[4,] 128144.432    1968.89
> trans <- gh_get_transformations(gs[[1]], inverse = T)
> ch <- "<R660-A>"
> g@boundaries[, ch] <- trans[[ch]](g@boundaries[, ch])
> g@boundaries
         <R660-A>   <V545-A>
[1,]     1250.027    1968.89
[2,]     1250.027 -279569.10
[3,] 82094871.977 -279569.10
[4,] 82094871.977    1968.89
Close-your-eyes commented 3 years ago

So, I began to try ggcyto a little more dedicated then before. It's awesome. Thank you.