RGLab / flowWorkspace

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

Custom transform doesn't invert with ggcyto::axis_x_inverse_trans() #396

Open hrj21 opened 8 months ago

hrj21 commented 8 months ago

Hello!

I think this is probably my misunderstanding rather than a bug, but while I can successfully define a new transform that can be applied to a gatingSet, I cannot get ggcyto::axis_x_inverse_trans() to invert it (I think the issue is probably with how I have defined the transform rather than anything to do with ggcyto). Below is a reprex

# Load packages -----------------------------------------------------------
library(flowWorkspace) # for cytoset and gatingset classes
library(openCyto)      # for automated gating 
library(ggcyto)        # for ggplot wrappers
library(scales)        # for creating custom transformations

# Load data and create gatingSet ------------------------------------------
fcs_path  <- system.file("extdata", package = "flowWorkspaceData")
fcs_files <- list.files(pattern = "CytoTrol", fcs_path, full = TRUE)
cs        <- load_cytoset_from_fcs(fcs_files)
comp_mat  <- spillover(cs[[1]])$SPILL
compensate(cs, comp_mat)
gs <- GatingSet(cs)

# Define new trans object -------------------------------------------------
asinhTransform <- function(transformationId = "defaultAsinhTransform", cofactor = 5) {
  t <- new("transform", .Data = function(x) x <- asinh(x / cofactor)) 
  t@transformationId <- transformationId 
  t
}

sinhTransform <- function(transformationId = "defaultSinhTransform", cofactor = 5) {
  t <- new("transform", .Data = function(x) x <- sinh(x) * cofactor) 
  t@transformationId <- transformationId 
  t
}

simple_asinh <- function (...) {
  trans.obj <- asinhTransform(...)
  trans <- trans.obj@.Data
  inv <- sinhTransform(...)@.Data
  trans_new(name = "a_sinh", transform = trans, inverse = inv)
}

# Transform data ----------------------------------------------------------
trans        <- simple_asinh(cofactor = 500)
to_transform <- colnames(gs)[5:11]
trans_list   <- transformerList(to_transform, trans)
gs           <- transform(gs, trans_list)

ggcyto(gs[[1]], subset = "root", aes("B710-A", "R780-A")) +
  geom_hex(bins = 100) +
  axis_x_inverse_trans() +
  axis_y_inverse_trans()

As you can see, the axes still display the transformed values, not the original ones. What is it I've done wrong here?

image

Many thanks for your help and an excellent ecosystem of packages. Hefin

> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] scales_1.2.1             ggcyto_1.22.0            ncdfFlow_2.40.0         
[4] BH_1.81.0-1              RcppArmadillo_0.12.6.6.1 flowCore_2.6.0          
[7] ggplot2_3.4.4            openCyto_2.6.0           flowWorkspace_4.6.0     

loaded via a namespace (and not attached):
 [1] bitops_1.0-7        matrixStats_1.2.0   RColorBrewer_1.1-3  httr_1.4.7         
 [5] Rgraphviz_2.38.0    tools_4.1.0         utf8_1.2.4          R6_2.5.1           
 [9] KernSmooth_2.23-22  DBI_1.2.0           BiocGenerics_0.40.0 colorspace_2.1-0   
