Cell Annotation: AI-based

· 5 min read · Analysis Notes

Cell Annotation: AI-based

Cell Annotation: AI-based

Cell Annotation: AI-based

TL;DR: Use foundation models like scGPT or LLMs like GPT-4 for automated cell type annotation. scGPT learns from single-cell data directly; GPTcelltype uses marker genes as prompts. Both integrate with Seurat and reduce manual curation time.


Why AI for Cell Annotation?

Traditional annotation relies on manual marker gene lookup and expert knowledge. AI-based methods offer:

  • Speed: Annotate thousands of clusters in seconds
  • Consistency: Reproducible across datasets
  • Scalability: Handles rare cell types and novel tissues
  • Knowledge integration: Leverages millions of annotated cells or biomedical literature

Comparison of Approaches

Method Type Input Training Data Best For
scGPT Foundation model Gene expression 33M cells Novel cell types, cross-species
GPTcelltype LLM (GPT-4) Marker gene lists Biomedical text Standard tissues, quick annotation
scimilarity Cell embedding Expression profiles Reference atlases Known cell types

Method 1: scGPT (Foundation Model)

scGPT is a transformer model pre-trained on 33 million cells. It learns cell type representations directly from gene expression.

When to Use

  • ✅ Novel or poorly characterized cell types
  • ✅ Cross-species annotation
  • ✅ Integration with perturbation analysis
  • ⚠️ Requires GPU and Python environment

Installation

# Create conda environment (Python 3.7-3.10 required)
conda create -n scGPT python=3.10 r-base
conda activate scGPT

# Install stable version
pip install scgpt

# Or development version with Poetry
curl -sSL https://install.python-poetry.org | python3 -
poetry install  # in scGPT repo directory

Note: scGPT works best on GPU. For CPU-only usage, set device='cpu' in inference calls.

Basic Usage

import scgpt

# Load pre-trained model
model = scgpt.load_model("scGPT_human")

# Annotate Scanpy AnnData object
adata = sc.read_h5ad("pbmc.h5ad")
predictions = scgpt.tasks.cell_type_annotation(model, adata)

# Add predictions to metadata
adata.obs['scGPT_celltype'] = predictions

Integration with R/Seurat

Use reticulate to call scGPT from R:

library(reticulate)
library(Seurat)

# Convert Seurat to AnnData
sc <- use_condaenv("scGPT")
scgpt <- import("scgpt")
anndata <- import("anndata")

# Export Seurat object
SaveH5Seurat(pbmc, "pbmc.h5seurat")
Convert("pbmc.h5seurat", dest = "h5ad")

# Run scGPT annotation (in Python)
adata <- anndata$read_h5ad("pbmc.h5ad")
predictions <- scgpt$tasks$cell_type_annotation(model, adata)

# Import results back to Seurat
pbmc$scGPT_celltype <- predictions

Method 2: GPTcelltype (LLM-based)

GPTcelltype uses OpenAI's GPT-4 to infer cell types from marker gene lists. It integrates seamlessly with Seurat workflows.

When to Use

  • ✅ Standard tissues (PBMC, brain, lung, etc.)
  • ✅ Quick annotation without setup
  • ✅ Seurat-based workflows
  • ⚠️ Requires OpenAI API key ($$$)
  • ⚠️ Risk of AI hallucination

Installation

# Install from GitHub
if (!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("Winnie09/GPTCelltype")

library(GPTCelltype)

Setup API Key

Get your API key from platform.openai.com/api-keys:

# Set API key (or add to .Renviron)
Sys.setenv(OPENAI_API_KEY = "sk-...")

# Verify
Sys.getenv("OPENAI_API_KEY")

Basic Usage with Seurat

library(Seurat)
library(GPTCelltype)

# Load PBMC data
pbmc <- readRDS("pbmc_clustered.rds")

# Find markers for all clusters
all.markers <- FindAllMarkers(pbmc, only.pos = TRUE)

# Annotate with GPT-4
celltypes <- gptcelltype(
  input = all.markers,
  tissuename = "human PBMC",
  model = "gpt-4",
  topgenenumber = 10
)

# Add annotations to Seurat object
names(celltypes) <- levels(pbmc$seurat_clusters)
pbmc$GPT_celltype <- celltypes[pbmc$seurat_clusters]

Custom Gene Lists

# Define marker sets manually
markers <- list(
  cluster0 = c("CD3D", "CD3E", "CD4"),
  cluster1 = c("CD14", "LYZ", "S100A8"),
  cluster2 = c("CD79A", "MS4A1", "CD19")
)

# Annotate
celltypes <- gptcelltype(
  input = markers,
  tissuename = "PBMC",
  model = "gpt-4"
)

Cost Considerations

GPTcelltype uses OpenAI API:

  • GPT-4: ~$0.03 per annotation run (30 clusters)
  • GPT-3.5-turbo: Cheaper but less accurate
# Use GPT-3.5 to save costs
celltypes <- gptcelltype(all.markers, model = "gpt-3.5-turbo")

Method 3: scimilarity (Reference-based)

scimilarity uses pre-computed cell embeddings from reference atlases.

Quick Start

from scimilarity import CellAnnotation

# Load annotator with CellTypist model
annotator = CellAnnotation(model="human_immune")

# Annotate
predictions = annotator.predict(adata)
adata.obs['scimilarity_celltype'] = predictions

Best for: Fast annotation when reference atlases match your tissue.


Comparison & Recommendations

Choose scGPT if:

  • Studying rare/novel cell types
  • Working with non-human species
  • Need embedding for downstream tasks

Choose GPTcelltype if:

  • Analyzing standard tissues (PBMC, tumor, brain)
  • Want quick annotation without Python setup
  • Already using Seurat

Choose scimilarity if:

  • Your tissue is well-represented in references
  • Need fast, lightweight annotation
  • Working in Python (Scanpy)

Validation Best Practices

Always validate AI predictions:

  1. Check known markers

    # Violin plot for canonical markers
    VlnPlot(pbmc, features = c("CD3D", "CD14", "MS4A1"), 
            group.by = "GPT_celltype")
    
  2. Compare with manual annotation

    # Confusion matrix
    table(Manual = pbmc$manual_celltype, 
          AI = pbmc$GPT_celltype)
    
  3. Review low-confidence predictions

    • scGPT: Check prediction probabilities
    • GPTcelltype: Inspect marker genes used in prompt
  4. Use multiple methods

    # Consensus annotation
    pbmc$consensus <- ifelse(
      pbmc$scGPT_celltype == pbmc$GPT_celltype,
      pbmc$scGPT_celltype,
      "Ambiguous"
    )
    

Troubleshooting

GPTcelltype API errors

Error: "Rate limit exceeded"
Fix: Add delays between requests or upgrade API plan

scGPT memory errors

Error: CUDA out of memory
Fix: Reduce batch size or use CPU mode:

predictions = scgpt.tasks.cell_type_annotation(
    model, adata, device='cpu', batch_size=32
)

Inconsistent predictions

Cause: Insufficient marker genes or poor clustering
Fix: Increase topgenenumber or refine clustering resolution


Resources


See also:


Originally posted on WeChat Official Account: bioinfo_studyroom

Cell Annotation