Skip to main content

GenomicInterval

Struct GenomicInterval 

Source
pub struct GenomicInterval {
    pub chrom: String,
    pub start: u64,
    pub end: u64,
}
Expand description

A genomic interval representing a region on a chromosome

Uses 0-based, half-open [start, end) coordinates:

  • start is inclusive (0-based)
  • end is exclusive (0-based)

This matches BED format and is the standard for genomic intervals.

Fields§

§chrom: String

Chromosome/contig name (e.g., “chr1”, “chrX”, “1”)

§start: u64

Start position (0-based, inclusive)

§end: u64

End position (0-based, exclusive)

Implementations§

Source§

impl GenomicInterval

Source

pub fn new<S: Into<String>>(chrom: S, start: u64, end: u64) -> Self

Create a new genomic interval

§Arguments
  • chrom - Chromosome name
  • start - Start position (0-based, inclusive)
  • end - End position (0-based, exclusive)
§Panics

Panics if start > end

§Example
use genomicframe_core::interval::GenomicInterval;

let interval = GenomicInterval::new("chr1", 1000, 2000);
assert_eq!(interval.len(), 1000);
Source

pub fn try_new<S: Into<String>>(chrom: S, start: u64, end: u64) -> Result<Self>

Try to create a new genomic interval, returning an error if invalid

This is the fallible version of new().

Source

pub fn point<S: Into<String>>(chrom: S, pos: u64) -> Self

Create a point interval (length 1) at the given position

Useful for representing SNPs or single positions.

Source

pub fn len(&self) -> u64

Get the length of this interval

Returns end - start.

Source

pub fn is_empty(&self) -> bool

Check if this is an empty interval (start == end)

Source

pub fn is_point(&self) -> bool

Check if this interval is a point (length 1)

Source

pub fn overlaps(&self, other: &GenomicInterval) -> bool

Check if two intervals overlap (share at least one base)

Returns false if intervals are on different chromosomes.

§Example
use genomicframe_core::interval::GenomicInterval;

let a = GenomicInterval::new("chr1", 100, 200);
let b = GenomicInterval::new("chr1", 150, 250);
assert!(a.overlaps(&b));

let c = GenomicInterval::new("chr1", 200, 300);
assert!(!a.overlaps(&c)); // Adjacent but not overlapping
Source

pub fn contains(&self, other: &GenomicInterval) -> bool

Check if this interval completely contains another interval

Returns false if intervals are on different chromosomes.

§Example
use genomicframe_core::interval::GenomicInterval;

let a = GenomicInterval::new("chr1", 100, 300);
let b = GenomicInterval::new("chr1", 150, 200);
assert!(a.contains(&b));
assert!(!b.contains(&a));
Source

pub fn contains_pos(&self, chrom: &str, pos: u64) -> bool

Check if this interval contains a specific position

§Example
use genomicframe_core::interval::GenomicInterval;

let interval = GenomicInterval::new("chr1", 100, 200);
assert!(interval.contains_pos("chr1", 150));
assert!(!interval.contains_pos("chr1", 200)); // end is exclusive
Source

pub fn overlap_length(&self, other: &GenomicInterval) -> u64

Compute the length of overlap between two intervals

Returns 0 if intervals don’t overlap or are on different chromosomes.

§Example
use genomicframe_core::interval::GenomicInterval;

let a = GenomicInterval::new("chr1", 100, 200);
let b = GenomicInterval::new("chr1", 150, 250);
assert_eq!(a.overlap_length(&b), 50);
Source

pub fn intersect(&self, other: &GenomicInterval) -> Option<GenomicInterval>

Compute the intersection of two intervals

Returns None if intervals don’t overlap or are on different chromosomes.

§Example
use genomicframe_core::interval::GenomicInterval;

let a = GenomicInterval::new("chr1", 100, 200);
let b = GenomicInterval::new("chr1", 150, 250);
let intersection = a.intersect(&b).unwrap();
assert_eq!(intersection.start, 150);
assert_eq!(intersection.end, 200);
Source

pub fn union(&self, other: &GenomicInterval) -> Option<GenomicInterval>

Compute the union (span) of two intervals

Returns the smallest interval that contains both intervals. Returns None if intervals are on different chromosomes.

Note: This may include regions not covered by either original interval.

§Example
use genomicframe_core::interval::GenomicInterval;

