pub struct Interval<S: System> { /* private fields */ }Expand description
An interval.
Implementations§
Source§impl Interval<Base>
impl Interval<Base>
Sourcepub fn into_equivalent_interbase(self) -> Interval<Interbase>
pub fn into_equivalent_interbase(self) -> Interval<Interbase>
Consumes self and returns the equivalent interbase interval.
§Examples
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
let interval = "seq0:+:1-1000".parse::<Interval<Base>>()?;
let equivalent = interval.into_equivalent_interbase();
assert_eq!("seq0:+:0-1000".parse::<Interval<Interbase>>()?, equivalent);
Source§impl Interval<Interbase>
impl Interval<Interbase>
Sourcepub fn contains_next_entity(&self, coordinate: Coordinate) -> Option<bool>
pub fn contains_next_entity(&self, coordinate: Coordinate) -> Option<bool>
Checks whether the interbase interval contains the entity after the specified interbase coordinate.
This method returns an Option because the next coordinate may or may
not be a valid position—if you’d like to handle that separately, you can
do so with the check on the option.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Interbase;
let interval = "seq0:+:10-1000".parse::<Interval<Interbase>>()?;
assert!(
!interval
.contains_next_entity("seq0:+:9".parse::<Coordinate<Interbase>>()?)
.unwrap()
);
assert!(
interval
.contains_next_entity("seq0:+:10".parse::<Coordinate<Interbase>>()?)
.unwrap()
);
assert!(
interval
.contains_next_entity("seq0:+:999".parse::<Coordinate<Interbase>>()?)
.unwrap()
);
assert!(
!interval
.contains_next_entity("seq0:+:1000".parse::<Coordinate<Interbase>>()?)
.unwrap()
);
Sourcepub fn contains_prev_entity(&self, coordinate: Coordinate) -> Option<bool>
pub fn contains_prev_entity(&self, coordinate: Coordinate) -> Option<bool>
Checks whether the interbase interval contains the entity before the specified interbase coordinate.
This method returns an Option because the next coordinate may or may
not be a valid position—if you’d like to handle that separately, you can
do so with the check on the option.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Interbase;
let interval = "seq0:+:10-1000".parse::<Interval<Interbase>>()?;
assert!(
!interval
.contains_prev_entity("seq0:+:10".parse::<Coordinate<Interbase>>()?)
.unwrap()
);
assert!(
interval
.contains_prev_entity("seq0:+:11".parse::<Coordinate<Interbase>>()?)
.unwrap()
);
assert!(
interval
.contains_prev_entity("seq0:+:1000".parse::<Coordinate<Interbase>>()?)
.unwrap()
);
assert!(
!interval
.contains_prev_entity("seq0:+:1001".parse::<Coordinate<Interbase>>()?)
.unwrap()
);
Sourcepub fn into_equivalent_base(self) -> Interval<Base>
pub fn into_equivalent_base(self) -> Interval<Base>
Consumes self and returns the equivalent in-base interval.
§Examples
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
let interval = "seq0:+:0-1000".parse::<Interval<Interbase>>()?;
let equivalent = interval.into_equivalent_base();
assert_eq!("seq0:+:1-1000".parse::<Interval<Base>>()?, equivalent);
Source§impl<S: System> Interval<S>
impl<S: System> Interval<S>
Sourcepub fn try_new(start: Coordinate<S>, end: Coordinate<S>) -> Result<Interval<S>>
pub fn try_new(start: Coordinate<S>, end: Coordinate<S>) -> Result<Interval<S>>
Creates a new interval if the following invariants are upheld.
- The contigs of the two coordinates must match.
- If this does not hold, a
NonsensicalError::MismatchedContigswill be returned.
- If this does not hold, a
- The strands of the two coordinates must match.
- If this does not hold, a
NonsensicalError::MismatchedStrandswill be returned.
- If this does not hold, a
- The start must come before or be equal to the end in that (a) on
positive strand,
start <= end, or, (b) on the negative strand,end <= start. This ensures that the interval is always oriented from start to end of the molecule.- If this does not hold, a
NonsensicalError::NegativelySizedwill be returned.
- If this does not hold, a
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
// Positive strand.
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end)?;
// Negative strand.
let start = Coordinate::<Interbase>::try_new("seq0", "-", 20)?;
let end = Coordinate::<Interbase>::try_new("seq0", "-", 10)?;
let interval = Interval::try_new(start, end)?;
//======//
// Base //
//======//
// Positive strand.
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end)?;
// Negative strand.
let start = Coordinate::<Base>::try_new("seq0", "-", 20)?;
let end = Coordinate::<Base>::try_new("seq0", "-", 10)?;
let interval = Interval::try_new(start, end)?;
Sourcepub fn start(&self) -> &Coordinate<S>
pub fn start(&self) -> &Coordinate<S>
Gets a reference to the start coordinate.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start.clone(), end)?;
assert_eq!(interval.start(), &start);
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start.clone(), end)?;
assert_eq!(interval.start(), &start);
Sourcepub fn into_start(self) -> Coordinate<S>
pub fn into_start(self) -> Coordinate<S>
Consumes self and returns the start coordinate.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start.clone(), end)?;
assert_eq!(interval.into_start(), start);
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start.clone(), end)?;
assert_eq!(interval.into_start(), start);
Sourcepub fn end(&self) -> &Coordinate<S>
pub fn end(&self) -> &Coordinate<S>
Gets a reference to the end coordinate.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end.clone())?;
assert_eq!(interval.end(), &end);
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end.clone())?;
assert_eq!(interval.end(), &end);
Sourcepub fn into_end(self) -> Coordinate<S>
pub fn into_end(self) -> Coordinate<S>
Consumes self and returns the end coordinate.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end.clone())?;
assert_eq!(interval.into_end(), end);
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end.clone())?;
assert_eq!(interval.into_end(), end);
Sourcepub fn into_coordinates(self) -> (Coordinate<S>, Coordinate<S>)
pub fn into_coordinates(self) -> (Coordinate<S>, Coordinate<S>)
Consumes self and returns the start and end coordinates.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start.clone(), end.clone())?;
let parts = interval.into_coordinates();
assert_eq!(parts.0, start);
assert_eq!(parts.1, end);
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start.clone(), end.clone())?;
let parts = interval.into_coordinates();
assert_eq!(parts.0, start);
assert_eq!(parts.1, end);
Sourcepub fn contig(&self) -> &Contig
pub fn contig(&self) -> &Contig
Returns a reference to the contig.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end)?;
assert_eq!(interval.contig().as_str(), "seq0");
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end)?;
assert_eq!(interval.contig().as_str(), "seq0");
Sourcepub fn strand(&self) -> Strand
pub fn strand(&self) -> Strand
Returns the strand.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::Strand;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end)?;
assert_eq!(interval.strand(), Strand::Positive);
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "-", 20)?;
let end = Coordinate::<Base>::try_new("seq0", "-", 10)?;
let interval = Interval::try_new(start, end)?;
assert_eq!(interval.strand(), Strand::Negative);
Sourcepub fn contains_coordinate(&self, coordinate: &Coordinate<S>) -> bool
pub fn contains_coordinate(&self, coordinate: &Coordinate<S>) -> bool
Returns whether or not a coordinate is contained within this interval. Notably, when checked whether coordinates are included in the interval, both the start and end positions are considered inclusive.
§Caution
**This is not the method you want to use when checking if a nucleotide or amino acid at a particular position is included in the interval. This checks the coordinates themselves and, in-so-doing, considers both the start and the end positions of the interval to be inclusive.
If you’d like to check whether a particular nucleotide, amino acid, or
other entity is contained within the interval, use the
contains_entity() method.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::Strand;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 0)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let interval = Interval::try_new(start, end)?;
// Coordinates on the same contig, strand, and within the interval's range
// are contained within the interval.
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 0)?));
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 5)?));
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 10)?));
// Coordinates on different contigs, strands, or outside the range are
// not contained within the interval.
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq1", "+", 5)?));
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq0", "-", 5)?));
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 11)?));
//======//
// Base //
//======//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 1)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let interval = Interval::try_new(start, end)?;
// Coordinates on the same contig, strand, and within the interval's range
// are contained within the interval.
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 1)?));
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 5)?));
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 10)?));
// Coordinates on different contigs, strands, or outside the range are
// not contained within the interval.
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq1", "+", 5)?));
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq0", "-", 5)?));
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 11)?));
Sourcepub fn contains_entity(&self, coordinate: &Coordinate<Base>) -> bool
pub fn contains_entity(&self, coordinate: &Coordinate<Base>) -> bool
Returns whether or not the entity at the in-base coordinate is contained within this interval.
/// # Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::Strand;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 0)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let interval = Interval::try_new(start, end)?;
// Coordinates on the same contig, strand, and within the interval's range
// are contained within the interval.
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 0)?));
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 5)?));
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 10)?));
// Coordinates on different contigs, strands, or outside the range are
// not contained within the interval.
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq1", "+", 5)?));
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq0", "-", 5)?));
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 11)?));
//======//
// Base //
//======//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 1)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let interval = Interval::try_new(start, end)?;
// Coordinates on the same contig, strand, and within the interval's range
// are contained within the interval.
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 1)?));
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 5)?));
assert!(interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 10)?));
// Coordinates on different contigs, strands, or outside the range are
// not contained within the interval.
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq1", "+", 5)?));
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq0", "-", 5)?));
assert!(!interval.contains_coordinate(&Coordinate::try_new("seq0", "+", 11)?));
Sourcepub fn count_entities(&self) -> Number
pub fn count_entities(&self) -> Number
Counts the number of entities in the interval.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
// Positive strand.
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end)?;
assert_eq!(interval.count_entities(), 10);
// Negative strand.
let start = Coordinate::<Interbase>::try_new("seq0", "-", 20)?;
let end = Coordinate::<Interbase>::try_new("seq0", "-", 10)?;
let interval = Interval::try_new(start, end)?;
assert_eq!(interval.count_entities(), 10);
//======//
// Base //
//======//
// Positive strand.
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end)?;
assert_eq!(interval.count_entities(), 11);
// Negative strand.
let start = Coordinate::<Base>::try_new("seq0", "-", 20)?;
let end = Coordinate::<Base>::try_new("seq0", "-", 10)?;
let interval = Interval::try_new(start, end)?;
assert_eq!(interval.count_entities(), 11);
Sourcepub fn clamp(self, interval: Interval<S>) -> Result<Interval<S>>
pub fn clamp(self, interval: Interval<S>) -> Result<Interval<S>>
Consumes self and clamps an interval by another interval.
Clamping is an operation whereby the ends of an interval are restricted to the range of the argument passed in with a tendency to restrict towards the middle of the interval.
§Summary
- If the interval being operated on is completely contained within the argument interval, the interval being operated on is returned.
╔═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════ →
10 11 12 13 14 15 16 17 18 19 20 |
●───────────────────────● [13, 17] | Original Interval
●───────────────────────────────────────────────● [11, 19] | Argument Interval
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
●───────────────────────● [13, 17] | Resulting Interval
Here, no modifications were made to the original interval, as neither
the start nor the end of the interval would be restricted by the
argument interval.- If the argument interval is completely within the interval being operated on, the argument interval will clamp both sides of the original interval, and the argument interval will be returned.
╔═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════ →
10 11 12 13 14 15 16 17 18 19 20 |
●───────────────────────────────────────────────● [11, 19] | Original Interval
●───────────────────────● [13, 17] | Argument Interval
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
●───────────────────────● [13, 17] | Resulting Interval
Here, both the start and the end position of the original interval were
restricted by the start and end of the argument interval respectively.- If the argument interval would restrict the length of one side of the subject interval on either end, that end is restricted to the argument interval’s value, whereas the non-restricted end is the original interval’s value.
╔═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════ →
10 11 12 13 14 15 16 17 18 19 20 |
●───────────────────────────────────● [11, 17] | Original Interval
●───────────────────────● [13, 17] | Argument Interval
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
●───────────────────────● [13, 17] | Resulting Interval
Here, the start of the original interval is clamped by the argument
interval's start position. However, the end position of the original
interval is not restricted by the argument interval's end position,
so it remains the same. This results in the latter half of the interval
being clamped.
╔═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════╦═════ →
10 11 12 13 14 15 16 17 18 19 20 |
●───────────────────────────────────● [13, 19] | Original Interval
●───────────────────────● [13, 17] | Argument Interval
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
●───────────────────────● [13, 17] | Resulting Interval
Here, the start position of the original interval would not be
restricted by the argument interval's start position, so it remains
the same. However, the end position is clamped by the end position
of the argument interval, so the resulting end position is that of the
argument interval's end position. This results in the first half of
interval being clamped.§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let interval = "seq0:+:10-20".parse::<Interval<Interbase>>()?;
let clamped = interval.clamp("seq0:+:5-15".parse::<Interval<Interbase>>()?)?;
assert_eq!(clamped, "seq0:+:10-15".parse::<Interval<Interbase>>()?);
//======//
// Base //
//======//
let interval = "seq0:-:20-10".parse::<Interval<Base>>()?;
let clamped = interval.clamp("seq0:-:25-15".parse::<Interval<Base>>()?)?;
assert_eq!(clamped, "seq0:-:20-15".parse::<Interval<Base>>()?);
Ok::<(), Box<dyn std::error::Error>>(())Sourcepub fn coordinate_offset(&self, coordinate: &Coordinate<S>) -> Option<Number>
pub fn coordinate_offset(&self, coordinate: &Coordinate<S>) -> Option<Number>
Gets the offset of a coordinate from the start of the interval.
If the coordinate is not contained within the interval, None is
returned.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let interval = Interval::try_new(start, end)?;
let query = Coordinate::<Interbase>::try_new("seq0", "+", 15)?;
assert_eq!(interval.coordinate_offset(&query).unwrap(), 5);
let query = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
assert_eq!(interval.coordinate_offset(&query).unwrap(), 10);
let query = Coordinate::<Interbase>::try_new("seq0", "+", 21)?;
assert!(interval.coordinate_offset(&query).is_none());
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "-", 20)?;
let end = Coordinate::<Base>::try_new("seq0", "-", 10)?;
let interval = Interval::try_new(start, end)?;
let query = Coordinate::<Base>::try_new("seq0", "-", 15)?;
assert_eq!(interval.coordinate_offset(&query).unwrap(), 5);
let query = Coordinate::<Base>::try_new("seq0", "-", 10)?;
assert_eq!(interval.coordinate_offset(&query).unwrap(), 10);
let query = Coordinate::<Base>::try_new("seq0", "-", 9)?;
assert!(interval.coordinate_offset(&query).is_none());
Ok::<(), Box<dyn std::error::Error>>(())Sourcepub fn coordinate_at_offset(&self, offset: Number) -> Option<Coordinate<S>>
pub fn coordinate_at_offset(&self, offset: Number) -> Option<Coordinate<S>>
Returns the coordinate at the offset within the interval.
This method only returns the coordinate if the coordinate falls within the interval.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
// Positive strand.
let interval = "seq0:+:0-1000".parse::<Interval<Interbase>>()?;
let expected = "seq0:+:5".parse::<Coordinate<Interbase>>()?;
assert_eq!(interval.coordinate_at_offset(5).unwrap(), expected);
let expected = "seq0:+:1000".parse::<Coordinate<Interbase>>()?;
assert_eq!(interval.coordinate_at_offset(1000).unwrap(), expected);
assert!(interval.coordinate_at_offset(1001).is_none());
// Negative strand.
let interval = "seq0:-:1000-0".parse::<Interval<Interbase>>()?;
let expected = "seq0:-:995".parse::<Coordinate<Interbase>>()?;
assert_eq!(interval.coordinate_at_offset(5).unwrap(), expected);
let expected = "seq0:-:0".parse::<Coordinate<Interbase>>()?;
assert_eq!(interval.coordinate_at_offset(1000).unwrap(), expected);
assert_eq!(interval.coordinate_at_offset(1001), None);
//======//
// Base //
//======//
// Positive strand.
let interval = "seq0:+:1-1000".parse::<Interval<Base>>()?;
let expected = "seq0:+:6".parse::<Coordinate<Base>>()?;
assert_eq!(interval.coordinate_at_offset(5).unwrap(), expected);
let expected = "seq0:+:1000".parse::<Coordinate<Base>>()?;
assert_eq!(interval.coordinate_at_offset(999).unwrap(), expected);
assert!(interval.coordinate_at_offset(1000).is_none());
// Negative strand.
let interval = "seq0:-:1000-1".parse::<Interval<Base>>()?;
let expected = "seq0:-:995".parse::<Coordinate<Base>>()?;
assert_eq!(interval.coordinate_at_offset(5).unwrap(), expected);
let expected = "seq0:-:1".parse::<Coordinate<Base>>()?;
assert_eq!(interval.coordinate_at_offset(999).unwrap(), expected);
assert_eq!(interval.coordinate_at_offset(1000), None);
Ok::<(), Box<dyn std::error::Error>>(())Sourcepub fn reverse_complement(self) -> Interval<S>
pub fn reverse_complement(self) -> Interval<S>
Reverse complements the interval, meaning that:
- the start and end positions are swapped, and
- the strand is swapped.
§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::system::Base;
use omics_coordinate::system::Interbase;
//===========//
// Interbase //
//===========//
let start = Coordinate::<Interbase>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Interbase>::try_new("seq0", "+", 20)?;
let original = Interval::try_new(start, end)?;
let complemented = original.clone().reverse_complement();
assert_eq!(complemented, "seq0:-:20-10".parse::<Interval<Interbase>>()?);
let recomplemented = complemented.reverse_complement();
assert_eq!(recomplemented, original);
//======//
// Base //
//======//
let start = Coordinate::<Base>::try_new("seq0", "+", 10)?;
let end = Coordinate::<Base>::try_new("seq0", "+", 20)?;
let original = Interval::try_new(start, end)?;
let complemented = original.clone().reverse_complement();
assert_eq!(complemented, "seq0:-:20-10".parse::<Interval<Base>>()?);
let recomplemented = complemented.reverse_complement();
assert_eq!(recomplemented, original);
Ok::<(), Box<dyn std::error::Error>>(())