Skip to content

Instantly share code, notes, and snippets.

@JustAPerson
Last active August 29, 2015 13:58
Show Gist options
  • Save JustAPerson/10012055 to your computer and use it in GitHub Desktop.
Save JustAPerson/10012055 to your computer and use it in GitHub Desktop.
extern crate regexp;
static VARIANTS : [&'static str, ..9 ] = [
"agggtaaa|tttaccct",
"[cgt]gggtaaa|tttaccc[acg]",
"a[act]ggtaaa|tttacc[agt]t",
"ag[act]gtaaa|tttac[agt]ct",
"agg[act]taaa|ttta[agt]cct",
"aggg[acg]aaa|ttt[cgt]ccct",
"agggt[cgt]aa|tt[acg]accct",
"agggta[cgt]a|t[acg]taccct",
"agggtaa[cgt]|[acg]ttaccct",
];
struct Subst<'a>(&'a str, &'a str);
static SUBST : [Subst<'static>, .. 11] = [
Subst("B", "(c|g|t)"), Subst("D", "(a|g|t)"), Subst("H", "(a|c|t)"), Subst("K", "(g|t)"),
Subst("M", "(a|c)"), Subst("N", "(a|c|g|t)"), Subst("R", "(a|g)"), Subst("S", "(c|g)"),
Subst("V", "(a|c|g)"), Subst("W", "(a|t)"), Subst("Y", "(c|t)"),
];
fn replace(text: &str, regex: &str, rep: &str) -> ~str {
regexp::Regexp::new(regex).unwrap().replace_all(text, regexp::NoExpand(rep))
}
fn count_matches(seq: &str, variant: &str) -> int {
let mut n = 0;
for _ in regexp::Regexp::new(variant).unwrap().captures_iter(seq) {
n += 1;
}
return n
}
fn main() {
let mut stdin = std::io::stdio::stdin();
let mut seq = stdin.read_to_str().unwrap();
let ilen = seq.len();
seq = replace(seq, ">[:^cntrl:]*[:cntrl:]*", "");
seq = replace(seq, "[:cntrl:]+", "");
let clen = seq.len();
for variant in VARIANTS.iter() {
println!("{} {}", variant, count_matches(seq,*variant));
}
println!("");
for &Subst(k, v) in SUBST.iter() {
seq = replace(seq, k, v);
}
println!("{}", ilen);
println!("{}", clen);
println!("{}", seq.len());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment