See the Wikipedia page on the Smith-Waterman algorithm.


Imports and implicits

import org.jblas.DoubleMatrix

import cats.implicits._

import spire.implicits.IntAlgebra

import SmithWatermanDefaults._
import SmithWaterman.optimalAlignment

implicit val laJblasInt = axle.jblas.linearAlgebraDoubleMatrix[Int]


val dna3 = "ACACACTA"
// dna3: String = ACACACTA

val dna4 = "AGCACACA"
// dna4: String = AGCACACA

Align the sequences

val swAlignment = optimalAlignment[IndexedSeq[Char], Char, DoubleMatrix, Int, Int](
  dna3, dna4, w, mismatchPenalty, gap)
// swAlignment: (IndexedSeq[Char], IndexedSeq[Char]) = (Vector(A, -, C, A, C, A, C, T, A),Vector(A, G, C, A, C, A, C, -, A))

Compute distance of the sequences

import spire.implicits.DoubleAlgebra
// import spire.implicits.DoubleAlgebra

val space = SmithWatermanMetricSpace[IndexedSeq[Char], Char, DoubleMatrix, Int, Int](w, mismatchPenalty)
// space:[IndexedSeq[Char],Char,org.jblas.DoubleMatrix,Int,Int] = SmithWatermanMetricSpace(<function3>,-1)

space.distance(dna3, dna4)
// res4: Int = 12