NeedlemanWunsch (NeoBio API)

Overview | Package | Class | Tree | Index | Help
| NeoBio API
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## neobio.alignment
Class NeedlemanWunsch

    java.lang.Object
      |
      +--neobio.alignment.PairwiseAlignmentAlgorithm
            |
            +--neobio.alignment.NeedlemanWunsch

public class NeedlemanWunsch
extends PairwiseAlignmentAlgorithm

This class implements the classic global alignment algorithm (with linear gap penalty function) due to S.B.Needleman and C.D.Wunsch (1970).

It is based on a dynamic programming approach. The idea consists of, given two sequences A and B of sizes n and m, respectively, building an (n+1 x m+1) matrix M that contains the similarity of prefixes of A and B. Every position M[i,j] in the matrix holds the score between the subsequences A[1..i] and B[1..j]. The first row and column represent alignments with spaces.

Starting from row 0, column 0, the algorithm computes each position M[i,j] with the following recurrence:

`

>      M[0,0] = 0
>      M[i,j] = max { M[i,j-1]   + scoreInsertion (B[j]),
>                     M[i-1,j-1] + scoreSubstitution (A[i], B[j]),
>                     M[i-1,j]   + scoreDeletion(A[i])             }

`

In the end, the value at the last position (last row, last column) will contain the similarity between the two sequences. This part of the algorithm is accomplished by the `computeMatrix` method. It has quadratic space complexity since it needs to keep an (n+1 x m+1) matrix in memory. And since the work of computing each cell is constant, it also has quadratic time complexity.

After the matrix has been computed, the alignment can be retrieved by tracing a path back in the matrix from the last position to the first. This step is performed by the `buildOptimalAlignment` method, and since the path can be roughly as long as (m + n), this method has O(n) time complexity.

If the similarity value only is needed (and not the alignment itself), it is easy to reduce the space requirement to O(n) by keeping just the last row or column in memory. This is precisely what is done by the `computeScore` method. Note that it still requires O(n2) time.

For a more efficient approach to the global alignment problem, see the CrochemoreLandauZivUkelson algorithm. For local alignment, see the SmithWaterman algorithm.

Author:
  Sergio A. de Carvalho Jr.
See Also:
  `SmithWaterman`, `CrochemoreLandauZivUkelson`, `CrochemoreLandauZivUkelsonLocalAlignment`, `CrochemoreLandauZivUkelsonGlobalAlignment`

Field Summary
`protected int[][]` | `matrix` The dynamic programming matrix.
`protected CharSequence` | `seq1` The first sequence of an alignment.
`protected CharSequence` | `seq2` The second sequence of an alignment.

Fields inherited from class neobio.alignment.PairwiseAlignmentAlgorithm
`alignment, APPROXIMATE_MATCH_TAG, GAP_CHARACTER, GAP_TAG, MATCH_TAG, MISMATCH_TAG, score, score_computed, scoring, sequences_loaded, use_match_tag`

Constructor Summary
`NeedlemanWunsch()`

Method Summary
`protected PairwiseAlignment` | `buildOptimalAlignment()` Builds an optimal global alignment between the loaded sequences.
`protected void` | `computeMatrix()` Computes the dynamic programming matrix.
`protected PairwiseAlignment` | `computePairwiseAlignment()` Builds an optimal global alignment between the loaded sequences after computing the dynamic programming matrix.
`protected int` | `computeScore()` Computes the score of the best global alignment between the two sequences using the scoring scheme previously set.
`protected void` | `loadSequencesInternal(java.io.Reader input1, java.io.Reader input2)` Loads sequences into CharSequence instances.
`protected void` | `unloadSequencesInternal()` Frees pointers to loaded sequences and the dynamic programming matrix so that their data can be garbage collected.

Methods inherited from class neobio.alignment.PairwiseAlignmentAlgorithm
`getPairwiseAlignment, getScore, loadSequences, max, max, max, scoreDeletion, scoreInsertion, scoreSubstitution, setScoringScheme, unloadSequences, useMatchTag`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### seq1

    protected CharSequence seq1

  The first sequence of an alignment.

### seq2

    protected CharSequence seq2

  The second sequence of an alignment.

### matrix

    protected int[][] matrix

  The dynamic programming matrix. Each position (i, j) represents the best score between the firsts i characters of `seq1` and j characters of `seq2`.

Constructor Detail

### NeedlemanWunsch

    public NeedlemanWunsch()

Method Detail

### loadSequencesInternal

    protected void loadSequencesInternal(java.io.Reader input1,
                                         java.io.Reader input2)
                                  throws java.io.IOException,
                                         InvalidSequenceException

  Loads sequences into CharSequence instances. In case of any error, an exception is raised by the constructor of `CharSequence` (please check the specification of that class for specific requirements).

  Specified by:
    `loadSequencesInternal` in class `PairwiseAlignmentAlgorithm`
  Parameters:
    `input1` - Input for first sequence
    `input2` - Input for second sequence
  Throws:
    `java.io.IOException` - If an I/O error occurs when reading the sequences
    `InvalidSequenceException` - If the sequences are not valid
  See Also:
    `CharSequence`

### unloadSequencesInternal

    protected void unloadSequencesInternal()

  Frees pointers to loaded sequences and the dynamic programming matrix so that their data can be garbage collected.

  Specified by:
    `unloadSequencesInternal` in class `PairwiseAlignmentAlgorithm`
  See Also:
    `PairwiseAlignmentAlgorithm.unloadSequences()`

### computePairwiseAlignment

    protected PairwiseAlignment computePairwiseAlignment()
                                                  throws IncompatibleScoringSchemeException

  Builds an optimal global alignment between the loaded sequences after computing the dynamic programming matrix. It calls the `buildOptimalAlignment` method after the `computeMatrix` method computes the dynamic programming matrix.

  Specified by:
    `computePairwiseAlignment` in class `PairwiseAlignmentAlgorithm`
  Returns:
    an optimal global alignment between the loaded sequences
  Throws:
    `IncompatibleScoringSchemeException` - If the scoring scheme is not compatible with the loaded sequences.
  See Also:
    `computeMatrix()`, `buildOptimalAlignment()`

### computeMatrix

    protected void computeMatrix()
                          throws IncompatibleScoringSchemeException

  Computes the dynamic programming matrix.

  Throws:
    `IncompatibleScoringSchemeException` - If the scoring scheme is not compatible with the loaded sequences.

### buildOptimalAlignment

    protected PairwiseAlignment buildOptimalAlignment()
                                               throws IncompatibleScoringSchemeException

  Builds an optimal global alignment between the loaded sequences. Before it is executed, the dynamic programming matrix must already have been computed by the `computeMatrix` method.

  Returns:
    an optimal global alignment between the loaded sequences
  Throws:
    `IncompatibleScoringSchemeException` - If the scoring scheme is not compatible with the loaded sequences.
  See Also:
    `computeMatrix()`

### computeScore

    protected int computeScore()
                        throws IncompatibleScoringSchemeException

  Computes the score of the best global alignment between the two sequences using the scoring scheme previously set. This method calculates the similarity value only (doesn't build the whole matrix so the alignment cannot be recovered, however it has the advantage of requiring O(n) space only).

  Specified by:
    `computeScore` in class `PairwiseAlignmentAlgorithm`
  Returns:
    score of the best global alignment between the loaded sequences
  Throws:
    `IncompatibleScoringSchemeException` - If the scoring scheme is not compatible with the loaded sequences.
  See Also:
    `PairwiseAlignmentAlgorithm.getScore()`

Overview | Package | Class | Tree | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

http://neobio.sourceforge.net
NeoBio is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. NeoBio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with NeoBio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.