Jupyter Notebook Binder

Flow cytometry#

Here, you will

  1. read a single .fcs file as an AnnData and seed a versioned dataset with it

  2. append a new data batch (a new .fcs file) to create a new version of the dataset

  3. look at an overview of ingested files and cell markers

  4. query the data and store analytical results as plots

Setup#

!lamin init --storage ./test-facs --schema bionty
Hide code cell output
✅ saved: User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-10-01 16:44:24)
✅ saved: Storage(id='mUeGHHxE', root='/home/runner/work/lamin-usecases/lamin-usecases/docs/test-facs', type='local', updated_at=2023-10-01 16:44:24, created_by_id='DzTjkKse')
💡 loaded instance: testuser1/test-facs
💡 did not register local instance on hub (if you want, call `lamin register`)

import lamindb as ln
import lnschema_bionty as lb
import readfcs

lb.settings.species = "human"
💡 loaded instance: testuser1/test-facs (lamindb 0.54.4)
ln.track()
💡 notebook imports: lamindb==0.54.4 lnschema_bionty==0.31.2 pytometry==0.1.4 readfcs==1.1.6 scanpy==1.9.5
💡 Transform(id='OWuTtS4SAponz8', name='Flow cytometry', short_name='facs', version='0', type=notebook, updated_at=2023-10-01 16:44:27, created_by_id='DzTjkKse')
💡 Run(id='TJweM0VKGkTQHyy8CZci', run_at=2023-10-01 16:44:27, transform_id='OWuTtS4SAponz8', created_by_id='DzTjkKse')

Ingest a first file#

Access #

We start with a flow cytometry file from Alpert et al., Nat. Med. (2019).

Calling the following function downloads the file and pre-populates a few relevant registries:

ln.dev.datasets.file_fcs_alpert19(populate_registries=True)
PosixPath('Alpert19.fcs')

We use readfcs to read the raw fcs file into memory and create an AnnData object:

adata = readfcs.read("Alpert19.fcs")
adata
AnnData object with n_obs × n_vars = 166537 × 40
    var: 'n', 'channel', 'marker', '$PnB', '$PnE', '$PnR'
    uns: 'meta'

Transform: normalize #

In this use case, we’d like to ingest & store curated data, and hence, we split signal and normalize using the pytometry package.

import pytometry as pm
pm.pp.split_signal(adata, var_key="channel")
'area' is not in adata.var['signal_type']. Return all.

pm.tl.normalize_arcsinh(adata, cofactor=150)

Validate: cell markers #

First, we validate features in .var using CellMarker:

validated = lb.CellMarker.validate(adata.var.index)
13 terms (32.50%) are not validated for name: Time, Cell_length, Dead, (Ba138)Dd, Bead, CD19, CD4, IgD, CD11b, CD14, CCR6, CCR7, PD-1

We see that many features aren’t validated because they’re not standardized.

Hence, let’s standardize feature names & validate again:

adata.var.index = lb.CellMarker.standardize(adata.var.index)
validated = lb.CellMarker.validate(adata.var.index)
5 terms (12.50%) are not validated for name: Time, Cell_length, Dead, (Ba138)Dd, Bead

The remaining non-validated features don’t appear to be cell markers but rather metadata features.

Let’s move them into adata.obs:

adata.obs = adata[:, ~validated].to_df()
adata = adata[:, validated].copy()

Now we have a clean panel of 35 validated cell markers:

validated = lb.CellMarker.validate(adata.var.index)
assert all(validated)  # all markers are validated

Register: metadata #

Next, let’s register the metadata features we moved to .obs.

For this, we create one feature record for each column in the .obs dataframe:

features = ln.Feature.from_df(adata.obs)
ln.save(features)

We use the Experimental Factor Ontology through Bionty to create a “FACS” label for the dataset:

lb.ExperimentalFactor.bionty().search("FACS").head(2)  # search the public ontology
ontology_id definition synonyms parents molecule instrument measurement __ratio__
name
fluorescence-activated cell sorting EFO:0009108 A Flow Cytometry Assay That Provides A Method ... FACS|FAC sorting [] None None None 100.0
BALB/c EFO:0000602 Balb/C Is A Mouse Strain Of Albion Mice. BALB/cJ|C|BALBc [] None None None 90.0
# import the record from the public ontology and save it to the registry
lb.ExperimentalFactor.from_bionty(ontology_id="EFO:0009108").save()

# show the content of the registry
lb.ExperimentalFactor.filter().df()
name ontology_id abbr synonyms description molecule instrument measurement bionty_source_id updated_at created_by_id
id
lh5Cxy8w fluorescence-activated cell sorting EFO:0009108 None FACS|FAC sorting A Flow Cytometry Assay That Provides A Method ... None None None KxPX 2023-10-01 16:44:33 DzTjkKse

Register: data & annotate with metadata #

modalities = ln.Modality.lookup()
features = ln.Feature.lookup()
efs = lb.ExperimentalFactor.lookup()
species = lb.Species.lookup()
file = ln.File.from_anndata(
    adata, description="Alpert19", field=lb.CellMarker.name, modality=modalities.protein
)
... storing '$PnE' as categorical
... storing '$PnR' as categorical
file.save()

