1/* 2Redistribution and use in source and binary forms, with or without 3modification, are permitted provided that the following conditions are met: 4 5 * Redistributions of source code must retain the above copyright 6 notice, this list of conditions and the following disclaimer. 7 8 * Redistributions in binary form must reproduce the above copyright 9 notice, this list of conditions and the following disclaimer in the 10 documentation and/or other materials provided with the distribution. 11 12 * Neither the name of "The Computer Language Benchmarks Game" nor the 13 name of "The Computer Language Shootout Benchmarks" nor the names of 14 its contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27POSSIBILITY OF SUCH DAMAGE. 28*/ 29 30/* The Computer Language Benchmarks Game 31 * http://shootout.alioth.debian.org/ 32 * 33 * contributed by The Go Authors. 34 */ 35 36package main 37 38import ( 39 "fmt" 40 "io/ioutil" 41 "os" 42 "regexp" 43) 44 45var variants = []string{ 46 "agggtaaa|tttaccct", 47 "[cgt]gggtaaa|tttaccc[acg]", 48 "a[act]ggtaaa|tttacc[agt]t", 49 "ag[act]gtaaa|tttac[agt]ct", 50 "agg[act]taaa|ttta[agt]cct", 51 "aggg[acg]aaa|ttt[cgt]ccct", 52 "agggt[cgt]aa|tt[acg]accct", 53 "agggta[cgt]a|t[acg]taccct", 54 "agggtaa[cgt]|[acg]ttaccct", 55} 56 57type Subst struct { 58 pat, repl string 59} 60 61var substs = []Subst{ 62 Subst{"B", "(c|g|t)"}, 63 Subst{"D", "(a|g|t)"}, 64 Subst{"H", "(a|c|t)"}, 65 Subst{"K", "(g|t)"}, 66 Subst{"M", "(a|c)"}, 67 Subst{"N", "(a|c|g|t)"}, 68 Subst{"R", "(a|g)"}, 69 Subst{"S", "(c|g)"}, 70 Subst{"V", "(a|c|g)"}, 71 Subst{"W", "(a|t)"}, 72 Subst{"Y", "(c|t)"}, 73} 74 75func countMatches(pat string, bytes []byte) int { 76 re := regexp.MustCompile(pat) 77 n := 0 78 for { 79 e := re.FindIndex(bytes) 80 if len(e) == 0 { 81 break 82 } 83 n++ 84 bytes = bytes[e[1]:] 85 } 86 return n 87} 88 89func main() { 90 bytes, err := ioutil.ReadAll(os.Stdin) 91 if err != nil { 92 fmt.Fprintf(os.Stderr, "can't read input: %s\n", err) 93 os.Exit(2) 94 } 95 ilen := len(bytes) 96 // Delete the comment lines and newlines 97 bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{}) 98 clen := len(bytes) 99 for _, s := range variants { 100 fmt.Printf("%s %d\n", s, countMatches(s, bytes)) 101 } 102 for _, sub := range substs { 103 bytes = regexp.MustCompile(sub.pat).ReplaceAll(bytes, []byte(sub.repl)) 104 } 105 fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes)) 106} 107