Debian Codegraph @

shootout/k-nucleotide.go

raw symbols SCIP
1 //go:build ignore
2 // +build ignore
3
4 /*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 * Neither the name of "The Computer Language Benchmarks Game" nor the
16 name of "The Computer Language Shootout Benchmarks" nor the names of
17 its contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* The Computer Language Benchmarks Game
34 * http://shootout.alioth.debian.org/
35 *
36 * contributed by The Go Authors.
37 */
38
39 package main
40
41 import (
42 "bufio"
43 "bytes"
44 "fmt"
45 "io"
46 "os"
47 "sort"
48 )
49
50 var in *bufio.Reader
51
52 func count(data string, n int) map[string]int {
53 counts := make(map[string]int)
54 top := len(data) - n
55 for i := 0; i <= top; i++ {
56 s := data[i : i+n]
57 counts[s]++
58 }
59 return counts
60 }
61
62 func countOne(data string, s string) int {
63 return count(data, len(s))[s]
64 }
65
66 type kNuc struct {
67 name string
68 count int
69 }
70
71 type kNucArray []kNuc
72
73 func (kn kNucArray) Len() int { return len(kn) }
74 func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
75 func (kn kNucArray) Less(i, j int) bool {
76 if kn[i].count == kn[j].count {
77 return kn[i].name > kn[j].name // sort down
78 }
79 return kn[i].count > kn[j].count
80 }
81
82 func sortedArray(m map[string]int) kNucArray {
83 kn := make(kNucArray, len(m))
84 i := 0
85 for k, v := range m {
86 kn[i].name = k
87 kn[i].count = v
88 i++
89 }
90 sort.Sort(kn)
91 return kn
92 }
93
94 func print(m map[string]int) {
95 a := sortedArray(m)
96 sum := 0
97 for _, kn := range a {
98 sum += kn.count
99 }
100 for _, kn := range a {
101 fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum))
102 }
103 }
104
105 func main() {
106 in = bufio.NewReader(os.Stdin)
107 three := []byte(">THREE ")
108 for {
109 line, err := in.ReadSlice('\n')
110 if err != nil {
111 fmt.Fprintln(os.Stderr, "ReadLine err:", err)
112 os.Exit(2)
113 }
114 if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
115 break
116 }
117 }
118 data, err := io.ReadAll(in)
119 if err != nil {
120 fmt.Fprintln(os.Stderr, "ReadAll err:", err)
121 os.Exit(2)
122 }
123 // delete the newlines and convert to upper case
124 j := 0
125 for i := 0; i < len(data); i++ {
126 if data[i] != '\n' {
127 data[j] = data[i] &^ ' ' // upper case
128 j++
129 }
130 }
131 str := string(data[0:j])
132
133 print(count(str, 1))
134 fmt.Print("\n")
135
136 print(count(str, 2))
137 fmt.Print("\n")
138
139 interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"}
140 for _, s := range interests {
141 fmt.Printf("%d %s\n", countOne(str, s), s)
142 }
143 }