github.com/biogo/biogo@v1.0.4/align/nw.go (about)

     1  // Copyright ©2011-2012 The bíogo Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package align
     6  
     7  import (
     8  	"github.com/biogo/biogo/alphabet"
     9  	"github.com/biogo/biogo/feat"
    10  )
    11  
    12  // Setting debugNeedle to true gives verbose scoring table output for the dynamic programming.
    13  const debugNeedle = false
    14  
    15  // NW is the linear gap penalty Needleman-Wunsch aligner type.
    16  type NW Linear
    17  
    18  // Align aligns two sequences using the Needleman-Wunsch algorithm. It returns an alignment description
    19  // or an error if the scoring matrix is not square, or the sequence data types or alphabets do not match.
    20  func (a NW) Align(reference, query AlphabetSlicer) ([]feat.Pair, error) {
    21  	alpha := reference.Alphabet()
    22  	if alpha == nil {
    23  		return nil, ErrNoAlphabet
    24  	}
    25  	if alpha != query.Alphabet() {
    26  		return nil, ErrMismatchedAlphabets
    27  	}
    28  	if alpha.IndexOf(alpha.Gap()) != 0 {
    29  		return nil, ErrNotGappedAlphabet
    30  	}
    31  	switch rSeq := reference.Slice().(type) {
    32  	case alphabet.Letters:
    33  		qSeq, ok := query.Slice().(alphabet.Letters)
    34  		if !ok {
    35  			return nil, ErrMismatchedTypes
    36  		}
    37  		return a.alignLetters(rSeq, qSeq, alpha)
    38  	case alphabet.QLetters:
    39  		qSeq, ok := query.Slice().(alphabet.QLetters)
    40  		if !ok {
    41  			return nil, ErrMismatchedTypes
    42  		}
    43  		return a.alignQLetters(rSeq, qSeq, alpha)
    44  	default:
    45  		return nil, ErrTypeNotHandled
    46  	}
    47  }