#![deny(unsafe_code)]
#![allow(
clippy::must_use_candidate,
clippy::missing_panics_doc,
clippy::missing_errors_doc,
clippy::module_name_repetitions
)]
pub mod color;
pub mod matcher;
use lazy_static::lazy_static;
use std::{borrow::Borrow, path::Path};
pub const DNA_BASES: [u8; 5] = *b"ACGTN";
pub const IUPAC_BASES: [u8; 15] = *b"AGCTYRWSKMDVHBN";
pub const IUPAC_BASES_COMPLEMENT: [u8; 15] = *b"TCGARYWSMKHBDVN";
lazy_static! {
pub static ref COMPLEMENT: [u8; 256] = {
let mut comp = [0; 256];
for (v, a) in comp.iter_mut().enumerate() {
*a = v as u8;
}
for (&a, &b) in IUPAC_BASES.iter().zip(IUPAC_BASES_COMPLEMENT.iter()) {
comp[a as usize] = b;
comp[a as usize + 32] = b + 32; }
comp
};
}
fn complement(a: u8) -> u8 {
COMPLEMENT[a as usize]
}
fn reverse_complement<C, T>(text: T) -> Vec<u8>
where
C: Borrow<u8>,
T: IntoIterator<Item = C>,
T::IntoIter: DoubleEndedIterator,
{
text.into_iter()
.rev()
.map(|a| complement(*a.borrow()))
.collect()
}
fn is_path_with_extension<P: AsRef<Path>>(p: &P, extensions: [&str; 2]) -> bool {
if let Some(ext) = p.as_ref().extension() {
match ext.to_str() {
Some(x) => extensions.contains(&x),
None => false,
}
} else {
false
}
}
const GZIP_EXTENSIONS: [&str; 2] = ["gz", "bgz"];
pub fn is_gzip_path<P: AsRef<Path>>(p: &P) -> bool {
is_path_with_extension(p, GZIP_EXTENSIONS)
}
const FASTQ_EXTENSIONS: [&str; 2] = ["fastq", "fq"];
pub fn is_fastq_path<P: AsRef<Path>>(p: &P) -> bool {
is_path_with_extension(p, FASTQ_EXTENSIONS)
}
#[cfg(test)]
pub mod tests {
use crate::*;
use rstest::rstest;
use std::str;
use tempfile::TempDir;
#[rstest]
#[case("ACGT", "ACGT")] #[case("ACG", "CGT")] fn test_reverse_complement(#[case] seq: &str, #[case] expected: &str) {
let result = reverse_complement(seq.as_bytes());
let string_result = str::from_utf8(&result).unwrap();
assert_eq!(&string_result, &expected);
}
#[rstest]
#[case("test_fastq.fq.gz", true)] #[case("test_fastq.fq.bgz", true)] #[case("test_fastq.fq.tar", false)] fn test_is_gzip_path(#[case] file_name: &str, #[case] expected: bool) {
let dir = TempDir::new().unwrap();
let file_path = dir.path().join(file_name);
let result = is_gzip_path(&file_path);
assert_eq!(result, expected);
}
#[rstest]
#[case("test_fastq.fq", true)] #[case("test_fastq.fastq", true)] #[case("test_fastq.sam", false)] fn test_is_fastq_path(#[case] file_name: &str, #[case] expected: bool) {
let dir = TempDir::new().unwrap();
let file_path = dir.path().join(file_name);
let result = is_fastq_path(&file_path);
assert_eq!(result, expected);
}
}