From 65ea501b1883ed0f4de39e29c56fb9e9dc7d5cf1 Mon Sep 17 00:00:00 2001 From: Andrew Frantz Date: Fri, 15 Dec 2023 09:52:29 -0500 Subject: [PATCH] fix(endedness): remove all Arcs and lazy_statics --- Cargo.lock | 1 - Cargo.toml | 1 - src/derive/command/endedness.rs | 19 +++++++--------- src/derive/endedness/compute.rs | 39 +++++++++++++-------------------- 4 files changed, 23 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 925f544..f6669ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -867,7 +867,6 @@ dependencies = [ "indexmap", "indicatif", "itertools", - "lazy_static", "noodles", "num-format", "plotly", diff --git a/Cargo.toml b/Cargo.toml index c9081d8..306ad22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,6 @@ git-testament = "0.2.1" indexmap = "1.9.1" indicatif = "0.16.2" itertools = "0.10.5" -lazy_static = "1.4.0" noodles = { version = "0.34.0", features = [ "async", "bam", diff --git a/src/derive/command/endedness.rs b/src/derive/command/endedness.rs index be0c1ba..56a246b 100644 --- a/src/derive/command/endedness.rs +++ b/src/derive/command/endedness.rs @@ -3,7 +3,6 @@ use std::collections::HashMap; use std::collections::HashSet; use std::path::PathBuf; -use std::sync::Arc; use clap::Args; use noodles::sam::record::data::field::Tag; @@ -64,8 +63,8 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> { let mut found_rgs = HashSet::new(); let mut ordering_flags: HashMap<&str, OrderingFlagsCounts> = HashMap::new(); - ordering_flags.insert(OVERALL.as_str(), OrderingFlagsCounts::new()); - ordering_flags.insert(UNKNOWN_READ_GROUP.as_str(), OrderingFlagsCounts::new()); + ordering_flags.insert(OVERALL, OrderingFlagsCounts::new()); + ordering_flags.insert(UNKNOWN_READ_GROUP, OrderingFlagsCounts::new()); // only used if args.calc_rpt is true let mut read_names: HashMap> = HashMap::new(); @@ -101,9 +100,9 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> { } found_rgs.get(rg).unwrap() } - None => UNKNOWN_READ_GROUP.as_str(), + None => UNKNOWN_READ_GROUP, }, - None => UNKNOWN_READ_GROUP.as_str(), + None => UNKNOWN_READ_GROUP, }; if args.calc_rpt { @@ -129,10 +128,8 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> { } } - let overall_rg = OVERALL.as_str(); - if record.flags().is_first_segment() && !record.flags().is_last_segment() { - ordering_flags.entry(overall_rg).and_modify(|e| { + ordering_flags.entry(OVERALL).and_modify(|e| { e.first += 1; }); @@ -148,7 +145,7 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> { neither: 0, }); } else if !record.flags().is_first_segment() && record.flags().is_last_segment() { - ordering_flags.entry(overall_rg).and_modify(|e| { + ordering_flags.entry(OVERALL).and_modify(|e| { e.last += 1; }); @@ -164,7 +161,7 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> { neither: 0, }); } else if record.flags().is_first_segment() && record.flags().is_last_segment() { - ordering_flags.entry(overall_rg).and_modify(|e| { + ordering_flags.entry(OVERALL).and_modify(|e| { e.both += 1; }); @@ -180,7 +177,7 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> { neither: 0, }); } else if !record.flags().is_first_segment() && !record.flags().is_last_segment() { - ordering_flags.entry(overall_rg).and_modify(|e| { + ordering_flags.entry(OVERALL).and_modify(|e| { e.neither += 1; }); diff --git a/src/derive/endedness/compute.rs b/src/derive/endedness/compute.rs index f4f6869..247ef58 100644 --- a/src/derive/endedness/compute.rs +++ b/src/derive/endedness/compute.rs @@ -1,22 +1,16 @@ //! Module holding the logic for computing the endedness of a BAM. use anyhow::bail; -use lazy_static::lazy_static; use serde::Serialize; use std::collections::HashMap; use std::collections::HashSet; -use std::sync::Arc; use tracing::warn; -// Strings used to index into the HashMaps used to store the Read Group ordering flags. -// Lazy statics are used to save memory. -lazy_static! { - /// String used to index into the HashMaps used to store the "overall" ordering flags. - pub static ref OVERALL: Arc = Arc::new(String::from("overall")); +/// String used to index into the HashMaps used to store the "overall" ordering flags. +pub static OVERALL: &str = "overall"; - /// String used to index into th HashMaps used to store the "unknown_read_group" ordering flags. - pub static ref UNKNOWN_READ_GROUP: Arc = Arc::new(String::from("unknown_read_group")); -} +/// String used to index into th HashMaps used to store the "unknown_read_group" ordering flags. +pub static UNKNOWN_READ_GROUP: &str = "unknown_read_group"; /// Struct holding the ordering flags for a single read group. #[derive(Debug, Clone)] @@ -204,10 +198,7 @@ fn calculate_reads_per_template<'rg>( } } - reads_per_template.insert( - OVERALL.as_str(), - total_reads as f64 / total_templates as f64, - ); + reads_per_template.insert(OVERALL, total_reads as f64 / total_templates as f64); for (read_group, num_reads) in read_group_reads.iter() { let num_templates = read_group_templates.get(read_group).unwrap(); @@ -330,7 +321,7 @@ pub fn predict( ); for (read_group, rg_ordering_flags) in ordering_flags.iter() { - if (*read_group == UNKNOWN_READ_GROUP.as_str()) + if (*read_group == UNKNOWN_READ_GROUP) && (rg_ordering_flags.first == 0 && rg_ordering_flags.last == 0 && rg_ordering_flags.both == 0 @@ -369,7 +360,7 @@ mod tests { fn test_predict_endedness() { let mut ordering_flags: HashMap<&str, OrderingFlagsCounts> = HashMap::new(); ordering_flags.insert( - OVERALL.as_str(), + OVERALL, OrderingFlagsCounts { first: 1, last: 1, @@ -379,7 +370,7 @@ mod tests { ); let result = predict_endedness( "overall".to_string(), - &ordering_flags.get(OVERALL.as_str()).unwrap(), + &ordering_flags.get(OVERALL).unwrap(), 0.0, None, false, @@ -398,7 +389,7 @@ mod tests { #[test] fn test_derive_endedness_from_all_zero_counts() { let mut ordering_flags: HashMap<&str, OrderingFlagsCounts> = HashMap::new(); - ordering_flags.insert(OVERALL.as_str(), OrderingFlagsCounts::new()); + ordering_flags.insert(OVERALL, OrderingFlagsCounts::new()); let result = predict(&ordering_flags, &HashMap::new(), 0.0, false); assert!(result.is_err()); } @@ -407,7 +398,7 @@ mod tests { fn test_derive_endedness_from_only_first() { let mut ordering_flags: HashMap<&str, OrderingFlagsCounts> = HashMap::new(); ordering_flags.insert( - OVERALL.as_str(), + OVERALL, OrderingFlagsCounts { first: 1, last: 0, @@ -432,7 +423,7 @@ mod tests { fn test_derive_endedness_from_only_last() { let mut ordering_flags: HashMap<&str, OrderingFlagsCounts> = HashMap::new(); ordering_flags.insert( - OVERALL.as_str(), + OVERALL, OrderingFlagsCounts { first: 0, last: 1, @@ -457,7 +448,7 @@ mod tests { fn test_derive_endedness_from_only_both() { let mut ordering_flags: HashMap<&str, OrderingFlagsCounts> = HashMap::new(); ordering_flags.insert( - OVERALL.as_str(), + OVERALL, OrderingFlagsCounts { first: 0, last: 0, @@ -482,7 +473,7 @@ mod tests { fn test_derive_endedness_from_only_neither() { let mut ordering_flags: HashMap<&str, OrderingFlagsCounts> = HashMap::new(); ordering_flags.insert( - OVERALL.as_str(), + OVERALL, OrderingFlagsCounts { first: 0, last: 0, @@ -507,7 +498,7 @@ mod tests { fn test_derive_endedness_from_first_and_last() { let mut ordering_flags: HashMap<&str, OrderingFlagsCounts> = HashMap::new(); ordering_flags.insert( - OVERALL.as_str(), + OVERALL, OrderingFlagsCounts { first: 1, last: 1, @@ -551,7 +542,7 @@ mod tests { let rg_paired = "rg_paired"; let rg_single = "rg_single"; ordering_flags.insert( - OVERALL.as_str(), + OVERALL, OrderingFlagsCounts { first: 8, last: 8,