Files
scientific-agent-skills/skills/pydeseq2/scripts/run_deseq2_analysis.py
T
Timothy Kassis 4fb7e0bc29 Test every script-bearing skill, and enforce that it stays that way
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.
2026-07-28 10:20:12 -07:00

389 lines
13 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
PyDESeq2 Analysis Script
This script performs a complete differential expression analysis using PyDESeq2.
It can be used as a template for standard RNA-seq DEA workflows.
Usage:
python run_deseq2_analysis.py --counts counts.csv --metadata metadata.csv \
--design "~condition" --contrast condition treated control \
--output results/
Requirements:
- pydeseq2
- pandas
- matplotlib (optional, for plots)
"""
import argparse
import sys
from pathlib import Path
import pandas as pd
try:
from pydeseq2.dds import DeseqDataSet
from pydeseq2.default_inference import DefaultInference
from pydeseq2.ds import DeseqStats
except ImportError:
print("Error: pydeseq2 not installed. Install with: uv pip install pydeseq2==0.5.4")
sys.exit(1)
def load_and_validate_data(counts_path, metadata_path, transpose_counts=True):
"""Load count matrix and metadata, perform basic validation."""
print(f"Loading count data from {counts_path}...")
counts_df = pd.read_csv(counts_path, index_col=0)
if transpose_counts:
print("Transposing count matrix to samples × genes format...")
counts_df = counts_df.T
print(f"Loading metadata from {metadata_path}...")
metadata = pd.read_csv(metadata_path, index_col=0)
print(f"\nData loaded:")
print(f" Counts shape: {counts_df.shape} (samples × genes)")
print(f" Metadata shape: {metadata.shape} (samples × variables)")
# Validate. `Index.equals` also covers a length mismatch, which element-wise
# comparison cannot: `index_a == index_b` raises when the two differ in
# length, so the intersection fallback below would never be reached.
if not counts_df.index.equals(metadata.index):
print("\nWarning: Sample indices don't match perfectly. Taking intersection...")
common_samples = counts_df.index.intersection(metadata.index)
counts_df = counts_df.loc[common_samples]
metadata = metadata.loc[common_samples]
print(f" Using {len(common_samples)} common samples")
# Check for negative or non-integer values
if (counts_df < 0).any().any():
raise ValueError("Count matrix contains negative values")
return counts_df, metadata
def filter_data(counts_df, metadata, min_counts=10, condition_col=None):
"""Filter low-count genes and samples with missing data."""
print(f"\nFiltering data...")
initial_genes = counts_df.shape[1]
initial_samples = counts_df.shape[0]
# Filter genes
genes_to_keep = counts_df.columns[counts_df.sum(axis=0) >= min_counts]
counts_df = counts_df[genes_to_keep]
genes_removed = initial_genes - counts_df.shape[1]
print(f" Removed {genes_removed} genes with < {min_counts} total counts")
# Filter samples with missing condition data
if condition_col and condition_col in metadata.columns:
samples_to_keep = ~metadata[condition_col].isna()
counts_df = counts_df.loc[samples_to_keep]
metadata = metadata.loc[samples_to_keep]
samples_removed = initial_samples - counts_df.shape[0]
if samples_removed > 0:
print(f" Removed {samples_removed} samples with missing '{condition_col}' data")
print(f" Final data shape: {counts_df.shape[0]} samples × {counts_df.shape[1]} genes")
return counts_df, metadata
def run_deseq2(counts_df, metadata, design, n_cpus=1):
"""Run DESeq2 normalization and fitting."""
print(f"\nInitializing DeseqDataSet with design: {design}")
inference = DefaultInference(n_cpus=n_cpus)
dds = DeseqDataSet(
counts=counts_df,
metadata=metadata,
design=design,
refit_cooks=True,
inference=inference,
quiet=False
)
print("\nRunning DESeq2 pipeline...")
print(" Step 1/7: Computing size factors...")
print(" Step 2/7: Fitting genewise dispersions...")
print(" Step 3/7: Fitting dispersion trend curve...")
print(" Step 4/7: Computing dispersion priors...")
print(" Step 5/7: Fitting MAP dispersions...")
print(" Step 6/7: Fitting log fold changes...")
print(" Step 7/7: Calculating Cook's distances...")
dds.deseq2()
print("\n✓ DESeq2 fitting complete")
return dds, inference
def infer_shrink_coeff(dds, contrast):
"""Infer the formulaic coefficient name for a categorical contrast."""
coeff = f"{contrast[0]}[T.{contrast[1]}]"
design_columns = list(dds.obsm["design_matrix"].columns)
if coeff in design_columns:
return coeff
raise ValueError(
"Could not infer LFC shrinkage coefficient "
f"{coeff!r}. Available design columns: {design_columns}. "
"Pass --shrink-coeff explicitly or use --no-shrink."
)
def run_statistical_tests(dds, contrast, alpha=0.05, shrink_lfc=True, inference=None, shrink_coeff=None):
"""Perform Wald tests and compute p-values."""
print(f"\nPerforming statistical tests...")
print(f" Contrast: {contrast}")
print(f" Significance threshold: {alpha}")
ds = DeseqStats(
dds,
contrast=contrast,
alpha=alpha,
cooks_filter=True,
independent_filter=True,
inference=inference,
quiet=False
)
print("\n Running Wald tests...")
print(" Filtering outliers based on Cook's distance...")
print(" Applying independent filtering...")
print(" Adjusting p-values (Benjamini-Hochberg)...")
ds.summary()
print("\n✓ Statistical testing complete")
# Optional LFC shrinkage
if shrink_lfc:
print("\nApplying LFC shrinkage for visualization...")
coeff = shrink_coeff or infer_shrink_coeff(dds, contrast)
ds.lfc_shrink(coeff=coeff)
print("✓ LFC shrinkage complete")
return ds
def save_results(ds, dds, output_dir, shrink_lfc=True):
"""Save results and intermediate objects."""
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
print(f"\nSaving results to {output_dir}/")
# Save statistical results
results_path = output_dir / "deseq2_results.csv"
ds.results_df.to_csv(results_path)
print(f" Saved: {results_path}")
# Save significant genes
significant = ds.results_df[ds.results_df.padj < 0.05]
sig_path = output_dir / "significant_genes.csv"
significant.to_csv(sig_path)
print(f" Saved: {sig_path} ({len(significant)} significant genes)")
# Save sorted results
sorted_results = ds.results_df.sort_values("padj")
sorted_path = output_dir / "results_sorted_by_padj.csv"
sorted_results.to_csv(sorted_path)
print(f" Saved: {sorted_path}")
# Save as AnnData/H5AD to avoid unsafe pickle interchange.
dds_path = output_dir / "deseq_dataset.h5ad"
dds.to_picklable_anndata().write_h5ad(dds_path)
print(f" Saved: {dds_path}")
# Print summary
print(f"\n{'='*60}")
print("ANALYSIS SUMMARY")
print(f"{'='*60}")
print(f"Total genes tested: {len(ds.results_df)}")
print(f"Significant genes (padj < 0.05): {len(significant)}")
print(f"Upregulated: {len(significant[significant.log2FoldChange > 0])}")
print(f"Downregulated: {len(significant[significant.log2FoldChange < 0])}")
print(f"{'='*60}")
# Show top genes
print("\nTop 10 most significant genes:")
print(sorted_results.head(10)[["baseMean", "log2FoldChange", "pvalue", "padj"]])
return results_path
def create_plots(ds, output_dir):
"""Create basic visualization plots."""
try:
import matplotlib.pyplot as plt
import numpy as np
except ImportError:
print("\nNote: matplotlib not installed. Skipping plot generation.")
return
output_dir = Path(output_dir)
results = ds.results_df.copy()
print("\nGenerating plots...")
# Volcano plot
results["-log10(padj)"] = -np.log10(results.padj.fillna(1))
plt.figure(figsize=(10, 6))
significant = results.padj < 0.05
plt.scatter(
results.loc[~significant, "log2FoldChange"],
results.loc[~significant, "-log10(padj)"],
alpha=0.3, s=10, c='gray', label='Not significant'
)
plt.scatter(
results.loc[significant, "log2FoldChange"],
results.loc[significant, "-log10(padj)"],
alpha=0.6, s=10, c='red', label='Significant (padj < 0.05)'
)
plt.axhline(-np.log10(0.05), color='blue', linestyle='--', linewidth=1, alpha=0.5)
plt.axvline(1, color='gray', linestyle='--', linewidth=1, alpha=0.5)
plt.axvline(-1, color='gray', linestyle='--', linewidth=1, alpha=0.5)
plt.xlabel("Log2 Fold Change", fontsize=12)
plt.ylabel("-Log10(Adjusted P-value)", fontsize=12)
plt.title("Volcano Plot", fontsize=14, fontweight='bold')
plt.legend()
plt.tight_layout()
volcano_path = output_dir / "volcano_plot.png"
plt.savefig(volcano_path, dpi=300)
plt.close()
print(f" Saved: {volcano_path}")
# MA plot
plt.figure(figsize=(10, 6))
plt.scatter(
np.log10(results.loc[~significant, "baseMean"] + 1),
results.loc[~significant, "log2FoldChange"],
alpha=0.3, s=10, c='gray', label='Not significant'
)
plt.scatter(
np.log10(results.loc[significant, "baseMean"] + 1),
results.loc[significant, "log2FoldChange"],
alpha=0.6, s=10, c='red', label='Significant (padj < 0.05)'
)
plt.axhline(0, color='blue', linestyle='--', linewidth=1, alpha=0.5)
plt.xlabel("Log10(Base Mean + 1)", fontsize=12)
plt.ylabel("Log2 Fold Change", fontsize=12)
plt.title("MA Plot", fontsize=14, fontweight='bold')
plt.legend()
plt.tight_layout()
ma_path = output_dir / "ma_plot.png"
plt.savefig(ma_path, dpi=300)
plt.close()
print(f" Saved: {ma_path}")
def main():
parser = argparse.ArgumentParser(
description="Run PyDESeq2 differential expression analysis",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Basic analysis
python run_deseq2_analysis.py \\
--counts counts.csv \\
--metadata metadata.csv \\
--design "~condition" \\
--contrast condition treated control \\
--output results/
# Multi-factor analysis
python run_deseq2_analysis.py \\
--counts counts.csv \\
--metadata metadata.csv \\
--design "~batch + condition" \\
--contrast condition treated control \\
--output results/ \\
--n-cpus 4 \\
--shrink-coeff "condition[T.treated]"
"""
)
parser.add_argument("--counts", required=True, help="Path to count matrix CSV file")
parser.add_argument("--metadata", required=True, help="Path to metadata CSV file")
parser.add_argument("--design", required=True, help="Design formula (e.g., '~condition')")
parser.add_argument(
"--contrast",
nargs=3,
required=True,
metavar=("VARIABLE", "TEST", "REFERENCE"),
help="Contrast specification: variable test_level reference_level",
)
parser.add_argument("--output", default="results", help="Output directory (default: results)")
parser.add_argument(
"--min-counts",
type=int,
default=10,
help="Minimum total counts for gene filtering (default: 10)",
)
parser.add_argument("--alpha", type=float, default=0.05, help="Significance threshold (default: 0.05)")
parser.add_argument(
"--no-transpose",
action="store_true",
help="Don't transpose count matrix (use if already samples × genes)",
)
parser.add_argument("--no-shrink", action="store_true", help="Skip LFC shrinkage")
parser.add_argument(
"--shrink-coeff",
help="Design-matrix coefficient to shrink (e.g., 'condition[T.treated]')",
)
parser.add_argument(
"--n-cpus",
type=int,
default=1,
help="Number of CPUs for parallel processing (default: 1)",
)
parser.add_argument("--plots", action="store_true", help="Generate volcano and MA plots")
args = parser.parse_args()
# Load data
counts_df, metadata = load_and_validate_data(
args.counts,
args.metadata,
transpose_counts=not args.no_transpose,
)
# Filter data
condition_col = args.contrast[0]
counts_df, metadata = filter_data(
counts_df,
metadata,
min_counts=args.min_counts,
condition_col=condition_col,
)
# Run DESeq2
dds, inference = run_deseq2(counts_df, metadata, args.design, n_cpus=args.n_cpus)
# Statistical testing
ds = run_statistical_tests(
dds,
contrast=args.contrast,
alpha=args.alpha,
shrink_lfc=not args.no_shrink,
inference=inference,
shrink_coeff=args.shrink_coeff,
)
# Save results
save_results(ds, dds, args.output, shrink_lfc=not args.no_shrink)
# Create plots if requested
if args.plots:
create_plots(ds, args.output)
print(f"\n✓ Analysis complete! Results saved to {args.output}/")
if __name__ == "__main__":
main()