Skip to main content

Difference

Enum Difference 

Source
pub enum Difference {
    Match(usize),
    Mismatch(u8),
    Insertion(Vec<u8>),
    Deletion(usize),
    End,
}
Expand description

An operation in a difference string describing an alignment between a query sequence and a target sequence.

This implementation supports the following operations:

  • =: A match given as the matching sequence.
  • :: A match given as the match length.
  • *: A mismatch given as the target base and the query base.
  • +: An insertion given as the inserted sequence.
  • -: A deletion given as the deleted sequence.

The operations do not store target bases, as the query sequence can be reconstructed without that information. Operation ~ (intron length and splice signal) is not supported yet. Parsing is based on bytes rather than characters to avoid unnecessary UTF-8 validation.

§Examples

use gbz_base::Difference;

let with_gaps = b":48-CAT:44+GATTACA:51";
let ops = Difference::parse(with_gaps);
assert!(ops.is_ok());
let ops = ops.unwrap();
assert_eq!(ops.len(), 5);
assert_eq!(ops[0], Difference::Match(48));
assert_eq!(ops[1], Difference::Deletion(3));
assert_eq!(ops[2], Difference::Match(44));
assert_eq!(ops[3], Difference::Insertion(b"GATTACA".to_vec()));
assert_eq!(ops[4], Difference::Match(51));

Variants§

§

Match(usize)

A match of the given length.

§

Mismatch(u8)

Mismatch represented as the query base.

§

Insertion(Vec<u8>)

Insertion to the reference represented as the inserted sequence.

§

Deletion(usize)

Deletion from the reference represented as deletion length.

§

End

Marker for the end of a sequence when concatenating multiple difference strings.

Implementations§

Source§

impl Difference

Source

pub const NUM_TYPES: usize = 5

Number of supported operation types.

Source

pub const UNKNOWN_BASE: u8 = b'X'

Symbol used as a substitute for an unknown base.

Source

pub fn parse(difference_string: &[u8]) -> Result<Vec<Self>, String>

Parses a difference string and returns it as a vector of operations.

Returns an error if the difference string is invalid.

Source

pub fn parse_normalized(difference_string: &[u8]) -> Result<Vec<Self>, String>

Parses a difference string and returns it as a normalized vector of operations.

The operations are merged and empty operations are removed. Returns an error if the difference string is invalid.

Source

pub fn stats(difference_string: &[Self]) -> (usize, usize, usize, usize)

Calculates various statistics from a sequence of operations.

The return value is (query length, target length, matches, edits).

Source

pub fn len(&self) -> usize

Returns the length of the operation.

Source

pub fn is_empty(&self) -> bool

Returns true if the operation is empty.

Source

pub fn target_len(&self) -> usize

Returns the length of the operation in the target sequence.

Source

pub fn query_len(&self) -> usize

Returns the length of the operation in the query sequence.

Source

pub fn try_merge(&mut self, op: &Self) -> bool

Merges the given operation into this operation if they can be merged.

Returns true if the operations were merged.

Source

pub fn normalize(ops: Vec<Self>) -> Vec<Self>

Normalizes the sequence of operations.

This merges adjacent matches and insertions and removes empty operations.

Source

pub fn to_bytes(ops: &[Difference], target_sequence: &[u8]) -> Vec<u8>

Writes a difference string as a Vec<u8> string.

Trait Implementations§

Source§

impl Clone for Difference

Source§

fn clone(&self) -> Difference

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 Difference

Source§

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

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

impl PartialEq for Difference

Source§

fn eq(&self, other: &Difference) -> 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 Eq for Difference

Source§

impl StructuralPartialEq for Difference

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> Same for T

Source§

type Output = T

Should always be Self
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, 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.