Exercise: Parsing BED by hand

Goal

Write parse_bed_line(line: &str) -> Result<BedRecord, BedError>: parse one line of a BED file (chromosome, start, end — the three mandatory fields) into a struct. Then use it from main to parse a whole file.

This is the day’s warm-up: we get a real bioinformatics format flowing through Rust using only the standard library before we reach for external crates. On day 4 exercise 2 we will let noodles do the same thing for FASTA, and you will see what the crate is buying us.

Background

BED is tab-separated text, one interval per line. The first three columns are always:

chrom   start   end

start and end are half-open zero-based coordinates, as on day 3 exercise 1. Many real BED files have more columns (name, score, strand, …); we will ignore them today and just take the first three.

The exercise

cd day4/ex-bed-parse

Implement parse_bed_line. Make all six unit tests pass. The starter also includes a main that reads a file path from the CLI and prints a summary; once your function works, run it on the bundled data/sample.bed.

How to run

cargo test
cargo run -- data/sample.bed   # summary of intervals

Hints

str::split takes a delimiter and returns an iterator. Collect the first three pieces:

let mut parts = line.split('\t');
let chrom = parts.next().ok_or(BedError::TooFewFields)?;
let start_s = parts.next().ok_or(BedError::TooFewFields)?;
let end_s = parts.next().ok_or(BedError::TooFewFields)?;

Any extra columns are simply ignored — call parts.next() only as many times as you need.

Same pattern as day 3’s parse_region: use str::parse and Result::map_err to convert the parse error into our domain enum:

let start: u64 = start_s.parse().map_err(|_| BedError::InvalidStart)?;
let end:   u64 = end_s  .parse().map_err(|_| BedError::InvalidEnd)?;

BED allows end == start (a zero-length interval, sometimes used as an insertion point), but end < start is malformed:

if end < start {
    return Err(BedError::EndBeforeStart);
}
Ok(BedRecord { chrom: chrom.to_string(), start, end })

Solution

fn parse_bed_line(line: &str) -> Result<BedRecord, BedError> {
    let mut parts = line.split('\t');
    let chrom = parts.next().ok_or(BedError::TooFewFields)?;
    let start_s = parts.next().ok_or(BedError::TooFewFields)?;
    let end_s = parts.next().ok_or(BedError::TooFewFields)?;
    let start: u64 = start_s.parse().map_err(|_| BedError::InvalidStart)?;
    let end:   u64 = end_s  .parse().map_err(|_| BedError::InvalidEnd)?;
    if end < start {
        return Err(BedError::EndBeforeStart);
    }
    Ok(BedRecord { chrom: chrom.to_string(), start, end })
}

Full reference in day4/ex-bed-parse-solution/src/main.rs.

Where this leads

  • Exercise 02: same pattern, but for FASTA, with noodles-fasta handling the parsing. Notice how much less code is yours and how much comes from the crate.
  • A real BED parser would also accept noodles-bed, support BED12 (12-column variant), and parse strand. We leave those as natural extensions.