Exercise: A genomic interval struct

Goal

Define your first Rust struct and enum:

enum Strand { Plus, Minus }

struct GenomicInterval {
    chrom: String,
    start: u64,
    end: u64,
    strand: Strand,
}

Then implement two methods on the struct:

  • length(&self) -> u64 — the size of the interval in base pairs.
  • overlaps(&self, other: &GenomicInterval) -> bool — whether two intervals overlap, treating coordinates as half-open and respecting chromosome.

Background

A genomic interval is the most basic spatial data type in bioinformatics. Every BED record, every gene annotation, every variant call ultimately resolves to “chromosome X, position a to position b, on this strand”. Coordinates are half-open: an interval [100, 200) covers 100 base pairs and starts at position 100, but does not include position 200. The interval [200, 300) starts where the previous one ends and they do not overlap.

A horizontal number line from 0 to 300 with ticks every 50. A filled black dot marks position 100 labelled start = 100 (inclusive); an open circle marks position 200 labelled end = 200 (exclusive). A light blue bar above the line spans 100 to 200 and is labelled length = end − start = 100. An arrow to the right of position 200 notes that the next interval [200, 300) starts there with no overlap and no gap.
Figure 1: Half-open intervals (the BED/UCSC convention): the start position is included (filled dot) and the end position is excluded (open dot), so the length is simply end − start and the next interval [200, 300) abuts this one without overlap or gap.

We will build BED parsing on top of this struct on day 4.

The exercise

cd day3/ex-genomic-interval

Define Strand, GenomicInterval, and the two methods. Make all six tests pass.

How to run

cargo test
cargo run -- chr1 1000 2000 +    # prints length and a self-overlap check

Hints

A C-style enum lists its variants with no associated data. Derive Debug, Clone, Copy, and PartialEq so it can be printed, copied, and compared:

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Strand {
    Plus,
    Minus,
}

Copy is appropriate here because Strand is a single byte tag — copying it is free. Larger types should usually be Clone only. (See the Rust Book on Copy vs Clone.)

A struct bundles named fields. Derive Debug, Clone, and PartialEq (but not Copy — String is not Copy):

#[derive(Debug, Clone, PartialEq, Eq)]
struct GenomicInterval {
    chrom: String,
    start: u64,
    end: u64,
    strand: Strand,
}

Methods live inside an impl block. The first argument is one of self, &self, or &mut self depending on whether the method consumes, reads, or modifies the value:

impl GenomicInterval {
    fn length(&self) -> u64 {
        self.end - self.start
    }

    fn overlaps(&self, other: &GenomicInterval) -> bool {
        // TODO
    }
}

The half-open overlap rule: two intervals [a, b) and [c, d) overlap iff a < d && c < b. (Adjacency — where b == c — is not overlap.) And of course they must be on the same chromosome.

Solution

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Strand {
    Plus,
    Minus,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct GenomicInterval {
    chrom: String,
    start: u64,
    end: u64,
    strand: Strand,
}

impl GenomicInterval {
    fn length(&self) -> u64 {
        self.end - self.start
    }

    fn overlaps(&self, other: &GenomicInterval) -> bool {
        self.chrom == other.chrom
            && self.start < other.end
            && other.start < self.end
    }
}

Full reference in day3/ex-genomic-interval-solution/src/main.rs.

Where this leads

  • Next exercise (02): parse strings like "chr1:1000-2000" into the bare components of an interval, with a custom error type for malformed input.
  • Day 4: read a real BED file (one interval per line) into a Vec<GenomicInterval>, find overlaps, and write the result back out.