Genomic Data Smoothing

LOWESS for methylation profiles, ChIP-seq signals, and other genomic data.

Overview

Genomic data often contains noise from sequencing depth variation, PCR artifacts, or biological heterogeneity. LOWESS smoothing helps reveal underlying patterns.


Methylation Profile Smoothing

The Challenge

DNA methylation data (from bisulfite sequencing or arrays) shows position-dependent patterns that can be obscured by measurement noise.

Solution

A small fraction = 0.1 lets LOWESS follow fine-scale spatial structure without smearing the transitions between methylated and unmethylated regions. IntervalsOptions.builder().confidence(0.95) produces uncertainty bands that naturally widen at positions with sparser CpG coverage, making low-confidence segments immediately apparent in the plot.

import fastlowess.IntervalsOptions;
import fastlowess.Lowess;
import fastlowess.Options;
import fastlowess.Result;

public class Example {
    public static void main(String[] args) {
        int n = 100;
        double[] positions = new double[n];
        double[] observed = new double[n];
        for (int i = 0; i < n; i++) {
            positions[i] = i * 1000.0;
            observed[i] = 50.0 + Math.sin(positions[i] / 1000.0) * 20.0 + 5.0;
        }

        Options options = Options.builder()
                .fraction(0.1)
                .iterations(3)
                .intervals(IntervalsOptions.builder().confidence(0.95).build())
                .build();

        try (Lowess model = new Lowess(options)) {
            Result result = model.fit(positions, observed);

            double[] lower = result.confidenceLower().orElseThrow();
            double[] upper = result.confidenceUpper().orElseThrow();
            System.out.printf("95%% CI: [%s, %s]%n", lower[0], upper[0]);
        }
    }
}
95% CI: [53.27919505033735, 67.15544006032717]

ChIP-seq Signal Smoothing

Application

ChIP-seq experiments produce sparse, noisy coverage data. LOWESS can help identify binding regions.

fraction = 0.05 provides high spatial resolution — important for resolving narrow binding peaks that would otherwise be smeared into the background. The larger iterations = 5 is deliberate: Poisson-distributed read counts produce tall, isolated spikes, and extra robustness iterations progressively down-weight them so the estimated background level is not inflated by a handful of extreme counts.

import fastlowess.Lowess;
import fastlowess.Options;
import fastlowess.Result;

public class Example {
    public static void main(String[] args) {
        int n = 100;
        double[] positions = new double[n];
        double[] observed = new double[n];
        for (int i = 0; i < n; i++) {
            positions[i] = i * 1000.0;
            observed[i] = 50.0 + Math.sin(positions[i] / 1000.0) * 20.0 + 5.0;
        }

        Options options = Options.builder()
                .fraction(0.05)
                .iterations(5)
                .build();

        try (Lowess model = new Lowess(options)) {
            Result result = model.fit(positions, observed);

            int peakCount = 0;
            for (double y : result.y()) {
                if (y > 65.0) peakCount++;
            }
            System.out.println("y[0]: " + result.y()[0]);
            System.out.println("Peak count: " + peakCount);
        }
    }
}
y[0]: 59.937594476093395
Peak count: 26

Large Genome Coverage (Streaming)

For whole-genome data that doesn’t fit in memory:

import fastlowess.Result;
import fastlowess.StreamingLowess;
import fastlowess.StreamingOptions;

public class Example {
    public static void main(String[] args) {
        int n = 1001;
        double[] xChunk = new double[n];
        double[] yChunk = new double[n];
        for (int i = 0; i < n; i++) {
            xChunk[i] = i * 10.0;
            yChunk[i] = 50.0 + Math.sin(xChunk[i] / 100.0) * 20.0 + 5.0;
        }

        StreamingOptions options = StreamingOptions.builder()
                .fraction(0.05)
                .iterations(3)
                .chunkSize(50)
                .overlap(10)
                .mergeStrategy("weighted_average")
                .build();

        try (StreamingLowess model = new StreamingLowess(options)) {
            model.processChunk(xChunk, yChunk);
            Result result = model.finish();
            System.out.println("y[0]: " + result.y()[0]);
        }
    }
}
y[0]: 41.28390556612269

Best Practices for Genomic Data

Consideration Recommendation

Fraction

0.05–0.15 (preserve local features)

Iterations

3–5 (handle sequencing outliers)

Large data

Use streaming mode

Sparse regions

Use boundaryPolicy = "extend"

Multiple chromosomes

Process separately or ensure sorted


See Also