Last active
July 7, 2024 12:48
-
-
Save mbhall88/cd900add6335c96127efea0e0f6a9f48 to your computer and use it in GitHub Desktop.
Reverse complement DNA sequences in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Reverse complement a DNA sequence with bit-twiddling | |
/// | |
/// # Example | |
/// | |
/// ```rust | |
/// let seq = b"ATGCTTCCAGAA"; | |
/// | |
/// let actual = revcomp(seq); | |
/// let expected = b"TTCTGGAAGCAT";/// | |
/// assert_eq!(actual, expected) | |
/// ``` | |
/// | |
/// # Note | |
/// Implementation is taken from https://doi.org/10.1101/082214 | |
pub fn revcomp(seq: &[u8]) -> Vec<u8> { | |
seq.iter() | |
.rev() | |
.map(|c| if c & 2 != 0 { c ^ 4 } else { c ^ 21 }) | |
.collect() | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn test_revcomp() { | |
let seq = b"ATGCTTCCAGAA"; | |
let actual = revcomp(seq); | |
let expected = b"TTCTGGAAGCAT"; | |
assert_eq!(actual, expected) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment