Skip to content

Commit 21e774b

Browse files
killerswanbrson
authored andcommitted
---
yaml --- r: 13399 b: refs/heads/master c: c2a9cc9 h: refs/heads/master i: 13397: bc815c1 13395: 8f71e36 13391: 4c57cc8 v: v3
1 parent 91e9137 commit 21e774b

File tree

3 files changed

+208
-12
lines changed

3 files changed

+208
-12
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: c1859d4cd0cd356de3e8daadaff34492d51fd934
2+
refs/heads/master: c2a9cc93944184b4f061c94fa346413ef6e7d813
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/test/bench/shootout-fasta.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import vec;
1010
import uint;
1111
import int;
1212
import str;
13+
import io::writer_util;
1314

1415
fn LINE_LENGTH() -> uint { ret 60u; }
1516

@@ -42,46 +43,53 @@ fn select_random(r: u32, genelist: [aminoacids]) -> char {
4243
ret bisect(genelist, 0u, vec::len::<aminoacids>(genelist) - 1u, r);
4344
}
4445

45-
fn make_random_fasta(id: str, desc: str, genelist: [aminoacids], n: int) {
46-
log(debug, ">" + id + " " + desc);
46+
fn make_random_fasta(wr: io::writer, id: str, desc: str, genelist: [aminoacids], n: int) {
47+
wr.write_line(">" + id + " " + desc);
4748
let rng = @{mut last: std::rand::rng().next()};
4849
let mut op: str = "";
4950
for uint::range(0u, n as uint) {|_i|
5051
str::push_char(op, select_random(myrandom_next(rng, 100u32),
5152
genelist));
5253
if str::len(op) >= LINE_LENGTH() {
53-
log(debug, op);
54+
wr.write_line(op);
5455
op = "";
5556
}
5657
}
57-
if str::len(op) > 0u { log(debug, op); }
58+
if str::len(op) > 0u { wr.write_line(op); }
5859
}
5960

60-
fn make_repeat_fasta(id: str, desc: str, s: str, n: int) unsafe {
61-
log(debug, ">" + id + " " + desc);
61+
fn make_repeat_fasta(wr: io::writer, id: str, desc: str, s: str, n: int) unsafe {
62+
wr.write_line(">" + id + " " + desc);
6263
let mut op: str = "";
6364
let sl: uint = str::len(s);
6465
for uint::range(0u, n as uint) {|i|
6566
str::unsafe::push_byte(op, s[i % sl]);
6667
if str::len(op) >= LINE_LENGTH() {
67-
log(debug, op);
68+
wr.write_line(op);
6869
op = "";
6970
}
7071
}
71-
if str::len(op) > 0u { log(debug, op); }
72+
if str::len(op) > 0u { wr.write_line(op); }
7273
}
7374

7475
fn acid(ch: char, prob: u32) -> aminoacids { ret {ch: ch, prob: prob}; }
7576

7677
fn main(args: [str]) {
7778
let args = if os::getenv("RUST_BENCH").is_some() {
79+
// alioth tests k-nucleotide with this data at 25,000,000
7880
["", "300000"]
7981
} else if args.len() <= 1u {
8082
["", "1000"]
8183
} else {
8284
args
8385
};
8486

87+
let writer = if os::getenv("RUST_BENCH").is_some() {
88+
result::get(io::file_writer("./shootout-fasta.data", [io::truncate, io::create]))
89+
} else {
90+
io::stdout()
91+
};
92+
8593
let n = int::from_str(args[1]).get();
8694

8795
let iub: [aminoacids] =
@@ -101,7 +109,8 @@ fn main(args: [str]) {
101109
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
102110
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
103111
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
104-
make_repeat_fasta("ONE", "Homo sapiens alu", alu, n * 2);
105-
make_random_fasta("TWO", "IUB ambiguity codes", iub, n * 3);
106-
make_random_fasta("THREE", "Homo sapiens frequency", homosapiens, n * 5);
112+
make_repeat_fasta(writer, "ONE", "Homo sapiens alu", alu, n * 2);
113+
make_random_fasta(writer, "TWO", "IUB ambiguity codes", iub, n * 3);
114+
make_random_fasta(writer, "THREE",
115+
"Homo sapiens frequency", homosapiens, n * 5);
107116
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// multi tasking k-nucleotide
2+
3+
import io::reader_util;
4+
5+
use std;
6+
import std::map;
7+
import std::map::hashmap;
8+
import std::sort;
9+
10+
// given a map, print a sorted version of it
11+
fn sort_and_fmt(mm: hashmap<[u8], uint>, total: uint) -> str {
12+
fn pct(xx: uint, yy: uint) -> float {
13+
ret (xx as float) * 100f / (yy as float);
14+
}
15+
16+
fn le_by_val<TT: copy, UU: copy>(kv0: (TT,UU), kv1: (TT,UU)) -> bool {
17+
let (_, v0) = kv0;
18+
let (_, v1) = kv1;
19+
ret v0 >= v1;
20+
}
21+
22+
fn le_by_key<TT: copy, UU: copy>(kv0: (TT,UU), kv1: (TT,UU)) -> bool {
23+
let (k0, _) = kv0;
24+
let (k1, _) = kv1;
25+
ret k0 <= k1;
26+
}
27+
28+
// sort by key, then by value
29+
fn sortKV<TT: copy, UU: copy>(orig: [(TT,UU)]) -> [(TT,UU)] {
30+
ret sort::merge_sort(le_by_val, sort::merge_sort(le_by_key, orig));
31+
}
32+
33+
let mut pairs = [];
34+
35+
// map -> [(k,%)]
36+
mm.each(fn&(key: [u8], val: uint) -> bool {
37+
pairs += [(key, pct(val, total))];
38+
ret true;
39+
});
40+
41+
let pairs_sorted = sortKV(pairs);
42+
43+
let mut buffer = "";
44+
45+
pairs_sorted.each(fn&(kv: ([u8], float)) -> bool unsafe {
46+
let (k,v) = kv;
47+
buffer += (#fmt["%s %0.3f\n", str::to_upper(str::unsafe::from_bytes(k)), v]);
48+
ret true;
49+
});
50+
51+
ret buffer;
52+
}
53+
54+
// given a map, search for the frequency of a pattern
55+
fn find(mm: hashmap<[u8], uint>, key: str) -> uint {
56+
alt mm.find(str::bytes(str::to_lower(key))) {
57+
option::none { ret 0u; }
58+
option::some(num) { ret num; }
59+
}
60+
}
61+
62+
// given a map, increment the counter for a key
63+
fn update_freq(mm: hashmap<[u8], uint>, key: [u8]) {
64+
alt mm.find(key) {
65+
option::none { mm.insert(key, 1u ); }
66+
option::some(val) { mm.insert(key, 1u + val); }
67+
}
68+
}
69+
70+
// given a [u8], for each window call a function
71+
// i.e., for "hello" and windows of size four,
72+
// run it("hell") and it("ello"), then return "llo"
73+
fn windows_with_carry(bb: [const u8], nn: uint, it: fn(window: [u8])) -> [u8] {
74+
let mut ii = 0u;
75+
76+
let len = vec::len(bb);
77+
while ii < len - (nn - 1u) {
78+
it(vec::slice(bb, ii, ii+nn));
79+
ii += 1u;
80+
}
81+
82+
ret vec::slice(bb, len - (nn - 1u), len);
83+
}
84+
85+
fn make_sequence_processor(sz: uint, from_parent: comm::port<[u8]>, to_parent: comm::chan<str>) {
86+
87+
let freqs: hashmap<[u8], uint> = map::bytes_hash();
88+
let mut carry: [u8] = [];
89+
let mut total: uint = 0u;
90+
91+
let mut line: [u8];
92+
93+
loop {
94+
95+
line = comm::recv(from_parent);
96+
if line == [] { break; }
97+
98+
carry = windows_with_carry(carry + line, sz, { |window|
99+
update_freq(freqs, window);
100+
total += 1u;
101+
});
102+
}
103+
104+
let buffer = alt sz {
105+
1u { sort_and_fmt(freqs, total) }
106+
2u { sort_and_fmt(freqs, total) }
107+
3u { #fmt["%u\t%s", find(freqs, "GGT"), "GGT"] }
108+
4u { #fmt["%u\t%s", find(freqs, "GGTA"), "GGTA"] }
109+
6u { #fmt["%u\t%s", find(freqs, "GGTATT"), "GGTATT"] }
110+
12u { #fmt["%u\t%s", find(freqs, "GGTATTTTAATT"), "GGTATTTTAATT"] }
111+
18u { #fmt["%u\t%s", find(freqs, "GGTATTTTAATTTATAGT"), "GGTATTTTAATTTATAGT"] }
112+
_ { "" }
113+
};
114+
115+
//comm::send(to_parent, #fmt["yay{%u}", sz]);
116+
comm::send(to_parent, buffer);
117+
}
118+
119+
// given a FASTA file on stdin, process sequence THREE
120+
fn main(args: [str]) {
121+
let rdr = if os::getenv("RUST_BENCH").is_some() {
122+
result::get(io::file_reader("./shootout-fasta.data"))
123+
} else {
124+
io::stdin()
125+
};
126+
127+
128+
129+
// initialize each sequence sorter
130+
let sizes = [1u,2u,3u,4u,6u,12u,18u];
131+
let from_child = vec::map (sizes, { |_sz| comm::port() });
132+
let to_parent = vec::mapi(sizes, { |ii, _sz| comm::chan(from_child[ii]) });
133+
let to_child = vec::mapi(sizes, fn@(ii: uint, sz: uint) -> comm::chan<[u8]> {
134+
ret task::spawn_listener { |from_parent|
135+
make_sequence_processor(sz, from_parent, to_parent[ii]);
136+
};
137+
});
138+
139+
140+
// latch stores true after we've started
141+
// reading the sequence of interest
142+
let mut proc_mode = false;
143+
144+
while !rdr.eof() {
145+
let line: str = rdr.read_line();
146+
147+
if str::len(line) == 0u { cont; }
148+
149+
alt (line[0], proc_mode) {
150+
151+
// start processing if this is the one
152+
('>' as u8, false) {
153+
alt str::find_str_from(line, "THREE", 1u) {
154+
option::some(_) { proc_mode = true; }
155+
option::none { }
156+
}
157+
}
158+
159+
// break our processing
160+
('>' as u8, true) { break; }
161+
162+
// process the sequence for k-mers
163+
(_, true) {
164+
let line_bytes = str::bytes(line);
165+
166+
for sizes.eachi { |ii, _sz|
167+
let mut lb = line_bytes;
168+
comm::send(to_child[ii], lb);
169+
}
170+
}
171+
172+
// whatever
173+
_ { }
174+
}
175+
}
176+
177+
// finish...
178+
for sizes.eachi { |ii, _sz|
179+
comm::send(to_child[ii], []);
180+
}
181+
182+
// now fetch and print result messages
183+
for sizes.eachi { |ii, _sz|
184+
io::println(comm::recv(from_child[ii]));
185+
}
186+
}
187+

0 commit comments

Comments
 (0)