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

Add method to wrap a striped sequence

parent 475c9d0d
No related branches found
No related tags found
No related merge requests found
......@@ -32,7 +32,7 @@ impl<A: Alphabet> EncodedSequence<A> {
/// Convert the encoded sequence to a striped matrix.
pub fn to_striped<const C: usize>(&self) -> StripedSequence<A, C> {
let length = self.data.len();
let n = (length + C) / C;
let n = length / C + ((length % C) != 0) as usize;
let mut data = DenseMatrix::new(n);
for (i, &x) in self.data.iter().enumerate() {
data[i % n][i / n] = x;
......@@ -41,6 +41,7 @@ impl<A: Alphabet> EncodedSequence<A> {
alphabet: self.alphabet,
data,
length,
wrap: 0,
}
}
}
......@@ -55,11 +56,65 @@ impl<A: Alphabet> AsRef<EncodedSequence<A>> for EncodedSequence<A> {
pub struct StripedSequence<A: Alphabet, const C: usize = 32> {
pub alphabet: A,
pub length: usize,
pub wrap: usize,
pub data: DenseMatrix<A::Symbol, C>,
}
impl<A: Alphabet, const C: usize> StripedSequence<A, C> {
/// Add wrap-around rows for a motif of length `m`.
pub fn configure_wrap(&mut self, m: usize) {
if m > self.wrap {
let rows = self.data.rows() - self.wrap;
self.data.resize( self.data.rows() + m - self.wrap );
for i in 0..m {
for j in 0..C-1 {
self.data[rows + i][j] = self.data[i][j+1];
}
}
self.wrap = m;
}
}
}
impl<A: Alphabet, const C: usize> From<EncodedSequence<A>> for StripedSequence<A, C> {
fn from(encoded: EncodedSequence<A>) -> Self {
encoded.to_striped()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::DnaAlphabet;
use crate::DnaSymbol::*;
#[test]
fn test_stripe() {
let seq = EncodedSequence::<DnaAlphabet>::from_text("ATGCA").unwrap();
let striped = seq.to_striped::<4>();
assert_eq!(striped.data.rows(), 2);
assert_eq!(&striped.data[0], &[ A, G, A, N ]);
assert_eq!(&striped.data[1], &[ T, C, N, N ]);
let striped = seq.to_striped::<2>();
assert_eq!(striped.data.rows(), 3);
assert_eq!(&striped.data[0], &[ A, C, ]);
assert_eq!(&striped.data[1], &[ T, A, ]);
assert_eq!(&striped.data[2], &[ G, N, ]);
}
#[test]
fn test_configure_wrap() {
let seq = EncodedSequence::<DnaAlphabet>::from_text("ATGCA").unwrap();
let mut striped = seq.to_striped::<4>();
striped.configure_wrap(2);
assert_eq!(striped.data.rows(), 4);
assert_eq!(&striped.data[0], &[ A, G, A, N ]);
assert_eq!(&striped.data[1], &[ T, C, N, N ]);
assert_eq!(&striped.data[2], &[ G, A, N, N ]);
assert_eq!(&striped.data[3], &[ C, N, N, N ]);
}
}
\ No newline at end of file
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