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

Add proper `Display` trait implementation for `EncodedSequence`

parent 8e940621
No related branches found
No related tags found
No related merge requests found
......@@ -3,14 +3,13 @@
extern crate lightmotif;
extern crate test;
use std::str::FromStr;
use lightmotif::abc::Dna;
use lightmotif::pli::Pipeline;
use lightmotif::pli::Score;
use lightmotif::pli::StripedScores;
use lightmotif::pwm::CountMatrix;
use lightmotif::seq::EncodedSequence;
use lightmotif::seq::StripedSequence;
use lightmotif::utils::StrictlyPositive;
use typenum::consts::U16;
use typenum::consts::U32;
......@@ -18,12 +17,11 @@ use typenum::consts::U32;
const SEQUENCE: &str = include_str!("ecoli.txt");
fn bench<C: StrictlyPositive, P: Score<Dna, C>>(bencher: &mut test::Bencher, pli: &P) {
let encoded = EncodedSequence::<Dna>::from_str(SEQUENCE).unwrap();
let mut striped = encoded.to_striped::<C>();
let mut striped = StripedSequence::<Dna, C>::encode(SEQUENCE).unwrap();
let cm = CountMatrix::<Dna>::from_sequences(&[
EncodedSequence::from_str("GTTGACCTTATCAAC").unwrap(),
EncodedSequence::from_str("GTTGATCCAGTCAAC").unwrap(),
EncodedSequence::encode("GTTGACCTTATCAAC").unwrap(),
EncodedSequence::encode("GTTGATCCAGTCAAC").unwrap(),
])
.unwrap();
let pbm = cm.to_freq(0.1);
......
//! Linear and striped storage for alphabet-encoded sequences.
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::str::FromStr;
use std::string::ToString;
use typenum::marker_traits::NonZero;
use typenum::marker_traits::Unsigned;
......@@ -90,6 +92,15 @@ impl<A: Alphabet> AsRef<[<A as Alphabet>::Symbol]> for EncodedSequence<A> {
}
}
impl<A: Alphabet> Display for EncodedSequence<A> {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
for c in self.data.iter() {
write!(f, "{}", c.as_char())?;
}
Ok(())
}
}
impl<A: Alphabet> FromStr for EncodedSequence<A> {
type Err = InvalidSymbol;
fn from_str(seq: &str) -> Result<Self, Self::Err> {
......@@ -106,12 +117,6 @@ impl<'a, A: Alphabet> IntoIterator for &'a EncodedSequence<A> {
}
}
impl<A: Alphabet> ToString for EncodedSequence<A> {
fn to_string(&self) -> String {
self.to_string()
}
}
// --- StripedSequence ---------------------------------------------------------
/// An encoded sequence stored in a striped matrix with a fixed column count.
......@@ -124,6 +129,22 @@ pub struct StripedSequence<A: Alphabet, C: StrictlyPositive> {
}
impl<A: Alphabet, C: StrictlyPositive> StripedSequence<A, C> {
/// Create a new striped sequence from a textual representation.
pub fn encode(sequence: &str) -> Result<Self, InvalidSymbol> {
let length = sequence.len();
let n = (length + (C::USIZE - 1)) / C::USIZE;
let mut data = DenseMatrix::new(n);
for (i, x) in sequence.chars().enumerate() {
data[i % n][i / n] = A::Symbol::from_char(x)?;
}
Ok(StripedSequence {
alphabet: std::marker::PhantomData,
data,
length,
wrap: 0,
})
}
/// Reconfigure the striped sequence for searching with a motif.
pub fn configure(&mut self, motif: &ScoringMatrix<A>) {
if motif.len() > 0 {
......@@ -158,6 +179,13 @@ impl<A: Alphabet, C: StrictlyPositive> From<EncodedSequence<A>> for StripedSeque
}
}
impl<A: Alphabet, C: StrictlyPositive> FromStr for StripedSequence<A, C> {
type Err = InvalidSymbol;
fn from_str(seq: &str) -> Result<Self, Self::Err> {
Self::encode(seq)
}
}
#[cfg(test)]
mod test {
use typenum::consts::U2;
......
......@@ -8,6 +8,7 @@ use lightmotif::CountMatrix;
use lightmotif::Dna;
use lightmotif::EncodedSequence;
use lightmotif::Pipeline;
use lightmotif::StripedSequence;
use typenum::consts::U1;
use typenum::consts::U16;
......@@ -35,8 +36,7 @@ const EXPECTED: &[f32] = &[
];
fn test_score<C: StrictlyPositive, P: Score<Dna, C>>(pli: &P) {
let encoded = EncodedSequence::<Dna>::encode(SEQUENCE).unwrap();
let mut striped = encoded.to_striped::<C>();
let mut striped = StripedSequence::<Dna, C>::encode(SEQUENCE).unwrap();
let cm = CountMatrix::<Dna>::from_sequences(
PATTERNS.iter().map(|x| EncodedSequence::encode(x).unwrap()),
......@@ -63,8 +63,7 @@ fn test_score<C: StrictlyPositive, P: Score<Dna, C>>(pli: &P) {
}
fn test_best_position<C: StrictlyPositive, P: Score<Dna, C> + BestPosition<C>>(pli: &P) {
let encoded = EncodedSequence::<Dna>::encode(SEQUENCE).unwrap();
let mut striped = encoded.to_striped();
let mut striped = StripedSequence::<Dna, C>::encode(SEQUENCE).unwrap();
let cm = CountMatrix::<Dna>::from_sequences(
PATTERNS.iter().map(|x| EncodedSequence::encode(x).unwrap()),
......
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