Skip to content

Commit

Permalink
rearrange extra codec modules, Maskable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-k committed Oct 15, 2024
1 parent b6f59ac commit 4c8bb00
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
9 changes: 8 additions & 1 deletion bio-seq/src/codec/degenerate/dna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ impl ReverseComplement for Seq<Dna> {

#[cfg(test)]
mod tests {
use crate::codec::masked;
use crate::codec::degenerate;
use crate::prelude::*;

#[test]
fn test_1bit() {
let seq = Seq::<degenerate::Dna>::from_str("SSSWWWSW").unwrap();
let seq_rc: Seq<degenerate::Dna> = seq.revcomp();
assert_eq!("WSWWWSSS", String::from(seq_rc));
}
}
11 changes: 4 additions & 7 deletions bio-seq/src/codec/degenerate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Experimental encodings with degenerate representations
//! Includes `N`, `n`, `.`, `-`
//! Experimental encodings for degenerate representations (eg 1-bit)
pub mod dna;

Expand All @@ -9,8 +8,6 @@ pub mod dna;
pub use dna::Dna;
//pub use iupac::Iupac;

#[cfg(test)]
mod tests {
use crate::codec::masked;
use crate::prelude::*;
}
//#[cfg(test)]
//mod tests {
//}
31 changes: 21 additions & 10 deletions bio-seq/src/codec/masked/dna.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::codec::Codec;
use crate::codec::masked::Maskable;
use crate::codec::{Codec, Complement};
use crate::seq::{ReverseComplement, Seq};

/// **Experimental** 4-bit nucleotide encoding with fast reverse complement and toggled mask operation
///
/// Note that masking/unmasking are not idempotent
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Codec)]
#[bits(4)]
#[repr(u8)]
Expand Down Expand Up @@ -38,26 +42,32 @@ pub enum Dna {
Unknown2 = 0b1001,
}

/*
impl Complement for Dna {
/// This representation can be complemented by reversing the bit pattern
fn comp(&self) -> Self {
// reverse the bits
Dna::unsafe_from_bits(b)
todo!()
}
}
*/

impl Dna {
/// Flipping the bit pattern masks/unmasks this representation
pub fn mask(&self) -> Self {
impl Maskable for Dna {
/// Inverting the bit pattern masks/unmasks this representation
fn mask(&self) -> Self {
let b = *self as u8 ^ 0b1111;
Dna::unsafe_from_bits(b)
}

fn unmask(&self) -> Self {
let b = *self as u8 ^ 0b1111;
Dna::unsafe_from_bits(b)
}
}

impl Seq<Dna> {
pub fn mask(&self) -> Self {
impl Maskable for Seq<Dna> {
fn mask(&self) -> Self {
Self::from(!self.bv.clone())
}

fn unmask(&self) -> Self {
Self::from(!self.bv.clone())
}
}
Expand All @@ -75,6 +85,7 @@ impl ReverseComplement for Seq<Dna> {
#[cfg(test)]
mod tests {
use crate::codec::masked;
use crate::codec::masked::Maskable;
use crate::prelude::*;

#[test]
Expand Down
23 changes: 22 additions & 1 deletion bio-seq/src/codec/masked/iupac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,21 @@ impl Maskable for Iupac {
}
}

impl Maskable for Seq<Iupac> {
fn mask(&self) -> Self {
// set the third bit of every chunk of 5 bits
todo!()
}

fn unmask(&self) -> Self {
todo!()
}
}

impl ReverseComplement for Seq<Iupac> {
type Output = Self;

/// A reverse complementing just requires reversing the bit sequence
/// Reverse complementing just requires reversing the bit sequence
fn revcomp(&self) -> Self {
let mut bv = self.bv.clone();
bv.reverse();
Expand All @@ -104,5 +115,15 @@ impl ReverseComplement for Seq<Iupac> {
#[cfg(test)]
mod tests {
use crate::codec::masked;
use crate::codec::masked::Maskable;
use crate::prelude::*;

#[ignore]
#[test]
fn mask_iupac_seq() {
let seq = Seq::<masked::Iupac>::try_from("A.TCGCgtcataN--A").unwrap();

assert_ne!(seq.mask().to_string(), "a.tcgcGTGATAN--a".to_string());
assert_eq!(seq.mask().to_string(), "a.tcgcGTCATAn--a".to_string());
}
}
3 changes: 2 additions & 1 deletion bio-seq/src/codec/masked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ pub trait Maskable {
fn unmask(&self) -> Self;
}

/*
#[cfg(test)]
mod tests {
use crate::codec::masked;
use crate::prelude::*;
}
*/

0 comments on commit 4c8bb00

Please sign in to comment.