Skip to contents

Preprocessing with tof_preprocess

Generally speaking, the raw ion counts measured for each analyte on a mass cytometer (the content of raw FCS files obtained directly from a mass cytometer) need to be transformed before CyTOF data analysis. Common preprocessing steps may include variance-stabilizing transformations - such as the hyperbolic arcsine (arcsinh) transformation or a log transformation - scaling/centering, and/or denoising.

To perform standard preprocessing tasks with tidytof, use tof_preprocess. tof_preprocess’s default behavior is to apply the arcsinh transformation (with a cofactor of 5) to each numeric column in the input tof_tibble as well as to remove the gaussian noise that Fluidigm software adds to each ion count (this noise is added for visualization purposes, but for most analyses, removing it is recommended).

As an example, we can preprocess tidytof’s built-in phenograph_data tof_tibble and see how our first few measurements change before and after.

data(phenograph_data)

# before preprocessing
phenograph_data %>%
    select(cd45, cd34, cd38) %>%
    head()
#> # A tibble: 6 × 3
#>    cd45   cd34  cd38
#>   <dbl>  <dbl> <dbl>
#> 1  131.  3.23   1.51
#> 2  230. -0.582 11.4 
#> 3  293.  5.20   1.84
#> 4  431.  0.363 13.3 
#> # ℹ 2 more rows
phenograph_data %>%
    # perform preprocessing
    tof_preprocess() %>%
    # inspect new values
    select(cd45, cd34, cd38) %>%
    head()
#> # A tibble: 6 × 3
#>    cd45    cd34  cd38
#>   <dbl>   <dbl> <dbl>
#> 1  3.96  0.608  0.298
#> 2  4.52 -0.116  1.56 
#> 3  4.76  0.909  0.360
#> 4  5.15  0.0725 1.70 
#> # ℹ 2 more rows

To alter tof_preprocess’s default behavior, change the channel_cols argument to specify which columns of tof_tibble should be transformed. Alter the transform_fun argument to specify a vector-valued function that should be used to transform each of the channel_cols. For example, suppose we want to center and scale each of our numeric columns instead of arcsinh-transforming them:

phenograph_data %>%
    # preprocess
    tof_preprocess(transform_fun = scale) %>%
    # inspect new values
    select(cd45, cd34, cd38) %>%
    head()
#> # A tibble: 6 × 3
#>   cd45[,1] cd34[,1] cd38[,1]
#>      <dbl>    <dbl>    <dbl>
#> 1   -1.40     1.01   -0.437 
#> 2   -1.15    -0.911   0.0316
#> 3   -0.999    2.00   -0.422 
#> 4   -0.661   -0.436   0.120 
#> # ℹ 2 more rows

To keep the gaussian noise added by Fluidigm software (or if you are working with a dataset that does not have this noise), set the undo_noise argument to FALSE.

Postprocessing with tof_postprocess

As a final note, note that the built-in function tof_postprocess works nearly identically tof_preprocess, but provides different default behavior (namely, applying the reverse arcsinh transformation with a cofactor of 5 to all numeric columns. See ?tof_postprocess for details).

print(phenograph_data) %>%
    select(cd45, cd34, cd38) %>%
    head()
#> # A tibble: 3,000 × 25
#>   sample_name  phenograph_cluster    cd19 cd11b   cd34  cd45  cd123   cd33  cd47
#>   <chr>        <chr>                <dbl> <dbl>  <dbl> <dbl>  <dbl>  <dbl> <dbl>
#> 1 H1_PhenoGra… cluster1           -0.168  29.0   3.23   131. -0.609  1.21   13.0
#> 2 H1_PhenoGra… cluster1            1.65    4.83 -0.582  230.  2.53  -0.507  12.9
#> 3 H1_PhenoGra… cluster1            2.79   36.1   5.20   293. -0.265  3.67   27.1
#> 4 H1_PhenoGra… cluster1            0.0816 48.8   0.363  431.  2.04   9.40   41.0
#> # ℹ 2,996 more rows
#> # ℹ 16 more variables: cd7 <dbl>, cd44 <dbl>, cd38 <dbl>, cd3 <dbl>,
#> #   cd117 <dbl>, cd64 <dbl>, cd41 <dbl>, pstat3 <dbl>, pstat5 <dbl>,
#> #   pampk <dbl>, p4ebp1 <dbl>, ps6 <dbl>, pcreb <dbl>, `pzap70-syk` <dbl>,
#> #   prb <dbl>, `perk1-2` <dbl>
#> # A tibble: 6 × 3
#>    cd45   cd34  cd38
#>   <dbl>  <dbl> <dbl>
#> 1  131.  3.23   1.51
#> 2  230. -0.582 11.4 
#> 3  293.  5.20   1.84
#> 4  431.  0.363 13.3 
#> # ℹ 2 more rows

