From ccd49950b403678238deebbd4b6b009feecdcdc4 Mon Sep 17 00:00:00 2001 From: jeff-k Date: Mon, 14 Oct 2024 02:16:49 +0100 Subject: [PATCH] rearrange Seq/SeqSlice methods, implement mutable methods --- bio-seq/src/seq/index.rs | 64 +++--------------------------------- bio-seq/src/seq/iterators.rs | 10 +----- bio-seq/src/seq/mod.rs | 46 ++++++++------------------ bio-seq/src/seq/slice.rs | 11 ++++++- 4 files changed, 28 insertions(+), 103 deletions(-) diff --git a/bio-seq/src/seq/index.rs b/bio-seq/src/seq/index.rs index 99b2b2c..a92404b 100644 --- a/bio-seq/src/seq/index.rs +++ b/bio-seq/src/seq/index.rs @@ -1,68 +1,10 @@ -use core::ops::{ - Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, -}; +use core::ops::{Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}; use core::ptr; use crate::codec::Codec; -use crate::seq::{Seq, SeqSlice}; +use crate::seq::SeqSlice; use crate::Bs; -impl Index> for Seq { - type Output = SeqSlice; - - fn index(&self, range: Range) -> &Self::Output { - self.as_ref().index(range) - } -} - -impl Index> for Seq { - type Output = SeqSlice; - - fn index(&self, range: RangeTo) -> &Self::Output { - self.as_ref().index(range) - } -} - -impl Index> for Seq { - type Output = SeqSlice; - - fn index(&self, range: RangeToInclusive) -> &Self::Output { - self.as_ref().index(range) - } -} - -impl Index> for Seq { - type Output = SeqSlice; - - fn index(&self, range: RangeInclusive) -> &Self::Output { - self.as_ref().index(range) - } -} - -impl Index> for Seq { - type Output = SeqSlice; - - fn index(&self, range: RangeFrom) -> &Self::Output { - self.as_ref().index(range) - } -} - -impl Index for Seq { - type Output = SeqSlice; - - fn index(&self, range: RangeFull) -> &Self::Output { - self.as_ref().index(range) - } -} - -impl Index for Seq { - type Output = SeqSlice; - - fn index(&self, i: usize) -> &Self::Output { - self.as_ref().index(i) - } -} - impl Index> for SeqSlice { type Output = SeqSlice; @@ -133,8 +75,10 @@ impl Index for SeqSlice { } } +/* impl IndexMut for Seq { fn index_mut(&mut self, _index: usize) -> &mut SeqSlice { unimplemented!() } } +*/ diff --git a/bio-seq/src/seq/iterators.rs b/bio-seq/src/seq/iterators.rs index 1dd7054..0e88c8a 100644 --- a/bio-seq/src/seq/iterators.rs +++ b/bio-seq/src/seq/iterators.rs @@ -34,10 +34,8 @@ impl<'a, A: Codec> SeqSlice { pub fn iter(&'a self) -> SeqIter<'a, A> { <&Self as IntoIterator>::into_iter(self) } -} -impl SeqSlice { - /// Iterate over sliding windows of size K + /// Iterate over sliding windows of length K pub fn kmers(&self) -> KmerIter { KmerIter:: { slice: self, @@ -57,10 +55,6 @@ impl SeqSlice { /// Iterate over the sequence in overlapping windows of a specified width /// - /// This will panic if the length of the window is greater than the length of the sequence. - /// - /// Example: - /// /// ``` /// use bio_seq::prelude::*; /// @@ -82,8 +76,6 @@ impl SeqSlice { /// The last incomplete chunk will be excluded if the sequence length is not divisible by the specified /// width. /// - /// Example: - /// /// ``` /// use bio_seq::prelude::*; /// diff --git a/bio-seq/src/seq/mod.rs b/bio-seq/src/seq/mod.rs index 506a6f2..d01e4e5 100644 --- a/bio-seq/src/seq/mod.rs +++ b/bio-seq/src/seq/mod.rs @@ -48,13 +48,11 @@ use bitvec::view::BitView; use serde::{Deserialize, Serialize}; use core::borrow::Borrow; -use core::fmt; use core::hash::{Hash, Hasher}; use core::marker::PhantomData; use core::ops::Deref; -use core::ptr; -use core::str; use core::str::FromStr; +use core::{fmt, ptr, str}; /// A arbitrary length sequence of bit-packed symbols /// @@ -140,28 +138,6 @@ impl Seq { } } - /// Unsafely index into a sequence. - pub fn nth(&self, i: usize) -> A { - A::unsafe_from_bits(self[i].into()) - } - - /// Get the `i`th element of a `Seq`. Returns `None` if index out of range. - pub fn get(&self, i: usize) -> Option { - if i >= self.bv.len() / A::BITS as usize { - None - } else { - Some(A::unsafe_from_bits(self[i].into())) - } - } - - pub fn len(&self) -> usize { - self.bv.len() / A::BITS as usize - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - pub fn bit_and(self, rhs: Seq) -> Seq { Seq:: { _p: PhantomData, @@ -186,20 +162,24 @@ impl Seq { self.bv.clear(); } - pub fn pop(&mut self) -> Option { - unimplemented!() + pub fn truncate(&mut self, len: usize) { + self.bv.truncate(len * A::BITS as usize); } - pub fn truncate(&mut self, _len: usize) { - unimplemented!() + pub fn append(&mut self, other: &SeqSlice) { + self.bv.extend_from_bitslice(&other.bs); } - pub fn remove(&mut self, _index: usize) -> A { - unimplemented!() + pub fn splice(&mut self, _other: &SeqSlice, _index: usize) { + todo!() } - pub fn insert(&mut self, _index: usize, _element: A) { - unimplemented!() + pub fn remove(&mut self, index: usize) -> A { + debug_assert!(index <= self.len(), "Sequence index out of range"); + let start = index * A::BITS as usize; + let end = start + A::BITS as usize; + let bits = self.bv.drain(start..end).collect::(); + A::unsafe_from_bits(bits.load_le()) } pub fn extend>(&mut self, iter: I) { diff --git a/bio-seq/src/seq/slice.rs b/bio-seq/src/seq/slice.rs index d01716e..63ac0da 100644 --- a/bio-seq/src/seq/slice.rs +++ b/bio-seq/src/seq/slice.rs @@ -42,7 +42,7 @@ impl TryFrom<&SeqSlice> for usize { impl From<&SeqSlice> for u8 { fn from(slice: &SeqSlice) -> u8 { - assert!(slice.bs.len() <= u8::BITS as usize); + debug_assert!(slice.bs.len() <= u8::BITS as usize); slice.bs.load_le::() } } @@ -68,6 +68,15 @@ impl SeqSlice { self.bs.len() / A::BITS as usize } + /// Get the `i`th element of a `Seq`. Returns `None` if index out of range. + pub fn get(&self, i: usize) -> Option { + if i >= self.bs.len() / A::BITS as usize { + None + } else { + Some(A::unsafe_from_bits(self[i].into())) + } + } + pub fn is_empty(&self) -> bool { self.len() == 0 }