Scala good local alignment

  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
import scala.io.Source
import Array._
/**
* Created by Const.
*/
class Aligner(fileWithData: String) {
// Read file with data
val data = Source.fromFile(fileWithData).mkString.split("\n").toList
// Read alphabet from alphabet string
val alphabet = data(0).zipWithIndex.map{case (x: Char, i: Int) => (x, i)}.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
val mtx = mtxData.map((x: String) => x.split(" ").map((num: String) => num.toInt)).toArray
// Some debug print of error matrix
println("Error matrix")
for ((ch, idx) <- alphabet)
print(f"$ch%-5c")
println()
for ((row, idx) <- mtx.zipWithIndex) {
for (j <- row) {
print(f"$j%-5d")
}
println()
}
println()
// 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.")
}
def alignWithNiddleman(maximumGaps: Int = maxGaps) {
// Initialize and build Score Matrix
var scoreMatrix = ofDim[Int](secondSeq.length + 1, firstSeq.length + 1)
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) = List[Int](matcher, delete, insert, 0).max
}
}
// Some debug print pf Score Matrix
println("Score Matrix:")
for (row <- scoreMatrix) {
for (j <- row) {
printf("%-5d", j)
}
println()
}
// Find optimal local alignment
var aligned = (List[Char](), List[Char]())
var i = secondSeq.length
var j = firstSeq.length
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) match {
case x:List[Int] if forward == x.max => {
aligned = (firstSeq(j - 1) +: aligned._1, secondSeq(i - 1) +: aligned._2)
i -= 1
j -= 1
}
case x:List[Int] if gapFirst == x.max => {
aligned = (firstSeq(j - 1) +: aligned._1, '-' +: aligned._2)
j -= 1
}
case x:List[Int] if gapSecond == x.max => {
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
}
}
println(aligned._1 mkString)
println(aligned._2 mkString)
}
}