Skip to content

Turn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 250,000+ scientists worldwide. 177 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.

Update Geniml skill for current APIs

Timothy Kassisdd952bd1244c
diff --git a/skills/geniml/SKILL.md b/skills/geniml/SKILL.md
index 7fb6aa03..1ef259ea 100644
--- a/skills/geniml/SKILL.md
+++ b/skills/geniml/SKILL.md
@@ -1,315 +1,315 @@
---
name: geniml
-description: This skill should be used when working with genomic interval data (BED files) for machine learning tasks. Use for training region embeddings (Region2Vec, BEDspace), single-cell ATAC-seq analysis (scEmbed), building consensus peaks (universes), or any ML-based analysis of genomic regions. Applies to BED file collections, scATAC-seq data, chromatin accessibility datasets, and region-based genomic feature learning.
-license: BSD-2-Clause license
-metadata: {"version": "1.0", "skill-author": "K-Dense Inc."}
+description: "Use Geniml for audited local genomic-interval workflows: validate BED and universe contracts, plan Region2Vec or scEmbed runs, inspect model/tokenizer compatibility, and assess consensus universes."
+license: MIT
+compatibility: Requires Python 3.10+ and uv. Guidance targets geniml 0.8.4 with gtars 0.9.2; ML workflows need the pinned ml extra and compatible native wheels. Bundled planners and inspectors are dependency-free, local-only, and make no network requests.
+allowed-tools:
+ - Read
+ - Write
+ - Edit
+ - Bash
+ - Glob
+metadata:
+ version: "1.1"
+ skill-author: "K-Dense Inc."
+ upstream-version: "0.8.4"
+ last-reviewed: "2026-07-23"
---
-# Geniml: Genomic Interval Machine Learning
+# Geniml
-## Overview
+Use Geniml for machine learning and statistical workflows over genomic interval
+sets. Treat coordinates, assemblies, token vocabularies, model artifacts, and
+sample grouping as explicit contracts. The bundled scripts validate or plan;
+they do not import Geniml, contact services, deserialize models, or execute
+training.
-Geniml is a Python package for building machine learning models on genomic interval data from BED files. It provides unsupervised methods for learning embeddings of genomic regions, single cells, and metadata labels, enabling similarity searches, clustering, and downstream ML tasks.
+`Bash` is declared only for explicit, user-approved `uv`, Python, Geniml,
+Gtars, Git, and native CLI commands shown in this guide; bundled Python helpers
+do not spawn subprocesses. Example paths under `data/`, `refs/`, `work/`, and
+`models/` are user-provided project placeholders, not missing bundled files.
-## Installation
+## Verified release snapshot
-Install geniml using uv:
+- Latest stable PyPI release on 2026-07-23: `geniml==0.8.4` (2026-01-14).
+- PyPI does not declare `Requires-Python`; its classifiers list Python
+ 3.10-3.14. Prefer Python 3.11 or 3.12 where all native/ML wheels resolve.
+- `geniml==0.8.4` accepts `gtars>=0.2.5`; the verified base smoke used current
+ `gtars==0.9.2` (2026-06-17, Python >=3.10).
+- Extras are `ml` and `test`. The base install omits Torch, Gensim, Scanpy,
+ Hugging Face Hub, pyBigWig, and HMM dependencies.
+- Upstream documentation contains stale examples. Release source and installed
+ `--help` output take precedence where they conflict.
-```bash
-uv pip install geniml
-```
+## Install reproducibly
-For ML dependencies (PyTorch, etc.):
+Use a project environment and commit its generated lockfile:
```bash
-uv pip install 'geniml[ml]'
+uv venv --python 3.12
+uv pip install "geniml==0.8.4" "gtars==0.9.2"
```
-Development version from GitHub:
+For Region2Vec, scEmbed, evaluation, or universe methods needing ML libraries:
```bash
-uv pip install git+https://github.com/databio/geniml.git
+uv pip install "geniml[ml]==0.8.4" "gtars==0.9.2"
```
-## Core Capabilities
-
-Geniml provides five primary capabilities, each detailed in dedicated reference files:
-
-### 1. Region2Vec: Genomic Region Embeddings
-
-Train unsupervised embeddings of genomic regions using word2vec-style learning.
-
-**Use for:** Dimensionality reduction of BED files, region similarity analysis, feature vectors for downstream ML.
-
-**Workflow:**
-1. Tokenize BED files using a universe reference
-2. Train Region2Vec model on tokens
-3. Generate embeddings for regions
-
-**Reference:** See `references/region2vec.md` for detailed workflow, parameters, and examples.
-
-### 2. BEDspace: Joint Region and Metadata Embeddings
-
-Train shared embeddings for region sets and metadata labels using StarSpace.
-
-**Use for:** Metadata-aware searches, cross-modal queries (region→label or label→region), joint analysis of genomic content and experimental conditions.
-
-**Workflow:**
-1. Preprocess regions and metadata
-2. Train BEDspace model
-3. Compute distances
-4. Query across regions and labels
-
-**Reference:** See `references/bedspace.md` for detailed workflow, search types, and examples.
-
-### 3. scEmbed: Single-Cell Chromatin Accessibility Embeddings
-
-Train Region2Vec models on single-cell ATAC-seq data for cell-level embeddings.
-
-**Use for:** scATAC-seq clustering, cell-type annotation, dimensionality reduction of single cells, integration with scanpy workflows.
-
-**Workflow:**
-1. Prepare AnnData with peak coordinates
-2. Pre-tokenize cells
-3. Train scEmbed model
-4. Generate cell embeddings
-5. Cluster and visualize with scanpy
-
-**Reference:** See `references/scembed.md` for detailed workflow, parameters, and examples.
-
-### 4. Consensus Peaks: Universe Building
-
-Build reference peak sets (universes) from BED file collections using multiple statistical methods.
-
-**Use for:** Creating tokenization references, standardizing regions across datasets, defining consensus features with statistical rigor.
-
-**Workflow:**
-1. Combine BED files
-2. Generate coverage tracks
-3. Build universe using CC, CCF, ML, or HMM method
-
-**Methods:**
-- **CC (Coverage Cutoff)**: Simple threshold-based
-- **CCF (Coverage Cutoff Flexible)**: Confidence intervals for boundaries
-- **ML (Maximum Likelihood)**: Probabilistic modeling of positions
-- **HMM (Hidden Markov Model)**: Complex state modeling
+For a durable project, prefer:
-**Reference:** See `references/consensus_peaks.md` for method comparison, parameters, and examples.
-
-### 5. Utilities: Supporting Tools
-
-Additional tools for caching, randomization, evaluation, and search.
+```bash
+uv add "geniml[ml]==0.8.4" "gtars==0.9.2"
+uv lock
+```
-**Available utilities:**
-- **BBClient**: BED file caching for repeated access
-- **BEDshift**: Randomization preserving genomic context
-- **Evaluation**: Metrics for embedding quality (silhouette, Davies-Bouldin, etc.)
-- **Tokenization**: Region tokenization utilities (hard, soft, universe-based)
-- **Text2BedNN**: Neural search backends for genomic queries
+Do not install an unpinned Git branch. Record Python, OS/architecture, the
+resolved lockfile, and the PyPI artifact digest. Geniml itself is BSD-2-Clause;
+the `MIT` frontmatter value licenses this skill's content.
+
+## Start with the safety gate
+
+Before importing Geniml or running an external binary:
+
+1. Work only with explicit local regular files. Reject URLs, FIFOs, devices,
+ and symlinks unless the user deliberately changes that policy.
+2. Validate BED structure and the declared assembly against a trusted local
+ chromosome-sizes file.
+3. Bound file count, bytes, rows, workers, epochs, and output size.
+4. Separate train/validation/test by patient, donor, biological replicate, or
+ other independent unit—not by BED row or cell alone.
+5. Inventory and checksum the universe, tokenizer, model, config, inputs,
+ metadata manifest, and native binaries.
+6. Obtain explicit approval before any BEDbase or Hugging Face download. Never
+ infer approval from a model ID or BEDbase identifier.
+7. Keep logs aggregate and bounded. BED filenames, sample IDs, phenotypes,
+ labels, barcodes, and genomic intervals may be sensitive.
+
+## Coordinate and assembly contract
+
+BED intervals are normally **0-based, half-open** `[start, end)`: start is
+included, end is excluded, and length is `end - start`. Do not mix them with
+1-based closed coordinates from VCF/GFF or user-facing genome browsers.
+
+For every corpus and artifact, record:
+
+- assembly and patch/accession where possible (for example GRCh38 versus
+ GRCh38.p14), plus the chromosome-sizes checksum;
+- contig naming convention (`chr1` versus `1`), alt/random/decoy policy, and
+ mitochondrial naming;
+- coordinate convention, sorting order, duplicate/overlap policy, and whether
+ BED strand is meaningful;
+- liftover tool, chain digest, source/target assemblies, unmapped fraction, and
+ post-liftover validation.
+
+Reject negative coordinates, `end <= start`, integer overflow, unknown
+contigs, ends beyond contig length, malformed columns, mixed assemblies, and
+silent contig renaming. Sorting and normalization never repair an assembly
+mismatch. BED3 has no strand; when column 6 is present, preserve `+`, `-`, or
+`.` unless the assay contract says otherwise.
+
+Run a bounded validation and normalization **plan** before analysis:
-**Reference:** See `references/utilities.md` for detailed usage of each utility.
+```bash
+python skills/geniml/scripts/bed_validator.py \
+ --input data/peaks.bed \
+ --assembly GRCh38 \
+ --chrom-sizes refs/GRCh38.chrom.sizes
+```
-## Common Workflows
+The validator reports proposed actions but never rewrites the BED file.
-### Basic Region Embedding Pipeline
+## Current API map
-```python
-from geniml.tokenization import hard_tokenization
-from geniml.region2vec import region2vec
-from geniml.evaluation import evaluate_embeddings
-
-# Step 1: Tokenize BED files
-hard_tokenization(
- src_folder='bed_files/',
- dst_folder='tokens/',
- universe_file='universe.bed',
- p_value_threshold=1e-9
-)
-
-# Step 2: Train Region2Vec
-region2vec(
- token_folder='tokens/',
- save_dir='model/',
- num_shufflings=1000,
- embedding_dim=100
-)
-
-# Step 3: Evaluate
-metrics = evaluate_embeddings(
- embeddings_file='model/embeddings.npy',
- labels_file='metadata.csv'
-)
-```
+### Region and tokenizer I/O
-### scATAC-seq Analysis Pipeline
+Prefer Gtars for new interval/tokenizer code:
```python
-import scanpy as sc
-from geniml.scembed import ScEmbed
-from geniml.io import tokenize_cells
-
-# Step 1: Load data
-adata = sc.read_h5ad('scatac_data.h5ad')
-
-# Step 2: Tokenize cells
-tokenize_cells(
- adata='scatac_data.h5ad',
- universe_file='universe.bed',
- output='tokens.parquet'
-)
-
-# Step 3: Train scEmbed
-model = ScEmbed(embedding_dim=100)
-model.train(dataset='tokens.parquet', epochs=100)
-
-# Step 4: Generate embeddings
-embeddings = model.encode(adata)
-adata.obsm['scembed_X'] = embeddings
-
-# Step 5: Cluster with scanpy
-sc.pp.neighbors(adata, use_rep='scembed_X')
-sc.tl.leiden(adata)
-sc.tl.umap(adata)
-```
+from gtars.models import Region, RegionSet
+from gtars.tokenizers import Tokenizer
-### Universe Building and Evaluation
-
-```bash
-# Generate coverage
-cat bed_files/*.bed > combined.bed
-uniwig -m 25 combined.bed chrom.sizes coverage/
-
-# Build universe with coverage cutoff
-geniml universe build cc \
- --coverage-folder coverage/ \
- --output-file universe.bed \
- --cutoff 5 \
- --merge 100 \
- --filter-size 50
-
-# Evaluate universe quality
-geniml universe evaluate \
- --universe universe.bed \
- --coverage-folder coverage/ \
- --bed-folder bed_files/
+regions = RegionSet("data/peaks.bed")
+tokenizer = Tokenizer.from_bed("refs/universe.bed")
+encoded = tokenizer(regions)
+input_ids = encoded["input_ids"]
```
-## CLI Reference
+`RegionSet` and `Tokenizer` also accept remote inputs in some constructors;
+this skill permits local paths only unless network access is explicitly
+approved. `geniml.io.RegionSet(regions, backed=False)` remains available as a
+legacy Python implementation; backed sets are iterable but not indexable.
+`geniml.io.Region` uses `stop`, while `gtars.models.Region` uses `end`.
-Geniml provides command-line interfaces for major operations:
+With gtars 0.9.2, seven special tokens are added to a BED vocabulary. Therefore
+`len(tokenizer)` is not simply the number of universe rows. Preserve universe
+row order and the exact special-token map.
-```bash
-# Region2Vec training
-geniml region2vec --token-folder tokens/ --save-dir model/ --num-shuffle 1000
+### Region2Vec
-# BEDspace preprocessing
-geniml bedspace preprocess --input regions/ --metadata labels.csv --universe universe.bed
+The modern class lives at a concrete module path:
-# BEDspace training
-geniml bedspace train --input preprocessed.txt --output model/ --dim 100
-
-# BEDspace search
-geniml bedspace search -t r2l -d distances.pkl -q query.bed -n 10
-
-# Universe building
-geniml universe build cc --coverage-folder coverage/ --output universe.bed --cutoff 5
-
-# BEDshift randomization
-geniml bedshift --input peaks.bed --genome hg38 --preserve-chrom --iterations 100
+```python
+from geniml.region2vec.main import Region2VecExModel
+from geniml.region2vec.utils import Region2VecDataset
+from gtars.tokenizers import Tokenizer
+
+tokenizer = Tokenizer.from_bed("refs/universe.bed")
+dataset = Region2VecDataset("work/tokens.parquet", shuffle=True)
+model = Region2VecExModel(tokenizer=tokenizer, embedding_dim=100)
+model.train(dataset, epochs=10, window_size=5, num_cpus=4, seed=42)
```
-## When to Use Which Tool
-
-**Use Region2Vec when:**
-- Working with bulk genomic data (ChIP-seq, ATAC-seq, etc.)
-- Need unsupervised embeddings without metadata
-- Comparing region sets across experiments
-- Building features for downstream supervised learning
+The Parquet input must contain one list-valued `tokens` column, one document
+per row. See [references/region2vec.md](references/region2vec.md) for export,
+encoding, legacy CLI, and evaluation details.
-**Use BEDspace when:**
-- Metadata labels available (cell types, tissues, conditions)
-- Need to query regions by metadata or vice versa
-- Want joint embedding space for regions and labels
-- Building searchable genomic databases
+### scEmbed
-**Use scEmbed when:**
-- Analyzing single-cell ATAC-seq data
-- Clustering cells by chromatin accessibility
-- Annotating cell types from scATAC-seq
-- Integration with scanpy is desired
+Import `ScEmbed` from `geniml.scembed.main`. AnnData `.var` must contain
+`chr`, `start`, and `end`; rows are cells and nonzero features identify
+accessible regions. Pre-tokenize to a Parquet `tokens` column and use the same
+Tokenizer for training and inference. See
+[references/scembed.md](references/scembed.md).
-**Use Universe Building when:**
-- Need reference peak sets for tokenization
-- Combining multiple experiments into consensus
-- Want statistically rigorous region definitions
-- Building standard references for a project
+### BEDspace
-**Use Utilities when:**
-- Need to cache remote BED files (BBClient)
-- Generating null models for statistics (BEDshift)
-- Evaluating embedding quality (Evaluation)
-- Building search interfaces (Text2BedNN)
+BEDspace remains in 0.8.4 and invokes an external StarSpace executable.
+StarSpace is archived and upstream Geniml does not pin a compatible revision.
+Treat BEDspace as a legacy reproduction path, not the default for new systems.
+See [references/bedspace.md](references/bedspace.md) for the exact stable CLI
+spelling and an immutable, explicitly unverified build baseline.
-## Best Practices
+### Consensus universes and assessment
-### General Guidelines
+The installed 0.8.4 CLI uses:
-- **Universe quality is critical**: Invest time in building comprehensive, well-constructed universes
-- **Tokenization validation**: Check coverage (>80% ideal) before training
-- **Parameter tuning**: Experiment with embedding dimensions, learning rates, and training epochs
-- **Evaluation**: Always validate embeddings with multiple metrics and visualizations
-- **Documentation**: Record parameters and random seeds for reproducibility
+```text
+geniml build-universe {cc,ccf,ml,hmm} ...
+geniml assess-universe ...
+geniml eval {gdst,npt,ctt,rct,bin-gen} ...
+```
-### Performance Considerations
+CC/CCF/ML/HMM consume precomputed coverage bigWigs. Do not concatenate or
+generate coverage until all BED files pass the same assembly contract.
+Assessment and embedding metrics are distinct: `assess-universe` measures fit
+of a universe to interval collections, while `eval` implements CTT, RCT, GDST,
+and NPT for embeddings. See
+[references/consensus_peaks.md](references/consensus_peaks.md) and
+[references/utilities.md](references/utilities.md).
+
+## Important 0.8.4 migration notes
+
+- The 0.7.0 changelog moved new RegionSet/tokenizer work toward Gtars.
+- The 0.4.0 names `TreeTokenizer` and `AnnDataTokenizer` are historical; the
+ current Gtars API exposes `Tokenizer`.
+- In the 0.8.4 wheel, `geniml.region2vec` and `geniml.scembed` do not re-export
+ their modern classes/functions. Use the concrete module paths above.
+- `geniml tokenize` and `geniml region2vec` call names no longer exported by
+ their package `__init__` files; do not build new workflows around those CLI
+ paths without an installed-version smoke test.
+- `geniml scembed` parses legacy MatrixMarket options but its command body is a
+ no-op in 0.8.4. Use `geniml.scembed.main.ScEmbed`.
+- Official pages still show `geniml assess`; the release command is
+ `geniml assess-universe`.
+- `.gtok` remains present in legacy datasets, but upstream issue #14 proposes
+ deprecating many-file `.gtok` workflows. Prefer one bounded Parquet corpus.
+- Config key `embedding_size` is accepted only for backward compatibility;
+ use `embedding_dim`.
+
+## Model and universe compatibility
+
+A Region2Vec/scEmbed inference bundle is valid only when these agree:
+
+- model `config.yaml` `vocab_size` and `embedding_dim`;
+- exact `universe.bed` bytes/order and assembly;
+- tokenizer implementation/version and special-token IDs;
+- checkpoint tensor shapes and pooling policy;
+- Geniml/Gtars versions and any tokenization parameters.
+
+Geniml 0.8.4 defaults to `checkpoint.pt`, `config.yaml`, and `universe.bed`.
+Its loader uses `torch.load(..., weights_only=True)`, but `.pt`, Gensim
+`.model`, pickle, joblib, and native binaries remain untrusted inputs. Inspect
+and checksum artifacts before loading; use an isolated environment and never
+load a checkpoint merely to discover its metadata.
-- **Pre-tokenization**: For scEmbed, always pre-tokenize cells for faster training
-- **Memory management**: Large datasets may require batch processing or downsampling
-- **Computational resources**: ML/HMM universe methods are computationally intensive
-- **Model caching**: Use BBClient to avoid repeated downloads
+```bash
+python skills/geniml/scripts/model_artifact_inspector.py \
+ --model-dir models/region2vec
-### Integration Patterns
+python skills/geniml/scripts/tokenizer_compatibility.py \
+ --model-dir models/region2vec \
+ --universe refs/universe.bed \
+ --assembly GRCh38
+```
-- **With scanpy**: scEmbed embeddings integrate seamlessly as `adata.obsm` entries
-- **With BEDbase**: Use BBClient for accessing remote BED repositories
-- **With Hugging Face**: Export trained models for sharing and reproducibility
-- **With R**: Use reticulate for R integration (see utilities reference)
+`Region2VecExModel(model_path="org/repo")`, `ScEmbed(model_path="org/repo")`,
+and Gtars `Tokenizer.from_pretrained(...)` can download from Hugging Face.
+Local `from_pretrained("models/local")` loads a local bundle. Pin Hub revision
+and expected hashes when a user approves download; then work offline from the
+verified cache.
-## Related Projects
+## BEDbase downloads and caches
-Geniml is part of the BEDbase ecosystem:
+`BBClient.load_bed`, `load_bedset`, and token-cache operations may contact
+`https://api.bedbase.org`. The default cache is
+`$BBCLIENT_CACHE` or `~/.bbcache`; `BEDBASE_API` changes the endpoint. Do not
+read unrelated environment variables. Set an explicit project cache, estimate
+size, approve identifiers/endpoints, and verify returned checksums before use.
-- **BEDbase**: Unified platform for genomic regions
-- **BEDboss**: Processing pipeline for BED files
-- **Gtars**: Genomic tools and utilities
-- **BBClient**: Client for BEDbase repositories
+Local inspection commands are safer:
-## Additional Resources
+```text
+geniml bbclient seek ID --cache-folder /absolute/project/cache
+geniml bbclient inspect-bedfiles --cache-folder /absolute/project/cache
+geniml bbclient inspect-bedsets --cache-folder /absolute/project/cache
+```
-- **Documentation**: https://docs.bedbase.org/geniml/
-- **GitHub**: https://github.com/databio/geniml
-- **Pre-trained models**: Available on Hugging Face (databio organization)
-- **Publications**: Cited in documentation for methodological details
+The `cache-bed`, `cache-bedset`, and `cache-tokens` subcommands may use the
+network. Do not run them implicitly or include sensitive local BED files in an
+upload/cache workflow.
-## Troubleshooting
+## Local audit and planning CLIs
-**"Tokenization coverage too low":**
-- Check universe quality and completeness
-- Adjust p-value threshold (try 1e-6 instead of 1e-9)
-- Ensure universe matches genome assembly
+All scripts are standard-library-only and default to redacted JSON:
-**"Training not converging":**
-- Adjust learning rate (try 0.01-0.05 range)
-- Increase training epochs
-- Check data quality and preprocessing
+```bash
+# Audit manifest paths, checksums, assemblies, and patient/donor leakage
+python skills/geniml/scripts/corpus_auditor.py \
+ --manifest data/manifest.tsv --assembly-column assembly \
+ --group-column patient_id --split-column split
+
+# Plan tokenizer/model compatibility checks
+python skills/geniml/scripts/tokenizer_compatibility.py \
+ --model-dir models/r2v --universe refs/universe.bed --assembly GRCh38
+
+# Plan consensus construction; does not execute Geniml or coverage tools
+python skills/geniml/scripts/consensus_plan.py \
+ --manifest data/manifest.tsv --chrom-sizes refs/GRCh38.chrom.sizes \
+ --assembly GRCh38 --method cc --output-dir work/consensus
+
+# Plan an embedding run; does not import ML libraries
+python skills/geniml/scripts/embedding_plan.py \
+ --mode region2vec --data work/tokens.parquet \
+ --universe refs/universe.bed --output-dir work/r2v \
+ --assembly GRCh38
+```
-**"Out of memory errors":**
-- Reduce batch size for scEmbed
-- Process data in chunks
-- Use pre-tokenization for single-cell data
+Use `--help` for resource limits and explicit path-disclosure controls.
-**"StarSpace not found" (BEDspace):**
-- Install StarSpace separately: https://github.com/facebookresearch/StarSpace
-- Set `--path-to-starspace` parameter correctly
+## References
-For detailed troubleshooting and method-specific issues, consult the appropriate reference file.
+- [Region2Vec](references/region2vec.md): modern API, artifacts, CLI drift,
+ training, encoding, and evaluation.
+- [scEmbed](references/scembed.md): AnnData/token preparation, training,
+ inference, annotation, privacy, and leakage.
+- [BEDspace](references/bedspace.md): metadata schema, exact legacy CLI,
+ StarSpace status, artifacts, and retrieval.
+- [Consensus peaks](references/consensus_peaks.md): coverage prerequisites,
+ CC/CCF/ML/HMM, assessment, and assembly safeguards.
+- [Utilities](references/utilities.md): I/O, Gtars tokenizers, BBClient,
+ evaluation, model safety, migration, and dated sources.
+Source snapshot and primary-paper links are dated in
+[references/utilities.md](references/utilities.md). Re-check release metadata
+and installed signatures before changing the pinned versions.
diff --git a/skills/geniml/references/bedspace.md b/skills/geniml/references/bedspace.md
index e8c0d901..5acb5c3c 100644
--- a/skills/geniml/references/bedspace.md
+++ b/skills/geniml/references/bedspace.md
@@ -1,127 +1,267 @@
-# BEDspace: Joint Region and Metadata Embeddings
+# BEDspace
-## Overview
+Verified against `geniml==0.8.4` release source, the official BEDbase tutorial,
+and the archived StarSpace repository on 2026-07-23.
-BEDspace applies the StarSpace model to genomic data, enabling simultaneous training of numerical embeddings for both region sets and their metadata labels in a shared low-dimensional space. This allows for rich queries across regions and metadata.
+## Status
-## When to Use
+BEDspace jointly embeds region sets and metadata labels using the external
+StarSpace program. The primary paper evaluates label-to-region,
+region-to-label, and region-to-region retrieval.
-Use BEDspace when working with:
-- Region sets with associated metadata (cell types, tissues, conditions)
-- Search tasks requiring metadata-aware similarity
-- Cross-modal queries (e.g., "find regions similar to label X")
-- Joint analysis of genomic content and experimental conditions
+Primary source: Gharavi et al. (2024), *Joint representation learning for
+retrieval and annotation of genomic interval sets*,
+doi:[10.3390/bioengineering11030263](https://doi.org/10.3390/bioengineering11030263).
-## Workflow
+The code path still exists in Geniml 0.8.4, but it is a **legacy reproduction
+workflow**:
-BEDspace consists of four sequential operations:
+- StarSpace is not a Python dependency and must be compiled separately.
+- `facebookresearch/StarSpace` is archived.
+- Geniml does not state or enforce a compatible StarSpace version.
+- several official examples and 0.8.4 CLI/API details disagree;
+- the 0.8.4 `bedspace search` dispatcher imports a `main` function that is
+ absent from `geniml.bedspace.search`.
-### 1. Preprocess
+Do not choose BEDspace by default for a new production search service. Use it
+when reproducing the published method or an existing pinned workflow, and
+record the limitations.
-Format genomic intervals and metadata for StarSpace training:
+## Data and privacy contract
-```bash
-geniml bedspace preprocess \
- --input /path/to/regions/ \
- --metadata labels.csv \
- --universe universe.bed \
- --labels "cell_type,tissue" \
- --output preprocessed.txt
-```
+Required inputs:
-**Required files:**
-- **Input folder**: Directory containing BED files
-- **Metadata CSV**: Must include `file_name` column matching BED filenames, plus metadata columns
-- **Universe file**: Reference BED file for tokenization
-- **Labels**: Comma-separated list of metadata columns to use
+- a local directory of validated BED files;
+- a local metadata CSV with a file-path/name column and selected label columns;
+- a local universe BED;
+- explicit train/test manifests grouped by patient/donor;
+- one assembly, coordinate convention, and contig policy.
-The preprocessing step adds `__label__` prefixes to metadata and converts regions to StarSpace-compatible format.
+Metadata values can reveal diagnoses, cell types, tissues, treatment, cohort,
+or donor identity. BEDspace places selected labels directly into training text
+and writes filenames/labels into result CSVs. Keep inputs and outputs in a
+restricted project directory. Default logs should report only row/file counts
+and schema names, not values.
-### 2. Train
+Before preprocessing:
-Execute StarSpace model on preprocessed data:
+1. Split complete patients/donors into train/validation/test.
+2. Build or select the universe using training data only.
+3. Validate every BED and the universe against the same chromosome sizes.
+4. Confirm metadata path values resolve to intended local regular files.
+5. Reject URLs, symlinks, duplicate paths, missing files, and mixed assemblies.
+6. Decide how missing/multi-valued labels are encoded.
```bash
-geniml bedspace train \
- --path-to-starspace /path/to/starspace \
- --input preprocessed.txt \
- --output model/ \
- --dim 100 \
- --epochs 50 \
- --lr 0.05
+python skills/geniml/scripts/corpus_auditor.py \
+ --manifest data/bedspace.tsv \
+ --group-column patient_id \
+ --split-column split \
+ --assembly-column assembly
```
-**Key training parameters:**
-- `--dim`: Embedding dimension (typical: 50-200)
-- `--epochs`: Training epochs (typical: 20-100)
-- `--lr`: Learning rate (typical: 0.01-0.1)
+## Exact 0.8.4 CLI surface
+
+The release exposes:
-### 3. Distances
+```text
+geniml bedspace preprocess
+geniml bedspace train
+geniml bedspace distances
+geniml bedspace search
+```
+
+Use installed `--help` as the final authority. Source-backed flags follow.
-Compute distance metrics between region sets and metadata labels:
+### Preprocess
```bash
-geniml bedspace distances \
- --input model/ \
- --metadata labels.csv \
- --universe universe.bed \
- --output distances.pkl
+geniml bedspace preprocess \
+ --input /absolute/project/beds \
+ --metadata /absolute/project/train.csv \
+ --universe /absolute/project/universe.bed \
+ --labels "cell_type,target" \
+ --output /absolute/project/preprocessed/
```
-This step creates a distance matrix needed for similarity searches.
+The implementation creates a Gtars `Tokenizer` from the universe, uses a
+hard-coded pool of eight processes, and writes:
-### 4. Search
+```text
+<output>train_input.txt
+```
-Retrieve similar items across three scenarios:
+The code joins this filename by string concatenation, not `os.path.join`, so
+the output argument must end in a path separator. Create and validate the
+directory first. The preprocessing text contains labels and tokenized genomic
+content; protect it as sensitive derived data.
-**Region-to-Label (r2l)**: Query region set → retrieve similar metadata labels
-```bash
-geniml bedspace search -t r2l -d distances.pkl -q query_regions.bed -n 10
+The current source does not expose worker bounds through the CLI. Run only
+after estimating memory and CPU impact, or invoke a reviewed wrapper that
+controls resources.
+
+### Train
+
+The long source flag is misspelled:
+
+```text
+--path-to-starsapce
```
-**Label-to-Region (l2r)**: Query metadata label → retrieve similar region sets
+Use the stable short form `-s`. Its value must be the **directory containing**
+the executable named `starspace`; despite some help text, do not pass the
+executable itself.
+
```bash
-geniml bedspace search -t l2r -d distances.pkl -q "T_cell" -n 10
+geniml bedspace train \
+ -s /absolute/project/vendor/StarSpace \
+ --input /absolute/project/preprocessed/train_input.txt \
+ --output /absolute/project/model/ \
+ --dim 100 \
+ --epochs 50 \
+ --lr 0.05
+```
+
+Geniml invokes an argv list equivalent to:
+
+```text
+starspace train
+ -trainFile INPUT
+ -model OUTPUT/starspace_trained_model
+ -trainMode 0
+ -dim DIM
+ -epoch EPOCHS
+ -negSearchLimit 5
+ -thread 20
+ -lr LEARNING_RATE
```
-**Region-to-Region (r2r)**: Query region set → retrieve similar region sets
+The thread count is hard-coded to 20. The implementation waits for the process
+but does not check a nonzero return code. Verify output existence, size, and
+checksums yourself. If an existing model path is present, Geniml adds
+`-initModel` and resumes/mutates training; use a fresh output directory unless
+resume is intentional.
+
+### Distances
+
```bash
-geniml bedspace search -t r2r -d distances.pkl -q query_regions.bed -n 10
+geniml bedspace distances \
+ -i /absolute/project/model/starspace_trained_model \
+ -s /absolute/project/vendor/StarSpace \
+ --metadata-train /absolute/project/train.csv \
+ --metadata-test /absolute/project/test.csv \
+ --universe /absolute/project/universe.bed \
+ --project-name heldout \
+ --files /absolute/project/beds \
+ --labels "cell_type,target" \
+ --output /absolute/project/distances/ \
+ --threshold 0.5
```
-The `-n` parameter controls the number of results returned.
+The current outputs are CSV/text files, not a single pickle:
+
+- `raw_cosdist_rl.csv`
+- `similarity_score_rl.csv`
+- `similarity_score_rr.csv`
+- `<project>_starspace_embed.txt`
+- `<project>_train_starspace_embed.txt`
-## Python API
+The implementation also uses `~/.bedspace/test_documents.txt` and
+`~/.bedspace/train_documents.txt`. Isolate `HOME` or review that cache before
+running on sensitive data. Output CSVs contain filenames and labels; never
+paste unredacted rows into chat or CI logs.
-```python
-from geniml.bedspace import BEDSpaceModel
+### Search
-# Load trained model
-model = BEDSpaceModel.load('model/')
+The CLI advertises search types `l2r`, `r2l`, and `r2r`, with the query as a
+positional argument:
-# Query similar items
-results = model.search(
- query="T_cell",
- search_type="l2r",
- top_k=10
-)
+```text
+geniml bedspace search QUERY -t l2r -d DISTANCES.csv -n 10
```
-## Best Practices
+Do not rely on this path in 0.8.4: the dispatcher imports
+`geniml.bedspace.search.main`, but the release file defines only
+`run_scenario1`, `run_scenario2`, and `run_scenario3`. There is no
+`BEDSpaceModel` class in the release API. Read the verified CSVs with a safe
+local data-frame workflow instead of loading an old `.pkl` or calling the
+broken dispatcher.
-- **Metadata structure**: Ensure metadata CSV includes `file_name` column that exactly matches BED filenames (without path)
-- **Label selection**: Choose informative metadata columns that capture biological variation of interest
-- **Universe consistency**: Use the same universe file across preprocessing, distances, and any subsequent analyses
-- **Validation**: Preprocess and check output format before investing in training
-- **StarSpace installation**: Install StarSpace separately as it's an external dependency
+## StarSpace setup: explicit legacy-only baseline
-## Output Interpretation
+Only do this after the user approves network access and native compilation.
+There is no upstream Geniml compatibility pin. The only immutable baseline
+available from the archived upstream default branch is its final commit:
-Search results return items ranked by similarity in the joint embedding space:
-- **r2l**: Identifies metadata labels characterizing your query regions
-- **l2r**: Finds region sets matching your metadata criteria
-- **r2r**: Discovers region sets with similar genomic content
+```text
+8aee0a950aa607c023e5c91cff518bec335b5df5
+```
+
+A reproducible source checkout is:
-## Requirements
+```bash
+git init vendor/StarSpace
+git -C vendor/StarSpace remote add origin https://github.com/facebookresearch/StarSpace.git
+git -C vendor/StarSpace fetch --depth 1 origin 8aee0a950aa607c023e5c91cff518bec335b5df5
+git -C vendor/StarSpace checkout --detach FETCH_HEAD
+make -C vendor/StarSpace
+```
-BEDspace requires StarSpace to be installed separately. Download from: https://github.com/facebookresearch/StarSpace
+This pin makes the source immutable; it does **not** establish compatibility
+with Geniml 0.8.4. Compile in an isolated build environment after reviewing
+the archived source and Boost/native toolchain. Record:
+
+- commit and repository URL;
+- compiler, make, Boost, OS, and architecture;
+- build log;
+- SHA-256 and executable permissions of `vendor/StarSpace/starspace`;
+- a synthetic preprocess/train/distances smoke result.
+
+Never execute an unverified StarSpace binary downloaded from a third party.
+Do not add its directory globally to `PATH`; pass the explicit local directory
+with `-s`.
+
+## Model and retrieval provenance
+
+Keep one immutable manifest covering:
+
+- Geniml/Gtars and StarSpace versions/commit;
+- Python lockfile and native binary checksum;
+- train/test manifest checksums and grouping;
+- universe checksum, assembly, row order, and tokenizer special tokens;
+- selected metadata columns and missing-value policy;
+- preprocessing text checksum;
+- dimension, epochs, learning rate, hard-coded thread count, and resume state;
+- every model/embedding/distance output checksum.
+
+Similarity is not a calibrated probability. Validate retrieval on held-out
+patients/donors, report per-query metrics and class support, and compare with
+metadata-only and interval-overlap baselines. Avoid searching the test set
+while selecting labels, thresholds, or the universe.
+
+## Migration guidance
+
+Old guidance to remove:
+
+- `BEDSpaceModel.load(...)` / `.search(...)`: not present in 0.8.4.
+- `distances.pkl`: current distance code writes CSV/text.
+- `--path-to-starspace`: official docs show it, but release source spells the
+ long flag `--path-to-starsapce`; use `-s`.
+- advice to install StarSpace from an unpinned branch.
+
+For new systems, first define the retrieval task and privacy boundary. A
+maintained vector-search stack over locally generated, fully versioned
+embeddings may be safer than building new infrastructure around archived
+StarSpace, but it is not automatically method-equivalent to BEDspace.
+
+## Official sources
+
+- [Official BEDspace tutorial](https://docs.bedbase.org/geniml/tutorials/bedspace/)
+ (undated; accessed 2026-07-23)
+- [Geniml v0.8.4 BEDspace source](https://github.com/databio/geniml/tree/v0.8.4/geniml/bedspace)
+ (released 2026-01-14; accessed 2026-07-23)
+- [Archived StarSpace repository](https://github.com/facebookresearch/StarSpace)
+ (final default-branch commit dated 2019-12-13; repository archived; accessed
+ 2026-07-23)
+- [Primary BEDspace paper](https://doi.org/10.3390/bioengineering11030263)
+ (2024)
diff --git a/skills/geniml/references/consensus_peaks.md b/skills/geniml/references/consensus_peaks.md
index 98bb5400..de7e9cf5 100644
--- a/skills/geniml/references/consensus_peaks.md
+++ b/skills/geniml/references/consensus_peaks.md
@@ -1,238 +1,334 @@
-# Consensus Peaks: Universe Building
+# Consensus peaks and universe assessment
-## Overview
+Verified against `geniml==0.8.4` release source and official BEDbase
+documentation on 2026-07-23.
-Geniml provides tools for building genomic "universes" — standardized reference sets of consensus peaks from collections of BED files. These universes represent genomic regions where analyzed datasets show significant coverage overlap, serving as reference vocabularies for tokenization and analysis.
+## Method scope
-## When to Use
+A Geniml universe is a reference interval vocabulary derived from coverage
+across a collection of BED files. Release 0.8.4 implements:
-Use consensus peak creation when:
-- Building reference peak sets from multiple experiments
-- Creating universe files for Region2Vec or BEDspace tokenization
-- Standardizing genomic regions across a collection of datasets
-- Defining regions of interest with statistical significance
+- **CC**: coverage cutoff;
+- **CCF**: coverage cutoff with flexible core/boundary fields;
+- **ML**: maximum-likelihood flexible universe;
+- **HMM**: hidden-state model over start/core/end coverage.
-## Workflow
+Primary source: Rymuza et al. (2024), *Methods for constructing and evaluating
+consensus genomic interval sets*,
+doi:[10.1093/nar/gkae685](https://doi.org/10.1093/nar/gkae685).
-### Step 1: Combine BED Files
+The paper motivates and evaluates these methods; it does not make one method
+universally best. Choose using training-only data, assay-specific validation,
+resource constraints, and held-out universe-fit metrics.
-Merge all BED files into a single combined file:
+## Non-negotiable input contract
-```bash
-cat /path/to/bed/files/*.bed > combined_files.bed
-```
+All source BED files and chromosome sizes must agree on:
+
+- assembly/accession and patch;
+- 0-based half-open BED coordinates;
+- chromosome/contig naming and inclusion policy;
+- sort order;
+- duplicate and overlap handling;
+- strand interpretation;
+- liftover provenance, if any.
-### Step 2: Generate Coverage Tracks
+Reject malformed rows, negative starts, `end <= start`, coordinates beyond
+contig length, unknown contigs, mixed assemblies, and integer overflow before
+coverage generation. A chromosome name match alone is not proof of assembly
+compatibility.
-Create bigWig coverage tracks using uniwig with a smoothing window:
+Build the universe only from the training patients/donors. If samples from a
+held-out patient contribute to coverage, the resulting vocabulary leaks test
+feature prevalence.
```bash
-uniwig -m 25 combined_files.bed chrom.sizes coverage/
+python skills/geniml/scripts/corpus_auditor.py \
+ --manifest data/train_manifest.tsv \
+ --group-column patient_id \
+ --split-column split \
+ --assembly-column assembly
```
-**Parameters:**
-- `-m 25`: Smoothing window size (25bp typical for chromatin accessibility)
-- `chrom.sizes`: Chromosome sizes file for your genome
-- `coverage/`: Output directory for bigWig files
+## Coverage prerequisites
-The smoothing window helps reduce noise and creates more robust peak boundaries.
+Geniml consumes bigWig tracks in a local coverage directory. With the default
+prefix `all`, methods expect:
-### Step 3: Build Universe
+```text
+all_start.bw
+all_core.bw
+all_end.bw
+```
-Use one of four methods to construct the consensus peaks:
+CC and CCF read `all_core.bw`. HMM and likelihood-based methods use
+start/core/end tracks. The tracks must share contigs and lengths with the
+checksummed chromosome-sizes file.
-## Universe-Building Methods
+Official Geniml pages describe producing these tracks with the ecosystem's
+coverage tooling, but the current Gtars CLI has changed across releases. Do not
+emit or run a guessed `uniwig` command. Pin the exact Gtars/uniwig executable,
+capture its `--help`, and smoke-test its output naming on synthetic local BED
+data. Record:
-### 1. Coverage Cutoff (CC)
+- tool version and binary SHA-256;
+- complete argv (not a shell-expanded wildcard);
+- chromosome-sizes SHA-256;
+- ordered input manifest and checksums;
+- smoothing/binning parameters;
+- output track sizes, contigs, lengths, and checksums.
-The simplest approach using a fixed coverage threshold:
+The bundled planner validates local inputs and emits the Geniml stage, but
+intentionally marks coverage generation as an external prerequisite:
```bash
-geniml universe build cc \
- --coverage-folder coverage/ \
- --output-file universe_cc.bed \
- --cutoff 5 \
- --merge 100 \
- --filter-size 50
+python skills/geniml/scripts/consensus_plan.py \
+ --manifest data/train_manifest.tsv \
+ --chrom-sizes refs/GRCh38.chrom.sizes \
+ --assembly GRCh38 \
+ --method cc \
+ --cutoff 2 \
+ --output-dir work/consensus
```
-**Parameters:**
-- `--cutoff`: Coverage threshold (1 = union; file count = intersection)
-- `--merge`: Distance for merging adjacent peaks (bp)
-- `--filter-size`: Minimum peak size for inclusion (bp)
+It does not execute Geniml, Gtars, native binaries, or network requests.
-**Use when:** Simple threshold-based selection is sufficient
+## Exact 0.8.4 CLI
-### 2. Coverage Cutoff Flexible (CCF)
+The top-level command is `build-universe`, not `universe build`.
-Creates confidence intervals around likelihood cutoffs for boundaries and region cores:
+### CC
```bash
-geniml universe build ccf \
- --coverage-folder coverage/ \
- --output-file universe_ccf.bed \
- --cutoff 5 \
- --confidence 0.95 \
+geniml build-universe cc \
+ --coverage-folder /absolute/project/coverage \
+ --coverage-prefix all \
+ --output-file /absolute/project/universe_cc.bed \
+ --cutoff 2 \
--merge 100 \
--filter-size 50
```
-**Additional parameters:**
-- `--confidence`: Confidence level for flexible boundaries (0-1)
+`--cutoff` is an integer. If omitted, release source uses mean base coverage
+for each chromosome. `--merge` merges nearby output segments; `--filter-size`
+removes shorter segments. The output file must not already exist.
-**Use when:** Uncertainty in peak boundaries should be captured
+Do not claim `cutoff=number_of_files` is a strict sample intersection unless
+coverage generation contributes exactly one unit per sample at each base.
+Fragment/read coverage or duplicated intervals can violate that assumption.
-### 3. Maximum Likelihood (ML)
+Python:
-Builds probabilistic models accounting for region start/end positions:
-
-```bash
-geniml universe build ml \
- --coverage-folder coverage/ \
- --output-file universe_ml.bed \
- --merge 100 \
- --filter-size 50 \
- --model-type gaussian
+```python
+from geniml.universe.cc_universe import cc_universe
+
+cc_universe(
+ cove="work/coverage",
+ file_out="work/universe_cc.bed",
+ cove_prefix="all",
+ merge=100,
+ filter_size=50,
+ cutoff=2,
+)
```
-**Parameters:**
-- `--model-type`: Distribution for likelihood estimation (gaussian, poisson)
-
-**Use when:** Statistical modeling of peak locations is important
-
-### 4. Hidden Markov Model (HMM)
-
-Models genomic regions as hidden states with coverage as emissions:
+### CCF
```bash
-geniml universe build hmm \
- --coverage-folder coverage/ \
- --output-file universe_hmm.bed \
- --states 3 \
- --merge 100 \
- --filter-size 50
+geniml build-universe ccf \
+ --coverage-folder /absolute/project/coverage \
+ --coverage-prefix all \
+ --output-file /absolute/project/universe_ccf.bed
```
-**Parameters:**
-- `--states`: Number of HMM hidden states (typically 2-5)
-
-**Use when:** Complex patterns of genomic states should be captured
-
-## Python API
+Python:
```python
-from geniml.universe import build_universe
-
-# Build using coverage cutoff method
-universe = build_universe(
- coverage_folder='coverage/',
- method='cc',
- cutoff=5,
- merge_distance=100,
- min_size=50,
- output_file='universe.bed'
+from geniml.universe.ccf_universe import ccf_universe
+
+ccf_universe(
+ cove="work/coverage",
+ file_out="work/universe_ccf.bed",
+ cove_prefix="all",
)
```
-## Method Comparison
-
-| Method | Complexity | Flexibility | Computational Cost | Best For |
-|--------|------------|-------------|-------------------|----------|
-| CC | Low | Low | Low | Quick reference sets |
-| CCF | Medium | Medium | Medium | Boundary uncertainty |
-| ML | High | High | High | Statistical rigor |
-| HMM | High | High | Very High | Complex patterns |
-
-## Best Practices
+The stable source has no CCF `--confidence`, `--merge`, or `--filter-size`
+arguments. CCF writes BED9-like rows carrying core/boundary information; do
+not reduce them to BED3 before confirming downstream semantics.
-### Choosing a Method
+### Likelihood model and ML universe
-1. **Start with CC**: Quick and interpretable for initial exploration
-2. **Use CCF**: When peak boundaries are uncertain or noisy
-3. **Apply ML**: For publication-quality statistical analysis
-4. **Deploy HMM**: When modeling complex chromatin states
+The 0.8.4 likelihood command has no `build_model` subcommand:
-### Parameter Selection
-
-**Coverage cutoff:**
-- `cutoff = 1`: Union of all peaks (most permissive)
-- `cutoff = n_files`: Intersection (most stringent)
-- `cutoff = 0.5 * n_files`: Moderate consensus (typical choice)
-
-**Merge distance:**
-- ATAC-seq: 100-200bp
-- ChIP-seq (narrow peaks): 50-100bp
-- ChIP-seq (broad peaks): 500-1000bp
+```bash
+geniml lh \
+ --model-file /absolute/project/model.tar \
+ --coverage-folder /absolute/project/coverage \
+ --coverage-prefix all \
+ --file-no 4
+```
-**Filter size:**
-- Minimum 30bp to avoid artifacts
-- 50-100bp typical for most assays
-- Larger for broad histone marks
+Then:
-### Quality Control
+```bash
+geniml build-universe ml \
+ --model-file /absolute/project/model.tar \
+ --coverage-folder /absolute/project/coverage \
+ --coverage-prefix all \
+ --output-file /absolute/project/universe_ml.bed
+```
-After building, assess universe quality:
+Python:
```python
-from geniml.evaluation import assess_universe
-
-metrics = assess_universe(
- universe_file='universe.bed',
- coverage_folder='coverage/',
- bed_files='bed_files/'
+from geniml.likelihood.build_model import main as build_likelihood
+from geniml.universe.ml_universe import ml_universe
+
+build_likelihood(
+ model_file="work/model.tar",
+ coverage_folder="work/coverage",
+ coverage_prefix="all",
+ file_no=4,
+)
+ml_universe(
+ model_file="work/model.tar",
+ cove_folder="work/coverage",
+ cove_prefix="all",
+ file_out="work/universe_ml.bed",
)
-
-print(f"Number of regions: {metrics['n_regions']}")
-print(f"Mean region size: {metrics['mean_size']:.1f}bp")
-print(f"Coverage of input peaks: {metrics['coverage']:.1%}")
```
-**Key metrics:**
-- **Region count**: Should capture major features without excessive fragmentation
-- **Size distribution**: Should match expected biology (e.g., ~500bp for ATAC-seq)
-- **Input coverage**: Proportion of original peaks represented (typically >80%)
+Treat the `.tar` likelihood model as an untrusted archive if it is not locally
+created and checksummed. Inspect archive member names and reject absolute
+paths, `..`, links, devices, and excessive expansion before extraction.
-## Output Format
-
-Consensus peaks are saved as BED files with three required columns:
+### HMM
+```bash
+geniml build-universe hmm \
+ --coverage-folder /absolute/project/coverage \
+ --coverage-prefix all \
+ --output-file /absolute/project/universe_hmm.bed
```
-chr1 1000 1500
-chr1 2000 2800
-chr2 500 1000
+
+Use `--not-normalize` only after validating what scale the model expects.
+`--save-max-cove` adds maximum coverage information. The 0.8.4 CLI has no
+`--states` argument; the model structure is defined in source constants.
+
+Python:
+
+```python
+from geniml.universe.hmm_universe import hmm_universe
+
+hmm_universe(
+ coverage_folder="work/coverage",
+ out_file="work/universe_hmm.bed",
+ prefix="all",
+ normalize=True,
+ save_max_cove=False,
+)
```
-Additional columns may include confidence scores or state annotations depending on the method.
+## Validate every output
-## Common Workflows
+Universe builders do not replace input validation. After construction:
-### For Region2Vec
+1. Re-run BED validation against the same chromosome sizes.
+2. Confirm sorted, nonempty output and expected BED column count.
+3. Check region count, length distribution, covered bases, overlaps, and
+ duplicate coordinates.
+4. Confirm no unknown contigs or out-of-bounds ends.
+5. Record output SHA-256 and method parameters.
+6. Build a fresh Gtars tokenizer and record vocabulary/special-token sizes.
+7. Never reorder the universe after a model or token corpus has been created.
-1. Build universe using preferred method
-2. Use universe as tokenization reference
-3. Tokenize BED files
-4. Train Region2Vec model
+Some 0.8.4 functions assume at least one selected base per chromosome and may
+index an empty result. Test sparse/empty chromosomes synthetically and fail
+closed rather than accepting a partial output.
-### For BEDspace
+## Assess fit to held-out collections
-1. Build universe from all datasets
-2. Use universe in preprocessing step
-3. Train BEDspace with metadata
-4. Query across regions and labels
+The release CLI is:
-### For scEmbed
+```bash
+geniml assess-universe \
+ --raw-data-folder /absolute/project/validation_beds \
+ --file-list /absolute/project/validation_files.txt \
+ --universe /absolute/project/universe_cc.bed \
+ --overlap \
+ --distance \
+ --distance-universe-to-file \
+ --folder-out /absolute/project/assessment \
+ --pref validation \
+ --no-workers 4
+```
-1. Create universe from bulk or aggregated scATAC-seq
-2. Use for cell tokenization
-3. Train scEmbed model
-4. Generate cell embeddings
+Available flags include:
-## Troubleshooting
+- `--overlap`;
+- `--distance`;
+- `--distance-flexible`;
+- `--distance-universe-to-file`;
+- `--distance-flexible-universe-to-file`;
+- `--save-to-file`;
+- `--save-each`.
-**Too few regions:** Lower cutoff threshold or reduce filter size
+`--save-each` can generate large, sensitive per-interval outputs. Leave it off
+unless required and bound output size. The docs still show `geniml assess`;
+that is not the 0.8.4 top-level command.
-**Too many regions:** Raise cutoff threshold, increase merge distance, or increase filter size
+Python entry points include:
-**Noisy boundaries:** Use CCF or ML methods instead of CC
+```python
+from geniml.assess.assess import (
+ get_f_10_score,
+ get_mean_rbs,
+ run_all_assessment_methods,
+)
+```
-**Long computation:** Start with CC method for quick results, then refine with ML/HMM if needed
+F10, reciprocal-boundary-style distance summaries, and likelihood measure
+different properties. Compare multiple candidate universes on validation
+patients, then evaluate the chosen one once on test patients. Do not tune the
+cutoff, merging, or method on the test collection.
+
+## Reproducibility record
+
+Store:
+
+- ordered input manifest and grouping;
+- assembly/accession, chromosome sizes, coordinate and contig policy;
+- every input and coverage checksum;
+- exact coverage and Geniml argv;
+- Geniml, Gtars, pyBigWig, NumPy, HMM, Python, OS, and architecture versions;
+- method, cutoff/model, prefix, normalization, merge/filter parameters;
+- output and assessment checksums;
+- exclusions, failures, empty contigs, and liftover losses.
+
+Keep file names and sample labels redacted in portable reports.
+
+## Migration corrections
+
+Remove or correct these stale patterns:
+
+- `geniml universe build ...` → `geniml build-universe ...`;
+- `geniml universe evaluate ...` → `geniml assess-universe ...`;
+- CCF `--confidence` → not present in 0.8.4;
+- HMM `--states` → not present in 0.8.4;
+- ML `--model-type gaussian|poisson` → not present in 0.8.4;
+- generic `build_universe(...)` → not exported by the stable universe module;
+- claims that a fixed percentage coverage is universally appropriate.
+
+## Official sources
+
+- [Official consensus CLI guide](https://docs.bedbase.org/geniml/tutorials/create-consensus-peaks)
+ (undated; accessed 2026-07-23)
+- [Official consensus Python guide](https://docs.bedbase.org/geniml/notebooks/create-consensus-peaks-python)
+ (undated; accessed 2026-07-23)
+- [Official universe assessment guide](https://docs.bedbase.org/geniml/tutorials/assess-universe/)
+ (undated; accessed 2026-07-23)
+- [Geniml v0.8.4 universe source](https://github.com/databio/geniml/tree/v0.8.4/geniml/universe)
+ (released 2026-01-14; accessed 2026-07-23)
+- [Primary consensus-universe paper](https://doi.org/10.1093/nar/gkae685)
+ (2024)
diff --git a/skills/geniml/references/region2vec.md b/skills/geniml/references/region2vec.md
index acafab1c..df3ca529 100644
--- a/skills/geniml/references/region2vec.md
+++ b/skills/geniml/references/region2vec.md
@@ -1,90 +1,289 @@
-# Region2Vec: Genomic Region Embeddings
+# Region2Vec
-## Overview
+Verified against `geniml==0.8.4` release source and the official BEDbase
+documentation on 2026-07-23.
-Region2Vec generates unsupervised embeddings of genomic regions and region sets from BED files. It maps genomic regions to a vocabulary, creates sentences through concatenation, and applies word2vec training to learn meaningful representations.
+## What the method does
-## When to Use
+Region2Vec learns vectors for genomic regions from region co-occurrence within
+interval sets. The primary paper describes randomizing regions within each set
+to create word2vec-like contexts, then pooling region vectors to represent
+sets. Treat learned proximity as a property of the training corpus and
+universe, not as proof of a biological mechanism.
-Use Region2Vec when working with:
-- BED file collections requiring dimensionality reduction
-- Genomic region similarity analysis
-- Downstream ML tasks requiring region feature vectors
-- Comparative analysis across multiple genomic datasets
+Primary method source: Gharavi et al. (2021), *Embeddings of genomic region sets
+capture rich biological associations in low dimensions*,
+doi:[10.1093/bioinformatics/btab439](https://doi.org/10.1093/bioinformatics/btab439).
-## Workflow
+## Stable 0.8.4 API reality
-### Step 1: Prepare Data
+Use concrete module paths:
-Gather BED files in a source folder. Optionally specify a file list (default uses all files in the directory). Prepare a universe file as the reference vocabulary for tokenization.
+```python
+from geniml.region2vec.main import Region2VecExModel
+from geniml.region2vec.utils import Region2VecDataset
+from gtars.tokenizers import Tokenizer
+```
+
+The release's `geniml.region2vec.__init__` does not export
+`Region2VecExModel` or the legacy `region2vec` function. Consequently,
+`from geniml.region2vec import region2vec` and the installed
+`geniml region2vec ...` dispatch path are not reliable in 0.8.4. The old
+function still exists at `geniml.region2vec.main_legacy.region2vec`, but use it
+only to reproduce an existing workflow after a pinned smoke test.
+
+## Universe and tokenizer contract
+
+Create the tokenizer from a validated local BED universe:
+
+```python
+from gtars.tokenizers import Tokenizer
+
+tokenizer = Tokenizer.from_bed("refs/universe.bed")
+```
+
+With verified `gtars==0.9.2`:
+
+- universe regions receive stable IDs in file order;
+- seven special tokens are added (`unk`, `pad`, `mask`, `cls`, `eos`, `bos`,
+ and `sep`);
+- `len(tokenizer)` is universe row count plus special tokens;
+- `tokenizer(region_set)["input_ids"]` returns integer IDs.
+
+Compatibility requires the exact universe bytes/order, assembly, contig policy,
+Gtars version, special-token map/IDs, and tokenization behavior. Re-sorting a
+universe changes IDs even when the interval set is mathematically identical.
+Never infer compatibility from a shared filename such as `hg38.bed`.
+
+Before tokenizing:
+
+1. Validate BED as 0-based half-open intervals.
+2. Confirm a single assembly with a checksummed chromosome-sizes file.
+3. Resolve `chr1`/`1`, alt-contig, mitochondrial, and strand policies.
+4. Split by patient/donor before learning or evaluating representations.
+5. Record the universe SHA-256 and row count.
+
+Run:
-### Step 2: Tokenization
+```bash
+python skills/geniml/scripts/bed_validator.py \
+ --input refs/universe.bed \
+ --assembly GRCh38 \
+ --chrom-sizes refs/GRCh38.chrom.sizes
+```
-Run hard tokenization to convert genomic regions into tokens:
+## Prepare the token corpus
+
+`Region2VecDataset` reads a Parquet file with one list-valued column named
+`tokens`; each row is one BED document, sample, or cell. IDs must come from the
+same tokenizer that initializes the model.
```python
-from geniml.tokenization import hard_tokenization
+import pyarrow as pa
+import pyarrow.parquet as pq
+from gtars.models import RegionSet
-src_folder = '/path/to/raw/bed/files'
-dst_folder = '/path/to/tokenized_files'
-universe_file = '/path/to/universe_file.bed'
+documents = []
+for local_bed in validated_local_beds:
+ ids = tokenizer(RegionSet(local_bed))["input_ids"]
+ documents.append(ids)
-hard_tokenization(src_folder, dst_folder, universe_file, 1e-9)
+table = pa.table({"tokens": pa.array(documents, type=pa.list_(pa.int32()))})
+pq.write_table(table, "work/tokens.parquet")
```
-The final parameter (1e-9) is the p-value threshold for tokenization overlap significance.
+This example assumes `validated_local_beds` came from a bounded, local
+manifest. Do not discover arbitrary directory contents, follow symlinks, or
+log sample filenames. Ensure every token is an integer in
+`[0, len(tokenizer))`. Empty or unusually short documents need an explicit
+policy; do not silently discard them after splitting.
-### Step 3: Train Region2Vec Model
+`Region2VecDataset(path, shuffle=True, convert_to_str=False)` loads the full
+Parquet `tokens` column into memory. Bound rows and total tokens before
+construction. `shuffle=True` mutates each document order when accessed; record
+the training seed, but do not assume every library/thread schedule is bitwise
+deterministic.
-Execute Region2Vec training on the tokenized files:
+## Train the modern model
```python
-from geniml.region2vec import region2vec
+from geniml.region2vec.main import Region2VecExModel
+from geniml.region2vec.utils import Region2VecDataset
-region2vec(
- token_folder=dst_folder,
- save_dir='./region2vec_model',
- num_shufflings=1000,
+dataset = Region2VecDataset("work/tokens.parquet", shuffle=True)
+model = Region2VecExModel(
+ tokenizer=tokenizer,
embedding_dim=100,
- context_len=50,
+ pooling_method="mean",
+ device="cpu",
+)
+model.train(
+ dataset,
window_size=5,
- init_lr=0.025
+ epochs=10,
+ min_count=10,
+ num_cpus=4,
+ seed=42,
)
```
-## Key Parameters
+Current source defaults are not fully consistent across legacy and modern
+modules. Pass every material setting explicitly. `train` uses Gensim
+Word2Vec, then copies learned weights into a Torch embedding matrix.
+`load_from_checkpoint` and per-epoch Gensim `.model` files deserialize Gensim
+artifacts; load only artifacts you created or independently trust.
+
+Suggested run record:
+
+- Geniml, Gtars, Python, Torch, Gensim, NumPy, and PyArrow versions;
+- lockfile digest and platform;
+- universe/checkpoint/config/token-corpus/manifest SHA-256;
+- assembly, coordinate and contig contracts;
+- vocabulary and special-token sizes;
+- embedding dimension, window, epochs, `min_count`, workers, seed, shuffling,
+ pooling, and device;
+- train/validation/test grouping and excluded documents.
+
+Generate a bounded plan first:
+
+```bash
+python skills/geniml/scripts/embedding_plan.py \
+ --mode region2vec \
+ --data work/tokens.parquet \
+ --universe refs/universe.bed \
+ --output-dir work/region2vec \
+ --assembly GRCh38 \
+ --embedding-dim 100 --epochs 10 --workers 4 --seed 42
+```
+
+## Export and inspect artifacts
+
+The 0.8.4 constants are:
+
+- `checkpoint.pt`
+- `config.yaml`
+- `universe.bed`
+
+The config uses `vocab_size` and `embedding_dim`; `embedding_size` is accepted
+only for backward compatibility and is marked for future deprecation.
+
+Important release-source caveat: `model.export(path)` calls
+`export_region2vec_model`, which writes the Torch checkpoint and YAML config
+but does **not** write the tokenizer's universe, despite the API docstring.
+Copy the exact validated universe into the bundle yourself, without changing
+row order, then create a checksum manifest.
-| Parameter | Description | Typical Range |
-|-----------|-------------|---------------|
-| `init_lr` | Initial learning rate | 0.01 - 0.05 |
-| `window_size` | Context window size | 3 - 10 |
-| `num_shufflings` | Number of shuffling iterations | 500 - 2000 |
-| `embedding_dim` | Dimension of output embeddings | 50 - 300 |
-| `context_len` | Context length for training | 30 - 100 |
+```python
+from pathlib import Path
+import shutil
+
+bundle = Path("models/region2vec")
+model.export(str(bundle))
+shutil.copyfile("refs/universe.bed", bundle / "universe.bed")
+```
-## CLI Usage
+Do not overwrite an existing bundle without preserving its prior manifest.
+Inspect without deserialization:
```bash
-geniml region2vec --token-folder /path/to/tokens \
- --save-dir ./region2vec_model \
- --num-shuffle 1000 \
- --embed-dim 100 \
- --context-len 50 \
- --window-size 5 \
- --init-lr 0.025
+python skills/geniml/scripts/model_artifact_inspector.py \
+ --model-dir models/region2vec
+
+python skills/geniml/scripts/tokenizer_compatibility.py \
+ --model-dir models/region2vec \
+ --universe refs/universe.bed \
+ --assembly GRCh38
+```
+
+The checkpoint is a `.pt` file. Geniml's local loader uses
+`torch.load(..., weights_only=True)`, which reduces but does not eliminate
+untrusted-artifact risks such as resource exhaustion, parser defects, or
+native-library vulnerabilities. Never use `torch.load`, Gensim load, pickle,
+or joblib merely to inspect metadata.
+
+## Load only after verification
+
+Local bundle:
+
+```python
+from geniml.region2vec.main import Region2VecExModel
+
+model = Region2VecExModel.from_pretrained("models/region2vec")
+```
+
+Despite its name, this classmethod joins local filenames and makes no Hub
+request. In contrast:
+
+```python
+model = Region2VecExModel(model_path="organization/model")
+```
+
+calls `huggingface_hub.hf_hub_download` for the checkpoint, universe, and
+config. Do not use that form without explicit network approval, a pinned Hub
+revision, an approved cache directory, and expected hashes.
+
+The loader constructs the tokenizer from `universe.bed`, reads `config.yaml`
+with YAML `safe_load`, creates a model of `vocab_size × embedding_dim`, and
+loads checkpoint weights. A checksum match is necessary but not sufficient:
+also compare assembly, special tokens, shape, pooling, and software versions.
+
+## Encode intervals and sets
+
+`Region2VecExModel.encode` accepts a local BED path, a Region, a sequence of
+regions, `geniml.io.RegionSet`, or `gtars.models.RegionSet`.
+
+```python
+vectors = model.encode(
+ "data/query.bed",
+ pooling="mean",
+ batch_size=64,
+)
+```
+
+The method tokenizes each input region, projects its token IDs, and applies
+mean or max pooling. It returns one vector per input region. It does not
+validate assembly or repair malformed intervals. Validate first, and report
+aggregate shapes/statistics rather than raw genomic coordinates.
+
+## Evaluate without leakage
+
+Geniml's `eval` module implements the paper's:
+
+- CTT: cluster tendency;
+- RCT: preservation of training-occurrence information;
+- GDST: relation between genomic and embedding distance;
+- NPT: preservation of genomic neighborhoods.
+
+Source-backed CLI:
+
+```text
+geniml eval ctt --model-path MODEL --embed-type region2vec
+geniml eval gdst --model-path MODEL --embed-type region2vec
+geniml eval npt --model-path MODEL --embed-type region2vec --K 10
+geniml eval rct --model-path MODEL --embed-type region2vec \
+ --bin-path BINARY_EMBEDDINGS
```
-## Best Practices
+`rct` also requires binary embeddings from the same tokenized corpus. The
+official tutorial and `eval bin-gen` write pickle; treat that format as trusted
+local output only and never load a third-party pickle. Hold out independent
+patients/donors before universe selection, hyperparameter tuning, training, and
+metric selection. Report all metrics and baselines rather than selecting a
+single favorable score.
-- **Parameter tuning**: Frequently tune `init_lr`, `window_size`, `num_shufflings`, and `embedding_dim` for optimal performance on your specific dataset
-- **Universe file**: Use a comprehensive universe file that covers all regions of interest in your analysis
-- **Validation**: Always validate tokenization output before proceeding to training
-- **Resources**: Training can be computationally intensive; monitor memory usage with large datasets
+Primary evaluation source: Zheng et al. (2024), *Methods for evaluating
+unsupervised vector representations of genomic regions*,
+doi:[10.1093/nargab/lqae086](https://doi.org/10.1093/nargab/lqae086).
-## Output
+## Official sources
-The trained model saves embeddings that can be used for:
-- Similarity searches across genomic regions
-- Clustering region sets
-- Feature vectors for downstream ML tasks
-- Visualization via dimensionality reduction (t-SNE, UMAP)
+- [PyPI geniml 0.8.4](https://pypi.org/project/geniml/0.8.4/) (released
+ 2026-01-14; accessed 2026-07-23)
+- [v0.8.4 release source](https://github.com/databio/geniml/tree/v0.8.4)
+ (commit `5e8dd14126c45d14917df74de4fb405f383afb61`; accessed 2026-07-23)
+- [Official Region2Vec tutorial](https://docs.bedbase.org/geniml/tutorials/region2vec/)
+ (undated; accessed 2026-07-23; contains legacy imports)
+- [Official evaluation tutorial](https://docs.bedbase.org/geniml/tutorials/evaluation/)
+ (undated; accessed 2026-07-23)
+- [Gtars tokenizer documentation](https://docs.bedbase.org/gtars/tokenizers)
+ (undated; accessed 2026-07-23)
diff --git a/skills/geniml/references/scembed.md b/skills/geniml/references/scembed.md
index 6eee8617..12e86b54 100644
--- a/skills/geniml/references/scembed.md
+++ b/skills/geniml/references/scembed.md
@@ -1,197 +1,307 @@
-# scEmbed: Single-Cell Embedding Generation
+# scEmbed
-## Overview
+Verified against `geniml==0.8.4` release source, current Gtars
+`0.9.2`, and official BEDbase documentation on 2026-07-23.
-scEmbed trains Region2Vec models on single-cell ATAC-seq datasets to generate cell embeddings for clustering and analysis. It provides an unsupervised machine learning framework for representing and analyzing scATAC-seq data in low-dimensional space.
+## Scope and evidence
-## When to Use
+scEmbed learns region embeddings from scATAC-seq accessibility and pools them
+to represent cells. The primary paper reports that pre-trained region
+embeddings can support clustering and transfer to unseen datasets. Do not turn
+that result into a universal accuracy claim; performance depends on assay,
+reference corpus, universe, filtering, cell types, and split design.
-Use scEmbed when working with:
-- Single-cell ATAC-seq (scATAC-seq) data requiring clustering
-- Cell-type annotation tasks
-- Dimensionality reduction for single-cell chromatin accessibility
-- Integration with scanpy workflows for downstream analysis
+Primary source: LeRoy et al. (2024), *Fast clustering and cell-type annotation
+of scATAC data with pre-trained embeddings*,
+doi:[10.1093/nargab/lqae073](https://doi.org/10.1093/nargab/lqae073).
-## Workflow
+## Stable API and known drift
-### Step 1: Data Preparation
+Use:
-Input data must be in AnnData format with `.var` attributes containing `chr`, `start`, and `end` values for peaks.
+```python
+from geniml.scembed.main import ScEmbed
+from geniml.region2vec.utils import Region2VecDataset
+from geniml.tokenization.utils import tokenize_anndata
+from gtars.tokenizers import Tokenizer
+```
+
+Do not use `from geniml.scembed import ScEmbed`: the 0.8.4 package
+`__init__` does not export it. The installed `geniml scembed` command parses
+legacy MatrixMarket options but its 0.8.4 command body does no training or
+encoding.
+
+The source method `ScEmbed.encode(adata)` is public, but 0.8.4's nested token
+handling does not match the current `tokenize_anndata` return shape observed
+with modern Gtars. Require a pinned synthetic smoke test before relying on
+that convenience method. For production, pre-tokenize explicitly, inspect the
+shape, and keep the exact versions locked.
+
+## AnnData contract
+
+The AnnData object must satisfy:
+
+- rows (`obs`) are cells;
+- columns (`var`) are accessible regions/features;
+- `var["chr"]`, `var["start"]`, and `var["end"]` describe each feature;
+- coordinates are validated 0-based half-open BED coordinates;
+- all features use one declared assembly and contig convention;
+- `X` is sparse CSR for bounded tokenization performance;
+- duplicate feature coordinates and duplicate barcodes have an explicit policy.
+
+Confirm matrix orientation. A 10x peak-by-barcode MatrixMarket file is often
+transposed when constructing AnnData; inspect dimensions instead of copying a
+blind `.T`.
+
+Do not expose barcodes, patient IDs, phenotypes, rare cell labels, or raw
+intervals in logs. An `.h5ad` may contain identifying metadata in `obs`,
+`uns`, embeddings, and file provenance. Output only bounded aggregate counts
+unless the user explicitly approves disclosure.
+
+## Leakage-safe split order
+
+Split before fitting or selecting anything:
+
+1. Group cells by patient/donor and biological replicate.
+2. Assign complete groups to train/validation/test.
+3. Fit QC thresholds, feature/universe selection, token vocabulary, model,
+ annotation references, and hyperparameters on training data only.
+4. Apply the frozen universe/tokenizer/model to validation and test.
+5. Keep technical replicates and multiple samples from one patient together.
+
+Randomly splitting cells from the same donor leaks donor- and batch-specific
+accessibility. Building a consensus universe from all patients can also leak
+test-set feature prevalence even when labels are hidden.
+
+Audit a local manifest without printing metadata values:
+
+```bash
+python skills/geniml/scripts/corpus_auditor.py \
+ --manifest data/cells.tsv \
+ --group-column patient_id \
+ --split-column split \
+ --assembly-column assembly
+```
+
+## Build and validate the tokenizer
-**Starting from raw data** (barcodes.txt, peaks.bed, matrix.mtx):
+Use a local, checksummed universe from the training partition:
```python
-import scanpy as sc
-import pandas as pd
-import scipy.io
-import anndata
-
-# Load data
-barcodes = pd.read_csv('barcodes.txt', header=None, names=['barcode'])
-peaks = pd.read_csv('peaks.bed', sep='\t', header=None,
- names=['chr', 'start', 'end'])
-matrix = scipy.io.mmread('matrix.mtx').tocsr()
-
-# Create AnnData
-adata = anndata.AnnData(X=matrix.T, obs=barcodes, var=peaks)
-adata.write('scatac_data.h5ad')
+from gtars.tokenizers import Tokenizer
+
+tokenizer = Tokenizer.from_bed("refs/training_universe.bed")
```
-### Step 2: Pre-tokenization
+Record:
+
+- source cohort and split;
+- assembly, chromosome sizes, coordinate/contig/strand policy;
+- universe SHA-256, row order, and row count;
+- Gtars version and special-token map/IDs;
+- `len(tokenizer)`.
-Convert genomic regions into tokens using gtars utilities. This creates a parquet file with tokenized cells for faster training:
+Do not use `Tokenizer.from_pretrained("organization/model")` unless the user
+approves a network download and supplies a pinned revision and expected
+hashes. A model and tokenizer are compatible only when the exact universe,
+special-token IDs, and model vocabulary size agree.
+
+## Pre-tokenize to one bounded Parquet file
```python
-from geniml.io import tokenize_cells
+import pyarrow as pa
+import pyarrow.parquet as pq
+import scanpy as sc
+from geniml.tokenization.utils import tokenize_anndata
-tokenize_cells(
- adata='scatac_data.h5ad',
- universe_file='universe.bed',
- output='tokenized_cells.parquet'
-)
+adata = sc.read_h5ad("data/train.h5ad")
+adata.X = adata.X.tocsr()
+
+encoded_cells = tokenize_anndata(adata, tokenizer)
+cells = [encoded["input_ids"] for encoded in encoded_cells]
+
+table = pa.table({
+ "tokens": pa.array(cells, type=pa.list_(pa.int32()))
+})
+pq.write_table(table, "work/train_tokens.parquet")
```
-**Benefits of pre-tokenization:**
-- Faster training iterations
-- Reduced memory requirements
-- Reusable tokenized data for multiple training runs
+Before writing:
-### Step 3: Model Training
+- verify `len(cells) == adata.n_obs`;
+- check each token list is bounded and contains IDs in
+ `[0, len(tokenizer))`;
+- quantify empty cells and out-of-vocabulary/unmatched features;
+- preserve row correspondence in a separate protected manifest;
+- do not include barcodes or labels in the training Parquet unless required.
-Train the scEmbed model using tokenized data:
+The upstream issue
+[`databio/geniml#14`](https://github.com/databio/geniml/issues/14)
+(opened 2025-09-05) proposes moving away from one `.gtok` file per cell.
+Prefer the single Parquet corpus for current work; treat `.gtok` as legacy.
-```python
-from geniml.scembed import ScEmbed
-from geniml.region2vec import Region2VecDataset
+## Train
-# Load tokenized dataset
-dataset = Region2VecDataset('tokenized_cells.parquet')
+```python
+from geniml.region2vec.utils import Region2VecDataset
+from geniml.scembed.main import ScEmbed
-# Initialize and train model
+dataset = Region2VecDataset(
+ "work/train_tokens.parquet",
+ shuffle=True,
+)
model = ScEmbed(
+ tokenizer=tokenizer,
embedding_dim=100,
- window_size=5,
- negative_samples=5
+ pooling_method="mean",
+ device="cpu",
)
-
model.train(
- dataset=dataset,
- epochs=100,
- batch_size=256,
- learning_rate=0.025
+ dataset,
+ window_size=5,
+ epochs=10,
+ min_count=10,
+ num_cpus=4,
+ seed=42,
)
-
-# Save model
-model.save('scembed_model/')
```
-### Step 4: Generate Cell Embeddings
+Bound cells, nonzeros, tokens per cell, workers, epochs, checkpoint frequency,
+RAM, and disk. `Region2VecDataset` loads the full Parquet token column into
+memory. Training uses Gensim and Torch; Gensim checkpoint loading is unsafe for
+untrusted `.model` files.
+
+Generate a run plan first:
+
+```bash
+python skills/geniml/scripts/embedding_plan.py \
+ --mode scembed \
+ --data work/train_tokens.parquet \
+ --universe refs/training_universe.bed \
+ --output-dir work/scembed \
+ --assembly GRCh38 \
+ --embedding-dim 100 --epochs 10 --workers 4 --seed 42
+```
-Use the trained model to generate embeddings for cells:
+## Export and local loading
```python
-from geniml.scembed import ScEmbed
-
-# Load trained model
-model = ScEmbed.from_pretrained('scembed_model/')
-
-# Generate embeddings for AnnData object
-embeddings = model.encode(adata)
-
-# Add to AnnData for downstream analysis
-adata.obsm['scembed_X'] = embeddings
+from pathlib import Path
+import shutil
+
+bundle = Path("models/scembed")
+model.export(str(bundle))
+shutil.copyfile(
+ "refs/training_universe.bed",
+ bundle / "universe.bed",
+)
```
-### Step 5: Downstream Analysis
+As in Region2Vec, the 0.8.4 export utility writes `checkpoint.pt` and
+`config.yaml` but does not write the tokenizer universe. Add the exact
+validated `universe.bed` yourself and generate checksums.
-Integrate with scanpy for clustering and visualization:
+Inspect before loading:
-```python
-import scanpy as sc
+```bash
+python skills/geniml/scripts/model_artifact_inspector.py \
+ --model-dir models/scembed
-# Use scEmbed embeddings for neighborhood graph
-sc.pp.neighbors(adata, use_rep='scembed_X')
+python skills/geniml/scripts/tokenizer_compatibility.py \
+ --model-dir models/scembed \
+ --universe refs/training_universe.bed \
+ --assembly GRCh38
+```
-# Cluster cells
-sc.tl.leiden(adata, resolution=0.5)
+Then, for a trusted local bundle:
-# Compute UMAP for visualization
-sc.tl.umap(adata)
+```python
+from geniml.scembed.main import ScEmbed
-# Plot results
-sc.pl.umap(adata, color='leiden')
+model = ScEmbed.from_pretrained("models/scembed")
```
-## Key Parameters
+This classmethod is local. In contrast,
+`ScEmbed(model_path="organization/model")` downloads three files through
+Hugging Face Hub. Never trigger that constructor implicitly. Pin a revision,
+cache path, expected size, and checksums when a download is explicitly
+approved.
-### Training Parameters
+`checkpoint.pt` is loaded with Torch `weights_only=True`. Continue to treat it
+as untrusted until verified and load in an isolated, resource-bounded
+environment. Never inspect it using pickle.
-| Parameter | Description | Typical Range |
-|-----------|-------------|---------------|
-| `embedding_dim` | Dimension of cell embeddings | 50 - 200 |
-| `window_size` | Context window for training | 3 - 10 |
-| `negative_samples` | Number of negative samples | 5 - 20 |
-| `epochs` | Training epochs | 50 - 200 |
-| `batch_size` | Training batch size | 128 - 512 |
-| `learning_rate` | Initial learning rate | 0.01 - 0.05 |
+## Generate and attach cell embeddings
-### Tokenization Parameters
+After a pinned synthetic smoke test confirms the installed convenience API:
-- **Universe file**: Reference BED file defining the genomic vocabulary
-- **Overlap threshold**: Minimum overlap for peak-universe matching (typically 1e-9)
+```python
+embeddings = model.encode(adata, pooling="mean")
+assert embeddings.shape[0] == adata.n_obs
+adata.obsm["X_scembed"] = embeddings
+```
-## Pre-trained Models
+If the smoke fails, do not patch around token nesting silently. Pin a known
+compatible Geniml/Gtars pair or implement an explicit, tested projection using
+the verified token IDs and model contract. Never substitute a different
+universe to make shapes fit.
-Pre-trained scEmbed models are available on Hugging Face for common reference datasets. Load them using:
+For Scanpy downstream analysis:
```python
-from geniml.scembed import ScEmbed
-
-# Load pre-trained model
-model = ScEmbed.from_pretrained('databio/scembed-pbmc-10k')
+import scanpy as sc
-# Generate embeddings
-embeddings = model.encode(adata)
+sc.pp.neighbors(adata, use_rep="X_scembed")
+sc.tl.leiden(adata, resolution=0.5, random_state=42)
+sc.tl.umap(adata, random_state=42)
```
-## Best Practices
+UMAP and Leiden are exploratory unless validated on held-out donors. Store
+software versions, seeds, neighborhood parameters, and the embedding checksum.
-- **Data quality**: Use filtered peak-barcode matrices, not raw counts
-- **Pre-tokenization**: Always pre-tokenize to improve training efficiency
-- **Parameter tuning**: Adjust `embedding_dim` and training epochs based on dataset size
-- **Validation**: Use known cell-type markers to validate clustering quality
-- **Integration**: Combine with scanpy for comprehensive single-cell analysis
-- **Model sharing**: Export trained models to Hugging Face for reproducibility
+## Cell-type annotation
-## Example Dataset
+The release contains `geniml.scembed.annotation.Annotator`, which queries a
+Qdrant collection and can use local or remote endpoints. That is a separate
+network/data-disclosure decision: embeddings and metadata can be sensitive.
+Do not create or contact an annotation server without explicit approval.
-The 10x Genomics PBMC 10k dataset (10,000 peripheral blood mononuclear cells) serves as a standard benchmark:
-- Contains diverse immune cell types
-- Well-characterized cell populations
-- Available from 10x Genomics website
+For any KNN annotation:
-## Cell-Type Annotation
+- reference and query embeddings must use the same model/tokenizer/universe;
+- fit the reference index using training donors only;
+- tune `k` and score thresholds on validation donors;
+- include unknown/reject behavior;
+- report per-class metrics and calibration on held-out donors;
+- avoid claiming labels for absent reference cell types.
-After clustering, annotate cell types using k-nearest neighbors (KNN) with reference datasets:
+Never send raw barcodes, patient metadata, or interval lists to a hosted vector
+store by default.
-```python
-from geniml.scembed import annotate_celltypes
-
-# Annotate using reference
-annotations = annotate_celltypes(
- query_adata=adata,
- reference_adata=reference,
- embedding_key='scembed_X',
- k=10
-)
+## Evaluation
-adata.obs['cell_type'] = annotations
-```
+Report:
+
+- donor-grouped clustering metrics with confidence intervals;
+- annotation macro/micro F1 and per-class support;
+- unknown/reject rate;
+- batch/donor association;
+- runtime and peak memory;
+- baselines fitted on the same training split.
+
+Do not select clusters, labels, or universe parameters by inspecting the test
+UMAP. If pre-trained public models were trained on overlapping donors or
+datasets, document that possible leakage.
-## Output
+## Official sources
-scEmbed produces:
-- Low-dimensional cell embeddings (stored in `adata.obsm`)
-- Trained model files for reuse
-- Compatible format for scanpy downstream analysis
-- Optional export to Hugging Face for sharing
+- [scEmbed training tutorial](https://docs.bedbase.org/geniml/tutorials/train-scembed-model)
+ (undated; accessed 2026-07-23)
+- [scEmbed API page](https://docs.bedbase.org/geniml/api-reference/scembed/)
+ (undated; accessed 2026-07-23)
+- [Geniml v0.8.4 source](https://github.com/databio/geniml/tree/v0.8.4/geniml/scembed)
+ (released 2026-01-14; accessed 2026-07-23)
+- [Gtars tokenizer documentation](https://docs.bedbase.org/gtars/tokenizers)
+ (undated; accessed 2026-07-23)
+- [Primary scEmbed paper](https://doi.org/10.1093/nargab/lqae073)
+ (2024)
diff --git a/skills/geniml/references/utilities.md b/skills/geniml/references/utilities.md
index f4285164..e011aa64 100644
--- a/skills/geniml/references/utilities.md
+++ b/skills/geniml/references/utilities.md
@@ -1,385 +1,385 @@
-# Geniml Utilities and Additional Tools
+# I/O, tokenization, caches, evaluation, and security
-## BBClient: BED File Caching
+Research snapshot: 2026-07-23. API claims below use official documentation,
+PyPI metadata, and the `geniml` v0.8.4 release source. Where current docs and
+release code disagree, the discrepancy is stated explicitly.
-### Overview
+## Release and dependency facts
-BBClient provides efficient caching of BED files from remote sources, enabling faster repeated access and integration with R workflows.
+`geniml==0.8.4` was released on 2026-01-14. PyPI metadata:
-### When to Use
+- does not set `Requires-Python`;
+- classifies Python 3.10, 3.11, 3.12, 3.13, and 3.14;
+- provides `ml` and `test` extras;
+- ships a pure-Python wheel, but dependencies include native packages;
+- uses Trusted Publishing with a Sigstore provenance link to tag `v0.8.4`.
-Use BBClient when:
-- Repeatedly accessing BED files from remote databases
-- Working with BEDbase repositories
-- Integrating genomic data with R pipelines
-- Need local caching for performance
+Release files:
-### Python Usage
+```text
+geniml-0.8.4-py3-none-any.whl
+sha256 ac0fc520cde6f6461120aee6b40d3cbf20e1e3d8bdb95a3a45345724eee545b5
-```python
-from geniml.bbclient import BBClient
-
-# Initialize client
-client = BBClient(cache_folder='~/.bedcache')
-
-# Fetch and cache BED file
-bed_file = client.load_bed(bed_id='GSM123456')
-
-# Access cached file
-regions = client.get_regions('GSM123456')
-```
-
-### R Integration
-
-```r
-library(reticulate)
-geniml <- import("geniml.bbclient")
-
-# Initialize client
-client <- geniml$BBClient(cache_folder='~/.bedcache')
-
-# Load BED file
-bed_file <- client$load_bed(bed_id='GSM123456')
+geniml-0.8.4.tar.gz
+sha256 6f429c7c89d06a4c2c378e349d14b3a2d984dc441440ada38473772fce6addb1
```
-### Best Practices
-
-- Configure cache directory with sufficient storage
-- Use consistent cache locations across analyses
-- Clear cache periodically to remove unused files
+The stable release requires `gtars>=0.2.5` without an upper bound. The
+2026-07-23 verified base smoke resolved `gtars==0.9.2` and successfully
+imported Geniml 0.8.4, `geniml.io.RegionSet`, and
+`gtars.tokenizers.Tokenizer`. Gtars 0.9.2 was released 2026-06-17 and requires
+Python >=3.10.
----
+Pin both direct packages and retain an `uv.lock`; upstream transitive
+requirements are mostly lower bounds.
-## BEDshift: BED File Randomization
+## BED coordinates and validation
-### Overview
+BED is normally 0-based, half-open `[start, end)`. A valid BED3 row requires:
-BEDshift provides tools for randomizing BED files while preserving genomic context, essential for generating null distributions and statistical testing.
+- nonempty contig;
+- integer `start >= 0`;
+- integer `end > start`;
+- `end` no greater than the declared contig length;
+- values within bounded integer limits.
-### When to Use
+Do not confuse BED with 1-based closed VCF/GFF coordinates. BED columns beyond
+BED3 have their own constraints; preserve them rather than guessing.
+BED3 carries no strand. When BED6 strand exists, preserve `+`, `-`, or `.`
+unless a documented assay conversion says otherwise.
-Use BEDshift when:
-- Creating null models for statistical testing
-- Generating control datasets
-- Assessing significance of genomic overlaps
-- Benchmarking analysis methods
+Assembly compatibility requires more than matching `chr` prefixes. BEDbase
+describes chromosome-name sensitivity (XS), out-of-bounds regions (OOBR), and
+sequence fit (SF) as separate criteria. Record a concrete assembly/accession
+and chromosome-sizes digest. Reject unknown alt/decoy contigs rather than
+dropping them silently.
-### Usage
-
-```python
-from geniml.bedshift import bedshift
-
-# Randomize BED file preserving chromosome distribution
-randomized = bedshift(
- input_bed='peaks.bed',
- genome='hg38',
- preserve_chrom=True,
- n_iterations=100
-)
-```
-
-### CLI Usage
+Run:
```bash
-geniml bedshift \
- --input peaks.bed \
- --genome hg38 \
- --preserve-chrom \
- --iterations 100 \
- --output randomized_peaks.bed
+python skills/geniml/scripts/bed_validator.py \
+ --input data/peaks.bed \
+ --assembly GRCh38 \
+ --chrom-sizes refs/GRCh38.chrom.sizes
```
-### Randomization Strategies
+The script emits aggregate errors and a normalization plan. It never rewrites
+coordinates or performs liftover.
-**Preserve chromosome distribution:**
-```python
-bedshift(input_bed, genome, preserve_chrom=True)
-```
-Maintains regions on same chromosomes as original.
+## Region and RegionSet APIs
-**Preserve distance distribution:**
-```python
-bedshift(input_bed, genome, preserve_distance=True)
-```
-Maintains inter-region distances.
+### Gtars API for new workflows
-**Preserve region sizes:**
```python
-bedshift(input_bed, genome, preserve_size=True)
-```
-Keeps original region lengths.
-
-### Best Practices
-
-- Choose randomization strategy matching null hypothesis
-- Generate multiple iterations for robust statistics
-- Validate randomized output maintains desired properties
-- Document randomization parameters for reproducibility
-
----
-
-## Evaluation: Model Assessment Tools
-
-### Overview
-
-Geniml provides evaluation utilities for assessing embedding quality and model performance.
+from gtars.models import Region, RegionSet
-### When to Use
-
-Use evaluation tools when:
-- Validating trained embeddings
-- Comparing different models
-- Assessing clustering quality
-- Publishing model results
-
-### Embedding Evaluation
-
-```python
-from geniml.evaluation import evaluate_embeddings
-
-# Evaluate Region2Vec embeddings
-metrics = evaluate_embeddings(
- embeddings_file='region2vec_model/embeddings.npy',
- labels_file='metadata.csv',
- metrics=['silhouette', 'davies_bouldin', 'calinski_harabasz']
-)
-
-print(f"Silhouette score: {metrics['silhouette']:.3f}")
-print(f"Davies-Bouldin index: {metrics['davies_bouldin']:.3f}")
+region = Region("chr1", 100, 200, None)
+regions = RegionSet("data/peaks.bed")
+print(len(regions))
+regions.to_bed("work/peaks.copy.bed")
```
-### Clustering Metrics
-
-**Silhouette score:** Measures cluster cohesion and separation (-1 to 1, higher better)
+Gtars 0.9.2 exposes operations including `sort`, `reduce`, `coverage`,
+`count_overlaps`, `jaccard`, `nearest_neighbors`, `to_bed`, `to_bed_gz`, and
+`to_bigbed`. Do not assume these operations validate assembly or coordinate
+semantics. Validate before constructing the object and after writing output.
-**Davies-Bouldin index:** Average similarity between clusters (≥0, lower better)
+Official Gtars constructors can accept URLs. This skill restricts them to
+validated local regular files by default.
-**Calinski-Harabasz score:** Ratio of between/within cluster dispersion (higher better)
-
-### scEmbed Cell-Type Annotation Evaluation
+### Legacy Geniml I/O
```python
-from geniml.evaluation import evaluate_annotation
-
-# Evaluate cell-type predictions
-results = evaluate_annotation(
- predicted=adata.obs['predicted_celltype'],
- true=adata.obs['true_celltype'],
- metrics=['accuracy', 'f1', 'confusion_matrix']
-)
+from geniml.io import BedSet, Region, RegionSet
-print(f"Accuracy: {results['accuracy']:.1%}")
-print(f"F1 score: {results['f1']:.3f}")
+region = Region("chr1", 100, 200) # third field is named stop
+regions = RegionSet("data/peaks.bed", backed=False)
```
-### Best Practices
-
-- Use multiple complementary metrics
-- Compare against baseline models
-- Report metrics on held-out test data
-- Visualize embeddings (UMAP/t-SNE) alongside metrics
-
----
-
-## Tokenization: Region Tokenization Utilities
-
-### Overview
-
-Tokenization converts genomic regions into discrete tokens using a reference universe, enabling word2vec-style training.
-
-### When to Use
+`geniml.io.RegionSet(regions, backed=False)` accepts a path/URL or a list of
+legacy Regions. With `backed=True`, it streams the BED, supports iteration and
+length, and does not support indexing. Other release classes include `BedSet`,
+`Maf`, `SNP`, `TokenizedRegionSet`, and `RegionSetCollection`, though only
+`Region`, `RegionSet`, and `BedSet` are exported from `geniml.io`.
-Tokenization is a required preprocessing step for:
-- Region2Vec training
-- scEmbed model training
-- Any embedding method requiring discrete tokens
+The 0.7.0 changelog says RegionSet use switched toward Gtars. Avoid mixing
+legacy and Gtars Region objects accidentally: field names and accepted types
+differ.
-### Hard Tokenization
+## Tokenization
-Strict overlap-based tokenization:
+### Current Gtars tokenizer
```python
-from geniml.tokenization import hard_tokenization
+from gtars.models import RegionSet
+from gtars.tokenizers import Tokenizer
-hard_tokenization(
- src_folder='bed_files/',
- dst_folder='tokenized/',
- universe_file='universe.bed',
- p_value_threshold=1e-9
-)
+tokenizer = Tokenizer.from_bed("refs/universe.bed")
+encoded = tokenizer(RegionSet("data/peaks.bed"))
+input_ids = encoded["input_ids"]
```
-**Parameters:**
-- `p_value_threshold`: Significance level for overlap (typically 1e-9 or 1e-6)
+Gtars also documents `Tokenizer.from_config` and
+`Tokenizer.from_pretrained`. The latter can access Hugging Face; do not call it
+without explicit network approval, revision pinning, cache bounds, and
+expected hashes.
-### Soft Tokenization
+With Gtars 0.9.2, a two-region universe produced `len(tokenizer) == 9` because
+seven special tokens are included. Preserve:
-Probabilistic tokenization allowing partial matches:
+- universe bytes, row order, and assembly;
+- Gtars version;
+- `special_tokens_map` and every special-token ID;
+- tokenizer length;
+- token-corpus schema and checksum.
-```python
-from geniml.tokenization import soft_tokenization
+Run the local compatibility planner:
-soft_tokenization(
- src_folder='bed_files/',
- dst_folder='tokenized/',
- universe_file='universe.bed',
- overlap_threshold=0.5
-)
+```bash
+python skills/geniml/scripts/tokenizer_compatibility.py \
+ --model-dir models/region2vec \
+ --universe refs/universe.bed \
+ --assembly GRCh38
```
-**Parameters:**
-- `overlap_threshold`: Minimum overlap fraction (0-1)
-
-### Universe-Based Tokenization
+### Legacy hard tokenization
-Map regions to universe tokens with custom parameters:
+The stable file `geniml.tokenization.main` defines:
```python
-from geniml.tokenization import universe_tokenization
-
-universe_tokenization(
- bed_file='peaks.bed',
- universe_file='universe.bed',
- output_file='tokens.txt',
- method='hard',
- threshold=1e-9
+hard_tokenization_main(
+ src_folder,
+ dst_folder,
+ universe_file,
+ fraction=1e-9,
+ file_list=None,
+ num_workers=10,
+ bedtools_path="bedtools",
)
```
-### Best Practices
+It invokes an external Bedtools executable and multiprocessing. The
+`fraction` is passed to interval intersection logic; it is **not a statistical
+p-value**. The package `geniml.tokenization.__init__` comments out its public
+exports, so `from geniml.tokenization import hard_tokenization` and the
+installed `geniml tokenize` dispatcher are unreliable in 0.8.4.
-- **Universe quality**: Use comprehensive, well-constructed universes
-- **Threshold selection**: More stringent (lower p-value) for higher confidence
-- **Validation**: Check tokenization coverage (what % of regions tokenized)
-- **Consistency**: Use same universe and parameters across related analyses
+Do not use an arbitrary `bedtools` found on `PATH`. If reproducing the legacy
+path, pin and checksum the binary, pass an explicit absolute path, validate
+argv, bound workers, use a fresh output directory, and compare synthetic
+results with the Gtars tokenizer.
-### Tokenization Coverage
+The current preferred token corpus is a bounded Parquet file with one
+list-valued `tokens` column. Upstream issue #14 proposes deprecating one
+`.gtok` file per cell.
-Check how well regions tokenize:
+## BBClient and cache behavior
```python
-from geniml.tokenization import check_coverage
-
-coverage = check_coverage(
- bed_file='peaks.bed',
- universe_file='universe.bed',
- threshold=1e-9
-)
+from geniml.bbclient import BBClient
-print(f"Tokenization coverage: {coverage:.1%}")
+client = BBClient(cache_folder="/absolute/project/.bbcache")
```
-Aim for >80% coverage for reliable training.
+Release defaults:
----
+- endpoint: `BEDBASE_API` or `https://api.bedbase.org`;
+- cache root: `BBCLIENT_CACHE` or `~/.bbcache`;
+- BEDs under `bedfiles/`;
+- BED sets under `bedsets/`;
+- token cache in `tokens.zarr`.
-## Text2BedNN: Search Backend
+`load_bed`, `load_bedset`, `add_bed_tokens_to_cache`, and corresponding
+`cache-*` CLI commands may use the network. `add_bed_to_s3` and
+`get_bed_from_s3` accept cloud credentials and endpoints; do not use them
+without an explicit upload/download request and a separate credential review.
+Never read or print the environment broadly. Only consult `BEDBASE_API` and
+`BBCLIENT_CACHE` when the user approves those overrides.
-### Overview
+Local-oriented CLI:
-Text2BedNN creates neural network-based search backends for querying genomic regions using natural language or metadata.
+```text
+geniml bbclient seek ID --cache-folder CACHE
+geniml bbclient inspect-bedfiles --cache-folder CACHE
+geniml bbclient inspect-bedsets --cache-folder CACHE
+geniml bbclient rm ID --cache-folder CACHE
+```
-### When to Use
+`rm` mutates cache state; confirm the exact resolved target first. Inspection
+can still expose BED IDs and local paths, so redact output in shared logs.
-Use Text2BedNN when:
-- Building search interfaces for genomic databases
-- Enabling natural language queries over BED files
-- Creating metadata-aware search systems
-- Deploying interactive genomic search applications
+Before an approved download:
-### Workflow
+1. approve endpoint and exact IDs;
+2. set a project-scoped cache and byte quota;
+3. reject redirects to unapproved hosts where tooling permits;
+4. verify compressed and expanded size;
+5. checksum downloaded bytes;
+6. validate BED and assembly;
+7. record provenance and retrieval time.
-**Step 1: Prepare embeddings**
+No bundled skill script performs BBClient or Hub downloads.
-Train BEDspace or Region2Vec model with metadata.
+## Model loading and artifact safety
-**Step 2: Build search index**
+Modern local classes:
```python
-from geniml.search import build_search_index
+from geniml.region2vec.main import Region2VecExModel
+from geniml.scembed.main import ScEmbed
-build_search_index(
- embeddings_file='bedspace_model/embeddings.npy',
- metadata_file='metadata.csv',
- output_dir='search_backend/'
-)
+r2v = Region2VecExModel.from_pretrained("models/region2vec")
+sce = ScEmbed.from_pretrained("models/scembed")
```
-**Step 3: Query the index**
+These classmethods use local bundle paths. Constructors with
+`model_path="organization/model"` call Hugging Face Hub and download
+`checkpoint.pt`, `universe.bed`, and `config.yaml`.
-```python
-from geniml.search import SearchBackend
+Geniml's loader uses `torch.load(..., weights_only=True)` and YAML
+`safe_load`. This is safer than unrestricted pickle, but artifacts can still
+cause resource exhaustion or exploit parser/native-library vulnerabilities.
+Gensim `.model`, pickle, joblib, TorchScript, shared libraries, and external
+binaries require even stronger distrust.
-backend = SearchBackend.load('search_backend/')
+Inspect without importing Torch or deserializing:
-# Natural language query
-results = backend.query(
- text="T cell regulatory regions",
- top_k=10
-)
-
-# Metadata query
-results = backend.query(
- metadata={'cell_type': 'T_cell', 'tissue': 'blood'},
- top_k=10
-)
+```bash
+python skills/geniml/scripts/model_artifact_inspector.py \
+ --model-dir models/region2vec \
+ --verify-manifest models/region2vec/SHA256SUMS
```
-### Best Practices
-
-- Train embeddings with rich metadata for better search
-- Index large collections for comprehensive coverage
-- Validate search relevance on known queries
-- Deploy with API for interactive applications
+The inspector reads bounded metadata formats, hashes bytes, flags risky
+extensions, rejects URLs/symlinks/traversal, and never loads model objects.
----
+## Embedding evaluation
-## Additional Tools
+The `geniml eval` release CLI has:
-### I/O Utilities
-
-```python
-from geniml.io import read_bed, write_bed, load_universe
-
-# Read BED file
-regions = read_bed('peaks.bed')
-
-# Write BED file
-write_bed(regions, 'output.bed')
-
-# Load universe
-universe = load_universe('universe.bed')
+```text
+gdst Genome distance scaling test
+npt Neighborhood preserving test
+ctt Cluster tendency test
+rct Reconstruction test
+bin-gen Generate binary embeddings
```
-### Model Utilities
-
-```python
-from geniml.models import save_model, load_model
-
-# Save trained model
-save_model(model, 'my_model/')
+Examples:
-# Load model
-model = load_model('my_model/')
+```bash
+geniml eval gdst \
+ --model-path /absolute/project/model \
+ --embed-type region2vec \
+ --num-samples 10000 \
+ --seed 42
+
+geniml eval npt \
+ --model-path /absolute/project/model \
+ --embed-type region2vec \
+ --K 10 \
+ --num-samples 1000 \
+ --seed 42 \
+ --num-workers 4
```
-### Common Patterns
+The official tutorial's `BaseEmbeddings` and `bin-gen` workflows use pickle
+for binary embedding objects. Generate them locally and never load an
+untrusted pickle. Bound samples/workers and record random seeds. Evaluate on
+patients/donors excluded from universe selection and training.
-**Pipeline workflow:**
-```python
-# 1. Build universe
-universe = build_universe(coverage_folder='coverage/', method='cc', cutoff=5)
+Primary source: Zheng et al. (2024), *Methods for evaluating unsupervised
+vector representations of genomic regions*,
+doi:[10.1093/nargab/lqae086](https://doi.org/10.1093/nargab/lqae086).
-# 2. Tokenize
-hard_tokenization(src_folder='beds/', dst_folder='tokens/',
- universe_file='universe.bed', p_value_threshold=1e-9)
+## BEDshift
-# 3. Train embeddings
-region2vec(token_folder='tokens/', save_dir='model/', num_shufflings=1000)
+The integrated Bedshift CLI is:
-# 4. Evaluate
-metrics = evaluate_embeddings(embeddings_file='model/embeddings.npy',
- labels_file='metadata.csv')
+```text
+geniml bedshift --help
```
-This modular design allows flexible composition of geniml tools for diverse genomic ML workflows.
+It can use a local chromosome-length file or resolve a Refgenie genome through
+`refgenconf`. Prefer an explicit, checksummed local chromosome-sizes file to
+avoid implicit lookup/network behavior. Set a seed, output bound, and
+perturbation policy matching the null hypothesis. Validate randomized
+coordinates exactly like originals.
+
+Primary source: Gu et al. (2021), *Bedshift: perturbation of genomic interval
+sets*, doi:[10.1186/s13059-021-02440-w](https://doi.org/10.1186/s13059-021-02440-w).
+
+## Privacy and bounded reporting
+
+Potentially sensitive:
+
+- BED coordinates and filenames;
+- sample, donor, patient, treatment, diagnosis, and tissue metadata;
+- cell barcodes and rare labels;
+- embeddings that permit membership or nearest-neighbor inference;
+- local cache paths and BEDbase identifiers.
+
+Default reports should contain only aggregate counts, schema names, checksums,
+software versions, and redacted file IDs. Cap error examples; never dump
+entire malformed rows or metadata values. Store full provenance manifests in
+the protected project, not chat output.
+
+## Migration and deprecation ledger
+
+- 0.4.0: tokenizers renamed to `TreeTokenizer` and `AnnDataTokenizer`.
+- 0.7.0: RegionSet use moved toward Gtars and encoding changed.
+- 0.8.0: Atacformer added.
+- 0.8.1: BEDspace fixed according to the changelog.
+- 0.8.4: latest stable; release notes contain only the version bump.
+- Current Gtars API: use the unified `gtars.tokenizers.Tokenizer`.
+- `.gtok`: proposed for deprecation in upstream issue #14; prefer Parquet.
+- `embedding_size`: backward-compatible config key; use `embedding_dim`.
+- Top-level Region2Vec/scEmbed/tokenization exports shown by old docs are
+ absent/commented in the 0.8.4 wheel.
+- Official `geniml assess` examples are stale; use
+ `geniml assess-universe`.
+
+## Dated authoritative sources
+
+Package and source:
+
+- [PyPI: geniml 0.8.4](https://pypi.org/project/geniml/0.8.4/) — released
+ 2026-01-14; accessed 2026-07-23.
+- [PyPI JSON: geniml 0.8.4](https://pypi.org/pypi/geniml/0.8.4/json) —
+ artifact hashes, dependencies, extras, and null `Requires-Python`; accessed
+ 2026-07-23.
+- [GitHub release v0.8.4](https://github.com/databio/geniml/releases/tag/v0.8.4)
+ — commit `5e8dd14126c45d14917df74de4fb405f383afb61`; released
+ 2026-01-14; accessed 2026-07-23.
+- [Geniml changelog](https://docs.bedbase.org/geniml/changelog/) — through
+ 0.8.1 on the rendered page; accessed 2026-07-23.
+- [PyPI: gtars 0.9.2](https://pypi.org/project/gtars/0.9.2/) — released
+ 2026-06-17; accessed 2026-07-23.
+
+Official API/ecosystem documentation:
+
+- [Geniml documentation](https://docs.bedbase.org/geniml/) — accessed
+ 2026-07-23.
+- [Geniml I/O API](https://docs.bedbase.org/geniml/api-reference/io/) —
+ accessed 2026-07-23.
+- [Gtars RegionSet](https://docs.bedbase.org/gtars/regionSet) — accessed
+ 2026-07-23.
+- [Gtars tokenizers](https://docs.bedbase.org/gtars/tokenizers) — accessed
+ 2026-07-23.
+- [BEDbase reference-genome compatibility](https://docs.bedbase.org/bedbase/user/reference-genome-compatibility/)
+ — accessed 2026-07-23.
+- [BEDbase BBClient and caching](https://docs.bedbase.org/bedbase/user/bbclient/)
+ — accessed 2026-07-23.
+- [Geniml evaluation tutorial](https://docs.bedbase.org/geniml/tutorials/evaluation/)
+ — accessed 2026-07-23.
+- [Official citation map](https://docs.bedbase.org/citations) — accessed
+ 2026-07-23.
+
+Primary papers:
+
+- [Gharavi et al. 2021, Region2Vec](https://doi.org/10.1093/bioinformatics/btab439)
+- [Gu et al. 2021, BEDshift](https://doi.org/10.1186/s13059-021-02440-w)
+- [Gharavi et al. 2024, BEDspace](https://doi.org/10.3390/bioengineering11030263)
+- [LeRoy et al. 2024, scEmbed](https://doi.org/10.1093/nargab/lqae073)
+- [Rymuza et al. 2024, consensus universes](https://doi.org/10.1093/nar/gkae685)
+- [Zheng et al. 2024, embedding evaluation](https://doi.org/10.1093/nargab/lqae086)
diff --git a/skills/geniml/scripts/__init__.py b/skills/geniml/scripts/__init__.py
new file mode 100644
index 00000000..f88ca89d
--- /dev/null
+++ b/skills/geniml/scripts/__init__.py
@@ -0,0 +1 @@
+"""Dependency-free local helpers for the Geniml skill."""
diff --git a/skills/geniml/scripts/_common.py b/skills/geniml/scripts/_common.py
new file mode 100644
index 00000000..d790ce76
--- /dev/null
+++ b/skills/geniml/scripts/_common.py
@@ -0,0 +1,399 @@
+#!/usr/bin/env python3
+"""Shared, dependency-free safety helpers for local Geniml skill CLIs."""
+
+from __future__ import annotations
+
+import csv
+import gzip
+import hashlib
+import io
+import json
+import os
+import re
+import stat
+import sys
+from pathlib import Path
+from typing import Any, Iterator
+
+
+HARD_MAX_FILES = 100_000
+HARD_MAX_BYTES = 8 * 1024**3
+HARD_MAX_RECORDS = 10_000_000
+HARD_MAX_LINE_BYTES = 1024 * 1024
+HARD_MAX_WORKERS = 256
+HARD_MAX_EPOCHS = 100_000
+MAX_COORDINATE = 2**63 - 1
+
+_SCHEMES = (
+ "http:",
+ "https:",
+ "ftp:",
+ "file:",
+ "s3:",
+ "gs:",
+ "hf:",
+ "ssh:",
+)
+_INTEGER = re.compile(r"^-?(?:0|[1-9][0-9]*)$")
+_FLOAT = re.compile(
+ r"^-?(?:(?:0|[1-9][0-9]*)\.[0-9]+|(?:0|[1-9][0-9]*)(?:[eE][+-]?[0-9]+))$"
+)
+_YAML_KEY = re.compile(r"^[A-Za-z_][A-Za-z0-9_.-]*$")
+
+
+class SafetyError(ValueError):
+ """Raised when a local-input safety contract is violated."""
+
+
+def bounded_int(
+ value: str,
+ *,
+ minimum: int = 0,
+ maximum: int,
+ label: str,
+) -> int:
+ """Parse an integer while enforcing an explicit hard bound."""
+ try:
+ parsed = int(value)
+ except ValueError as exc:
+ raise SafetyError(f"{label} must be an integer") from exc
+ if parsed < minimum or parsed > maximum:
+ raise SafetyError(f"{label} must be between {minimum} and {maximum}")
+ return parsed
+
+
+def int_type(*, minimum: int = 0, maximum: int, label: str):
+ """Return an argparse-compatible bounded integer parser."""
+
+ def parse(value: str) -> int:
+ try:
+ return bounded_int(
+ value,
+ minimum=minimum,
+ maximum=maximum,
+ label=label,
+ )
+ except SafetyError as exc:
+ raise ValueError(str(exc)) from exc
+
+ return parse
+
+
+def _reject_unsafe_text_path(raw: str) -> None:
+ if not raw or "\x00" in raw:
+ raise SafetyError("path must be a nonempty string without NUL bytes")
+ lowered = raw.strip().lower()
+ if lowered.startswith(_SCHEMES) or "://" in lowered:
+ raise SafetyError("URLs and URI-like paths are not allowed")
+ if raw.startswith("~"):
+ raise SafetyError("home expansion is not allowed; pass an explicit path")
+ if ".." in Path(raw).parts:
+ raise SafetyError("parent traversal ('..') is not allowed")
+
+
+def _reject_symlink_components(path: Path) -> None:
+ current = Path(path.anchor)
+ for part in path.parts[1:]:
+ current = current / part
+ try:
+ mode = current.lstat().st_mode
+ except FileNotFoundError:
+ continue
+ if stat.S_ISLNK(mode):
+ raise SafetyError("symlink path component is not allowed")
+
+
+def local_path(
+ raw: str,
+ *,
+ must_exist: bool = True,
+ kind: str = "any",
+) -> Path:
+ """Resolve a strict local path without following symlinks."""
+ _reject_unsafe_text_path(raw)
+ candidate = Path(raw)
+ if not candidate.is_absolute():
+ candidate = Path.cwd() / candidate
+ candidate = Path(os.path.abspath(candidate))
+ _reject_symlink_components(candidate)
+
+ try:
+ mode = candidate.lstat().st_mode
+ except FileNotFoundError:
+ if must_exist:
+ raise SafetyError("required local path does not exist")
+ parent = candidate.parent
+ try:
+ parent_mode = parent.lstat().st_mode
+ except FileNotFoundError as exc:
+ raise SafetyError("output parent does not exist") from exc
+ if not stat.S_ISDIR(parent_mode):
+ raise SafetyError("output parent is not a directory")
+ return candidate
+
+ if stat.S_ISLNK(mode):
+ raise SafetyError("symlinks are not allowed")
+ if kind == "file" and not stat.S_ISREG(mode):
+ raise SafetyError("expected a regular file")
+ if kind == "dir" and not stat.S_ISDIR(mode):
+ raise SafetyError("expected a directory")
+ if kind == "any" and not (stat.S_ISREG(mode) or stat.S_ISDIR(mode)):
+ raise SafetyError("only regular files and directories are allowed")
+ return candidate
+
+
+def _open_binary_nofollow(path: Path):
+ flags = os.O_RDONLY
+ if hasattr(os, "O_NOFOLLOW"):
+ flags |= os.O_NOFOLLOW
+ descriptor = os.open(path, flags)
+ mode = os.fstat(descriptor).st_mode
+ if not stat.S_ISREG(mode):
+ os.close(descriptor)
+ raise SafetyError("expected a regular file")
+ return os.fdopen(descriptor, "rb")
+
+
+def iter_text_lines(
+ path: Path,
+ *,
+ max_bytes: int,
+ max_records: int,
+ max_line_bytes: int = HARD_MAX_LINE_BYTES,
+) -> Iterator[tuple[int, str]]:
+ """Yield UTF-8 text lines with compressed and expanded bounds."""
+ if max_bytes < 1 or max_bytes > HARD_MAX_BYTES:
+ raise SafetyError("max_bytes is outside the hard safety bound")
+ if max_records < 1 or max_records > HARD_MAX_RECORDS:
+ raise SafetyError("max_records is outside the hard safety bound")
+ if max_line_bytes < 1 or max_line_bytes > HARD_MAX_LINE_BYTES:
+ raise SafetyError("max_line_bytes is outside the hard safety bound")
+ if path.stat().st_size > max_bytes:
+ raise SafetyError("compressed/input file exceeds byte limit")
+
+ expanded_bytes = 0
+ with _open_binary_nofollow(path) as raw_handle:
+ if path.name.lower().endswith(".gz"):
+ stream = gzip.GzipFile(fileobj=raw_handle, mode="rb")
+ else:
+ stream = raw_handle
+ try:
+ for line_number, raw_line in enumerate(stream, start=1):
+ if line_number > max_records:
+ raise SafetyError(f"record limit exceeded in {path}")
+ if len(raw_line) > max_line_bytes:
+ raise SafetyError(f"line {line_number} exceeds line-size limit")
+ expanded_bytes += len(raw_line)
+ if expanded_bytes > max_bytes:
+ raise SafetyError("expanded text exceeds byte limit")
+ if b"\x00" in raw_line:
+ raise SafetyError(f"NUL byte found at line {line_number}")
+ try:
+ text = raw_line.decode("utf-8")
+ except UnicodeDecodeError as exc:
+ raise SafetyError(
+ f"file is not valid UTF-8 near line {line_number}"
+ ) from exc
+ yield line_number, text.rstrip("\r\n")
+ finally:
+ if stream is not raw_handle:
+ stream.close()
+
+
+def sha256_file(path: Path, *, max_bytes: int) -> tuple[str, int]:
+ """Hash a bounded regular file without following symlinks."""
+ if max_bytes < 1 or max_bytes > HARD_MAX_BYTES:
+ raise SafetyError("max_bytes is outside the hard safety bound")
+ digest = hashlib.sha256()
+ total = 0
+ with _open_binary_nofollow(path) as handle:
+ while True:
+ chunk = handle.read(1024 * 1024)
+ if not chunk:
+ break
+ total += len(chunk)
+ if total > max_bytes:
+ raise SafetyError("file exceeds hash byte limit")
+ digest.update(chunk)
+ return digest.hexdigest(), total
+
+
+def display_path(path: Path, index: int, mode: str) -> str:
+ """Render a path using the requested disclosure level."""
+ if mode == "redacted":
+ return f"file_{index:04d}"
+ if mode == "basename":
+ return path.name
+ if mode == "full":
+ return str(path)
+ raise SafetyError(f"unknown path display mode: {mode}")
+
+
+def add_path_mode_argument(parser) -> None:
+ parser.add_argument(
+ "--path-mode",
+ choices=("redacted", "basename", "full"),
+ default="redacted",
+ help="Path disclosure in output (default: redacted).",
+ )
+
+
+def print_json(payload: dict[str, Any]) -> None:
+ """Print deterministic JSON with no non-finite values."""
+ json.dump(payload, sys.stdout, indent=2, sort_keys=True, allow_nan=False)
+ sys.stdout.write("\n")
+
+
+def read_delimited_manifest(
+ path: Path,
+ *,
+ delimiter_name: str,
+ max_bytes: int,
+ max_rows: int,
+) -> tuple[list[str], list[dict[str, str]]]:
+ """Read a bounded UTF-8 CSV/TSV manifest."""
+ delimiter = "\t" if delimiter_name == "tsv" else ","
+ lines = [
+ line
+ for _, line in iter_text_lines(
+ path,
+ max_bytes=max_bytes,
+ max_records=max_rows + 1,
+ )
+ ]
+ if not lines:
+ raise SafetyError("manifest is empty")
+ reader = csv.DictReader(io.StringIO("\n".join(lines)), delimiter=delimiter)
+ if not reader.fieldnames:
+ raise SafetyError("manifest has no header")
+ raw_fieldnames = [field if field is not None else "" for field in reader.fieldnames]
+ if any(field != field.strip() for field in raw_fieldnames):
+ raise SafetyError("manifest column names must not have surrounding whitespace")
+ fieldnames = raw_fieldnames
+ if any(not field for field in fieldnames):
+ raise SafetyError("manifest contains an empty column name")
+ if len(set(fieldnames)) != len(fieldnames):
+ raise SafetyError("manifest contains duplicate column names")
+ if len(fieldnames) > 1_000:
+ raise SafetyError("manifest exceeds the 1,000-column safety bound")
+
+ rows: list[dict[str, str]] = []
+ for row_number, row in enumerate(reader, start=2):
+ if len(rows) >= max_rows:
+ raise SafetyError("manifest row limit exceeded")
+ if None in row:
+ raise SafetyError(f"manifest row {row_number} has extra fields")
+ normalized = {
+ key.strip(): (value.strip() if value is not None else "")
+ for key, value in row.items()
+ }
+ rows.append(normalized)
+ return fieldnames, rows
+
+
+def delimiter_from_path(path: Path, requested: str) -> str:
+ if requested != "auto":
+ return requested
+ return "csv" if path.suffix.lower() == ".csv" else "tsv"
+
+
+def load_chrom_sizes(
+ path: Path,
+ *,
+ max_bytes: int,
+ max_records: int,
+) -> tuple[dict[str, int], list[str]]:
+ """Load a strict two-column chromosome-sizes file."""
+ sizes: dict[str, int] = {}
+ order: list[str] = []
+ for line_number, line in iter_text_lines(
+ path,
+ max_bytes=max_bytes,
+ max_records=max_records,
+ ):
+ if not line or line.startswith("#"):
+ continue
+ fields = line.split("\t")
+ if len(fields) != 2:
+ raise SafetyError(
+ f"chromosome-sizes line {line_number} must have two tab-separated fields"
+ )
+ chrom, size_text = fields
+ if not chrom or any(character.isspace() for character in chrom):
+ raise SafetyError(f"invalid contig name at line {line_number}")
+ if chrom in sizes:
+ raise SafetyError(f"duplicate contig in chromosome sizes: {chrom}")
+ if not _INTEGER.fullmatch(size_text):
+ raise SafetyError(f"invalid contig size at line {line_number}")
+ size = int(size_text)
+ if size <= 0 or size > MAX_COORDINATE:
+ raise SafetyError(f"contig size out of bounds at line {line_number}")
+ sizes[chrom] = size
+ order.append(chrom)
+ if not sizes:
+ raise SafetyError("chromosome-sizes file has no records")
+ return sizes, order
+
+
+def simple_yaml_mapping(
+ path: Path,
+ *,
+ max_bytes: int,
+ max_records: int = 10_000,
+) -> dict[str, Any]:
+ """Parse a conservative top-level scalar YAML mapping without PyYAML."""
+ result: dict[str, Any] = {}
+ for line_number, line in iter_text_lines(
+ path,
+ max_bytes=max_bytes,
+ max_records=max_records,
+ ):
+ stripped = line.strip()
+ if not stripped or stripped.startswith("#") or stripped == "---":
+ continue
+ if line[:1].isspace():
+ raise SafetyError("nested YAML is not supported by the safe inspector")
+ if ":" not in line:
+ raise SafetyError(f"unsupported YAML at line {line_number}")
+ key, raw_value = line.split(":", 1)
+ key = key.strip()
+ raw_value = raw_value.strip()
+ if not _YAML_KEY.fullmatch(key):
+ raise SafetyError(f"invalid YAML key at line {line_number}")
+ if key in result:
+ raise SafetyError(f"duplicate YAML key: {key}")
+ if any(marker in raw_value for marker in ("!", "&", "*", "{", "[", "|", ">")):
+ raise SafetyError(f"complex YAML is not supported at line {line_number}")
+ if not raw_value or raw_value in {"null", "Null", "NULL", "~"}:
+ value: Any = None
+ elif raw_value in {"true", "True", "TRUE"}:
+ value = True
+ elif raw_value in {"false", "False", "FALSE"}:
+ value = False
+ elif _INTEGER.fullmatch(raw_value):
+ value = int(raw_value)
+ elif _FLOAT.fullmatch(raw_value):
+ value = float(raw_value)
+ elif (
+ len(raw_value) >= 2
+ and raw_value[0] == raw_value[-1]
+ and raw_value[0] in {"'", '"'}
+ ):
+ value = raw_value[1:-1]
+ else:
+ value = raw_value
+ result[key] = value
+ return result
+
+
+def fail_json(tool: str, exc: Exception) -> int:
+ """Emit a bounded machine-readable error."""
+ print_json(
+ {
+ "ok": False,
+ "tool": tool,
+ "error": type(exc).__name__,
+ "message": str(exc)[:500],
+ }
+ )
+ return 2
diff --git a/skills/geniml/scripts/bed_validator.py b/skills/geniml/scripts/bed_validator.py
new file mode 100644
index 00000000..604e22ec
--- /dev/null
+++ b/skills/geniml/scripts/bed_validator.py
@@ -0,0 +1,363 @@
+#!/usr/bin/env python3
+"""Validate one local BED file and emit a non-mutating normalization plan."""
+
+from __future__ import annotations
+
+import argparse
+import re
+import sys
+from collections import Counter
+from pathlib import Path
+
+from _common import (
+ HARD_MAX_BYTES,
+ HARD_MAX_RECORDS,
+ MAX_COORDINATE,
+ SafetyError,
+ add_path_mode_argument,
+ display_path,
+ fail_json,
+ int_type,
+ iter_text_lines,
+ load_chrom_sizes,
+ local_path,
+ print_json,
+ sha256_file,
+)
+
+
+TOOL = "bed-validator"
+_DECIMAL = re.compile(r"^-?[0-9]+$")
+_VALID_STRANDS = {"+", "-", "."}
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description=(
+ "Validate a local BED file as 0-based half-open intervals and emit "
+ "a bounded normalization plan. The input is never rewritten."
+ )
+ )
+ parser.add_argument("--input", required=True, help="Local BED or BED.GZ file.")
+ parser.add_argument(
+ "--assembly",
+ required=True,
+ help="Declared assembly/accession recorded in the report.",
+ )
+ parser.add_argument(
+ "--chrom-sizes",
+ help="Optional local two-column chromosome-sizes file for contig/bounds checks.",
+ )
+ parser.add_argument(
+ "--max-bytes",
+ type=int_type(minimum=1, maximum=HARD_MAX_BYTES, label="max-bytes"),
+ default=512 * 1024**2,
+ help="Maximum compressed and expanded input bytes (default: 512 MiB).",
+ )
+ parser.add_argument(
+ "--max-records",
+ type=int_type(minimum=1, maximum=HARD_MAX_RECORDS, label="max-records"),
+ default=1_000_000,
+ help="Maximum text records, including headers (default: 1,000,000).",
+ )
+ parser.add_argument(
+ "--max-examples",
+ type=int_type(minimum=0, maximum=100, label="max-examples"),
+ default=10,
+ help="Maximum redacted issue examples (default: 10).",
+ )
+ parser.add_argument(
+ "--allow-empty",
+ action="store_true",
+ help="Do not treat a BED with zero data records as an error.",
+ )
+ add_path_mode_argument(parser)
+ return parser
+
+
+def _record_issue(
+ counter: Counter[str],
+ examples: list[dict[str, int | str]],
+ code: str,
+ line_number: int,
+ maximum: int,
+) -> None:
+ counter[code] += 1
+ if len(examples) < maximum:
+ examples.append({"code": code, "line": line_number})
+
+
+def validate(args: argparse.Namespace) -> tuple[dict, int]:
+ if not args.assembly.strip() or len(args.assembly) > 200:
+ raise SafetyError("assembly must be a nonempty value of at most 200 characters")
+
+ bed_path = local_path(args.input, kind="file")
+ chrom_sizes: dict[str, int] | None = None
+ chrom_order: dict[str, int] | None = None
+ chrom_sizes_digest: str | None = None
+ if args.chrom_sizes:
+ chrom_path = local_path(args.chrom_sizes, kind="file")
+ sizes, order = load_chrom_sizes(
+ chrom_path,
+ max_bytes=min(args.max_bytes, 128 * 1024**2),
+ max_records=min(args.max_records, 1_000_000),
+ )
+ chrom_sizes = sizes
+ chrom_order = {chrom: index for index, chrom in enumerate(order)}
+ chrom_sizes_digest, _ = sha256_file(
+ chrom_path,
+ max_bytes=min(args.max_bytes, 128 * 1024**2),
+ )
+
+ errors: Counter[str] = Counter()
+ warnings: Counter[str] = Counter()
+ examples: list[dict[str, int | str]] = []
+ records = 0
+ skipped = 0
+ min_columns: int | None = None
+ max_columns = 0
+ contigs: set[str] = set()
+ seen: set[tuple[str, int, int]] = set()
+ duplicate_count = 0
+ overlap_count = 0
+ max_end_by_contig: dict[str, int] = {}
+ previous_sort_key: tuple[int | str, int, int] | None = None
+ unsorted_count = 0
+ strand_rows = 0
+
+ for line_number, line in iter_text_lines(
+ bed_path,
+ max_bytes=args.max_bytes,
+ max_records=args.max_records,
+ ):
+ if not line:
+ skipped += 1
+ warnings["blank_line"] += 1
+ continue
+ if line.startswith("#") or line.startswith("track") or line.startswith("browser"):
+ skipped += 1
+ continue
+
+ fields = line.split("\t")
+ if len(fields) < 3:
+ _record_issue(
+ errors,
+ examples,
+ "fewer_than_three_columns",
+ line_number,
+ args.max_examples,
+ )
+ continue
+ records += 1
+ min_columns = len(fields) if min_columns is None else min(min_columns, len(fields))
+ max_columns = max(max_columns, len(fields))
+
+ chrom = fields[0]
+ if not chrom or any(character.isspace() or ord(character) < 32 for character in chrom):
+ _record_issue(
+ errors,
+ examples,
+ "invalid_contig",
+ line_number,
+ args.max_examples,
+ )
+ continue
+
+ start_text, end_text = fields[1], fields[2]
+ if not _DECIMAL.fullmatch(start_text) or not _DECIMAL.fullmatch(end_text):
+ _record_issue(
+ errors,
+ examples,
+ "non_decimal_coordinate",
+ line_number,
+ args.max_examples,
+ )
+ continue
+ start, end = int(start_text), int(end_text)
+ if start < 0 or end < 0:
+ _record_issue(
+ errors,
+ examples,
+ "negative_coordinate",
+ line_number,
+ args.max_examples,
+ )
+ continue
+ if start > MAX_COORDINATE or end > MAX_COORDINATE:
+ _record_issue(
+ errors,
+ examples,
+ "coordinate_integer_overflow",
+ line_number,
+ args.max_examples,
+ )
+ continue
+ if end == start:
+ _record_issue(
+ errors,
+ examples,
+ "zero_length_interval",
+ line_number,
+ args.max_examples,
+ )
+ continue
+ if end < start:
+ _record_issue(
+ errors,
+ examples,
+ "end_before_start",
+ line_number,
+ args.max_examples,
+ )
+ continue
+
+ if chrom_sizes is not None:
+ if chrom not in chrom_sizes:
+ _record_issue(
+ errors,
+ examples,
+ "unknown_contig",
+ line_number,
+ args.max_examples,
+ )
+ continue
+ if end > chrom_sizes[chrom]:
+ _record_issue(
+ errors,
+ examples,
+ "end_beyond_contig",
+ line_number,
+ args.max_examples,
+ )
+ continue
+
+ if len(fields) >= 6:
+ strand_rows += 1
+ if fields[5] not in _VALID_STRANDS:
+ _record_issue(
+ errors,
+ examples,
+ "invalid_bed6_strand",
+ line_number,
+ args.max_examples,
+ )
+
+ contigs.add(chrom)
+ key = (chrom, start, end)
+ if key in seen:
+ duplicate_count += 1
+ else:
+ seen.add(key)
+
+ prior_end = max_end_by_contig.get(chrom)
+ if prior_end is not None and start < prior_end:
+ overlap_count += 1
+ max_end_by_contig[chrom] = max(end, prior_end or end)
+
+ if chrom_order is None:
+ sort_key: tuple[int | str, int, int] = (chrom, start, end)
+ else:
+ sort_key = (chrom_order[chrom], start, end)
+ if previous_sort_key is not None and sort_key < previous_sort_key:
+ unsorted_count += 1
+ previous_sort_key = sort_key
+
+ if records == 0 and not args.allow_empty:
+ errors["no_data_records"] += 1
+ if chrom_sizes is None:
+ warnings["bounds_not_checked_without_chrom_sizes"] += 1
+ if unsorted_count:
+ warnings["out_of_order_records"] = unsorted_count
+ if duplicate_count:
+ warnings["duplicate_intervals"] = duplicate_count
+ if overlap_count:
+ warnings["overlapping_intervals"] = overlap_count
+
+ digest, size = sha256_file(bed_path, max_bytes=args.max_bytes)
+ actions: list[dict[str, str | int]] = []
+ if errors:
+ actions.append(
+ {
+ "action": "reject_or_quarantine_invalid_rows",
+ "count": sum(errors.values()),
+ }
+ )
+ if unsorted_count:
+ actions.append(
+ {
+ "action": "stable_sort",
+ "detail": (
+ "use chromosome-sizes order, then numeric start/end"
+ if chrom_order is not None
+ else "supply chromosome sizes before choosing contig order"
+ ),
+ }
+ )
+ if duplicate_count:
+ actions.append(
+ {
+ "action": "decide_duplicate_policy",
+ "count": duplicate_count,
+ }
+ )
+ if chrom_sizes is None:
+ actions.append(
+ {
+ "action": "supply_chromosome_sizes",
+ "detail": "required before overflow/contig normalization",
+ }
+ )
+ actions.append(
+ {
+ "action": "preserve_coordinate_semantics",
+ "detail": "BED 0-based half-open; no implicit liftover or chr renaming",
+ }
+ )
+
+ report = {
+ "ok": not errors,
+ "tool": TOOL,
+ "contract": {
+ "assembly": args.assembly,
+ "coordinate_system": "0-based-half-open",
+ "input_mutated": False,
+ "network_used": False,
+ },
+ "input": {
+ "path": display_path(bed_path, 1, args.path_mode),
+ "sha256": digest,
+ "size_bytes": size,
+ },
+ "chromosome_sizes_sha256": chrom_sizes_digest,
+ "summary": {
+ "records": records,
+ "skipped_header_or_blank_lines": skipped,
+ "contig_count": len(contigs),
+ "minimum_columns": min_columns,
+ "maximum_columns": max_columns if records else None,
+ "bed6_or_wider_rows": strand_rows,
+ "duplicate_intervals": duplicate_count,
+ "overlap_observations": overlap_count,
+ "out_of_order_records": unsorted_count,
+ },
+ "errors": dict(sorted(errors.items())),
+ "warnings": dict(sorted(warnings.items())),
+ "issue_examples": examples,
+ "normalization_plan": actions,
+ }
+ return report, 0 if not errors else 2
+
+
+def main() -> int:
+ parser = build_parser()
+ args = parser.parse_args()
+ try:
+ report, exit_code = validate(args)
+ print_json(report)
+ return exit_code
+ except (OSError, SafetyError, UnicodeError) as exc:
+ return fail_json(TOOL, exc)
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/skills/geniml/scripts/consensus_plan.py b/skills/geniml/scripts/consensus_plan.py
new file mode 100644
index 00000000..1d6e2776
--- /dev/null
+++ b/skills/geniml/scripts/consensus_plan.py
@@ -0,0 +1,416 @@
+#!/usr/bin/env python3
+"""Create a bounded, local-only Geniml consensus-universe execution plan."""
+
+from __future__ import annotations
+
+import argparse
+import re
+import sys
+from collections import Counter
+from pathlib import Path
+
+from _common import (
+ HARD_MAX_BYTES,
+ HARD_MAX_FILES,
+ SafetyError,
+ add_path_mode_argument,
+ delimiter_from_path,
+ display_path,
+ fail_json,
+ int_type,
+ load_chrom_sizes,
+ local_path,
+ print_json,
+ read_delimited_manifest,
+ sha256_file,
+)
+
+
+TOOL = "consensus-peaks-planner"
+_SAFE_PREFIX = re.compile(r"^[A-Za-z0-9_.-]{1,100}$")
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description=(
+ "Validate local consensus inputs and emit argv arrays for Geniml "
+ "0.8.4. No command, coverage tool, or network request is executed."
+ )
+ )
+ parser.add_argument("--manifest", required=True)
+ parser.add_argument("--chrom-sizes", required=True)
+ parser.add_argument("--assembly", required=True)
+ parser.add_argument("--output-dir", required=True)
+ parser.add_argument("--coverage-dir")
+ parser.add_argument("--likelihood-model")
+ parser.add_argument("--method", choices=("cc", "ccf", "ml", "hmm"), required=True)
+ parser.add_argument("--coverage-prefix", default="all")
+ parser.add_argument(
+ "--cutoff",
+ type=int_type(minimum=1, maximum=2**31 - 1, label="cutoff"),
+ )
+ parser.add_argument(
+ "--merge",
+ type=int_type(minimum=0, maximum=10_000_000, label="merge"),
+ default=0,
+ )
+ parser.add_argument(
+ "--filter-size",
+ type=int_type(minimum=0, maximum=10_000_000, label="filter-size"),
+ default=0,
+ )
+ parser.add_argument("--no-normalize", action="store_true")
+ parser.add_argument("--save-max-coverage", action="store_true")
+ parser.add_argument("--path-column", default="path")
+ parser.add_argument("--assembly-column", default="assembly")
+ parser.add_argument(
+ "--delimiter",
+ choices=("auto", "csv", "tsv"),
+ default="auto",
+ )
+ parser.add_argument(
+ "--max-files",
+ type=int_type(minimum=1, maximum=HARD_MAX_FILES, label="max-files"),
+ default=10_000,
+ )
+ parser.add_argument(
+ "--max-manifest-bytes",
+ type=int_type(
+ minimum=1,
+ maximum=HARD_MAX_BYTES,
+ label="max-manifest-bytes",
+ ),
+ default=64 * 1024**2,
+ )
+ parser.add_argument(
+ "--max-total-input-bytes",
+ type=int_type(
+ minimum=1,
+ maximum=HARD_MAX_BYTES,
+ label="max-total-input-bytes",
+ ),
+ default=4 * 1024**3,
+ )
+ add_path_mode_argument(parser)
+ return parser
+
+
+def _manifest_bed(raw: str, manifest: Path) -> Path:
+ candidate = Path(raw)
+ if not candidate.is_absolute():
+ candidate = manifest.parent / candidate
+ return local_path(str(candidate), kind="file")
+
+
+def _render(path: Path, label: str, mode: str) -> str:
+ if mode == "full":
+ return str(path)
+ if mode == "basename":
+ return path.name
+ return f"<{label}>"
+
+
+def plan(args: argparse.Namespace) -> tuple[dict, int]:
+ if not args.assembly.strip() or len(args.assembly) > 200:
+ raise SafetyError("assembly must be a nonempty value of at most 200 characters")
+ if not _SAFE_PREFIX.fullmatch(args.coverage_prefix):
+ raise SafetyError("coverage prefix contains unsupported characters")
+ if args.cutoff is not None and args.cutoff < 1:
+ raise SafetyError("cutoff must be at least 1")
+ if args.method != "cc" and (
+ args.cutoff is not None or args.merge != 0 or args.filter_size != 0
+ ):
+ raise SafetyError("cutoff, merge, and filter-size apply only to method cc")
+ if args.method != "hmm" and (args.no_normalize or args.save_max_coverage):
+ raise SafetyError("normalization and max-coverage flags apply only to method hmm")
+ if args.likelihood_model and args.method != "ml":
+ raise SafetyError("likelihood-model applies only to method ml")
+
+ manifest = local_path(args.manifest, kind="file")
+ chrom_sizes = local_path(args.chrom_sizes, kind="file")
+ output_dir = local_path(args.output_dir, kind="dir")
+ delimiter = delimiter_from_path(manifest, args.delimiter)
+ fields, rows = read_delimited_manifest(
+ manifest,
+ delimiter_name=delimiter,
+ max_bytes=args.max_manifest_bytes,
+ max_rows=args.max_files,
+ )
+ if args.path_column not in fields:
+ raise SafetyError(f"path column is missing: {args.path_column}")
+ if args.assembly_column not in fields:
+ raise SafetyError(f"assembly column is missing: {args.assembly_column}")
+ if not rows:
+ raise SafetyError("manifest has no data rows")
+
+ sizes, contig_order = load_chrom_sizes(
+ chrom_sizes,
+ max_bytes=min(args.max_manifest_bytes, 128 * 1024**2),
+ max_records=1_000_000,
+ )
+ manifest_digest, manifest_size = sha256_file(
+ manifest,
+ max_bytes=args.max_manifest_bytes,
+ )
+ chrom_digest, chrom_size_bytes = sha256_file(
+ chrom_sizes,
+ max_bytes=min(args.max_manifest_bytes, 128 * 1024**2),
+ )
+
+ errors: Counter[str] = Counter()
+ warnings: Counter[str] = Counter()
+ local_beds: list[Path] = []
+ total_input_bytes = 0
+ for row in rows:
+ raw_path = row.get(args.path_column, "")
+ if not raw_path:
+ errors["blank_path"] += 1
+ continue
+ if row.get(args.assembly_column, "") != args.assembly:
+ errors["assembly_mismatch"] += 1
+ try:
+ bed = _manifest_bed(raw_path, manifest)
+ except (OSError, SafetyError):
+ errors["invalid_or_missing_local_bed"] += 1
+ continue
+ local_beds.append(bed)
+ total_input_bytes += bed.lstat().st_size
+ if total_input_bytes > args.max_total_input_bytes:
+ raise SafetyError("BED corpus exceeds max-total-input-bytes")
+ if len(set(local_beds)) != len(local_beds):
+ errors["duplicate_bed_paths"] = len(local_beds) - len(set(local_beds))
+
+ coverage_dir: Path | None = None
+ required_coverage_names = (
+ [f"{args.coverage_prefix}_core.bw"]
+ if args.method in {"cc", "ccf"}
+ else [
+ f"{args.coverage_prefix}_start.bw",
+ f"{args.coverage_prefix}_core.bw",
+ f"{args.coverage_prefix}_end.bw",
+ ]
+ )
+ coverage_files: list[dict[str, str | int]] = []
+ if args.coverage_dir:
+ coverage_dir = local_path(args.coverage_dir, kind="dir")
+ for index, name in enumerate(required_coverage_names, start=1):
+ try:
+ coverage_file = local_path(str(coverage_dir / name), kind="file")
+ except (OSError, SafetyError):
+ errors[f"missing_coverage:{name}"] += 1
+ continue
+ digest, size = sha256_file(
+ coverage_file,
+ max_bytes=args.max_total_input_bytes,
+ )
+ coverage_files.append(
+ {
+ "name": name,
+ "sha256": digest,
+ "size_bytes": size,
+ }
+ )
+ else:
+ warnings["coverage_generation_required"] += 1
+
+ output_file = output_dir / f"universe_{args.method}.bed"
+ if output_file.exists() or output_file.is_symlink():
+ errors["output_file_already_exists"] += 1
+
+ coverage_arg = (
+ _render(coverage_dir, "LOCAL_COVERAGE_DIR", args.path_mode)
+ if coverage_dir
+ else "<LOCAL_COVERAGE_DIR>"
+ )
+ output_arg = _render(output_file, "OUTPUT_UNIVERSE_BED", args.path_mode)
+
+ stages: list[dict] = [
+ {
+ "stage": "validate_beds",
+ "executed": False,
+ "instruction": (
+ "Run bed_validator.py for every BED using the same chromosome sizes; "
+ "reject any invalid result."
+ ),
+ },
+ {
+ "stage": "generate_coverage",
+ "executed": False,
+ "blocked": coverage_dir is None,
+ "required_outputs": required_coverage_names,
+ "instruction": (
+ "Use an explicitly pinned and checksummed coverage executable. "
+ "Capture its installed --help; this planner does not guess a Gtars/uniwig argv."
+ ),
+ },
+ ]
+
+ likelihood_path: Path | None = None
+ if args.method == "ml":
+ if args.likelihood_model:
+ likelihood_path = local_path(args.likelihood_model, kind="file")
+ else:
+ likelihood_path = output_dir / "likelihood_model.tar"
+ if likelihood_path.exists() or likelihood_path.is_symlink():
+ errors["planned_likelihood_model_already_exists"] += 1
+ lh_template = [
+ "geniml",
+ "lh",
+ "--model-file",
+ "<LIKELIHOOD_MODEL_TAR>",
+ "--coverage-folder",
+ "<LOCAL_COVERAGE_DIR>",
+ "--coverage-prefix",
+ args.coverage_prefix,
+ "--file-no",
+ str(len(set(local_beds))),
+ ]
+ lh_argv = None
+ if args.path_mode == "full" and coverage_dir:
+ lh_argv = [
+ "geniml",
+ "lh",
+ "--model-file",
+ str(likelihood_path),
+ "--coverage-folder",
+ str(coverage_dir),
+ "--coverage-prefix",
+ args.coverage_prefix,
+ "--file-no",
+ str(len(set(local_beds))),
+ ]
+ stages.append(
+ {
+ "stage": "build_likelihood_model",
+ "executed": False,
+ "argv_template": lh_template,
+ "argv": lh_argv,
+ }
+ )
+
+ command_template = [
+ "geniml",
+ "build-universe",
+ args.method,
+ "--coverage-folder",
+ "<LOCAL_COVERAGE_DIR>",
+ "--coverage-prefix",
+ args.coverage_prefix,
+ "--output-file",
+ "<OUTPUT_UNIVERSE_BED>",
+ ]
+ command = [
+ "geniml",
+ "build-universe",
+ args.method,
+ "--coverage-folder",
+ coverage_arg,
+ "--coverage-prefix",
+ args.coverage_prefix,
+ "--output-file",
+ output_arg,
+ ]
+ if args.method == "cc":
+ if args.cutoff is not None:
+ command_template.extend(["--cutoff", str(args.cutoff)])
+ command.extend(["--cutoff", str(args.cutoff)])
+ if args.cutoff > len(set(local_beds)):
+ warnings["cutoff_exceeds_file_count"] += 1
+ command_template.extend(["--merge", str(args.merge), "--filter-size", str(args.filter_size)])
+ command.extend(["--merge", str(args.merge), "--filter-size", str(args.filter_size)])
+ elif args.method == "ml":
+ command_template.extend(["--model-file", "<LIKELIHOOD_MODEL_TAR>"])
+ command.extend(
+ [
+ "--model-file",
+ _render(
+ likelihood_path,
+ "LIKELIHOOD_MODEL_TAR",
+ args.path_mode,
+ ),
+ ]
+ )
+ elif args.method == "hmm":
+ if args.no_normalize:
+ command_template.append("--not-normalize")
+ command.append("--not-normalize")
+ if args.save_max_coverage:
+ command_template.append("--save-max-cove")
+ command.append("--save-max-cove")
+
+ stages.append(
+ {
+ "stage": "build_universe",
+ "executed": False,
+ "argv_template": command_template,
+ "argv": command if args.path_mode == "full" and coverage_dir else None,
+ }
+ )
+ stages.append(
+ {
+ "stage": "validate_output",
+ "executed": False,
+ "instruction": (
+ "Validate BED coordinates, sorting, contigs, bounds, nonempty output, "
+ "and checksum before constructing a tokenizer."
+ ),
+ }
+ )
+
+ ready = not errors and coverage_dir is not None
+ report = {
+ "ok": not errors,
+ "ready_to_execute": ready,
+ "tool": TOOL,
+ "contract": {
+ "assembly": args.assembly,
+ "coordinate_system": "0-based-half-open",
+ "method": args.method,
+ "network_used": False,
+ "commands_executed": False,
+ "paths_disclosed": args.path_mode == "full",
+ },
+ "inputs": {
+ "manifest": {
+ "path": display_path(manifest, 1, args.path_mode),
+ "sha256": manifest_digest,
+ "size_bytes": manifest_size,
+ },
+ "chromosome_sizes": {
+ "path": display_path(chrom_sizes, 2, args.path_mode),
+ "sha256": chrom_digest,
+ "size_bytes": chrom_size_bytes,
+ "contig_count": len(sizes),
+ "first_contigs": contig_order[:10],
+ },
+ "bed_rows": len(rows),
+ "unique_local_beds": len(set(local_beds)),
+ "total_bed_bytes": total_input_bytes,
+ "coverage_files": coverage_files,
+ },
+ "errors": dict(sorted(errors.items())),
+ "warnings": dict(sorted(warnings.items())),
+ "stages": stages,
+ "provenance_requirements": [
+ "patient/donor-grouped training-only manifest",
+ "input BED and chromosome-sizes checksums",
+ "coverage executable version, checksum, help output, and argv",
+ "coverage-track and universe checksums",
+ "Geniml/Gtars/Python lockfile and platform",
+ ],
+ }
+ return report, 0 if not errors else 2
+
+
+def main() -> int:
+ args = build_parser().parse_args()
+ try:
+ report, exit_code = plan(args)
+ print_json(report)
+ return exit_code
+ except (OSError, SafetyError, UnicodeError) as exc:
+ return fail_json(TOOL, exc)
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/skills/geniml/scripts/corpus_auditor.py b/skills/geniml/scripts/corpus_auditor.py
new file mode 100644
index 00000000..0a125535
--- /dev/null
+++ b/skills/geniml/scripts/corpus_auditor.py
@@ -0,0 +1,304 @@
+#!/usr/bin/env python3
+"""Audit a local interval manifest without exposing sample metadata values."""
+
+from __future__ import annotations
+
+import argparse
+import sys
+from collections import Counter, defaultdict
+from pathlib import Path
+
+from _common import (
+ HARD_MAX_BYTES,
+ HARD_MAX_FILES,
+ SafetyError,
+ add_path_mode_argument,
+ delimiter_from_path,
+ display_path,
+ fail_json,
+ int_type,
+ local_path,
+ print_json,
+ read_delimited_manifest,
+ sha256_file,
+)
+
+
+TOOL = "interval-corpus-auditor"
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description=(
+ "Audit a local CSV/TSV manifest for paths, assemblies, duplicate "
+ "content, and patient/donor split leakage. Metadata values are not printed."
+ )
+ )
+ parser.add_argument("--manifest", required=True, help="Local CSV/TSV manifest.")
+ parser.add_argument(
+ "--delimiter",
+ choices=("auto", "csv", "tsv"),
+ default="auto",
+ )
+ parser.add_argument(
+ "--path-column",
+ default="path",
+ help="Column containing BED paths (default: path).",
+ )
+ parser.add_argument(
+ "--assembly-column",
+ default="assembly",
+ help="Assembly column (default: assembly).",
+ )
+ parser.add_argument(
+ "--expected-assembly",
+ help="Optional expected assembly/accession.",
+ )
+ parser.add_argument(
+ "--group-column",
+ action="append",
+ default=[],
+ help="Patient/donor grouping column; repeat as needed.",
+ )
+ parser.add_argument(
+ "--split-column",
+ help="Train/validation/test column used for leakage checks.",
+ )
+ parser.add_argument(
+ "--checksums",
+ action="store_true",
+ help="Hash each bounded BED to detect duplicate content.",
+ )
+ parser.add_argument(
+ "--max-files",
+ type=int_type(minimum=1, maximum=HARD_MAX_FILES, label="max-files"),
+ default=10_000,
+ )
+ parser.add_argument(
+ "--max-manifest-bytes",
+ type=int_type(
+ minimum=1,
+ maximum=HARD_MAX_BYTES,
+ label="max-manifest-bytes",
+ ),
+ default=64 * 1024**2,
+ )
+ parser.add_argument(
+ "--max-file-bytes",
+ type=int_type(minimum=1, maximum=HARD_MAX_BYTES, label="max-file-bytes"),
+ default=2 * 1024**3,
+ )
+ parser.add_argument(
+ "--max-total-bytes",
+ type=int_type(minimum=1, maximum=HARD_MAX_BYTES, label="max-total-bytes"),
+ default=4 * 1024**3,
+ )
+ parser.add_argument(
+ "--max-output-files",
+ type=int_type(minimum=0, maximum=1_000, label="max-output-files"),
+ default=100,
+ help="Maximum bounded per-file summaries (default: 100).",
+ )
+ add_path_mode_argument(parser)
+ return parser
+
+
+def _manifest_local_path(raw: str, manifest: Path) -> Path:
+ candidate = Path(raw)
+ if not candidate.is_absolute():
+ candidate = manifest.parent / candidate
+ return local_path(str(candidate), kind="file")
+
+
+def audit(args: argparse.Namespace) -> tuple[dict, int]:
+ manifest = local_path(args.manifest, kind="file")
+ delimiter = delimiter_from_path(manifest, args.delimiter)
+ fields, rows = read_delimited_manifest(
+ manifest,
+ delimiter_name=delimiter,
+ max_bytes=args.max_manifest_bytes,
+ max_rows=args.max_files,
+ )
+ if args.path_column not in fields:
+ raise SafetyError(f"path column is missing: {args.path_column}")
+ if args.split_column and args.split_column not in fields:
+ raise SafetyError(f"split column is missing: {args.split_column}")
+ if len(args.group_column) > 20:
+ raise SafetyError("at most 20 group columns may be checked")
+ missing_groups = [column for column in args.group_column if column not in fields]
+ if missing_groups:
+ raise SafetyError(f"group columns are missing: {', '.join(missing_groups)}")
+ if args.expected_assembly and not args.expected_assembly.strip():
+ raise SafetyError("expected assembly must not be blank")
+
+ errors: Counter[str] = Counter()
+ warnings: Counter[str] = Counter()
+ path_counts: Counter[Path] = Counter()
+ assembly_counts: Counter[str] = Counter()
+ group_splits: dict[str, dict[str, set[str]]] = {
+ column: defaultdict(set) for column in args.group_column
+ }
+ missing_group_values: Counter[str] = Counter()
+ split_missing = 0
+ total_bytes = 0
+ file_summaries: list[dict] = []
+ checksum_counts: Counter[str] = Counter()
+
+ if args.assembly_column not in fields:
+ warnings["assembly_column_missing"] += 1
+
+ for row_index, row in enumerate(rows, start=1):
+ raw_path = row.get(args.path_column, "")
+ if not raw_path:
+ errors["blank_path"] += 1
+ continue
+ try:
+ bed_path = _manifest_local_path(raw_path, manifest)
+ except (OSError, SafetyError):
+ errors["invalid_or_missing_local_path"] += 1
+ continue
+
+ path_counts[bed_path] += 1
+ size = bed_path.lstat().st_size
+ if size > args.max_file_bytes:
+ errors["file_exceeds_byte_limit"] += 1
+ continue
+ total_bytes += size
+ if total_bytes > args.max_total_bytes:
+ raise SafetyError("corpus exceeds max-total-bytes")
+ lowered_name = bed_path.name.lower()
+ if not (lowered_name.endswith(".bed") or lowered_name.endswith(".bed.gz")):
+ warnings["nonstandard_bed_extension"] += 1
+
+ assembly = row.get(args.assembly_column, "") if args.assembly_column in fields else ""
+ if assembly:
+ if len(assembly) > 200:
+ errors["assembly_value_too_long"] += 1
+ else:
+ assembly_counts[assembly] += 1
+ if args.expected_assembly and assembly != args.expected_assembly:
+ errors["unexpected_assembly"] += 1
+ elif args.assembly_column in fields:
+ errors["blank_assembly"] += 1
+
+ split = row.get(args.split_column, "") if args.split_column else ""
+ if args.split_column and not split:
+ split_missing += 1
+ for group_column in args.group_column:
+ group_value = row.get(group_column, "")
+ if not group_value:
+ missing_group_values[group_column] += 1
+ elif args.split_column and split:
+ group_splits[group_column][group_value].add(split)
+
+ checksum: str | None = None
+ if args.checksums:
+ checksum, _ = sha256_file(bed_path, max_bytes=args.max_file_bytes)
+ checksum_counts[checksum] += 1
+ if len(file_summaries) < args.max_output_files:
+ summary = {
+ "path": display_path(bed_path, row_index, args.path_mode),
+ "size_bytes": size,
+ }
+ if checksum:
+ summary["sha256"] = checksum
+ file_summaries.append(summary)
+
+ duplicate_path_rows = sum(count - 1 for count in path_counts.values() if count > 1)
+ if duplicate_path_rows:
+ warnings["duplicate_path_rows"] = duplicate_path_rows
+ if len(assembly_counts) > 1:
+ errors["mixed_assemblies"] = len(assembly_counts)
+ if not assembly_counts:
+ warnings["assembly_not_recorded"] += 1
+ if split_missing:
+ errors["blank_split"] = split_missing
+ for column, count in missing_group_values.items():
+ if count:
+ errors[f"blank_group:{column}"] = count
+
+ leakage: dict[str, dict[str, int]] = {}
+ for column, groups in group_splits.items():
+ crossing = sum(1 for splits in groups.values() if len(splits) > 1)
+ leakage[column] = {
+ "distinct_groups": len(groups),
+ "groups_crossing_splits": crossing,
+ }
+ if crossing:
+ errors[f"group_leakage:{column}"] = crossing
+
+ duplicate_content_groups = 0
+ duplicate_content_files = 0
+ if args.checksums:
+ duplicate_content_groups = sum(1 for count in checksum_counts.values() if count > 1)
+ duplicate_content_files = sum(
+ count for count in checksum_counts.values() if count > 1
+ )
+ if duplicate_content_groups:
+ warnings["duplicate_content_groups"] = duplicate_content_groups
+
+ manifest_digest, manifest_size = sha256_file(
+ manifest,
+ max_bytes=args.max_manifest_bytes,
+ )
+ report = {
+ "ok": not errors,
+ "tool": TOOL,
+ "contract": {
+ "network_used": False,
+ "metadata_values_emitted": False,
+ "symlinks_allowed": False,
+ },
+ "manifest": {
+ "path": display_path(manifest, 0, args.path_mode),
+ "sha256": manifest_digest,
+ "size_bytes": manifest_size,
+ "delimiter": delimiter,
+ "column_count": len(fields),
+ "column_names_emitted": False,
+ },
+ "summary": {
+ "manifest_rows": len(rows),
+ "valid_local_file_rows": sum(path_counts.values()),
+ "unique_local_files": len(path_counts),
+ "total_input_bytes": total_bytes,
+ "assembly_count": len(assembly_counts),
+ "assemblies": (
+ sorted(assembly_counts)[:20]
+ if args.path_mode == "full"
+ else "<redacted>"
+ ),
+ "duplicate_path_rows": duplicate_path_rows,
+ "duplicate_content_groups": duplicate_content_groups,
+ "duplicate_content_files": duplicate_content_files,
+ "file_summaries_omitted": max(
+ 0,
+ sum(path_counts.values()) - len(file_summaries),
+ ),
+ },
+ "leakage_checks": leakage,
+ "errors": dict(sorted(errors.items())),
+ "warnings": dict(sorted(warnings.items())),
+ "files": file_summaries,
+ "recommendations": [
+ "keep every patient/donor/biological replicate in exactly one split",
+ "build universes and fit preprocessing on training groups only",
+ "validate each BED against one checksummed chromosome-sizes file",
+ "store full metadata only in the protected project manifest",
+ ],
+ }
+ return report, 0 if not errors else 2
+
+
+def main() -> int:
+ args = build_parser().parse_args()
+ try:
+ report, exit_code = audit(args)
+ print_json(report)
+ return exit_code
+ except (OSError, SafetyError, UnicodeError) as exc:
+ return fail_json(TOOL, exc)
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/skills/geniml/scripts/embedding_plan.py b/skills/geniml/scripts/embedding_plan.py
new file mode 100644
index 00000000..3fc53ea2
--- /dev/null
+++ b/skills/geniml/scripts/embedding_plan.py
@@ -0,0 +1,476 @@
+#!/usr/bin/env python3
+"""Plan a bounded local Geniml embedding run without importing ML packages."""
+
+from __future__ import annotations
+
+import argparse
+import os
+import stat
+import sys
+from collections import Counter
+from pathlib import Path
+
+from _common import (
+ HARD_MAX_BYTES,
+ HARD_MAX_EPOCHS,
+ HARD_MAX_WORKERS,
+ SafetyError,
+ add_path_mode_argument,
+ display_path,
+ fail_json,
+ int_type,
+ local_path,
+ print_json,
+ sha256_file,
+ simple_yaml_mapping,
+)
+
+
+TOOL = "embedding-run-planner"
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description=(
+ "Validate local inputs and emit a redacted Region2Vec, scEmbed, or "
+ "BEDspace run plan. No package, model, binary, or network call is executed."
+ )
+ )
+ parser.add_argument("--mode", choices=("region2vec", "scembed", "bedspace"), required=True)
+ parser.add_argument("--data", required=True, help="Token Parquet or BED directory.")
+ parser.add_argument("--metadata", help="BEDspace metadata CSV.")
+ parser.add_argument("--universe", required=True)
+ parser.add_argument("--output-dir", required=True)
+ parser.add_argument("--model-dir", help="Optional trusted local model bundle.")
+ parser.add_argument("--starspace-dir", help="BEDspace-only local StarSpace directory.")
+ parser.add_argument("--assembly", required=True)
+ parser.add_argument(
+ "--split-unit",
+ choices=("patient", "donor", "biological-replicate", "sample", "cell"),
+ default="patient",
+ )
+ parser.add_argument(
+ "--embedding-dim",
+ type=int_type(minimum=1, maximum=4096, label="embedding-dim"),
+ default=100,
+ )
+ parser.add_argument(
+ "--epochs",
+ type=int_type(minimum=1, maximum=HARD_MAX_EPOCHS, label="epochs"),
+ default=10,
+ )
+ parser.add_argument(
+ "--workers",
+ type=int_type(minimum=1, maximum=HARD_MAX_WORKERS, label="workers"),
+ default=4,
+ )
+ parser.add_argument(
+ "--window-size",
+ type=int_type(minimum=1, maximum=100_000, label="window-size"),
+ default=5,
+ )
+ parser.add_argument(
+ "--min-count",
+ type=int_type(minimum=1, maximum=10_000_000, label="min-count"),
+ default=10,
+ )
+ parser.add_argument(
+ "--batch-size",
+ type=int_type(minimum=1, maximum=1_000_000, label="batch-size"),
+ default=64,
+ )
+ parser.add_argument(
+ "--seed",
+ type=int_type(minimum=0, maximum=2**32 - 1, label="seed"),
+ default=42,
+ )
+ parser.add_argument("--pooling", choices=("mean", "max"), default="mean")
+ parser.add_argument("--geniml-version", default="0.8.4")
+ parser.add_argument("--gtars-version", default="0.9.2")
+ parser.add_argument(
+ "--max-file-bytes",
+ type=int_type(minimum=1, maximum=HARD_MAX_BYTES, label="max-file-bytes"),
+ default=2 * 1024**3,
+ )
+ parser.add_argument(
+ "--max-total-bytes",
+ type=int_type(minimum=1, maximum=HARD_MAX_BYTES, label="max-total-bytes"),
+ default=4 * 1024**3,
+ )
+ add_path_mode_argument(parser)
+ return parser
+
+
+def _parquet_magic(path: Path) -> bool:
+ flags = os.O_RDONLY
+ if hasattr(os, "O_NOFOLLOW"):
+ flags |= os.O_NOFOLLOW
+ descriptor = os.open(path, flags)
+ try:
+ info = os.fstat(descriptor)
+ if not stat.S_ISREG(info.st_mode) or info.st_size < 8:
+ return False
+ first = os.read(descriptor, 4)
+ os.lseek(descriptor, -4, os.SEEK_END)
+ last = os.read(descriptor, 4)
+ return first == b"PAR1" and last == b"PAR1"
+ finally:
+ os.close(descriptor)
+
+
+def _render(path: Path, label: str, mode: str) -> str:
+ if mode == "full":
+ return str(path)
+ if mode == "basename":
+ return path.name
+ return f"<{label}>"
+
+
+def plan(args: argparse.Namespace) -> tuple[dict, int]:
+ if not args.assembly.strip() or len(args.assembly) > 200:
+ raise SafetyError("assembly must be a nonempty value of at most 200 characters")
+ if args.mode != "bedspace" and (args.metadata or args.starspace_dir):
+ raise SafetyError("metadata and starspace-dir apply only to bedspace")
+ if args.mode == "bedspace" and not args.metadata:
+ raise SafetyError("bedspace requires --metadata")
+ if args.mode == "bedspace" and args.model_dir:
+ raise SafetyError("model-dir applies only to Region2Vec or scEmbed bundles")
+
+ data = local_path(args.data, kind="dir" if args.mode == "bedspace" else "file")
+ universe = local_path(args.universe, kind="file")
+ output_dir = local_path(args.output_dir, kind="dir")
+ model_dir = local_path(args.model_dir, kind="dir") if args.model_dir else None
+ metadata = local_path(args.metadata, kind="file") if args.metadata else None
+ starspace_dir = (
+ local_path(args.starspace_dir, kind="dir") if args.starspace_dir else None
+ )
+
+ errors: Counter[str] = Counter()
+ warnings: Counter[str] = Counter()
+ if args.split_unit == "cell":
+ errors["nonindependent_split_unit:cell"] += 1
+ elif args.split_unit == "sample":
+ warnings["verify_samples_are_independent_across_patients"] += 1
+ if any(output_dir.iterdir()):
+ warnings["output_directory_not_empty"] += 1
+
+ universe_digest, universe_size = sha256_file(
+ universe,
+ max_bytes=args.max_file_bytes,
+ )
+ input_summary: dict[str, object] = {
+ "data": display_path(data, 1, args.path_mode),
+ "universe": display_path(universe, 2, args.path_mode),
+ "universe_sha256": universe_digest,
+ "universe_size_bytes": universe_size,
+ "output_dir": display_path(output_dir, 3, args.path_mode),
+ }
+
+ if args.mode in {"region2vec", "scembed"}:
+ data_digest, data_size = sha256_file(data, max_bytes=args.max_file_bytes)
+ parquet_ok = _parquet_magic(data)
+ if not parquet_ok:
+ errors["data_not_parquet"] += 1
+ input_summary.update(
+ {
+ "data_sha256": data_digest,
+ "data_size_bytes": data_size,
+ "parquet_magic_valid": parquet_ok,
+ "parquet_schema_inspected": False,
+ }
+ )
+ else:
+ bed_entries = list(data.iterdir())
+ if len(bed_entries) > 100_000:
+ raise SafetyError("BEDspace input directory exceeds 100,000 entries")
+ symlinks = sum(1 for entry in bed_entries if entry.is_symlink())
+ special = sum(
+ 1
+ for entry in bed_entries
+ if not entry.is_symlink() and not entry.is_file()
+ )
+ if symlinks:
+ errors["bedspace_data_symlinks"] = symlinks
+ if special:
+ errors["bedspace_non_file_entries"] = special
+ total_bed_bytes = 0
+ oversized_beds = 0
+ for entry in bed_entries:
+ if entry.is_symlink() or not entry.is_file():
+ continue
+ size = entry.lstat().st_size
+ total_bed_bytes += size
+ if size > args.max_file_bytes:
+ oversized_beds += 1
+ if total_bed_bytes > args.max_total_bytes:
+ raise SafetyError("BEDspace input directory exceeds max-total-bytes")
+ if oversized_beds:
+ errors["bedspace_files_exceed_byte_limit"] = oversized_beds
+ metadata_digest, metadata_size = sha256_file(
+ metadata,
+ max_bytes=args.max_file_bytes,
+ )
+ input_summary.update(
+ {
+ "bed_directory_entries": len(bed_entries),
+ "bed_directory_bytes": total_bed_bytes,
+ "metadata": display_path(metadata, 4, args.path_mode),
+ "metadata_sha256": metadata_digest,
+ "metadata_size_bytes": metadata_size,
+ "metadata_values_emitted": False,
+ }
+ )
+
+ model_summary = None
+ if model_dir:
+ config = local_path(str(model_dir / "config.yaml"), kind="file")
+ checkpoint = local_path(str(model_dir / "checkpoint.pt"), kind="file")
+ bundle_universe = local_path(str(model_dir / "universe.bed"), kind="file")
+ config_metadata = simple_yaml_mapping(
+ config,
+ max_bytes=min(args.max_file_bytes, 16 * 1024**2),
+ )
+ config_digest, config_size = sha256_file(
+ config,
+ max_bytes=min(args.max_file_bytes, 16 * 1024**2),
+ )
+ checkpoint_digest, checkpoint_size = sha256_file(
+ checkpoint,
+ max_bytes=args.max_file_bytes,
+ )
+ bundle_digest, bundle_size = sha256_file(
+ bundle_universe,
+ max_bytes=args.max_file_bytes,
+ )
+ if bundle_digest != universe_digest:
+ errors["model_universe_checksum_mismatch"] += 1
+ if config_metadata.get("embedding_dim", config_metadata.get("embedding_size")) != (
+ args.embedding_dim
+ ):
+ warnings["planned_embedding_dim_differs_from_model"] += 1
+ model_summary = {
+ "path": display_path(model_dir, 5, args.path_mode),
+ "config_sha256": config_digest,
+ "config_size_bytes": config_size,
+ "checkpoint_sha256": checkpoint_digest,
+ "checkpoint_size_bytes": checkpoint_size,
+ "bundle_universe_sha256": bundle_digest,
+ "bundle_universe_size_bytes": bundle_size,
+ "metadata": {
+ "vocab_size": config_metadata.get("vocab_size"),
+ "embedding_dim": config_metadata.get(
+ "embedding_dim",
+ config_metadata.get("embedding_size"),
+ ),
+ "pooling_method": config_metadata.get("pooling_method"),
+ },
+ "deserialized": False,
+ }
+
+ stages: list[dict] = [
+ {
+ "stage": "environment",
+ "executed": False,
+ "requirements": [
+ f"geniml[ml]=={args.geniml_version}",
+ f"gtars=={args.gtars_version}",
+ ],
+ "instruction": "resolve with uv and retain the generated lockfile",
+ },
+ {
+ "stage": "data_contract",
+ "executed": False,
+ "instruction": (
+ "verify 0-based half-open coordinates, one assembly/chromosome-sizes "
+ "digest, bounded token counts, and patient/donor-grouped splits"
+ ),
+ },
+ ]
+
+ if args.mode == "region2vec":
+ stages.extend(
+ [
+ {
+ "stage": "api_smoke",
+ "executed": False,
+ "imports": [
+ "geniml.region2vec.main.Region2VecExModel",
+ "geniml.region2vec.utils.Region2VecDataset",
+ "gtars.tokenizers.Tokenizer",
+ ],
+ "note": "do not use the broken 0.8.4 top-level region2vec export",
+ },
+ {
+ "stage": "train",
+ "executed": False,
+ "parameters": {
+ "embedding_dim": args.embedding_dim,
+ "epochs": args.epochs,
+ "window_size": args.window_size,
+ "min_count": args.min_count,
+ "num_cpus": args.workers,
+ "seed": args.seed,
+ "pooling_method": args.pooling,
+ },
+ },
+ ]
+ )
+ elif args.mode == "scembed":
+ stages.extend(
+ [
+ {
+ "stage": "api_smoke",
+ "executed": False,
+ "imports": [
+ "geniml.scembed.main.ScEmbed",
+ "geniml.region2vec.utils.Region2VecDataset",
+ "gtars.tokenizers.Tokenizer",
+ ],
+ "note": (
+ "synthetically test ScEmbed.encode token shape with the pinned "
+ "Gtars version before real data"
+ ),
+ },
+ {
+ "stage": "train",
+ "executed": False,
+ "parameters": {
+ "embedding_dim": args.embedding_dim,
+ "epochs": args.epochs,
+ "window_size": args.window_size,
+ "min_count": args.min_count,
+ "num_cpus": args.workers,
+ "seed": args.seed,
+ "pooling_method": args.pooling,
+ },
+ },
+ ]
+ )
+ else:
+ starspace_summary = None
+ if starspace_dir:
+ binary = local_path(str(starspace_dir / "starspace"), kind="file")
+ binary_digest, binary_size = sha256_file(
+ binary,
+ max_bytes=args.max_file_bytes,
+ )
+ starspace_summary = {
+ "directory": display_path(starspace_dir, 6, args.path_mode),
+ "binary_sha256": binary_digest,
+ "binary_size_bytes": binary_size,
+ "executed": False,
+ }
+ else:
+ warnings["starspace_binary_required"] += 1
+ preprocess_template = [
+ "geniml",
+ "bedspace",
+ "preprocess",
+ "--input",
+ "<LOCAL_BED_DIRECTORY>",
+ "--metadata",
+ "<LOCAL_METADATA_CSV>",
+ "--universe",
+ "<LOCAL_UNIVERSE_BED>",
+ "--output",
+ "<LOCAL_OUTPUT_DIRECTORY_WITH_TRAILING_SEPARATOR>",
+ ]
+ train_template = [
+ "geniml",
+ "bedspace",
+ "train",
+ "-s",
+ "<LOCAL_STARSPACE_DIRECTORY>",
+ "--input",
+ "<LOCAL_OUTPUT_DIRECTORY>/train_input.txt",
+ "--output",
+ "<LOCAL_OUTPUT_DIRECTORY>",
+ "--dim",
+ str(args.embedding_dim),
+ "--epochs",
+ str(args.epochs),
+ ]
+ stages.extend(
+ [
+ {
+ "stage": "legacy_dependency_review",
+ "executed": False,
+ "starspace": starspace_summary,
+ "note": (
+ "StarSpace is archived; Geniml has no compatibility pin and "
+ "hard-codes 20 training threads"
+ ),
+ },
+ {
+ "stage": "preprocess",
+ "executed": False,
+ "argv_template": preprocess_template,
+ },
+ {
+ "stage": "train",
+ "executed": False,
+ "argv_template": train_template,
+ "note": "the 0.8.4 bedspace search dispatcher is not usable",
+ },
+ ]
+ )
+
+ stages.append(
+ {
+ "stage": "export_and_verify",
+ "executed": False,
+ "instruction": (
+ "export to a fresh directory, copy the exact universe.bed, create "
+ "SHA-256 provenance, and inspect without deserialization"
+ ),
+ "expected_local_output": _render(
+ output_dir,
+ "LOCAL_OUTPUT_DIRECTORY",
+ args.path_mode,
+ ),
+ }
+ )
+
+ report = {
+ "ok": not errors,
+ "ready_to_execute": not errors
+ and (args.mode != "bedspace" or starspace_dir is not None),
+ "tool": TOOL,
+ "contract": {
+ "mode": args.mode,
+ "assembly": args.assembly,
+ "coordinate_system": "0-based-half-open",
+ "split_unit": args.split_unit,
+ "network_used": False,
+ "packages_imported": False,
+ "models_deserialized": False,
+ "commands_executed": False,
+ },
+ "inputs": input_summary,
+ "existing_model": model_summary,
+ "errors": dict(sorted(errors.items())),
+ "warnings": dict(sorted(warnings.items())),
+ "stages": stages,
+ "required_postconditions": [
+ "token IDs are within the pinned tokenizer vocabulary",
+ "universe bytes/order and special-token IDs match model config",
+ "all output artifacts have provenance and SHA-256 checksums",
+ "evaluation uses patients/donors excluded from fitting and model selection",
+ "logs contain aggregate/redacted values only",
+ ],
+ }
+ return report, 0 if not errors else 2
+
+
+def main() -> int:
+ args = build_parser().parse_args()
+ try:
+ report, exit_code = plan(args)
+ print_json(report)
+ return exit_code
+ except (OSError, SafetyError, UnicodeError) as exc:
+ return fail_json(TOOL, exc)
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/skills/geniml/scripts/model_artifact_inspector.py b/skills/geniml/scripts/model_artifact_inspector.py
new file mode 100644
index 00000000..8488fde0
--- /dev/null
+++ b/skills/geniml/scripts/model_artifact_inspector.py
@@ -0,0 +1,358 @@
+#!/usr/bin/env python3
+"""Inspect local model artifacts and checksums without deserialization."""
+
+from __future__ import annotations
+
+import argparse
+import json
+import os
+import re
+import sys
+from collections import Counter
+from pathlib import Path
+
+from _common import (
+ HARD_MAX_BYTES,
+ HARD_MAX_FILES,
+ SafetyError,
+ add_path_mode_argument,
+ display_path,
+ fail_json,
+ int_type,
+ iter_text_lines,
+ local_path,
+ print_json,
+ sha256_file,
+ simple_yaml_mapping,
+)
+
+
+TOOL = "model-artifact-inspector"
+_HASH_LINE = re.compile(r"^([0-9a-fA-F]{64})[ \t]+[* ]?(.+?)$")
+_DESERIALIZATION_RISK = {
+ ".pt",
+ ".pth",
+ ".ckpt",
+ ".bin",
+ ".pkl",
+ ".pickle",
+ ".joblib",
+ ".model",
+}
+_NATIVE_RISK = {".so", ".dylib", ".dll", ".exe"}
+_ARCHIVE_RISK = {".tar", ".zip", ".tgz", ".gz", ".bz2", ".xz"}
+_METADATA_KEYS = {
+ "architectures",
+ "embedding_dim",
+ "embedding_size",
+ "model_type",
+ "pooling_method",
+ "vocab_size",
+}
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description=(
+ "Inventory and hash a local Geniml model bundle without importing "
+ "ML libraries, extracting archives, or deserializing model files."
+ )
+ )
+ parser.add_argument("--model-dir", required=True)
+ parser.add_argument(
+ "--verify-manifest",
+ help="Optional local SHA256SUMS-style manifest.",
+ )
+ parser.add_argument(
+ "--max-files",
+ type=int_type(minimum=1, maximum=HARD_MAX_FILES, label="max-files"),
+ default=1_000,
+ )
+ parser.add_argument(
+ "--max-file-bytes",
+ type=int_type(minimum=1, maximum=HARD_MAX_BYTES, label="max-file-bytes"),
+ default=2 * 1024**3,
+ )
+ parser.add_argument(
+ "--max-total-bytes",
+ type=int_type(minimum=1, maximum=HARD_MAX_BYTES, label="max-total-bytes"),
+ default=4 * 1024**3,
+ )
+ parser.add_argument(
+ "--max-output-files",
+ type=int_type(minimum=0, maximum=5_000, label="max-output-files"),
+ default=200,
+ )
+ add_path_mode_argument(parser)
+ return parser
+
+
+def _walk_regular_files(root: Path, max_files: int) -> list[Path]:
+ files: list[Path] = []
+ pending = [root]
+ while pending:
+ directory = pending.pop()
+ with os.scandir(directory) as entries:
+ ordered = sorted(entries, key=lambda entry: entry.name)
+ for entry in ordered:
+ path = Path(entry.path)
+ if entry.is_symlink():
+ raise SafetyError("symlink is not allowed in model bundle")
+ if entry.is_dir(follow_symlinks=False):
+ pending.append(path)
+ elif entry.is_file(follow_symlinks=False):
+ files.append(path)
+ if len(files) > max_files:
+ raise SafetyError("model bundle exceeds max-files")
+ else:
+ raise SafetyError("special filesystem entry is not allowed")
+ return sorted(files)
+
+
+def _risk_for(path: Path) -> str:
+ suffix = path.suffix.lower()
+ if suffix in _DESERIALIZATION_RISK:
+ return "deserialization"
+ if suffix in _NATIVE_RISK:
+ return "native-executable"
+ if suffix in _ARCHIVE_RISK:
+ return "archive-expansion"
+ if suffix == ".safetensors":
+ return "bounded-format-still-untrusted"
+ return "data-or-metadata"
+
+
+def _safe_json(path: Path, max_bytes: int):
+ lines = [
+ line
+ for _, line in iter_text_lines(
+ path,
+ max_bytes=max_bytes,
+ max_records=100_000,
+ )
+ ]
+ try:
+ return json.loads("\n".join(lines))
+ except json.JSONDecodeError as exc:
+ raise SafetyError("invalid JSON metadata") from exc
+
+
+def _read_checksum_manifest(
+ path: Path,
+ *,
+ max_bytes: int,
+ max_files: int,
+) -> dict[str, str]:
+ expected: dict[str, str] = {}
+ for line_number, line in iter_text_lines(
+ path,
+ max_bytes=max_bytes,
+ max_records=max_files + 100,
+ ):
+ if not line or line.startswith("#"):
+ continue
+ match = _HASH_LINE.fullmatch(line)
+ if not match:
+ raise SafetyError(f"invalid checksum manifest line {line_number}")
+ digest, name = match.groups()
+ relative = Path(name)
+ if (
+ relative.is_absolute()
+ or ".." in relative.parts
+ or not relative.parts
+ or "://" in name
+ or name.startswith("~")
+ ):
+ raise SafetyError(f"unsafe checksum path at line {line_number}")
+ normalized = relative.as_posix()
+ if normalized in expected:
+ raise SafetyError(f"duplicate checksum entry at line {line_number}")
+ expected[normalized] = digest.lower()
+ if len(expected) > max_files:
+ raise SafetyError("checksum manifest exceeds max-files")
+ return expected
+
+
+def _redacted_metadata(value) -> tuple[dict[str, object], int]:
+ if not isinstance(value, dict):
+ raise SafetyError("config metadata must be a mapping")
+ selected: dict[str, object] = {}
+ for key in _METADATA_KEYS:
+ if key not in value:
+ continue
+ item = value[key]
+ if isinstance(item, (str, int, float, bool)) or item is None:
+ if isinstance(item, str) and len(item) > 200:
+ selected[key] = "<redacted-long-string>"
+ else:
+ selected[key] = item
+ elif key == "architectures" and isinstance(item, list):
+ selected[key] = [
+ entry if isinstance(entry, str) and len(entry) <= 100 else "<redacted>"
+ for entry in item[:20]
+ ]
+ return selected, max(0, len(value) - len(selected))
+
+
+def inspect(args: argparse.Namespace) -> tuple[dict, int]:
+ model_dir = local_path(args.model_dir, kind="dir")
+ files = _walk_regular_files(model_dir, args.max_files)
+ if not files:
+ raise SafetyError("model directory is empty")
+
+ errors: Counter[str] = Counter()
+ warnings: Counter[str] = Counter()
+ risk_counts: Counter[str] = Counter()
+ computed: dict[str, str] = {}
+ summaries: list[dict] = []
+ metadata: dict[str, object] = {}
+ total_bytes = 0
+
+ for index, path in enumerate(files, start=1):
+ relative = path.relative_to(model_dir).as_posix()
+ size = path.lstat().st_size
+ if size > args.max_file_bytes:
+ errors["file_exceeds_byte_limit"] += 1
+ continue
+ total_bytes += size
+ if total_bytes > args.max_total_bytes:
+ raise SafetyError("model bundle exceeds max-total-bytes")
+ digest, hashed_size = sha256_file(path, max_bytes=args.max_file_bytes)
+ computed[relative] = digest
+ risk = _risk_for(path)
+ risk_counts[risk] += 1
+ if risk in {"deserialization", "native-executable", "archive-expansion"}:
+ warnings[f"risky_artifact:{risk}"] += 1
+
+ if path.name in {"config.yaml", "config.yml"}:
+ parsed = simple_yaml_mapping(
+ path,
+ max_bytes=min(args.max_file_bytes, 16 * 1024**2),
+ )
+ selected, omitted = _redacted_metadata(parsed)
+ metadata[path.name] = selected
+ if omitted:
+ warnings["config_metadata_fields_redacted"] += omitted
+ elif path.name in {"config.json", "tokenizer_config.json"}:
+ parsed = _safe_json(
+ path,
+ max_bytes=min(args.max_file_bytes, 16 * 1024**2),
+ )
+ selected, omitted = _redacted_metadata(parsed)
+ metadata[path.name] = selected
+ if omitted:
+ warnings["config_metadata_fields_redacted"] += omitted
+
+ if len(summaries) < args.max_output_files:
+ if args.path_mode == "redacted":
+ shown_path = f"artifact_{index:04d}"
+ elif args.path_mode == "basename":
+ shown_path = path.name
+ else:
+ shown_path = str(path)
+ summaries.append(
+ {
+ "path": shown_path,
+ "sha256": digest,
+ "size_bytes": hashed_size,
+ "risk_class": risk,
+ "deserialized": False,
+ }
+ )
+
+ standard = {
+ name: name in computed for name in ("checkpoint.pt", "config.yaml", "universe.bed")
+ }
+ for name, present in standard.items():
+ if not present:
+ errors[f"missing_standard_artifact:{name}"] += 1
+
+ verification = None
+ if args.verify_manifest:
+ manifest = local_path(args.verify_manifest, kind="file")
+ expected = _read_checksum_manifest(
+ manifest,
+ max_bytes=min(args.max_file_bytes, 64 * 1024**2),
+ max_files=args.max_files,
+ )
+ missing = sorted(set(expected) - set(computed))
+ unlisted = sorted(set(computed) - set(expected))
+ mismatched = sorted(
+ name
+ for name in set(expected) & set(computed)
+ if expected[name] != computed[name]
+ )
+ if missing:
+ errors["checksum_manifest_missing_files"] = len(missing)
+ if mismatched:
+ errors["checksum_mismatches"] = len(mismatched)
+ if unlisted:
+ warnings["files_not_listed_in_checksum_manifest"] = len(unlisted)
+ verification = {
+ "manifest": display_path(manifest, 2, args.path_mode),
+ "expected_entries": len(expected),
+ "missing_count": len(missing),
+ "mismatch_count": len(mismatched),
+ "unlisted_count": len(unlisted),
+ "verified": not missing and not mismatched,
+ }
+
+ config = metadata.get("config.yaml")
+ if isinstance(config, dict):
+ vocab_size = config.get("vocab_size")
+ embedding_dim = config.get("embedding_dim", config.get("embedding_size"))
+ if not isinstance(vocab_size, int) or vocab_size <= 0:
+ errors["config:invalid_vocab_size"] += 1
+ if not isinstance(embedding_dim, int) or embedding_dim <= 0:
+ errors["config:invalid_embedding_dim"] += 1
+ if "embedding_size" in config and "embedding_dim" not in config:
+ warnings["config:deprecated_embedding_size_key"] += 1
+
+ report = {
+ "ok": not errors,
+ "tool": TOOL,
+ "contract": {
+ "network_used": False,
+ "model_deserialized": False,
+ "archives_extracted": False,
+ "dynamic_imports_used": False,
+ "symlinks_allowed": False,
+ },
+ "model_dir": display_path(model_dir, 1, args.path_mode),
+ "summary": {
+ "file_count": len(files),
+ "hashed_file_count": len(computed),
+ "total_bytes": total_bytes,
+ "output_file_summaries": len(summaries),
+ "omitted_file_summaries": max(0, len(files) - len(summaries)),
+ "risk_classes": dict(sorted(risk_counts.items())),
+ "standard_artifacts": standard,
+ },
+ "metadata": metadata,
+ "checksum_verification": verification,
+ "errors": dict(sorted(errors.items())),
+ "warnings": dict(sorted(warnings.items())),
+ "files": summaries,
+ "loading_policy": [
+ "verify provenance and checksums before loading",
+ "never inspect .pt/.model/pickle/joblib by deserializing it",
+ "load only in an isolated environment with CPU, RAM, disk, and time bounds",
+ "compare universe bytes/order, tokenizer special IDs, vocab size, and assembly",
+ "treat native binaries and archives as independently untrusted",
+ ],
+ }
+ return report, 0 if not errors else 2
+
+
+def main() -> int:
+ args = build_parser().parse_args()
+ try:
+ report, exit_code = inspect(args)
+ print_json(report)
+ return exit_code
+ except (OSError, SafetyError, UnicodeError) as exc:
+ return fail_json(TOOL, exc)
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/skills/geniml/scripts/tokenizer_compatibility.py b/skills/geniml/scripts/tokenizer_compatibility.py
new file mode 100644
index 00000000..c87c9422
--- /dev/null
+++ b/skills/geniml/scripts/tokenizer_compatibility.py
@@ -0,0 +1,321 @@
+#!/usr/bin/env python3
+"""Plan local tokenizer/universe/model compatibility checks without imports."""
+
+from __future__ import annotations
+
+import argparse
+import os
+import stat
+import sys
+from collections import Counter
+from pathlib import Path
+
+from _common import (
+ HARD_MAX_BYTES,
+ HARD_MAX_RECORDS,
+ MAX_COORDINATE,
+ SafetyError,
+ add_path_mode_argument,
+ display_path,
+ fail_json,
+ int_type,
+ iter_text_lines,
+ local_path,
+ print_json,
+ sha256_file,
+ simple_yaml_mapping,
+)
+
+
+TOOL = "tokenizer-universe-compatibility"
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description=(
+ "Compare a local model bundle and universe using checksums and bounded "
+ "metadata only. No model is deserialized and no package is imported."
+ )
+ )
+ parser.add_argument("--model-dir", required=True, help="Local model bundle directory.")
+ parser.add_argument("--universe", required=True, help="Expected local universe BED.")
+ parser.add_argument("--assembly", required=True, help="Declared assembly/accession.")
+ parser.add_argument("--config-name", default="config.yaml")
+ parser.add_argument("--checkpoint-name", default="checkpoint.pt")
+ parser.add_argument("--bundle-universe-name", default="universe.bed")
+ parser.add_argument(
+ "--token-corpus",
+ help="Optional local Parquet token corpus to fingerprint (not parsed).",
+ )
+ parser.add_argument("--geniml-version", default="0.8.4")
+ parser.add_argument("--gtars-version", default="0.9.2")
+ parser.add_argument(
+ "--special-token-count",
+ type=int_type(minimum=0, maximum=100, label="special-token-count"),
+ default=7,
+ help="Expected Gtars special tokens (0.9.2 default: 7).",
+ )
+ parser.add_argument(
+ "--max-bytes",
+ type=int_type(minimum=1, maximum=HARD_MAX_BYTES, label="max-bytes"),
+ default=2 * 1024**3,
+ )
+ parser.add_argument(
+ "--max-universe-records",
+ type=int_type(
+ minimum=1,
+ maximum=HARD_MAX_RECORDS,
+ label="max-universe-records",
+ ),
+ default=5_000_000,
+ )
+ add_path_mode_argument(parser)
+ return parser
+
+
+def _safe_child(directory: Path, name: str) -> Path:
+ if not name or Path(name).name != name or name in {".", ".."}:
+ raise SafetyError("artifact names must be plain filenames")
+ return local_path(str(directory / name), kind="file")
+
+
+def _count_universe(
+ path: Path,
+ *,
+ max_bytes: int,
+ max_records: int,
+) -> tuple[int, int, Counter[str]]:
+ records = 0
+ duplicates = 0
+ issues: Counter[str] = Counter()
+ seen: set[tuple[str, int, int]] = set()
+ for _, line in iter_text_lines(
+ path,
+ max_bytes=max_bytes,
+ max_records=max_records,
+ ):
+ if not line or line.startswith("#") or line.startswith("track") or line.startswith("browser"):
+ continue
+ fields = line.split("\t")
+ if len(fields) < 3:
+ issues["fewer_than_three_columns"] += 1
+ continue
+ chrom = fields[0]
+ try:
+ start = int(fields[1])
+ end = int(fields[2])
+ except ValueError:
+ issues["non_integer_coordinate"] += 1
+ continue
+ if (
+ not chrom
+ or start < 0
+ or end <= start
+ or start > MAX_COORDINATE
+ or end > MAX_COORDINATE
+ ):
+ issues["invalid_interval"] += 1
+ continue
+ records += 1
+ key = (chrom, start, end)
+ if key in seen:
+ duplicates += 1
+ else:
+ seen.add(key)
+ return records, duplicates, issues
+
+
+def _parquet_magic(path: Path) -> bool:
+ flags = os.O_RDONLY
+ if hasattr(os, "O_NOFOLLOW"):
+ flags |= os.O_NOFOLLOW
+ descriptor = os.open(path, flags)
+ try:
+ mode = os.fstat(descriptor).st_mode
+ size = os.fstat(descriptor).st_size
+ if not stat.S_ISREG(mode) or size < 8:
+ return False
+ first = os.read(descriptor, 4)
+ os.lseek(descriptor, -4, os.SEEK_END)
+ last = os.read(descriptor, 4)
+ return first == b"PAR1" and last == b"PAR1"
+ finally:
+ os.close(descriptor)
+
+
+def inspect(args: argparse.Namespace) -> tuple[dict, int]:
+ if not args.assembly.strip() or len(args.assembly) > 200:
+ raise SafetyError("assembly must be a nonempty value of at most 200 characters")
+ model_dir = local_path(args.model_dir, kind="dir")
+ expected_universe = local_path(args.universe, kind="file")
+ config_path = _safe_child(model_dir, args.config_name)
+ checkpoint_path = _safe_child(model_dir, args.checkpoint_name)
+ bundle_universe = _safe_child(model_dir, args.bundle_universe_name)
+
+ errors: Counter[str] = Counter()
+ warnings: Counter[str] = Counter()
+ checks: list[dict[str, str | int | bool | None]] = []
+
+ expected_digest, expected_size = sha256_file(
+ expected_universe,
+ max_bytes=args.max_bytes,
+ )
+ bundle_digest, bundle_size = sha256_file(
+ bundle_universe,
+ max_bytes=args.max_bytes,
+ )
+ universe_rows, duplicate_rows, universe_issues = _count_universe(
+ expected_universe,
+ max_bytes=args.max_bytes,
+ max_records=args.max_universe_records,
+ )
+ for code, count in universe_issues.items():
+ errors[f"universe:{code}"] = count
+ if duplicate_rows:
+ errors["universe:duplicate_intervals"] = duplicate_rows
+ if universe_rows == 0:
+ errors["universe:no_valid_records"] += 1
+
+ universe_match = expected_digest == bundle_digest
+ if not universe_match:
+ errors["bundle_universe_checksum_mismatch"] += 1
+ checks.append(
+ {
+ "check": "exact_universe_bytes_and_order",
+ "passed": universe_match,
+ "expected_sha256": expected_digest,
+ "bundle_sha256": bundle_digest,
+ }
+ )
+
+ config = simple_yaml_mapping(
+ config_path,
+ max_bytes=min(args.max_bytes, 16 * 1024**2),
+ )
+ vocab_size = config.get("vocab_size")
+ embedding_dim = config.get("embedding_dim")
+ old_embedding_dim = config.get("embedding_size")
+ if embedding_dim is None and old_embedding_dim is not None:
+ embedding_dim = old_embedding_dim
+ warnings["deprecated_config_key:embedding_size"] += 1
+ if not isinstance(vocab_size, int) or vocab_size <= 0:
+ errors["config:invalid_vocab_size"] += 1
+ if not isinstance(embedding_dim, int) or embedding_dim <= 0:
+ errors["config:invalid_embedding_dim"] += 1
+ pooling = config.get("pooling_method")
+ if pooling is not None and pooling not in {"mean", "max"}:
+ errors["config:invalid_pooling_method"] += 1
+
+ expected_vocab_size = universe_rows + args.special_token_count
+ vocab_match = isinstance(vocab_size, int) and vocab_size == expected_vocab_size
+ if not vocab_match:
+ errors["model_tokenizer_vocab_size_mismatch"] += 1
+ checks.append(
+ {
+ "check": "vocab_size_equals_universe_plus_special_tokens",
+ "passed": vocab_match,
+ "model_vocab_size": vocab_size,
+ "universe_records": universe_rows,
+ "special_token_count": args.special_token_count,
+ "expected_vocab_size": expected_vocab_size,
+ }
+ )
+
+ config_digest, config_size = sha256_file(
+ config_path,
+ max_bytes=min(args.max_bytes, 16 * 1024**2),
+ )
+ checkpoint_digest, checkpoint_size = sha256_file(
+ checkpoint_path,
+ max_bytes=args.max_bytes,
+ )
+ if checkpoint_size == 0:
+ errors["checkpoint:empty"] += 1
+
+ token_corpus_summary = None
+ if args.token_corpus:
+ token_path = local_path(args.token_corpus, kind="file")
+ token_digest, token_size = sha256_file(token_path, max_bytes=args.max_bytes)
+ magic_ok = _parquet_magic(token_path)
+ if not magic_ok:
+ errors["token_corpus:invalid_parquet_magic"] += 1
+ token_corpus_summary = {
+ "path": display_path(token_path, 4, args.path_mode),
+ "sha256": token_digest,
+ "size_bytes": token_size,
+ "parquet_magic_valid": magic_ok,
+ "schema_inspected": False,
+ }
+
+ warnings["assembly_provenance_not_embedded_in_standard_config"] += 1
+ report = {
+ "ok": not errors,
+ "tool": TOOL,
+ "contract": {
+ "assembly": args.assembly,
+ "coordinate_system": "0-based-half-open",
+ "geniml_version": args.geniml_version,
+ "gtars_version": args.gtars_version,
+ "network_used": False,
+ "model_deserialized": False,
+ },
+ "paths": {
+ "model_dir": display_path(model_dir, 1, args.path_mode),
+ "expected_universe": display_path(expected_universe, 2, args.path_mode),
+ "bundle_universe": display_path(bundle_universe, 3, args.path_mode),
+ },
+ "artifacts": {
+ "checkpoint": {
+ "name": checkpoint_path.name,
+ "sha256": checkpoint_digest,
+ "size_bytes": checkpoint_size,
+ "deserialized": False,
+ },
+ "config": {
+ "name": config_path.name,
+ "sha256": config_digest,
+ "size_bytes": config_size,
+ "metadata": {
+ "vocab_size": vocab_size,
+ "embedding_dim": embedding_dim,
+ "pooling_method": pooling,
+ },
+ },
+ "expected_universe": {
+ "sha256": expected_digest,
+ "size_bytes": expected_size,
+ "records": universe_rows,
+ "duplicate_intervals": duplicate_rows,
+ },
+ "bundle_universe": {
+ "sha256": bundle_digest,
+ "size_bytes": bundle_size,
+ },
+ "token_corpus": token_corpus_summary,
+ },
+ "checks": checks,
+ "errors": dict(sorted(errors.items())),
+ "warnings": dict(sorted(warnings.items())),
+ "next_checks": [
+ "compare assembly accession and chromosome-sizes checksum",
+ "instantiate the pinned tokenizer only after this plan passes",
+ "verify every special-token name and ID in a synthetic smoke test",
+ "verify all token IDs are in range and Parquet has one list-valued tokens column",
+ "load the checkpoint only in an isolated, resource-bounded environment",
+ ],
+ }
+ return report, 0 if not errors else 2
+
+
+def main() -> int:
+ args = build_parser().parse_args()
+ try:
+ report, exit_code = inspect(args)
+ print_json(report)
+ return exit_code
+ except (OSError, SafetyError, UnicodeError) as exc:
+ return fail_json(TOOL, exc)
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/skills/geniml/tests/__init__.py b/skills/geniml/tests/__init__.py
new file mode 100644
index 00000000..0acf92c3
--- /dev/null
+++ b/skills/geniml/tests/__init__.py
@@ -0,0 +1 @@
+"""Tests for the Geniml skill's local helper CLIs."""
diff --git a/skills/geniml/tests/test_scripts.py b/skills/geniml/tests/test_scripts.py
new file mode 100644
index 00000000..30c0a3ee
--- /dev/null
+++ b/skills/geniml/tests/test_scripts.py
@@ -0,0 +1,366 @@
+"""Dependency-free synthetic tests for Geniml skill helper CLIs."""
+
+from __future__ import annotations
+
+import ast
+import json
+import subprocess
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+
+SKILL_ROOT = Path(__file__).resolve().parents[1]
+SCRIPTS = SKILL_ROOT / "scripts"
+CLI_NAMES = (
+ "bed_validator.py",
+ "corpus_auditor.py",
+ "tokenizer_compatibility.py",
+ "consensus_plan.py",
+ "model_artifact_inspector.py",
+ "embedding_plan.py",
+)
+
+
+def run_cli(name: str, *arguments: str, cwd: Path | None = None):
+ command = [sys.executable, "-B", str(SCRIPTS / name), *map(str, arguments)]
+ return subprocess.run(
+ command,
+ cwd=cwd,
+ text=True,
+ capture_output=True,
+ timeout=20,
+ check=False,
+ )
+
+
+def json_output(completed: subprocess.CompletedProcess[str]) -> dict:
+ try:
+ return json.loads(completed.stdout)
+ except json.JSONDecodeError as exc:
+ raise AssertionError(
+ f"invalid JSON\nstdout={completed.stdout}\nstderr={completed.stderr}"
+ ) from exc
+
+
+class ScriptHelpTests(unittest.TestCase):
+ def test_all_dependency_free_clis_have_help(self):
+ for name in CLI_NAMES:
+ with self.subTest(name=name):
+ completed = run_cli(name, "--help")
+ self.assertEqual(completed.returncode, 0, completed.stderr)
+ self.assertIn("usage:", completed.stdout.lower())
+
+ def test_scripts_parse_and_forbid_dangerous_capabilities(self):
+ forbidden_import_roots = {
+ "boto3",
+ "geniml",
+ "gtars",
+ "httpx",
+ "huggingface_hub",
+ "pickle",
+ "requests",
+ "socket",
+ "torch",
+ "urllib",
+ }
+ forbidden_calls = {"eval", "exec", "__import__"}
+ for path in sorted(SCRIPTS.glob("*.py")):
+ with self.subTest(path=path.name):
+ source = path.read_text(encoding="utf-8")
+ tree = ast.parse(source, filename=str(path))
+ imported_roots = set()
+ called_names = set()
+ for node in ast.walk(tree):
+ if isinstance(node, ast.Import):
+ imported_roots.update(
+ alias.name.split(".", 1)[0] for alias in node.names
+ )
+ elif isinstance(node, ast.ImportFrom) and node.module:
+ imported_roots.add(node.module.split(".", 1)[0])
+ elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
+ called_names.add(node.func.id)
+ self.assertFalse(imported_roots & forbidden_import_roots)
+ self.assertFalse(called_names & forbidden_calls)
+ self.assertNotIn("os.getenv", source)
+ self.assertNotIn("os.environ", source)
+
+
+class BedValidatorTests(unittest.TestCase):
+ def test_valid_bed_and_normalization_plan(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ (root / "chrom.sizes").write_text("chr1\t1000\nchr2\t500\n")
+ (root / "valid.bed").write_text(
+ "chr1\t0\t10\tpeak1\t0\t+\n"
+ "chr1\t20\t30\tpeak2\t0\t.\n"
+ "chr2\t1\t5\tpeak3\t0\t-\n"
+ )
+ completed = run_cli(
+ "bed_validator.py",
+ "--input",
+ "valid.bed",
+ "--assembly",
+ "GRCh38",
+ "--chrom-sizes",
+ "chrom.sizes",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 0, completed.stderr)
+ self.assertTrue(payload["ok"])
+ self.assertEqual(payload["summary"]["records"], 3)
+ self.assertEqual(payload["contract"]["coordinate_system"], "0-based-half-open")
+ self.assertFalse(payload["contract"]["input_mutated"])
+ self.assertNotIn(str(root), completed.stdout)
+
+ def test_negative_and_out_of_bounds_intervals_fail(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ (root / "chrom.sizes").write_text("chr1\t100\n")
+ (root / "invalid.bed").write_text("chr1\t-1\t10\nchr1\t90\t101\n")
+ completed = run_cli(
+ "bed_validator.py",
+ "--input",
+ "invalid.bed",
+ "--assembly",
+ "GRCh38",
+ "--chrom-sizes",
+ "chrom.sizes",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 2)
+ self.assertFalse(payload["ok"])
+ self.assertEqual(payload["errors"]["negative_coordinate"], 1)
+ self.assertEqual(payload["errors"]["end_beyond_contig"], 1)
+
+ def test_symlink_is_rejected(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ target = root / "target.bed"
+ target.write_text("chr1\t0\t10\n")
+ link = root / "link.bed"
+ try:
+ link.symlink_to(target)
+ except (OSError, NotImplementedError):
+ self.skipTest("symlinks unavailable")
+ completed = run_cli(
+ "bed_validator.py",
+ "--input",
+ "link.bed",
+ "--assembly",
+ "GRCh38",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 2)
+ self.assertFalse(payload["ok"])
+ self.assertIn("symlink", payload["message"].lower())
+
+ def test_url_is_rejected_without_network(self):
+ completed = run_cli(
+ "bed_validator.py",
+ "--input",
+ "https://example.invalid/private.bed",
+ "--assembly",
+ "GRCh38",
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 2)
+ self.assertFalse(payload["ok"])
+ self.assertIn("url", payload["message"].lower())
+ self.assertNotIn("private.bed", completed.stdout)
+
+
+class CorpusAuditorTests(unittest.TestCase):
+ def test_detects_patient_leakage_without_emitting_values(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ (root / "a.bed").write_text("chr1\t0\t10\n")
+ (root / "b.bed").write_text("chr1\t20\t30\n")
+ (root / "manifest.tsv").write_text(
+ "path\tassembly\tpatient_id\tsplit\n"
+ "a.bed\tGRCh38\tSECRET_PATIENT\ttrain\n"
+ "b.bed\tGRCh38\tSECRET_PATIENT\ttest\n"
+ )
+ completed = run_cli(
+ "corpus_auditor.py",
+ "--manifest",
+ "manifest.tsv",
+ "--group-column",
+ "patient_id",
+ "--split-column",
+ "split",
+ "--checksums",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 2)
+ self.assertEqual(
+ payload["leakage_checks"]["patient_id"]["groups_crossing_splits"],
+ 1,
+ )
+ self.assertNotIn("SECRET_PATIENT", completed.stdout)
+ self.assertNotIn("a.bed", completed.stdout)
+
+
+class ModelAndTokenizerTests(unittest.TestCase):
+ def _bundle(self, root: Path, universe_text: str = "chr1\t0\t10\nchr1\t10\t20\n"):
+ bundle = root / "model"
+ bundle.mkdir()
+ (bundle / "checkpoint.pt").write_bytes(
+ b"\x80\x04untrusted-placeholder-not-a-real-checkpoint"
+ )
+ (bundle / "config.yaml").write_text(
+ "vocab_size: 9\nembedding_dim: 100\npooling_method: mean\n"
+ )
+ (bundle / "universe.bed").write_text(universe_text)
+ return bundle
+
+ def test_model_inspector_never_deserializes_checkpoint(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ self._bundle(root)
+ completed = run_cli(
+ "model_artifact_inspector.py",
+ "--model-dir",
+ "model",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 0, completed.stderr)
+ self.assertTrue(payload["ok"])
+ self.assertFalse(payload["contract"]["model_deserialized"])
+ self.assertEqual(
+ payload["summary"]["risk_classes"]["deserialization"],
+ 1,
+ )
+ self.assertNotIn("untrusted-placeholder", completed.stdout)
+
+ def test_tokenizer_plan_matches_exact_bundle_universe(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ bundle = self._bundle(root)
+ expected = root / "expected.bed"
+ expected.write_text((bundle / "universe.bed").read_text())
+ completed = run_cli(
+ "tokenizer_compatibility.py",
+ "--model-dir",
+ "model",
+ "--universe",
+ "expected.bed",
+ "--assembly",
+ "GRCh38",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 0, completed.stderr)
+ self.assertTrue(payload["ok"])
+ self.assertEqual(payload["artifacts"]["config"]["metadata"]["vocab_size"], 9)
+ self.assertTrue(all(check["passed"] for check in payload["checks"]))
+
+ def test_tokenizer_plan_rejects_universe_mismatch(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ self._bundle(root)
+ (root / "expected.bed").write_text("chr1\t0\t11\nchr1\t10\t20\n")
+ completed = run_cli(
+ "tokenizer_compatibility.py",
+ "--model-dir",
+ "model",
+ "--universe",
+ "expected.bed",
+ "--assembly",
+ "GRCh38",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 2)
+ self.assertIn(
+ "bundle_universe_checksum_mismatch",
+ payload["errors"],
+ )
+
+
+class PlannerTests(unittest.TestCase):
+ def test_consensus_plan_emits_current_build_universe_command(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ (root / "a.bed").write_text("chr1\t0\t10\n")
+ (root / "manifest.tsv").write_text(
+ "path\tassembly\n"
+ "a.bed\tGRCh38\n"
+ )
+ (root / "chrom.sizes").write_text("chr1\t1000\n")
+ (root / "coverage").mkdir()
+ (root / "coverage" / "all_core.bw").write_bytes(b"synthetic-bigwig")
+ (root / "output").mkdir()
+ completed = run_cli(
+ "consensus_plan.py",
+ "--manifest",
+ "manifest.tsv",
+ "--chrom-sizes",
+ "chrom.sizes",
+ "--assembly",
+ "GRCh38",
+ "--method",
+ "cc",
+ "--cutoff",
+ "1",
+ "--coverage-dir",
+ "coverage",
+ "--output-dir",
+ "output",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 0, completed.stderr)
+ self.assertTrue(payload["ready_to_execute"])
+ build_stage = next(
+ stage for stage in payload["stages"] if stage["stage"] == "build_universe"
+ )
+ self.assertEqual(
+ build_stage["argv_template"][:3],
+ ["geniml", "build-universe", "cc"],
+ )
+ self.assertIsNone(build_stage["argv"])
+ self.assertFalse(payload["contract"]["commands_executed"])
+
+ def test_embedding_plan_accepts_bounded_synthetic_parquet_envelope(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ (root / "tokens.parquet").write_bytes(b"PAR1syntheticPAR1")
+ (root / "universe.bed").write_text("chr1\t0\t10\n")
+ (root / "output").mkdir()
+ completed = run_cli(
+ "embedding_plan.py",
+ "--mode",
+ "region2vec",
+ "--data",
+ "tokens.parquet",
+ "--universe",
+ "universe.bed",
+ "--output-dir",
+ "output",
+ "--assembly",
+ "GRCh38",
+ cwd=root,
+ )
+ payload = json_output(completed)
+ self.assertEqual(completed.returncode, 0, completed.stderr)
+ self.assertTrue(payload["ok"])
+ self.assertFalse(payload["contract"]["packages_imported"])
+ smoke = next(
+ stage for stage in payload["stages"] if stage["stage"] == "api_smoke"
+ )
+ self.assertIn(
+ "geniml.region2vec.main.Region2VecExModel",
+ smoke["imports"],
+ )
+ self.assertNotIn(str(root), completed.stdout)
+
+
+if __name__ == "__main__":
+ unittest.main()