bitnuc

Function from_2bit

Source
pub fn from_2bit(
    packed: u64,
    expected_size: usize,
    sequence: &mut Vec<u8>,
) -> Result<(), NucleotideError>
Expand description

Converts a 2-bit packed representation back into a nucleotide sequence.

This function reverses the packing performed by as_2bit.

§Arguments

  • packed - A u64 containing the 2-bit packed sequence
  • expected_size - The number of bases to unpack

§Returns

Returns a Vec<u8> containing the ASCII sequence.

§Errors

Returns NucleotideError::InvalidLength if expected_size is greater than 32 (as a u64 can only store 32 * 2 bits).

§Examples

Basic unpacking:

use bitnuc::{as_2bit, from_2bit};

// Pack and unpack
let packed = as_2bit(b"ACGT")?;
let mut unpacked = Vec::new();
from_2bit(packed, 4, &mut unpacked)?;
assert_eq!(&unpacked, b"ACGT");
unpacked.clear();

// Partial unpacking
from_2bit(packed, 2, &mut unpacked)?;
assert_eq!(&unpacked, b"AC");

Error handling:

use bitnuc::{from_2bit, NucleotideError};

// Length too long
assert!(matches!(
    from_2bit(0, 33, &mut Vec::new()),
    Err(NucleotideError::InvalidLength(33))
));

§Implementation Details

The bases are unpacked from least significant to most significant bits:

use bitnuc::{as_2bit, from_2bit};

let packed = 0b11100100; // "ACGT" in 2-bit encoding
let mut seq = Vec::new();
from_2bit(packed, 4, &mut seq)?;
assert_eq!(seq[0], b'A'); // 00
assert_eq!(seq[1], b'C'); // 01
assert_eq!(seq[2], b'G'); // 10
assert_eq!(seq[3], b'T'); // 11