1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/* The Computer Language Shootout
http://shootout.alioth.debian.org/
contributed by Isaac Gouy
*/
import java.io._
import scala.collection.mutable.HashMap
import scala.Console
object knucleotide {
var sequence: String = _
def main(args: Array[String]) = {
val r = new BufferedReader(new InputStreamReader(System.in))
findSequence(">THREE", r)
sequence = nextSequence(r)
r.close
writeFrequencies(1)
writeFrequencies(2)
writeCount("GGT")
writeCount("GGTA")
writeCount("GGTATT")
writeCount("GGTATTTTAATT")
writeCount("GGTATTTTAATTTATAGT")
}
def findSequence(id: String, r: BufferedReader) {
var line = r.readLine
while (line != null) {
val c = line.charAt(0)
if (c == '>' && line.startsWith(id)) return
line = r.readLine
}
}
def nextSequence(r: BufferedReader): String = {
val b = new StringBuffer()
var line = r.readLine
while (line != null) {
val c = line.charAt(0)
if (c == '>') {
return b.toString
} else {
if (c != ';') b.append(line.toUpperCase)
}
line = r.readLine
}
b.toString
}
class Counter(_value: Int) {
var value = _value
def ++() = { value += 1 }
}
def generateFrequencies(length: Int) = {
val d: HashMap[String,Counter] = new HashMap()
def kFrequency(offset: Int, j: Int) = {
val n = sequence.length - j + 1
var i = offset
while (i < n){
val k = sequence.substring(i, i+j)
d.get(k) match {
case None => d += (k -> new Counter(1))
case Some(c) => c++
}
i += j
}
}
for (o <- Iterator.range(0,length)) kFrequency(o,length)
d
}
def writeFrequencies(j: Int) {
val d = generateFrequencies(j)
val n = sequence.length - j + 1.0
val sortedValues = d.toList.sort(
(a,b) => if (a._2.value == b._2.value) a._1 > b._1
else a._2.value > b._2.value )
for (a <- sortedValues)
Console.printf("%s %.3f\n", a._1, a._2.value / n * 100.0)
Console.println
}
def writeCount(fragment: String) {
val d = generateFrequencies(fragment.length)
val count = if (d.contains(fragment)) d(fragment).value else 0
Console.println(count + "\t" + fragment)
}
}
|