Skip to content

Commit

Permalink
expose and reconstruct Seq from raw &[usize] (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-k committed Oct 10, 2024
1 parent a71f46d commit 7b96b96
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
39 changes: 39 additions & 0 deletions bio-seq/src/seq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ impl<A: Codec> Seq<A> {
pub fn extend<I: IntoIterator<Item = A>>(&mut self, iter: I) {
iter.into_iter().for_each(|base| self.push(base));
}

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

Seq {
_p: PhantomData,
bv,
}
}

/// **Experimental**
pub fn into_raw(&self) -> &[usize] {
self.bv.as_raw_slice()
}
}

impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqArray<A, N, W>> for Seq<A> {
Expand Down Expand Up @@ -471,6 +487,17 @@ impl From<Vec<usize>> for Seq<text::Dna> {
}
}

/// **Unstable** construct a `Seq` from a bitslice. This may change in the future.
impl<A: Codec> From<&Bs> for Seq<A> {
fn from(bs: &Bs) -> Self {
Seq {
_p: PhantomData,
bv: bs.into(),
}
}
}

/// **Unstable** construct a `Seq` from a bitvec. This may change in the future.
impl<A: Codec> From<Bv> for Seq<A> {
fn from(bv: Bv) -> Self {
Seq {
Expand Down Expand Up @@ -1014,6 +1041,18 @@ mod tests {

assert_eq!(s.to_string(), x);
}

#[test]
fn test_to_from_raw() {
let s = "TCAGCTAGCTACGACTGATCGATCGACTGATGCCGCGCGCGGCGCCGCGCGCGCGCGCCGCGCGCCCCGCGCGCGGCGCGCGCCGCGCGCGCGCGCGGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC";

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);
let bad = Seq::<Dna>::from_raw(342, raw);
assert_ne!(bad, seq);
}
/*
#[test]
fn test_seq_and_seq_slice_eq_and_hash() {
Expand Down
6 changes: 3 additions & 3 deletions bio-seq/src/seq/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use crate::error::ParseBioError;
use crate::seq::ReverseComplement;
use crate::seq::Seq;

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

use core::fmt;
//use core::hash::{Hash, Hasher};
Expand All @@ -26,7 +26,7 @@ use core::ops::{BitAnd, BitOr};
#[repr(transparent)]
pub struct SeqSlice<A: Codec> {
pub(crate) _p: PhantomData<A>,
pub(crate) bs: BitSlice<usize, Order>,
pub(crate) bs: Bs,
}

impl<A: Codec> TryFrom<&SeqSlice<A>> for usize {
Expand Down

0 comments on commit 7b96b96

Please sign in to comment.