Clonalstats
by pwwang
Generate comprehensive clonality statistics and diversity visualizations for TCR/BCR repertoire analysis. Quantifies clonal expansion, measures diversity metrics (Shannon, Simpson, Gini), and creates publication-ready plots.
Skill Details
Repository Files
1 file in this skill directory
name: clonalstats description: Generate comprehensive clonality statistics and diversity visualizations for TCR/BCR repertoire analysis. Quantifies clonal expansion, measures diversity metrics (Shannon, Simpson, Gini), and creates publication-ready plots.
ClonalStats Process Configuration
Purpose
Generate comprehensive clonality statistics and diversity visualizations for TCR/BCR repertoire analysis. Quantifies clonal expansion, measures diversity metrics (Shannon, Simpson, Gini), and creates publication-ready plots.
When to Use
- To quantify clonal expansion patterns in TCR/BCR data
- For diversity analysis comparing multiple samples or conditions
- To identify hyperexpanded clones and their distribution
- For rarefaction analysis to assess sampling depth
- After
ScRepCombiningExpressionto analyze integrated TCR+RNA data
Configuration Structure
Process Enablement
[ClonalStats]
cache = true
Input Specification
[ClonalStats.in]
screpfile = ["ScRepCombiningExpression"]
Core Environment Variables
[ClonalStats.envs]
# Clone definition: "gene" (VDJC), "aa" (CDR3 amino acid), "nt" (CDR3 nucleotide)
clone_call = "aa"
# Chain analysis: "both", "TRA", "TRB", "TRG", "IGH", "IGL"
chain = "both"
# Data transformations (dplyr::mutate syntax)
mutaters = {}
# Data filtering (dplyr::filter syntax)
subset = null
# Output device parameters
devpars = {width = 800, height = 600, res = 100}
# Save code and data (large files - use with caution)
save_code = false
save_data = false
Case-Based Plot Generation
[ClonalStats.envs.cases."Case Name"]
viz_type = "volume" # volume, abundance, length, residency, stat,
# composition, overlap, diversity, geneusage,
# positional, kmer, rarefaction
Diversity Metrics
| Metric | Range | Interpretation | Best For |
|---|---|---|---|
| shannon | 0 - ∞ | Higher = more diversity | General comparison |
| inv.simpson | 1 - ∞ | Higher = more diversity | Common clones |
| gini.coeff | 0 - 1 | 0 = equality, 1 = inequality | Clonality dominance |
| norm.entropy | 0 - 1 | Higher = more diversity | Evenness-focused |
| chao1 | ≥ richness | Estimates total richness | Small samples |
| d50 | Count | Clones making up 50% | Practical dominance |
Interpretation:
- High diversity = Many unique clones, even distribution (healthy repertoire)
- Low diversity = Few dominant clones (antigen-specific response, infection, cancer)
- Gini ≈ 1 = Very skewed, few clones dominate
- Gini ≈ 0 = Even distribution
Visualization Types
viz_type options:
volume- Number of clones per sample/groupabundance- Clone abundance distribution (trend/histogram/density)length- CDR3 sequence length distributionresidency- Clones present across groups (venn/upset)stat- Expanded clone analysis (pies/sankey)diversity- Diversity metrics (bar/box/violin)geneusage- V/D/J gene usage frequencyrarefaction- Sampling depth assessment
Configuration Examples
Minimal Configuration
[ClonalStats.in]
screpfile = ["ScRepCombiningExpression"]
Standard Diversity Analysis
[ClonalStats.in]
screpfile = ["ScRepCombiningExpression"]
[ClonalStats.envs.cases."Diversity"]
viz_type = "diversity"
method = "shannon"
plot_type = "box"
group_by = "Diagnosis"
comparisons = true
[ClonalStats.envs.cases."Gini Coeff"]
viz_type = "diversity"
method = "gini.coeff"
plot_type = "violin"
group_by = "Diagnosis"
add_box = true
Expanded Clone Analysis
[ClonalStats.in]
screpfile = ["ScRepCombiningExpression"]
[ClonalStats.envs.cases."Expanded Clones"]
viz_type = "stat"
plot_type = "pies"
group_by = "Diagnosis"
subgroup_by = "seurat_clusters"
clones = {"Expanded (>2)" = "sel(Colitis > 2)"}
Rarefaction Analysis
[ClonalStats.in]
screpfile = ["ScRepCombiningExpression"]
[ClonalStats.envs.cases."Rarefaction"]
viz_type = "rarefaction"
group_by = "Patient"
q = 1 # 0=richness, 1=shannon, 2=simpson
n_boots = 20
Complete Analysis Suite
[ClonalStats.in]
screpfile = ["ScRepCombiningExpression"]
[ClonalStats.envs.cases."Volume"]
viz_type = "volume"
[ClonalStats.envs.cases."Abundance"]
viz_type = "abundance"
plot_type = "density"
[ClonalStats.envs.cases."Diversity"]
viz_type = "diversity"
method = "shannon"
[ClonalStats.envs.cases."Rarefaction"]
viz_type = "rarefaction"
Common Patterns
Disease vs Healthy
[ClonalStats.envs.cases."Comparison"]
viz_type = "diversity"
method = "gini.coeff"
plot_type = "box"
group_by = "Condition"
comparisons = true
Time Course
[ClonalStats.envs.cases."Timepoint"]
viz_type = "volume"
x = "Timepoint"
[ClonalStats.envs.cases."Diversity"]
viz_type = "diversity"
method = "shannon"
group_by = "Timepoint"
Treatment Response
[ClonalStats.envs.cases."Response"]
viz_type = "diversity"
method = "gini.coeff"
group_by = "Response"
plot_type = "box"
comparisons = true
Dependencies
- Upstream:
ScRepCombiningExpression(required) - Related:
ScRepLoading,CDR3Clustering,TESSA(optional)
Validation Rules
- Input must be valid scRepertoire object
- For
viz_type = "diversity", method must be supported - For rarefaction,
n_bootsshould be ≥ 10 - Use
sel()syntax inclonesparameter for filtering
Troubleshooting
Sample column not found: Input must have Sample column or specify x parameter.
Strange diversity values: Small repertoire sizes cause bias. Use plot_type = "box".
Rarefaction curves noisy: Increase n_boots (try 50-100).
Too many clones in stat plots: Use subset or stricter clones thresholds.
Plot generation slow: Use clone_call = "gene" for speed, apply subset.
Missing comparisons: Set comparisons = true to add significance tests.
Best Practices
- Start with default cases to see standard visualizations
- Use multiple diversity metrics: Shannon + Gini
- Check rarefaction curves to ensure sufficient sampling
- Document clone thresholds when defining expanded clones
- Use
clone_call = "gene"for speed, "aa" for granularity - Set
save_data = truefor debugging (watch disk space) - Validate findings with complementary diversity indices
- Consider sample size: small samples underestimate richness
Related Skills
Attack Tree Construction
Build comprehensive attack trees to visualize threat paths. Use when mapping attack scenarios, identifying defense gaps, or communicating security risks to stakeholders.
Grafana Dashboards
Create and manage production Grafana dashboards for real-time visualization of system and application metrics. Use when building monitoring dashboards, visualizing metrics, or creating operational observability interfaces.
Matplotlib
Foundational plotting library. Create line plots, scatter, bar, histograms, heatmaps, 3D, subplots, export PNG/PDF/SVG, for scientific visualization and publication figures.
Scientific Visualization
Create publication figures with matplotlib/seaborn/plotly. Multi-panel layouts, error bars, significance markers, colorblind-safe, export PDF/EPS/TIFF, for journal-ready scientific plots.
Seaborn
Statistical visualization. Scatter, box, violin, heatmaps, pair plots, regression, correlation matrices, KDE, faceted plots, for exploratory analysis and publication figures.
Shap
Model interpretability and explainability using SHAP (SHapley Additive exPlanations). Use this skill when explaining machine learning model predictions, computing feature importance, generating SHAP plots (waterfall, beeswarm, bar, scatter, force, heatmap), debugging models, analyzing model bias or fairness, comparing models, or implementing explainable AI. Works with tree-based models (XGBoost, LightGBM, Random Forest), deep learning (TensorFlow, PyTorch), linear models, and any black-box model
Pydeseq2
Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.
Query Writing
For writing and executing SQL queries - from simple single-table queries to complex multi-table JOINs and aggregations
Pydeseq2
Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.
Scientific Visualization
Meta-skill for publication-ready figures. Use when creating journal submission figures requiring multi-panel layouts, significance annotations, error bars, colorblind-safe palettes, and specific journal formatting (Nature, Science, Cell). Orchestrates matplotlib/seaborn/plotly with publication styles. For quick exploration use seaborn or plotly directly.
