summaryrefslogtreecommitdiff
path: root/test/pending/shootout/fasta.scala
blob: ae99ba5936c1f7dab1d6e15c4684101c70fd1903 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* The Computer Language Shootout
   http://shootout.alioth.debian.org/
   contributed by Isaac Gouy
*/

import java.io._

object fasta {
   def main(args: Array[String]) = {

      val ALU =
         "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" +
         "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" +
         "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" +
         "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" +
         "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
         "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
         "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"

      val _IUB = Array(
         ('a', 0.27),
         ('c', 0.12),
         ('g', 0.12),
         ('t', 0.27),

         ('B', 0.02),
         ('D', 0.02),
         ('H', 0.02),
         ('K', 0.02),
         ('M', 0.02),
         ('N', 0.02),
         ('R', 0.02),
         ('S', 0.02),
         ('V', 0.02),
         ('W', 0.02),
         ('Y', 0.02)
      )

      val IUB = makeCumulative(_IUB)

      val _HomoSapiens = Array(
         ('a', 0.3029549426680),
         ('c', 0.1979883004921),
         ('g', 0.1975473066391),
         ('t', 0.3015094502008)
      )

      val HomoSapiens = makeCumulative(_HomoSapiens)


      val n = Integer parseInt(args(0))
      val s = new FastaOutputStream(System.out)

      s.writeDescription("ONE Homo sapiens alu")
      s.writeRepeatingSequence(ALU,n*2)

      s.writeDescription("TWO IUB ambiguity codes")
      s.writeRandomSequence(IUB,n*3)

      s.writeDescription("THREE Homo sapiens frequency")
      s.writeRandomSequence(HomoSapiens,n*5)

      s.close
   }

   def makeCumulative(a: Array[Tuple2[Char,Double]]) = {
      var cp = 0.0
      a map (frequency =>
         frequency match {
            case (code,percent) =>
               cp = cp + percent; new Frequency(code.toByte,cp)
         }
      )
   }

}


// We could use instances of Pair or Tuple2 but specific labels
// make the code more readable than index numbers

class Frequency(_code: Byte, _percent: Double){
   var code = _code; var percent = _percent;
}


// extend the Java BufferedOutputStream class

class FastaOutputStream(out: OutputStream) extends BufferedOutputStream(out) {

   private val LineLength = 60
   private val nl = '\n'.toByte

   def writeDescription(desc: String) = { write( (">" + desc + "\n").getBytes ) }

   def writeRepeatingSequence(_alu: String, length: Int) = {
      val alu = _alu.getBytes
      var n = length; var k = 0; val kn = alu.length;

      while (n > 0) {
         val m = if (n < LineLength) n else LineLength

         var i = 0
         while (i < m){
            if (k == kn) k = 0
            val b = alu(k)
            if (count < buf.length){ buf(count) = b; count = count + 1 }
            else { write(b) } // flush buffer
            k = k+1
            i = i+1
         }

         write(nl)
         n = n - LineLength
      }

   }

   def writeRandomSequence(distribution: Array[Frequency], length: Int) = {
      var n = length
      while (n > 0) {
         val m = if (n < LineLength) n else LineLength

         var i = 0
         while (i < m){
            val b = selectRandom(distribution)
            if (count < buf.length){ buf(count) = b; count = count + 1 }
            else { write(b) } // flush buffer
            i = i+1
         }

         if (count < buf.length){ buf(count) = nl; count = count + 1 }
         else { write(nl) } // flush buffer
         n = n - LineLength
      }
   }

   private def selectRandom(distribution: Array[Frequency]): Byte = {
      val n = distribution.length
      val r = RandomNumber scaledTo(1.0)

      var i = 0
      while (i < n) {
         if (r < distribution(i).percent) return distribution(i).code
         i = i+1
      }
      return distribution(n-1).code
   }
}


object RandomNumber {
   private val IM = 139968
   private val IA = 3877
   private val IC = 29573
   private var seed = 42

   def scaledTo(max: Double) = {
      seed = (seed * IA + IC) % IM
      max * seed / IM
   }
}