omics_coordinate

Struct Coordinate

source
pub struct Coordinate<S: System> { /* private fields */ }
Expand description

A coordinate within a genome consisting of a contig, a strand, and a position.

Implementations§

source§

impl Coordinate<Zero>

source

pub fn lower_bound<C: TryInto<Contig>>(contig: C) -> Self
where <C as TryInto<Contig>>::Error: Error,

Creates a new Coordinate with a Value::LowerBound.

Note that a lower bound position can only sit on the Strand::Negative, so this hardcodes the strand to Strand::Negative.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Strand;
use omics_coordinate::system::Zero;

let coordinate = Coordinate::<Zero>::lower_bound("seq0");
source§

impl<S: System> Coordinate<S>

source

pub fn try_new<C: TryInto<Contig>, Q: TryInto<Strand>, P: TryInto<Position<S>>>( contig: C, strand: Q, position: P, ) -> Result<Self>
where <C as TryInto<Contig>>::Error: Error, <Q as TryInto<Strand>>::Error: Error, <P as TryInto<Position<S>>>::Error: Error,

Attempts to create a new Coordinate.

Note that a lower bound position can only sit on the Strand::Negative, so trying to create a Coordinate with a lower bound position on any non-negative strand will result in an Error::LowerBoundOnNonNegativeStrand.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Strand;
use omics_coordinate::system::Zero;

let coordinate = Coordinate::<Zero>::try_new("seq0", "+", 1)?;
source

pub fn contig(&self) -> &Contig

Gets the Contig for this Coordinate by reference.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:1".parse::<Coordinate<Zero>>()?;
assert_eq!(coordinate.contig().inner(), "seq0");
source

pub fn into_contig(self) -> Contig

Consumes self and returns the inner Contig from this Coordinate.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:1".parse::<Coordinate<Zero>>()?;
assert_eq!(coordinate.into_contig().inner(), "seq0");
source

pub fn strand(&self) -> &Strand

Gets the Strand for this Coordinate by reference.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Strand;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:1".parse::<Coordinate<Zero>>()?;
assert_eq!(coordinate.strand(), &Strand::Positive);
source

pub fn into_strand(self) -> Strand

Consumes self and returns the inner Strand from this Coordinate.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Strand;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:1".parse::<Coordinate<Zero>>()?;
assert_eq!(coordinate.into_strand(), Strand::Positive);
source

pub fn position(&self) -> &Position<S>

Gets the Position for this Coordinate by reference.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Position;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:1".parse::<Coordinate<Zero>>()?;
assert_eq!(coordinate.position(), &"1".parse::<Position<Zero>>()?);
source

pub fn into_position(self) -> Position<S>

Consumes self and returns the inner Position from this Coordinate.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Strand;
use omics_coordinate::position::Value;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:1".parse::<Coordinate<Zero>>()?;

let (contig, strand, position) = coordinate.into_parts();
assert_eq!(contig.inner(), "seq0");
assert_eq!(strand, Strand::Positive);
assert_eq!(position.inner(), &Value::Usize(1));
source

pub fn into_parts(self) -> (Contig, Strand, Position<S>)

Consumes self to return the parts that comprise this Coordinate.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::position::Value;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:1".parse::<Coordinate<Zero>>()?;
assert_eq!(coordinate.into_position().inner(), &Value::Usize(1));
source

pub fn move_forward(self, magnitude: usize) -> Result<Option<Coordinate<S>>>
where Position<S>: Position<S>,

Consumes the Coordinate to attempt to move the coordinate forward by a specified magnitude.

A checked add (for positive strand) or subtract (for negative strand) is performed to ensure we don’t overflow.

Note that, though the position is checked for usize overflow, we don’t do any bounds checking to make sure that the coordinates fall within any given interval.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Strand;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:0".parse::<Coordinate<Zero>>()?;
let result = coordinate.move_forward(10)?.unwrap();
source

pub fn move_backward(self, magnitude: usize) -> Result<Option<Coordinate<S>>>
where Position<S>: Position<S>,

Consumes the Coordinate to attempt to move the coordinate backward by a specified magnitude.

A checked sub (for positive strand) or add (for negative strand) is performed to ensure we don’t overflow.

Note that, though the position is checked for usize overflow, we don’t do any bounds checking to make sure that the coordinates fall within any given interval.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Strand;
use omics_coordinate::system::Zero;

