Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 212 lines (163 sloc) 5.577 kB
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
1 // xfail-pretty
2
3 // multi tasking k-nucleotide
4
5 import io::reader_util;
6
7 use std;
8 import std::map;
9 import std::map::hashmap;
10 import std::sort;
11
22e955a @eholk Move streams into core.
eholk authored
12 import pipes::{stream, port, chan};
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
13
14 // given a map, print a sorted version of it
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
15 fn sort_and_fmt(mm: hashmap<~[u8], uint>, total: uint) -> ~str {
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
16 fn pct(xx: uint, yy: uint) -> float {
17 ret (xx as float) * 100f / (yy as float);
18 }
19
20 fn le_by_val<TT: copy, UU: copy>(kv0: (TT,UU), kv1: (TT,UU)) -> bool {
21 let (_, v0) = kv0;
22 let (_, v1) = kv1;
23 ret v0 >= v1;
24 }
25
26 fn le_by_key<TT: copy, UU: copy>(kv0: (TT,UU), kv1: (TT,UU)) -> bool {
27 let (k0, _) = kv0;
28 let (k1, _) = kv1;
29 ret k0 <= k1;
30 }
31
32 // sort by key, then by value
33 fn sortKV<TT: copy, UU: copy>(orig: ~[(TT,UU)]) -> ~[(TT,UU)] {
34 ret sort::merge_sort(le_by_val, sort::merge_sort(le_by_key, orig));
35 }
36
37 let mut pairs = ~[];
38
39 // map -> [(k,%)]
40 mm.each(fn&(key: ~[u8], val: uint) -> bool {
41 vec::push(pairs, (key, pct(val, total)));
42 ret true;
43 });
44
45 let pairs_sorted = sortKV(pairs);
46
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
47 let mut buffer = ~"";
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
48
49 pairs_sorted.each(fn&(kv: (~[u8], float)) -> bool unsafe {
50 let (k,v) = kv;
51 buffer += (#fmt["%s %0.3f\n", str::to_upper(str::unsafe::from_bytes(k)), v]);
52 ret true;
53 });
54
55 ret buffer;
56 }
57
58 // given a map, search for the frequency of a pattern
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
59 fn find(mm: hashmap<~[u8], uint>, key: ~str) -> uint {
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
60 alt mm.find(str::bytes(str::to_lower(key))) {
61 option::none { ret 0u; }
62 option::some(num) { ret num; }
63 }
64 }
65
66 // given a map, increment the counter for a key
67 fn update_freq(mm: hashmap<~[u8], uint>, key: &[u8]) {
68 let key = vec::slice(key, 0, key.len());
69 alt mm.find(key) {
70 option::none { mm.insert(key, 1u ); }
71 option::some(val) { mm.insert(key, 1u + val); }
72 }
73 }
74
75 // given a ~[u8], for each window call a function
76 // i.e., for "hello" and windows of size four,
77 // run it("hell") and it("ello"), then return "llo"
c918bd0 @eholk Fix borrow check errors in k-nucleotide.
eholk authored
78 fn windows_with_carry(bb: &[u8], nn: uint,
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
79 it: fn(window: &[u8])) -> ~[u8] {
80 let mut ii = 0u;
81
82 let len = vec::len(bb);
83 while ii < len - (nn - 1u) {
65beca4 @eholk Use iteration protocol for ebml, use vec::view in more places (issue …
eholk authored
84 it(vec::view(bb, ii, ii+nn));
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
85 ii += 1u;
86 }
87
88 ret vec::slice(bb, len - (nn - 1u), len);
89 }
90
22e955a @eholk Move streams into core.
eholk authored
91 fn make_sequence_processor(sz: uint, from_parent: pipes::port<~[u8]>,
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
92 to_parent: pipes::chan<~str>) {
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
93
94 let freqs: hashmap<~[u8], uint> = map::bytes_hash();
95 let mut carry: ~[u8] = ~[];
96 let mut total: uint = 0u;
97
98 let mut line: ~[u8];
99
100 loop {
101
102 line = from_parent.recv();
103 if line == ~[] { break; }
104
105 carry = windows_with_carry(carry + line, sz, |window| {
106 update_freq(freqs, window);
107 total += 1u;
108 });
109 }
110
111 let buffer = alt sz {
112 1u { sort_and_fmt(freqs, total) }
113 2u { sort_and_fmt(freqs, total) }
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
114 3u { #fmt["%u\t%s", find(freqs, ~"GGT"), ~"GGT"] }
115 4u { #fmt["%u\t%s", find(freqs, ~"GGTA"), ~"GGTA"] }
116 6u { #fmt["%u\t%s", find(freqs, ~"GGTATT"), ~"GGTATT"] }
117 12u { #fmt["%u\t%s", find(freqs, ~"GGTATTTTAATT"), ~"GGTATTTTAATT"] }
118 18u { #fmt["%u\t%s", find(freqs, ~"GGTATTTTAATTTATAGT"), ~"GGTATTTTAATTTATAGT"] }
119 _ { ~"" }
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
120 };
121
122 //comm::send(to_parent, #fmt["yay{%u}", sz]);
123 to_parent.send(buffer);
124 }
125
126 // given a FASTA file on stdin, process sequence THREE
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
127 fn main(args: ~[~str]) {
128 let rdr = if os::getenv(~"RUST_BENCH").is_some() {
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
129 // FIXME: Using this compile-time env variable is a crummy way to
130 // get to this massive data set, but #include_bin chokes on it (#2598)
131 let path = path::connect(
132 #env("CFG_SRC_DIR"),
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
133 ~"src/test/bench/shootout-k-nucleotide.data"
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
134 );
135 result::get(io::file_reader(path))
136 } else {
137 io::stdin()
138 };
139
140
141
142 // initialize each sequence sorter
143 let sizes = ~[1u,2u,3u,4u,6u,12u,18u];
144 let streams = vec::map(sizes, |_sz| some(stream()));
145 let streams = vec::to_mut(streams);
146 let mut from_child = ~[];
147 let to_child = vec::mapi(sizes, |ii, sz| {
148 let mut stream = none;
149 stream <-> streams[ii];
150 let (to_parent_, from_child_) = option::unwrap(stream);
151
152 vec::push(from_child, from_child_);
153
22e955a @eholk Move streams into core.
eholk authored
154 let (to_child, from_parent) = pipes::stream();
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
155
156 do task::spawn_with(from_parent) |from_parent| {
157 make_sequence_processor(sz, from_parent, to_parent_);
158 };
159
160 to_child
161 });
162
163
164 // latch stores true after we've started
165 // reading the sequence of interest
166 let mut proc_mode = false;
167
168 while !rdr.eof() {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
169 let line: ~str = rdr.read_line();
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
170
c26d025 @graydon Switch 'cont' to 'again' everywhere. Close #2229.
graydon authored
171 if str::len(line) == 0u { again; }
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
172
173 alt (line[0], proc_mode) {
174
175 // start processing if this is the one
176 ('>' as u8, false) {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
177 alt str::find_str_from(line, ~"THREE", 1u) {
b925648 @eholk Added a k-nucleotide version that uses pipes. 31% speedup.
eholk authored
178 option::some(_) { proc_mode = true; }
179 option::none { }
180 }
181 }
182
183 // break our processing
184 ('>' as u8, true) { break; }
185
186 // process the sequence for k-mers
187 (_, true) {
188 let line_bytes = str::bytes(line);
189
190 for sizes.eachi |ii, _sz| {
191 let mut lb = line_bytes;
192 to_child[ii].send(lb);
193 }
194 }
195
196 // whatever
197 _ { }
198 }
199 }
200
201 // finish...
202 for sizes.eachi |ii, _sz| {
203 to_child[ii].send(~[]);
204 }
205
206 // now fetch and print result messages
207 for sizes.eachi |ii, _sz| {
208 io::println(from_child[ii].recv());
209 }
210 }
211
Something went wrong with that request. Please try again.