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
use flate2::bufread::MultiGzDecoder;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
pub fn seq_id_from_description<'a>(line: &'a str, separator: &'a str, id_index: usize) -> &'a str {
if line.contains(separator) {
let fields = line.split(separator).collect::<Vec<&str>>();
if id_index == 0 {
&fields[id_index][1..]
} else {
fields[id_index]
}
} else {
&line[1..]
}
}
pub fn open(path: &Path) -> Box<dyn std::io::Read> {
if let Some(extension) = path.extension() {
match extension.to_str().unwrap() {
"gz" => {
let fin = File::open(path)
.unwrap_or_else(|_| panic!("Could not open path: {}", path.display()));
Box::new(MultiGzDecoder::new(BufReader::new(fin)))
}
_ => Box::new(BufReader::new(File::open(path).unwrap_or_else(|_| {
panic!("Could not open path: {}", path.display())
}))),
}
} else {
Box::new(BufReader::new(File::open(path).unwrap_or_else(|_| {
panic!("Could not open path: {}", path.display())
})))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn seq_id_short_descr() {
let descr = ">Q2HZH0";
assert_eq!(seq_id_from_description(descr, "|", 1), "Q2HZH0");
}
#[test]
fn seq_id_long_descr() {
let descr =
">sp|Q2HZH0|IL1B_PUSHI Interleukin-1 beta OS=Pusa hispida OX=9718 GN=IL1B PE=2 SV=1";
assert_eq!(seq_id_from_description(descr, "|", 1), "Q2HZH0");
}
}