Skip to content

Commit

Permalink
clean up dead code, more debug_assert!s, better HashMap example
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-k committed Oct 13, 2024
1 parent 1f5bdab commit c7c67f0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 176 deletions.
77 changes: 5 additions & 72 deletions bio-seq/src/kmer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ impl sealed::KmerStorage for usize {
}

fn from_bitslice(bs: &Bs) -> Self {
debug_assert!(
bs.len() <= Self::BITS as usize,
"bitslice larger than kmer storage type"
);
bs.load_le()
}

Expand Down Expand Up @@ -166,14 +170,6 @@ pub struct Kmer<C: Codec, const K: usize, S: KmerStorage = usize> {
pub bs: S,
}

/*
impl<A: Codec, const K: usize, S1: KmerStorage, S2: KmerStorage> PartialEq<Kmer<A, K, S2>> for Kmer<A, K, S1> {
fn eq(&self, other: &Kmer<A, K, S2>) -> bool {
self.bs.to_bitarray() == other.bs.to_bitarray()
}
}
*/

impl<A: Codec, const K: usize, S: KmerStorage> Kmer<A, K, S> {
// This error message can be formatted with constants in nightly (const_format)
const _ASSERT_K: () = assert!(
Expand Down Expand Up @@ -264,27 +260,6 @@ impl<A: Codec, const K: usize, S: KmerStorage> Kmer<A, K, S> {
bs: S::from_bitslice(&seq.bs),
}
}
/*
/// Iterate through all bases of a Kmer
pub fn iter(self) -> KmerBases<A, K> {
KmerBases {
_p: PhantomData,
bits: Ba::from(self.bs),
index: 0,
}
}
*/
/*
/// tail
pub fn tail(self, base: A) -> Seq<A> {
let bv: Bv::from(self.bs);
Seq {
_p: PhantomData,
bv: bv[A::BITS ..],
}
}
*/
}

impl<A: Codec, const K: usize> From<usize> for Kmer<A, K, usize> {
Expand Down Expand Up @@ -313,17 +288,6 @@ impl<A: Codec, const K: usize> From<&Kmer<A, K, usize>> for usize {
}
}

/*
impl<A: Codec, const K: usize> Deref for SeqArray<A, K, 1> {
type Target = Kmer<A, K>;
fn deref(&self) -> &Self::Target {
let bs: usize = &self.ba.view_bits()[0..(K * A::BITS as usize)];
unsafe { &*(bs *const Kmer<A, K>) }
}
}
*/

