Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
nh13 committed Jan 16, 2025
1 parent 7a5a4da commit a44f400
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions src/main/scala/com/fulcrumgenomics/alignment/Aligner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ object Aligner {
)
}.toArray

override protected def buildMatrices(query: Array[Byte], target: Array[Byte]): Array[AlignmentMatrix] = {
override protected def allocMatrices(query: Array[Byte], target: Array[Byte]): Array[AlignmentMatrix] = {
if (query.length > this.matrices(0).scoring.x || target.length > this.matrices(0).scoring.y) {
val xLength = math.max(query.length, this.matrices(0).scoring.x)
val yLength = math.max(target.length, this.matrices(0).scoring.y)
Expand All @@ -162,30 +162,10 @@ object Aligner {
matrix.copy(queryLength=query.length, targetLength=target.length)
}
}

// While we have `matrices` above, it's useful to unpack all the matrices for direct access
// in the core loop; when we know the exact matrix we need at compile time, it's faster
val (leftScoreMatrix, leftTraceMatrix, upScoreMatrix, upTraceMatrix, diagScoreMatrix, diagTraceMatrix) = {
val Seq(l, u, d) = Seq(Left, Up, Diagonal).map(matrices.apply)
(l.scoring, l.trace, u.scoring, u.trace, d.scoring, d.trace)
}

// Top left corner - allow all to be zero score but then must be careful to initialize Up and Left to have a gap open
AllDirections.foreach { direction =>
matrices(direction).scoring(0, 0) = 0
matrices(direction).trace(0, 0) = Done
}

fillLeftmostColumn(query, target, leftScoreMatrix, leftTraceMatrix, upScoreMatrix, upTraceMatrix, diagScoreMatrix, diagTraceMatrix)
fillTopRow(query, target, leftScoreMatrix, leftTraceMatrix, upScoreMatrix, upTraceMatrix, diagScoreMatrix, diagTraceMatrix)
fillInterior(query, target, leftScoreMatrix, leftTraceMatrix, upScoreMatrix, upTraceMatrix, diagScoreMatrix, diagTraceMatrix)

matrices.map(_.asInstanceOf[AlignmentMatrix])
this.matrices.map(_.asInstanceOf[AlignmentMatrix])
}

}


/** Represents a cell within the set of matrices used for alignment. */
private case class MatrixLocation(queryIndex: Int, targetIndex: Int, direction: Direction)

Expand Down Expand Up @@ -304,6 +284,10 @@ class Aligner(val scorer: AlignmentScorer,
locations.map(l => generateAlignment(query, target, matrices, l))
}

protected def allocMatrices(query: Array[Byte], target: Array[Byte]): Array[AlignmentMatrix] = {
AllDirections.sorted.map(dir => AlignmentMatrix(direction=dir, queryLength=query.length, targetLength=target.length)).toArray
}

/**
* Constructs both the scoring and traceback matrices.
*
Expand All @@ -320,8 +304,9 @@ class Aligner(val scorer: AlignmentScorer,
* @param target the target sequence
* @return an array of alignment matrices, where the indices to the array are the Directions
*/

protected def buildMatrices(query: Array[Byte], target: Array[Byte]): Array[AlignmentMatrix] = {
val matrices = AllDirections.sorted.map(dir => AlignmentMatrix(direction=dir, queryLength=query.length, targetLength=target.length)).toArray
val matrices = allocMatrices(query=query, target=target)

// While we have `matrices` above, it's useful to unpack all the matrices for direct access
// in the core loop; when we know the exact matrix we need at compile time, it's faster
Expand Down

0 comments on commit a44f400

Please sign in to comment.