Preprocess flow data#

In this notebook, we load an fcs file into the anndata format, move the forward scatter (FCS) and sideward scatter (SSC) information to the .obs section of the anndata file and perform compensation on the data. Next, we apply different types of normalisation to the data.

import readfcs
import pytometry as pm
%load_ext autoreload
%autoreload 2

Read data from readfcs package example.

path_data = readfcs.datasets.example()
adata = pm.io.read_fcs(path_data)
adata
AnnData object with n_obs × n_vars = 65016 × 16
    var: 'channel', 'marker'
    uns: 'meta'

Reduce features#

We split the data matrix into the marker intensity part and the FSC/SSC part. Moreover, we move all height related features to the .obs part of the anndata file. Notably. the function split_signal checks if a feature name is either FSC/SSC or whether a name endswith -A for area related features and -H for height related features.

Let us check the var_names of the features and the channel names. In this example, the channel names have been cleaned such that none of the markers have the -A or -H suffix.

adata.var
channel marker
FSC-A FSC-A
FSC-H FSC-H
SSC-A SSC-A
KI67 B515-A KI67
CD3 R780-A CD3
CD28 R710-A CD28
CD45RO R660-A CD45RO
CD8 V800-A CD8
CD4 V655-A CD4
CD57 V585-A CD57
VIVID / CD14 V450-A VIVID / CD14
CCR5 G780-A CCR5
CD19 G710-A CD19
CD27 G660-A CD27
CCR7 G610-A CCR7
CD127 G560-A CD127

We use the channel column of the adata.var data frame to split the matrix.

pm.pp.split_signal(adata, var_key="channel")
adata
AnnData object with n_obs × n_vars = 65016 × 13
    obs: 'FSC-A', 'FSC-H', 'SSC-A'
    var: 'channel', 'marker', 'signal_type'
    uns: 'meta'

The data matrix was reduced by three features (FSC-A, FSC-H and SSC-A).

Compensation#

Next, we compensate the data using the compensation matrix that is included in the FCS file header. Alternatively, one may provide a custom compensation matrix.

The compensate function matches the var_names of adata with the column names of the spillover matrix to compensate the correct channels.

pm.pp.compensate(adata)
5616 NaN values found after compensation. Please adjust compensation matrix.

Normalize data#

In the next step, we normalize the data. By default, normalization is an inplace operation, i.e. we only create a new anndata object, if we set the argument inplace=False. We demonstrate three different normalization methods that are build in pytometry:

  • arcsinh

  • logicle

  • bi-exponential

adata_arcsinh = pm.tl.normalize_arcsinh(adata, cofactor=150, inplace=False)
adata_logicle = pm.tl.normalize_logicle(adata, inplace=False)
/home/runner/work/pytometry/pytometry/.nox/build-3-9/lib/python3.9/site-packages/pytometry/tools/_normalization.py:175: RuntimeWarning: invalid value encountered in double_scalars
  y = (ae2bx + p["f"]) - (ce2mdx + value)
adata_biex = pm.tl.normalize_biExp(adata, inplace=False)