biomics 0.0.1

Early-stage bioinformatics primitives for the Biomics project
Documentation
//! Early-stage bioinformatics primitives for Biomics.

/// Calculate the fraction of DNA bases that are G or C.
///
/// ASCII case is ignored. Non-ACGT characters are excluded from the
/// denominator.
///
/// # Examples
///
/// ```
/// assert_eq!(biomics::gc_fraction(b"ACGT"), Some(0.5));
/// ```
#[must_use]
pub fn gc_fraction(sequence: &[u8]) -> Option<f64> {
    let mut valid = 0usize;
    let mut gc = 0usize;

    for base in sequence {
        match base.to_ascii_uppercase() {
            b'G' | b'C' => {
                valid += 1;
                gc += 1;
            }
            b'A' | b'T' => valid += 1,
            _ => {}
        }
    }

    (valid != 0).then(|| gc as f64 / valid as f64)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn calculates_gc_fraction() {
        assert_eq!(gc_fraction(b"ACGT"), Some(0.5));
        assert_eq!(gc_fraction(b"GGCC"), Some(1.0));
    }

    #[test]
    fn ignores_unknown_characters() {
        assert_eq!(gc_fraction(b"GCNN"), Some(1.0));
    }

    #[test]
    fn handles_empty_sequences() {
        assert_eq!(gc_fraction(b""), None);
    }
}