nwalign

Globally align two sequences using Needleman-Wunsch algorithm

Syntax

Score = nwalign(Seq1,Seq2)
[Score, Alignment] = nwalign(Seq1,Seq2)
[Score, Alignment, Start] = nwalign(Seq1,Seq2)
... = nwalign(Seq1,Seq2, ...'Alphabet', AlphabetValue, ...)
... = nwalign(Seq1,Seq2, ...'ScoringMatrix', ScoringMatrixValue, ...)
... = nwalign(Seq1,Seq2, ...'Scale', ScaleValue, ...)
... = nwalign(Seq1,Seq2, ...'GapOpen', GapOpenValue, ...)
... = nwalign(Seq1,Seq2, ...'ExtendGap', ExtendGapValue, ...)
... = nwalign(Seq1,Seq2, ...'Glocal', GlocalValue, ...)
... = nwalign(Seq1,Seq2, ...'Showscore', ShowscoreValue, ...)

Input Arguments

Seq1, Seq2

Amino acid or nucleotide sequences. Enter any of the following:

  • Character vector or string of letters representing amino acids or nucleotides, such as returned by int2aa or int2nt

  • Vector of integers representing amino acids or nucleotides, such as returned by aa2int or nt2int

  • Structure containing a Sequence field

Tip

For help with letter and integer representations of amino acids and nucleotides, see Amino Acid Lookup or Nucleotide Lookup.

AlphabetValueCharacter vector or string specifying the type of sequence. Choices are 'AA' (default) or 'NT'.
ScoringMatrixValue

Either of the following:

  • Character vector or string specifying the scoring matrix to use for the global alignment. Choices for amino acid sequences are:

    • 'BLOSUM62'

    • 'BLOSUM30' increasing by 5 up to 'BLOSUM90'

    • 'BLOSUM100'

    • 'PAM10' increasing by 10 up to 'PAM500'

    • 'DAYHOFF'

    • 'GONNET'

    Default is:

    • 'BLOSUM50' — When AlphabetValue equals 'AA'

    • 'NUC44' — When AlphabetValue equals 'NT'

    Note

    The above scoring matrices, provided with the software, also include a structure containing a scale factor that converts the units of the output score to bits. You can also use the 'Scale' property to specify an additional scale factor to convert the output score from bits to another unit.

  • Matrix representing the scoring matrix to use for the global alignment, such as returned by the blosum, pam, dayhoff, gonnet, or nuc44 function.

    Note

    If you use a scoring matrix that you created or was created by one of the above functions, the matrix does not include a scale factor. The output score will be returned in the same units as the scoring matrix. You can use the 'Scale' property to specify a scale factor to convert the output score to another unit.

Note

If you need to compile nwalign into a stand-alone application or software component using MATLAB® Compiler™, use a matrix instead of a character vector or string for ScoringMatrixValue.

ScaleValue

Positive value that specifies a scale factor that is applied to the output score.

For example, if the output score is initially determined in bits, and you enter log(2) for ScaleValue, then nwalign returns Score in nats.

Default is 1, which does not change the units of the output score.

Note

If the 'ScoringMatrix' property also specifies a scale factor, then nwalign uses it first to scale the output score, then applies the scale factor specified by ScaleValue to rescale the output score.

Tip

Before comparing alignment scores from multiple alignments, ensure the scores are in the same units. You can use the 'Scale' property to control the units of the output scores.

GapOpenValuePositive value specifying the penalty for opening a gap in the alignment. Default is 8.
ExtendGapValue

Positive value specifying the penalty for extending a gap using the affine gap penalty scheme.

Note

If you specify this value, nwalign uses the affine gap penalty scheme, that is, it scores the first gap using the GapOpenValue and scores subsequent gaps using the ExtendGapValue. If you do not specify this value, nwalign scores all gaps equally, using the GapOpenValue penalty.

GlocalValueControls the return of a semiglobal or “glocal” alignment. In a semiglobal alignment, gap penalties at the end of the sequences are null. Choices are true or false (default).
ShowscoreValueControls the display of the scoring space and the winning path of the alignment. Choices are true or false (default).

Output Arguments

ScoreOptimal global alignment score in bits.
Alignment3-by-N character array showing the two sequences, Seq1 and Seq2, in the first and third rows, and symbols representing the optimal global alignment for them in the second row.
Start2-by-1 vector of indices indicating the starting point in each sequence for the alignment. Because this is a global alignment, Start is always [1;1].

Description

Score = nwalign(Seq1,Seq2) returns the optimal global alignment score in bits. The scale factor used to calculate the score is provided by the scoring matrix.

