Skip to content

Commit

Permalink
revise: applies Clay's edits
Browse files Browse the repository at this point in the history
  • Loading branch information
claymcleod committed Dec 5, 2023
1 parent 5ed13e6 commit c2fe1d8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
1 change: 0 additions & 1 deletion src/derive/command/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::path::PathBuf;

use anyhow::Ok;
use clap::Args;
use tracing::info;

/// Clap arguments for the `ngs derive encoding` subcommand.
#[derive(Args)]
Expand Down
53 changes: 18 additions & 35 deletions src/derive/command/endedness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::PathBuf;
use std::rc::Rc;

use clap::Args;
use noodles::sam::record::data::field::Tag;
Expand Down Expand Up @@ -63,24 +64,6 @@ struct ReadGroup {
both: usize,
neither: usize,
}

struct FoundReadGroups {
read_groups: HashSet<String>,
}

impl FoundReadGroups {
fn new() -> Self {
FoundReadGroups {
read_groups: HashSet::new(),
}
}

fn insert_and_get_ref(&mut self, read_group: &str) -> &String {
self.read_groups.insert(read_group.to_string());
self.read_groups.get(read_group).unwrap()
}
}

/// Main function for the `ngs derive endedness` subcommand.
pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> {
info!("Starting derive endedness subcommand.");
Expand All @@ -91,9 +74,9 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> {
new_ordering_flags.insert("f+l+".to_string(), 0);
new_ordering_flags.insert("f-l-".to_string(), 0);

let mut ordering_flags: HashMap<String, HashMap<String, usize>> = HashMap::new();
ordering_flags.insert("overall".to_string(), new_ordering_flags.clone());
ordering_flags.insert("unknown_read_group".to_string(), new_ordering_flags.clone());
let mut ordering_flags: HashMap<Rc<String>, HashMap<String, usize>> = HashMap::new();
ordering_flags.insert(Rc::new("overall".to_string()), new_ordering_flags.clone());
ordering_flags.insert(Rc::new("unknown_read_group".to_string()), new_ordering_flags.clone());

new_ordering_flags
.entry("f+l-".to_string())
Expand All @@ -109,8 +92,8 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> {
.and_modify(|e| *e += 1);

// only used if args.calc_rpt is true
let mut found_rgs = FoundReadGroups::new();
let mut read_names = Trie::<String, Vec<&str>>::new();
let mut found_rgs = HashSet::new();
let mut read_names = Trie::<String, Vec<Rc<String>>>::new();

let ParsedBAMFile {
mut reader, header, ..
Expand Down Expand Up @@ -138,15 +121,15 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> {
let read_group = record
.data()
.get(Tag::ReadGroup)
.and_then(|v| v.as_str())
.unwrap_or("unknown_read_group");
.map(|v| Rc::new(v.to_string()))
.unwrap_or(Rc::new(String::from("unknown_read_group")));

if args.calc_rpt {
let rg_ref = found_rgs.insert_and_get_ref(read_group);
found_rgs.insert(Rc::clone(&read_group));

match record.read_name() {
Some(rn) => {
read_names.insert(rn.to_string(), vec![rg_ref]);
read_names.insert(rn.to_string(), vec![Rc::clone(&read_group)]);
}
None => {
trace!("Could not parse a QNAME from a read in the file.");
Expand All @@ -157,41 +140,41 @@ pub fn derive(args: DeriveEndednessArgs) -> anyhow::Result<()> {
}

if record.flags().is_first_segment() && !record.flags().is_last_segment() {
ordering_flags.entry("overall".to_string()).and_modify(|e| {
ordering_flags.entry(Rc::new("overall".to_string())).and_modify(|e| {
e.entry("f+l-".to_string()).and_modify(|e| *e += 1);
});
ordering_flags
.entry(read_group.to_string())
.entry(read_group)
.and_modify(|e| {
e.entry("f+l-".to_string()).and_modify(|e| *e += 1);
})
.or_insert(new_ordering_flags.clone());
} else if !record.flags().is_first_segment() && record.flags().is_last_segment() {
ordering_flags.entry("overall".to_string()).and_modify(|e| {
ordering_flags.entry(Rc::new("overall".to_string())).and_modify(|e| {
e.entry("f-l+".to_string()).and_modify(|e| *e += 1);
});
ordering_flags
.entry(read_group.to_string())
.entry(read_group)
.and_modify(|e| {
e.entry("f-l+".to_string()).and_modify(|e| *e += 1);
})
.or_insert(new_ordering_flags.clone());
} else if record.flags().is_first_segment() && record.flags().is_last_segment() {
ordering_flags.entry("overall".to_string()).and_modify(|e| {
ordering_flags.entry(Rc::new("overall".to_string())).and_modify(|e| {
e.entry("f+l+".to_string()).and_modify(|e| *e += 1);
});
ordering_flags
.entry(read_group.to_string())
.entry(read_group)
.and_modify(|e| {
e.entry("f+l+".to_string()).and_modify(|e| *e += 1);
})
.or_insert(new_ordering_flags.clone());
} else if !record.flags().is_first_segment() && !record.flags().is_last_segment() {
ordering_flags.entry("overall".to_string()).and_modify(|e| {
ordering_flags.entry(Rc::new("overall".to_string())).and_modify(|e| {
e.entry("f-l-".to_string()).and_modify(|e| *e += 1);
});
ordering_flags
.entry(read_group.to_string())
.entry(read_group)
.and_modify(|e| {
e.entry("f-l-".to_string()).and_modify(|e| *e += 1);
})
Expand Down
11 changes: 6 additions & 5 deletions src/derive/endedness/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use anyhow::bail;
use serde::Serialize;
use std::collections::HashMap;
use std::rc::Rc;

/// Struct holding the final results for an `ngs derive endedness` subcommand
/// call.
Expand Down Expand Up @@ -52,13 +53,13 @@ impl DerivedEndednessResult {
/// return a result for the endedness of the file. This may fail, and the
/// resulting [`DerivedEndednessResult`] should be evaluated accordingly.
pub fn predict(
ordering_flags: HashMap<String, HashMap<String, usize>>,
ordering_flags: HashMap<Rc<String>, HashMap<String, usize>>,
paired_deviance: f64,
) -> Result<DerivedEndednessResult, anyhow::Error> {
let first = ordering_flags["overall"]["f+l-"];
let last = ordering_flags["overall"]["f-l+"];
let both = ordering_flags["overall"]["f+l+"];
let neither = ordering_flags["overall"]["f-l-"];
let first = ordering_flags[&String::from("overall")]["f+l-"];
let last = ordering_flags[&String::from("overall")]["f-l+"];
let both = ordering_flags[&String::from("overall")]["f+l+"];
let neither = ordering_flags[&String::from("overall")]["f-l-"];

let mut result =
DerivedEndednessResult::new(false, "Unknown".to_string(), first, last, both, neither);
Expand Down
2 changes: 1 addition & 1 deletion src/qc/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ fn app(
info!("Starting second pass for QC stats.");
let mut reader = File::open(&src).map(bam::Reader::new)?;
let index =
bai::read(&src.with_extension("bam.bai")).with_context(|| "reading BAM index")?;
bai::read(src.with_extension("bam.bai")).with_context(|| "reading BAM index")?;

let mut counter = RecordCounter::new();

Expand Down

0 comments on commit c2fe1d8

Please sign in to comment.