| name | bio-tcr-bcr-analysis-scirpy-analysis |
| description | Analyze single-cell TCR and BCR data integrated with gene expression using scirpy. Use when working with 10x Genomics VDJ data alongside scRNA-seq or when integrating immune receptor information with cell state analysis. |
| tool_type | python |
| primary_tool | scirpy |
Version Compatibility
Reference examples tested with: MiXCR 4.6+, VDJtools 1.2.1+, scanpy 1.10+
Before using code patterns, verify installed versions match. If versions differ:
- Python:
pip show <package> then help(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.
scirpy Analysis
"Analyze single-cell TCR/BCR with gene expression" → Integrate immune receptor clonotype data with scRNA-seq gene expression for joint analysis of clonal expansion and cell state.
- Python:
scirpy.io.read_10x_vdj(), scirpy.tl.clonal_expansion(), scirpy.tl.clonotype_network()
Load VDJ Data
Goal: Import single-cell VDJ annotations and integrate them with an existing scRNA-seq AnnData object.
Approach: Read 10x filtered_contig_annotations or AIRR-format files and attach receptor metadata to the AnnData obs.
import scirpy as ir
import scanpy as sc
adata = sc.read_h5ad('scrnaseq.h5ad')
ir.io.read_10x_vdj(adata, 'filtered_contig_annotations.csv')
ir.io.read_airr(adata, 'airr_rearrangement.tsv')
Quality Control
Goal: Identify cells with aberrant chain pairing (doublets, orphan chains, ambiguous pairings).
Approach: Run scirpy chain QC to categorize cells by receptor chain status and visualize QC distributions.
ir.tl.chain_qc(adata)
ir.pl.group_abundance(adata, groupby='chain_pairing', target_col='receptor_subtype')
Define Clonotypes
ir.pp.ir_dist(
adata,
metric='identity',
sequence='aa',
cutoff=0
)
ir.tl.define_clonotypes(adata, receptor_arms='all', dual_ir='primary_only')
print(f"Unique clonotypes: {adata.obs['clone_id'].nunique()}")
Clonal Expansion
ir.tl.clonal_expansion(adata)
ir.pl.clonal_expansion(adata, groupby='cell_type')
Repertoire Diversity
diversity = ir.tl.repertoire_overlap(
adata,
groupby='sample',
target_col='clone_id',
metric='jaccard'
)
ir.tl.alpha_diversity(adata, groupby='sample', target_col='clone_id')
Compare Groups
ir.pl.group_abundance(
adata,
groupby='clone_id',
target_col='condition',
max_cols=20
)
ir.pl.repertoire_overlap(adata, groupby='sample', target_col='clone_id')
V(D)J Gene Usage
ir.pl.vdj_usage(
adata,
vdj_cols=['v_call_TRA', 'v_call_TRB'],
full_names=False
)
ir.pl.spectratype(adata, chain='TRB', target_col='cell_type')
Integration with Gene Expression
adata_tcr = adata[adata.obs['has_ir'] == 'True'].copy()
adata_tcr.obs['is_expanded'] = adata_tcr.obs['clonal_expansion'].isin(['3-10', '>10'])
sc.tl.rank_genes_groups(adata_tcr, groupby='is_expanded')
sc.pl.rank_genes_groups(adata_tcr, n_genes=20)
sc.pl.umap(adata_tcr, color=['cell_type', 'clonal_expansion'])
Export for Downstream Analysis
clonotypes = adata.obs[['clone_id', 'IR_VDJ_1_junction_aa', 'IR_VJ_1_junction_aa',
'IR_VDJ_1_v_call', 'IR_VDJ_1_j_call']].drop_duplicates()
clonotypes.to_csv('clonotypes.csv')
ir.io.write_airr(adata, 'scirpy_airr.tsv')
Related Skills
- mixcr-analysis - Process raw VDJ FASTQ
- single-cell/data-io - Load scRNA-seq data
- single-cell/clustering - Cell type annotation