use atomic_counter::AtomicCounter as _;
use integer_encoding::*;
use std::sync::Mutex;
pub struct KmerMatch {
pub id_read: usize,
pub position: usize,
pub forward: bool,
}
pub trait KmerCounter: Default + Sync + Send {
fn add_match(&self, m: KmerMatch);
fn to_string(&self) -> String;
fn get_count(&self) -> usize;
}
impl KmerCounter for atomic_counter::RelaxedCounter {
fn add_match(&self, _m: KmerMatch) {
self.inc();
}
fn to_string(&self) -> String {
self.get().to_string()
}
fn get_count(&self) -> usize {
self.get()
}
}
#[derive(Default)]
pub struct KmerCounterWithLog {
count: usize,
log: Vec<u8>,
}
impl KmerCounterWithLog {
fn iter_matches(&self) -> impl std::iter::Iterator<Item = (usize, usize, bool)> + '_ {
let mut cursor = std::io::Cursor::new(&self.log);
std::iter::from_fn(move || {
let id_read = match cursor.read_varint::<usize>() {
Ok(v) => v,
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return None,
_ => unreachable!(),
};
let (position, stranded) = match cursor.read_varint::<i64>() {
Ok(v) if v < 0 => (-v, true),
Ok(v) if v >= 0 => (v, false),
_ => unreachable!(),
};
Some((id_read, position as usize, stranded))
})
}
}
impl KmerCounter for Mutex<KmerCounterWithLog> {
fn add_match(&self, m: KmerMatch) {
let mut counter = self.lock().unwrap();
counter.count += 1;
counter.log.write_varint(m.id_read).unwrap();
counter
.log
.write_varint(if m.forward {
-(m.position as i64)
} else {
m.position as i64
})
.unwrap();
}
fn to_string(&self) -> String {
let counter = self.lock().unwrap();
let mut result = String::new(); for (id_read, position, stranded) in counter.iter_matches() {
result.push_str(&format!("({},{},{}) ", id_read, position, stranded));
}
result }
fn get_count(&self) -> usize {
self.lock().unwrap().count
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn relaxed_counter() {
let counter = atomic_counter::RelaxedCounter::new(0);
assert_eq!(counter.get_count(), 0);
assert_eq!(counter.to_string(), "0".to_string());
for i in 1..11 {
counter.add_match(KmerMatch {
id_read: 0,
position: 0,
forward: true,
});
assert_eq!(counter.get_count(), i);
assert_eq!(counter.to_string(), format!("{}", i));
}
assert_eq!(counter.get_count(), 10);
assert_eq!(counter.to_string(), "10".to_string());
}
#[test]
fn conter_with_log() {
let object = KmerCounterWithLog {
count: 0,
log: vec![],
};
let counter = Mutex::new(object);
assert_eq!(counter.get_count(), 0);
assert_eq!(counter.to_string(), "".to_string());
for i in 1..11 {
counter.add_match(KmerMatch {
id_read: i,
position: i,
forward: (i % 2) != 0,
});
assert_eq!(counter.get_count(), i);
}
assert_eq!(counter.get_count(), 10);
assert_eq!(counter.to_string(), "(1,1,true) (2,2,false) (3,3,true) (4,4,false) (5,5,true) (6,6,false) (7,7,true) (8,8,false) (9,9,true) (10,10,false) ".to_string());
}
}