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:
startis inclusive (0-based)endis exclusive (0-based)
This matches BED format and is the standard for genomic intervals.
Fields§
§chrom: StringChromosome/contig name (e.g., “chr1”, “chrX”, “1”)
start: u64Start position (0-based, inclusive)
end: u64End position (0-based, exclusive)
Implementations§
Source§impl GenomicInterval
impl GenomicInterval
Sourcepub fn new<S: Into<String>>(chrom: S, start: u64, end: u64) -> Self
pub fn new<S: Into<String>>(chrom: S, start: u64, end: u64) -> Self
Create a new genomic interval
§Arguments
chrom- Chromosome namestart- 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);Sourcepub fn try_new<S: Into<String>>(chrom: S, start: u64, end: u64) -> Result<Self>
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().
Sourcepub fn point<S: Into<String>>(chrom: S, pos: u64) -> Self
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.
Sourcepub fn overlaps(&self, other: &GenomicInterval) -> bool
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 overlappingSourcepub fn contains(&self, other: &GenomicInterval) -> bool
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));Sourcepub fn contains_pos(&self, chrom: &str, pos: u64) -> bool
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 exclusiveSourcepub fn overlap_length(&self, other: &GenomicInterval) -> u64
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);Sourcepub fn intersect(&self, other: &GenomicInterval) -> Option<GenomicInterval>
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);Sourcepub fn union(&self, other: &GenomicInterval) -> Option<GenomicInterval>
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!Sourcepub fn distance(&self, other: &GenomicInterval) -> Option<u64>
pub fn distance(&self, other: &GenomicInterval) -> Option<u64>
Compute the distance between two intervals
Returns:
Some(0)if intervals overlapSome(n)where n > 0 is the gap size between intervalsNoneif 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)); // OverlappingSourcepub fn expand(&self, amount: u64) -> GenomicInterval
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);Sourcepub fn shrink(&self, amount: u64) -> Option<GenomicInterval>
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);Sourcepub fn to_bed_string(&self) -> String
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");Sourcepub fn from_bed_string(s: &str) -> Result<Self>
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
impl Clone for GenomicInterval
Source§fn clone(&self) -> GenomicInterval
fn clone(&self) -> GenomicInterval
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GenomicInterval
impl Debug for GenomicInterval
Source§impl Display for GenomicInterval
impl Display for GenomicInterval
Source§impl From<&BamRecord> for GenomicInterval
impl From<&BamRecord> for GenomicInterval
Source§impl From<&BedRecord> for GenomicInterval
impl From<&BedRecord> for GenomicInterval
Source§impl From<&GffRecord> for GenomicInterval
impl From<&GffRecord> for GenomicInterval
Source§impl From<&SamRecord> for GenomicInterval
impl From<&SamRecord> for GenomicInterval
Source§impl From<&VcfRecord> for GenomicInterval
impl From<&VcfRecord> for GenomicInterval
Source§impl From<BamRecord> for GenomicInterval
impl From<BamRecord> for GenomicInterval
Source§impl From<BedRecord> for GenomicInterval
impl From<BedRecord> for GenomicInterval
Source§impl From<GffRecord> for GenomicInterval
impl From<GffRecord> for GenomicInterval
Source§impl From<SamRecord> for GenomicInterval
impl From<SamRecord> for GenomicInterval
Source§impl From<VcfRecord> for GenomicInterval
impl From<VcfRecord> for GenomicInterval
Source§impl Hash for GenomicInterval
impl Hash for GenomicInterval
Source§impl Ord for GenomicInterval
impl Ord for GenomicInterval
Source§impl PartialEq for GenomicInterval
impl PartialEq for GenomicInterval
Source§impl PartialOrd for GenomicInterval
Helper for sorting intervals
impl PartialOrd for GenomicInterval
Helper for sorting intervals
impl Eq for GenomicInterval
impl StructuralPartialEq for GenomicInterval
Auto Trait Implementations§
impl Freeze for GenomicInterval
impl RefUnwindSafe for GenomicInterval
impl Send for GenomicInterval
impl Sync for GenomicInterval
impl Unpin for GenomicInterval
impl UnsafeUnpin for GenomicInterval
impl UnwindSafe for GenomicInterval
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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