From b79e23176b6e50a022c3d05d6dd7c389df9884eb Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Wed, 7 Aug 2019 11:24:21 +0200 Subject: [PATCH] add a minimum frequency argument to jsontree and report --- Cargo.toml | 2 +- src/args.rs | 8 ++++++++ src/main.rs | 8 +++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b970551..80dbf01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "umgap" -version = "0.3.2" +version = "0.3.3" authors = ["Felix Van der Jeugt ", "Stijn Seghers ", "Niels De Graef ", diff --git a/src/args.rs b/src/args.rs index 11d4f94..b475343 100644 --- a/src/args.rs +++ b/src/args.rs @@ -489,6 +489,10 @@ pub struct JsonTree { #[structopt(short = "r", long = "ranked")] pub ranked_only: bool, + /// The minimum frequency to be reported + #[structopt(short = "f", long = "frequency", default_value = "1")] + pub min_frequency: usize, + /// The NCBI taxonomy tsv-file #[structopt(parse(from_os_str))] pub taxon_file: PathBuf, @@ -521,6 +525,10 @@ pub struct Report { #[structopt(short = "r", long = "rank", default_value = "species")] pub rank: Rank, + /// The minimum frequency to be reported + #[structopt(short = "f", long = "frequency", default_value = "1")] + pub min_frequency: usize, + /// The NCBI taxonomy tsv-file #[structopt(parse(from_os_str))] pub taxon_file: PathBuf, diff --git a/src/main.rs b/src/main.rs index 3eb3b88..9fe39f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -534,7 +534,7 @@ fn jsontree(args: args::JsonTree) -> Result<()> { } // Recursive json transformation - fn to_json(node: &tree::tree::Tree, aggnode: &tree::tree::Tree, by_id: &taxon::TaxonList) -> value::Value { + fn to_json(node: &tree::tree::Tree, aggnode: &tree::tree::Tree, by_id: &taxon::TaxonList, freq: usize) -> value::Value { let root = by_id.get(node.root).unwrap(); json!({ "name": root.name, @@ -546,14 +546,15 @@ fn jsontree(args: args::JsonTree) -> Result<()> { "self_count": node.value }, "children": node.children.iter().zip(aggnode.children.iter()) - .map(|(n, s)| to_json(n, s, by_id)) + .filter(|&(_n, s)| s.value > freq) + .map(|(n, s)| to_json(n, s, by_id, freq)) .collect::>() }) } let tree = tree::tree::Tree::new(1, &by_id.ancestry(), &counts)?; let aggtree = tree.aggregate(&ops::Add::add); - print!("{}", to_json(&tree, &aggtree, &by_id)); + print!("{}", to_json(&tree, &aggtree, &by_id, args.min_frequency)); Ok(()) } @@ -660,6 +661,7 @@ fn report(args: args::Report) -> Result<()> { } let mut counts = counts.into_iter() + .filter(|&(_taxon, count)| count > args.min_frequency) .map(|(taxon, count)| (count, taxon)) .collect::>(); counts.sort();