OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [bench/] [go1/] [fasta_test.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// Copyright 2011 The Go Authors.  All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package go1
6
 
7
// Not a benchmark; input for revcomp.
8
 
9
var fasta25m = fasta(25e6)
10
 
11
func fasta(n int) []byte {
12
        out := make(fastaBuffer, 0, 11*n)
13
 
14
        iub := []fastaAcid{
15
                {prob: 0.27, sym: 'a'},
16
                {prob: 0.12, sym: 'c'},
17
                {prob: 0.12, sym: 'g'},
18
                {prob: 0.27, sym: 't'},
19
                {prob: 0.02, sym: 'B'},
20
                {prob: 0.02, sym: 'D'},
21
                {prob: 0.02, sym: 'H'},
22
                {prob: 0.02, sym: 'K'},
23
                {prob: 0.02, sym: 'M'},
24
                {prob: 0.02, sym: 'N'},
25
                {prob: 0.02, sym: 'R'},
26
                {prob: 0.02, sym: 'S'},
27
                {prob: 0.02, sym: 'V'},
28
                {prob: 0.02, sym: 'W'},
29
                {prob: 0.02, sym: 'Y'},
30
        }
31
 
32
        homosapiens := []fastaAcid{
33
                {prob: 0.3029549426680, sym: 'a'},
34
                {prob: 0.1979883004921, sym: 'c'},
35
                {prob: 0.1975473066391, sym: 'g'},
36
                {prob: 0.3015094502008, sym: 't'},
37
        }
38
 
39
        alu := []byte(
40
                "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" +
41
                        "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" +
42
                        "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" +
43
                        "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" +
44
                        "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
45
                        "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
46
                        "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA")
47
 
48
        out.WriteString(">ONE Homo sapiens alu\n")
49
        fastaRepeat(&out, alu, 2*n)
50
        out.WriteString(">TWO IUB ambiguity codes\n")
51
        fastaRandom(&out, iub, 3*n)
52
        out.WriteString(">THREE Homo sapiens frequency\n")
53
        fastaRandom(&out, homosapiens, 5*n)
54
        return out
55
}
56
 
57
type fastaBuffer []byte
58
 
59
func (b *fastaBuffer) Flush() {
60
        panic("flush")
61
}
62
 
63
func (b *fastaBuffer) WriteString(s string) {
64
        p := b.NextWrite(len(s))
65
        copy(p, s)
66
}
67
 
68
func (b *fastaBuffer) NextWrite(n int) []byte {
69
        p := *b
70
        if len(p)+n > cap(p) {
71
                b.Flush()
72
                p = *b
73
        }
74
        out := p[len(p) : len(p)+n]
75
        *b = p[:len(p)+n]
76
        return out
77
}
78
 
79
const fastaLine = 60
80
 
81
func fastaRepeat(out *fastaBuffer, alu []byte, n int) {
82
        buf := append(alu, alu...)
83
        off := 0
84
        for n > 0 {
85
                m := n
86
                if m > fastaLine {
87
                        m = fastaLine
88
                }
89
                buf1 := out.NextWrite(m + 1)
90
                copy(buf1, buf[off:])
91
                buf1[m] = '\n'
92
                if off += m; off >= len(alu) {
93
                        off -= len(alu)
94
                }
95
                n -= m
96
        }
97
}
98
 
99
const (
100
        fastaLookupSize          = 4096
101
        fastaLookupScale float64 = fastaLookupSize - 1
102
)
103
 
104
var fastaRand uint32 = 42
105
 
106
type fastaAcid struct {
107
        sym   byte
108
        prob  float64
109
        cprob float64
110
        next  *fastaAcid
111
}
112
 
113
func fastaComputeLookup(acid []fastaAcid) *[fastaLookupSize]*fastaAcid {
114
        var lookup [fastaLookupSize]*fastaAcid
115
        var p float64
116
        for i := range acid {
117
                p += acid[i].prob
118
                acid[i].cprob = p * fastaLookupScale
119
                if i > 0 {
120
                        acid[i-1].next = &acid[i]
121
                }
122
        }
123
        acid[len(acid)-1].cprob = 1.0 * fastaLookupScale
124
 
125
        j := 0
126
        for i := range lookup {
127
                for acid[j].cprob < float64(i) {
128
                        j++
129
                }
130
                lookup[i] = &acid[j]
131
        }
132
 
133
        return &lookup
134
}
135
 
136
func fastaRandom(out *fastaBuffer, acid []fastaAcid, n int) {
137
        const (
138
                IM = 139968
139
                IA = 3877
140
                IC = 29573
141
        )
142
        lookup := fastaComputeLookup(acid)
143
        for n > 0 {
144
                m := n
145
                if m > fastaLine {
146
                        m = fastaLine
147
                }
148
                buf := out.NextWrite(m + 1)
149
                f := fastaLookupScale / IM
150
                myrand := fastaRand
151
                for i := 0; i < m; i++ {
152
                        myrand = (myrand*IA + IC) % IM
153
                        r := float64(int(myrand)) * f
154
                        a := lookup[int(r)]
155
                        for a.cprob < r {
156
                                a = a.next
157
                        }
158
                        buf[i] = a.sym
159
                }
160
                fastaRand = myrand
161
                buf[m] = '\n'
162
                n -= m
163
        }
164
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.