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
sourceimpl 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>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
where
C: Borrow<u8>,
T: IntoIterator<Item = C>,
pub fn transform<C, T>(&self, text: T) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
where
C: Borrow<u8>,
T: IntoIterator<Item = C>,
A: Allocator,
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>ⓘNotable traits for QGrams<'a, C, T>impl<'a, C, T> Iterator for QGrams<'a, C, T> where
C: Borrow<u8>,
T: Iterator<Item = C>, type Item = usize;
where
C: Borrow<u8>,
T: IntoIterator<Item = C>,
pub fn qgrams<C, T>(&self, q: u32, text: T) -> QGrams<'_, C, T::IntoIter>ⓘNotable traits for QGrams<'a, C, T>impl<'a, C, T> Iterator for QGrams<'a, C, T> where
C: Borrow<u8>,
T: Iterator<Item = C>, type Item = usize;
where
C: Borrow<u8>,
T: IntoIterator<Item = C>,
C: Borrow<u8>,
T: Iterator<Item = C>, type Item = usize;
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
sourceimpl<'de> Deserialize<'de> for RankTransform
impl<'de> Deserialize<'de> for RankTransform
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Serialize for RankTransform
impl Serialize 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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
pub fn to_subset(&self) -> Option<SS>
pub fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct self
from the equivalent element of its
superset. Read more
pub fn is_in_subset(&self) -> bool
pub fn is_in_subset(&self) -> bool
Checks if self
is actually part of its subset T
(and can be converted to it).
pub fn to_subset_unchecked(&self) -> SS
pub fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset
but without any property checks. Always succeeds.
pub fn from_subset(element: &SS) -> SP
pub fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.