mirror of
https://github.com/K-Dense-AI/scientific-agent-skills.git
synced 2026-12-06 03:13:56 +08:00
AGENTS.md has always said that a skill shipping scripts/ puts its tests in
tests/<name>/, but nothing checked it: 54 of the 100 such skills had no suite
at all, including docx, pptx, xlsx, pdf and scanpy. All 100 now do.
tests/_meta is the guard. It runs the shared structural contract across every
skill in a single process -- safe because it parses scripts with ast and never
imports them -- and fails when a skill ships scripts/ without a suite or
without a [skills.<name>] entry in skill-requirements.toml. It needs no
scientific packages and finishes in seconds, so skill-tests.yml blocks every
pull request on it, plus the packages=[] suites.
tests/_contract holds what the per-skill suites were each reimplementing:
frontmatter conformance, the 500-line limit, no tests or bytecode under
skills/, local links resolving, scripts parsing, no eval/exec/os.system, no
standard-library shadowing, no hardcoded local paths, valid shell scripts. Also
the --help contract, which skips when a skill's packages are absent and runs
for real under --isolated, and shared behaviour for the files docx/pptx/xlsx
and five schematic-shipping skills carry byte-identical copies of, with drift
detection so they cannot diverge silently.
67 new suites, 3325 test functions. The existing suites were retrofitted: 18
no longer pin an exact skill version, so a version bump no longer breaks a
test; 11 duplicated structural methods removed; 19 wired to the --help
contract; and 5 that failed collection without their packages now skip
cleanly. run_all.py in the bare project environment goes from 6 failures to 0.
Writing the tests surfaced 19 defects in the skills, fixed here with version
bumps. The ones that changed scientific output:
- openpiv reported vorticity 0 for a rotating flow, from a sign error in
openpiv's y-up coordinate relabelling; solid-body rotation now gives 2w
exactly, on grids of either orientation
- deepchem returned solubility predictions in z-scored space while labelling
them log(mol/L), because it transformed a y-less dataset instead of
untransforming the output
- neuropixels-analysis had the Allen and IBL ISI thresholds swapped,
contradicting its own references/QUALITY_METRICS.md and inverting the two
standards' relative strictness
- scanpy's summarize() raised TypeError on every AnnData under anndata 0.13,
which reports an unnamed None key on .layers; scanpy convert was broken
- experimental-design's Latin hypercube was never reproducible: pyDOE3 draws
from its own default_rng and ignores numpy's global seed
- primekg shipped a hardcoded path naming a person, which is why
no_personal_paths is now a contract rule
The remainder is upstream API drift, each verified against the installed
package: retired symbols in bioservices 1.16, gget helpers that returned lists
where a string was written, ArviZ 1.x kwargs in pymc, a positional-only
factory in pymoo, ReduceLROnPlateau(verbose=) in torch 2.13, a removed scvelo
parameter, and PyPDF2 in scientific-slides.
Two manifest environments could not build and are pinned: gget, where an
unpinned scanpy walked back to 1.9.8 and pulled llvmlite 0.36 which does not
compile on 3.13, and pymatgen, pinned to the snapshot its own _common.py
enforces rather than loosening that check. deepchem gains torch, without which
no model class exists.
python tests/run_all.py --isolated: 101 passed, 0 failed.
241 lines
8.2 KiB
Python
241 lines
8.2 KiB
Python
"""
|
||
RNA Velocity Analysis Workflow using scVelo
|
||
===========================================
|
||
Complete pipeline from raw data to velocity visualization.
|
||
|
||
Usage:
|
||
python rna_velocity_workflow.py
|
||
|
||
Or import and use run_velocity_analysis() with your AnnData object.
|
||
"""
|
||
|
||
import scvelo as scv
|
||
import scanpy as sc
|
||
import numpy as np
|
||
import matplotlib
|
||
matplotlib.use('Agg') # Non-interactive backend
|
||
import matplotlib.pyplot as plt
|
||
import os
|
||
|
||
|
||
def run_velocity_analysis(
|
||
adata,
|
||
groupby="leiden",
|
||
n_top_genes=2000,
|
||
n_neighbors=30,
|
||
mode="dynamical",
|
||
n_jobs=4,
|
||
output_dir="velocity_results",
|
||
):
|
||
"""
|
||
Complete RNA velocity analysis workflow.
|
||
|
||
Parameters
|
||
----------
|
||
adata : AnnData
|
||
AnnData object with 'spliced' and 'unspliced' layers.
|
||
Should already have UMAP and cluster annotations.
|
||
groupby : str
|
||
Column in adata.obs for cell type labels.
|
||
n_top_genes : int
|
||
Number of top highly variable genes.
|
||
n_neighbors : int
|
||
Number of neighbors for moment computation.
|
||
mode : str
|
||
Velocity model: 'stochastic' (fast) or 'dynamical' (accurate).
|
||
n_jobs : int
|
||
Parallel jobs for dynamical model fitting.
|
||
output_dir : str
|
||
Directory for saving output figures.
|
||
|
||
Returns
|
||
-------
|
||
AnnData with velocity annotations.
|
||
"""
|
||
os.makedirs(output_dir, exist_ok=True)
|
||
|
||
# ── Settings ──────────────────────────────────────────────────────────────
|
||
scv.settings.verbosity = 2
|
||
scv.settings.figdir = output_dir
|
||
|
||
# ── Step 1: Check layers ───────────────────────────────────────────────────
|
||
assert "spliced" in adata.layers, "Missing 'spliced' layer. Run velocyto first."
|
||
assert "unspliced" in adata.layers, "Missing 'unspliced' layer. Run velocyto first."
|
||
print(f"Input: {adata.n_obs} cells × {adata.n_vars} genes")
|
||
|
||
# ── Step 2: Preprocessing ─────────────────────────────────────────────────
|
||
print("Step 1/5: Preprocessing...")
|
||
# scVelo 0.3 narrowed filter_and_normalize() to gene filtering plus
|
||
# per-cell normalization: it no longer accepts n_top_genes and no longer
|
||
# log-transforms, so the log step and HVG selection come from Scanpy.
|
||
scv.pp.filter_and_normalize(adata, min_shared_counts=20)
|
||
if "log1p" not in adata.uns:
|
||
sc.pp.log1p(adata)
|
||
sc.pp.highly_variable_genes(
|
||
adata, n_top_genes=min(n_top_genes, adata.n_vars), subset=True
|
||
)
|
||
|
||
if "neighbors" not in adata.uns:
|
||
sc.pp.neighbors(adata, n_neighbors=n_neighbors, n_pcs=30)
|
||
|
||
scv.pp.moments(adata, n_pcs=30, n_neighbors=n_neighbors)
|
||
print(f" {adata.n_vars} velocity genes selected")
|
||
|
||
# ── Step 3: Velocity estimation ────────────────────────────────────────────
|
||
print(f"Step 2/5: Fitting velocity model ({mode})...")
|
||
if mode == "dynamical":
|
||
scv.tl.recover_dynamics(adata, n_jobs=n_jobs)
|
||
scv.tl.velocity(adata, mode=mode)
|
||
scv.tl.velocity_graph(adata)
|
||
print(" Velocity graph computed")
|
||
|
||
# ── Step 4: Downstream analyses ────────────────────────────────────────────
|
||
print("Step 3/5: Computing latent time and confidence...")
|
||
scv.tl.velocity_confidence(adata)
|
||
scv.tl.velocity_pseudotime(adata)
|
||
|
||
if mode == "dynamical":
|
||
scv.tl.latent_time(adata)
|
||
|
||
if groupby in adata.obs.columns:
|
||
scv.tl.rank_velocity_genes(adata, groupby=groupby, min_corr=0.3)
|
||
|
||
# ── Step 5: Visualization ─────────────────────────────────────────────────
|
||
print("Step 4/5: Generating figures...")
|
||
|
||
# Stream plot
|
||
scv.pl.velocity_embedding_stream(
|
||
adata,
|
||
basis="umap",
|
||
color=groupby,
|
||
title="RNA Velocity",
|
||
save=f"{output_dir}/velocity_stream.png",
|
||
)
|
||
|
||
# Arrow plot
|
||
scv.pl.velocity_embedding(
|
||
adata,
|
||
arrow_length=3,
|
||
arrow_size=2,
|
||
color=groupby,
|
||
basis="umap",
|
||
save=f"{output_dir}/velocity_arrows.png",
|
||
)
|
||
|
||
# Pseudotime
|
||
scv.pl.scatter(
|
||
adata,
|
||
color="velocity_pseudotime",
|
||
cmap="gnuplot",
|
||
title="Velocity Pseudotime",
|
||
save=f"{output_dir}/pseudotime.png",
|
||
)
|
||
|
||
if mode == "dynamical" and "latent_time" in adata.obs:
|
||
scv.pl.scatter(
|
||
adata,
|
||
color="latent_time",
|
||
color_map="gnuplot",
|
||
title="Latent Time",
|
||
save=f"{output_dir}/latent_time.png",
|
||
)
|
||
|
||
# Speed and coherence
|
||
scv.pl.scatter(
|
||
adata,
|
||
c=["velocity_length", "velocity_confidence"],
|
||
cmap="coolwarm",
|
||
perc=[5, 95],
|
||
save=f"{output_dir}/velocity_quality.png",
|
||
)
|
||
|
||
# Top driver genes heatmap (dynamical only)
|
||
if mode == "dynamical" and "fit_likelihood" in adata.var:
|
||
top_genes = adata.var["fit_likelihood"].sort_values(ascending=False).index[:50]
|
||
scv.pl.heatmap(
|
||
adata,
|
||
var_names=top_genes,
|
||
sortby="latent_time",
|
||
col_color=groupby,
|
||
n_convolve=50,
|
||
save=f"{output_dir}/driver_gene_heatmap.png",
|
||
)
|
||
|
||
# ── Step 6: Save results ───────────────────────────────────────────────────
|
||
print("Step 5/5: Saving results...")
|
||
output_h5ad = os.path.join(output_dir, "adata_velocity.h5ad")
|
||
adata.write_h5ad(output_h5ad)
|
||
print(f" Saved to {output_h5ad}")
|
||
|
||
# Summary statistics
|
||
confidence = adata.obs["velocity_confidence"].dropna()
|
||
print("\nSummary:")
|
||
print(f" Velocity model: {mode}")
|
||
print(f" Cells: {adata.n_obs}")
|
||
print(f" Velocity genes: {adata.n_vars}")
|
||
print(f" Mean velocity confidence: {confidence.mean():.3f}")
|
||
print(f" High-confidence cells (>0.7): {(confidence > 0.7).sum()} ({(confidence > 0.7).mean():.1%})")
|
||
|
||
if mode == "dynamical" and "fit_likelihood" in adata.var:
|
||
good_genes = (adata.var["fit_likelihood"] > 0.1).sum()
|
||
print(f" Well-fit genes (likelihood>0.1): {good_genes}")
|
||
|
||
print(f"\nOutput files saved to: {output_dir}/")
|
||
return adata
|
||
|
||
|
||
def load_from_loom(loom_path, processed_h5ad=None):
|
||
"""
|
||
Load velocity data from velocyto loom file.
|
||
|
||
Args:
|
||
loom_path: Path to velocyto output loom file
|
||
processed_h5ad: Optional path to pre-processed Scanpy h5ad file
|
||
"""
|
||
adata_loom = scv.read(loom_path, cache=True)
|
||
|
||
if processed_h5ad:
|
||
adata_processed = sc.read_h5ad(processed_h5ad)
|
||
# Merge: keep processed metadata and add velocity layers
|
||
adata = scv.utils.merge(adata_processed, adata_loom)
|
||
else:
|
||
adata = adata_loom
|
||
# Run basic Scanpy pipeline
|
||
sc.pp.normalize_total(adata, target_sum=1e4)
|
||
sc.pp.log1p(adata)
|
||
sc.pp.highly_variable_genes(adata, n_top_genes=3000)
|
||
sc.pp.pca(adata)
|
||
sc.pp.neighbors(adata)
|
||
sc.tl.umap(adata)
|
||
sc.tl.leiden(adata, resolution=0.5)
|
||
|
||
return adata
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# Example usage with simulated data (for testing)
|
||
print("scVelo RNA Velocity Workflow - Demo Mode")
|
||
print("=" * 50)
|
||
|
||
# Load example dataset
|
||
adata = scv.datasets.pancreas()
|
||
print(f"Loaded pancreas dataset: {adata}")
|
||
|
||
# Run analysis
|
||
adata = run_velocity_analysis(
|
||
adata,
|
||
groupby="clusters",
|
||
n_top_genes=2000,
|
||
mode="dynamical",
|
||
n_jobs=2,
|
||
output_dir="pancreas_velocity",
|
||
)
|
||
|
||
print("\nAnalysis complete!")
|
||
print(f"Key results:")
|
||
print(f" adata.layers['velocity']: velocity per gene per cell")
|
||
print(f" adata.obs['latent_time']: pseudotime from dynamics")
|
||
print(f" adata.obs['velocity_confidence']: per-cell confidence")
|
||
if "rank_velocity_genes" in adata.uns:
|
||
print(f" adata.uns['rank_velocity_genes']: driver genes per cluster")
|