[Score, Alignment] = nwalign(Seq1,Seq2) returns a 3-by-N character array showing the two sequences, Seq1 and Seq2, in the first and third rows, and symbols representing the optimal global alignment for them in the second row. The symbol | indicates amino acids or nucleotides that match exactly. The symbol : indicates amino acids or nucleotides that are related as defined by the scoring matrix (nonmatches with a zero or positive scoring matrix value).

[Score, Alignment, Start] = nwalign(Seq1,Seq2) returns a 2-by-1 vector of indices indicating the starting point in each sequence for the alignment. Because this is a global alignment, Start is always [1;1].

... = nwalign(Seq1,Seq2, ...'PropertyName', PropertyValue, ...) calls nwalign with optional properties that use property name/property value pairs. You can specify one or more properties in any order. Each PropertyName must be enclosed in single quotation marks and is case insensitive. These property name/property value pairs are as follows:

... = nwalign(Seq1,Seq2, ...'Alphabet', AlphabetValue, ...) specifies the type of sequences. Choices are 'AA' (default) or 'NT'.

... = nwalign(Seq1,Seq2, ...'ScoringMatrix', ScoringMatrixValue, ...) specifies the scoring matrix to use for the global alignment. Default is:

  • 'BLOSUM50' — When AlphabetValue equals 'AA'

  • 'NUC44' — When AlphabetValue equals 'NT'

... = nwalign(Seq1,Seq2, ...'Scale', ScaleValue, ...) specifies a scale factor that is applied to the output score, thereby controlling the units of the output score. Choices are any positive value.

... = nwalign(Seq1,Seq2, ...'GapOpen', GapOpenValue, ...) specifies the penalty for opening a gap in the alignment. Choices are any positive value. Default is 8.

... = nwalign(Seq1,Seq2, ...'ExtendGap', ExtendGapValue, ...) specifies the penalty for extending a gap using the affine gap penalty scheme. Choices are any positive value.

... = nwalign(Seq1,Seq2, ...'Glocal', GlocalValue, ...) controls the return of a semiglobal or “glocal” alignment. In a semiglobal alignment, gap penalties at the end of the sequences are null. Choices are true or false (default).

... = nwalign(Seq1,Seq2, ...'Showscore', ShowscoreValue, ...) controls the display of the scoring space and winning path of the alignment. Choices are true or false (default).

The scoring space is a heat map displaying the best scores for all the partial alignments of two sequences. The color of each (n1,n2) coordinate in the scoring space represents the best score for the pairing of subsequences Seq1(1:n1) and Seq2(1:n2), where n1 is a position in Seq1 and n2 is a position in Seq2. The best score for a pairing of specific subsequences is determined by scoring all possible alignments of the subsequences by summing matches and gap penalties.

The winning path is represented by black dots in the scoring space, and it illustrates the pairing of positions in the optimal global alignment. The color of the last point (lower right) of the winning path represents the optimal global alignment score for the two sequences and is the Score output returned by nwalign.

Note

The scoring space visually indicates if there are potential alternate winning paths, which is useful when aligning sequences with big gaps. Visual patterns in the scoring space can also indicate a possible sequence rearrangement.

Examples

  1. Globally align two amino acid sequences using the BLOSUM50 (default) scoring matrix and the default values for the GapOpen and ExtendGap properties. Return the optimal global alignment score in bits and the alignment character array.

    [Score, Alignment] = nwalign('VSPAGMASGYD','IPGKASYD')
    Score =
    
        7.3333
    
    Alignment =
    
    VSPAGMASGYD
    : | | || ||
    I-P-GKAS-YD
  2. Globally align two amino acid sequences specifying the PAM250 scoring matrix and a gap open penalty of 5.

    [Score, Alignment] = nwalign('IGRHRYHIGG','SRYIGRG',...
                                 'scoringmatrix','pam250',...
                                 'gapopen',5)
    Score =
    
        2.3333
    
    Alignment =
    
    IGRHRYHIG-G
     :  || || |
    -S--RY-IGRG
    
  3. Globally align two amino acid sequences returning the Score in nat units (nats) by specifying a scale factor of log(2).

    [Score, Alignment] = nwalign('HEAGAWGHEE','PAWHEAE','Scale',log(2))
                                 
    Score =
    
        0.2310
    
    Alignment =
    
    HEAGAWGHE-E
        || || |
    --P-AW-HEAE

References

[1] Durbin, R., Eddy, S., Krogh, A., and Mitchison, G. (1998). Biological Sequence Analysis (Cambridge University Press).

Introduced before R2006a