Skip to main content

A PyTorch-based tool for predicting missing proteins in cytometry/single-cell data

Project description

CyStainer

CyStainer package for cytometry marker imputation

CyStainer is a PyTorch-based deep learning tool for predicting missing proteins and imputing marker expression in cytometry and single-cell data. It utilizes a combination of Variational Autoencoders (VAEs) and Transformer architectures to integrate multiple batches/panels and infer missing markers accurately.

📦 Installation

Since CyStainer is built on PyTorch, ensure you have an environment with Python 3.11+ and the appropriate PyTorch version for your hardware (CUDA recommended).

You can install CyStainer directly from the source:

git clone https://github.com/sysgen-uef/cystainer_package.git
cd cystainer_package
pip install .

🧹 Data Preprocessing (From .fcs to .h5ad)

CyStainer expects your input data to be formatted as AnnData objects (either passed as a list in memory or saved locally as .h5ad files). Before feeding your cytometry data into the model, it must be properly preprocessed.

Mandatory Pre-processing:

  • Cleaning: Ensure your data is pre-gated to remove doublets, debris, and dead cells.
  • Compensation: Your .fcs files should be already compensated.

Transformation & Scaling (Recommended): You will typically need to transform your fluorescence intensities (e.g., using an arcsinh transformation) and optionally scale them. Removing saturated values (extreme highs or zeros) can also improve model performance depending on your dataset.

Here is a minimal example of how to process a standard .fcs file into a ready-to-use .h5ad file using FlowKit and AnnData:

import flowkit as fk
import pandas as pd
import numpy as np
import anndata as ad

# Load the compensated and cleaned .fcs file
sample = fk.Sample('path/to/cleaned_sample.fcs')
df = sample.as_dataframe('raw')
df.columns = sample.pnn_labels # Set marker names

# Transformation (e.g., arcsinh with a cofactor of 100 or 150)
cofactor = 100
non_scatter = ['FSC' not in c and 'SSC' not in c for c in df.columns]
df.loc[:, non_scatter] = np.arcsinh(df.loc[:, non_scatter] / cofactor)

# Optional: Remove saturation / extreme outliers
# Useful if your instrument records artificial bounds (e.g., exactly 0 or max value)
df = df[~np.any(((df <= 0) | (df >= df.max().max())), axis=1)]

# Optional: Scaling (Min-Max scaling to [0, 1] or Z-score normalization)
# Min-Max Scaling example:
df = (df - df.min()) / (df.max() - df.min())
# Z-score Scaling example (alternatively): 
# df = (df - df.mean()) / df.std()

# Convert to AnnData and save for CyStainer
adata = ad.AnnData(df)
adata.write('preprocessed_sample.h5ad', compression='gzip')

Once your .fcs files are converted into .h5ad objects, you can load them directly into your workflow.

🚀 Quick Start Guide

The primary way to interact with the package is through the CyStainer wrapper class. The standard workflow consists of initializing the model, loading training data, building the network, and running inference.

1. Training a Base Model

You can load your data either from a folder of .h5ad files or by passing a list of AnnData objects directly in memory.

from cystainer import CyStainer

# Initialize the stainer (automatically detects CUDA/CPU)
stainer = CyStainer()

# Load training data
# Alternatively, use: adata_list=[adata1, adata2]
# CyStainer includes a built-in utility to visualize how markers overlap across different panels or batches.
stainer.load_train_data(folder_path='./data_example/train', get_panel_vis=True)

image

# Build the model 
# You can pass custom hyperparameters here if needed
stainer.build_model()

# Train the model
stainer.train()

# By default the model is automatically saved in the same directory
# as cystainer.pt

2. Predicting Missing Markers

Once a model is trained, you can load inference data. The .load_predict_data() method ensures your cells are not shuffled so that the output matches your input order.

# Load prediction data using the base model's reference markers
stainer.load_predict_data(folder_path='./data_example/test')

# Run predictions and save directly to disk
stainer.predict(output_path='imputed_cells.h5ad')

# Alternatively, return the predictions as a pandas DataFrame:
# df_imputed = stainer.predict(return_pred=True)

3. Fine-Tuning on New Batches

If you receive new data from a different batch or panel, you don't need to retrain from scratch. CyStainer can freeze the main network and fine-tune only the batch embeddings to align the new data.

# Load the pre-trained model
stainer = CyStainer.load('cystainer.pt')

# Load the new data for fine-tuning
# Note, anndata objects must have batch info column
stainer.load_finetune_data(folder_path='./data_example/test', batch_info='batch')

# Fine-tune the batch embeddings
stainer.finetune()

# Predict on the newly fine-tuned data
stainer.predict(output_path='imputed_new_batch.h5ad')

4. Batch Correction

CyStainer allows you to translate cells from their original batch to a specific target batch distribution.

stainer.predict(
    output_path='batch_corrected_cells.h5ad',
    correct_batch=True,
    target_batch_name='single_batch' # Must match a batch name in stainer.batch_dict
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cystainer-0.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cystainer-0.1.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file cystainer-0.1.0.tar.gz.

File metadata

  • Download URL: cystainer-0.1.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for cystainer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c447bc12bae8ee512cfafded97288a83914522c1da382abd59f9d20f1209276e
MD5 5954b106c161c0c3c64df6e304ac3993
BLAKE2b-256 42fc5cf01b4744c4cfdf6b6372bb1ee9be777f400d97d6db8fd8a9322bcd284b

See more details on using hashes here.

File details

Details for the file cystainer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cystainer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for cystainer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 016f7dd30463ef77d45847eaa7ddcff9e3bf528891406dbc8c5910abdf8c8975
MD5 24800f9f1ec2c9ab10a0241b209df9d3
BLAKE2b-256 ed2323f0915e277aa523ab01dbc3fe9d5e9fb86b3d2ee305cfb8fb49ef092ac4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page