[13] withr_2.5.2         gridExtra_2.3       tidyselect_1.2.0    mnormt_2.1.1       
[17] curl_5.2.0          compiler_4.1.0      graph_1.72.0        cli_3.6.2          
[21] Biobase_2.54.0      flowClust_3.32.0    xml2_1.3.6          labeling_0.4.3     
[25] flowStats_4.6.0     DEoptimR_1.1-3      hexbin_1.28.3       mvtnorm_1.2-4      
[29] robustbase_0.99-1   RBGL_1.70.0         digest_0.6.33       rainbow_3.7        
[33] R.utils_2.12.3      rrcov_1.7-4         base64enc_0.1-3     jpeg_0.1-10        
[37] pkgconfig_2.0.3     rlang_1.1.2         rstudioapi_0.15.0   farver_2.1.1       
[41] generics_0.1.3      mclust_6.0.1        gtools_3.9.5        dplyr_1.1.4        
[45] R.oo_1.25.0         RCurl_1.98-1.12     magrittr_2.0.3      RProtoBufLib_2.6.0 
[49] interp_1.1-5        Matrix_1.6-4        Rcpp_1.0.11         munsell_0.5.0      
[53] S4Vectors_0.32.4    fansi_1.0.6         lifecycle_1.0.4     R.methodsS3_1.8.2  
[57] MASS_7.3-60         zlibbioc_1.40.0     plyr_1.8.9          grid_4.1.0         
[61] parallel_4.1.0      deldir_2.0-2        lattice_0.22-5      splines_4.1.0      
[65] pillar_1.9.0        fda_6.1.4           corpcor_1.6.10      stats4_4.1.0       
[69] XML_3.99-0.14       glue_1.6.2          latticeExtra_0.6-30 data.table_1.14.10 
[73] RcppParallel_5.1.7  deSolve_1.40        png_0.1-8           vctrs_0.6.5        
[77] gtable_0.3.4        aws.s3_0.3.21       clue_0.3-65         ks_1.14.1          
[81] fds_1.8             pracma_2.4.4        IDPmisc_1.1.20      pcaPP_2.0-4        
[85] tibble_3.2.1        cytolib_2.6.2       aws.signature_0.6.0 flowViz_1.58.0     
[89] ellipse_0.5.0       cluster_2.1.6       hdrcde_3.4     
mikejiang commented 8 months ago

try this

p = ggcyto(gs[[1]], subset = "root", aes("B710-A", "R780-A")) + geom_hex(bins = 100) 
p + axis_x_inverse_trans() + axis_y_inverse_trans()

Not sure why it has to be this way, maybe the latest ggplot changes its behavior, anyway, I don't have time to dig into that, hopefully this works for you

hrj21 commented 8 months ago

Thanks for such a quick response, but that doesn't make a difference on my end. I'll keep on digging. I'm in a managed environment so not all my packages are up to date, I'll try on a personal setup.

hrj21 commented 5 months ago

I haven't revisited this before now sorry, but I get the same behaviour with:

I appreciate you might not have time to dig into this but posting this here for more information. Thanks Hefin

R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 21.3

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/London
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] scales_1.3.0         ggcyto_1.30.2        ncdfFlow_2.48.0      BH_1.84.0-0         
[5] flowCore_2.14.1      openCyto_2.14.0      flowWorkspace_4.14.2 ggplot2_3.5.0       

loaded via a namespace (and not attached):
 [1] utf8_1.2.4          generics_0.1.3      lattice_0.22-5      magrittr_2.0.3      grid_4.3.2         
 [6] RColorBrewer_1.1-3  plyr_1.8.9          graph_1.80.0        gridExtra_2.3       BiocManager_1.30.22
[11] fansi_1.0.6         XML_3.99-0.16.1     Rgraphviz_2.46.0    textshaping_0.3.7   cli_3.6.2          
[16] rlang_1.1.3         RProtoBufLib_2.14.0 Biobase_2.62.0      munsell_0.5.0       withr_3.0.0        
[21] cytolib_2.14.1      tools_4.3.2         parallel_4.3.2      dplyr_1.1.4         colorspace_2.1-0   
[26] BiocGenerics_0.48.1 vctrs_0.6.5         R6_2.5.1            matrixStats_1.2.0   stats4_4.3.2       
[31] lifecycle_1.0.4     zlibbioc_1.48.0     S4Vectors_0.40.2    RBGL_1.78.0         ragg_1.2.7         
[36] pkgconfig_2.0.3     pillar_1.9.0        hexbin_1.28.3       gtable_0.3.4        Rcpp_1.0.12        
[41] data.table_1.15.2   glue_1.7.0          flowClust_3.40.0    systemfonts_1.0.5   tibble_3.2.1       
[46] tidyselect_1.2.0    rstudioapi_0.15.0   farver_2.1.1        labeling_0.4.3      compiler_4.3.2