RGLab / flowCore

Core flow cytometry infrastructure
43 stars 25 forks source link

flocore filter issue #239

Open fbenedett opened 1 year ago

fbenedett commented 1 year ago

Hello,

I am trying to apply some filter on my data and plot them before and after the filter is applied.

I have a specific type of flow set for which I have renamed some channels:

fset=read.ncdfFlowSet(paste0(adir, l_files), channel_alias = map, emptyValue = FALSE) I have transformed it:

asinhTrans <- arcsinhTransform(transformationId="Asinh-transformation", a=1, b=5, c=1)
translist <- transformList(trs_col, asinhTrans)
fset <- transform(fset, translist)

And now I want to apply a rectangular gate:

rg <- rectangleGate("140Ce"=c(0,10), "DNA1"=c(7, 15))
fset1= as.flowSet(fset)
for(i in 1:length(fset)){
 fset1[[i]]=flowCore::filter(fset[[i]], rg)  
}

But I get this error:

Error in `[[<-`(`*tmp*`, i, value = new("logicalFilterResult", subSet = c(TRUE,  : 
  [[<- defined for objects of type "S4" only for subclasses of environment

If I use "filter", without specifying that it is from flowcore:

fset1[[i]]=filter(fset[[i]], rg)

I get:

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

Any Idea why what I am doing is wrong?

Here is my sessioninfo:

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=French_Switzerland.1252  LC_CTYPE=French_Switzerland.1252    LC_MONETARY=French_Switzerland.1252
[4] LC_NUMERIC=C                        LC_TIME=French_Switzerland.1252    

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

other attached packages:
 [1] ggcyto_1.22.0               flowWorkspace_4.6.0         DelayedMatrixStats_1.16.0   DelayedArray_0.20.0        
 [5] IRanges_2.28.0              S4Vectors_0.32.4            BiocGenerics_0.40.0         Matrix_1.5-1               
 [9] MatrixGenerics_1.6.0        gridExtra_2.3               openCyto_2.6.0              ggplot2_3.4.0              
[13] RColorBrewer_1.1-3          ConsensusClusterPlus_1.58.0 pheatmap_1.0.12             matrixStats_0.62.0         
[17] dplyr_1.0.10                ncdfFlow_2.40.0             BH_1.78.0-0                 RcppArmadillo_0.11.4.0.1   
[21] flowCore_2.6.0              readxl_1.4.1                FlowSOM_2.2.0               igraph_1.3.5               

loaded via a namespace (and not attached):
  [1] backports_1.4.1         plyr_1.8.7              splines_4.1.1           fda_6.0.5               scattermore_0.8        
  [6] digest_0.6.30           htmltools_0.5.3         fansi_1.0.3             magrittr_2.0.3          CytoML_2.6.0           
 [11] cluster_2.1.4           ks_1.13.5               hdrcde_3.4              aws.signature_0.6.0     RcppParallel_5.1.5     
 [16] R.utils_2.12.2          fds_1.8                 cytolib_2.6.2           jpeg_0.1-9              colorspace_2.0-3       
 [21] rrcov_1.7-2             ggrepel_0.9.1           xfun_0.34               RCurl_1.98-1.8          jsonlite_1.8.3         
 [26] hexbin_1.28.2           graph_1.72.0            glue_1.6.2              flowClust_3.32.0        polyclip_1.10-4        
 [31] gtable_0.3.1            zlibbioc_1.40.0         car_3.1-1               IDPmisc_1.1.20          Rgraphviz_2.38.0       
 [36] DEoptimR_1.0-11         abind_1.4-5             scales_1.2.1            mvtnorm_1.1-3           DBI_1.1.3              
 [41] rstatix_0.7.1           Rcpp_1.0.9              clue_0.3-62             mclust_6.0.0            deSolve_1.34           
 [46] httr_1.4.4              rainbow_3.7             pkgconfig_2.0.3         XML_3.99-0.10           R.methodsS3_1.8.2      
 [51] farver_2.1.1            flowViz_1.58.0          deldir_1.0-6            utf8_1.2.2              labeling_0.4.2         
 [56] flowStats_4.6.0         tidyselect_1.2.0        rlang_1.0.6             reshape2_1.4.4          munsell_0.5.0          
 [61] cellranger_1.1.0        tools_4.1.1             cli_3.3.0               generics_0.1.3          broom_1.0.1            
 [66] aws.s3_0.3.21           evaluate_0.18           stringr_1.5.0           fastmap_1.1.0           yaml_2.3.6             
 [71] knitr_1.41              robustbase_0.95-0       purrr_0.3.5             sparseMatrixStats_1.6.0 RBGL_1.70.0            
 [76] R.oo_1.25.0             pracma_2.4.2            xml2_1.3.3              compiler_4.1.1          rstudioapi_0.14        
 [81] curl_4.3.3              png_0.1-7               ggsignif_0.6.4          tibble_3.1.8            tweenr_2.0.2           
 [86] pcaPP_2.0-3             stringi_1.7.8           lattice_0.20-45         vctrs_0.5.0             pillar_1.8.1           
 [91] lifecycle_1.0.3         BiocManager_1.30.19     data.table_1.14.4       bitops_1.0-7            corpcor_1.6.10         
 [96] colorRamps_2.3.1        R6_2.5.1                latticeExtra_0.6-30     KernSmooth_2.23-20      RProtoBufLib_2.6.0     
[101] MASS_7.3-58.1           gtools_3.9.3            assertthat_0.2.1        withr_2.5.0             mnormt_2.1.1           
[106] parallel_4.1.1          grid_4.1.1              tidyr_1.2.1             ggpointdensity_0.1.0    rmarkdown_2.18         
[111] carData_3.0-5           Rtsne_0.16              ggpubr_0.5.0            ggnewscale_0.4.8        ggforce_0.4.1          
[116] Biobase_2.54.0          base64enc_0.1-3         ellipse_0.4.3           interp_1.1-3  
djhammill commented 1 year ago

@fbenedett, I think you are looking for Subset() - which returns a flowSet of events inside the gate:

# construct a rectangleGate - co-ordinates supplied on transformed scale
rg <- rectangleGate(
  filterId = "DNA_gate+",   # label the gate
  "140Ce" = c(0, 10),
  "DNA1" = c(7, 15)
)

# fs is your flowSet - fs_sub is a flowSet of events inside the gate
fs_sub <- Subset(
  fs,
  rg
)

If you want to have separate gates for every sample, you can include them in a list() named with sampleNames():

# rectangleGate list - example for flowSet with 2 samples (i.e. 2 gates)
rg_list <- structure(
  list(
    rg,    # any gate objects can be added here - one per sample
    rg
  ),
  names = sampleNames(fs)
)
# Subset samples individually
fs_sub <- Subset(
  fs,
  rg_list
)

I'd recommend using ggcyto to visualise the results:

ggcyto(
  fs, 
  aes(140Ce, DNA1)
) + 
geom_hex(     # add points
  bins = 256
) + 
geom_gate(    # add gate
  rg
) + 
geom_stats(    # add statistics
  type = "percent"
)