Skip to content

Commit

Permalink
add a minimum frequency argument to jsontree and report
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Van der Jeugt committed Aug 7, 2019
1 parent 843f512 commit b79e231
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "umgap"
version = "0.3.2"
version = "0.3.3"
authors = ["Felix Van der Jeugt <[email protected]>",
"Stijn Seghers <[email protected]>",
"Niels De Graef <[email protected]>",
Expand Down
8 changes: 8 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ fn jsontree(args: args::JsonTree) -> Result<()> {
}

// Recursive json transformation
fn to_json(node: &tree::tree::Tree<usize>, aggnode: &tree::tree::Tree<usize>, by_id: &taxon::TaxonList) -> value::Value {
fn to_json(node: &tree::tree::Tree<usize>, aggnode: &tree::tree::Tree<usize>, by_id: &taxon::TaxonList, freq: usize) -> value::Value {
let root = by_id.get(node.root).unwrap();
json!({
"name": root.name,
Expand All @@ -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::<Vec<_>>()
})
}

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(())
}
Expand Down Expand Up @@ -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::<Vec<(taxon::TaxonId, usize)>>();
counts.sort();
Expand Down

0 comments on commit b79e231

Please sign in to comment.