| name | bio-flow-cytometry-compensation-transformation |
| description | Spillover compensation and data transformation for flow cytometry. Covers compensation matrix calculation, application, and biexponential/arcsinh transforms. Use when correcting spectral overlap between fluorophores or transforming data for analysis. |
| tool_type | r |
| primary_tool | flowCore |
Version Compatibility
Reference examples tested with: flowCore 2.14+, scanpy 1.10+
Before using code patterns, verify installed versions match. If versions differ:
- R:
packageVersion('<pkg>') then ?function_name to verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
Compensation and Transformation
"Compensate and transform my flow cytometry data" → Correct spectral overlap between fluorophores using a compensation matrix and apply biexponential/arcsinh transforms for visualization and analysis.
- R:
flowCore::compensate() then flowCore::transform() with estimateLogicle()
Load Compensation Matrix
library(flowCore)
fcs <- read.FCS('sample.fcs', transformation = FALSE)
comp_matrix <- keyword(fcs)$`$SPILLOVER`
comp_matrix <- as.matrix(read.csv('compensation.csv', row.names = 1))
Apply Compensation
comp <- compensation(comp_matrix)
fcs_comp <- compensate(fcs, comp)
fs_comp <- compensate(fs, comp)
Calculate Compensation from Controls
library(flowStats)
controls <- read.flowSet(list.files('controls', pattern = '\\.fcs$', full.names = TRUE))
spillover <- spillover(controls,
unstained = 'Unstained.fcs',
fsc = 'FSC-A', ssc = 'SSC-A',
patt = '-A$',
stain_match = 'regexpr')
comp_matrix <- spillover$comp
Transformation: Biexponential (Logicle)
library(flowWorkspace)
lgcl <- estimateLogicle(fcs, colnames(fcs)[3:10])
fcs_trans <- transform(fcs, lgcl)
lgcl_manual <- logicleTransform(
w = 0.5,
t = 262144,
m = 4.5,
a = 0
)
Transformation: Arcsinh (CyTOF)
arcsinh_transform <- function(x, cofactor = 5) {
asinh(x / cofactor)
}
expr <- exprs(fcs)
expr_trans <- apply(expr[, marker_channels], 2, arcsinh_transform, cofactor = 5)
asinhTrans <- arcsinhTransform(transformationId = 'arcsinh', a = 0, b = 1/5)
trans_list <- transformList(marker_channels, asinhTrans)
fcs_trans <- transform(fcs, trans_list)
Transformation: Log
logTrans <- logTransform(transformationId = 'log10', logbase = 10, r = 1, d = 1)
trans_list <- transformList(marker_channels, logTrans)
fcs_trans <- transform(fcs, trans_list)
View Before/After Compensation
library(ggcyto)
p1 <- autoplot(fcs, 'FITC-A', 'PE-A') + ggtitle('Before Compensation')
p2 <- autoplot(fcs_comp, 'FITC-A', 'PE-A') + ggtitle('After Compensation')
library(patchwork)
p1 + p2
Complete Preprocessing Pipeline
Goal: Apply a standard compensation-then-transformation workflow to all samples in a flowSet.
Approach: Define a reusable preprocessing function that first applies the spillover compensation matrix, then auto-estimates and applies logicle transformation on marker channels, and map it across all samples with fsApply.
preprocess_flow <- function(fcs, comp_matrix, marker_channels) {
comp <- compensation(comp_matrix)
fcs <- compensate(fcs, comp)
lgcl <- estimateLogicle(fcs, marker_channels)
fcs <- transform(fcs, lgcl)
return(fcs)
}
fs_processed <- fsApply(fs, function(f) {
preprocess_flow(f, comp_matrix, marker_channels)
})
CATALYST Preprocessing (CyTOF)
library(CATALYST)
library(SingleCellExperiment)
sce <- prepData(fs,
panel = panel,
md = sample_info,
transform = TRUE,
cofactor = 5,
FACS = FALSE)
Panel File Format (CATALYST)
panel <- data.frame(
fcs_colname = c('Yb176Di', 'Er168Di', 'Nd142Di'),
antigen = c('CD45', 'CD3', 'CD4'),
marker_class = c('type', 'type', 'type')
)
Save Preprocessed Data
write.FCS(fcs_trans, 'sample_preprocessed.fcs')
saveRDS(list(comp = comp_matrix, transform = lgcl), 'preprocessing_params.rds')
Related Skills
- fcs-handling - Load FCS files first
- gating-analysis - Gate after preprocessing
- clustering-phenotyping - Cluster transformed data