DimViz vs. Transformation

Author

David Rach

Published

February 6, 2028

AGPL-3.0 CC BY-SA 4.0

Toy example

Set Up

library(flowWorkspace)
As part of improvements to flowWorkspace, some behavior of
GatingSet objects has changed. For details, please read the section
titled "The cytoframe and cytoset classes" in the package vignette:

  vignette("flowWorkspace-Introduction", "flowWorkspace")
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(ggcyto)
Loading required package: flowCore
Loading required package: ncdfFlow
Loading required package: BH
library(flowGate)
Registered polygon_gate
Registered span_gate
library(purrr)
library(uwot) # CRAN # install.packages("uwot")
Loading required package: Matrix

Attaching package: 'Matrix'
The following object is masked from 'package:flowCore':

    %&%
# RFolder <- file.path("community", "ejemplos", "R") # For Interactive
RFolder <- file.path("R") # For Quarto Rendering
MyFunctions <- list.files(RFolder, full.names=TRUE)
purrr::walk(.x=MyFunctions, .f=source)
StorageLocation <- file.path("data") # For Quarto Rendering
#StorageLocation <- file.path("community", "ejemplos", "data")

OutputFolder <- file.path("outputs") # For Quarto Rendering
#OutputFolder <- file.path("community", "ejemplos", "outputs")

fcs_files <- list.files(StorageLocation, pattern=".fcs", full.names=TRUE)

Transformations

Biexponential 1

Biexponential1_cytoset <- load_cytoset_from_fcs(fcs_files, truncate_max_range=FALSE, transformation=FALSE)
Biexponential1_GatingSet <- GatingSet(Biexponential1_cytoset)

Biexponential1_Parameters <- colnames(Biexponential1_GatingSet)
FluorophoresOnly <- Biexponential1_Parameters[!stringr::str_detect(Biexponential1_Parameters, "FSC|SSC|Time")]

Biexponential1 <- flowjo_biexp_trans(channelRange=4096, maxValue=262144,
     pos=4.5, neg=2, widthBasis=-750)
MyBiexTransform_1 <- transformerList(FluorophoresOnly, Biexponential1)
transform(Biexponential1_GatingSet, MyBiexTransform_1)
A GatingSet with 3 samples
# pData(Biexponential1_GatingSet)
# markernames(Biexponential1_GatingSet)

# remotes::install_github("DavidRach/Luciernaga")

SinglePlot <- Luciernaga::Utility_RidgePlots(gs=Biexponential1_GatingSet, subset="root", 
     TheFill="name", TheX = "BUV805-A", TheY="name",returntype="patchwork",
     outpath=OutputFolder, filename="CD4RidgePlot_Biexponential1")

SinglePlot
$`1`
Picking joint bandwidth of 104

Biexponential 2

Biexponential2_cytoset <- load_cytoset_from_fcs(fcs_files, truncate_max_range=FALSE, transformation=FALSE)
Biexponential2_GatingSet <- GatingSet(Biexponential2_cytoset)

Biexponential2_Parameters <- colnames(Biexponential2_GatingSet)
FluorophoresOnly <- Biexponential2_Parameters[!stringr::str_detect(Biexponential2_Parameters, "FSC|SSC|Time")]

Biexponential2 <- flowjo_biexp_trans(channelRange=4096, maxValue=262144,
     pos=5.5, neg=2, widthBasis=-1000)
MyBiexTransform_2 <- transformerList(FluorophoresOnly, Biexponential2)
transform(Biexponential2_GatingSet, MyBiexTransform_2)
A GatingSet with 3 samples
# pData(Biexponential1_GatingSet)
# markernames(Biexponential1_GatingSet)

# remotes::install_github("DavidRach/Luciernaga")

SinglePlot <- Luciernaga::Utility_RidgePlots(gs=Biexponential2_GatingSet, subset="root", 
     TheFill="name", TheX = "BUV805-A", TheY="name",returntype="patchwork",
     outpath=OutputFolder, filename="CD4RidgePlot_Biexponential2")

SinglePlot
$`1`
Picking joint bandwidth of 124

Arcsinh 1

Arcsinh1_cytoset <- load_cytoset_from_fcs(fcs_files, truncate_max_range=FALSE, transformation=FALSE)
Arcsinh1_GatingSet <- GatingSet(Arcsinh1_cytoset)

Arcsinh1_Parameters <- colnames(Arcsinh1_GatingSet)
FluorophoresOnly <- Arcsinh1_Parameters[!stringr::str_detect(Arcsinh1_Parameters, "FSC|SSC|Time")]
Arcsinh_1 <- flowjo_fasinh_trans(t = 262144, m = 4.5, a = 0)
MyArcsinhTransform_1 <- transformerList(FluorophoresOnly, Arcsinh_1)
transform(Arcsinh1_GatingSet, MyArcsinhTransform_1)
A GatingSet with 3 samples
# pData(Biexponential1_GatingSet)
# markernames(Biexponential1_GatingSet)

