Comprehensive quality control for flow cytometry and CyTOF data. Covers flow rate stability, signal drift, margin events, dead cell exclusion, and batch QC. Use when assessing acquisition quality or identifying problematic samples before analysis.
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.
Cytometry QC
"Run quality control on my flow cytometry data" → Assess acquisition quality by checking flow rate stability, signal drift, margin events, and dead cell frequencies to identify problematic samples.
R: flowAI::flow_auto_qc() for automated anomaly detection
Automated QC with flowAI
library(flowAI)
library(flowCore)
# Load FCS file
ff <- read.FCS('sample.fcs')
# Run automated QC
# Checks: flow rate, signal stability, dynamic range
qc_result <- flow_auto_qc(
ff,
folder_results = 'qc_output/',
fcs_QC = TRUE, # Export QC'd FCS
html_report = TRUE, # Generate HTML report
mini_report = TRUE # Also make summary
)
# Get cleaned data
ff_clean <- qc_result$fcs
# QC metrics
cat('Original events:', nrow(ff), '\n')
cat('After QC:', nrow(ff_clean), '\n')
cat('Removed:', nrow(ff) - nrow(ff_clean), '(',
round((1 - nrow(ff_clean)/nrow(ff)) * 100, 1), '%)\n')
# Remove events at detector saturation limits
remove_margin_events <- function(ff, channels = NULL) {
expr <- exprs(ff)
if (is.null(channels)) {
channels <- colnames(expr)
}
# Get channel ranges from FCS parameters
params <- parameters(ff)
margin_mask <- rep(FALSE, nrow(expr))
for (ch in channels) {
if (ch %in% colnames(expr)) {
# Get max range from parameters
idx <- match(ch, params@data$name)
if (!is.na(idx)) {
max_val <- params@data$range[idx]
# Events at max or min are margin events
margin_mask <- margin_mask | (expr[, ch] >= max_val * 0.99) | (expr[, ch] <= 0)
}
}
}
cat('Margin events:', sum(margin_mask), '(', round(mean(margin_mask) * 100, 2), '%)\n')
ff[!margin_mask, ]
}
ff_no_margin <- remove_margin_events(ff, c('FSC-A', 'SSC-A'))
Dead Cell Exclusion
# Exclude dead cells using viability marker
exclude_dead_cells <- function(ff, viability_channel, threshold = NULL) {
expr <- exprs(ff)
viability <- expr[, viability_channel]
if (is.null(threshold)) {
# Auto-threshold using bimodal distribution
# Dead cells have higher viability dye uptake
threshold <- quantile(viability, 0.9)
}
live_mask <- viability < threshold
cat('Total events:', length(live_mask), '\n')
cat('Live cells:', sum(live_mask), '(', round(mean(live_mask) * 100, 1), '%)\n')
cat('Dead cells:', sum(!live_mask), '(', round(mean(!live_mask) * 100, 1), '%)\n')
ff[live_mask, ]
}
# Example with zombie dye
ff_live <- exclude_dead_cells(ff, 'Zombie-Aqua')
CyTOF-Specific QC
# CyTOF-specific quality metrics
cytof_qc <- function(ff) {
expr <- exprs(ff)
# Event length check (cell size proxy)
if ('Event_length' %in% colnames(expr)) {
event_length <- expr[, 'Event_length']
# Typical single cells: 15-45
good_length <- event_length >= 15 & event_length <= 45
cat('Event length filter:', sum(good_length), '/', length(good_length),
'(', round(mean(good_length) * 100, 1), '%)\n')
}
# DNA intercalator check (nucleated cells)
dna_channels <- grep('(Ir191|Ir193|DNA)', colnames(expr), value = TRUE)
if (length(dna_channels) > 0) {
dna_signal <- rowMeans(expr[, dna_channels, drop = FALSE])
# Cells should have DNA signal
has_dna <- dna_signal > quantile(dna_signal, 0.1)
cat('DNA+ events:', sum(has_dna), '(', round(mean(has_dna) * 100, 1), '%)\n')
}
# Gaussian parameters (if present)
gauss_channels <- grep('(Center|Offset|Width|Residual)', colnames(expr), value = TRUE)
if (length(gauss_channels) > 0) {
cat('Gaussian parameters available for additional QC\n')
}
}
cytof_qc(ff)
Batch QC Summary
Goal: Generate a per-sample QC summary table for an entire experiment batch, flagging outlier samples that may need exclusion.
Approach: Loop through FCS files, compute event counts, flow rate CV, and median signal intensity for each, then flag samples with abnormal event counts or unstable flow rates.