let coordinate = "seq0:+:0".parse::<Coordinate<Zero>>()?;
let result = coordinate.move_forward(10)?.unwrap();
source

pub fn move_forward_checked_bounds( self, magnitude: usize, interval: &Interval<S>, ) -> Result<Option<Coordinate<S>>>
where Position<S>: Position<S>, Interval<S>: Interval<S>,

Consumes self to attempt to move the Coordinate forward by the specified magnitude while also performing a bounds check within the provided interval.

The following steps are performed:

  • First, the coordinate is moved forward by the specified magnitude. During this move, the position is checked for overflows.
  • Next, the calculated result is checked to ensure it falls within the specified interval. This is to ensure that, although the usize limits may not broken, the interval continues to contain the moved coordinate.
§Examples
use omics_coordinate::system::Zero;
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::position::Value;
use omics_coordinate::Strand;

// Positive-stranded coordinate that falls within the provided interval.

let mut coordinate = Coordinate::<Zero>::try_new("seq0", Strand::Positive, 0)?;
let interval = "seq0:+:0-1000".parse::<Interval<Zero>>()?;

let result = coordinate
    .move_forward_checked_bounds(10, &interval)
    .unwrap()
    .unwrap();

assert_eq!(result.contig().inner(), "seq0");
assert_eq!(result.position().inner(), &Value::Usize(10));
assert_eq!(result.strand(), &Strand::Positive);

// Negative-stranded position that falls within the provided interval.

let coordinate = Coordinate::try_new("seq0", Strand::Negative, 1000)?;
let interval = "seq0:-:1000-0".parse::<Interval<Zero>>()?;
let result = coordinate
    .move_forward_checked_bounds(10, &interval)
    .unwrap()
    .unwrap();

assert_eq!(result.contig().inner(), "seq0");
assert_eq!(result.position().inner(), &Value::Usize(990));
assert_eq!(result.strand(), &Strand::Negative);

// Positive-stranded position that _does not_ fall within the provided interval.

let coordinate = Coordinate::try_new("seq0", Strand::Positive, 0)?;
let interval = "seq0:+:0-10".parse::<Interval<Zero>>()?;
let result = coordinate.move_forward_checked_bounds(10, &interval).unwrap();

assert_eq!(result, None);

// Negative-stranded position that _does not_ fall within the provided interval.

let coordinate = Coordinate::try_new("seq0", Strand::Negative, 10)?;
let interval = "seq0:-:10-0".parse::<Interval<Zero>>()?;
let result = coordinate.move_forward_checked_bounds(10, &interval).unwrap();

assert_eq!(result, None);

// Lower-bound position that _does not_ fall within interval
// (and also would not move forward due to underflow).

let coordinate = Coordinate::<Zero>::lower_bound("seq0");
let interval = "seq0:-:10-[".parse::<Interval<Zero>>()?;
let result = coordinate.move_forward_checked_bounds(1, &interval).unwrap();

assert_eq!(result, None);
source

pub fn move_backward_checked_bounds( self, magnitude: usize, interval: &Interval<S>, ) -> Result<Option<Coordinate<S>>>
where Position<S>: Position<S>, Interval<S>: Interval<S>,

Consumes self to attempt to move the Coordinate backward by the specified magnitude while also performing a bounds check within the provided interval.

The following steps are performed:

  • First, the coordinate is moved backward by the specified magnitude. During this move, the position is checked for overflows.
  • Next, the calculated result is checked to ensure it falls within the specified interval. This is to ensure that, although the usize limits may not broken, the interval continues to contain the moved coordinate.
§Examples
use omics_coordinate::system::Zero;
use omics_coordinate::Coordinate;
use omics_coordinate::Interval;
use omics_coordinate::position::Value;
use omics_coordinate::Strand;

// Positive-stranded coordinate that falls within the provided interval.

let mut coordinate = Coordinate::<Zero>::try_new("seq0", Strand::Positive, 10)?;
let interval = "seq0:+:0-1000".parse::<Interval<Zero>>()?;

let result = coordinate
    .move_backward_checked_bounds(10, &interval)
    .unwrap()
    .unwrap();

assert_eq!(result.contig().inner(), "seq0");
assert_eq!(result.position().inner(), &Value::Usize(0));
assert_eq!(result.strand(), &Strand::Positive);

// Negative-stranded position that falls within the provided interval.

let coordinate = Coordinate::try_new("seq0", Strand::Negative, 990)?;
let interval = "seq0:-:1000-0".parse::<Interval<Zero>>()?;
let result = coordinate
    .move_backward_checked_bounds(10, &interval)
    .unwrap()
    .unwrap();

assert_eq!(result.contig().inner(), "seq0");
assert_eq!(result.position().inner(), &Value::Usize(1000));
assert_eq!(result.strand(), &Strand::Negative);

// Positive-stranded position that _does not_ fall within the provided interval.

let coordinate = Coordinate::try_new("seq0", Strand::Positive, 0)?;
let interval = "seq0:+:0-10".parse::<Interval<Zero>>()?;
let result = coordinate.move_backward_checked_bounds(1, &interval).unwrap();

assert_eq!(result, None);

// Negative-stranded position that _does not_ fall within the provided interval.

let coordinate = Coordinate::try_new("seq0", Strand::Negative, 10)?;
let interval = "seq0:-:10-0".parse::<Interval<Zero>>()?;
let result = coordinate.move_backward_checked_bounds(1, &interval).unwrap();

assert_eq!(result, None);

// Lower-bound position that _does not_ fall within interval
// (and also would not move forward due to underflow).

let coordinate = Coordinate::<Zero>::lower_bound("seq0");
let interval = "seq0:-:10-[".parse::<Interval<Zero>>()?;
let result = coordinate
    .move_backward_checked_bounds(1, &interval)
    .unwrap()
    .unwrap();

assert_eq!(result.contig().inner(), "seq0");
assert_eq!(result.position().inner(), &Value::Usize(0));
assert_eq!(result.strand(), &Strand::Negative);
source

pub fn swap_strand(self) -> Result<Coordinate<S>>

Consumes self to swap the Strand of the Coordinate without modifying the contig or the position.

§Examples
use omics_coordinate::Coordinate;
use omics_coordinate::Strand;
use omics_coordinate::position::Value;
use omics_coordinate::system::Zero;

// Swapping a positive-stranded position to a negative-stranded position.

let coordinate = "seq0:+:1000".parse::<Coordinate<Zero>>()?;
let swapped = coordinate.swap_strand()?;

assert_eq!(swapped.contig().inner(), "seq0");
assert_eq!(swapped.strand(), &Strand::Negative);
assert_eq!(swapped.position().inner(), &Value::Usize(1000));

// Swapping a negative-stranded position to a positive-stranded position.

let coordinate = "seq0:-:1000".parse::<Coordinate<Zero>>()?;
let swapped = coordinate.swap_strand()?;

assert_eq!(swapped.contig().inner(), "seq0");
assert_eq!(swapped.strand(), &Strand::Positive);
assert_eq!(swapped.position().inner(), &Value::Usize(1000));

// Failing to swap the lower bound.

let coordinate = "seq0:-:[".parse::<Coordinate<Zero>>()?;
let err = coordinate.swap_strand().unwrap_err();

assert_eq!(
    err.to_string(),
    String::from(
        "attempted to place lower bound position on non-negative strand for coordinate"
    )
);

Trait Implementations§

source§

impl<S: Clone + System> Clone for Coordinate<S>

source§

fn clone(&self) -> Coordinate<S>

Returns a copy 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<S: Debug + System> Debug for Coordinate<S>

source§

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

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

impl<S: System> Display for Coordinate<S>

source§

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

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

impl<S: System> FromStr for Coordinate<S>
where Position<S>: Position<S>,

source§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<S: PartialEq + System> PartialEq for Coordinate<S>

source§

fn eq(&self, other: &Coordinate<S>) -> 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<S: Eq + System> Eq for Coordinate<S>

source§

impl<S: System> StructuralPartialEq for Coordinate<S>

Auto Trait Implementations§

§

impl<S> Freeze for Coordinate<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Coordinate<S>
where S: RefUnwindSafe,

§

impl<S> Send for Coordinate<S>
where S: Send,

§

impl<S> Sync for Coordinate<S>
where S: Sync,

§

impl<S> Unpin for Coordinate<S>
where S: Unpin,

§

impl<S> UnwindSafe for Coordinate<S>
where S: UnwindSafe,

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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§

default 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.