let a = GenomicInterval::new("chr1", 100, 200);
let b = GenomicInterval::new("chr1", 300, 400);
let union = a.union(&b).unwrap();
assert_eq!(union.start, 100);
assert_eq!(union.end, 400);
assert_eq!(union.len(), 300); // Includes gap!
Source

pub fn distance(&self, other: &GenomicInterval) -> Option<u64>

Compute the distance between two intervals

Returns:

  • Some(0) if intervals overlap
  • Some(n) where n > 0 is the gap size between intervals
  • None if intervals are on different chromosomes
§Example
use genomicframe_core::interval::GenomicInterval;

let a = GenomicInterval::new("chr1", 100, 200);
let b = GenomicInterval::new("chr1", 250, 300);
assert_eq!(a.distance(&b), Some(50)); // 250 - 200 = 50

let c = GenomicInterval::new("chr1", 150, 250);
assert_eq!(a.distance(&c), Some(0)); // Overlapping
Source

pub fn expand(&self, amount: u64) -> GenomicInterval

Expand this interval by a fixed amount on both sides

Start is reduced by amount, end is increased by amount. Start is clamped to 0 (won’t go negative).

§Example
use genomicframe_core::interval::GenomicInterval;

let interval = GenomicInterval::new("chr1", 100, 200);
let expanded = interval.expand(50);
assert_eq!(expanded.start, 50);
assert_eq!(expanded.end, 250);
Source

pub fn shrink(&self, amount: u64) -> Option<GenomicInterval>

Shrink this interval by a fixed amount on both sides

Returns None if shrinking would make the interval invalid.

§Example
use genomicframe_core::interval::GenomicInterval;

let interval = GenomicInterval::new("chr1", 100, 200);
let shrunk = interval.shrink(25).unwrap();
assert_eq!(shrunk.start, 125);
assert_eq!(shrunk.end, 175);
Source

pub fn to_bed_string(&self) -> String

Convert to BED format string (0-based, tab-separated)

§Example
use genomicframe_core::interval::GenomicInterval;

let interval = GenomicInterval::new("chr1", 100, 200);
assert_eq!(interval.to_bed_string(), "chr1\t100\t200");
Source

pub fn from_bed_string(s: &str) -> Result<Self>

Parse from BED format string (0-based, tab-separated)

§Example
use genomicframe_core::interval::GenomicInterval;

let interval = GenomicInterval::from_bed_string("chr1\t100\t200").unwrap();
assert_eq!(interval.chrom, "chr1");
assert_eq!(interval.start, 100);
assert_eq!(interval.end, 200);

Trait Implementations§

Source§

impl Clone for GenomicInterval

Source§

fn clone(&self) -> GenomicInterval

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GenomicInterval

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for GenomicInterval

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&BamRecord> for GenomicInterval

Source§

fn from(record: &BamRecord) -> Self

Converts to this type from the input type.
Source§

impl From<&BedRecord> for GenomicInterval

Source§

fn from(record: &BedRecord) -> Self

Converts to this type from the input type.
Source§

impl From<&GffRecord> for GenomicInterval

Source§

fn from(record: &GffRecord) -> Self

Converts to this type from the input type.
Source§

impl From<&SamRecord> for GenomicInterval

Source§

fn from(record: &SamRecord) -> Self

Converts to this type from the input type.
Source§

impl From<&VcfRecord> for GenomicInterval

Source§

fn from(record: &VcfRecord) -> Self

Converts to this type from the input type.
Source§

impl From<BamRecord> for GenomicInterval

Source§

fn from(record: BamRecord) -> Self

Converts to this type from the input type.
Source§

impl From<BedRecord> for GenomicInterval

Source§

fn from(record: BedRecord) -> Self

Converts to this type from the input type.
Source§

impl From<GffRecord> for GenomicInterval

Source§

fn from(record: GffRecord) -> Self

Converts to this type from the input type.
Source§

impl From<SamRecord> for GenomicInterval

Source§

fn from(record: SamRecord) -> Self

Converts to this type from the input type.
Source§

impl From<VcfRecord> for GenomicInterval

Source§

fn from(record: VcfRecord) -> Self

Converts to this type from the input type.
Source§

impl Hash for GenomicInterval

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for GenomicInterval

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for GenomicInterval

Source§

fn eq(&self, other: &GenomicInterval) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for GenomicInterval

Helper for sorting intervals

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for GenomicInterval

Source§

impl StructuralPartialEq for GenomicInterval

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.