Inspect the registered file#

Inspect features on a high level:

file.features
Features:
  var: FeatureSet(id='bnQuiJvBNk4ejenZnNN1', n=35, type='number', registry='bionty.CellMarker', hash='ldY9_GmptHLCcT7Nrpgo', updated_at=2023-10-01 16:44:33, modality_id='eQeesRLr', created_by_id='DzTjkKse')
    'ICOS', 'CD16', 'CD25', 'Cd4', 'HLADR', 'CD3', 'Ccr6', 'CD57', 'CD161', 'DNA1', 'CD56', 'CXCR5', 'CD20', 'Cd14', 'CD8', 'CD127', 'PD1', 'CD85j', 'CXCR3', 'CD27', ...
  obs: FeatureSet(id='5A836rl3Y9Gcm5nZNyXv', n=5, registry='core.Feature', hash='KdKUyw2SnCQDA_SARN3_', updated_at=2023-10-01 16:44:33, modality_id='j6I6Ej4f', created_by_id='DzTjkKse')
    Cell_length (number)
    Time (number)
    Dead (number)
    Bead (number)
    (Ba138)Dd (number)

Inspect low-level features in .var:

file.features["var"].df().head()
name synonyms gene_symbol ncbi_gene_id uniprotkb_id species_id bionty_source_id updated_at created_by_id
id
0vAls2cmLKWq ICOS ICOS 29851 Q53QY6 uHJU 8RAX 2023-10-01 16:44:29 DzTjkKse
bspnQ0igku6c CD16 FCGR3A 2215 O75015 uHJU 8RAX 2023-10-01 16:44:29 DzTjkKse
50v4SaR2m5zQ CD25 IL2RA 3559 P01589 uHJU 8RAX 2023-10-01 16:44:29 DzTjkKse
HEK41hvaIazP Cd4 CD4 920 B4DT49 uHJU 8RAX 2023-10-01 16:44:29 DzTjkKse
k0zGbSgZEX3q HLADR HLA‐DR|HLA-DR|HLA DR None None None uHJU 8RAX 2023-10-01 16:44:29 DzTjkKse

Use auto-complete for marker names:

markers = file.features["var"].lookup()
import scanpy as sc

sc.pp.pca(adata)
sc.pl.pca(adata, color=markers.cd14.name)
https://d33wubrfki0l68.cloudfront.net/6cf1cff3b13837e5b37022281f4e5617009f2fb7/1d71f/_images/6028acd809d1a761a952221d3c0d91f30c5ae89a51328d053674a3a362a3d248.png
file.view_flow()
https://d33wubrfki0l68.cloudfront.net/7f2a1e9b0a62498d460441c30ca641e1a3dc6dfc/2a44b/_images/0b142fff0b451fbdd015542eab01ddb7e0871f8653333b3692ff7a62de806af9.svg

Create a dataset from the file#

dataset = ln.Dataset(file, name="My versioned FACS dataset", version="1")

dataset
Dataset(id='8zlLWz5kwz4eVoYxBRwf', name='My versioned FACS dataset', version='1', hash='Piw2n0vdnoNoAV7ZxgsW-g', transform_id='OWuTtS4SAponz8', run_id='TJweM0VKGkTQHyy8CZci', file_id='8zlLWz5kwz4eVoYxBRwf', created_by_id='DzTjkKse')

Let’s inspect the features measured in this dataset which were inherited from the file:

dataset.features
Features:
  var: FeatureSet(id='bnQuiJvBNk4ejenZnNN1', n=35, type='number', registry='bionty.CellMarker', hash='ldY9_GmptHLCcT7Nrpgo', updated_at=2023-10-01 16:44:33, modality_id='eQeesRLr', created_by_id='DzTjkKse')
    'ICOS', 'CD16', 'CD25', 'Cd4', 'HLADR', 'CD3', 'Ccr6', 'CD57', 'CD161', 'DNA1', 'CD56', 'CXCR5', 'CD20', 'Cd14', 'CD8', 'CD127', 'PD1', 'CD85j', 'CXCR3', 'CD27', ...
  obs: FeatureSet(id='5A836rl3Y9Gcm5nZNyXv', n=5, registry='core.Feature', hash='KdKUyw2SnCQDA_SARN3_', updated_at=2023-10-01 16:44:33, modality_id='j6I6Ej4f', created_by_id='DzTjkKse')
    Cell_length (number)
    Time (number)
    Dead (number)
    Bead (number)
    (Ba138)Dd (number)

This looks all good, hence, let’s save it:

dataset.save()

Annotate by linking FACS & species labels:

dataset.labels.add(efs.fluorescence_activated_cell_sorting, features.assay)
dataset.labels.add(species.human, features.species)
dataset.view_flow()
https://d33wubrfki0l68.cloudfront.net/fa6059f20e0dd15d3fb235e434f9885c61f1a00a/c3f5e/_images/18a5c625dc12a41d254c3f6f0f5660aff607d8efc1815d1bdada989582e28454.svg