SKILL.md
Version Compatibility
Reference examples tested with: FlowSOM 2.10+, anndata 0.10+, matplotlib 3.8+, numpy 1.26+, pandas 2.2+, scanpy 1.10+, scikit-learn 1.4+
Before using code patterns, verify installed versions match. If versions differ:
- Python:
pip show <package>thenhelp(module.function)to check signatures
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Cell Phenotyping for IMC
"Assign cell types to my segmented IMC cells" → Classify cells based on protein marker expression using clustering, manual gating, or supervised classification approaches.
- Python:
scanpy.tl.leiden()for unsupervised clustering, then manual annotation - R:
FlowSOMfor self-organizing map-based phenotyping
Load Single-Cell Data
import anndata as ad
import scanpy as sc
import pandas as pd
import numpy as np
# Load from h5ad
adata = ad.read_h5ad('imc_segmented.h5ad')
# Or create from CSVs
intensities = pd.read_csv('cell_intensities.csv')
cell_info = pd.read_csv('cell_info.csv')
adata = ad.AnnData(X=intensities.values)
adata.var_names = intensities.columns
adata.obs = cell_info
Data Transformation
# Arcsinh transformation (standard for cytometry)
def arcsinh_transform(adata, cofactor=5):
adata.X = np.arcsinh(adata.X / cofactor)
return adata
adata = arcsinh_transform(adata)
# Z-score normalization
sc.pp.scale(adata, max_value=10)
Clustering-Based Phenotyping
# PCA and neighbors
sc.pp.pca(adata, n_comps=15)
sc.pp.neighbors(adata, n_neighbors=15, n_pcs=15)
# Clustering
sc.tl.leiden(adata, resolution=0.5)
# UMAP for visualization
sc.tl.umap(adata)
# Plot
sc.pl.umap(adata, color='leiden', save='_clusters.png')
Manual Gating
def gate_cells(adata, marker, threshold, above=True):
'''Gate cells based on marker expression'''
values = adata[:, marker].X.flatten()
if above:
return values > threshold
else:
return values < threshold
# Example gating strategy for T cells
adata.obs['CD45_pos'] = gate_cells(adata, 'CD45', 1.5)
adata.obs['CD3_pos'] = gate_cells(adata, 'CD3', 1.0)
adata.obs['CD8_pos'] = gate_cells(adata, 'CD8', 0.8)
adata.obs['CD4_pos'] = gate_cells(adata, 'CD4', 0.8)
# Assign cell types
def assign_cell_type(row):
if not row['CD45_pos']:
return 'Other'
if not row['CD3_pos']:
return 'Non-T immune'
if row['CD8_pos']:
return 'CD8 T cell'
if row['CD4_pos']:
return 'CD4 T cell'
return 'T cell (other)'
adata.obs['cell_type'] = adata.obs.apply(assign_cell_type, axis=1)
