$npx -y skills add LeonChaoX/qinyan-academic-skills --skill scveloRNA velocity analysis with scVelo. Estimate cell state transitions from unspliced/spliced mRNA dynamics, infer trajectory directions, compute latent time, and identify driver genes in single-cell RNA-seq data. Complements Scanpy/scVI-tools for trajectory inference.
| 1 | # scVelo — RNA Velocity Analysis |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | scVelo is the leading Python package for RNA velocity analysis in single-cell RNA-seq data. It infers cell state transitions by modeling the kinetics of mRNA splicing — using the ratio of unspliced (pre-mRNA) to spliced (mature mRNA) abundances to determine whether a gene is being upregulated or downregulated in each cell. This allows reconstruction of developmental trajectories and identification of cell fate decisions without requiring time-course data. |
| 6 | |
| 7 | **Installation:** `pip install scvelo` |
| 8 | |
| 9 | **Key resources:** |
| 10 | - Documentation: https://scvelo.readthedocs.io/ |
| 11 | - GitHub: https://github.com/theislab/scvelo |
| 12 | - Paper: Bergen et al. (2020) Nature Biotechnology. PMID: 32747759 |
| 13 | |
| 14 | ## When to Use This Skill |
| 15 | |
| 16 | Use scVelo when: |
| 17 | |
| 18 | - **Trajectory inference from snapshot data**: Determine which direction cells are differentiating |
| 19 | - **Cell fate prediction**: Identify progenitor cells and their downstream fates |
| 20 | - **Driver gene identification**: Find genes whose dynamics best explain observed trajectories |
| 21 | - **Developmental biology**: Model hematopoiesis, neurogenesis, epithelial-to-mesenchymal transitions |
| 22 | - **Latent time estimation**: Order cells along a pseudotime derived from splicing dynamics |
| 23 | - **Complement to Scanpy**: Add directional information to UMAP embeddings |
| 24 | |
| 25 | ## Prerequisites |
| 26 | |
| 27 | scVelo requires count matrices for both **unspliced** and **spliced** RNA. These are generated by: |
| 28 | 1. **STARsolo** or **kallisto|bustools** with `lamanno` mode |
| 29 | 2. **velocyto** CLI: `velocyto run10x` / `velocyto run` |
| 30 | 3. **alevin-fry** / **simpleaf** with spliced/unspliced output |
| 31 | |
| 32 | Data is stored in an `AnnData` object with `layers["spliced"]` and `layers["unspliced"]`. |
| 33 | |
| 34 | ## Standard RNA Velocity Workflow |
| 35 | |
| 36 | ### 1. Setup and Data Loading |
| 37 | |
| 38 | ```python |
| 39 | import scvelo as scv |
| 40 | import scanpy as sc |
| 41 | import numpy as np |
| 42 | import matplotlib.pyplot as plt |
| 43 | |
| 44 | # Configure settings |
| 45 | scv.settings.verbosity = 3 # Show computation steps |
| 46 | scv.settings.presenter_view = True |
| 47 | scv.settings.set_figure_params('scvelo') |
| 48 | |
| 49 | # Load data (AnnData with spliced/unspliced layers) |
| 50 | # Option A: Load from loom (velocyto output) |
| 51 | adata = scv.read("cellranger_output.loom", cache=True) |
| 52 | |
| 53 | # Option B: Merge velocyto loom with Scanpy-processed AnnData |
| 54 | adata_processed = sc.read_h5ad("processed.h5ad") # Has UMAP, clusters |
| 55 | adata_velocity = scv.read("velocyto.loom") |
| 56 | adata = scv.utils.merge(adata_processed, adata_velocity) |
| 57 | |
| 58 | # Verify layers |
| 59 | print(adata) |
| 60 | # obs × var: N × G |
| 61 | # layers: 'spliced', 'unspliced' (required) |
| 62 | # obsm['X_umap'] (required for visualization) |
| 63 | ``` |
| 64 | |
| 65 | ### 2. Preprocessing |
| 66 | |
| 67 | ```python |
| 68 | # Filter and normalize (follows Scanpy conventions) |
| 69 | scv.pp.filter_and_normalize( |
| 70 | adata, |
| 71 | min_shared_counts=20, # Minimum counts in spliced+unspliced |
| 72 | n_top_genes=2000 # Top highly variable genes |
| 73 | ) |
| 74 | |
| 75 | # Compute first and second order moments (means and variances) |
| 76 | # knn_connectivities must be computed first |
| 77 | sc.pp.neighbors(adata, n_neighbors=30, n_pcs=30) |
| 78 | scv.pp.moments( |
| 79 | adata, |
| 80 | n_pcs=30, |
| 81 | n_neighbors=30 |
| 82 | ) |
| 83 | ``` |
| 84 | |
| 85 | ### 3. Velocity Estimation — Stochastic Model |
| 86 | |
| 87 | The stochastic model is fast and suitable for exploratory analysis: |
| 88 | |
| 89 | ```python |
| 90 | # Stochastic velocity (faster, less accurate) |
| 91 | scv.tl.velocity(adata, mode='stochastic') |
| 92 | scv.tl.velocity_graph(adata) |
| 93 | |
| 94 | # Visualize |
| 95 | scv.pl.velocity_embedding_stream( |
| 96 | adata, |
| 97 | basis='umap', |
| 98 | color='leiden', |
| 99 | title="RNA Velocity (Stochastic)" |
| 100 | ) |
| 101 | ``` |
| 102 | |
| 103 | ### 4. Velocity Estimation — Dynamical Model (Recommended) |
| 104 | |
| 105 | The dynamical model fits the full splicing kinetics and is more accurate: |
| 106 | |
| 107 | ```python |
| 108 | # Recover dynamics (computationally intensive; ~10-30 min for 10K cells) |
| 109 | scv.tl.recover_dynamics(adata, n_jobs=4) |
| 110 | |
| 111 | # Compute velocity from dynamical model |
| 112 | scv.tl.velocity(adata, mode='dynamical') |
| 113 | scv.tl.velocity_graph(adata) |
| 114 | ``` |
| 115 | |
| 116 | ### 5. Latent Time |
| 117 | |
| 118 | The dynamical model enables computation of a shared latent time (pseudotime): |
| 119 | |
| 120 | ```python |
| 121 | # Compute latent time |
| 122 | scv.tl.latent_time(adata) |
| 123 | |
| 124 | # Visualize latent time on UMAP |
| 125 | scv.pl.scatter( |
| 126 | adata, |
| 127 | color='latent_time', |
| 128 | color_map='gnuplot', |
| 129 | size=80, |
| 130 | title='Latent time' |
| 131 | ) |
| 132 | |
| 133 | # Identify top genes ordered by latent time |
| 134 | top_genes = adata.var['fit_likelihood'].sort_values(ascending=False).index[:300] |
| 135 | scv.pl.heatmap( |
| 136 | adata, |
| 137 | var_names=top_genes, |
| 138 | sortby='latent_time', |
| 139 | col_color='leiden', |
| 140 | n_convolve=100 |
| 141 | ) |
| 142 | ``` |
| 143 | |
| 144 | ### 6. Driver Gene Analysis |
| 145 | |
| 146 | ```python |
| 147 | # Identify genes with highest velocity fit |
| 148 | scv.tl.rank_velocity_genes(adata, groupby='leiden', min_corr=0.3) |
| 149 | df = scv.DataFrame(adata.uns['rank_velocity_genes']['names']) |
| 150 | print(df.head(10)) |
| 151 | |
| 152 | # Speed and coherence |
| 153 | scv.tl.velocity_confidence(ad |