Skip to content

Instantly share code, notes, and snippets.

@mbhall88
Last active July 2, 2023 01:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbhall88/cd900add6335c96127efea0e0f6a9f48 to your computer and use it in GitHub Desktop.
Save mbhall88/cd900add6335c96127efea0e0f6a9f48 to your computer and use it in GitHub Desktop.
Reverse complement DNA sequences in Rust
/// 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