Skip to content

FCS File I/O

For each cell that passes the laser, a flow cytometer records the signal on every fluorescence detector and on the forward and side scatter detectors. The instrument writes the result as an FCS file, a matrix of events by parameters with a text segment of keywords that describe the run. The FCS format page takes the binary layout apart. This page reads the files into R with flowCore.

One FCS file holds one sample, and a real experiment produces dozens of them. The first job is to read them as a set and attach the metadata that says which file is which condition. flowCore splits that metadata between two places. The parameter table describes the columns, and the keywords describe the run.

flowCore is the base package for flow cytometry data in R. The example lists the three FCS files that ship inside the package and opens the first, which a FACSCalibur wrote.

The FACSCalibur example files come from the flowCore 2.24.0 source archive. Its Artistic 2.0 license covers the distributed file collection. The fixture source notice identifies the package authors, original files, and derived lesson results. The complete license accompanies that notice.

library(flowCore)
extdata_dir <- system.file("extdata", package = "flowCore")
fcs_files <- list.files(extdata_dir, pattern = "0877408774", full.names = TRUE)
basename(fcs_files)
# Keep the values exactly as the instrument stored them.
frame <- read.FCS(fcs_files[1], transformation = FALSE)
# The expression matrix.
expr <- exprs(frame)
dim(expr)
head(expr, 3)
# The parameter table: one row per column.
params <- parameters(frame)
head(pData(params), 3)
[1] "0877408774.B08" "0877408774.E07" "0877408774.F06"
[1] 10000 8
FSC-H SSC-H FL1-H FL2-H FL3-H FL1-A FL4-H Time
[1,] 382 77 618 0 225 55 286 1
[2,] 628 280 245 431 259 0 371 1
[3,] 1023 735 699 448 215 143 638 1
name desc range minRange maxRange
$P1 FSC-H FSC-H 1024 0 1023
$P2 SSC-H SSC-H 1024 0 1023
$P3 FL1-H <NA> 1024 0 1023

exprs() returns the numeric matrix, here 10000 events across 8 channels. parameters() returns an AnnotatedDataFrame with one row per detector, and its columns hold the channel name, the fluorophore and the range. The channel names come back when you build a gating template and when you match markers across files.

transformation = FALSE keeps the numbers the instrument stored. On this FACSCalibur the four fluorescence detectors sit behind a log amplifier, which the $P3E keyword printed further down records, so their values are channel numbers on a log scale from 0 to 1023. The default read converts them back to linear intensities, which is what compensation needs, and the compensation page reads the files that way.

Every cytometry analysis starts with forward scatter against side scatter, which is roughly size against granularity. It is where you find the cells, and where you find the debris and the dead events you do not want.

library(ggcyto)
library(ggplot2)
dir.create("outputs", showWarnings = FALSE, recursive = TRUE)
scatter_plot <- ggcyto(frame, aes(x = `FSC-H`, y = `SSC-H`)) +
geom_hex(bins = 80) +
labs(title = "Forward against side scatter, one FACSCalibur file")
ggsave("outputs/cyto-fsc-ssc.png", plot = scatter_plot,
width = 6, height = 5, dpi = 110, bg = "white")

Forward scatter against side scatter for the 10000 events in the file. A dense population sits in the lower middle, around 450 on forward scatter and 175 on side scatter. Events spread up and to the right from it, a vertical line of events stands at the right edge where forward scatter reached 1023, and a row of events runs along the top edge where side scatter reached 1023.

The vertical line at the right edge is the detector saturating. Forward scatter is stored in 1024 channels, and every event that exceeded the top of that range was recorded as 1023, so those events carry a bound rather than a value. Side scatter saturates the same way, and its saturated events form the row along the top edge. A gate drawn through either set is measuring the instrument.

read.flowSet reads every file matching a pattern in a directory and returns a flowSet, a list of frames that share one column structure. The sample metadata attaches as a pheno data frame, one row per file.

fs <- read.flowSet(
path = extdata_dir,
pattern = "0877408774",
transformation = FALSE
)
# Number of files and shared column names.
length(fs)
colnames(fs)
# The per-file metadata table. The name column holds the file names.
head(pData(fs), 3)
[1] 3
[1] "FSC-H" "SSC-H" "FL1-H" "FL2-H" "FL3-H" "FL1-A" "FL4-H" "Time"
name
0877408774.B08 0877408774.B08
0877408774.E07 0877408774.E07
0877408774.F06 0877408774.F06

