-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvariables.rs
More file actions
169 lines (160 loc) · 7.3 KB
/
variables.rs
File metadata and controls
169 lines (160 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::envs::error_handler as err;
use crate::util::message as msg;
use std::fs::File;
use std::path::MAIN_SEPARATOR as SEP;
use std::process::Command;
use num_cpus;
// global variables
pub const VERSION: &str = "1.0.2";
const STABLE: bool = false;
pub const STABLE_TEXT: &str = if STABLE { "" } else { "unstable" };
pub const STABLE_FULL: &str = if STABLE { "Stable" } else { "Unstable" };
pub const LOGO_ART: &str = r"
██╗ ██╗███╗ ██╗██╗ ██████╗ ██████╗ ██████╗ ███████╗
██║ ██║████╗ ██║██║██╔════╝██╔═══██╗██╔══██╗██╔════╝
██║ ██║██╔██╗ ██║██║██║ ██║ ██║██████╔╝███████╗
██║ ██║██║╚██╗██║██║██║ ██║ ██║██╔══██╗██╔══╝
╚██████╔╝██║ ╚████║██║╚██████╗╚██████╔╝██║ ██║███████╗
╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝";
pub const CITATION: &str = "Kim, D., Park, S., & Steinegger, M. (2024). Unicore enables scalable and accurate phylogenetic reconstruction with structural core genes. bioRxiv";
// environment paths
pub fn parent_dir() -> String {
// assume binary path = parent/bin/unicore
std::env::current_exe()
.unwrap_or_else(|_| err::error(err::ERR_GENERAL, Some("Could not get current directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.to_str()
.unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not convert path to string".to_string())))
.to_string()
}
pub fn src_parent_dir() -> String {
// assume binary path = parent/target/release/unicore
std::env::current_exe()
.unwrap_or_else(|_| err::error(err::ERR_GENERAL, Some("Could not get current directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.to_str()
.unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not convert path to string".to_string())))
.to_string()
}
pub fn test_parent_dir() -> String {
// assume binary path = parent/target/debug/deps/unicore-*
std::env::current_exe()
.unwrap_or_else(|_| err::error(err::ERR_GENERAL, Some("Could not get current directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.parent().unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not get parent directory".to_string())))
.to_str()
.unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not convert path to string".to_string())))
.to_string()
}
pub fn current_dir() -> String {
std::env::current_dir()
.unwrap_or_else(|_| err::error(err::ERR_GENERAL, Some("Could not get current directory".to_string())))
.to_str()
.unwrap_or_else(|| err::error(err::ERR_GENERAL, Some("Could not convert path to string".to_string())))
.to_string()
}
pub fn locate_path_cfg() -> String {
if File::open(format!("{}{}etc{}path.cfg", parent_dir(), SEP, SEP)).is_ok() {
format!("{}{}etc{}path.cfg", parent_dir(), SEP, SEP)
} else if File::open(format!("{}{}path.cfg", src_parent_dir(), SEP)).is_ok() {
format!("{}{}path.cfg", src_parent_dir(), SEP)
} else if File::open(format!("{}{}path.cfg", test_parent_dir(), SEP)).is_ok() {
format!("{}{}path.cfg", test_parent_dir(), SEP)
} else {
err::error(err::ERR_GENERAL, Some("Could not locate path.cfg".to_string()));
}
}
// binary paths
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 {
Command::new(&self.path)
.args(args)
.output()
.is_ok()
}
}
use std::collections::HashMap;
pub struct BinaryPaths {
bin: Vec<Binary>,
map: HashMap<String, usize>,
}
impl BinaryPaths {
pub fn new() -> Self {
let mut bin = Vec::new();
let mut map = HashMap::new();
for (i, &name) in VALID_BINARY.iter().enumerate() {
bin.push(Binary::new(name, name));
map.insert(name.to_string(), i);
}
BinaryPaths { bin, map }
}
pub fn init(&mut self, config_path: &std::path::Path) -> Result<(), std::io::Error> {
let config = std::fs::read_to_string(config_path)?;
for line in config.lines() {
if line.is_empty() || line.starts_with('#') { continue; }
let mut split = line.split('=');
let name = split.next().unwrap_or("");
let path = split.next().unwrap_or("");
if path.len() == 0 { continue; }
if let Some(&i) = self.map.get(name) {
self.bin[i].path = path.to_string();
self.bin[i].set = true;
}
}
Ok(())
}
pub fn get(&self, name: &str) -> Option<&Binary> {
self.map.get(name).map(|&i| &self.bin[i])
}
pub fn set(&mut self, name: &str, path: &str) {
if let Some(&i) = self.map.get(name) {
self.bin[i].path = path.to_string();
}
}
pub fn test(&self, name: &str, args: Vec<&str>) -> bool {
self.get(name).map(|bin| bin.test(args)).unwrap_or(false)
}
}
pub static mut VERBOSITY: u8 = 3;
pub fn set_verbosity(verbosity: u8) {
unsafe { VERBOSITY = verbosity; }
}
pub fn verbosity() -> u8 {
unsafe { VERBOSITY }
}
pub static mut THREADS: usize = 0;
pub fn set_threads(threads: usize) {
let system_cpus = num_cpus::get();
let threads = if threads > system_cpus {
msg::eprintln_message(&format!("Warning: the given number of threads is greater than the number of system CPUs; adjusting to {}", system_cpus), 2);
system_cpus
} else { threads };
let threads = if threads > 0 { threads } else {
msg::println_message(&format!("Automatically setting the number of threads to {}", system_cpus), 4);
system_cpus
};
unsafe { THREADS = threads; }
}
pub fn threads() -> usize {
unsafe { THREADS }
}