SCAN for sequence-to-function predictions.

Add uncertainty intervals to predictions from a DNA sequence model. Start with chromBPNet predictions for K562 ATAC-seq, then adapt the steps to your own model.

Run the sequence-to-function example

1. Install SCAN and download the data

git clone https://github.com/jzhoulab/SCAN.git
cd SCAN
mamba env create -f environment.yaml
mamba activate SCAN

This installs R, Stan, and the required packages. You can use conda instead of mamba. Download the two ENCODE tracks (about 2.7 GB):

bash scripts/download_encode_data.sh
File in data/encode/Meaning
ENCFF364BHF.bigWigK562 ATAC-seq counts predicted by chromBPNet, averaged across model folds.
ENCFF874FUM.bigWigObserved integer ATAC cut counts.

2. Match predictions to counts

Use hg38 chr10 to fit SCAN and chr8 to test it. These regions must also be excluded from the sequence model’s training. Keep only positions with predictions. Gaps in this observed-count track mean zero cuts.

Open the chromBPNet notebook from the SCAN root and select the R kernel:

jupyter lab notebooks/chrombpnet_scan_analysis.ipynb

Review the paths and chromosome settings in its configuration cell. Keep the notebook’s setup of repo_root and cache_dir:

# Review these assignments in the notebook's R configuration cell.
pred_bw <- file.path(repo_root, "data", "encode", "ENCFF364BHF.bigWig")
obs_bw  <- file.path(repo_root, "data", "encode", "ENCFF874FUM.bigWig")
stan_file <- file.path(repo_root, "nb_spline_binned.stan")
model_rds <- file.path(cache_dir, "chr10_stan_bsplinedisp_knots20.rds")
train_chrom <- "chr10"; train_len <- 133797422
eval_chrom  <- "chr8";  eval_len  <- 145138636

The notebook’s read_pred_obs()reader preserves each record’s full interval and pairs predictions with counts at 1-based positions. Missing predictions are excluded. For your own data, also exclude unmeasured regions; treat observed gaps as zero only when they mean measured zeros.

3. Fit SCAN on chr10

Pair each prediction with its observed count. Use counts directly: this model predicts from DNA sequence, so there are no input reads to subtract.

Run the notebook cells in order through “Train the SCAN model”. The fitting cell uses four chains of 1,000 iterations each and saves the posterior to model_rds. It translates the Stan array syntax in memory for current compilers. Use a new RDS path when data or settings change, because an existing fit is reused.

Check the fitted model

The CLI writes a posterior RDS file, binned_data_summary.csv, fit_summary.csv, and pred_vs_obs.pdf. Check Rhat (ideally below 1.01), effective sample sizes, and warnings about divergences or tree depth. Inspect the fitted curves, especially where high-signal counts are sparse.

The notebooks use SCAN’s nb_spline_binned.stan model and save the posterior to model_rds. Inspect summary(fit) and rstan::get_sampler_params(fit) in the fitting cell before it removes the fit. Use a new output directory or RDS path when data, splits, or settings change.

4. Check the intervals on chr8

Continue through loading the fitted model and chr8 tracks, then run the interval and calibration figure cells. Use scale = 1 because calibration and test counts have the same read depth.

A well-calibrated 90% interval should contain about 90% of the test counts. Keep observed zeros and exclude predictions outside the fitted range before calculating coverage. Report which positions were tested and how many were excluded.

Quiet regions can hide poor results in active ones. Check several signal thresholds and interval levels, chosen in advance, using the Figure 2 calibration cell.

Inspect the MYC enhancer in Figure 3. Figure 1 compares count intervals with intervals for the underlying signal; use the count intervals for coverage checks.

What the intervals mean

How SCAN estimates uncertainty

SCAN learns the average observed count and its spread at each prediction level. Smooth curves let both change with signal strength. A negative-binomial model allows counts to vary more than Poisson sampling alone would predict.

Model equations and fitting details

Here, p is the prediction, y the observed count, μ the mean, and φ the dispersion. Smaller φ means wider intervals at the same mean.

z = log(p + 1e-9)
log μ(p) = I-spline(z) · β_mean
log φ(p) = B-spline(z) · β_dispersion
y | p ~ NegativeBinomial(mean = μ, size = φ)
Variance(y | p) = μ + μ² / φ

Fitting groups similar prediction values to reduce computation. These groups can span distant genomic positions. The mean curve is not constrained to increase.

Choose the interval for your question

Both include uncertainty in the fitted model. Check coverage on independent counts: a label such as “90%” is a target, not a guarantee.

Stay within the fitted range

Exclude predictions outside the range shared by the fitted mean and dispersion curves before calculating intervals or coverage. Report exclusions, especially in high-signal regions; extrapolating the dispersion curve can produce misleading bounds.

Adjust for read depth

Set the notebook’s scale argument to test depth divided by calibration-target depth. Evaluate both curves at the original prediction, scale the fitted mean, and keep dispersion fixed. Check coverage again after changing depth.

What calibration changes

SCAN adjusts how predictions map to counts and estimates uncertainty. The sequence model stays fixed. Good count coverage does not validate the model’s predicted variant effects or biological explanations.

Use your own model

Prepare matching counts

Pair each prediction with an observed count for the same base, bin, or region. Match coordinates, strand, channel, and read depth. Prepare your model’s output as follows:

Model outputPreparation for count calibration
Counts per basePair with the integer count at that base and channel.
Profile probabilities and total countsConvert to expected counts using the model’s documented normalization and count transform.
Counts per bin or regionSum observed counts over the same unit. Keep one row per unit.
Log-counts or transformed countsApply the model’s documented inverse transform. Set the scale using training or calibration data.
Fractions, probabilities, or normalized signalsChoose a different statistical model suited to these values.

Keep measured zeros and exclude unmeasured regions. Calibrate tissues, tasks, strands, and resolutions separately unless you have evidence they can share a fit. Decide how to combine overlapping predictions before pairing them with counts.

Fit and test your data

Adapt the notebook’s data-loading cells to separate calibration and test tables. Each needs a prediction, an integer observed count, and coordinates. Remove missing or unmeasured rows and keep a record of exclusions. Pass the paired prediction and count arrays to the notebook’s fitting block, then apply its interval and coverage calculations to the test arrays.

Split by chromosome or well-separated regions so nearby windows stay together. Keep test regions out of both model training and SCAN fitting. Choose transforms and settings using calibration data before checking test coverage.

Adjust the notebook’s scale argument when test read depth differs, then check coverage on independent counts. A new tissue or assay chemistry may need a new calibration.

Adapt with a coding agent

The SCAN Agent Skill explains the fitting and evaluation steps. Ask your coding agent to follow it:

Example adaptation prompt
Read .cursor/skill.md. Add SCAN intervals to [model] predictions of [output type] for [assay] at [resolution]. Match predictions to integer counts and check whether the count model fits my data. Use separate calibration and test regions excluded from model training. Check excluded positions, fit diagnostics, and coverage in quiet and active regions. Export intervals with coordinates and settings.

Save the model version, data preparation, region splits, fitted SCAN model, and coverage results. Label exported intervals with their type, level, depth scale, coordinates, and exclusions.

Continue with the chromBPNet analysis notebook

The chromBPNet notebook includes plots of intervals, coverage, the MYC region, and dispersion. Run the remaining cells in order to generate the full set of figures.

Set the track paths, chromosome lengths, and region coordinates. The default fit is data/chrombpnet_cache/chr10_stan_bsplinedisp_knots20.rds under the SCAN root. Keep model_rds pointing to the fit for the data and settings you intend to evaluate.

PDFs go in notebooks/pdfs/chrombpnet/. fig1b_predicted_intervals_chr10.pdf shows the fitting chromosome; use chr8 to test coverage. Save diagnostics before the fit is removed. Exclude missing predictions and values outside the fitted range from coverage checks and plots.