The pheno data frame carries one row per file, and its name column holds the file name. That name is the stable identifier. Once the files are read, the experimental metadata, the condition, the donor, the panel, merges onto it.

An FCS file also carries a text segment, a list of keyword-value pairs. Keywords prefixed with $ belong to the standard, and the rest are vendor-specific. The ones worth keeping are the spillover matrix, the instrument, the acquisition date, the fluorophore on each detector and the amplifier each detector went through.

# Keywords for the first file in the set. $P3E is the amplifier keyword of
# the third parameter, FL1-H.
frame1 <- fs[[1]]
keyword(frame1, c("$FIL", "$DATE", "$CYT", "FCSversion", "$P3E"))
$`$FIL`
[1] "0877408774.B08"
$`$DATE`
[1] "03-Feb-06"
$`$CYT`
[1] "FACSCalibur"
$FCSversion
[1] "2"
$`$P3E`
[1] "4,1"

FCSversion has no $ because flowCore takes it from the file header rather than from the text segment, and this file follows FCS 2.0. $P3E holds two numbers: the decades a log amplifier spans, then the linear value of channel zero. FL1-H spans four decades, and a linear detector reads 0,0.

The spillover matrix lives under $SPILLOVER in a file written to the FCS 3.1 standard, and under the older SPILL keyword that many instruments still write. flowCore’s spillover() looks for both. These three files carry neither, because they follow FCS 2.0, which has no keyword for a spillover matrix. The FACSCalibur is an analog instrument that compensated in its electronics before it wrote the file, so any compensation the operator set is already inside the stored values. The compensation page shows what a second matrix does to them.

The reproducible unit is the annotated file, not the file alone. Build the sample metadata table early, one row per FCS file, and keep it beside the data.

sample_metadata <- data.frame(
file = pData(fs)$name,
sample_id = sprintf("S%02d", seq_len(length(fs))),
condition = rep(c("control", "treated"), length.out = length(fs))
)
head(sample_metadata, 3)
file sample_id condition
1 0877408774.B08 S01 control
2 0877408774.E07 S02 treated
3 0877408774.F06 S03 control

Reading files as a set is worth it because you can compare them immediately. The same channel plotted across all three is the first thing to look at, and on these three files it is not reassuring.

library(ggcyto)
library(ggplot2)
dir.create("outputs", showWarnings = FALSE, recursive = TRUE)
fl1_plot <- ggcyto(fs, aes(x = `FL1-H`)) +
geom_density(fill = "steelblue", alpha = 0.4) +
labs(title = "FL1-H across the three files")
ggsave("outputs/cyto-flowset-fl1.png", plot = fl1_plot,
width = 8, height = 3, dpi = 110, bg = "white")

Density of FL1-H for each of the three files, one panel each. The B08 file has two peaks, one near 230 and one near 600. The E07 file has a single peak near 650. The F06 file has a single peak near 250 with a small shoulder near 400.

The three files do not agree. B08 is bimodal, E07 sits almost entirely in the positive range, and F06 sits almost entirely in the negative range with a small shoulder. That pattern is either three different samples, three different stains, or a problem, and the plot cannot tell you which. The sample metadata table can, which is the argument for building it before you look at anything.

ggcyto gives one panel per file without being asked, because it knows the object is a set. Three files fit on a screen and thirty do not, which is the other reason the metadata table above stops being bookkeeping.

In a real study this table is written by hand or parsed from the file names, and it is what the gating and clustering steps receive. The gating, automated gating, clustering, cell identification and harmonisation pages use the bundled Bodenmiller dataset, which ships with its metadata already attached.

The flowCore package by Ellis and colleagues supplies the basic data structures for flow cytometry in R.

The flowCore documentation describes read.FCS, read.flowSet, parameter metadata, and keyword access.

The ggcyto flowSet tutorial shows density and scatter plots across samples and explains automatic facets.

The FCS 3.1 standard paper explains the header and text segments, standardized spillover metadata, and the earlier compensation keywords.