# Overlap, gap and relation of two genomic intervals

`genome_interval_overlap` · Runs: On the server · Level: Checked against a reference: bedtools 2.31.1 intersect and closest -d

Works out the overlap, gap, distance and relation of two genomic intervals on one contig.

Research and education software; not medical advice, and not for diagnosis or treatment decisions. It computes from the inputs it receives. It does not check the person's data, design or sample.

[Catalogue](https://www.drybench.com/mcp/catalogue) / [Genomics](https://www.drybench.com/mcp/catalogue/genomics)

[Ask your assistant](https://www.drybench.com/mcp/connect)

```cite
Overlap, gap and relation of two genomic intervals (genome_interval_overlap), Drybench MCP server 1.0.0; genome-interval-overlap@1.0.2 (Checked against a reference); https://www.drybench.com/mcp/tools/genome-interval-overlap
```

## What it is

Returns the overlap, gap, end-to-start distance and relation of two genomic intervals on one contig. Use when asking whether a read, peak or variant falls in a feature, or how far apart two features lie. Not for one interval in another convention: use \`genome\_coordinate\_convert\`. System and strand are required on each interval; refuses different contigs and zero-length intervals; a strand rule is reported, not applied.

Field: [Sequencing and genomics](https://www.drybench.com/mcp/catalogue/genomics).

## What it does

**You give**

- `a` (required): Interval a.
- `b` (required): Interval b.
- `strandRule`: same or opposite: reported as satisfied or not; the overlap is never changed. Leave out to ignore strand. Optional.

No request is recorded for this tool.

**Limits**

- Research arithmetic for planning sequencing libraries and runs and for genomic coordinate bookkeeping; it does arithmetic on the inputs it receives, and does not measure the library, the instrument or the data.
- Two intervals on one contig; no distance across contigs; zero-length sites are refused; strand is ignored unless a rule is stated, and a rule never changes the geometric overlap; \`endToStartDistance\` is \`gapBases\` + 1 for separate intervals, the convention of \`bedtools closest -d\` 2.31.1, measured 10 November 2026.
- Every computed number a reply returns, other than an integer or an exact decimal, is rounded once to 12 significant figures: to the nearest, a change of at most 5 × 10⁻¹² of its value, except that a lower bound is rounded down and an upper bound up, a change of less than 1 × 10⁻¹¹; the method computes, and decides any pass or fail, on the unrounded value.

**It will not answer when**

- the intervals are on different contigs: there is no distance between chromosomes.
- an interval has zero length.
- a strand rule is stated but an interval's strand is unstranded or unknown.
- a coordinate is above 2^53 − 1 = 9007199254740991, the largest n for which n and n + 1 are both exact.

### Every input, with its units

3 inputs

- `a` (required): Interval a.
- `b` (required): Interval b.
- `strandRule`: same or opposite: reported as satisfied or not; the overlap is never changed. Leave out to ignore strand. Optional.

### Every refusal, with its code

4 refusals

| Code | When |
| --- | --- |
| `DIFFERENT_CONTIGS` | the intervals are on different contigs: there is no distance between chromosomes. |
| `ZERO_LENGTH_USE_SITE` | an interval has zero length. |
| `STRAND_RULE_NEEDS_STRANDS` | a strand rule is stated but an interval's strand is unstranded or unknown. |
| `COORDINATE_ABOVE_RANGE` | a coordinate is above 2^53 − 1 = 9007199254740991, the largest n for which n and n + 1 are both exact. |

## The calculation

The method's equations, constants and worked example.

### [Overlap, gap and relation of two genomic intervals](https://www.drybench.com/methods/m/genome-interval-overlap/1.0.2)

`genome-interval-overlap@1.0.2`

**Equation 1**

```text
overlap = max(0, min(e_a, e_b) − max(s_a, s_b))
gapBases = max(0, max(s_a, s_b) − min(e_a, e_b)): the standard definitions of the shared and intervening lengths of two half-open intervals
SAMv1 is cited for the 0-based half-open coordinate system only, and holds no overlap or gap formula
```

| Symbol | Meaning |
| --- | --- |
| `s, e` | half-open start and end of each interval |
| `overlap, gapBases` | bases shared by the intervals, and bases between them, bp |

Source 1.

**Equation 2**

```text
endToStartDistance = 0 if overlap > 0, else gapBases + 1: Drybench's convention, matching bedtools closest -d 2.31.1 as measured
relation is a's relation to b, decided in this order, names and precedence the record's own: equal (same start and end), contains (a holds b, ends may be shared), within (b holds a, ends may be shared), overlap (overlap > 0 and neither holds the other), book-ended (a's end is b's start or b's end is a's start, half-open), separate (otherwise)
checks per interval, in this order: a coordinate above 2^53 − 1 as given, before any conversion (COORDINATE_ABOVE_RANGE), then in 1-based closed a start below 1 or an end before the start, and in 0-based half-open a start below 0 or an end before the start (the kit's OUT_OF_RANGE: a zero-length interval cannot be written 1-based closed, so [s, s − 1] is declined as invalid input (OUT_OF_RANGE), not read as a site
ask genome_coordinate_convert for sites), then a half-open start equal to its end (ZERO_LENGTH_USE_SITE)
```

| Symbol | Meaning |
| --- | --- |
| `endToStartDistance` | distance from the earlier interval's end to the later one's start, 0 when they overlap, bp |

Source not recorded.

A definition, not a model: the equation states what the quantity means. A different convention gives a different number.

**Worked example**

Worked example: one case of genome-interval-overlap\@1.0.2, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.

**Inputs**

- a.contig = toy
- a.system = 0-based-half-open
- a.start = 10
- a.end = 20
- a.strand = +
- b.contig = toy
- b.system = 0-based-half-open
- b.start = 21
- b.end = 30
- b.strand = +

**Steps**

1. gap = 1 bp, from a.end, b.start; by equation 1; gives result.gapBases.
2. d = 2 bp, from gap; by equation 2; gives result.endToStartDistance.

The working, as the case writes it:

```text
a = [10,20), b = [21,30) half-open
overlap = max(0, 20 - 21) = 0
gap = max(0, 21 - 20) = 1
relation separate
```

**Answer**

- result.gapBases = 1 bp
- result.endToStartDistance = 2 bp

Where these values came from: an outside program, bedtools 2.31.1; overlapBases: bedtools 2.31.1 intersect -wo ('no record'); endToStartDistance: bedtools closest -d = 2; gapBases: exact formula max(0, max(s) - min(e)), agreeing with d - 1; relation: worked by hand.

At build the tool returned this answer within the tolerance the file states: exact: bases, distances, relations, withheld rules, statuses and codes.

Who worked this case: not recorded.

## Sources

1. Sequence Alignment/Map Format Specification v1.6, samtools/hts-specs, SAMv1.tex Used for: Overlap, gap and relation of two genomic intervals. At: section 1.2 Terminologies and Concepts: the 1-based and 0-based coordinate systems (the 3rd to 7th bases are \[3, 7\] 1-based and \[2, 7) 0-based). **Read in full, 11 November 2026**

Read by AI-assisted sessions. No person has checked these readings.

## How it is checked

**Level:** Checked against a reference.

**Checked against:** An outside program, bedtools, 2.31.1; our own exact-arithmetic script and a hand-worked case.

**Cases:** No case set is linked to this version.

**Not checked:** Sets of intervals (merge, subtract); upstream and downstream signs (bedtools -D); bedtools versions other than 2.31.1.

No independent scientist has yet reviewed this.

### What this project's records still lack

1 thing

- The source's location for each equation for genome-interval-overlap\@1.0.2.

### Changes to its methods

0 changes

No change is logged for these method versions.