# remotes::install_github("DavidRach/Luciernaga")

SinglePlot <- Luciernaga::Utility_RidgePlots(gs=Arcsinh1_GatingSet, subset="root", 
     TheFill="name", TheX = "BUV805-A", TheY="name",returntype="patchwork",
     outpath=OutputFolder, filename="CD4RidgePlot_Arcsinh1")

SinglePlot
$`1`
Picking joint bandwidth of 13.8

Arcsinh 2

Arcsinh2_cytoset <- load_cytoset_from_fcs(fcs_files, truncate_max_range=FALSE, transformation=FALSE)
Arcsinh2_GatingSet <- GatingSet(Arcsinh2_cytoset)

Arcsinh2_Parameters <- colnames(Arcsinh2_GatingSet)
FluorophoresOnly <- Arcsinh2_Parameters[!stringr::str_detect(Arcsinh2_Parameters, "FSC|SSC|Time")]
Arcsinh_2 <- flowjo_fasinh_trans(t = 13172, m = 5.5, a=0)
MyArcsinhTransform_2 <- transformerList(FluorophoresOnly, Arcsinh_2)
transform(Arcsinh2_GatingSet, MyArcsinhTransform_2)
A GatingSet with 3 samples
# pData(Biexponential1_GatingSet)
# markernames(Biexponential1_GatingSet)

# remotes::install_github("DavidRach/Luciernaga")

SinglePlot <- Luciernaga::Utility_RidgePlots(gs=Arcsinh2_GatingSet, subset="root", 
     TheFill="name", TheX = "BUV805-A", TheY="name",returntype="patchwork",
     outpath=OutputFolder, filename="CD4RidgePlot_Arcsinh2")

SinglePlot
$`1`
Picking joint bandwidth of 15.5

UMAP

# markernames(Biexponential1_GatingSet)
TheseMarkers <- c("BUV805-A", "BV650-A", "APC-Fire 750-A", "APC-Fire 810-A", "BUV395-A", "BV605-A")

Biexponential1

Dataset <- Concatenate(gs=Biexponential1_GatingSet, inverse.transform=FALSE,
 returnType="data.frame", desiredCols=c("name"), subset="root",
 DownsampleCount = 10000, addon="Tcells")


set.seed(1989)
Toy_UMAP <- umap(
  Dataset[, TheseMarkers],
  n_neighbors = 15,
  min_dist    = 0.2,
  metric      = "euclidean",
  verbose     = TRUE
)
14:33:45 UMAP embedding parameters a = 1.262 b = 1.003
14:33:45 Converting dataframe to numerical matrix
14:33:45 Read 30000 rows and found 6 numeric columns
14:33:45 Using Annoy for neighbor search, n_neighbors = 15
14:33:45 Building Annoy index with metric = euclidean, n_trees = 50
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
14:33:48 Writing NN index file to temp file /tmp/RtmpNdahdr/file60a65605d3e8
14:33:48 Searching Annoy index using 4 threads, search_k = 1500
14:33:51 Annoy recall = 100%
14:33:52 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 15
14:33:52 Initializing from normalized Laplacian + noise (using RSpectra)
14:33:53 Commencing optimization for 200 epochs, with 565688 positive edges
14:33:53 Using rng type: pcg
14:34:07 Optimization finished
Dataset$UMAP1 <- Toy_UMAP[, 1]
Dataset$UMAP2 <- Toy_UMAP[, 2]

# Plot
ggplot(Dataset, aes(x = UMAP1, y = UMAP2, color = `BUV805-A`)) +
  geom_point(size = 0.4, alpha = 0.5) +
  scale_color_viridis_c() +
  theme_bw() +
  labs(title = "UMAP — T cells - Biexponential1", color = "BUV805-A")

Biexponential2

Dataset <- Concatenate(gs=Biexponential2_GatingSet, inverse.transform=FALSE,
 returnType="data.frame", desiredCols=c("name"), subset="root",
 DownsampleCount = 10000, addon="Tcells")

