• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // The Computer Language Benchmarks Game
2 // https://benchmarksgame-team.pages.debian.net/benchmarksgame/
3 //
4 // contributed by the Rust Project Developers
5 // contributed by TeXitoi
6 // contributed by BurntSushi
7 
8 use std::io::{self, Read};
9 
10 macro_rules! regex {
11     ($re:expr) => {
12         ::regex::Regex::new($re).unwrap()
13     };
14 }
15 
main()16 fn main() {
17     let mut seq = String::with_capacity(50 * (1 << 20));
18     io::stdin().read_to_string(&mut seq).unwrap();
19     let ilen = seq.len();
20 
21     seq = regex!(">[^\n]*\n|\n").replace_all(&seq, "").into_owned();
22     let clen = seq.len();
23 
24     let variants = vec![
25         regex!("agggtaaa|tttaccct"),
26         regex!("[cgt]gggtaaa|tttaccc[acg]"),
27         regex!("a[act]ggtaaa|tttacc[agt]t"),
28         regex!("ag[act]gtaaa|tttac[agt]ct"),
29         regex!("agg[act]taaa|ttta[agt]cct"),
30         regex!("aggg[acg]aaa|ttt[cgt]ccct"),
31         regex!("agggt[cgt]aa|tt[acg]accct"),
32         regex!("agggta[cgt]a|t[acg]taccct"),
33         regex!("agggtaa[cgt]|[acg]ttaccct"),
34     ];
35     for re in variants {
36         println!("{} {}", re.to_string(), re.find_iter(&seq).count());
37     }
38 
39     let substs = vec![
40         (b'B', "(c|g|t)"),
41         (b'D', "(a|g|t)"),
42         (b'H', "(a|c|t)"),
43         (b'K', "(g|t)"),
44         (b'M', "(a|c)"),
45         (b'N', "(a|c|g|t)"),
46         (b'R', "(a|g)"),
47         (b'S', "(c|g)"),
48         (b'V', "(a|c|g)"),
49         (b'W', "(a|t)"),
50         (b'Y', "(c|t)"),
51     ]; // combined into one regex in `replace_all`
52     let seq = replace_all(&seq, substs);
53 
54     println!("\n{}\n{}\n{}", ilen, clen, seq.len());
55 }
56 
replace_all(text: &str, substs: Vec<(u8, &str)>) -> String57 fn replace_all(text: &str, substs: Vec<(u8, &str)>) -> String {
58     let mut replacements = vec![""; 256];
59     let mut alternates = vec![];
60     for (re, replacement) in substs {
61         replacements[re as usize] = replacement;
62         alternates.push((re as char).to_string());
63     }
64 
65     let re = regex!(&alternates.join("|"));
66     let mut new = String::with_capacity(text.len());
67     let mut last_match = 0;
68     for m in re.find_iter(text) {
69         new.push_str(&text[last_match..m.start()]);
70         new.push_str(replacements[text.as_bytes()[m.start()] as usize]);
71         last_match = m.end();
72     }
73     new.push_str(&text[last_match..]);
74     new
75 }
76