# after preprocessing and post-processing, the data are the same
# except that the re-added noise component is different for each value
phenograph_data %>%
    tof_preprocess() %>%
    tof_postprocess(redo_noise = TRUE) %>%
    select(cd45, cd34, cd38) %>%
    head()
#> # A tibble: 6 × 3
#>    cd45   cd34  cd38
#>   <dbl>  <dbl> <dbl>
#> 1  130.  2.36   1.15
#> 2  229. -1.51  10.4 
#> 3  293.  4.54   1.56
#> 4  431.  0.150 12.9 
#> # ℹ 2 more rows

Session info

sessionInfo()
#> R version 4.4.1 (2024-06-14)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> time zone: UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_1.1.4    tidytof_0.99.8
#> 
#> loaded via a namespace (and not attached):
#>   [1] tidyselect_1.2.1    viridisLite_0.4.2   timeDate_4032.109  
#>   [4] farver_2.1.2        viridis_0.6.5       ggraph_2.2.1       
#>   [7] fastmap_1.2.0       tweenr_2.0.3        rpart_4.1.23       
#>  [10] digest_0.6.37       timechange_0.3.0    lifecycle_1.0.4    
#>  [13] yardstick_1.3.1     survival_3.6-4      magrittr_2.0.3     
#>  [16] compiler_4.4.1      rlang_1.1.4         sass_0.4.9         
#>  [19] tools_4.4.1         igraph_2.0.3        utf8_1.2.4         
#>  [22] yaml_2.3.10         data.table_1.15.4   knitr_1.48         
#>  [25] graphlayouts_1.1.1  htmlwidgets_1.6.4   withr_3.0.1        
#>  [28] purrr_1.0.2         RProtoBufLib_2.16.0 BiocGenerics_0.50.0
#>  [31] desc_1.4.3          nnet_7.3-19         grid_4.4.1         
#>  [34] polyclip_1.10-7     stats4_4.4.1        fansi_1.0.6        
#>  [37] RcppHNSW_0.6.0      future_1.34.0       colorspace_2.1-1   
#>  [40] ggplot2_3.5.1       globals_0.16.3      scales_1.3.0       
#>  [43] iterators_1.0.14    MASS_7.3-60.2       cli_3.6.3          
#>  [46] rmarkdown_2.28      ragg_1.3.2          generics_0.1.3     
#>  [49] future.apply_1.11.2 tzdb_0.4.0          cachem_1.1.0       
#>  [52] flowCore_2.16.0     ggforce_0.4.2       stringr_1.5.1      
#>  [55] splines_4.4.1       parallel_4.4.1      matrixStats_1.3.0  
#>  [58] vctrs_0.6.5         hardhat_1.4.0       glmnet_4.1-8       
#>  [61] Matrix_1.7-0        jsonlite_1.8.8      cytolib_2.16.0     
#>  [64] hms_1.1.3           S4Vectors_0.42.1    ggrepel_0.9.5      
#>  [67] listenv_0.9.1       systemfonts_1.1.0   foreach_1.5.2      
#>  [70] gower_1.0.1         tidyr_1.3.1         jquerylib_0.1.4    
#>  [73] recipes_1.1.0       parallelly_1.38.0   glue_1.7.0         
#>  [76] pkgdown_2.1.0       codetools_0.2-20    stringi_1.8.4      
#>  [79] lubridate_1.9.3     gtable_0.3.5        shape_1.4.6.1      
#>  [82] munsell_0.5.1       tibble_3.2.1        pillar_1.9.0       
#>  [85] htmltools_0.5.8.1   ipred_0.9-15        lava_1.8.0         
#>  [88] R6_2.5.1            textshaping_0.4.0   doParallel_1.0.17  
#>  [91] tidygraph_1.3.1     evaluate_0.24.0     Biobase_2.64.0     
#>  [94] lattice_0.22-6      readr_2.1.5         memoise_2.0.1      
#>  [97] bslib_0.8.0         class_7.3-22        Rcpp_1.0.13        
#> [100] prodlim_2024.06.25  gridExtra_2.3       xfun_0.47          
#> [103] fs_1.6.4            pkgconfig_2.0.3