#![allow(clippy::cast_precision_loss)]
use mappy_core::types::MapletConfig;
use mappy_core::{CounterOperator, Maplet};
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("K-mer Counting with Maplets");
println!("===========================");
let config = MapletConfig::new(10000, 0.001); let kmer_counter = Maplet::<String, u32, CounterOperator>::with_config(config)?;
let sequences = vec![
"ATCGATCGATCG",
"GCTAGCTAGCTA",
"ATCGATCGATCG", "TTTTTTTTTTTT",
"ATCGATCGATCG", ];
let k = 3;
println!("Counting {}-mers in {} sequences...", k, sequences.len());
for (i, sequence) in sequences.iter().enumerate() {
println!("Processing sequence {}: {}", i + 1, sequence);
for j in 0..=sequence.len() - k {
let kmer = &sequence[j..j + k];
kmer_counter.insert(kmer.to_string(), 1).await?;
}
}
let test_kmers = vec!["ATC", "GAT", "TAG", "TTT", "XYZ"];
println!("\nK-mer counts:");
for kmer in &test_kmers {
let count = kmer_counter.query(&kmer.to_string()).await;
println!(" {}: {:?}", kmer, count);
}
let mut hashmap_counter: HashMap<String, u32> = HashMap::new();
for sequence in &sequences {
for j in 0..=sequence.len() - k {
let kmer = &sequence[j..j + k];
*hashmap_counter.entry(kmer.to_string()).or_insert(0) += 1;
}
}
println!("\nComparison with HashMap:");
for kmer in &test_kmers {
let maplet_count = kmer_counter.query(&kmer.to_string()).await;
let hashmap_count = hashmap_counter.get(&kmer.to_string());
println!(
" {}: Maplet={:?}, HashMap={:?}",
kmer, maplet_count, hashmap_count
);
}
let stats = kmer_counter.stats().await;
println!("\nMaplet Statistics:");
println!(" Capacity: {}", stats.capacity);
println!(" Items: {}", stats.len);
println!(" Load factor: {:.2}%", stats.load_factor * 100.0);
println!(" Memory usage: {} bytes", stats.memory_usage);
println!(" False positive rate: {:.4}", stats.false_positive_rate);
let hashmap_memory =
hashmap_counter.len() * (std::mem::size_of::<String>() + std::mem::size_of::<u32>());
let memory_savings = (1.0 - stats.memory_usage as f64 / hashmap_memory as f64) * 100.0;
println!("\nMemory Efficiency:");
println!(" HashMap memory: {} bytes", hashmap_memory);
println!(" Maplet memory: {} bytes", stats.memory_usage);
println!(" Memory savings: {:.1}%", memory_savings);
Ok(())
}