Skip to content

Commit

Permalink
rearrange Seq/SeqSlice methods, implement mutable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-k committed Oct 14, 2024
1 parent 6c5ee12 commit ccd4995
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 103 deletions.
64 changes: 4 additions & 60 deletions bio-seq/src/seq/index.rs
Original file line number Diff line number Diff line change
@@ -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<A: Codec> Index<Range<usize>> for Seq<A> {
type Output = SeqSlice<A>;

fn index(&self, range: Range<usize>) -> &Self::Output {
self.as_ref().index(range)
}
}

impl<A: Codec> Index<RangeTo<usize>> for Seq<A> {
type Output = SeqSlice<A>;

fn index(&self, range: RangeTo<usize>) -> &Self::Output {
self.as_ref().index(range)
}
}

impl<A: Codec> Index<RangeToInclusive<usize>> for Seq<A> {
type Output = SeqSlice<A>;

fn index(&self, range: RangeToInclusive<usize>) -> &Self::Output {
self.as_ref().index(range)
}
}

impl<A: Codec> Index<RangeInclusive<usize>> for Seq<A> {
type Output = SeqSlice<A>;

fn index(&self, range: RangeInclusive<usize>) -> &Self::Output {
self.as_ref().index(range)
}
}

impl<A: Codec> Index<RangeFrom<usize>> for Seq<A> {
type Output = SeqSlice<A>;

fn index(&self, range: RangeFrom<usize>) -> &Self::Output {
self.as_ref().index(range)
}
}

impl<A: Codec> Index<RangeFull> for Seq<A> {
type Output = SeqSlice<A>;

fn index(&self, range: RangeFull) -> &Self::Output {
self.as_ref().index(range)
}
}

impl<A: Codec> Index<usize> for Seq<A> {
type Output = SeqSlice<A>;

fn index(&self, i: usize) -> &Self::Output {
self.as_ref().index(i)
}
}

impl<A: Codec> Index<Range<usize>> for SeqSlice<A> {
type Output = SeqSlice<A>;

Expand Down Expand Up @@ -133,8 +75,10 @@ impl<A: Codec> Index<usize> for SeqSlice<A> {
}
}

/*
impl<A: Codec> IndexMut<usize> for Seq<A> {
fn index_mut(&mut self, _index: usize) -> &mut SeqSlice<A> {
unimplemented!()
}
}
*/
10 changes: 1 addition & 9 deletions bio-seq/src/seq/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ impl<'a, A: Codec> SeqSlice<A> {
pub fn iter(&'a self) -> SeqIter<'a, A> {
<&Self as IntoIterator>::into_iter(self)
}
}

impl<A: Codec> SeqSlice<A> {
/// Iterate over sliding windows of size K
/// Iterate over sliding windows of length K
pub fn kmers<const K: usize>(&self) -> KmerIter<A, K> {
KmerIter::<A, K> {
slice: self,
Expand All @@ -57,10 +55,6 @@ impl<A: Codec> SeqSlice<A> {

/// 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::*;
///
Expand All @@ -82,8 +76,6 @@ impl<A: Codec> SeqSlice<A> {
/// The last incomplete chunk will be excluded if the sequence length is not divisible by the specified
/// width.
///
/// Example:
///
/// ```
/// use bio_seq::prelude::*;
///
Expand Down
46 changes: 13 additions & 33 deletions bio-seq/src/seq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -140,28 +138,6 @@ impl<A: Codec> Seq<A> {
}
}

/// 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<A> {
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<A>) -> Seq<A> {
Seq::<A> {
_p: PhantomData,
Expand All @@ -186,20 +162,24 @@ impl<A: Codec> Seq<A> {
self.bv.clear();
}

pub fn pop(&mut self) -> Option<A> {
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<A>) {
self.bv.extend_from_bitslice(&other.bs);
}

pub fn remove(&mut self, _index: usize) -> A {
unimplemented!()
pub fn splice(&mut self, _other: &SeqSlice<A>, _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::<Bv>();
A::unsafe_from_bits(bits.load_le())
}

pub fn extend<I: IntoIterator<Item = A>>(&mut self, iter: I) {
Expand Down
11 changes: 10 additions & 1 deletion bio-seq/src/seq/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<A: Codec> TryFrom<&SeqSlice<A>> for usize {

impl<A: Codec> From<&SeqSlice<A>> for u8 {
fn from(slice: &SeqSlice<A>) -> u8 {
assert!(slice.bs.len() <= u8::BITS as usize);
debug_assert!(slice.bs.len() <= u8::BITS as usize);
slice.bs.load_le::<u8>()
}
}
Expand All @@ -68,6 +68,15 @@ impl<A: Codec> SeqSlice<A> {
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<A> {
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
}
Expand Down

0 comments on commit ccd4995

Please sign in to comment.