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

Add helper trait for strictly positive typenums

parent 271bd8d4
No related branches found
No related tags found
No related merge requests found
......@@ -11,13 +11,14 @@ use lightmotif::pli::Score;
use lightmotif::pli::StripedScores;
use lightmotif::pwm::CountMatrix;
use lightmotif::seq::EncodedSequence;
use lightmotif::utils::StrictlyPositive;
use typenum::consts::U32;
use typenum::marker_traits::NonZero;
use typenum::marker_traits::Unsigned;
const SEQUENCE: &'static str = include_str!("ecoli.txt");
fn bench<C: Unsigned + NonZero, P: Score<Dna, C>>(bencher: &mut test::Bencher, pli: &P) {
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>();
......
......@@ -9,6 +9,7 @@ pub mod err;
pub mod pli;
pub mod pwm;
pub mod seq;
pub mod utils;
pub use abc::Alphabet;
pub use abc::Dna;
......
//! Concrete implementations of the sequence scoring pipeline.
use typenum::marker_traits::NonZero;
use typenum::marker_traits::Unsigned;
pub use self::scores::StripedScores;
use self::platform::Avx2;
use self::platform::Backend;
use self::platform::Generic;
use self::platform::Sse2;
pub use self::scores::StripedScores;
use super::abc::Alphabet;
use super::abc::Dna;
use super::abc::Symbol;
......@@ -16,6 +13,7 @@ use super::dense::DenseMatrix;
use super::err::UnsupportedBackend;
use super::pwm::ScoringMatrix;
use super::seq::StripedSequence;
use super::utils::StrictlyPositive;
pub mod platform;
mod scores;
......@@ -23,7 +21,7 @@ mod scores;
// --- Score -------------------------------------------------------------------
/// Generic trait for computing sequence scores with a PSSM.
pub trait Score<A: Alphabet, C: NonZero + Unsigned> {
pub trait Score<A: Alphabet, C: StrictlyPositive> {
/// Compute the PSSM scores into the given striped score matrix.
fn score_into<S, M>(&self, seq: S, pssm: M, scores: &mut StripedScores<C>)
where
......@@ -68,7 +66,7 @@ pub trait Score<A: Alphabet, C: NonZero + Unsigned> {
}
/// Generic trait for finding the highest scoring site in a striped score matrix.
pub trait BestPosition<C: NonZero + Unsigned> {
pub trait BestPosition<C: StrictlyPositive> {
/// Find the sequence position with the highest score.
fn best_position(&self, scores: &StripedScores<C>) -> Option<usize> {
if scores.len() == 0 {
......@@ -112,9 +110,9 @@ impl<A: Alphabet> Pipeline<A, Generic> {
}
}
impl<A: Alphabet, C: NonZero + Unsigned> Score<A, C> for Pipeline<A, Generic> {}
impl<A: Alphabet, C: StrictlyPositive> Score<A, C> for Pipeline<A, Generic> {}
impl<A: Alphabet, C: NonZero + Unsigned> BestPosition<C> for Pipeline<A, Generic> {}
impl<A: Alphabet, C: StrictlyPositive> BestPosition<C> for Pipeline<A, Generic> {}
// --- SSE2 pipeline -----------------------------------------------------------
......
......@@ -11,6 +11,7 @@ use super::abc::Symbol;
use super::dense::DenseMatrix;
use super::err::InvalidSymbol;
use super::pwm::ScoringMatrix;
use super::utils::StrictlyPositive;
// --- EncodedSequence ---------------------------------------------------------
......@@ -115,14 +116,14 @@ impl<A: Alphabet> ToString for EncodedSequence<A> {
/// An encoded sequence stored in a striped matrix with a fixed column count.
#[derive(Clone, Debug)]
pub struct StripedSequence<A: Alphabet, C: Unsigned + NonZero> {
pub struct StripedSequence<A: Alphabet, C: StrictlyPositive> {
alphabet: std::marker::PhantomData<A>,
pub length: usize,
pub wrap: usize,
pub data: DenseMatrix<A::Symbol, C>,
}
impl<A: Alphabet, C: Unsigned + NonZero> StripedSequence<A, C> {
impl<A: Alphabet, C: StrictlyPositive> StripedSequence<A, C> {
/// Reconfigure the striped sequence for searching with a motif.
pub fn configure(&mut self, motif: &ScoringMatrix<A>) {
if motif.len() > 0 {
......@@ -145,13 +146,13 @@ impl<A: Alphabet, C: Unsigned + NonZero> StripedSequence<A, C> {
}
}
impl<A: Alphabet, C: Unsigned + NonZero> AsRef<StripedSequence<A, C>> for StripedSequence<A, C> {
impl<A: Alphabet, C: StrictlyPositive> AsRef<StripedSequence<A, C>> for StripedSequence<A, C> {
fn as_ref(&self) -> &Self {
&self
}
}
impl<A: Alphabet, C: Unsigned + NonZero> From<EncodedSequence<A>> for StripedSequence<A, C> {
impl<A: Alphabet, C: StrictlyPositive> From<EncodedSequence<A>> for StripedSequence<A, C> {
fn from(encoded: EncodedSequence<A>) -> Self {
encoded.to_striped()
}
......
use typenum::marker_traits::NonZero;
use typenum::marker_traits::Unsigned;
/// A marker trait for type numbers that are strictly positive.
pub trait StrictlyPositive: Unsigned + NonZero {}
impl<N: Unsigned + NonZero> StrictlyPositive for N {}
......@@ -3,6 +3,7 @@ extern crate typenum;
use lightmotif::pli::BestPosition;
use lightmotif::pli::Score;
use lightmotif::utils::StrictlyPositive;
use lightmotif::CountMatrix;
use lightmotif::Dna;
use lightmotif::EncodedSequence;
......@@ -34,7 +35,7 @@ const EXPECTED: &[f32] = &[
-30.922688 , -18.678621
];
fn test_score<C: Unsigned + NonZero, P: Score<Dna, C>>(pli: &P) {
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>();
......@@ -62,7 +63,7 @@ fn test_score<C: Unsigned + NonZero, P: Score<Dna, C>>(pli: &P) {
}
}
fn test_best_position<C: Unsigned + NonZero, P: Score<Dna, C> + BestPosition<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();
......
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