Struct bio::alphabets::RankTransform
source · pub struct RankTransform {
pub ranks: SymbolRanks,
}
Expand description
Tools based on transforming the alphabet symbols to their lexicographical ranks.
Lexicographical rank is computed using u8
representations,
i.e. ASCII codes, of the input characters.
For example, assuming that the alphabet consists of the symbols A
, C
, G
, and T
, this
will yield ranks 0
, 1
, 2
, 3
for them, respectively.
RankTransform
can be used in to perform bit encoding for texts over a
given alphabet via bio::data_structures::bitenc
.
Fields§
§ranks: SymbolRanks
Implementations§
source§impl RankTransform
impl RankTransform
sourcepub fn new(alphabet: &Alphabet) -> Self
pub fn new(alphabet: &Alphabet) -> Self
Construct a new RankTransform
.
Complexity: O(n), where n is the number of symbols in the alphabet.
Example
use bio::alphabets;
let dna_alphabet = alphabets::Alphabet::new(b"acgtACGT");
let dna_ranks = alphabets::RankTransform::new(&dna_alphabet);
sourcepub fn get(&self, a: u8) -> u8
pub fn get(&self, a: u8) -> u8
Get the rank of symbol a
.
This method panics for characters not contained in the alphabet.
Complexity: O(1)
Example
use bio::alphabets;
let dna_alphabet = alphabets::Alphabet::new(b"acgtACGT");
let dna_ranks = alphabets::RankTransform::new(&dna_alphabet);
assert_eq!(dna_ranks.get(65), 0); // "A"
assert_eq!(dna_ranks.get(116), 7); // "t"
sourcepub fn transform<C, T>(&self, text: T) -> Vec<u8>where
C: Borrow<u8>,
T: IntoIterator<Item = C>,
pub fn transform<C, T>(&self, text: T) -> Vec<u8>where C: Borrow<u8>, T: IntoIterator<Item = C>,
Transform a given text
into a vector of rank values.
Complexity: O(n), where n is the length of the text.
Example
use bio::alphabets;
let dna_alphabet = alphabets::Alphabet::new(b"ACGTacgt");
let dna_ranks = alphabets::RankTransform::new(&dna_alphabet);
let text = b"aAcCgGtT";
assert_eq!(dna_ranks.transform(text), vec![4, 0, 5, 1, 6, 2, 7, 3]);
sourcepub fn qgrams<C, T>(&self, q: u32, text: T) -> QGrams<'_, C, T::IntoIter> ⓘwhere
C: Borrow<u8>,
T: IntoIterator<Item = C>,
pub fn qgrams<C, T>(&self, q: u32, text: T) -> QGrams<'_, C, T::IntoIter> ⓘwhere C: Borrow<u8>, T: IntoIterator<Item = C>,
Iterate over q-grams (substrings of length q) of given text
. The q-grams are encoded
as usize
by storing the symbol ranks in log2(|A|) bits (with |A| being the alphabet size).
If q is larger than usize::BITS / log2(|A|), this method fails with an assertion.
Complexity: O(n), where n is the length of the text.
Example
use bio::alphabets;
let dna_alphabet = alphabets::Alphabet::new(b"ACGTacgt");
let dna_ranks = alphabets::RankTransform::new(&dna_alphabet);
let q_grams: Vec<usize> = dna_ranks.qgrams(2, b"ACGT").collect();
assert_eq!(q_grams, vec![1, 10, 19]);
sourcepub fn alphabet(&self) -> Alphabet
pub fn alphabet(&self) -> Alphabet
Restore alphabet from transform.
Complexity: O(n), where n is the number of symbols in the alphabet.
Example
use bio::alphabets;
let dna_alphabet = alphabets::Alphabet::new(b"acgtACGT");
let dna_ranks = alphabets::RankTransform::new(&dna_alphabet);
assert_eq!(dna_ranks.alphabet().symbols, dna_alphabet.symbols);
sourcepub fn get_width(&self) -> usize
pub fn get_width(&self) -> usize
Compute the number of bits required to encode the largest rank value.
For example, the alphabet b"ACGT"
with 4 symbols has the maximal rank
3, which can be encoded in 2 bits.
This value can be used to create a data_structures::bitenc::BitEnc
bit encoding tailored to the given alphabet.
Complexity: O(n), where n is the number of symbols in the alphabet.
Example
use bio::alphabets;
let dna_alphabet = alphabets::Alphabet::new(b"ACGT");
let dna_ranks = alphabets::RankTransform::new(&dna_alphabet);
assert_eq!(dna_ranks.get_width(), 2);
let dna_n_alphabet = alphabets::Alphabet::new(b"ACGTN");
let dna_n_ranks = alphabets::RankTransform::new(&dna_n_alphabet);
assert_eq!(dna_n_ranks.get_width(), 3);
Trait Implementations§
source§impl Clone for RankTransform
impl Clone for RankTransform
source§fn clone(&self) -> RankTransform
fn clone(&self) -> RankTransform
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for RankTransform
impl Debug for RankTransform
source§impl Default for RankTransform
impl Default for RankTransform
source§fn default() -> RankTransform
fn default() -> RankTransform
source§impl<'de> Deserialize<'de> for RankTransform
impl<'de> Deserialize<'de> for RankTransform
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl Hash for RankTransform
impl Hash for RankTransform
source§impl Ord for RankTransform
impl Ord for RankTransform
source§fn cmp(&self, other: &RankTransform) -> Ordering
fn cmp(&self, other: &RankTransform) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl PartialEq<RankTransform> for RankTransform
impl PartialEq<RankTransform> for RankTransform
source§fn eq(&self, other: &RankTransform) -> bool
fn eq(&self, other: &RankTransform) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<RankTransform> for RankTransform
impl PartialOrd<RankTransform> for RankTransform
source§fn partial_cmp(&self, other: &RankTransform) -> Option<Ordering>
fn partial_cmp(&self, other: &RankTransform) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for RankTransform
impl Serialize for RankTransform
impl Eq for RankTransform
impl StructuralEq for RankTransform
impl StructuralPartialEq for RankTransform
Auto Trait Implementations§
impl RefUnwindSafe for RankTransform
impl Send for RankTransform
impl Sync for RankTransform
impl Unpin for RankTransform
impl UnwindSafe for RankTransform
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<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.