DillonHammill / CytoExploreR

Interactive Cytometry Data Analysis
61 stars 13 forks source link

gatingTemplate in cyto_gate_draw doesn't output #50

Closed stefanucci-luca closed 4 years ago

stefanucci-luca commented 4 years ago

Describe the bug I tried to use gatingTemplate when exporting the gates drawn by cyto_gate_draw(), but I don't get any output.

To Reproduce library(CytoExploreRData)

fs <- Activation gs <- GatingSet(fs)

gs <- compensate(gs, fs[[1]]@description$SPILL)

trans <- estimateLogicle(gs[[4]], cyto_fluor_channels(fs)) gs <- transform(gs, trans)

gt_gating(Activation_gatingTemplate, gs)

cyto_gate_draw(gs, parent = "Dendritic Cells", channels = c("Alexa Fluor 488-A", "Alexa Fluor 700-A"), alias = c("CD8+ DC", "CD4+ DC"), gatingTemplate = "Example-gatingTemplate.csv", type = "rectangle", contour_lines = 15 )

Expected behavior I would expect a csv file called Example-gatingTemplate.csv in the getwd() directory

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context SessionInfo() R version 3.6.1 (2019-07-05) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Catalina 10.15.4

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale: [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

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

other attached packages: [1] Rgraphviz_2.30.0 graph_1.64.0 BiocGenerics_0.32.0 shiny_1.4.0.2
[5] scales_1.1.1 CytoExploreRData_1.0.1 CytoExploreR_1.0.7 openCyto_2.1.0
[9] flowWorkspace_4.1.2 flowCore_2.1.0

loaded via a namespace (and not attached): [1] Rtsne_0.15 colorspace_1.4-1 ellipsis_0.3.1 mclust_5.4.6 cytolib_2.1.4
[6] corpcor_1.6.9 clue_0.3-57 rstudioapi_0.11 hexbin_1.28.1 IDPmisc_1.1.20
[11] RSpectra_0.16-0 fansi_0.4.1 mvtnorm_1.1-0 splines_3.6.1 R.methodsS3_1.8.0
[16] mnormt_1.5-7 robustbase_0.93-6 knitr_1.28 shinythemes_1.1.2 jsonlite_1.6.1
[21] umap_0.2.5.0 cluster_2.1.0 png_0.1-7 R.oo_1.23.0 rrcov_1.5-2
[26] compiler_3.6.1 assertthat_0.2.1 Matrix_1.2-18 fastmap_1.0.1 cli_2.0.2
[31] later_1.0.0 visNetwork_2.0.9 htmltools_0.4.0 tools_3.6.1 ncdfFlow_2.32.0
[36] rsvd_1.0.3 gtable_0.3.0 glue_1.4.1 reshape2_1.4.4 dplyr_0.8.5
[41] Rcpp_1.0.4.6 Biobase_2.46.0 vctrs_0.3.0 changepoint_2.2.2 xfun_0.14
[46] stringr_1.4.0 mime_0.9 lifecycle_0.2.0 gtools_3.8.2 XML_3.99-0.3
[51] DEoptimR_1.0-8 zlibbioc_1.32.0 MASS_7.3-51.6 zoo_1.8-8 EmbedSOM_2.1.1
[56] RProtoBufLib_2.1.0 promises_1.1.0 RBGL_1.62.1 RColorBrewer_1.1-2 yaml_2.2.1
[61] reticulate_1.15 ggplot2_3.3.0 latticeExtra_0.6-29 stringi_1.4.6 pcaPP_1.9-73
[66] flowClust_3.24.0 flowAI_1.16.0 flowViz_1.50.0 rlang_0.4.6 pkgconfig_2.0.3
[71] matrixStats_0.56.0 evaluate_0.14 fda_5.1.4 lattice_0.20-41 purrr_0.3.4
[76] ks_1.11.7 htmlwidgets_1.5.1 tidyselect_1.1.0 plyr_1.8.6 magrittr_1.5
[81] R6_2.4.1 pillar_1.4.4 withr_2.2.0 tibble_3.0.1 crayon_1.3.4
[86] KernSmooth_2.23-17 ellipse_0.4.1 rhandsontable_0.3.7 rmarkdown_2.1 jpeg_0.1-8.1
[91] data.table_1.12.8 digest_0.6.25 xtable_1.8-4 tidyr_1.1.0 httpuv_1.5.2
[96] R.utils_2.9.2 flowStats_4.1.0 openssl_1.4.1 RcppParallel_5.0.1 stats4_3.6.1
[101] munsell_0.5.0 sessioninfo_1.1.1 askpass_1.1

DillonHammill commented 4 years ago

@stefanucci-luca, please can you show me the output of:

cyto_gatingTemplate_active()

I suspect that CytoExploreR thinks that you are using a different gatingTemplate.

DillonHammill commented 4 years ago

Works fine on my end, I think you may need to tell CytoExploreR that you want to use "Example-gatingTemplate.csv":

# Create empty gatingTemplate
cyto_gatingTemplate_create("Example-gatingTemplate.csv")
# Select new gatingTemplate as active
cyto_gatingTemplate_select("Example-gatingTemplate.csv")

Now all subsequent gates should be saved to Example-gatingTemplate.csv.

NOTE: cyto_setup() performs these steps behind the scenes, so if you have called cyto_setup() previously and indicated a gatngTemplate, you will need to tell CytoExploreR to switch gatingTemplates using cyto_gatingTemplate_select().

DillonHammill commented 4 years ago

Also check that you are looking in the correct directory:

getwd()
stefanucci-luca commented 4 years ago

Hi Dillon,

Thanks for your prompt reply.

The goal of this short script is to create the gates for a new experiment and saving them. I've followed your suggestion above, so manually created the gating template and selected it (the file is created in getwd()). The function cyto_gatingTemplate_active() now print the correct gates. However, if I run this:

fs <- Activation
gs <- GatingSet(fs)
gs <- compensate(gs, fs[[1]]@description$SPILL)
trans <- estimateLogicle(gs[[4]], cyto_fluor_channels(fs))
gs <- transform(gs, trans)

# Create empty gatingTemplate
cyto_gatingTemplate_create("gating_template_epicov.csv")
# Select new gatingTemplate as active
cyto_gatingTemplate_select("gating_template_epicov.csv")

cyto_gatingTemplate_active()

# Gate Cells
control_cs <- gs
cyto_names(control_cs)
#trans.log <- flowjo_log_trans(decade = 8, scale = 10)

cyto_gate_draw(control_cs,
               parent = "Dendritic Cells",
               channels = c("Alexa Fluor 488-A", "Alexa Fluor 700-A"),
               alias = c("CD8+ DC", "CD4+ DC"),
               type = "rectangle",
               contour_lines = 15,
               #parent = "root",
               #alias = "Cells",
               #channels = c("FSC-A", "SSC-A"), 
               #axes_trans = transformerList(c("FSC-A", "SSC-A"), trans.log),
               gatingTemplate = "gating_template_epicov.csv" 
)

I get the error message:

Error in get_cytoset_from_node(obj@pointer, y) : 
  c++ exception (unknown reason)

If I change the script and use the gt_gating() function it runs:

fs <- Activation
gs <- GatingSet(fs)
gs <- compensate(gs, fs[[1]]@description$SPILL)
trans <- estimateLogicle(gs[[4]], cyto_fluor_channels(fs))
gs <- transform(gs, trans)

# Create empty gatingTemplate
cyto_gatingTemplate_create("gating_template_epicov.csv")
# Select new gatingTemplate as active
cyto_gatingTemplate_select("gating_template_epicov.csv")

cyto_gatingTemplate_active()

# Gate Cells
control_cs <- gs
cyto_names(control_cs)
#trans.log <- flowjo_log_trans(decade = 8, scale = 10)
gt_gating(Activation_gatingTemplate, control_cs)

cyto_gate_draw(control_cs,
               parent = "Dendritic Cells",
               channels = c("Alexa Fluor 488-A", "Alexa Fluor 700-A"),
               alias = c("CD8+ DC", "CD4+ DC"),
               type = "rectangle",
               contour_lines = 15,
               #parent = "root",
               #alias = "Cells",
               #channels = c("FSC-A", "SSC-A"), 
               #axes_trans = transformerList(c("FSC-A", "SSC-A"), trans.log),
               gatingTemplate = "gating_template_epicov.csv" 
)

However, the gates are not saved in the gate csv file.

cat gating_template_epicov.csv 
"alias","pop","parent","dims","gating_method","gating_args","collapseDataForGating","groupBy","preprocessing_method","preprocessing_args"

If I try to set do gatingTemplate(file.path(path, "data/ICStemplate.csv"), "ICS") because I intend to use "gating_template_epicov.csv" as gating template I get:

`gatingTemplate(file.path(path, "data/ICStemplate.csv"), "ICS")`
Error in as.vector(x, "character") : 
  cannot coerce type 'closure' to vector of type 'character'
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

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

other attached packages:
 [1] Rgraphviz_2.30.0       graph_1.64.0           BiocGenerics_0.32.0    shiny_1.4.0.2         
 [5] scales_1.1.1           CytoExploreRData_1.0.1 CytoExploreR_1.0.7     openCyto_2.1.0        
 [9] flowWorkspace_4.1.2    flowCore_2.1.0        

loaded via a namespace (and not attached):
  [1] Rtsne_0.15          colorspace_1.4-1    ellipsis_0.3.1      mclust_5.4.6        cytolib_2.1.4      
  [6] corpcor_1.6.9       clue_0.3-57         rstudioapi_0.11     hexbin_1.28.1       IDPmisc_1.1.20     
 [11] RSpectra_0.16-0     fansi_0.4.1         mvtnorm_1.1-0       splines_3.6.1       R.methodsS3_1.8.0  
 [16] mnormt_1.5-7        robustbase_0.93-6   knitr_1.28          shinythemes_1.1.2   jsonlite_1.6.1     
 [21] umap_0.2.5.0        cluster_2.1.0       png_0.1-7           R.oo_1.23.0         rrcov_1.5-2        
 [26] compiler_3.6.1      assertthat_0.2.1    Matrix_1.2-18       fastmap_1.0.1       cli_2.0.2          
 [31] later_1.0.0         visNetwork_2.0.9    htmltools_0.4.0     tools_3.6.1         ncdfFlow_2.32.0    
 [36] rsvd_1.0.3          gtable_0.3.0        glue_1.4.1          reshape2_1.4.4      dplyr_0.8.5        
 [41] Rcpp_1.0.4.6        Biobase_2.46.0      vctrs_0.3.0         changepoint_2.2.2   xfun_0.14          
 [46] stringr_1.4.0       mime_0.9            lifecycle_0.2.0     gtools_3.8.2        XML_3.99-0.3       
 [51] DEoptimR_1.0-8      zlibbioc_1.32.0     MASS_7.3-51.6       zoo_1.8-8           EmbedSOM_2.1.1     
 [56] RProtoBufLib_2.1.0  promises_1.1.0      RBGL_1.62.1         RColorBrewer_1.1-2  yaml_2.2.1         
 [61] reticulate_1.15     ggplot2_3.3.0       latticeExtra_0.6-29 stringi_1.4.6       pcaPP_1.9-73       
 [66] flowClust_3.24.0    flowAI_1.16.0       flowViz_1.50.0      rlang_0.4.6         pkgconfig_2.0.3    
 [71] matrixStats_0.56.0  evaluate_0.14       fda_5.1.4           lattice_0.20-41     purrr_0.3.4        
 [76] ks_1.11.7           htmlwidgets_1.5.1   tidyselect_1.1.0    plyr_1.8.6          magrittr_1.5       
 [81] R6_2.4.1            pillar_1.4.4        withr_2.2.0         tibble_3.0.1        crayon_1.3.4       
 [86] KernSmooth_2.23-17  ellipse_0.4.1       rhandsontable_0.3.7 rmarkdown_2.1       jpeg_0.1-8.1       
 [91] data.table_1.12.8   digest_0.6.25       xtable_1.8-4        tidyr_1.1.0         httpuv_1.5.2       
 [96] R.utils_2.9.2       flowStats_4.1.0     openssl_1.4.1       RcppParallel_5.0.1  stats4_3.6.1       
[101] munsell_0.5.0       sessioninfo_1.1.1   askpass_1.1    
DillonHammill commented 4 years ago

@stefanucci-luca, I am struggling to reproduce this on my end. Everything works as expected when running your code on my windows machine. I haven't encountered this issue on mac either.

I checked the code and you should not need to cyto_gatingTemplate_select() or cyto_gatingTemplate_create() calls when supplying a gatingTemplate manually to cyto_gate_draw().

You definitely need a call to gt_gating() or cyto_gatingTemplate_apply() prior to gating as the parent populations will not exist in the GatingSet otherwise.

DillonHammill commented 4 years ago

Perhaps it is a file accessibility issue. Make sure that you don't have the file open in another program when you are drawing the gate(s).

stefanucci-luca commented 4 years ago

Hi @DillonHammill

Thanks for your help. I understood where I was wrong.

If the x in cyto_gate_draw() has

class(x)
[1] "cytoset"
attr(,"package")
[1] "flowWorkspace"

then gatingTemplate is not writing any output. If the

class(x)
[1] "GatingSet"
attr(,"package")
[1] "flowWorkspace"

then gatingTemplate works.

However, if the class is "GatingSet" I can't use a particular sample (e.g x[[1]]) to set the gate. If I run:

cyto_gate_draw(control_cs[[3]],
               parent = "Platelets CD42 +",
               alias = "P_Selectin + (PE)",
               channels = c("670/14 640nm-A")
)

I get this error message:

Error in UseMethod("cyto_merge_by") : 
  no applicable method for 'cyto_merge_by' applied to an object of class "flowFrame"

If I run the same command above but with class ("cytoset") I can make the gate but it is not output.

Is there a way to set the gate on a sample in particular?

DillonHammill commented 4 years ago

That is correct, the cytoset method does not write to the gatingTemplate, but instead returns the gate objects. You can only use gatingTemplate with GatingSet objects. You need to use GatingSet objects if you want to save the gates to the gatingTemplate.

stefanucci-luca commented 4 years ago

Thanks for your answer @DillonHammill. So, How can I set a gate on a specific sample? e.g. use the unstained sample to set the gate.

DillonHammill commented 4 years ago

See my comments on issue #39.