#!/usr/bin/env python3
"""Quick test to verify dependencies and script structure."""

import sys
from pathlib import Path

print("Testing regenerate_plot_svg.py dependencies...")
print("=" * 60)

# Test imports
deps = {
    'numpy': 'np',
    'pandas': 'pd',
    'matplotlib': 'plt',
    'logomaker': 'logomaker',
    'pyfaidx': 'pyfaidx',
}

missing = []
for module, alias in deps.items():
    try:
        __import__(module)
        print(f"✓ {module}")
    except ImportError:
        print(f"✗ {module} - MISSING")
        missing.append(module)

# Test varbook imports
print("\nTesting varbook imports...")
try:
    sys.path.insert(0, str(Path(__file__).parent / 'varbook'))
    from varbook.plot.variant.profiles import (
        _extract_variant_sequences,
        _plot_profile,
        _resolve_model_metadata,
        _process_single_model_with_folds
    )
    print("✓ varbook.plot.variant.profiles")
except Exception as e:
    print(f"✗ varbook imports failed: {e}")
    missing.append('varbook')

# Test FIMO availability
print("\nTesting FIMO tool...")
import subprocess
try:
    result = subprocess.run(['which', 'fimo'], capture_output=True, text=True, timeout=5)
    if result.returncode == 0:
        print(f"✓ FIMO found at: {result.stdout.strip()}")
    else:
        print("✗ FIMO not found in PATH")
        missing.append('fimo')
except Exception as e:
    print(f"✗ FIMO check failed: {e}")
    missing.append('fimo')

# Check for JASPAR file
print("\nChecking for JASPAR database...")
jaspar_paths = [
    Path(__file__).parent / 'data' / 'jaspar' / 'JASPAR2024_CORE_meme.txt',
    Path('/oak/stanford/groups/akundaje/airanman/refs/JASPAR/JASPAR2024_CORE_non-redundant_pfms_meme.txt'),
]
found_jaspar = False
for path in jaspar_paths:
    if path.exists():
        print(f"✓ JASPAR found at: {path}")
        found_jaspar = True
        break
if not found_jaspar:
    print("✗ JASPAR database not found")
    print("  Download from: https://jaspar.elixir.no/download/data/2024/CORE/JASPAR2024_CORE_non-redundant_pfms_meme.txt")
    missing.append('jaspar')

# Check model paths
print("\nChecking for model paths file...")
model_paths = Path('/oak/stanford/groups/akundaje/airanman/projects/lab/rare-disease-manuscript/curation/metadata_renaming/broad.model_paths.tsv')
if model_paths.exists():
    print(f"✓ Model paths found at: {model_paths}")
else:
    print(f"✗ Model paths not found at: {model_paths}")
    missing.append('model_paths')

# Check genome
print("\nChecking for genome file...")
genome_path = Path('/oak/stanford/groups/akundaje/airanman/refs/hg38/GRCh38_no_alt_analysis_set_GCA_000001405.15.fasta')
if genome_path.exists():
    print(f"✓ Genome found at: {genome_path}")
else:
    print(f"✗ Genome not found at: {genome_path}")
    missing.append('genome')

print("\n" + "=" * 60)
if missing:
    print(f"Missing dependencies: {', '.join(missing)}")
    print("\nNext steps:")
    if 'jaspar' in missing:
        print("  1. Download JASPAR: curl -L https://jaspar.elixir.no/download/data/2024/CORE/JASPAR2024_CORE_non-redundant_pfms_meme.txt -o data/jaspar/JASPAR2024_CORE_meme.txt")
    print("  2. Test script with a small variant")
else:
    print("✓ All dependencies available! Ready to test.")
print("=" * 60)
