]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/knucleotide/knucleotide.factor
34d5010f5de6cc33f98259e722dd8ac2981dfc61
[factor.git] / extra / benchmark / knucleotide / knucleotide.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: ascii kernel io io.files splitting strings
4 io.encodings.ascii hashtables sequences assocs math
5 math.statistics namespaces math.parser combinators arrays
6 sorting formatting grouping fry ;
7 IN: benchmark.knucleotide
8
9 CONSTANT: knucleotide-in "vocab:benchmark/knucleotide/knucleotide-input.txt"
10
11 : discard-lines ( -- )
12     readln
13     [ ">THREE" head? [ discard-lines ] unless ] when* ;
14
15 : read-input ( -- input )
16     discard-lines
17     ">" read-until drop
18     CHAR: \n swap remove >upper ;
19
20 : handle-table ( inputs n -- )
21     clump
22     [ sorted-histogram reverse ] [ length ] bi
23     '[
24         [ first write bl ]
25         [ second 100 * _ /f "%.3f" printf nl ] bi
26     ] each ;
27
28 : handle-n ( input x -- )
29     [ nip ] [ length clump histogram ] 2bi at 0 or "%d\t" printf ;
30
31 : process-input ( input -- )
32     [ 1 handle-table nl ]
33     [ 2 handle-table nl ]
34     [
35         { "GGT" "GGTA" "GGTATT" "GGTATTTTAATT" "GGTATTTTAATTTATAGT" }
36         [ [ handle-n ] keep print ] with each
37     ]
38     tri ;
39
40 : knucleotide-benchmark ( -- )
41     knucleotide-in
42     ascii [ read-input ] with-file-reader
43     process-input ;
44
45 MAIN: knucleotide-benchmark