Skip to content

Commit

Permalink
user-friendly messages
Browse files Browse the repository at this point in the history
  • Loading branch information
endixk committed Jan 9, 2025
1 parent f6426c9 commit e72e222
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/modules/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::path::Path;
use crate::util::arg_parser::Args;
use crate::envs::error_handler as err;
use crate::util::arg_parser::Args;
use crate::util::command as cmd;
use crate::util::checkpoint as chkpnt;
use crate::util::message as msg;

// Run foldseek cluster and createtsv
pub fn run(args: &Args, bin: &crate::envs::variables::BinaryPaths) -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -48,9 +49,11 @@ pub fn run(args: &Args, bin: &crate::envs::variables::BinaryPaths) -> Result<(),
foldseek_flag.extend(cluster_args.iter());

// Run foldseek cluster
msg::print_message(&"Running foldseek cluster...".to_string(), 3);
let mut cmd = std::process::Command::new(foldseek_path);
let mut cmd = cmd.args(&foldseek_flag);
cmd::run(&mut cmd);
msg::println_message(&" Done".to_string(), 3);

// Run foldseek createtsv
let mut cmd = std::process::Command::new(foldseek_path);
Expand Down
24 changes: 18 additions & 6 deletions src/modules/profile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::envs::variables as var;
use crate::util::arg_parser::Args;
use crate::util::checkpoint as chkpnt;
use crate::util::message as msg;

use std::collections::{HashMap, HashSet};
use std::fs;
Expand Down Expand Up @@ -37,15 +38,20 @@ fn profile(tsv_file: &str, mapping: &str, output_dir: &str, threshold: usize, pr
let mut curr_query: Option<String> = None;
let mut spe_cnt: HashMap<String, i32> = HashMap::new();
let mut gene2spe: HashMap<String, HashSet<String>> = HashMap::new();
let (mut total_cnt, mut core_cnt) = (0, 0);

msg::print_message(&"Profiling the taxonomic distribution of the genes...".to_string(), 3);
for line in reader.lines().filter_map(|l| l.ok()) {
let parts: Vec<&str> = line.split_whitespace().collect();
let query = parts[0].to_string();
let target = parts[1];

if Some(&query) != curr_query.as_ref() {
if let Some(q) = curr_query.take() {
output_statistics_and_genes(&mut output, &q, &spe_cnt, &gene2spe, species_count, threshold, output_dir)?;
total_cnt += 1;
let is_core = output_statistics_and_genes(&mut output, &q, &spe_cnt, &gene2spe, species_count, threshold, output_dir)
.expect("Failed to write out the statistics and genes");
if is_core { core_cnt += 1; }
}
curr_query = Some(query);
spe_cnt.clear();
Expand All @@ -61,20 +67,27 @@ fn profile(tsv_file: &str, mapping: &str, output_dir: &str, threshold: usize, pr
}

if let Some(q) = curr_query {
output_statistics_and_genes(&mut output, &q, &spe_cnt, &gene2spe, species_count, threshold, output_dir)?;
total_cnt += 1;
let is_core = output_statistics_and_genes(&mut output, &q, &spe_cnt, &gene2spe, species_count, threshold, output_dir)
.expect("Failed to write out the statistics and genes");
if is_core { core_cnt += 1; }
}

msg::println_message(&" Done".to_string(), 3);
msg::println_message(&format!("{} structural core genes found from {} candidates", core_cnt, total_cnt), 3);

Ok(())
}

fn output_statistics_and_genes<W: Write>(output: &mut Option<W>, query: &str, spe_cnt: &HashMap<String, i32>, gene2spe: &HashMap<String, HashSet<String>>, species_count: usize, threshold: usize, output_dir: &str) -> io::Result<()> {
fn output_statistics_and_genes<W: Write>(output: &mut Option<W>, query: &str, spe_cnt: &HashMap<String, i32>, gene2spe: &HashMap<String, HashSet<String>>, species_count: usize, threshold: usize, output_dir: &str) -> io::Result<bool> {
let single_copy = spe_cnt.values().filter(|&&count| count == 1).count();
let multiple_copy = spe_cnt.len();

let single_copy_percent = single_copy as f64 * 100.0 / species_count as f64;
let multiple_copy_percent = multiple_copy as f64 * 100.0 / species_count as f64;

// Write out to copiness.tsv
msg::println_message(&format!("Gene {} reported {:.2}% single copy and {:.2}% multiple copy", query, single_copy_percent, multiple_copy_percent), 4);
if let Some(output) = output.as_mut() {
writeln!(output, "{}\t{}\t{}", query, multiple_copy_percent, single_copy_percent)?;
}
Expand All @@ -91,9 +104,8 @@ fn output_statistics_and_genes<W: Write>(output: &mut Option<W>, query: &str, sp
}
}
output_file.flush()?;
}

Ok(())
Ok(true)
} else { Ok(false) }
}

pub fn run(args: &Args, _: &var::BinaryPaths) -> Result<(), Box<dyn std::error::Error>> {
Expand Down
2 changes: 1 addition & 1 deletion src/util/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::util::message as msg;

pub fn run(cmd: &mut std::process::Command) {
let cmdstr = format!("{:?}", cmd).replace("\"", "");
msg::println_message(&format!("Running command: {}", cmdstr), 3);
msg::println_message(&format!("Running command: {}", cmdstr), 4);
if let Ok(mut child) = cmd.spawn() {
let wait = child.wait();
if let Ok(status) = wait {
Expand Down

0 comments on commit e72e222

Please sign in to comment.