Niddleman-Wunsh + Smith-Waterman

  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
import scala.io.Source
import Array._
/**
* Created by Const.
*/
// Object-companion
object Aligner {
// Helper function
// Find index of maximum element in array
// And (optional) that is not contained in Excluded Indices Set
def indexOfLargest(array: Seq[Int], excIndices: Set[Int] = Set[Int]()): Int = {
val result = array.foldLeft(-1,Int.MinValue,0) {
case ((maxIndex, maxValue, currentIndex), currentValue) =>
if(currentValue > maxValue && !(excIndices contains currentIndex)) (currentIndex,currentValue,currentIndex+1)
else (maxIndex,maxValue,currentIndex+1)
}
result._1
}
def intMatrixDebugPrint(mtx: Array[Array[Int]]) {
for ((row, idx) <- mtx.zipWithIndex) {
for (j <- row) {
print(f"$j%-5d")
}
println()
}
println()
}
}
class Aligner(fileWithData: String) {
def buildScoreMatrix(isLocal: Boolean = false) {
for (i <- 0 to secondSeq.length)
scoreMatrix(i)(0) = 0
for (j <- 0 to firstSeq.length)
scoreMatrix(0)(j) = 0
for (i <- scoreMatrix.indices if i > 0) {
for (j <- scoreMatrix(i).indices if j > 0) {
val matcher = scoreMatrix(i - 1)(j - 1) + mtx(alphabet(secondSeq(i - 1)))(alphabet(firstSeq(j - 1)))
val delete = scoreMatrix(i - 1)(j) + gapScore
val insert = scoreMatrix(i)(j - 1) + gapScore
scoreMatrix(i)(j) = isLocal match {
case true => List[Int](matcher, delete, insert, 0).max
case false => List[Int](matcher, delete, insert).max
}
}
}
}
def alignWithNiddleman(maximumGaps: Int = maxGaps): (List[Char], List[Char]) = {
// Build Score Matrix
buildScoreMatrix(isLocal = false)
// Some debug print pf Score Matrix
println("Score Matrix:")
Aligner.intMatrixDebugPrint(scoreMatrix)
findProperNiddlemanAlignment()
}
def findProperNiddlemanAlignment(): (List[Char], List[Char]) = {
// Find optimal global alignment
var aligned = (List[Char](), List[Char]())
var i = scoreMatrix.size - 1
var j = scoreMatrix(i).size - 1
while (i != 0 && j != 0) {
val forward = scoreMatrix(i - 1)(j - 1)
val gapSecond = scoreMatrix(i - 1)(j)
val gapFirst = scoreMatrix(i)(j - 1)
(forward :: gapFirst :: gapSecond :: Nil).max match {
case x if forward == x => {
aligned = (firstSeq(j - 1) +: aligned._1, secondSeq(i - 1) +: aligned._2)
i -= 1
j -= 1
}
case x if gapFirst == x => {
aligned = (firstSeq(j - 1) +: aligned._1, '-' +: aligned._2)
j -= 1
}
case x if gapSecond == x => {
aligned = ('-' +: aligned._1, secondSeq(i - 1) +: aligned._2)
i -= 1
}
}
}
if (i > 0) {
while (i > 0) {
aligned = ('-' +: aligned._1, secondSeq(i - 1) +: aligned._2)
i -= 1
}
}
if (j > 0) {
while (j > 0) {
aligned = (firstSeq(j - 1) +: aligned._1, '-' +: aligned._2)
j -= 1
}
}
aligned
}
def alignWithSmithWaterman(maximumGaps: Int = maxGaps): (List[Char], List[Char]) = {
// Build Score Matrix
buildScoreMatrix(isLocal = true)
// Some debug print pf Score Matrix
println("Smith-Waterman Score Matrix:")
Aligner.intMatrixDebugPrint(scoreMatrix)
findProperSmithAlignment()
}
def findProperSmithAlignment(excludeSet: Set[Int] = Set[Int]()): (List[Char], List[Char]) = {
// Find optimal local alignment
var aligned = (List[Char](), List[Char]())
var i = scoreMatrix.size - 1
var idxOfMax = Aligner.indexOfLargest(scoreMatrix(scoreMatrix.size - 1), excludeSet)
var j = idxOfMax
var gapCount = 0
for (k <- firstSeq.length until j by -1) {
aligned = (firstSeq(k - 1) +: aligned._1, '+' +: aligned._2)
}
while (scoreMatrix(i)(j) != 0) {
val forward = scoreMatrix(i - 1)(j - 1)
val gapSecond = scoreMatrix(i - 1)(j)
val gapFirst = scoreMatrix(i)(j - 1)
(forward :: gapFirst :: gapSecond :: Nil).max match {
case x if forward == x => {
aligned = (firstSeq(j - 1) +: aligned._1, secondSeq(i - 1) +: aligned._2)
i -= 1
j -= 1
}
case x if gapFirst == x => {
aligned = (firstSeq(j - 1) +: aligned._1, '-' +: aligned._2)
j -= 1
gapCount += 1
}
case x if gapSecond == x => {
aligned = ('-' +: aligned._1, secondSeq(i - 1) +: aligned._2)
i -= 1
gapCount += 1
}
}
if (gapCount > maxGaps)
return findProperSmithAlignment(excludeSet + idxOfMax)
}
if (i > 0) {
while (i > 0) {
aligned = ('+' +: aligned._1, secondSeq(i - 1) +: aligned._2)
i -= 1
}
}
if (j > 0) {
while (j > 0) {
aligned = (firstSeq(j - 1) +: aligned._1, '+' +: aligned._2)
j -= 1
}
}
aligned
}
// Read file with data
val data = Source.fromFile(fileWithData).mkString.split("\n").toList
// Read alphabet from alphabet string
val alphabet = data(0).zipWithIndex.toMap
// Take N rows of (N, N)-matrix from readed data and preserve other data
val (mtxData, otherData) = (data.take(alphabet.size + 1).tail, data.drop(alphabet.size + 1))
// And make matrix from matrix rows (N strings represents N rows)
val mtx = mtxData map {_ split " " map {_ toInt} } toArray
// Some debug print of error matrix
println("Error matrix")
for ((ch, idx) <- alphabet)
print(f"$ch%-5c")
println()
Aligner.intMatrixDebugPrint(mtx)
// Take other data
val (gapScore, maxGaps, firstSeq, secondSeq) = otherData match {
case (score: String) :: (maxg: String) :: (fst: String) :: (scd: String) :: Nil => (score.toInt, maxg.toInt, fst, scd)
case _ => throw new IllegalArgumentException("Please check your input file.")
}
// Initialize Score Matrix
var scoreMatrix = ofDim[Int](secondSeq.length + 1, firstSeq.length + 1)
}