Skip to main content

OneHotEncoder

Struct OneHotEncoder 

Source
pub struct OneHotEncoder { /* private fields */ }
Expand description

One-hot encoder for biological sequences.

Encodes sequences as binary matrices where each position is represented as a one-hot vector. For DNA: A=[1,0,0,0], C=[0,1,0,0], G=[0,0,1,0], T=[0,0,0,1]

§Examples

use deepbiop_fa::encode::onehot::{OneHotEncoder, AmbiguousStrategy};
use deepbiop_core::types::EncodingType;

let encoder = OneHotEncoder::new(EncodingType::DNA, AmbiguousStrategy::Skip);
let encoded = encoder.encode(b"ACGT").unwrap();
assert_eq!(encoded.shape(), &[4, 4]); // 4 bases, 4 possible values

Implementations§

Source§

impl OneHotEncoder

Source

pub fn new( encoding_type: EncodingType, ambiguous_strategy: AmbiguousStrategy, ) -> OneHotEncoder

Create a new one-hot encoder.

§Arguments
  • encoding_type - The type of sequence (DNA, RNA, or Protein)
  • ambiguous_strategy - How to handle ambiguous bases
§Returns

A new OneHotEncoder instance

Source

pub fn with_seed( encoding_type: EncodingType, ambiguous_strategy: AmbiguousStrategy, seed: u64, ) -> OneHotEncoder

Create a new one-hot encoder with a random seed.

§Arguments
  • encoding_type - The type of sequence (DNA, RNA, or Protein)
  • ambiguous_strategy - How to handle ambiguous bases
  • seed - Random seed for reproducible random replacements
Source

pub fn encode( &self, sequence: &[u8], ) -> Result<ArrayBase<OwnedRepr<f32>, Dim<[usize; 2]>>, Error>

Encode a single sequence as a one-hot matrix.

§Arguments
  • sequence - The sequence to encode
§Returns

A 2D array of shape [sequence_length, alphabet_size]

§Errors

Returns an error if:

  • The sequence contains invalid characters for the encoding type
  • The sequence contains ambiguous bases and strategy is Skip
Source

pub fn encode_batch( &self, sequences: &[&[u8]], ) -> Result<ArrayBase<OwnedRepr<f32>, Dim<[usize; 3]>>, Error>

Encode multiple sequences in parallel.

All sequences are padded to the length of the longest sequence with zeros.

§Arguments
  • sequences - Slice of sequences to encode
§Returns

A 3D array of shape [num_sequences, max_length, alphabet_size]

§Errors

Returns an error if any sequence fails to encode

Source

pub fn encoding_type(&self) -> EncodingType

Get the encoding type.

Source

pub fn ambiguous_strategy(&self) -> AmbiguousStrategy

Get the ambiguous strategy.

Trait Implementations§

Source§

impl SequenceEncoder for OneHotEncoder

Source§

type EncodeOutput = ArrayBase<OwnedRepr<f32>, Dim<[usize; 2]>>

The type of the encoded output Read more
Source§

fn encode_sequence( &self, seq: &[u8], qual: Option<&[u8]>, ) -> Result<<OneHotEncoder as SequenceEncoder>::EncodeOutput, Error>

Encodes a biological sequence into a numerical representation Read more
Source§

fn expected_output_size(&self, seq_len: usize) -> usize

Returns the expected size of the encoded output Read more
Source§

fn batch_encode( &self, sequences: &[(&[u8], Option<&[u8]>)], ) -> Result<Vec<Self::EncodeOutput>, Error>

Encodes multiple sequences in batch for potential performance optimization Read more
Source§

fn validate_input(&self, seq: &[u8], qual: Option<&[u8]>) -> Result<(), Error>

Validates that a sequence and quality scores are compatible Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V