Skip to content
Snippets Groups Projects
Commit b5ac2896 authored by Martin Larralde's avatar Martin Larralde
Browse files

Add trait to count the number of symbol occurences in an arbitrary collection

parent 0a68afc6
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,19 @@ use super::err::InvalidSymbol; ...@@ -18,6 +18,19 @@ use super::err::InvalidSymbol;
use super::num::StrictlyPositive; use super::num::StrictlyPositive;
use super::pwm::ScoringMatrix; use super::pwm::ScoringMatrix;
// --- SymbolCount -------------------------------------------------------------
/// A trait for counting the number of occurences of a symbol in a sequence.
pub trait SymbolCount<A: Alphabet> {
fn count_symbol(&self, symbol: <A as Alphabet>::Symbol) -> usize;
}
impl<'a, A: Alphabet, T: IntoIterator<Item = &'a A::Symbol> + Copy> SymbolCount<A> for T {
fn count_symbol(&self, symbol: <A as Alphabet>::Symbol) -> usize {
self.into_iter().filter(|&&c| c == symbol).count()
}
}
// --- EncodedSequence --------------------------------------------------------- // --- EncodedSequence ---------------------------------------------------------
/// A biological sequence encoded with an alphabet. /// A biological sequence encoded with an alphabet.
...@@ -53,8 +66,8 @@ impl<A: Alphabet> EncodedSequence<A> { ...@@ -53,8 +66,8 @@ impl<A: Alphabet> EncodedSequence<A> {
/// Iterate over the symbols in the sequence. /// Iterate over the symbols in the sequence.
#[inline] #[inline]
pub fn iter(&self) -> impl IntoIterator<Item = &A::Symbol> { pub fn iter(&self) -> <&[A::Symbol] as IntoIterator>::IntoIter {
self.data.iter() self.data.as_slice().into_iter()
} }
/// Convert the encoded sequence to a striped matrix. /// Convert the encoded sequence to a striped matrix.
...@@ -233,7 +246,7 @@ impl<A: Alphabet, C: StrictlyPositive> AsRef<StripedSequence<A, C>> for StripedS ...@@ -233,7 +246,7 @@ impl<A: Alphabet, C: StrictlyPositive> AsRef<StripedSequence<A, C>> for StripedS
} }
} }
impl<A: Alphabet, C: Unsigned + NonZero> Index<usize> for StripedSequence<A, C> { impl<A: Alphabet, C: StrictlyPositive> Index<usize> for StripedSequence<A, C> {
type Output = <A as Alphabet>::Symbol; type Output = <A as Alphabet>::Symbol;
#[inline] #[inline]
fn index(&self, index: usize) -> &Self::Output { fn index(&self, index: usize) -> &Self::Output {
...@@ -244,6 +257,15 @@ impl<A: Alphabet, C: Unsigned + NonZero> Index<usize> for StripedSequence<A, C> ...@@ -244,6 +257,15 @@ impl<A: Alphabet, C: Unsigned + NonZero> Index<usize> for StripedSequence<A, C>
} }
} }
impl<A: Alphabet, C: StrictlyPositive> SymbolCount<A> for &StripedSequence<A, C> {
fn count_symbol(&self, symbol: <A as Alphabet>::Symbol) -> usize {
self.data
.iter()
.map(|row| SymbolCount::<A>::count_symbol(&row, symbol))
.sum()
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use typenum::consts::U2; use typenum::consts::U2;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment