From b1e9e8629b601d83fba60a327924ff4c474f08bc Mon Sep 17 00:00:00 2001 From: Andrew Frantz Date: Thu, 14 Dec 2023 11:59:26 -0500 Subject: [PATCH] tests(derive/endedness/comput): reimplement tests --- src/derive/command/endedness.rs | 2 +- src/derive/endedness/compute.rs | 516 +++++++++++++++++--------------- 2 files changed, 282 insertions(+), 236 deletions(-) diff --git a/src/derive/command/endedness.rs b/src/derive/command/endedness.rs index c111ebf..663a8ee 100644 --- a/src/derive/command/endedness.rs +++ b/src/derive/command/endedness.rs @@ -99,7 +99,7 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> { if !found_rgs.contains(&rg) { found_rgs.insert(Arc::new(rg.clone())); } - found_rgs.get(&rg).unwrap().clone() + Arc::clone(found_rgs.get(&rg).unwrap()) } None => Arc::clone(&UNKNOWN_READ_GROUP), }; diff --git a/src/derive/endedness/compute.rs b/src/derive/endedness/compute.rs index 4f934ed..2d8ef69 100644 --- a/src/derive/endedness/compute.rs +++ b/src/derive/endedness/compute.rs @@ -174,10 +174,10 @@ fn calculate_reads_per_template( let read_group_set: HashSet> = read_groups.iter().cloned().collect(); if read_group_set.len() == 1 { - let read_group = read_group_set.iter().next().unwrap().clone(); + let read_group = Arc::clone(read_group_set.iter().next().unwrap()); read_group_reads - .entry(read_group.clone()) + .entry(Arc::clone(&read_group)) .and_modify(|e| *e += num_reads) .or_insert(num_reads); read_group_templates @@ -191,7 +191,7 @@ fn calculate_reads_per_template( ); for read_group in read_groups { read_group_reads - .entry(read_group.clone()) + .entry(Arc::clone(read_group)) .and_modify(|e| *e += 1) .or_insert(1); } @@ -361,235 +361,281 @@ pub fn predict( Ok(final_result) } -// #[cfg(test)] -// mod tests { -// use super::*; - -// #[test] -// fn test_derive_endedness_from_all_zero_counts() { -// let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); -// ordering_flags.insert(Arc::clone(&OVERALL), OrderingFlagsCounts::new()); -// let result = predict(ordering_flags, Trie::new(), 0.0, false); -// assert!(result.is_err()); -// } - -// #[test] -// fn test_derive_endedness_from_only_first() { -// let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); -// ordering_flags.insert( -// Arc::clone(&OVERALL), -// OrderingFlagsCounts { -// first: 1, -// last: 0, -// both: 0, -// neither: 0, -// }, -// ); -// let result = predict(ordering_flags, Trie::new(), 0.0, false); -// assert!(result.is_ok()); -// let result = result.unwrap(); -// assert!(!result.succeeded); -// assert_eq!(result.endedness, "Unknown"); -// assert_eq!(result.first, 1); -// assert_eq!(result.last, 0); -// assert_eq!(result.both, 0); -// assert_eq!(result.neither, 0); -// assert_eq!(result.rpt, None); -// assert_eq!(result.read_groups.len(), 0); -// } -// -// #[test] -// fn test_derive_endedness_from_only_last() { -// let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); -// ordering_flags.insert( -// Arc::clone(&OVERALL), -// OrderingFlagsCounts { -// first: 0, -// last: 1, -// both: 0, -// neither: 0, -// }, -// ); -// let result = predict(ordering_flags, Trie::new(), 0.0, false); -// assert!(result.is_ok()); -// let result = result.unwrap(); -// assert!(!result.succeeded); -// assert_eq!(result.endedness, "Unknown"); -// assert_eq!(result.first, 0); -// assert_eq!(result.last, 1); -// assert_eq!(result.both, 0); -// assert_eq!(result.neither, 0); -// assert_eq!(result.rpt, None); -// assert_eq!(result.read_groups.len(), 0); -// } - -// #[test] -// fn test_derive_endedness_from_only_both() { -// let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); -// ordering_flags.insert( -// Arc::clone(&OVERALL), -// OrderingFlagsCounts { -// first: 0, -// last: 0, -// both: 1, -// neither: 0, -// }, -// ); -// let result = predict(ordering_flags, Trie::new(), 0.0, false); -// assert!(result.is_ok()); -// let result = result.unwrap(); -// assert!(result.succeeded); -// assert_eq!(result.endedness, "Single-End"); -// assert_eq!(result.first, 0); -// assert_eq!(result.last, 0); -// assert_eq!(result.both, 1); -// assert_eq!(result.neither, 0); -// assert_eq!(result.rpt, None); -// assert_eq!(result.read_groups.len(), 0); -// } - -// #[test] -// fn test_derive_endedness_from_only_neither() { -// let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); -// ordering_flags.insert( -// Arc::clone(&OVERALL), -// OrderingFlagsCounts { -// first: 0, -// last: 0, -// both: 0, -// neither: 1, -// }, -// ); -// let result = predict(ordering_flags, Trie::new(), 0.0, false); -// assert!(result.is_ok()); -// let result = result.unwrap(); -// assert!(!result.succeeded); -// assert_eq!(result.endedness, "Unknown"); -// assert_eq!(result.first, 0); -// assert_eq!(result.last, 0); -// assert_eq!(result.both, 0); -// assert_eq!(result.neither, 1); -// assert_eq!(result.rpt, None); -// assert_eq!(result.read_groups.len(), 0); -// } - -// #[test] -// fn test_derive_endedness_from_first_and_last() { -// let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); -// ordering_flags.insert( -// Arc::clone(&OVERALL), -// OrderingFlagsCounts { -// first: 1, -// last: 1, -// both: 0, -// neither: 0, -// }, -// ); -// let result = predict(ordering_flags, Trie::new(), 0.0, false); -// assert!(result.is_ok()); -// let result = result.unwrap(); -// assert!(result.succeeded); -// assert_eq!(result.endedness, "Paired-End"); -// assert_eq!(result.first, 1); -// assert_eq!(result.last, 1); -// assert_eq!(result.both, 0); -// assert_eq!(result.neither, 0); -// assert_eq!(result.rpt, None); -// assert_eq!(result.read_groups.len(), 0); -// } - -// #[test] -// fn test_calculate_reads_per_template() { -// let mut read_names: Trie>> = Trie::new(); -// let rg_paired = Arc::new("rg_paired".to_string()); -// let rg_single = Arc::new("rg_single".to_string()); -// read_names.insert( -// "read1".to_string(), -// vec![rg_paired.clone(), rg_paired.clone()], -// ); -// read_names.insert( -// "read2".to_string(), -// vec![rg_paired.clone(), rg_paired.clone(), rg_single.clone()], -// ); -// read_names.insert("read3".to_string(), vec![rg_single.clone()]); -// read_names.insert( -// "read4".to_string(), -// vec![rg_paired.clone(), rg_paired.clone()], -// ); -// read_names.insert( -// "read5".to_string(), -// vec![rg_paired.clone(), rg_paired.clone(), rg_single.clone()], -// ); -// let results = calculate_reads_per_template(read_names); -// assert_eq!(results.len(), 3); -// assert_eq!(results.get(&Arc::new("overall".to_string())).unwrap(), &2.2); -// assert_eq!(results.get(&rg_paired.clone()).unwrap(), &2.0); -// assert_eq!(results.get(&rg_single.clone()).unwrap(), &1.0); -// } - -// #[test] -// fn test_derive_endedness_from_first_and_last_with_rpt() { -// let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); -// let rg_paired = Arc::new("rg_paired".to_string()); -// let rg_single = Arc::new("rg_single".to_string()); -// ordering_flags.insert( -// Arc::new(OVERALL.to_string()), -// OrderingFlagsCounts { -// first: 8, -// last: 8, -// both: 2, -// neither: 0, -// }, -// ); -// ordering_flags.insert( -// rg_paired.clone(), -// OrderingFlagsCounts { -// first: 8, -// last: 8, -// both: 0, -// neither: 0, -// }, -// ); -// ordering_flags.insert( -// rg_single.clone(), -// OrderingFlagsCounts { -// first: 0, -// last: 0, -// both: 2, -// neither: 0, -// }, -// ); -// let mut read_names: Trie>> = Trie::new(); -// read_names.insert( -// "read1".to_string(), -// vec![rg_paired.clone(), rg_paired.clone()], -// ); -// read_names.insert( -// "read2".to_string(), -// vec![rg_paired.clone(), rg_paired.clone(), rg_single.clone()], -// ); -// read_names.insert("read3".to_string(), vec![rg_single.clone()]); -// read_names.insert( -// "read4".to_string(), -// vec![rg_paired.clone(), rg_paired.clone()], -// ); -// read_names.insert( -// "read5".to_string(), -// vec![rg_paired.clone(), rg_paired.clone(), rg_single.clone()], -// ); -// let result = predict(ordering_flags, read_names, 0.0, false); -// assert!(result.is_ok()); -// let result = result.unwrap(); -// assert!(!result.succeeded); -// assert_eq!(result.endedness, "Unknown"); -// assert_eq!(result.first, 8); -// assert_eq!(result.last, 8); -// assert_eq!(result.both, 2); -// assert_eq!(result.neither, 0); -// assert_eq!(result.rpt, Some(2.2)); -// assert_eq!(result.read_groups.len(), 2); -// // We can't know which read group will be first in the vector. -// // But both should succeed. -// assert!(result.read_groups[0].succeeded && result.read_groups[1].succeeded); -// } -// } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_predict_endedness() { + let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); + ordering_flags.insert( + Arc::clone(&OVERALL), + OrderingFlagsCounts { + first: 1, + last: 1, + both: 0, + neither: 0, + }, + ); + let result = predict_endedness( + "overall".to_string(), + &ordering_flags.get(&Arc::clone(&OVERALL)).unwrap(), + 0.0, + None, + false, + ); + assert!(result.is_ok()); + let result = result.unwrap(); + assert!(result.succeeded); + assert_eq!(result.endedness, "Paired-End"); + assert_eq!(result.first, 1); + assert_eq!(result.last, 1); + assert_eq!(result.both, 0); + assert_eq!(result.neither, 0); + assert_eq!(result.rpt, None); + } + + #[test] + fn test_derive_endedness_from_all_zero_counts() { + let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); + ordering_flags.insert(Arc::clone(&OVERALL), OrderingFlagsCounts::new()); + let result = predict(ordering_flags, HashMap::new(), 0.0, false); + assert!(result.is_err()); + } + + #[test] + fn test_derive_endedness_from_only_first() { + let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); + ordering_flags.insert( + Arc::clone(&OVERALL), + OrderingFlagsCounts { + first: 1, + last: 0, + both: 0, + neither: 0, + }, + ); + let result = predict(ordering_flags, HashMap::new(), 0.0, false); + assert!(result.is_ok()); + let result = result.unwrap(); + assert!(!result.succeeded); + assert_eq!(result.endedness, "Unknown"); + assert_eq!(result.first, 1); + assert_eq!(result.last, 0); + assert_eq!(result.both, 0); + assert_eq!(result.neither, 0); + assert_eq!(result.rpt, None); + assert_eq!(result.read_groups.len(), 0); + } + + #[test] + fn test_derive_endedness_from_only_last() { + let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); + ordering_flags.insert( + Arc::clone(&OVERALL), + OrderingFlagsCounts { + first: 0, + last: 1, + both: 0, + neither: 0, + }, + ); + let result = predict(ordering_flags, HashMap::new(), 0.0, false); + assert!(result.is_ok()); + let result = result.unwrap(); + assert!(!result.succeeded); + assert_eq!(result.endedness, "Unknown"); + assert_eq!(result.first, 0); + assert_eq!(result.last, 1); + assert_eq!(result.both, 0); + assert_eq!(result.neither, 0); + assert_eq!(result.rpt, None); + assert_eq!(result.read_groups.len(), 0); + } + + #[test] + fn test_derive_endedness_from_only_both() { + let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); + ordering_flags.insert( + Arc::clone(&OVERALL), + OrderingFlagsCounts { + first: 0, + last: 0, + both: 1, + neither: 0, + }, + ); + let result = predict(ordering_flags, HashMap::new(), 0.0, false); + assert!(result.is_ok()); + let result = result.unwrap(); + assert!(result.succeeded); + assert_eq!(result.endedness, "Single-End"); + assert_eq!(result.first, 0); + assert_eq!(result.last, 0); + assert_eq!(result.both, 1); + assert_eq!(result.neither, 0); + assert_eq!(result.rpt, None); + assert_eq!(result.read_groups.len(), 0); + } + + #[test] + fn test_derive_endedness_from_only_neither() { + let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); + ordering_flags.insert( + Arc::clone(&OVERALL), + OrderingFlagsCounts { + first: 0, + last: 0, + both: 0, + neither: 1, + }, + ); + let result = predict(ordering_flags, HashMap::new(), 0.0, false); + assert!(result.is_ok()); + let result = result.unwrap(); + assert!(!result.succeeded); + assert_eq!(result.endedness, "Unknown"); + assert_eq!(result.first, 0); + assert_eq!(result.last, 0); + assert_eq!(result.both, 0); + assert_eq!(result.neither, 1); + assert_eq!(result.rpt, None); + assert_eq!(result.read_groups.len(), 0); + } + + #[test] + fn test_derive_endedness_from_first_and_last() { + let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); + ordering_flags.insert( + Arc::clone(&OVERALL), + OrderingFlagsCounts { + first: 1, + last: 1, + both: 0, + neither: 0, + }, + ); + let result = predict(ordering_flags, HashMap::new(), 0.0, false); + assert!(result.is_ok()); + let result = result.unwrap(); + assert!(result.succeeded); + assert_eq!(result.endedness, "Paired-End"); + assert_eq!(result.first, 1); + assert_eq!(result.last, 1); + assert_eq!(result.both, 0); + assert_eq!(result.neither, 0); + assert_eq!(result.rpt, None); + assert_eq!(result.read_groups.len(), 0); + } + + #[test] + fn test_calculate_reads_per_template() { + let mut read_names: HashMap>> = HashMap::new(); + let rg_paired = Arc::new("rg_paired".to_string()); + let rg_single = Arc::new("rg_single".to_string()); + read_names.insert( + "read1".to_string(), + vec![Arc::clone(&rg_paired), Arc::clone(&rg_paired)], + ); + read_names.insert( + "read2".to_string(), + vec![ + Arc::clone(&rg_paired), + Arc::clone(&rg_paired), + Arc::clone(&rg_single), + ], + ); + read_names.insert("read3".to_string(), vec![Arc::clone(&rg_single)]); + read_names.insert( + "read4".to_string(), + vec![Arc::clone(&rg_paired), Arc::clone(&rg_paired)], + ); + read_names.insert( + "read5".to_string(), + vec![ + Arc::clone(&rg_paired), + Arc::clone(&rg_paired), + Arc::clone(&rg_single), + ], + ); + let results = calculate_reads_per_template(read_names); + assert_eq!(results.len(), 3); + assert_eq!(results.get(&Arc::new("overall".to_string())).unwrap(), &2.2); + assert_eq!(results.get(&Arc::clone(&rg_paired)).unwrap(), &2.0); + assert_eq!(results.get(&Arc::clone(&rg_single)).unwrap(), &1.0); + } + + #[test] + fn test_derive_endedness_from_first_and_last_with_rpt() { + let mut ordering_flags: HashMap, OrderingFlagsCounts> = HashMap::new(); + let rg_paired = Arc::new("rg_paired".to_string()); + let rg_single = Arc::new("rg_single".to_string()); + ordering_flags.insert( + Arc::clone(&OVERALL), + OrderingFlagsCounts { + first: 8, + last: 8, + both: 2, + neither: 0, + }, + ); + ordering_flags.insert( + Arc::clone(&rg_paired), + OrderingFlagsCounts { + first: 8, + last: 8, + both: 0, + neither: 0, + }, + ); + ordering_flags.insert( + Arc::clone(&rg_single), + OrderingFlagsCounts { + first: 0, + last: 0, + both: 2, + neither: 0, + }, + ); + let mut read_names: HashMap>> = HashMap::new(); + read_names.insert( + "read1".to_string(), + vec![Arc::clone(&rg_paired), Arc::clone(&rg_paired)], + ); + read_names.insert( + "read2".to_string(), + vec![ + Arc::clone(&rg_paired), + Arc::clone(&rg_paired), + Arc::clone(&rg_single), + ], + ); + read_names.insert("read3".to_string(), vec![Arc::clone(&rg_single)]); + read_names.insert( + "read4".to_string(), + vec![Arc::clone(&rg_paired), Arc::clone(&rg_paired)], + ); + read_names.insert( + "read5".to_string(), + vec![ + Arc::clone(&rg_paired), + Arc::clone(&rg_paired), + Arc::clone(&rg_single), + ], + ); + let result = predict(ordering_flags, read_names, 0.0, false); + assert!(result.is_ok()); + let result = result.unwrap(); + assert!(!result.succeeded); + assert_eq!(result.endedness, "Unknown"); + assert_eq!(result.first, 8); + assert_eq!(result.last, 8); + assert_eq!(result.both, 2); + assert_eq!(result.neither, 0); + assert_eq!(result.rpt, Some(2.2)); + assert_eq!(result.read_groups.len(), 2); + // We can't know which read group will be first in the vector. + // But both should succeed. + assert!(result.read_groups[0].succeeded && result.read_groups[1].succeeded); + } +}