-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzuker_2.scala
206 lines (187 loc) · 6.88 KB
/
zuker_2.scala
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import scala.collection.mutable.ArrayBuffer
import java.util.Random
object Main extends App {
val infinity = 99999
var matrix_w = ArrayBuffer[ArrayBuffer[Double]]()
var matrix_v = ArrayBuffer[ArrayBuffer[Double]]()
var length = 0
var sum: Double = 0
val d: scala.collection.immutable.Map[ArrayBuffer[Char], Array[java.lang.String]] =
Map(ArrayBuffer[Char]('P', 'h', 'e') -> Array[String]("UUA", "UUC", "UUG", "UUU"), ArrayBuffer[Char]('L', 'e', 'u') -> Array[String]("CUA", "CUC", "CUG", "CUU"),
ArrayBuffer[Char]('I', 'l', 'e') -> Array[String]("AUA", "AUC", "AUU"), ArrayBuffer[Char]('M', 'e', 't') -> Array[String]("AUG"),
ArrayBuffer[Char]('S', 'e', 'r') -> Array[String]("UCA", "UCC", "UCG", "UCU", "AGU", "AGC"), ArrayBuffer[Char]('P', 'r', 'o') -> Array[java.lang.String]("CCA", "CCC", "CCG", "CCU"),
ArrayBuffer[Char]('U', 'h', 'r') -> Array[String]("ACA", "ACC", "ACG", "ACU"), ArrayBuffer[Char]('A', 'l', 'a') -> Array[java.lang.String]("GCA", "GCC", "GCG", "GCU"),
ArrayBuffer[Char]('U', 'y', 'r') -> Array[String]("UAC", "UAU"), ArrayBuffer[Char]('H', 'i', 's') -> Array[String]("CAC", "CAU"), ArrayBuffer[Char]('G', 'l', 'n') -> Array[String]("CAA", "CAG"),
ArrayBuffer[Char]('A', 's', 'n') -> Array[String]("AAC", "AAU"), ArrayBuffer[Char]('L', 'y', 's') -> Array[String]("AAA", "AAG"), ArrayBuffer[Char]('A', 's', 'p') -> Array[String]("GAC", "GAU"),
ArrayBuffer[Char]('G', 'l', 'u') -> Array[String]("GAA", "GAG"), ArrayBuffer[Char]('C', 'y', 's') -> Array[String]("UGC", "UGU"),
ArrayBuffer[Char]('U', 'r', 'p') -> Array[String]("UGG"),
ArrayBuffer[Char]('A', 'r', 'g') -> Array[String]("CGA", "CGC", "CGG", "CGU", "AGA", "AGG"),
ArrayBuffer[Char]('G', 'l', 'y') -> Array[String]("GGA", "GGC", "GGG", "GGU"))
def is_complement(x: Char, y: Char): Boolean =
if (((x == 'A') && (y == 'U')) || ((x == 'U') && (y == 'A')) || ((x == 'G') && (y == 'C')) || ((x == 'C') && (y == 'G'))) true else false
def _matrix_v(str: String) = {
length = str.length()
var helping_list = ArrayBuffer[Double]()
for (i <- 0 to str.length - 1) {
for (j <- 0 to str.length - 1) {
helping_list += 8.4
}
matrix_v += helping_list
helping_list = ArrayBuffer[Double]()
}
for (i <- 0 to length - 1) {
for (j <- 0 to length - 1) {
if ((i > j - 4) && (i < j)) matrix_v(i)(j) = infinity
}
}
for (i <- 0 to length - 1) {
for (j <- 0 to length - 1) {
if (i < j) {
var my_list = ArrayBuffer[Double]()
my_list += eh(i, j)
my_list += es(i, j) + matrix_v(i + 1)(j - 1)
my_list += VBI(i, j)
var local_minimum = my_list(0)
for (k <- 1 to my_list.length - 1) {
if (local_minimum > my_list(k)) local_minimum = my_list(k)
}
matrix_v(i)(j) = local_minimum
}
}
}
zuker(str)
}
def one(i: Integer, j: Integer, str: java.lang.String): Double = {
var flag: Boolean = false
for (n <- i + 1 to j) {
if (is_complement(str(i), str(n))) flag = true
}
if (flag) infinity
else matrix_v(i + 1)(j)
}
def two(i: Integer, j: Integer, str: java.lang.String): Double = {
var flag: Boolean = false
for (n <- i + 0 to j - 1) {
if (is_complement(str(j), str(n))) flag = true
}
if (flag) infinity
else matrix_w(i)(j - 1)
}
def three(i: Integer, j: Integer, str: java.lang.String): Double = {
if (!is_complement(str(i), str(j))) infinity
else matrix_v(i)(j)
}
def four(i: Integer, j: Integer, str: java.lang.String): Double = {
if (is_complement(str(i), str(j))) infinity
var local_minimum: Double = matrix_w(i)(i + 1) + matrix_w(i + 2)(j)
for (k <- i + 2 to j - 2) {
if (matrix_w(i)(k) + matrix_w(k + 1)(j) < local_minimum) local_minimum = matrix_w(i)(k) + matrix_w(k + 1)(j)
}
local_minimum
}
def zuker(str: java.lang.String) = {
var helping_list = ArrayBuffer[Double]()
for (i <- 0 to length - 1) {
for (j <- 0 to length - 1) {
helping_list += 0
}
matrix_w += helping_list
helping_list = ArrayBuffer[Double]()
}
for (i <- 0 to length - 1) {
for (j <- 0 to length - 1) {
if ((i > j - 4) && (i < j)) matrix_w(i)(j) = infinity
}
}
for (i <- 0 to length - 1) {
for (j <- 0 to length - 1) {
if ((i < j) && (j - i > 4)) {
var my_list = ArrayBuffer[Double]()
my_list += one(i, j, str)
my_list += two(i, j, str)
my_list += three(i, j, str)
my_list += four(i, j, str)
var local_minimum = my_list(0)
for (k <- 1 to 3) {
if (local_minimum > my_list(k)) local_minimum = my_list(k)
}
matrix_w(i)(j) = local_minimum
}
}
}
for (i <- 0 to length - 1) {
for (j <- 0 to length - 1) {
if (matrix_w(i)(j) < infinity) sum += matrix_w(i)(j)
}
}
}
def eh(i: Integer, j: Integer): Double = {
if ((length > 4) && (length < 10)) 4.4
else if ((length > 9) && (length < 15)) 5.3
else if ((length > 14) && (length < 20)) 5.8
else if ((length > 19) && (length < 25)) 6.1
else if ((length > 24) && (length < 30)) 6.3
else if (length > 29) 6.5
else infinity
}
def es(i: Integer, j: Integer): Double = {
-0.5
}
def eb(i: Integer, j: Integer, a: Integer, b: Integer): Double = {
-0.4
}
def VBI(i: Integer, j: Integer): Double = {
var local_minimum: Double = infinity
for (a <- i + 1 to j - 2) {
for (b <- i + 2 to j - 1) {
if ((a < b) && (a - i + j - b > 2)) {
val d = eb(i, j, a, b) + matrix_v(a)(b)
if (local_minimum > d) local_minimum = d
}
}
}
local_minimum
}
def monte_carlo(str: String, number: Integer): Integer = {
var optimal_structure: String = ""
var optimal_energy: Double = 0
var s = ArrayBuffer[Char]()
length = str.length()
for (i <- 0 to length - 1) {
if ((i + 1) % 3 != 0) {
s += str(i)
}
else if ((i + 1) % 3 == 0) {
s += str(i)
optimal_structure += d(s)(0)
s = ArrayBuffer[Char]()
}
}
_matrix_v(optimal_structure)
optimal_energy = sum
for (i <- 2 to number) {
var this_structure: String = ""
var this_s = ArrayBuffer[Char]()
for (j <- 0 to length - 1) {
if ((j + 1) % 3 != 0) {
this_s += str(j)
}
else if ((j + 1) % 3 == 0) {
this_s += str(j)
var rand = new Random()
var r = rand.nextInt(d(this_s).length)
this_structure += d(this_s)(r)
this_s = ArrayBuffer[Char]()
}
_matrix_v(this_structure)
if (sum < optimal_energy) {
optimal_structure = this_structure
optimal_energy = sum
}
}
}
print(optimal_structure, optimal_energy)
1
}
monte_carlo("PheSerProProAlaUrpUrpCysPhe", 20)
}