impl<A: Codec, const K: usize> Deref for Kmer<A, K> {
type Target = SeqSlice<A>;

Expand Down Expand Up @@ -363,6 +327,7 @@ pub struct KmerIter<'a, A: Codec, const K: usize> {

impl<A: Codec, const K: usize, S: KmerStorage> Kmer<A, K, S> {
fn unsafe_from(seq: &SeqSlice<A>) -> Self {
debug_assert!(K == seq.len(), "K != seq.len()");
Kmer {
_p: PhantomData,
bs: S::from_bitslice(&seq.bs),
Expand All @@ -382,30 +347,6 @@ impl<A: Codec, const K: usize> Iterator for KmerIter<'_, A, K> {
}
}

/*
/// An iterator over the bases of a kmer
pub struct KmerBases<A: Codec, const K: usize, S: KmerStorage> {
pub _p: PhantomData<A>,
pub bits: S::BaN,
pub index: usize,
}
/// An iterator over the bases of a kmer
impl<A: Codec, const K: usize> Iterator for KmerBases<A, K> {
type Item = A;
fn next(&mut self) -> Option<A> {
let i = self.index * A::BITS as usize;
if self.index >= K {
return None;
}
self.index += 1;
let chunk = &self.bits[i..i + (A::BITS as usize)];
Some(A::unsafe_from_bits(chunk.load_le::<u8>()))
}
}
*/

/// ```
/// use bio_seq::prelude::*;
/// use std::hash::{Hash, Hasher, DefaultHasher};
Expand Down Expand Up @@ -448,14 +389,6 @@ impl<A: Codec, const K: usize> TryFrom<Seq<A>> for Kmer<A, K> {
}
}

/*
impl<A: Codec, const K: usize> From<Kmer<A, K>> for String {
fn from(kmer: Kmer<A, K>) -> Self {
kmer.to_string()
}
}
*/

impl<A: Codec, const K: usize, S: KmerStorage> PartialEq<SeqArray<A, K, 1>> for Kmer<A, K, S> {
fn eq(&self, seq: &SeqArray<A, K, 1>) -> bool {
if seq.len() != K {
Expand Down
2 changes: 0 additions & 2 deletions bio-seq/src/kmer/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use crate::codec::Codec;
use crate::kmer::{sealed, Kmer, KmerStorage};
//use crate::seq::{SeqSlice};
use crate::{Ba, Bs};
use bitvec::field::BitField;

Expand Down Expand Up @@ -62,7 +61,6 @@ impl<A: Codec, const K: usize> Hash for Kmer<A, K, simd::Simd<u64, 4>> {
#[cfg(test)]
mod tests {
use crate::prelude::*;
use crate::seq::SeqArray;
use core::simd;

#[test]
Expand Down
8 changes: 3 additions & 5 deletions bio-seq/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ pub mod error;
pub mod kmer;
pub mod seq;

//#[macro_use]
pub use bio_seq_derive::{dna, iupac};

#[doc(hidden)]
Expand Down Expand Up @@ -153,13 +152,12 @@ mod tests {
assert_eq!(iupac!("-").nth(0), Iupac::X);
}

/*
#[test]
fn into_usize() {
let a: usize = dna!("ACGT").into();
let a: usize = dna!("ACGT").to_owned().into_raw()[0];
assert_eq!(a, 0b11_10_01_00);

let b: usize = dna!("CGCG").into();
let b: usize = dna!("CGCG").to_owned().into_raw()[0];
assert_eq!(b, 0b10_01_10_01);

let c: usize = Seq::from(&vec![T, T]).into();
Expand All @@ -177,7 +175,7 @@ mod tests {
let g: usize = Seq::from(&vec![A]).into();
assert_eq!(g, 0b00);
}
*/

#[test]
fn test_display_aminos() {
let a: Seq<Amino> = Seq::from_str("DCMNLKG*HI").unwrap();
Expand Down
39 changes: 0 additions & 39 deletions bio-seq/src/seq/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::seq::Seq;
use crate::{Ba, Bs};

use bitvec::field::BitField;
//use bitvec::prelude::*;

use core::fmt;
use core::marker::PhantomData;
Expand Down Expand Up @@ -56,44 +55,6 @@ impl<A: Codec, const N: usize> From<&SeqArray<A, N, 1>> for usize {
}
}

/*
impl<A: Codec, const N: usize, const W: usize, const M: usize, const V: usize>
PartialEq<SeqArray<A, N, W>> for SeqArray<A, M, V>
{
fn eq(&self, other: &SeqArray<A, N, W>) -> bool {
if N == M {
self.ba == other.ba
} else {
false
}
}
}
impl<A: Codec, const N: usize, const W: usize> PartialEq<Seq<A>> for SeqArray<A, N, W> {
fn eq(&self, other: &Seq<A>) -> bool {
self.as_ref() == other.as_ref()
}
}
impl<A: Codec, const N: usize, const W: usize> PartialEq<Seq<A>> for &SeqArray<A, N, W> {
fn eq(&self, other: &Seq<A>) -> bool {
self.as_ref() == other.as_ref()
}
}
impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqSlice<A>> for SeqArray<A, N, W> {
fn eq(&self, other: &SeqSlice<A>) -> bool {
self.as_ref() == other
}
}
impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqSlice<A>> for &SeqArray<A, N, W> {
fn eq(&self, other: &SeqSlice<A>) -> bool {
self.as_ref() == other
}
}
*/

impl<A: Codec, const N: usize, const W: usize> fmt::Display for SeqArray<A, N, W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_ref(), f)
Expand Down
49 changes: 21 additions & 28 deletions bio-seq/src/seq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
//! ```
//! use std::collections::HashMap;
//! use bio_seq::prelude::*;
//! use bio_seq::seq;
//!
//! let reference = dna!("ACGTTCGCATGCTACGACGATC");
//!
//! let mut table: HashMap<&SeqSlice<Dna>, usize> = HashMap::new();
//! let mut table: HashMap<Seq<Dna>, usize> = HashMap::new();
//!
//! // Associate some kind of count with sequences as keys:
//! table.insert(dna!("ACGTT"), 1);
//! table.insert(dna!("ACACCCCC"), 0);
//! table.insert(dna!("ACGTT").into(), 1);
//! table.insert(dna!("ACACCCCC").into(), 0);
//!
//! // The query is a short window in the reference `Seq`
//! let query: &SeqSlice<Dna> = &reference[..5];
//!
//! if let Some(value) = table.get(&query) {
//! if let Some(value) = table.get(query) {
//! // `SeqSlice` implements `Display`
//! println!("{query}: {value}");
//! }
Expand Down Expand Up @@ -70,7 +69,7 @@ pub struct Seq<A: Codec> {

impl<A: Codec> From<Seq<A>> for usize {
fn from(slice: Seq<A>) -> usize {
assert!(slice.bv.len() <= usize::BITS as usize);
debug_assert!(slice.bv.len() <= usize::BITS as usize);
slice.bv.load_le::<usize>() //.wrapping_shr(shift)
}
}
Expand Down Expand Up @@ -208,13 +207,17 @@ impl<A: Codec> Seq<A> {
}

/// **Experimental**
pub fn from_raw(len: usize, bits: &[usize]) -> Self {
pub fn from_raw(len: usize, bits: &[usize]) -> Option<Self> {
let mut bv: Bv = Bv::from_slice(bits);
bv.truncate(len * A::BITS as usize);

Seq {
_p: PhantomData,
bv,
//debug_assert!(len <= bv.len(), "desired length is greater than provided bits string");
if len > bv.len() {
None
} else {
bv.truncate(len * A::BITS as usize);
Some(Seq {
_p: PhantomData,
bv,
})
}
}

Expand All @@ -224,20 +227,6 @@ impl<A: Codec> Seq<A> {
}
}

/*
impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqArray<A, N, W>> for Seq<A> {
fn eq(&self, other: &SeqArray<A, N, W>) -> bool {
self.as_ref() == other.as_ref()
}
}
impl<A: Codec, const N: usize, const W: usize> PartialEq<&SeqArray<A, N, W>> for Seq<A> {
fn eq(&self, other: &&SeqArray<A, N, W>) -> bool {
self.as_ref() == other.as_ref()
}
}
*/

impl<A: Codec> PartialEq<SeqSlice<A>> for Seq<A> {
fn eq(&self, other: &SeqSlice<A>) -> bool {
self.as_ref() == other
Expand Down Expand Up @@ -1042,9 +1031,13 @@ mod tests {
let seq: Seq<Dna> = s.try_into().unwrap();
let raw = seq.into_raw();
let new = Seq::<Dna>::from_raw(s.len(), raw);
assert_eq!(new, seq);
assert_eq!(new.unwrap(), seq);

let bad = Seq::<Dna>::from_raw(s.len() + 1, raw);
assert_ne!(bad.unwrap(), seq);

let bad = Seq::<Dna>::from_raw(342, raw);
assert_ne!(bad, seq);
assert_eq!(bad, None);
}
#[test]
fn test_seq_and_seq_slice_eq_and_hash() {
Expand Down
27 changes: 1 addition & 26 deletions bio-seq/src/seq/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
// This file may not be copied, modified, or distributed
// except according to those terms.

//use crate::codec::{Codec, Complement};
use crate::codec::{Codec, Complement};
use crate::error::ParseBioError;
//use crate::seq::array::SeqArray;
use crate::seq::ReverseComplement;
use crate::seq::Seq;

use crate::Bs;
use bitvec::field::BitField;
//use bitvec::prelude::*;

use core::fmt;
use core::hash::{Hash, Hasher};
Expand Down Expand Up @@ -82,20 +79,6 @@ impl<A: Codec> From<&SeqSlice<A>> for String {
}
}

/*
impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqArray<A, N, W>> for &SeqSlice<A> {
fn eq(&self, other: &SeqArray<A, N, W>) -> bool {
*self == other.as_ref()
}
}
impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqArray<A, N, W>> for SeqSlice<A> {
fn eq(&self, other: &SeqArray<A, N, W>) -> bool {
self == other.as_ref()
}
}
*/

impl<A: Codec> PartialEq<SeqSlice<A>> for SeqSlice<A> {
fn eq(&self, other: &SeqSlice<A>) -> bool {
self.bs == other.bs
Expand All @@ -108,14 +91,6 @@ impl<A: Codec> PartialEq<SeqSlice<A>> for &SeqSlice<A> {
}
}

/*
impl<A: Codec> PartialEq<&SeqSlice<A>> for &SeqSlice<A> {
fn eq(&self, other: &&SeqSlice<A>) -> bool {
self.bs == other.bs
}
}
*/

impl<A: Codec> PartialEq<Seq<A>> for SeqSlice<A> {
fn eq(&self, other: &Seq<A>) -> bool {
self == other.as_ref()
Expand Down Expand Up @@ -148,7 +123,7 @@ impl<A: Codec> PartialEq<&str> for SeqSlice<A> {
}
}

impl<A: Codec> Hash for &SeqSlice<A> {
impl<A: Codec> Hash for SeqSlice<A> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.bs.hash(state);
// prepend length to make robust against matching prefixes
Expand Down
Loading

0 comments on commit c7c67f0

Please sign in to comment.