Skip to content

Commit

Permalink
fix(endedness): remove all Arcs and lazy_statics
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz committed Dec 15, 2023
1 parent 8b23de0 commit 65ea501
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 37 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 8 additions & 11 deletions src/derive/command/endedness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Vec<&str>> = HashMap::new();
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
});

Expand All @@ -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;
});

Expand All @@ -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;
});

Expand All @@ -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;
});

Expand Down
39 changes: 15 additions & 24 deletions src/derive/endedness/compute.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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<String> = 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)]
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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());
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 65ea501

Please sign in to comment.