set.seed(1989)
Toy_UMAP <- umap(
  Dataset[, TheseMarkers],
  n_neighbors = 15,
  min_dist    = 0.2,
  metric      = "euclidean",
  verbose     = TRUE
)
14:34:09 UMAP embedding parameters a = 1.262 b = 1.003
14:34:09 Converting dataframe to numerical matrix
14:34:09 Read 30000 rows and found 6 numeric columns
14:34:09 Using Annoy for neighbor search, n_neighbors = 15
14:34:09 Building Annoy index with metric = euclidean, n_trees = 50
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
14:34:12 Writing NN index file to temp file /tmp/RtmpNdahdr/file60a61c4c0578
14:34:12 Searching Annoy index using 4 threads, search_k = 1500
14:34:15 Annoy recall = 100%
14:34:15 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 15
14:34:16 Initializing from normalized Laplacian + noise (using RSpectra)
14:34:17 Commencing optimization for 200 epochs, with 560876 positive edges
14:34:17 Using rng type: pcg
14:34:30 Optimization finished
Dataset$UMAP1 <- Toy_UMAP[, 1]
Dataset$UMAP2 <- Toy_UMAP[, 2]

# Plot
ggplot(Dataset, aes(x = UMAP1, y = UMAP2, color = `BUV805-A`)) +
  geom_point(size = 0.4, alpha = 0.5) +
  scale_color_viridis_c() +
  theme_bw() +
  labs(title = "UMAP — T cells - Biexponential2", color = "BUV805-A")

Arcsinh1

Dataset <- Concatenate(gs=Arcsinh1_GatingSet, inverse.transform=FALSE,
 returnType="data.frame", desiredCols=c("name"), subset="root",
 DownsampleCount = 10000, addon="Tcells")

set.seed(1989)
Toy_UMAP <- umap(
  Dataset[, TheseMarkers],
  n_neighbors = 15,
  min_dist    = 0.2,
  metric      = "euclidean",
  verbose     = TRUE
)
14:34:32 UMAP embedding parameters a = 1.262 b = 1.003
14:34:32 Converting dataframe to numerical matrix
14:34:32 Read 30000 rows and found 6 numeric columns
14:34:32 Using Annoy for neighbor search, n_neighbors = 15
14:34:32 Building Annoy index with metric = euclidean, n_trees = 50
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
14:34:35 Writing NN index file to temp file /tmp/RtmpNdahdr/file60a63443ba2f
14:34:35 Searching Annoy index using 4 threads, search_k = 1500
14:34:38 Annoy recall = 100%
14:34:38 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 15
14:34:39 Initializing from normalized Laplacian + noise (using RSpectra)
14:34:41 Commencing optimization for 200 epochs, with 568868 positive edges
14:34:41 Using rng type: pcg
14:34:54 Optimization finished
Dataset$UMAP1 <- Toy_UMAP[, 1]
Dataset$UMAP2 <- Toy_UMAP[, 2]

# Plot
ggplot(Dataset, aes(x = UMAP1, y = UMAP2, color = `BUV805-A`)) +
  geom_point(size = 0.4, alpha = 0.5) +
  scale_color_viridis_c() +
  theme_bw() +
  labs(title = "UMAP — T cells - ArcSinh1", color = "BUV805-A")

Arcsinh2

Dataset <- Concatenate(gs=Arcsinh2_GatingSet, inverse.transform=FALSE,
 returnType="data.frame", desiredCols=c("name"), subset="root",
 DownsampleCount = 10000, addon="Tcells")

set.seed(1989)
Toy_UMAP <- umap(
  Dataset[, TheseMarkers],
  n_neighbors = 15,
  min_dist    = 0.2,
  metric      = "euclidean",
  verbose     = TRUE
)
14:34:56 UMAP embedding parameters a = 1.262 b = 1.003
14:34:56 Converting dataframe to numerical matrix
14:34:56 Read 30000 rows and found 6 numeric columns
14:34:56 Using Annoy for neighbor search, n_neighbors = 15
14:34:56 Building Annoy index with metric = euclidean, n_trees = 50
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
14:34:59 Writing NN index file to temp file /tmp/RtmpNdahdr/file60a61137357a
14:34:59 Searching Annoy index using 4 threads, search_k = 1500
14:35:02 Annoy recall = 100%
14:35:03 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 15
14:35:04 Found 3 connected components, falling back to 'spca' initialization with init_sdev = 1
14:35:04 Using 'irlba' for PCA
14:35:04 PCA: 2 components explained 71.72% variance
14:35:04 Scaling init to sdev = 1
14:35:04 Commencing optimization for 200 epochs, with 571048 positive edges
14:35:04 Using rng type: pcg
14:35:17 Optimization finished
Dataset$UMAP1 <- Toy_UMAP[, 1]
Dataset$UMAP2 <- Toy_UMAP[, 2]

# Plot
ggplot(Dataset, aes(x = UMAP1, y = UMAP2, color = `BUV805-A`)) +
  geom_point(size = 0.4, alpha = 0.5) +
  scale_color_viridis_c() +
  theme_bw() +
  labs(title = "UMAP — T cells - ArcSinh2", color = "BUV805-A")

AGPL-3.0 CC BY-SA 4.0