import scala.io.Source import Array._ /** * Created by Const. */ class Aligner(fileWithData: String) { val data = Source.fromFile(fileWithData).mkString.split("\n").toList val alphabet = data(0).zipWithIndex.map{case (x: Char, i: Int) => (x, i)}.toMap val (xx, xt) = (data.take(alphabet.size + 1).tail, data.drop(alphabet.size + 1)) val mtx = xx.map((x: String) => x.split(" ").map((num: String) => num.toInt)).toArray for ((row, idx) <- mtx.zipWithIndex) { for (j <- row) { print("!" + j) } println() } val (gapScore, maxGaps, firstSeq, secondSeq) = xt 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 alignWithMiller() { var scoreMatrix = ofDim[Int](firstSeq.length + 1, secondSeq.length + 1) var initialString = Range(0, -secondSeq.length, -1).toArray } def alignWithNiddleman(maximumGaps: Int = maxGaps) { 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 = if (i > 1) Range(1, i).toList.map((k: Int) => scoreMatrix(i - k)(j) + gapScore*k).max else scoreMatrix(1)(j - 1) + gapScore val insert = if (j > 1) Range(1, j).toList.map((k: Int) => scoreMatrix(i)(j - k) + gapScore*k).max else scoreMatrix(i - 1)(1) + gapScore scoreMatrix(i)(j) = List[Int](matcher, delete, insert).max } } for (row <- scoreMatrix) { for (j <- row) { printf("%-5d", j) } println() } var aligned = (List[Char](), List[Char]()) var i = secondSeq.length var j = firstSeq.length 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) match { case x:List[Int] if forward == x.max => { aligned = (secondSeq(i - 1) +: aligned._1, firstSeq(j - 1) +: aligned._2) i -= 1 j -= 1 } case x:List[Int] if gapFirst == x.max => { aligned = ('-' +: aligned._1, firstSeq(j - 1) +: aligned._2) j -= 1 } case x:List[Int] if gapSecond == x.max => { aligned = (secondSeq(i - 1) +: aligned._1, '-' +: aligned._2) i -= 1 } } } if (i > 0) { while (i > 0) { aligned = (secondSeq(i - 1) +: aligned._1, '-' +: aligned._2) i -= 1 } } if (j > 0) { while (j > 0) { aligned = ('-' +: aligned._1, firstSeq(j - 1) +: aligned._2) j -= 1 } } println(aligned._1 mkString) println(aligned._2 mkString) } }