Skip to content

Commit

Permalink
config check route
Browse files Browse the repository at this point in the history
  • Loading branch information
endixk committed Feb 10, 2025
1 parent 8eadb12 commit 1bbafc9
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/envs/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,20 @@ pub fn locate_encoder_py() -> String {
}

// binary paths
const VALID_BINARY: [&str; 7] = [
"mmseqs", "foldseek", "mafft", "mafft-linsi", "foldmason", "iqtree", "fasttree",
pub const VALID_BINARY: [&str; 8] = [
"mmseqs", "foldseek", "mafft", "mafft-linsi", "foldmason", "iqtree", "fasttree", "raxml"
];
pub struct Binary {
name: String,
pub path: String,
pub set: bool,
}
impl Binary {
fn new(name: &str, path: &str) -> Self {
Binary {
name: name.to_string(),
path: path.to_string(),
set: false,
}
}
fn test(&self, args: Vec<&str>) -> bool {
Expand Down Expand Up @@ -131,6 +133,7 @@ impl BinaryPaths {
let path = split.next().unwrap_or("");
if let Some(&i) = self.map.get(name) {
self.bin[i].path = path.to_string();
self.bin[i].set = true;
}
}
Ok(())
Expand Down
82 changes: 82 additions & 0 deletions src/modules/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,87 @@
use crate::envs::error_handler as err;
use crate::envs::variables as var;
use crate::util::command as cmd;
use crate::util::message as msg;
use crate::util::arg_parser::Args;
use color_print::cstr;

fn task_check(bin: &crate::envs::variables::BinaryPaths) -> Result<(), Box<dyn std::error::Error>> {
msg::println_message(&format!("{}", cstr!(r#"<bold><underline>System:</underline></bold>"#)), 3);
msg::println_message(&format!("Unicore version: {}", var::VERSION), 3);
msg::println_message(&format!("OS: {}", std::env::consts::OS), 3);
msg::println_message(&format!("Threads: {}", var::threads()), 3);
println!();
msg::println_message(&format!("{}", cstr!(r#"<bold><underline>Dependencies:</underline></bold>"#)), 3);
msg::println_message(&format!("MMseqs2: {} .. {}",
if let Some(&ref bin) = &bin.get("mmseqs") { if bin.set { bin.path.clone() } else { "Unset".to_string() } } else { "Undefined".to_string() },
if let Some(&ref bin) = &bin.get("mmseqs") { if bin.set { if binary_run_test(&bin.path, "mmseqs") { cstr!(r#"<green>ok</green>"#) } else { cstr!(r#"<red>no</red>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) },
), 3);
msg::println_message(&format!("Foldseek: {} .. {}",
if let Some(&ref bin) = &bin.get("foldseek") { if bin.set { bin.path.clone() } else { "Unset".to_string() } } else { "Undefined".to_string() },
if let Some(&ref bin) = &bin.get("foldseek") { if bin.set { if binary_run_test(&bin.path, "foldseek") { cstr!(r#"<green>ok</green>"#) } else { cstr!(r#"<red>no</red>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) },
), 3);
msg::println_message(&format!("FoldMason: {} .. {}",
if let Some(&ref bin) = &bin.get("foldmason") { if bin.set { bin.path.clone() } else { "Unset".to_string() } } else { "Undefined".to_string() },
if let Some(&ref bin) = &bin.get("foldmason") { if bin.set { if binary_run_test(&bin.path, "foldmason") { cstr!(r#"<green>ok</green>"#) } else { cstr!(r#"<red>no</red>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) },
), 3);
msg::println_message(&format!("MAFFT: {} .. {}",
if let Some(&ref bin) = &bin.get("mafft") { if bin.set { bin.path.clone() } else { "Unset".to_string() } } else { "Undefined".to_string() },
if let Some(&ref bin) = &bin.get("mafft") { if bin.set { if binary_run_test(&bin.path, "mafft") { cstr!(r#"<green>ok</green>"#) } else { cstr!(r#"<red>no</red>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) },
), 3);
msg::println_message(&format!("IQ-TREE: {} .. {}",
if let Some(&ref bin) = &bin.get("iqtree") { if bin.set { bin.path.clone() } else { "Unset".to_string() } } else { "Undefined".to_string() },
if let Some(&ref bin) = &bin.get("iqtree") { if bin.set { if binary_run_test(&bin.path, "iqtree") { cstr!(r#"<green>ok</green>"#) } else { cstr!(r#"<red>no</red>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) },
), 3);
msg::println_message(&format!("FastTree: {} .. {}",
if let Some(&ref bin) = &bin.get("fasttree") { if bin.set { bin.path.clone() } else { "Unset".to_string() } } else { "Undefined".to_string() },
if let Some(&ref bin) = &bin.get("fasttree") { if bin.set { if binary_run_test(&bin.path, "fasttree") { cstr!(r#"<green>ok</green>"#) } else { cstr!(r#"<red>no</red>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) },
), 3);
msg::println_message(&format!("RAxML: {} .. {}",
if let Some(&ref bin) = &bin.get("raxml") { if bin.set { bin.path.clone() } else { "Unset".to_string() } } else { "Undefined".to_string() },
if let Some(&ref bin) = &bin.get("raxml") { if bin.set { if binary_run_test(&bin.path, "raxml") { cstr!(r#"<green>ok</green>"#) } else { cstr!(r#"<red>no</red>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) } } else { cstr!(r#"<dim>n/a</dim>"#) },
), 3);
Ok(())
}

fn binary_run_test(path: &str, sw: &str) -> bool {
if !var::VALID_BINARY.contains(&sw) { return false; }
let mut test_command = std::process::Command::new(path);
let test_command = match sw {
"mmseqs" | "foldseek" | "foldmason" => test_command.arg("version"),
"mafft" | "iqtree" => test_command.arg("--version"),
"fasttree" => &mut test_command,
"raxml" => test_command.arg("-v"),
_ => return false,
};
cmd::run_code(test_command) == 0
}

fn binary_status(bin: &crate::envs::variables::BinaryPaths, sw: &str) -> u8 {
if let Some(&ref bin) = &bin.get(sw) {
if !std::path::Path::new(&bin.path).exists() { return 1; } // Binary path does not exist
} else { return 2; } // Binary path is not set

0
}
const TASK_SET_MMSEQS: u8 = 0x02;
const TASK_SET_FOLDSEEK: u8 = 0x03;
const TASK_SET_FOLDMASON: u8 = 0x04;
const TASK_SET_MAFFT: u8 = 0x05;
const TASK_SET_MAFFT_LINSI: u8 = 0x06;
const TASK_SET_IQTREE: u8 = 0x07;
const TASK_SET_FASTTREE: u8 = 0x08;
const TASK_SET_RAXML: u8 = 0x09;

pub fn run(args: &Args, bin: &crate::envs::variables::BinaryPaths) -> Result<(), Box<dyn std::error::Error>> {
if args.config_check.is_some() && args.config_check.unwrap() { task_check(bin)?; }
else if args.config_set_mmseqs.is_some() { TASK_SET_MMSEQS; }
else if args.config_set_foldseek.is_some() { TASK_SET_FOLDSEEK; }
else if args.config_set_foldmason.is_some() { TASK_SET_FOLDMASON; }
else if args.config_set_mafft.is_some() { TASK_SET_MAFFT; }
else if args.config_set_mafft_linsi.is_some() { TASK_SET_MAFFT_LINSI; }
else if args.config_set_iqtree.is_some() { TASK_SET_IQTREE; }
else if args.config_set_fasttree.is_some() { TASK_SET_FASTTREE; }
else if args.config_set_raxml.is_some() { TASK_SET_RAXML; }
else { err::error(err::ERR_ARGPARSE, Some("No task specified".to_string())) };
Ok(())
}
11 changes: 2 additions & 9 deletions src/util/arg_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,7 @@ pub enum Commands {
/// Runtime environment configuration
#[clap(arg_required_else_help = true, allow_hyphen_values = true)]
Config {
/// Show current environment
#[arg(short='s', long)]
show: bool,
/// Check dependencies
/// Check current environment configuration
#[arg(short='c', long)]
check: bool,
/// Set mmseqs binary path
Expand Down Expand Up @@ -457,7 +454,6 @@ pub struct Args {
pub genetree_aligner: Option<String>,
pub genetree_aligner_options: Option<Option<String>>,

pub config_show: Option<bool>,
pub config_check: Option<bool>,
pub config_set_mmseqs: Option<String>,
pub config_set_foldseek: Option<String>,
Expand Down Expand Up @@ -690,9 +686,6 @@ impl Args {
Some(GeneTree { threshold, .. }) => Some(*threshold), _ => None,
};

let config_show = match &args.command {
Some(Config { show, .. }) => Some(*show), _ => None,
};
let config_check = match &args.command {
Some(Config { check, .. }) => Some(*check), _ => None,
};
Expand Down Expand Up @@ -729,7 +722,7 @@ impl Args {
cluster_input, cluster_output, cluster_tmp, cluster_keep_cluster_db, cluster_cluster_options,
tree_db, tree_input, tree_output, tree_aligner, tree_tree_builder, tree_aligner_options, tree_tree_options, tree_threshold,
genetree_input, genetree_names, genetree_tree_builder, genetree_tree_options, genetree_realign, genetree_aligner, genetree_aligner_options, genetree_threshold,
config_show, config_check, config_set_mmseqs, config_set_foldseek, config_set_foldmason, config_set_mafft, config_set_mafft_linsi, config_set_iqtree, config_set_fasttree, config_set_raxml,
config_check, config_set_mmseqs, config_set_foldseek, config_set_foldmason, config_set_mafft, config_set_mafft_linsi, config_set_iqtree, config_set_fasttree, config_set_raxml,
}
}
}
20 changes: 20 additions & 0 deletions src/util/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ pub fn run(cmd: &mut std::process::Command) {
}
}

pub fn run_code(cmd: &mut std::process::Command) -> i32 {
let cmd = cmd.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null());
let cmdstr = format!("{:?}", cmd).replace("\"", "");
msg::println_message(&format!("Running command: {}", cmdstr), 4);
if let Ok(mut child) = cmd.spawn() {
let wait = child.wait();
if let Ok(status) = wait {
if let Some(code) = status.code() {
code
} else {
1
}
} else {
1
}
} else {
1
}
}

pub fn _run_at(cmd: &mut std::process::Command, path: &std::path::Path) {
let cmdstr = format!("{:?}", cmd);
if let Ok(mut child) = cmd.current_dir(path).spawn() {
Expand Down

0 comments on commit 1bbafc9

Please sign in to comment.